2
0

wireless.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571
  1. /*
  2. * netifd - network interface daemon
  3. * Copyright (C) 2013 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. /* The wireless configuration is projected on the following objects
  15. *
  16. * 1. wireless device
  17. * 2. wireless interface
  18. * 3. wireless vlan
  19. * 4. wireless station
  20. *
  21. * A wireless device is a phy or simplified a wireless card.
  22. * A wireless interface is a SSID on a phy.
  23. * A wireless vlan can be assigned to a wireless interface. A wireless interface can
  24. * have multiple vlans.
  25. * A wireless station is a client connected to an wireless interface.
  26. */
  27. #include <signal.h>
  28. #include "netifd.h"
  29. #include "wireless.h"
  30. #include "handler.h"
  31. #include "ubus.h"
  32. #define WIRELESS_SETUP_RETRY 3
  33. struct vlist_tree wireless_devices;
  34. struct avl_tree wireless_drivers;
  35. static struct blob_buf b;
  36. static int drv_fd;
  37. static LIST_HEAD(handlers);
  38. static bool handler_pending;
  39. enum {
  40. WDEV_ATTR_DISABLED,
  41. WDEV_ATTR_RECONF,
  42. WDEV_ATTR_SERIALIZE,
  43. __WDEV_ATTR_MAX,
  44. };
  45. static const struct blobmsg_policy wdev_policy[__WDEV_ATTR_MAX] = {
  46. [WDEV_ATTR_DISABLED] = { .name = "disabled", .type = BLOBMSG_TYPE_BOOL },
  47. [WDEV_ATTR_RECONF] = { .name = "reconf", .type = BLOBMSG_TYPE_BOOL },
  48. [WDEV_ATTR_SERIALIZE] = { .name = "serialize", .type = BLOBMSG_TYPE_BOOL },
  49. };
  50. static const struct uci_blob_param_list wdev_param = {
  51. .n_params = ARRAY_SIZE(wdev_policy),
  52. .params = wdev_policy,
  53. };
  54. enum {
  55. VIF_ATTR_DISABLED,
  56. VIF_ATTR_NETWORK,
  57. VIF_ATTR_ISOLATE,
  58. VIF_ATTR_MODE,
  59. VIF_ATTR_PROXYARP,
  60. VIF_ATTR_MCAST_TO_UCAST,
  61. __VIF_ATTR_MAX,
  62. };
  63. static const struct blobmsg_policy vif_policy[__VIF_ATTR_MAX] = {
  64. [VIF_ATTR_DISABLED] = { .name = "disabled", .type = BLOBMSG_TYPE_BOOL },
  65. [VIF_ATTR_NETWORK] = { .name = "network", .type = BLOBMSG_TYPE_ARRAY },
  66. [VIF_ATTR_ISOLATE] = { .name = "isolate", .type = BLOBMSG_TYPE_BOOL },
  67. [VIF_ATTR_MODE] = { .name = "mode", .type = BLOBMSG_TYPE_STRING },
  68. [VIF_ATTR_PROXYARP] = { .name = "proxy_arp", .type = BLOBMSG_TYPE_BOOL },
  69. [VIF_ATTR_MCAST_TO_UCAST] = { .name = "multicast_to_unicast", .type = BLOBMSG_TYPE_BOOL },
  70. };
  71. static const struct uci_blob_param_list vif_param = {
  72. .n_params = ARRAY_SIZE(vif_policy),
  73. .params = vif_policy,
  74. };
  75. enum {
  76. VLAN_ATTR_DISABLED,
  77. VLAN_ATTR_NETWORK,
  78. VLAN_ATTR_ISOLATE,
  79. VLAN_ATTR_MCAST_TO_UCAST,
  80. __VLAN_ATTR_MAX,
  81. };
  82. static const struct blobmsg_policy vlan_policy[__VLAN_ATTR_MAX] = {
  83. [VLAN_ATTR_DISABLED] = { .name = "disabled", .type = BLOBMSG_TYPE_BOOL },
  84. [VLAN_ATTR_NETWORK] = { .name = "network", .type = BLOBMSG_TYPE_ARRAY },
  85. [VLAN_ATTR_ISOLATE] = { .name = "isolate", .type = BLOBMSG_TYPE_BOOL },
  86. [VLAN_ATTR_MCAST_TO_UCAST] = { .name = "multicast_to_unicast", .type = BLOBMSG_TYPE_BOOL },
  87. };
  88. static const struct uci_blob_param_list vlan_param = {
  89. .n_params = ARRAY_SIZE(vlan_policy),
  90. .params = vlan_policy,
  91. };
  92. enum {
  93. STA_ATTR_DISABLED,
  94. __STA_ATTR_MAX,
  95. };
  96. static const struct blobmsg_policy sta_policy[__STA_ATTR_MAX] = {
  97. [STA_ATTR_DISABLED] = { .name = "disabled", .type = BLOBMSG_TYPE_BOOL },
  98. };
  99. static const struct uci_blob_param_list station_param = {
  100. .n_params = ARRAY_SIZE(sta_policy),
  101. .params = sta_policy,
  102. };
  103. static void
  104. wireless_handler_stop(struct wireless_device *wdev)
  105. {
  106. if (wdev->handler_pending) {
  107. wdev->handler_pending = false;
  108. list_del(&wdev->handler);
  109. }
  110. }
  111. static void
  112. put_container(struct blob_buf *buf, struct blob_attr *attr, const char *name)
  113. {
  114. void *c = blobmsg_open_table(buf, name);
  115. blob_put_raw(buf, blob_data(attr), blob_len(attr));
  116. blobmsg_close_table(buf, c);
  117. }
  118. static void
  119. vif_config_add_bridge(struct blob_buf *buf, struct blob_attr *networks, bool prepare)
  120. {
  121. struct interface *iface;
  122. struct device *dev = NULL, *orig_dev;
  123. struct blob_attr *cur;
  124. const char *network;
  125. size_t rem;
  126. if (!networks)
  127. return;
  128. blobmsg_for_each_attr(cur, networks, rem) {
  129. network = blobmsg_data(cur);
  130. iface = vlist_find(&interfaces, network, iface, node);
  131. if (!iface)
  132. continue;
  133. dev = iface->main_dev.dev;
  134. if (!dev)
  135. return;
  136. if (!dev->hotplug_ops)
  137. return;
  138. }
  139. if (!dev)
  140. return;
  141. orig_dev = dev;
  142. if (dev->hotplug_ops && dev->hotplug_ops->prepare)
  143. dev->hotplug_ops->prepare(dev, &dev);
  144. if (!dev || !dev->type->bridge_capability)
  145. return;
  146. blobmsg_add_string(buf, "bridge", dev->ifname);
  147. blobmsg_add_string(buf, "bridge-ifname", orig_dev->ifname);
  148. if (dev->settings.flags & DEV_OPT_MULTICAST_TO_UNICAST)
  149. blobmsg_add_u8(buf, "multicast_to_unicast",
  150. dev->settings.multicast_to_unicast);
  151. }
  152. static void
  153. prepare_config(struct wireless_device *wdev, struct blob_buf *buf, bool up)
  154. {
  155. struct wireless_interface *vif;
  156. struct wireless_vlan *vlan;
  157. struct wireless_station *sta;
  158. void *l, *i, *j, *k;
  159. blob_buf_init(&b, 0);
  160. put_container(&b, wdev->config, "config");
  161. if (wdev->data)
  162. blobmsg_add_blob(&b, wdev->data);
  163. l = blobmsg_open_table(&b, "interfaces");
  164. vlist_for_each_element(&wdev->interfaces, vif, node) {
  165. i = blobmsg_open_table(&b, vif->name);
  166. vif_config_add_bridge(&b, vif->network, up);
  167. put_container(&b, vif->config, "config");
  168. if (vif->data)
  169. blobmsg_add_blob(&b, vif->data);
  170. j = blobmsg_open_table(&b, "vlans");
  171. vlist_for_each_element(&wdev->vlans, vlan, node) {
  172. if (strcmp(vlan->vif, vif->name))
  173. continue;
  174. k = blobmsg_open_table(&b, vlan->name);
  175. vif_config_add_bridge(&b, vlan->network, up);
  176. put_container(&b, vlan->config, "config");
  177. if (vlan->data)
  178. blobmsg_add_blob(&b, vlan->data);
  179. blobmsg_close_table(&b, k);
  180. }
  181. blobmsg_close_table(&b, j);
  182. j = blobmsg_open_table(&b, "stas");
  183. vlist_for_each_element(&wdev->stations, sta, node) {
  184. if (strcmp(sta->vif, vif->name))
  185. continue;
  186. k = blobmsg_open_table(&b, sta->name);
  187. put_container(&b, sta->config, "config");
  188. if (sta->data)
  189. blobmsg_add_blob(&b, sta->data);
  190. blobmsg_close_table(&b, k);
  191. }
  192. blobmsg_close_table(&b, j);
  193. blobmsg_close_table(&b, i);
  194. }
  195. blobmsg_close_table(&b, l);
  196. }
  197. static bool
  198. wireless_process_check(struct wireless_process *proc)
  199. {
  200. return check_pid_path(proc->pid, proc->exe);
  201. }
  202. static void
  203. wireless_complete_kill_request(struct wireless_device *wdev)
  204. {
  205. if (!wdev->kill_request)
  206. return;
  207. ubus_complete_deferred_request(ubus_ctx, wdev->kill_request, 0);
  208. free(wdev->kill_request);
  209. wdev->kill_request = NULL;
  210. }
  211. static void
  212. wireless_process_free(struct wireless_device *wdev, struct wireless_process *proc)
  213. {
  214. D(WIRELESS, "Wireless device '%s' free pid %d\n", wdev->name, proc->pid);
  215. list_del(&proc->list);
  216. free(proc);
  217. if (list_empty(&wdev->script_proc))
  218. wireless_complete_kill_request(wdev);
  219. }
  220. static void
  221. wireless_close_script_proc_fd(struct wireless_device *wdev)
  222. {
  223. if (wdev->script_proc_fd.fd < 0)
  224. return;
  225. uloop_fd_delete(&wdev->script_proc_fd);
  226. close(wdev->script_proc_fd.fd);
  227. wdev->script_proc_fd.fd = -1;
  228. }
  229. static void
  230. wireless_process_kill_all(struct wireless_device *wdev, int signal, bool free)
  231. {
  232. struct wireless_process *proc, *tmp;
  233. list_for_each_entry_safe(proc, tmp, &wdev->script_proc, list) {
  234. bool check = wireless_process_check(proc);
  235. if (check && !proc->keep) {
  236. D(WIRELESS, "Wireless device '%s' kill pid %d\n", wdev->name, proc->pid);
  237. kill(proc->pid, signal);
  238. }
  239. if (free || !check)
  240. wireless_process_free(wdev, proc);
  241. }
  242. if (free)
  243. wireless_close_script_proc_fd(wdev);
  244. }
  245. static void
  246. wireless_device_free_state(struct wireless_device *wdev)
  247. {
  248. struct wireless_interface *vif;
  249. struct wireless_vlan *vlan;
  250. struct wireless_station *sta;
  251. wireless_handler_stop(wdev);
  252. uloop_timeout_cancel(&wdev->script_check);
  253. uloop_timeout_cancel(&wdev->timeout);
  254. wireless_complete_kill_request(wdev);
  255. free(wdev->data);
  256. wdev->data = NULL;
  257. vlist_for_each_element(&wdev->interfaces, vif, node) {
  258. free(vif->data);
  259. vif->data = NULL;
  260. vif->ifname = NULL;
  261. }
  262. vlist_for_each_element(&wdev->vlans, vlan, node) {
  263. free(vlan->data);
  264. vlan->data = NULL;
  265. vlan->ifname = NULL;
  266. }
  267. vlist_for_each_element(&wdev->stations, sta, node) {
  268. free(sta->data);
  269. sta->data = NULL;
  270. }
  271. }
  272. static void wireless_device_set_mcast_to_unicast(struct device *dev, int val)
  273. {
  274. if (val < 0) {
  275. dev->settings.flags &= ~DEV_OPT_MULTICAST_TO_UNICAST;
  276. return;
  277. }
  278. dev->settings.multicast_to_unicast = !!val;
  279. dev->settings.flags |= DEV_OPT_MULTICAST_TO_UNICAST;
  280. }
  281. static void wireless_interface_handle_link(struct wireless_interface *vif, const char *ifname, bool up)
  282. {
  283. struct interface *iface;
  284. struct blob_attr *cur;
  285. const char *network;
  286. size_t rem;
  287. if (!vif->network || !vif->ifname)
  288. return;
  289. if (!ifname)
  290. ifname = vif->ifname;
  291. if (up) {
  292. struct device *dev = __device_get(ifname, 2, false);
  293. if (dev && !strcmp(ifname, vif->ifname)) {
  294. dev->wireless_isolate = vif->isolate;
  295. dev->wireless_proxyarp = vif->proxyarp;
  296. dev->wireless = true;
  297. dev->wireless_ap = vif->ap_mode;
  298. wireless_device_set_mcast_to_unicast(dev, vif->multicast_to_unicast);
  299. dev->bpdu_filter = dev->wireless_ap;
  300. }
  301. }
  302. blobmsg_for_each_attr(cur, vif->network, rem) {
  303. network = blobmsg_data(cur);
  304. iface = vlist_find(&interfaces, network, iface, node);
  305. if (!iface)
  306. continue;
  307. interface_handle_link(iface, ifname, NULL, up, true);
  308. }
  309. }
  310. static void wireless_vlan_handle_link(struct wireless_vlan *vlan, bool up)
  311. {
  312. struct interface *iface;
  313. struct blob_attr *cur;
  314. const char *network;
  315. size_t rem;
  316. if (!vlan->network || !vlan->ifname)
  317. return;
  318. if (up) {
  319. struct device *dev = device_get(vlan->ifname, 2);
  320. if (dev) {
  321. dev->wireless_isolate = vlan->isolate;
  322. dev->wireless = true;
  323. dev->wireless_ap = true;
  324. dev->bpdu_filter = true;
  325. wireless_device_set_mcast_to_unicast(dev, vlan->multicast_to_unicast);
  326. }
  327. }
  328. blobmsg_for_each_attr(cur, vlan->network, rem) {
  329. network = blobmsg_data(cur);
  330. iface = vlist_find(&interfaces, network, iface, node);
  331. if (!iface)
  332. continue;
  333. interface_handle_link(iface, vlan->ifname, NULL, up, true);
  334. }
  335. }
  336. static void
  337. wireless_device_setup_cancel(struct wireless_device *wdev)
  338. {
  339. if (wdev->cancel)
  340. return;
  341. wireless_handler_stop(wdev);
  342. D(WIRELESS, "Cancel wireless device '%s' setup\n", wdev->name);
  343. wdev->cancel = true;
  344. uloop_timeout_set(&wdev->timeout, 10 * 1000);
  345. }
  346. static void
  347. wireless_device_run_handler(struct wireless_device *wdev, bool up)
  348. {
  349. const char *action = up ? "setup" : "teardown";
  350. const char *argv[6];
  351. char *config;
  352. int i = 0;
  353. int fds[2] = { -1, -1 };
  354. wireless_handler_stop(wdev);
  355. if (handler_pending && wdev->serialize) {
  356. wdev->handler_action = up;
  357. wdev->handler_pending = true;
  358. list_add_tail(&wdev->handler, &handlers);
  359. return;
  360. }
  361. if (wdev->serialize)
  362. handler_pending = true;
  363. D(WIRELESS, "Wireless device '%s' run %s handler\n", wdev->name, action);
  364. if (!up && wdev->prev_config) {
  365. config = blobmsg_format_json(wdev->prev_config, true);
  366. free(wdev->prev_config);
  367. wdev->prev_config = NULL;
  368. } else {
  369. prepare_config(wdev, &b, up);
  370. config = blobmsg_format_json(b.head, true);
  371. }
  372. argv[i++] = wdev->drv->script;
  373. argv[i++] = wdev->drv->name;
  374. argv[i++] = action;
  375. argv[i++] = wdev->name;
  376. argv[i++] = config;
  377. argv[i] = NULL;
  378. if (up && pipe(fds) == 0) {
  379. if (wdev->script_proc_fd.fd >= 0)
  380. wireless_close_script_proc_fd(wdev);
  381. wdev->script_proc_fd.fd = fds[0];
  382. uloop_fd_add(&wdev->script_proc_fd,
  383. ULOOP_READ | ULOOP_EDGE_TRIGGER);
  384. }
  385. netifd_start_process(argv, NULL, &wdev->script_task);
  386. if (fds[1] >= 0)
  387. close(fds[1]);
  388. free(config);
  389. }
  390. static void
  391. wireless_handler_next(void)
  392. {
  393. struct wireless_device *wdev;
  394. if (handler_pending)
  395. return;
  396. if (list_empty(&handlers))
  397. return;
  398. wdev = list_first_entry(&handlers, struct wireless_device, handler);
  399. list_del(&wdev->handler);
  400. wdev->handler_pending = false;
  401. wireless_device_run_handler(wdev, wdev->handler_action);
  402. }
  403. static void
  404. __wireless_device_set_up(struct wireless_device *wdev, int force)
  405. {
  406. if (wdev->disabled)
  407. return;
  408. if (wdev->retry_setup_failed)
  409. return;
  410. if (!wdev->autostart)
  411. return;
  412. if (!force && (wdev->state != IFS_DOWN || config_init))
  413. return;
  414. free(wdev->prev_config);
  415. wdev->prev_config = NULL;
  416. wdev->state = IFS_SETUP;
  417. wireless_device_run_handler(wdev, true);
  418. }
  419. static void
  420. wireless_device_free(struct wireless_device *wdev)
  421. {
  422. wireless_handler_stop(wdev);
  423. vlist_flush_all(&wdev->interfaces);
  424. vlist_flush_all(&wdev->vlans);
  425. vlist_flush_all(&wdev->stations);
  426. avl_delete(&wireless_devices.avl, &wdev->node.avl);
  427. free(wdev->config);
  428. free(wdev->prev_config);
  429. free(wdev);
  430. }
  431. static void
  432. wdev_handle_config_change(struct wireless_device *wdev)
  433. {
  434. enum interface_config_state state = wdev->config_state;
  435. switch(state) {
  436. case IFC_RELOAD:
  437. wdev->retry = WIRELESS_SETUP_RETRY;
  438. wdev->retry_setup_failed = false;
  439. fallthrough;
  440. case IFC_NORMAL:
  441. __wireless_device_set_up(wdev, 0);
  442. wdev->config_state = IFC_NORMAL;
  443. break;
  444. case IFC_REMOVE:
  445. wireless_device_free(wdev);
  446. break;
  447. }
  448. }
  449. static void
  450. wireless_device_mark_down(struct wireless_device *wdev)
  451. {
  452. struct wireless_interface *vif;
  453. struct wireless_vlan *vlan;
  454. netifd_log_message(L_NOTICE, "Wireless device '%s' is now down\n", wdev->name);
  455. vlist_for_each_element(&wdev->vlans, vlan, node)
  456. wireless_vlan_handle_link(vlan, false);
  457. vlist_for_each_element(&wdev->interfaces, vif, node)
  458. wireless_interface_handle_link(vif, NULL, false);
  459. wireless_process_kill_all(wdev, SIGTERM, true);
  460. wdev->cancel = false;
  461. wdev->state = IFS_DOWN;
  462. wireless_device_free_state(wdev);
  463. wdev_handle_config_change(wdev);
  464. }
  465. /* timeout callback to protect the tear down */
  466. static void
  467. wireless_device_setup_timeout(struct uloop_timeout *timeout)
  468. {
  469. struct wireless_device *wdev = container_of(timeout, struct wireless_device, timeout);
  470. if (wdev->handler_pending) {
  471. wdev->handler_pending = false;
  472. list_del(&wdev->handler);
  473. }
  474. netifd_kill_process(&wdev->script_task);
  475. wdev->script_task.cb(&wdev->script_task, -1);
  476. wireless_device_mark_down(wdev);
  477. }
  478. void
  479. wireless_device_set_up(struct wireless_device *wdev)
  480. {
  481. wdev->retry = WIRELESS_SETUP_RETRY;
  482. wdev->autostart = true;
  483. __wireless_device_set_up(wdev, 0);
  484. }
  485. void
  486. wireless_device_reconf(struct wireless_device *wdev)
  487. {
  488. wdev->retry = WIRELESS_SETUP_RETRY;
  489. wdev->autostart = true;
  490. __wireless_device_set_up(wdev, wdev->reconf && (wdev->state == IFS_UP));
  491. }
  492. static void
  493. __wireless_device_set_down(struct wireless_device *wdev)
  494. {
  495. if (wdev->state == IFS_TEARDOWN || wdev->state == IFS_DOWN)
  496. return;
  497. if (wdev->script_task.uloop.pending) {
  498. wireless_device_setup_cancel(wdev);
  499. return;
  500. }
  501. wdev->state = IFS_TEARDOWN;
  502. wireless_device_run_handler(wdev, false);
  503. }
  504. /* ubus callback network.wireless.notify, command = up */
  505. static void
  506. wireless_device_mark_up(struct wireless_device *wdev)
  507. {
  508. struct wireless_interface *vif;
  509. struct wireless_vlan *vlan;
  510. if (wdev->cancel) {
  511. wdev->cancel = false;
  512. __wireless_device_set_down(wdev);
  513. return;
  514. }
  515. netifd_log_message(L_NOTICE, "Wireless device '%s' is now up\n", wdev->name);
  516. wdev->retry = WIRELESS_SETUP_RETRY;
  517. wdev->state = IFS_UP;
  518. vlist_for_each_element(&wdev->interfaces, vif, node)
  519. wireless_interface_handle_link(vif, NULL, true);
  520. vlist_for_each_element(&wdev->vlans, vlan, node)
  521. wireless_vlan_handle_link(vlan, true);
  522. }
  523. static void
  524. wireless_device_retry_setup(struct wireless_device *wdev)
  525. {
  526. if (wdev->state == IFS_TEARDOWN || wdev->state == IFS_DOWN || wdev->cancel)
  527. return;
  528. netifd_log_message(wdev->retry ? L_WARNING : L_CRIT,
  529. "Wireless device '%s' setup failed, retry=%d\n",
  530. wdev->name, wdev->retry);
  531. if (--wdev->retry < 0)
  532. wdev->retry_setup_failed = true;
  533. __wireless_device_set_down(wdev);
  534. }
  535. static void
  536. wireless_device_script_task_cb(struct netifd_process *proc, int ret)
  537. {
  538. struct wireless_device *wdev = container_of(proc, struct wireless_device, script_task);
  539. switch (wdev->state) {
  540. case IFS_SETUP:
  541. wireless_device_retry_setup(wdev);
  542. break;
  543. case IFS_TEARDOWN:
  544. wireless_device_mark_down(wdev);
  545. break;
  546. default:
  547. break;
  548. }
  549. if (wdev->serialize) {
  550. handler_pending = false;
  551. wireless_handler_next();
  552. }
  553. }
  554. void
  555. wireless_device_set_down(struct wireless_device *wdev)
  556. {
  557. wdev->retry_setup_failed = false;
  558. wdev->autostart = false;
  559. __wireless_device_set_down(wdev);
  560. }
  561. static void
  562. wdev_set_config_state(struct wireless_device *wdev, enum interface_config_state s)
  563. {
  564. if (wdev->config_state != IFC_NORMAL)
  565. return;
  566. wdev->config_state = s;
  567. if (wdev->state == IFS_DOWN)
  568. wdev_handle_config_change(wdev);
  569. else if (!wdev->reconf || wdev->state != IFS_UP)
  570. __wireless_device_set_down(wdev);
  571. }
  572. static void
  573. wdev_prepare_prev_config(struct wireless_device *wdev)
  574. {
  575. if (wdev->prev_config)
  576. return;
  577. prepare_config(wdev, &b, false);
  578. wdev->prev_config = blob_memdup(b.head);
  579. }
  580. static void
  581. wdev_change_config(struct wireless_device *wdev, struct wireless_device *wd_new)
  582. {
  583. struct blob_attr *new_config = wd_new->config;
  584. bool disabled = wd_new->disabled;
  585. free(wd_new);
  586. wdev_prepare_prev_config(wdev);
  587. if (blob_attr_equal(wdev->config, new_config) && wdev->disabled == disabled)
  588. return;
  589. D(WIRELESS, "Update configuration of wireless device '%s'\n", wdev->name);
  590. free(wdev->config);
  591. wdev->config = blob_memdup(new_config);
  592. wdev->disabled = disabled;
  593. wdev_set_config_state(wdev, IFC_RELOAD);
  594. }
  595. static void
  596. wdev_create(struct wireless_device *wdev)
  597. {
  598. wdev->retry = WIRELESS_SETUP_RETRY;
  599. wdev->config = blob_memdup(wdev->config);
  600. }
  601. /* vlist update call for wireless device list */
  602. static void
  603. wdev_update(struct vlist_tree *tree, struct vlist_node *node_new,
  604. struct vlist_node *node_old)
  605. {
  606. struct wireless_device *wd_old = container_of(node_old, struct wireless_device, node);
  607. struct wireless_device *wd_new = container_of(node_new, struct wireless_device, node);
  608. if (wd_old && wd_new) {
  609. D(WIRELESS, "Update wireless device '%s'\n", wd_old->name);
  610. wdev_change_config(wd_old, wd_new);
  611. } else if (wd_old) {
  612. D(WIRELESS, "Delete wireless device '%s'\n", wd_old->name);
  613. wdev_set_config_state(wd_old, IFC_REMOVE);
  614. } else if (wd_new) {
  615. D(WIRELESS, "Create wireless device '%s'\n", wd_new->name);
  616. wdev_create(wd_new);
  617. }
  618. }
  619. /* wireless netifd script handler */
  620. static void
  621. wireless_add_handler(const char *script, const char *name, json_object *obj)
  622. {
  623. struct wireless_driver *drv;
  624. char *name_str, *script_str;
  625. json_object *dev_config_obj, *iface_config_obj, *vlan_config_obj, *station_config_obj;
  626. struct uci_blob_param_list *dev_config, *iface_config, *vlan_config, *station_config;
  627. dev_config_obj = json_get_field(obj, "device", json_type_array);
  628. iface_config_obj = json_get_field(obj, "iface", json_type_array);
  629. vlan_config_obj = json_get_field(obj, "vlan", json_type_array);
  630. station_config_obj = json_get_field(obj, "station", json_type_array);
  631. if (!dev_config_obj || !iface_config_obj || !vlan_config_obj || !station_config_obj)
  632. return;
  633. drv = calloc_a(sizeof(*drv),
  634. &dev_config, sizeof(*dev_config) + sizeof(void *),
  635. &iface_config, sizeof(*iface_config) + sizeof(void *),
  636. &vlan_config, sizeof(*vlan_config) + sizeof(void *),
  637. &station_config, sizeof(*station_config) + sizeof(void *),
  638. &name_str, strlen(name) + 1,
  639. &script_str, strlen(script) + 1);
  640. drv->name = strcpy(name_str, name);
  641. drv->script = strcpy(script_str, script);
  642. dev_config->n_next = 1;
  643. dev_config->next[0] = &wdev_param;
  644. drv->device.config = dev_config;
  645. iface_config->n_next = 1;
  646. iface_config->next[0] = &vif_param;
  647. drv->interface.config = iface_config;
  648. vlan_config->n_next = 1;
  649. vlan_config->next[0] = &vlan_param;
  650. drv->vlan.config = vlan_config;
  651. station_config->n_next = 1;
  652. station_config->next[0] = &station_param;
  653. drv->station.config = station_config;
  654. drv->device.buf = netifd_handler_parse_config(drv->device.config, dev_config_obj);
  655. drv->interface.buf = netifd_handler_parse_config(drv->interface.config, iface_config_obj);
  656. drv->vlan.buf = netifd_handler_parse_config(drv->vlan.config, vlan_config_obj);
  657. drv->station.buf = netifd_handler_parse_config(drv->station.config, station_config_obj);
  658. drv->node.key = drv->name;
  659. avl_insert(&wireless_drivers, &drv->node);
  660. D(WIRELESS, "Add handler for script %s: %s\n", script, name);
  661. }
  662. void wireless_init(void)
  663. {
  664. vlist_init(&wireless_devices, avl_strcmp, wdev_update);
  665. wireless_devices.keep_old = true;
  666. wireless_devices.no_delete = true;
  667. avl_init(&wireless_drivers, avl_strcmp, false, NULL);
  668. drv_fd = netifd_open_subdir("wireless");
  669. if (drv_fd < 0)
  670. return;
  671. netifd_init_script_handlers(drv_fd, wireless_add_handler);
  672. }
  673. /* parse blob config into the wireless interface object */
  674. static void
  675. wireless_interface_init_config(struct wireless_interface *vif)
  676. {
  677. struct blob_attr *tb[__VIF_ATTR_MAX];
  678. struct blob_attr *cur;
  679. vif->network = NULL;
  680. blobmsg_parse(vif_policy, __VIF_ATTR_MAX, tb, blob_data(vif->config), blob_len(vif->config));
  681. if ((cur = tb[VIF_ATTR_NETWORK]))
  682. vif->network = cur;
  683. cur = tb[VIF_ATTR_MODE];
  684. vif->ap_mode = cur && !strcmp(blobmsg_get_string(cur), "ap");
  685. cur = tb[VIF_ATTR_ISOLATE];
  686. vif->isolate = vif->ap_mode && cur && blobmsg_get_bool(cur);
  687. cur = tb[VIF_ATTR_PROXYARP];
  688. vif->proxyarp = vif->ap_mode && cur && blobmsg_get_bool(cur);
  689. cur = tb[VIF_ATTR_MCAST_TO_UCAST];
  690. vif->multicast_to_unicast = cur ? blobmsg_get_bool(cur) : -1;
  691. }
  692. /* vlist update call for wireless interface list */
  693. static void
  694. vif_update(struct vlist_tree *tree, struct vlist_node *node_new,
  695. struct vlist_node *node_old)
  696. {
  697. struct wireless_interface *vif_old = container_of(node_old, struct wireless_interface, node);
  698. struct wireless_interface *vif_new = container_of(node_new, struct wireless_interface, node);
  699. struct wireless_device *wdev;
  700. if (vif_old)
  701. wdev = vif_old->wdev;
  702. else
  703. wdev = vif_new->wdev;
  704. if (vif_old && vif_new) {
  705. free((void *) vif_old->section);
  706. vif_old->section = strdup(vif_new->section);
  707. if (blob_attr_equal(vif_old->config, vif_new->config)) {
  708. free(vif_new);
  709. return;
  710. }
  711. D(WIRELESS, "Update wireless interface %s on device %s\n", vif_new->name, wdev->name);
  712. wireless_interface_handle_link(vif_old, NULL, false);
  713. free(vif_old->config);
  714. vif_old->config = blob_memdup(vif_new->config);
  715. wireless_interface_init_config(vif_old);
  716. free(vif_new);
  717. } else if (vif_new) {
  718. D(WIRELESS, "Create new wireless interface %s on device %s\n", vif_new->name, wdev->name);
  719. vif_new->section = strdup(vif_new->section);
  720. vif_new->config = blob_memdup(vif_new->config);
  721. wireless_interface_init_config(vif_new);
  722. } else if (vif_old) {
  723. D(WIRELESS, "Delete wireless interface %s on device %s\n", vif_old->name, wdev->name);
  724. wireless_interface_handle_link(vif_old, NULL, false);
  725. free((void *) vif_old->section);
  726. free(vif_old->config);
  727. free(vif_old);
  728. }
  729. wdev_set_config_state(wdev, IFC_RELOAD);
  730. }
  731. /* parse blob config into the vlan object */
  732. static void
  733. wireless_vlan_init_config(struct wireless_vlan *vlan)
  734. {
  735. struct blob_attr *tb[__VLAN_ATTR_MAX];
  736. struct blob_attr *cur;
  737. vlan->network = NULL;
  738. blobmsg_parse(vlan_policy, __VLAN_ATTR_MAX, tb, blob_data(vlan->config), blob_len(vlan->config));
  739. if ((cur = tb[VLAN_ATTR_NETWORK]))
  740. vlan->network = cur;
  741. cur = tb[VLAN_ATTR_ISOLATE];
  742. if (cur)
  743. vlan->isolate = blobmsg_get_bool(cur);
  744. cur = tb[VLAN_ATTR_MCAST_TO_UCAST];
  745. vlan->multicast_to_unicast = cur ? blobmsg_get_bool(cur) : -1;
  746. }
  747. /* vlist update call for vlan list */
  748. static void
  749. vlan_update(struct vlist_tree *tree, struct vlist_node *node_new,
  750. struct vlist_node *node_old)
  751. {
  752. struct wireless_vlan *vlan_old = container_of(node_old, struct wireless_vlan, node);
  753. struct wireless_vlan *vlan_new = container_of(node_new, struct wireless_vlan, node);
  754. struct wireless_device *wdev;
  755. if (vlan_old)
  756. wdev = vlan_old->wdev;
  757. else
  758. wdev = vlan_new->wdev;
  759. if (vlan_old && vlan_new) {
  760. free((void *) vlan_old->section);
  761. vlan_old->section = strdup(vlan_new->section);
  762. if (blob_attr_equal(vlan_old->config, vlan_new->config)) {
  763. free(vlan_new);
  764. return;
  765. }
  766. D(WIRELESS, "Update wireless vlan %s on device %s\n", vlan_new->name, wdev->name);
  767. wireless_vlan_handle_link(vlan_old, false);
  768. free(vlan_old->config);
  769. vlan_old->config = blob_memdup(vlan_new->config);
  770. vlan_old->isolate = vlan_new->isolate;
  771. wireless_vlan_init_config(vlan_old);
  772. free(vlan_new);
  773. } else if (vlan_new) {
  774. D(WIRELESS, "Create new wireless vlan %s on device %s\n", vlan_new->name, wdev->name);
  775. vlan_new->section = strdup(vlan_new->section);
  776. vlan_new->config = blob_memdup(vlan_new->config);
  777. wireless_vlan_init_config(vlan_new);
  778. } else if (vlan_old) {
  779. D(WIRELESS, "Delete wireless interface %s on device %s\n", vlan_old->name, wdev->name);
  780. wireless_vlan_handle_link(vlan_old, false);
  781. free((void *) vlan_old->section);
  782. free(vlan_old->config);
  783. free(vlan_old);
  784. }
  785. wdev_set_config_state(wdev, IFC_RELOAD);
  786. }
  787. /* vlist update call for station list */
  788. static void
  789. station_update(struct vlist_tree *tree, struct vlist_node *node_new,
  790. struct vlist_node *node_old)
  791. {
  792. struct wireless_station *sta_old = container_of(node_old, struct wireless_station, node);
  793. struct wireless_station *sta_new = container_of(node_new, struct wireless_station, node);
  794. struct wireless_device *wdev;
  795. if (sta_old)
  796. wdev = sta_old->wdev;
  797. else
  798. wdev = sta_new->wdev;
  799. if (sta_old && sta_new) {
  800. free((void *) sta_old->section);
  801. sta_old->section = strdup(sta_new->section);
  802. if (blob_attr_equal(sta_old->config, sta_new->config)) {
  803. free(sta_new);
  804. return;
  805. }
  806. D(WIRELESS, "Update wireless station %s on device %s\n", sta_new->name, wdev->name);
  807. free(sta_old->config);
  808. sta_old->config = blob_memdup(sta_new->config);
  809. free(sta_new);
  810. } else if (sta_new) {
  811. D(WIRELESS, "Create new wireless station %s on device %s\n", sta_new->name, wdev->name);
  812. sta_new->section = strdup(sta_new->section);
  813. sta_new->config = blob_memdup(sta_new->config);
  814. } else if (sta_old) {
  815. D(WIRELESS, "Delete wireless station %s on device %s\n", sta_old->name, wdev->name);
  816. free((void *) sta_old->section);
  817. free(sta_old->config);
  818. free(sta_old);
  819. }
  820. wdev_set_config_state(wdev, IFC_RELOAD);
  821. }
  822. static void
  823. wireless_proc_poll_fd(struct uloop_fd *fd, unsigned int events)
  824. {
  825. struct wireless_device *wdev = container_of(fd, struct wireless_device, script_proc_fd);
  826. char buf[128];
  827. while (1) {
  828. int b = read(fd->fd, buf, sizeof(buf));
  829. if (b < 0) {
  830. if (errno == EINTR)
  831. continue;
  832. if (errno == EAGAIN)
  833. return;
  834. goto done;
  835. }
  836. if (!b)
  837. goto done;
  838. }
  839. done:
  840. uloop_timeout_set(&wdev->script_check, 0);
  841. wireless_close_script_proc_fd(wdev);
  842. }
  843. /* watchdog and garbage collector for wireless processes.
  844. * It cleans up terminated processes. If a process is a requirement for the wireless device, it retries the setup */
  845. static void
  846. wireless_device_check_script_tasks(struct uloop_timeout *timeout)
  847. {
  848. struct wireless_device *wdev = container_of(timeout, struct wireless_device, script_check);
  849. struct wireless_process *proc, *tmp;
  850. bool restart = false;
  851. list_for_each_entry_safe(proc, tmp, &wdev->script_proc, list) {
  852. if (wireless_process_check(proc))
  853. continue;
  854. D(WIRELESS, "Wireless device '%s' pid %d has terminated\n", wdev->name, proc->pid);
  855. if (proc->required)
  856. restart = true;
  857. wireless_process_free(wdev, proc);
  858. }
  859. if (restart)
  860. wireless_device_retry_setup(wdev);
  861. else
  862. uloop_timeout_set(&wdev->script_check, 1000);
  863. }
  864. /* creates a wireless device object. Called by config */
  865. void
  866. wireless_device_create(struct wireless_driver *drv, const char *name, struct blob_attr *data)
  867. {
  868. struct wireless_device *wdev;
  869. char *name_buf;
  870. struct blob_attr *tb[__WDEV_ATTR_MAX];
  871. struct blob_attr *cur;
  872. blobmsg_parse(wdev_policy, __WDEV_ATTR_MAX, tb, blob_data(data), blob_len(data));
  873. wdev = calloc_a(sizeof(*wdev), &name_buf, strlen(name) + 1);
  874. cur = tb[WDEV_ATTR_DISABLED];
  875. wdev->disabled = cur && blobmsg_get_bool(cur);
  876. wdev->drv = drv;
  877. wdev->state = IFS_DOWN;
  878. wdev->config_state = IFC_NORMAL;
  879. wdev->name = strcpy(name_buf, name);
  880. wdev->config = data;
  881. wdev->handler_pending = false;
  882. cur = tb[WDEV_ATTR_SERIALIZE];
  883. wdev->serialize = cur && blobmsg_get_bool(cur);
  884. cur = tb[WDEV_ATTR_RECONF];
  885. wdev->reconf = cur && blobmsg_get_bool(cur);
  886. wdev->retry_setup_failed = false;
  887. wdev->autostart = true;
  888. INIT_LIST_HEAD(&wdev->script_proc);
  889. vlist_init(&wdev->interfaces, avl_strcmp, vif_update);
  890. wdev->interfaces.keep_old = true;
  891. vlist_init(&wdev->vlans, avl_strcmp, vlan_update);
  892. wdev->vlans.keep_old = true;
  893. vlist_init(&wdev->stations, avl_strcmp, station_update);
  894. wdev->stations.keep_old = true;
  895. wdev->timeout.cb = wireless_device_setup_timeout;
  896. wdev->script_task.cb = wireless_device_script_task_cb;
  897. wdev->script_task.dir_fd = drv_fd;
  898. wdev->script_task.log_prefix = wdev->name;
  899. wdev->script_proc_fd.fd = -1;
  900. wdev->script_proc_fd.cb = wireless_proc_poll_fd;
  901. wdev->script_check.cb = wireless_device_check_script_tasks;
  902. vlist_add(&wireless_devices, &wdev->node, wdev->name);
  903. }
  904. /* creates a wireless station object. Called by config */
  905. void
  906. wireless_station_create(struct wireless_device *wdev, char *vif, struct blob_attr *data, const char *section)
  907. {
  908. struct wireless_station *sta;
  909. struct blob_attr *tb[__STA_ATTR_MAX];
  910. struct blob_attr *cur;
  911. char *name_buf, *vif_buf;
  912. char name[8];
  913. blobmsg_parse(sta_policy, __STA_ATTR_MAX, tb, blob_data(data), blob_len(data));
  914. cur = tb[STA_ATTR_DISABLED];
  915. if (cur && blobmsg_get_bool(cur))
  916. return;
  917. sprintf(name, "%d", wdev->sta_idx++);
  918. sta = calloc_a(sizeof(*sta),
  919. &name_buf, strlen(name) + 1,
  920. &vif_buf, strlen(vif) + 1);
  921. sta->name = strcpy(name_buf, name);
  922. sta->vif = strcpy(vif_buf, vif);
  923. sta->wdev = wdev;
  924. sta->config = data;
  925. sta->section = section;
  926. vlist_add(&wdev->stations, &sta->node, sta->name);
  927. }
  928. /* ubus callback network.wireless.status, runs for every interface, encode the station */
  929. static void
  930. wireless_station_status(struct wireless_station *sta, struct blob_buf *b)
  931. {
  932. void *i;
  933. i = blobmsg_open_table(b, NULL);
  934. if (sta->section)
  935. blobmsg_add_string(b, "section", sta->section);
  936. put_container(b, sta->config, "config");
  937. blobmsg_close_table(b, i);
  938. }
  939. /* create a vlan object. Called by config */
  940. void
  941. wireless_vlan_create(struct wireless_device *wdev, char *vif, struct blob_attr *data, const char *section)
  942. {
  943. struct wireless_vlan *vlan;
  944. struct blob_attr *tb[__VLAN_ATTR_MAX];
  945. struct blob_attr *cur;
  946. char *name_buf, *vif_buf;
  947. char name[8];
  948. blobmsg_parse(vlan_policy, __VLAN_ATTR_MAX, tb, blob_data(data), blob_len(data));
  949. cur = tb[VLAN_ATTR_DISABLED];
  950. if (cur && blobmsg_get_bool(cur))
  951. return;
  952. sprintf(name, "%d", wdev->vlan_idx++);
  953. vlan = calloc_a(sizeof(*vlan),
  954. &name_buf, strlen(name) + 1,
  955. &vif_buf, strlen(vif) + 1);
  956. vlan->name = strcpy(name_buf, name);
  957. vlan->vif = strcpy(vif_buf, vif);
  958. vlan->wdev = wdev;
  959. vlan->config = data;
  960. vlan->section = section;
  961. vlan->isolate = false;
  962. vlist_add(&wdev->vlans, &vlan->node, vlan->name);
  963. }
  964. /* ubus callback network.wireless.status, runs for every interface, encode the vlan informations */
  965. static void
  966. wireless_vlan_status(struct wireless_vlan *vlan, struct blob_buf *b)
  967. {
  968. void *i;
  969. i = blobmsg_open_table(b, NULL);
  970. if (vlan->section)
  971. blobmsg_add_string(b, "section", vlan->section);
  972. if (vlan->ifname)
  973. blobmsg_add_string(b, "ifname", vlan->ifname);
  974. put_container(b, vlan->config, "config");
  975. blobmsg_close_table(b, i);
  976. }
  977. /* create a wireless interface object. Called by config */
  978. struct wireless_interface* wireless_interface_create(struct wireless_device *wdev, struct blob_attr *data, const char *section)
  979. {
  980. struct wireless_interface *vif;
  981. struct blob_attr *tb[__VIF_ATTR_MAX];
  982. struct blob_attr *cur;
  983. char *name_buf;
  984. char name[8];
  985. blobmsg_parse(vif_policy, __VIF_ATTR_MAX, tb, blob_data(data), blob_len(data));
  986. cur = tb[VIF_ATTR_DISABLED];
  987. if (cur && blobmsg_get_bool(cur))
  988. return NULL;
  989. sprintf(name, "%d", wdev->vif_idx++);
  990. vif = calloc_a(sizeof(*vif),
  991. &name_buf, strlen(name) + 1);
  992. vif->name = strcpy(name_buf, name);
  993. vif->wdev = wdev;
  994. vif->config = data;
  995. vif->section = section;
  996. vif->isolate = false;
  997. vlist_add(&wdev->interfaces, &vif->node, vif->name);
  998. return vlist_find(&wdev->interfaces, name, vif, node);
  999. }
  1000. /* ubus callback network.wireless.status, runs for every interface */
  1001. static void
  1002. wireless_interface_status(struct wireless_interface *iface, struct blob_buf *b)
  1003. {
  1004. struct wireless_vlan *vlan;
  1005. struct wireless_station *sta;
  1006. void *i, *j;
  1007. i = blobmsg_open_table(b, NULL);
  1008. if (iface->section)
  1009. blobmsg_add_string(b, "section", iface->section);
  1010. if (iface->ifname)
  1011. blobmsg_add_string(b, "ifname", iface->ifname);
  1012. put_container(b, iface->config, "config");
  1013. j = blobmsg_open_array(b, "vlans");
  1014. vlist_for_each_element(&iface->wdev->vlans, vlan, node)
  1015. if (!strcmp(iface->name, vlan->vif))
  1016. wireless_vlan_status(vlan, b);
  1017. blobmsg_close_array(b, j);
  1018. j = blobmsg_open_array(b, "stations");
  1019. vlist_for_each_element(&iface->wdev->stations, sta, node)
  1020. if (!strcmp(iface->name, sta->vif))
  1021. wireless_station_status(sta, b);
  1022. blobmsg_close_array(b, j);
  1023. blobmsg_close_table(b, i);
  1024. }
  1025. /* ubus callback network.wireless.status */
  1026. void
  1027. wireless_device_status(struct wireless_device *wdev, struct blob_buf *b)
  1028. {
  1029. struct wireless_interface *iface;
  1030. void *c, *i;
  1031. c = blobmsg_open_table(b, wdev->name);
  1032. blobmsg_add_u8(b, "up", wdev->state == IFS_UP);
  1033. blobmsg_add_u8(b, "pending", wdev->state == IFS_SETUP || wdev->state == IFS_TEARDOWN);
  1034. blobmsg_add_u8(b, "autostart", wdev->autostart);
  1035. blobmsg_add_u8(b, "disabled", wdev->disabled);
  1036. blobmsg_add_u8(b, "retry_setup_failed", wdev->retry_setup_failed);
  1037. put_container(b, wdev->config, "config");
  1038. i = blobmsg_open_array(b, "interfaces");
  1039. vlist_for_each_element(&wdev->interfaces, iface, node)
  1040. wireless_interface_status(iface, b);
  1041. blobmsg_close_array(b, i);
  1042. blobmsg_close_table(b, c);
  1043. }
  1044. /* ubus callback network.wireless.get_validate */
  1045. void
  1046. wireless_device_get_validate(struct wireless_device *wdev, struct blob_buf *b)
  1047. {
  1048. struct uci_blob_param_list *p;
  1049. void *c, *d;
  1050. int i;
  1051. c = blobmsg_open_table(b, wdev->name);
  1052. d = blobmsg_open_table(b, "device");
  1053. p = wdev->drv->device.config;
  1054. for (i = 0; i < p->n_params; i++)
  1055. blobmsg_add_string(b, p->params[i].name, uci_get_validate_string(p, i));
  1056. blobmsg_close_table(b, d);
  1057. d = blobmsg_open_table(b, "interface");
  1058. p = wdev->drv->interface.config;
  1059. for (i = 0; i < p->n_params; i++)
  1060. blobmsg_add_string(b, p->params[i].name, uci_get_validate_string(p, i));
  1061. blobmsg_close_table(b, d);
  1062. blobmsg_close_table(b, c);
  1063. }
  1064. /* ubus callback network.wireless.notify, command = set data, for vif */
  1065. static void
  1066. wireless_interface_set_data(struct wireless_interface *vif)
  1067. {
  1068. enum {
  1069. VIF_DATA_IFNAME,
  1070. __VIF_DATA_MAX,
  1071. };
  1072. static const struct blobmsg_policy data_policy[__VIF_DATA_MAX] = {
  1073. [VIF_DATA_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
  1074. };
  1075. struct blob_attr *tb[__VIF_DATA_MAX];
  1076. struct blob_attr *cur;
  1077. blobmsg_parse(data_policy, __VIF_DATA_MAX, tb,
  1078. blobmsg_data(vif->data), blobmsg_data_len(vif->data));
  1079. if ((cur = tb[VIF_DATA_IFNAME]))
  1080. vif->ifname = blobmsg_data(cur);
  1081. }
  1082. /* ubus callback network.wireless.notify, command = set data, for vlan */
  1083. static void
  1084. wireless_vlan_set_data(struct wireless_vlan *vlan)
  1085. {
  1086. enum {
  1087. VLAN_DATA_IFNAME,
  1088. __VLAN_DATA_MAX,
  1089. };
  1090. static const struct blobmsg_policy data_policy[__VLAN_DATA_MAX] = {
  1091. [VLAN_DATA_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
  1092. };
  1093. struct blob_attr *tb[__VLAN_DATA_MAX];
  1094. struct blob_attr *cur;
  1095. blobmsg_parse(data_policy, __VLAN_DATA_MAX, tb,
  1096. blobmsg_data(vlan->data), blobmsg_data_len(vlan->data));
  1097. if ((cur = tb[VLAN_DATA_IFNAME]))
  1098. vlan->ifname = blobmsg_data(cur);
  1099. }
  1100. /* ubus callback network.wireless.notify, command = process add */
  1101. static int
  1102. wireless_device_add_process(struct wireless_device *wdev, struct blob_attr *data)
  1103. {
  1104. enum {
  1105. PROC_ATTR_PID,
  1106. PROC_ATTR_EXE,
  1107. PROC_ATTR_REQUIRED,
  1108. PROC_ATTR_KEEP,
  1109. __PROC_ATTR_MAX
  1110. };
  1111. static const struct blobmsg_policy proc_policy[__PROC_ATTR_MAX] = {
  1112. [PROC_ATTR_PID] = { .name = "pid", .type = BLOBMSG_TYPE_INT32 },
  1113. [PROC_ATTR_EXE] = { .name = "exe", .type = BLOBMSG_TYPE_STRING },
  1114. [PROC_ATTR_REQUIRED] = { .name = "required", .type = BLOBMSG_TYPE_BOOL },
  1115. [PROC_ATTR_KEEP] = { .name = "keep", .type = BLOBMSG_TYPE_BOOL },
  1116. };
  1117. struct blob_attr *tb[__PROC_ATTR_MAX];
  1118. struct wireless_process *proc;
  1119. char *name;
  1120. int pid;
  1121. if (!data)
  1122. return UBUS_STATUS_INVALID_ARGUMENT;
  1123. blobmsg_parse(proc_policy, __PROC_ATTR_MAX, tb, blobmsg_data(data), blobmsg_data_len(data));
  1124. if (!tb[PROC_ATTR_PID] || !tb[PROC_ATTR_EXE])
  1125. return UBUS_STATUS_INVALID_ARGUMENT;
  1126. pid = blobmsg_get_u32(tb[PROC_ATTR_PID]);
  1127. if (pid < 2)
  1128. return UBUS_STATUS_INVALID_ARGUMENT;
  1129. proc = calloc_a(sizeof(*proc),
  1130. &name, strlen(blobmsg_data(tb[PROC_ATTR_EXE])) + 1);
  1131. proc->pid = pid;
  1132. proc->exe = strcpy(name, blobmsg_data(tb[PROC_ATTR_EXE]));
  1133. if (tb[PROC_ATTR_REQUIRED])
  1134. proc->required = blobmsg_get_bool(tb[PROC_ATTR_REQUIRED]);
  1135. if (tb[PROC_ATTR_KEEP])
  1136. proc->keep = blobmsg_get_bool(tb[PROC_ATTR_KEEP]);
  1137. D(WIRELESS, "Wireless device '%s' add pid %d\n", wdev->name, proc->pid);
  1138. list_add(&proc->list, &wdev->script_proc);
  1139. uloop_timeout_set(&wdev->script_check, 0);
  1140. return 0;
  1141. }
  1142. /* ubus callback network.wireless.notify, command = process kill all */
  1143. static int
  1144. wireless_device_process_kill_all(struct wireless_device *wdev, struct blob_attr *data,
  1145. struct ubus_request_data *req)
  1146. {
  1147. enum {
  1148. KILL_ATTR_SIGNAL,
  1149. KILL_ATTR_IMMEDIATE,
  1150. __KILL_ATTR_MAX
  1151. };
  1152. static const struct blobmsg_policy kill_policy[__KILL_ATTR_MAX] = {
  1153. [KILL_ATTR_SIGNAL] = { .name = "signal", .type = BLOBMSG_TYPE_INT32 },
  1154. [KILL_ATTR_IMMEDIATE] = { .name = "immediate", .type = BLOBMSG_TYPE_BOOL },
  1155. };
  1156. struct blob_attr *tb[__KILL_ATTR_MAX];
  1157. struct blob_attr *cur;
  1158. bool immediate = false;
  1159. int signal = SIGTERM;
  1160. blobmsg_parse(kill_policy, __KILL_ATTR_MAX, tb, blobmsg_data(data), blobmsg_data_len(data));
  1161. if ((cur = tb[KILL_ATTR_SIGNAL]))
  1162. signal = blobmsg_get_u32(cur);
  1163. if ((cur = tb[KILL_ATTR_IMMEDIATE]))
  1164. immediate = blobmsg_get_bool(cur);
  1165. if (wdev->state != IFS_TEARDOWN || wdev->kill_request)
  1166. return UBUS_STATUS_PERMISSION_DENIED;
  1167. wireless_process_kill_all(wdev, signal, immediate);
  1168. if (list_empty(&wdev->script_proc))
  1169. return 0;
  1170. wdev->kill_request = calloc(1, sizeof(*wdev->kill_request));
  1171. ubus_defer_request(ubus_ctx, req, wdev->kill_request);
  1172. return 0;
  1173. }
  1174. /* ubus callback network.wireless.notify, command = set_retry */
  1175. static int
  1176. wireless_device_set_retry(struct wireless_device *wdev, struct blob_attr *data)
  1177. {
  1178. static const struct blobmsg_policy retry_policy = {
  1179. .name = "retry", .type = BLOBMSG_TYPE_INT32
  1180. };
  1181. struct blob_attr *val;
  1182. blobmsg_parse(&retry_policy, 1, &val, blobmsg_data(data), blobmsg_data_len(data));
  1183. if (val)
  1184. wdev->retry = blobmsg_get_u32(val);
  1185. else
  1186. wdev->retry = WIRELESS_SETUP_RETRY;
  1187. __wireless_device_set_up(wdev, 0);
  1188. netifd_log_message(L_NOTICE, "Wireless device '%s' set retry=%d\n", wdev->name, wdev->retry);
  1189. return 0;
  1190. }
  1191. enum {
  1192. NOTIFY_CMD_UP = 0,
  1193. NOTIFY_CMD_SET_DATA = 1,
  1194. NOTIFY_CMD_PROCESS_ADD = 2,
  1195. NOTIFY_CMD_PROCESS_KILL_ALL = 3,
  1196. NOTIFY_CMD_SET_RETRY = 4,
  1197. };
  1198. /* ubus callback network.wireless.notify */
  1199. int
  1200. wireless_device_notify(struct wireless_device *wdev, struct blob_attr *data,
  1201. struct ubus_request_data *req)
  1202. {
  1203. enum {
  1204. NOTIFY_ATTR_COMMAND,
  1205. NOTIFY_ATTR_VIF,
  1206. NOTIFY_ATTR_VLAN,
  1207. NOTIFY_ATTR_DATA,
  1208. __NOTIFY_MAX,
  1209. };
  1210. static const struct blobmsg_policy notify_policy[__NOTIFY_MAX] = {
  1211. [NOTIFY_ATTR_COMMAND] = { .name = "command", .type = BLOBMSG_TYPE_INT32 },
  1212. [NOTIFY_ATTR_VIF] = { .name = "interface", .type = BLOBMSG_TYPE_STRING },
  1213. [NOTIFY_ATTR_VLAN] = { .name = "vlan", .type = BLOBMSG_TYPE_STRING },
  1214. [NOTIFY_ATTR_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE },
  1215. };
  1216. struct wireless_interface *vif = NULL;
  1217. struct wireless_vlan *vlan = NULL;
  1218. struct blob_attr *tb[__NOTIFY_MAX];
  1219. struct blob_attr *cur, **pdata;
  1220. blobmsg_parse(notify_policy, __NOTIFY_MAX, tb, blob_data(data), blob_len(data));
  1221. if (!tb[NOTIFY_ATTR_COMMAND])
  1222. return UBUS_STATUS_INVALID_ARGUMENT;
  1223. if ((cur = tb[NOTIFY_ATTR_VIF]) != NULL) {
  1224. vif = vlist_find(&wdev->interfaces, blobmsg_data(cur), vif, node);
  1225. if (!vif)
  1226. return UBUS_STATUS_NOT_FOUND;
  1227. }
  1228. if ((cur = tb[NOTIFY_ATTR_VLAN]) != NULL) {
  1229. vlan = vlist_find(&wdev->vlans, blobmsg_data(cur), vlan, node);
  1230. if (!vlan)
  1231. return UBUS_STATUS_NOT_FOUND;
  1232. }
  1233. cur = tb[NOTIFY_ATTR_DATA];
  1234. if (!cur)
  1235. return UBUS_STATUS_INVALID_ARGUMENT;
  1236. switch (blobmsg_get_u32(tb[NOTIFY_ATTR_COMMAND])) {
  1237. case NOTIFY_CMD_UP:
  1238. if (vif || vlan)
  1239. return UBUS_STATUS_INVALID_ARGUMENT;
  1240. if (wdev->state != IFS_SETUP)
  1241. return UBUS_STATUS_PERMISSION_DENIED;
  1242. wireless_device_mark_up(wdev);
  1243. break;
  1244. case NOTIFY_CMD_SET_DATA:
  1245. if (vif)
  1246. pdata = &vif->data;
  1247. else if (vlan)
  1248. pdata = &vlan->data;
  1249. else
  1250. pdata = &wdev->data;
  1251. if (*pdata)
  1252. return UBUS_STATUS_INVALID_ARGUMENT;
  1253. *pdata = blob_memdup(cur);
  1254. if (vif)
  1255. wireless_interface_set_data(vif);
  1256. else if (vlan)
  1257. wireless_vlan_set_data(vlan);
  1258. break;
  1259. case NOTIFY_CMD_PROCESS_ADD:
  1260. return wireless_device_add_process(wdev, cur);
  1261. case NOTIFY_CMD_PROCESS_KILL_ALL:
  1262. return wireless_device_process_kill_all(wdev, cur, req);
  1263. case NOTIFY_CMD_SET_RETRY:
  1264. return wireless_device_set_retry(wdev, cur);
  1265. default:
  1266. return UBUS_STATUS_INVALID_ARGUMENT;
  1267. }
  1268. return 0;
  1269. }
  1270. /* called on startup and by netifd reload() */
  1271. void
  1272. wireless_start_pending(void)
  1273. {
  1274. struct wireless_device *wdev;
  1275. vlist_for_each_element(&wireless_devices, wdev, node)
  1276. __wireless_device_set_up(wdev, 0);
  1277. }
  1278. void wireless_device_hotplug_event(const char *name, bool add)
  1279. {
  1280. struct wireless_interface *vif;
  1281. struct wireless_device *wdev;
  1282. const char *s;
  1283. size_t len;
  1284. s = strstr(name, ".sta");
  1285. if (s) {
  1286. if (strchr(s + 4, '.'))
  1287. return;
  1288. len = s - name;
  1289. } else if (!device_find(name)) {
  1290. len = strlen(name);
  1291. } else {
  1292. return;
  1293. }
  1294. vlist_for_each_element(&wireless_devices, wdev, node) {
  1295. vlist_for_each_element(&wdev->interfaces, vif, node) {
  1296. if (!vif->ifname)
  1297. continue;
  1298. if (strlen(vif->ifname) != len ||
  1299. strncmp(vif->ifname, name, len) != 0)
  1300. continue;
  1301. wireless_interface_handle_link(vif, name, add);
  1302. }
  1303. }
  1304. }