ubus.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501
  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. #define _GNU_SOURCE
  15. #include <arpa/inet.h>
  16. #include <string.h>
  17. #include <stdio.h>
  18. #include "netifd.h"
  19. #include "interface.h"
  20. #include "proto.h"
  21. #include "ubus.h"
  22. #include "system.h"
  23. #include "wireless.h"
  24. struct ubus_context *ubus_ctx = NULL;
  25. static struct blob_buf b;
  26. static const char *ubus_path;
  27. static struct udebug_ubus udebug;
  28. /* global object */
  29. static int
  30. netifd_handle_restart(struct ubus_context *ctx, struct ubus_object *obj,
  31. struct ubus_request_data *req, const char *method,
  32. struct blob_attr *msg)
  33. {
  34. netifd_restart();
  35. return 0;
  36. }
  37. static int
  38. netifd_handle_reload(struct ubus_context *ctx, struct ubus_object *obj,
  39. struct ubus_request_data *req, const char *method,
  40. struct blob_attr *msg)
  41. {
  42. if (netifd_reload())
  43. return UBUS_STATUS_NOT_FOUND;
  44. return UBUS_STATUS_OK;
  45. }
  46. enum {
  47. HR_TARGET,
  48. HR_V6,
  49. HR_INTERFACE,
  50. HR_EXCLUDE,
  51. __HR_MAX
  52. };
  53. static const struct blobmsg_policy route_policy[__HR_MAX] = {
  54. [HR_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING },
  55. [HR_V6] = { .name = "v6", .type = BLOBMSG_TYPE_BOOL },
  56. [HR_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_STRING },
  57. [HR_EXCLUDE] = { .name = "exclude", .type = BLOBMSG_TYPE_BOOL },
  58. };
  59. static int
  60. netifd_add_host_route(struct ubus_context *ctx, struct ubus_object *obj,
  61. struct ubus_request_data *req, const char *method,
  62. struct blob_attr *msg)
  63. {
  64. struct blob_attr *tb[__HR_MAX];
  65. struct interface *iface = NULL;
  66. union if_addr a;
  67. bool v6 = false;
  68. bool exclude = false;
  69. blobmsg_parse(route_policy, __HR_MAX, tb, blob_data(msg), blob_len(msg));
  70. if (!tb[HR_TARGET])
  71. return UBUS_STATUS_INVALID_ARGUMENT;
  72. if (tb[HR_V6])
  73. v6 = blobmsg_get_bool(tb[HR_V6]);
  74. if (tb[HR_EXCLUDE])
  75. exclude = blobmsg_get_bool(tb[HR_EXCLUDE]);
  76. if (tb[HR_INTERFACE])
  77. iface = vlist_find(&interfaces, blobmsg_data(tb[HR_INTERFACE]), iface, node);
  78. memset(&a, 0, sizeof(a));
  79. if (!inet_pton(v6 ? AF_INET6 : AF_INET, blobmsg_data(tb[HR_TARGET]), &a))
  80. return UBUS_STATUS_INVALID_ARGUMENT;
  81. iface = interface_ip_add_target_route(&a, v6, iface, exclude);
  82. if (!iface)
  83. return UBUS_STATUS_NOT_FOUND;
  84. blob_buf_init(&b, 0);
  85. blobmsg_add_string(&b, "interface", iface->name);
  86. ubus_send_reply(ctx, req, b.head);
  87. return 0;
  88. }
  89. static int
  90. netifd_get_proto_handlers(struct ubus_context *ctx, struct ubus_object *obj,
  91. struct ubus_request_data *req, const char *method,
  92. struct blob_attr *msg)
  93. {
  94. blob_buf_init(&b, 0);
  95. proto_dump_handlers(&b);
  96. ubus_send_reply(ctx, req, b.head);
  97. return 0;
  98. }
  99. enum {
  100. DI_NAME,
  101. __DI_MAX
  102. };
  103. static const struct blobmsg_policy dynamic_policy[__DI_MAX] = {
  104. [DI_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
  105. };
  106. static int
  107. netifd_add_dynamic(struct ubus_context *ctx, struct ubus_object *obj,
  108. struct ubus_request_data *req, const char *method,
  109. struct blob_attr *msg)
  110. {
  111. struct blob_attr *tb[__DI_MAX];
  112. struct interface *iface;
  113. struct blob_attr *config;
  114. blobmsg_parse(dynamic_policy, __DI_MAX, tb, blob_data(msg), blob_len(msg));
  115. if (!tb[DI_NAME])
  116. return UBUS_STATUS_INVALID_ARGUMENT;
  117. const char *name = blobmsg_get_string(tb[DI_NAME]);
  118. iface = interface_alloc(name, msg, true);
  119. if (!iface)
  120. return UBUS_STATUS_UNKNOWN_ERROR;
  121. config = blob_memdup(msg);
  122. if (!config)
  123. goto error;
  124. if (!interface_add(iface, config))
  125. goto error_free_config;
  126. return UBUS_STATUS_OK;
  127. error_free_config:
  128. free(config);
  129. error:
  130. free(iface);
  131. return UBUS_STATUS_UNKNOWN_ERROR;
  132. }
  133. enum {
  134. NETNS_UPDOWN_JAIL,
  135. NETNS_UPDOWN_START,
  136. __NETNS_UPDOWN_MAX
  137. };
  138. static const struct blobmsg_policy netns_updown_policy[__NETNS_UPDOWN_MAX] = {
  139. [NETNS_UPDOWN_JAIL] = { .name = "jail", .type = BLOBMSG_TYPE_STRING },
  140. [NETNS_UPDOWN_START] = { .name = "start", .type = BLOBMSG_TYPE_BOOL },
  141. };
  142. static int
  143. netifd_netns_updown(struct ubus_context *ctx, struct ubus_object *obj,
  144. struct ubus_request_data *req, const char *method,
  145. struct blob_attr *msg)
  146. {
  147. int target_netns_fd = ubus_request_get_caller_fd(req);
  148. struct blob_attr *tb[__NETNS_UPDOWN_MAX];
  149. bool start;
  150. if (target_netns_fd < 0)
  151. return UBUS_STATUS_INVALID_ARGUMENT;
  152. blobmsg_parse(netns_updown_policy, __NETNS_UPDOWN_MAX, tb, blob_data(msg), blob_len(msg));
  153. start = tb[NETNS_UPDOWN_START] && blobmsg_get_bool(tb[NETNS_UPDOWN_START]);
  154. if (start) {
  155. if (!tb[NETNS_UPDOWN_JAIL])
  156. return UBUS_STATUS_INVALID_ARGUMENT;
  157. interface_start_jail(target_netns_fd, blobmsg_get_string(tb[NETNS_UPDOWN_JAIL]));
  158. } else {
  159. interface_stop_jail(target_netns_fd);
  160. }
  161. close(target_netns_fd);
  162. return UBUS_STATUS_OK;
  163. }
  164. static struct ubus_method main_object_methods[] = {
  165. { .name = "restart", .handler = netifd_handle_restart },
  166. { .name = "reload", .handler = netifd_handle_reload },
  167. UBUS_METHOD("add_host_route", netifd_add_host_route, route_policy),
  168. { .name = "get_proto_handlers", .handler = netifd_get_proto_handlers },
  169. UBUS_METHOD("add_dynamic", netifd_add_dynamic, dynamic_policy),
  170. UBUS_METHOD("netns_updown", netifd_netns_updown, netns_updown_policy),
  171. };
  172. static struct ubus_object_type main_object_type =
  173. UBUS_OBJECT_TYPE("netifd", main_object_methods);
  174. static struct ubus_object main_object = {
  175. .name = "network",
  176. .type = &main_object_type,
  177. .methods = main_object_methods,
  178. .n_methods = ARRAY_SIZE(main_object_methods),
  179. };
  180. enum {
  181. DEV_NAME,
  182. __DEV_MAX,
  183. };
  184. static const struct blobmsg_policy dev_policy[__DEV_MAX] = {
  185. [DEV_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
  186. };
  187. static int
  188. netifd_dev_status(struct ubus_context *ctx, struct ubus_object *obj,
  189. struct ubus_request_data *req, const char *method,
  190. struct blob_attr *msg)
  191. {
  192. struct device *dev = NULL;
  193. struct blob_attr *tb[__DEV_MAX];
  194. blobmsg_parse(dev_policy, __DEV_MAX, tb, blob_data(msg), blob_len(msg));
  195. if (tb[DEV_NAME]) {
  196. dev = device_find(blobmsg_data(tb[DEV_NAME]));
  197. if (!dev)
  198. return UBUS_STATUS_INVALID_ARGUMENT;
  199. }
  200. blob_buf_init(&b, 0);
  201. device_dump_status(&b, dev);
  202. ubus_send_reply(ctx, req, b.head);
  203. return 0;
  204. }
  205. enum {
  206. ALIAS_ATTR_ALIAS,
  207. ALIAS_ATTR_DEV,
  208. __ALIAS_ATTR_MAX,
  209. };
  210. static const struct blobmsg_policy alias_attrs[__ALIAS_ATTR_MAX] = {
  211. [ALIAS_ATTR_ALIAS] = { "alias", BLOBMSG_TYPE_ARRAY },
  212. [ALIAS_ATTR_DEV] = { "device", BLOBMSG_TYPE_STRING },
  213. };
  214. static int
  215. netifd_handle_alias(struct ubus_context *ctx, struct ubus_object *obj,
  216. struct ubus_request_data *req, const char *method,
  217. struct blob_attr *msg)
  218. {
  219. struct device *dev = NULL;
  220. struct blob_attr *tb[__ALIAS_ATTR_MAX];
  221. struct blob_attr *cur;
  222. size_t rem;
  223. blobmsg_parse(alias_attrs, __ALIAS_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
  224. if (!tb[ALIAS_ATTR_ALIAS])
  225. return UBUS_STATUS_INVALID_ARGUMENT;
  226. if ((cur = tb[ALIAS_ATTR_DEV]) != NULL) {
  227. dev = device_get(blobmsg_data(cur), true);
  228. if (!dev)
  229. return UBUS_STATUS_NOT_FOUND;
  230. }
  231. blobmsg_for_each_attr(cur, tb[ALIAS_ATTR_ALIAS], rem) {
  232. if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
  233. goto error;
  234. if (!blobmsg_check_attr(cur, false))
  235. goto error;
  236. alias_notify_device(blobmsg_data(cur), dev);
  237. }
  238. return 0;
  239. error:
  240. device_free_unused();
  241. return UBUS_STATUS_INVALID_ARGUMENT;
  242. }
  243. enum {
  244. DEV_STATE_NAME,
  245. DEV_STATE_DEFER,
  246. DEV_STATE_AUTH_STATUS,
  247. DEV_STATE_AUTH_VLANS,
  248. __DEV_STATE_MAX,
  249. };
  250. static const struct blobmsg_policy dev_state_policy[__DEV_STATE_MAX] = {
  251. [DEV_STATE_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
  252. [DEV_STATE_DEFER] = { .name = "defer", .type = BLOBMSG_TYPE_BOOL },
  253. [DEV_STATE_AUTH_STATUS] = { .name = "auth_status", .type = BLOBMSG_TYPE_BOOL },
  254. [DEV_STATE_AUTH_VLANS] = { .name = "auth_vlans", BLOBMSG_TYPE_ARRAY },
  255. };
  256. static int
  257. netifd_handle_set_state(struct ubus_context *ctx, struct ubus_object *obj,
  258. struct ubus_request_data *req, const char *method,
  259. struct blob_attr *msg)
  260. {
  261. struct device *dev = NULL;
  262. struct blob_attr *tb[__DEV_STATE_MAX];
  263. struct blob_attr *cur;
  264. bool auth_status;
  265. blobmsg_parse(dev_state_policy, __DEV_STATE_MAX, tb, blob_data(msg), blob_len(msg));
  266. cur = tb[DEV_STATE_NAME];
  267. if (!cur)
  268. return UBUS_STATUS_INVALID_ARGUMENT;
  269. dev = device_find(blobmsg_data(cur));
  270. if (!dev)
  271. return UBUS_STATUS_NOT_FOUND;
  272. cur = tb[DEV_STATE_DEFER];
  273. if (cur)
  274. device_set_deferred(dev, !!blobmsg_get_u8(cur));
  275. if ((cur = tb[DEV_STATE_AUTH_STATUS]) != NULL)
  276. auth_status = blobmsg_get_bool(cur);
  277. else
  278. auth_status = dev->auth_status;
  279. if (tb[DEV_STATE_AUTH_STATUS] || tb[DEV_STATE_AUTH_VLANS])
  280. device_set_auth_status(dev, auth_status, tb[DEV_STATE_AUTH_VLANS]);
  281. return 0;
  282. }
  283. #ifdef DUMMY_MODE
  284. enum {
  285. DEV_HOTPLUG_ATTR_NAME,
  286. DEV_HOTPLUG_ATTR_ADD,
  287. __DEV_HOTPLUG_ATTR_MAX,
  288. };
  289. static const struct blobmsg_policy dev_hotplug_policy[__DEV_HOTPLUG_ATTR_MAX] = {
  290. [DEV_HOTPLUG_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
  291. [DEV_HOTPLUG_ATTR_ADD] = { "add", BLOBMSG_TYPE_BOOL },
  292. };
  293. static int
  294. netifd_handle_dev_hotplug(struct ubus_context *ctx, struct ubus_object *obj,
  295. struct ubus_request_data *req, const char *method,
  296. struct blob_attr *msg)
  297. {
  298. struct blob_attr *tb[__DEV_HOTPLUG_ATTR_MAX];
  299. const char *name;
  300. blobmsg_parse(dev_hotplug_policy, __DEV_HOTPLUG_ATTR_MAX, tb,
  301. blob_data(msg), blob_len(msg));
  302. if (!tb[DEV_HOTPLUG_ATTR_NAME] || !tb[DEV_HOTPLUG_ATTR_ADD])
  303. return UBUS_STATUS_INVALID_ARGUMENT;
  304. name = blobmsg_get_string(tb[DEV_HOTPLUG_ATTR_NAME]);
  305. device_hotplug_event(name, blobmsg_get_bool(tb[DEV_HOTPLUG_ATTR_ADD]));
  306. return 0;
  307. }
  308. #endif
  309. static int
  310. netifd_handle_stp_init(struct ubus_context *ctx, struct ubus_object *obj,
  311. struct ubus_request_data *req, const char *method,
  312. struct blob_attr *msg)
  313. {
  314. device_stp_init();
  315. return 0;
  316. }
  317. static struct ubus_method dev_object_methods[] = {
  318. UBUS_METHOD("status", netifd_dev_status, dev_policy),
  319. UBUS_METHOD("set_alias", netifd_handle_alias, alias_attrs),
  320. UBUS_METHOD("set_state", netifd_handle_set_state, dev_state_policy),
  321. #ifdef DUMMY_MODE
  322. UBUS_METHOD("hotplug_event", netifd_handle_dev_hotplug, dev_hotplug_policy),
  323. #endif
  324. UBUS_METHOD_NOARG("stp_init", netifd_handle_stp_init)
  325. };
  326. static struct ubus_object_type dev_object_type =
  327. UBUS_OBJECT_TYPE("device", dev_object_methods);
  328. static struct ubus_object dev_object = {
  329. .name = "network.device",
  330. .type = &dev_object_type,
  331. .methods = dev_object_methods,
  332. .n_methods = ARRAY_SIZE(dev_object_methods),
  333. };
  334. static void
  335. netifd_ubus_add_fd(void)
  336. {
  337. ubus_add_uloop(ubus_ctx);
  338. system_fd_set_cloexec(ubus_ctx->sock.fd);
  339. }
  340. void netifd_ubus_device_notify(const char *event, struct blob_attr *data, int timeout)
  341. {
  342. ubus_notify(ubus_ctx, &dev_object, event, data, timeout);
  343. }
  344. static void
  345. netifd_ubus_reconnect_timer(struct uloop_timeout *timeout)
  346. {
  347. static struct uloop_timeout retry = {
  348. .cb = netifd_ubus_reconnect_timer,
  349. };
  350. int t = 2;
  351. if (ubus_reconnect(ubus_ctx, ubus_path) != 0) {
  352. D(SYSTEM, "failed to reconnect, trying again in %d seconds", t);
  353. uloop_timeout_set(&retry, t * 1000);
  354. return;
  355. }
  356. D(SYSTEM, "reconnected to ubus, new id: %08x", ubus_ctx->local_id);
  357. netifd_ubus_add_fd();
  358. }
  359. static void
  360. netifd_ubus_connection_lost(struct ubus_context *ctx)
  361. {
  362. netifd_ubus_reconnect_timer(NULL);
  363. }
  364. /* per-interface object */
  365. static int
  366. netifd_handle_up(struct ubus_context *ctx, struct ubus_object *obj,
  367. struct ubus_request_data *req, const char *method,
  368. struct blob_attr *msg)
  369. {
  370. struct interface *iface;
  371. iface = container_of(obj, struct interface, ubus);
  372. interface_set_up(iface);
  373. return 0;
  374. }
  375. static int
  376. netifd_handle_down(struct ubus_context *ctx, struct ubus_object *obj,
  377. struct ubus_request_data *req, const char *method,
  378. struct blob_attr *msg)
  379. {
  380. struct interface *iface;
  381. iface = container_of(obj, struct interface, ubus);
  382. interface_set_down(iface);
  383. return 0;
  384. }
  385. static int
  386. netifd_handle_renew(struct ubus_context *ctx, struct ubus_object *obj,
  387. struct ubus_request_data *req, const char *method,
  388. struct blob_attr *msg)
  389. {
  390. struct interface *iface;
  391. iface = container_of(obj, struct interface, ubus);
  392. interface_renew(iface);
  393. return 0;
  394. }
  395. static void
  396. netifd_add_interface_errors(struct blob_buf *b, struct interface *iface)
  397. {
  398. struct interface_error *error;
  399. void *e, *e2, *e3;
  400. int i;
  401. e = blobmsg_open_array(b, "errors");
  402. list_for_each_entry(error, &iface->errors, list) {
  403. e2 = blobmsg_open_table(b, NULL);
  404. blobmsg_add_string(b, "subsystem", error->subsystem);
  405. blobmsg_add_string(b, "code", error->code);
  406. if (error->data[0]) {
  407. e3 = blobmsg_open_array(b, "data");
  408. for (i = 0; error->data[i]; i++)
  409. blobmsg_add_string(b, NULL, error->data[i]);
  410. blobmsg_close_array(b, e3);
  411. }
  412. blobmsg_close_table(b, e2);
  413. }
  414. blobmsg_close_array(b, e);
  415. }
  416. static void
  417. interface_ip_dump_address_list(struct interface_ip_settings *ip, bool v6, bool enabled)
  418. {
  419. struct device_addr *addr;
  420. char *buf;
  421. void *a;
  422. int buflen = 128;
  423. int af;
  424. time_t now = system_get_rtime();
  425. vlist_for_each_element(&ip->addr, addr, node) {
  426. if (addr->enabled != enabled)
  427. continue;
  428. if ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET4)
  429. af = AF_INET;
  430. else
  431. af = AF_INET6;
  432. if (af != (v6 ? AF_INET6 : AF_INET))
  433. continue;
  434. a = blobmsg_open_table(&b, NULL);
  435. buf = blobmsg_alloc_string_buffer(&b, "address", buflen);
  436. inet_ntop(af, &addr->addr, buf, buflen);
  437. blobmsg_add_string_buffer(&b);
  438. blobmsg_add_u32(&b, "mask", addr->mask);
  439. if (addr->point_to_point) {
  440. buf = blobmsg_alloc_string_buffer(&b, "ptpaddress", buflen);
  441. inet_ntop(af, &addr->point_to_point, buf, buflen);
  442. blobmsg_add_string_buffer(&b);
  443. }
  444. if (addr->preferred_until) {
  445. int preferred = addr->preferred_until - now;
  446. if (preferred < 0)
  447. preferred = 0;
  448. blobmsg_add_u32(&b, "preferred", preferred);
  449. }
  450. if (addr->valid_until)
  451. blobmsg_add_u32(&b, "valid", addr->valid_until - now);
  452. if (addr->pclass)
  453. blobmsg_add_string(&b, "class", addr->pclass);
  454. blobmsg_close_table(&b, a);
  455. }
  456. }
  457. static void
  458. interface_ip_dump_neighbor_list(struct interface_ip_settings *ip, bool enabled)
  459. {
  460. struct device_neighbor *neighbor;
  461. int buflen = 128;
  462. char *buf;
  463. void *r;
  464. int af;
  465. vlist_for_each_element(&ip->neighbor, neighbor, node) {
  466. if (neighbor->enabled != enabled)
  467. continue;
  468. if ((neighbor->flags & DEVADDR_FAMILY) == DEVADDR_INET4)
  469. af = AF_INET;
  470. else
  471. af = AF_INET6;
  472. r = blobmsg_open_table(&b, NULL);
  473. if (neighbor->flags & DEVNEIGH_MAC)
  474. blobmsg_add_string(&b, "mac", format_macaddr(neighbor->macaddr));
  475. buf = blobmsg_alloc_string_buffer(&b , "address", buflen);
  476. inet_ntop(af, &neighbor->addr, buf, buflen);
  477. blobmsg_add_string_buffer(&b);
  478. if (neighbor->proxy)
  479. blobmsg_add_u32(&b, "proxy", neighbor->proxy);
  480. if (neighbor->router)
  481. blobmsg_add_u32(&b, "router", neighbor->router);
  482. blobmsg_close_table(&b, r);
  483. }
  484. }
  485. static void
  486. interface_ip_dump_route_list(struct interface_ip_settings *ip, bool enabled)
  487. {
  488. struct device_route *route;
  489. int buflen = 128;
  490. char *buf;
  491. void *r;
  492. int af;
  493. time_t now = system_get_rtime();
  494. vlist_for_each_element(&ip->route, route, node) {
  495. if (route->enabled != enabled)
  496. continue;
  497. if ((ip->no_defaultroute == enabled) && !route->mask)
  498. continue;
  499. if ((route->flags & DEVADDR_FAMILY) == DEVADDR_INET4)
  500. af = AF_INET;
  501. else
  502. af = AF_INET6;
  503. r = blobmsg_open_table(&b, NULL);
  504. buf = blobmsg_alloc_string_buffer(&b, "target", buflen);
  505. inet_ntop(af, &route->addr, buf, buflen);
  506. blobmsg_add_string_buffer(&b);
  507. blobmsg_add_u32(&b, "mask", route->mask);
  508. buf = blobmsg_alloc_string_buffer(&b, "nexthop", buflen);
  509. inet_ntop(af, &route->nexthop, buf, buflen);
  510. blobmsg_add_string_buffer(&b);
  511. if (route->flags & DEVROUTE_TYPE)
  512. blobmsg_add_u32(&b, "type", route->type);
  513. if (route->flags & DEVROUTE_PROTO)
  514. blobmsg_add_u32(&b, "proto", route->proto);
  515. if (route->flags & DEVROUTE_MTU)
  516. blobmsg_add_u32(&b, "mtu", route->mtu);
  517. if (route->flags & DEVROUTE_METRIC)
  518. blobmsg_add_u32(&b, "metric", route->metric);
  519. if (route->flags & DEVROUTE_TABLE)
  520. blobmsg_add_u32(&b, "table", route->table);
  521. if (route->valid_until)
  522. blobmsg_add_u32(&b, "valid", route->valid_until - now);
  523. buf = blobmsg_alloc_string_buffer(&b, "source", buflen);
  524. inet_ntop(af, &route->source, buf, buflen);
  525. snprintf(buf + strlen(buf), buflen - strlen(buf), "/%u", route->sourcemask);
  526. blobmsg_add_string_buffer(&b);
  527. blobmsg_close_table(&b, r);
  528. }
  529. }
  530. static void
  531. interface_ip_dump_prefix_list(struct interface_ip_settings *ip)
  532. {
  533. struct device_prefix *prefix;
  534. char *buf;
  535. void *a, *c;
  536. const int buflen = INET6_ADDRSTRLEN;
  537. time_t now = system_get_rtime();
  538. vlist_for_each_element(&ip->prefix, prefix, node) {
  539. a = blobmsg_open_table(&b, NULL);
  540. buf = blobmsg_alloc_string_buffer(&b, "address", buflen);
  541. inet_ntop(AF_INET6, &prefix->addr, buf, buflen);
  542. blobmsg_add_string_buffer(&b);
  543. blobmsg_add_u32(&b, "mask", prefix->length);
  544. if (prefix->preferred_until) {
  545. int preferred = prefix->preferred_until - now;
  546. if (preferred < 0)
  547. preferred = 0;
  548. blobmsg_add_u32(&b, "preferred", preferred);
  549. }
  550. if (prefix->valid_until)
  551. blobmsg_add_u32(&b, "valid", prefix->valid_until - now);
  552. blobmsg_add_string(&b, "class", prefix->pclass);
  553. c = blobmsg_open_table(&b, "assigned");
  554. struct device_prefix_assignment *assign;
  555. list_for_each_entry(assign, &prefix->assignments, head) {
  556. if (!assign->name[0])
  557. continue;
  558. struct in6_addr addr = prefix->addr;
  559. addr.s6_addr32[1] |= htonl(assign->assigned);
  560. void *d = blobmsg_open_table(&b, assign->name);
  561. buf = blobmsg_alloc_string_buffer(&b, "address", buflen);
  562. inet_ntop(AF_INET6, &addr, buf, buflen);
  563. blobmsg_add_string_buffer(&b);
  564. blobmsg_add_u32(&b, "mask", assign->length);
  565. blobmsg_close_table(&b, d);
  566. }
  567. blobmsg_close_table(&b, c);
  568. blobmsg_close_table(&b, a);
  569. }
  570. }
  571. static void
  572. interface_ip_dump_prefix_assignment_list(struct interface *iface)
  573. {
  574. void *a;
  575. char *buf;
  576. const int buflen = INET6_ADDRSTRLEN;
  577. time_t now = system_get_rtime();
  578. struct device_prefix *prefix;
  579. list_for_each_entry(prefix, &prefixes, head) {
  580. struct device_prefix_assignment *assign;
  581. list_for_each_entry(assign, &prefix->assignments, head) {
  582. if (strcmp(assign->name, iface->name))
  583. continue;
  584. struct in6_addr addr = prefix->addr;
  585. addr.s6_addr32[1] |= htonl(assign->assigned);
  586. a = blobmsg_open_table(&b, NULL);
  587. buf = blobmsg_alloc_string_buffer(&b, "address", buflen);
  588. inet_ntop(AF_INET6, &addr, buf, buflen);
  589. blobmsg_add_string_buffer(&b);
  590. blobmsg_add_u32(&b, "mask", assign->length);
  591. if (prefix->preferred_until) {
  592. int preferred = prefix->preferred_until - now;
  593. if (preferred < 0)
  594. preferred = 0;
  595. blobmsg_add_u32(&b, "preferred", preferred);
  596. }
  597. if (prefix->valid_until)
  598. blobmsg_add_u32(&b, "valid", prefix->valid_until - now);
  599. void *c = blobmsg_open_table(&b, "local-address");
  600. if (assign->enabled) {
  601. buf = blobmsg_alloc_string_buffer(&b, "address", buflen);
  602. inet_ntop(AF_INET6, &assign->addr, buf, buflen);
  603. blobmsg_add_string_buffer(&b);
  604. blobmsg_add_u32(&b, "mask", assign->length);
  605. }
  606. blobmsg_close_table(&b, c);
  607. blobmsg_close_table(&b, a);
  608. }
  609. }
  610. }
  611. static void
  612. interface_ip_dump_dns_server_list(struct interface_ip_settings *ip, bool enabled)
  613. {
  614. struct dns_server *dns;
  615. int buflen = 128;
  616. char *buf;
  617. vlist_simple_for_each_element(&ip->dns_servers, dns, node) {
  618. if (ip->no_dns == enabled)
  619. continue;
  620. buf = blobmsg_alloc_string_buffer(&b, NULL, buflen);
  621. inet_ntop(dns->af, &dns->addr, buf, buflen);
  622. blobmsg_add_string_buffer(&b);
  623. }
  624. }
  625. static void
  626. interface_ip_dump_dns_search_list(struct interface_ip_settings *ip, bool enabled)
  627. {
  628. struct dns_search_domain *dns;
  629. vlist_simple_for_each_element(&ip->dns_search, dns, node) {
  630. if (ip->no_dns == enabled)
  631. continue;
  632. blobmsg_add_string(&b, NULL, dns->name);
  633. }
  634. }
  635. static void
  636. netifd_dump_status(struct interface *iface)
  637. {
  638. struct interface_data *data;
  639. struct device *dev;
  640. void *a, *inactive;
  641. blobmsg_add_u8(&b, "up", iface->state == IFS_UP);
  642. blobmsg_add_u8(&b, "pending", iface->state == IFS_SETUP);
  643. blobmsg_add_u8(&b, "available", iface->available);
  644. blobmsg_add_u8(&b, "autostart", iface->autostart);
  645. blobmsg_add_u8(&b, "dynamic", iface->dynamic);
  646. if (iface->state == IFS_UP) {
  647. time_t cur = system_get_rtime();
  648. blobmsg_add_u32(&b, "uptime", cur - iface->start_time);
  649. if (iface->l3_dev.dev)
  650. blobmsg_add_string(&b, "l3_device", iface->l3_dev.dev->ifname);
  651. }
  652. if (iface->proto_handler)
  653. blobmsg_add_string(&b, "proto", iface->proto_handler->name);
  654. dev = iface->main_dev.dev;
  655. if (dev && !dev->hidden && iface->proto_handler &&
  656. !(iface->proto_handler->flags & PROTO_FLAG_NODEV))
  657. blobmsg_add_string(&b, "device", dev->ifname);
  658. if (iface->jail)
  659. blobmsg_add_string(&b, "jail", iface->jail);
  660. if (iface->jail_device)
  661. blobmsg_add_string(&b, "jail_device", iface->jail_device);
  662. if (iface->tags)
  663. blobmsg_add_blob(&b, iface->tags);
  664. if (iface->state == IFS_UP) {
  665. if (iface->updated) {
  666. a = blobmsg_open_array(&b, "updated");
  667. if (iface->updated & IUF_ADDRESS)
  668. blobmsg_add_string(&b, NULL, "addresses");
  669. if (iface->updated & IUF_ROUTE)
  670. blobmsg_add_string(&b, NULL, "routes");
  671. if (iface->updated & IUF_PREFIX)
  672. blobmsg_add_string(&b, NULL, "prefixes");
  673. if (iface->updated & IUF_DATA)
  674. blobmsg_add_string(&b, NULL, "data");
  675. blobmsg_close_array(&b, a);
  676. }
  677. if (iface->ip4table)
  678. blobmsg_add_u32(&b, "ip4table", iface->ip4table);
  679. if (iface->ip6table)
  680. blobmsg_add_u32(&b, "ip6table", iface->ip6table);
  681. blobmsg_add_u32(&b, "metric", iface->metric);
  682. blobmsg_add_u32(&b, "dns_metric", iface->dns_metric);
  683. blobmsg_add_u8(&b, "delegation", !iface->proto_ip.no_delegation);
  684. if (iface->assignment_weight)
  685. blobmsg_add_u32(&b, "ip6weight", iface->assignment_weight);
  686. a = blobmsg_open_array(&b, "ipv4-address");
  687. interface_ip_dump_address_list(&iface->config_ip, false, true);
  688. interface_ip_dump_address_list(&iface->proto_ip, false, true);
  689. blobmsg_close_array(&b, a);
  690. a = blobmsg_open_array(&b, "ipv6-address");
  691. interface_ip_dump_address_list(&iface->config_ip, true, true);
  692. interface_ip_dump_address_list(&iface->proto_ip, true, true);
  693. blobmsg_close_array(&b, a);
  694. a = blobmsg_open_array(&b, "ipv6-prefix");
  695. interface_ip_dump_prefix_list(&iface->config_ip);
  696. interface_ip_dump_prefix_list(&iface->proto_ip);
  697. blobmsg_close_array(&b, a);
  698. a = blobmsg_open_array(&b, "ipv6-prefix-assignment");
  699. interface_ip_dump_prefix_assignment_list(iface);
  700. blobmsg_close_array(&b, a);
  701. a = blobmsg_open_array(&b, "route");
  702. interface_ip_dump_route_list(&iface->config_ip, true);
  703. interface_ip_dump_route_list(&iface->proto_ip, true);
  704. blobmsg_close_array(&b, a);
  705. a = blobmsg_open_array(&b, "dns-server");
  706. interface_ip_dump_dns_server_list(&iface->config_ip, true);
  707. interface_ip_dump_dns_server_list(&iface->proto_ip, true);
  708. blobmsg_close_array(&b, a);
  709. a = blobmsg_open_array(&b, "dns-search");
  710. interface_ip_dump_dns_search_list(&iface->config_ip, true);
  711. interface_ip_dump_dns_search_list(&iface->proto_ip, true);
  712. blobmsg_close_array(&b, a);
  713. a = blobmsg_open_array(&b, "neighbors");
  714. interface_ip_dump_neighbor_list(&iface->config_ip, true);
  715. interface_ip_dump_neighbor_list(&iface->proto_ip, true);
  716. blobmsg_close_array(&b, a);
  717. inactive = blobmsg_open_table(&b, "inactive");
  718. a = blobmsg_open_array(&b, "ipv4-address");
  719. interface_ip_dump_address_list(&iface->config_ip, false, false);
  720. interface_ip_dump_address_list(&iface->proto_ip, false, false);
  721. blobmsg_close_array(&b, a);
  722. a = blobmsg_open_array(&b, "ipv6-address");
  723. interface_ip_dump_address_list(&iface->config_ip, true, false);
  724. interface_ip_dump_address_list(&iface->proto_ip, true, false);
  725. blobmsg_close_array(&b, a);
  726. a = blobmsg_open_array(&b, "route");
  727. interface_ip_dump_route_list(&iface->config_ip, false);
  728. interface_ip_dump_route_list(&iface->proto_ip, false);
  729. blobmsg_close_array(&b, a);
  730. a = blobmsg_open_array(&b, "dns-server");
  731. interface_ip_dump_dns_server_list(&iface->config_ip, false);
  732. interface_ip_dump_dns_server_list(&iface->proto_ip, false);
  733. blobmsg_close_array(&b, a);
  734. a = blobmsg_open_array(&b, "dns-search");
  735. interface_ip_dump_dns_search_list(&iface->config_ip, false);
  736. interface_ip_dump_dns_search_list(&iface->proto_ip, false);
  737. blobmsg_close_array(&b, a);
  738. a = blobmsg_open_array(&b, "neighbors");
  739. interface_ip_dump_neighbor_list(&iface->config_ip, false);
  740. interface_ip_dump_neighbor_list(&iface->proto_ip, false);
  741. blobmsg_close_array(&b, a);
  742. blobmsg_close_table(&b, inactive);
  743. }
  744. a = blobmsg_open_table(&b, "data");
  745. if (iface->zone)
  746. blobmsg_add_string(&b, "zone", iface->zone);
  747. avl_for_each_element(&iface->data, data, node)
  748. blobmsg_add_blob(&b, data->data);
  749. blobmsg_close_table(&b, a);
  750. if (!list_empty(&iface->errors))
  751. netifd_add_interface_errors(&b, iface);
  752. }
  753. static int
  754. netifd_handle_status(struct ubus_context *ctx, struct ubus_object *obj,
  755. struct ubus_request_data *req, const char *method,
  756. struct blob_attr *msg)
  757. {
  758. struct interface *iface = container_of(obj, struct interface, ubus);
  759. blob_buf_init(&b, 0);
  760. netifd_dump_status(iface);
  761. ubus_send_reply(ctx, req, b.head);
  762. return 0;
  763. }
  764. static int
  765. netifd_handle_dump(struct ubus_context *ctx, struct ubus_object *obj,
  766. struct ubus_request_data *req, const char *method,
  767. struct blob_attr *msg)
  768. {
  769. blob_buf_init(&b, 0);
  770. void *a = blobmsg_open_array(&b, "interface");
  771. struct interface *iface;
  772. vlist_for_each_element(&interfaces, iface, node) {
  773. void *i = blobmsg_open_table(&b, NULL);
  774. blobmsg_add_string(&b, "interface", iface->name);
  775. netifd_dump_status(iface);
  776. blobmsg_close_table(&b, i);
  777. }
  778. blobmsg_close_array(&b, a);
  779. ubus_send_reply(ctx, req, b.head);
  780. return 0;
  781. }
  782. enum {
  783. DEV_LINK_NAME,
  784. DEV_LINK_EXT,
  785. DEV_LINK_VLAN,
  786. __DEV_LINK_MAX,
  787. };
  788. static const struct blobmsg_policy dev_link_policy[__DEV_LINK_MAX] = {
  789. [DEV_LINK_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
  790. [DEV_LINK_EXT] = { .name = "link-ext", .type = BLOBMSG_TYPE_BOOL },
  791. [DEV_LINK_VLAN] = { .name = "vlan", .type = BLOBMSG_TYPE_ARRAY },
  792. };
  793. static int
  794. netifd_iface_handle_device(struct ubus_context *ctx, struct ubus_object *obj,
  795. struct ubus_request_data *req, const char *method,
  796. struct blob_attr *msg)
  797. {
  798. struct blob_attr *tb[__DEV_LINK_MAX];
  799. struct blob_attr *cur;
  800. struct interface *iface;
  801. bool add = !strncmp(method, "add", 3);
  802. bool link_ext = true;
  803. iface = container_of(obj, struct interface, ubus);
  804. blobmsg_parse(dev_link_policy, __DEV_LINK_MAX, tb, blob_data(msg), blob_len(msg));
  805. if (!tb[DEV_LINK_NAME])
  806. return UBUS_STATUS_INVALID_ARGUMENT;
  807. cur = tb[DEV_LINK_EXT];
  808. if (cur)
  809. link_ext = blobmsg_get_bool(cur);
  810. return interface_handle_link(iface, blobmsg_data(tb[DEV_LINK_NAME]),
  811. tb[DEV_LINK_VLAN], add, link_ext);
  812. }
  813. static int
  814. netifd_iface_notify_proto(struct ubus_context *ctx, struct ubus_object *obj,
  815. struct ubus_request_data *req, const char *method,
  816. struct blob_attr *msg)
  817. {
  818. struct interface *iface;
  819. iface = container_of(obj, struct interface, ubus);
  820. if (!iface->proto || !iface->proto->notify)
  821. return UBUS_STATUS_NOT_SUPPORTED;
  822. return iface->proto->notify(iface->proto, msg);
  823. }
  824. static void
  825. netifd_iface_do_remove(struct uloop_timeout *timeout)
  826. {
  827. struct interface *iface;
  828. iface = container_of(timeout, struct interface, remove_timer);
  829. vlist_delete(&interfaces, &iface->node);
  830. }
  831. static int
  832. netifd_iface_remove(struct ubus_context *ctx, struct ubus_object *obj,
  833. struct ubus_request_data *req, const char *method,
  834. struct blob_attr *msg)
  835. {
  836. struct interface *iface;
  837. iface = container_of(obj, struct interface, ubus);
  838. if (iface->remove_timer.cb)
  839. return UBUS_STATUS_INVALID_ARGUMENT;
  840. iface->remove_timer.cb = netifd_iface_do_remove;
  841. uloop_timeout_set(&iface->remove_timer, 100);
  842. return 0;
  843. }
  844. static int
  845. netifd_handle_iface_prepare(struct ubus_context *ctx, struct ubus_object *obj,
  846. struct ubus_request_data *req, const char *method,
  847. struct blob_attr *msg)
  848. {
  849. struct interface *iface;
  850. struct device *dev, *bridge_dev = NULL;
  851. const struct device_hotplug_ops *ops;
  852. iface = container_of(obj, struct interface, ubus);
  853. dev = iface->main_dev.dev;
  854. if (!dev)
  855. goto out;
  856. ops = dev->hotplug_ops;
  857. if (!ops)
  858. goto out;
  859. ops->prepare(dev, &bridge_dev);
  860. out:
  861. blob_buf_init(&b, 0);
  862. if (bridge_dev)
  863. blobmsg_add_string(&b, "bridge", bridge_dev->ifname);
  864. ubus_send_reply(ctx, req, b.head);
  865. return 0;
  866. }
  867. static int
  868. netifd_handle_set_data(struct ubus_context *ctx, struct ubus_object *obj,
  869. struct ubus_request_data *req, const char *method,
  870. struct blob_attr *msg)
  871. {
  872. struct interface *iface;
  873. iface = container_of(obj, struct interface, ubus);
  874. return interface_parse_data(iface, msg);
  875. }
  876. static struct ubus_method iface_object_methods[] = {
  877. { .name = "up", .handler = netifd_handle_up },
  878. { .name = "down", .handler = netifd_handle_down },
  879. { .name = "renew", .handler = netifd_handle_renew },
  880. { .name = "status", .handler = netifd_handle_status },
  881. { .name = "prepare", .handler = netifd_handle_iface_prepare },
  882. { .name = "dump", .handler = netifd_handle_dump },
  883. UBUS_METHOD("add_device", netifd_iface_handle_device, dev_link_policy ),
  884. UBUS_METHOD("remove_device", netifd_iface_handle_device, dev_link_policy ),
  885. { .name = "notify_proto", .handler = netifd_iface_notify_proto },
  886. { .name = "remove", .handler = netifd_iface_remove },
  887. { .name = "set_data", .handler = netifd_handle_set_data },
  888. };
  889. static struct ubus_object_type iface_object_type =
  890. UBUS_OBJECT_TYPE("netifd_iface", iface_object_methods);
  891. static struct ubus_object iface_object = {
  892. .name = "network.interface",
  893. .type = &iface_object_type,
  894. .n_methods = ARRAY_SIZE(iface_object_methods),
  895. };
  896. static void netifd_add_object(struct ubus_object *obj)
  897. {
  898. int ret = ubus_add_object(ubus_ctx, obj);
  899. if (ret != 0)
  900. fprintf(stderr, "Failed to publish object '%s': %s\n", obj->name, ubus_strerror(ret));
  901. }
  902. static const struct blobmsg_policy iface_policy = {
  903. .name = "interface",
  904. .type = BLOBMSG_TYPE_STRING,
  905. };
  906. static int
  907. netifd_handle_iface(struct ubus_context *ctx, struct ubus_object *obj,
  908. struct ubus_request_data *req, const char *method,
  909. struct blob_attr *msg)
  910. {
  911. struct interface *iface;
  912. struct blob_attr *tb;
  913. size_t i;
  914. blobmsg_parse(&iface_policy, 1, &tb, blob_data(msg), blob_len(msg));
  915. if (!tb)
  916. return UBUS_STATUS_INVALID_ARGUMENT;
  917. iface = vlist_find(&interfaces, blobmsg_data(tb), iface, node);
  918. if (!iface)
  919. return UBUS_STATUS_NOT_FOUND;
  920. for (i = 0; i < ARRAY_SIZE(iface_object_methods); i++) {
  921. ubus_handler_t cb;
  922. if (strcmp(method, iface_object_methods[i].name) != 0)
  923. continue;
  924. cb = iface_object_methods[i].handler;
  925. return cb(ctx, &iface->ubus, req, method, msg);
  926. }
  927. return UBUS_STATUS_INVALID_ARGUMENT;
  928. }
  929. static void netifd_add_iface_object(void)
  930. {
  931. struct ubus_method *methods;
  932. size_t i;
  933. methods = calloc(1, sizeof(iface_object_methods));
  934. if (!methods)
  935. return;
  936. memcpy(methods, iface_object_methods, sizeof(iface_object_methods));
  937. iface_object.methods = methods;
  938. for (i = 0; i < ARRAY_SIZE(iface_object_methods); i++) {
  939. if (methods[i].handler == netifd_handle_dump)
  940. continue;
  941. methods[i].handler = netifd_handle_iface;
  942. methods[i].policy = &iface_policy;
  943. methods[i].n_policy = 1;
  944. }
  945. netifd_add_object(&iface_object);
  946. }
  947. static struct wireless_device *
  948. get_wdev(struct blob_attr *msg, int *ret)
  949. {
  950. struct blobmsg_policy wdev_policy = {
  951. .name = "device",
  952. .type = BLOBMSG_TYPE_STRING,
  953. };
  954. struct blob_attr *dev_attr;
  955. struct wireless_device *wdev = NULL;
  956. blobmsg_parse(&wdev_policy, 1, &dev_attr, blob_data(msg), blob_len(msg));
  957. if (!dev_attr) {
  958. *ret = UBUS_STATUS_INVALID_ARGUMENT;
  959. return NULL;
  960. }
  961. wdev = vlist_find(&wireless_devices, blobmsg_data(dev_attr), wdev, node);
  962. if (!wdev) {
  963. *ret = UBUS_STATUS_NOT_FOUND;
  964. return NULL;
  965. }
  966. *ret = 0;
  967. return wdev;
  968. }
  969. static int
  970. netifd_handle_wdev_reconf(struct ubus_context *ctx, struct ubus_object *obj,
  971. struct ubus_request_data *req, const char *method,
  972. struct blob_attr *msg)
  973. {
  974. struct wireless_device *wdev;
  975. int ret;
  976. wdev = get_wdev(msg, &ret);
  977. if (ret == UBUS_STATUS_NOT_FOUND)
  978. return ret;
  979. if (wdev) {
  980. wireless_device_reconf(wdev);
  981. } else {
  982. vlist_for_each_element(&wireless_devices, wdev, node)
  983. wireless_device_reconf(wdev);
  984. }
  985. return 0;
  986. }
  987. static int
  988. netifd_handle_wdev_up(struct ubus_context *ctx, struct ubus_object *obj,
  989. struct ubus_request_data *req, const char *method,
  990. struct blob_attr *msg)
  991. {
  992. struct wireless_device *wdev;
  993. int ret;
  994. wdev = get_wdev(msg, &ret);
  995. if (ret == UBUS_STATUS_NOT_FOUND)
  996. return ret;
  997. if (wdev) {
  998. wireless_device_set_up(wdev);
  999. } else {
  1000. vlist_for_each_element(&wireless_devices, wdev, node)
  1001. wireless_device_set_up(wdev);
  1002. }
  1003. return 0;
  1004. }
  1005. static int
  1006. netifd_handle_wdev_down(struct ubus_context *ctx, struct ubus_object *obj,
  1007. struct ubus_request_data *req, const char *method,
  1008. struct blob_attr *msg)
  1009. {
  1010. struct wireless_device *wdev;
  1011. int ret;
  1012. wdev = get_wdev(msg, &ret);
  1013. if (ret == UBUS_STATUS_NOT_FOUND)
  1014. return ret;
  1015. if (wdev) {
  1016. wireless_device_set_down(wdev);
  1017. } else {
  1018. vlist_for_each_element(&wireless_devices, wdev, node)
  1019. wireless_device_set_down(wdev);
  1020. }
  1021. return 0;
  1022. }
  1023. static int
  1024. netifd_handle_wdev_status(struct ubus_context *ctx, struct ubus_object *obj,
  1025. struct ubus_request_data *req, const char *method,
  1026. struct blob_attr *msg)
  1027. {
  1028. struct wireless_device *wdev;
  1029. int ret;
  1030. wdev = get_wdev(msg, &ret);
  1031. if (ret == UBUS_STATUS_NOT_FOUND)
  1032. return ret;
  1033. blob_buf_init(&b, 0);
  1034. if (wdev) {
  1035. wireless_device_status(wdev, &b);
  1036. } else {
  1037. vlist_for_each_element(&wireless_devices, wdev, node)
  1038. wireless_device_status(wdev, &b);
  1039. }
  1040. ubus_send_reply(ctx, req, b.head);
  1041. return 0;
  1042. }
  1043. static int
  1044. netifd_handle_wdev_get_validate(struct ubus_context *ctx, struct ubus_object *obj,
  1045. struct ubus_request_data *req, const char *method,
  1046. struct blob_attr *msg)
  1047. {
  1048. struct wireless_device *wdev;
  1049. int ret;
  1050. wdev = get_wdev(msg, &ret);
  1051. if (ret == UBUS_STATUS_NOT_FOUND)
  1052. return ret;
  1053. blob_buf_init(&b, 0);
  1054. if (wdev) {
  1055. wireless_device_get_validate(wdev, &b);
  1056. } else {
  1057. vlist_for_each_element(&wireless_devices, wdev, node)
  1058. wireless_device_get_validate(wdev, &b);
  1059. }
  1060. ubus_send_reply(ctx, req, b.head);
  1061. return 0;
  1062. }
  1063. static int
  1064. netifd_handle_wdev_notify(struct ubus_context *ctx, struct ubus_object *obj,
  1065. struct ubus_request_data *req, const char *method,
  1066. struct blob_attr *msg)
  1067. {
  1068. struct wireless_device *wdev;
  1069. int ret;
  1070. wdev = get_wdev(msg, &ret);
  1071. if (!wdev)
  1072. return ret;
  1073. return wireless_device_notify(wdev, msg, req);
  1074. }
  1075. static struct ubus_method wireless_object_methods[] = {
  1076. { .name = "up", .handler = netifd_handle_wdev_up },
  1077. { .name = "down", .handler = netifd_handle_wdev_down },
  1078. { .name = "reconf", .handler = netifd_handle_wdev_reconf },
  1079. { .name = "status", .handler = netifd_handle_wdev_status },
  1080. { .name = "notify", .handler = netifd_handle_wdev_notify },
  1081. { .name = "get_validate", .handler = netifd_handle_wdev_get_validate },
  1082. };
  1083. static struct ubus_object_type wireless_object_type =
  1084. UBUS_OBJECT_TYPE("netifd_iface", wireless_object_methods);
  1085. static struct ubus_object wireless_object = {
  1086. .name = "network.wireless",
  1087. .type = &wireless_object_type,
  1088. .methods = wireless_object_methods,
  1089. .n_methods = ARRAY_SIZE(wireless_object_methods),
  1090. };
  1091. int
  1092. netifd_extdev_invoke(uint32_t id, const char *method, struct blob_attr *msg,
  1093. ubus_data_handler_t data_cb, void *data)
  1094. {
  1095. return ubus_invoke(ubus_ctx, id, method, msg, data_cb, data, 3000);
  1096. }
  1097. int
  1098. netifd_ubus_init(const char *path)
  1099. {
  1100. ubus_path = path;
  1101. ubus_ctx = ubus_connect(path);
  1102. if (!ubus_ctx)
  1103. return -EIO;
  1104. D(SYSTEM, "connected as %08x", ubus_ctx->local_id);
  1105. ubus_ctx->connection_lost = netifd_ubus_connection_lost;
  1106. netifd_ubus_add_fd();
  1107. netifd_add_object(&main_object);
  1108. netifd_add_object(&dev_object);
  1109. netifd_add_object(&wireless_object);
  1110. netifd_add_iface_object();
  1111. udebug_ubus_init(&udebug, ubus_ctx, "netifd", netifd_udebug_config);
  1112. return 0;
  1113. }
  1114. void
  1115. netifd_ubus_done(void)
  1116. {
  1117. udebug_ubus_free(&udebug);
  1118. ubus_free(ubus_ctx);
  1119. }
  1120. void
  1121. netifd_ubus_interface_event(struct interface *iface, bool up)
  1122. {
  1123. blob_buf_init(&b, 0);
  1124. blobmsg_add_string(&b, "action", up ? "ifup" : "ifdown");
  1125. blobmsg_add_string(&b, "interface", iface->name);
  1126. ubus_send_event(ubus_ctx, "network.interface", b.head);
  1127. }
  1128. void
  1129. netifd_ubus_interface_notify(struct interface *iface, bool up)
  1130. {
  1131. const char *event = (up) ? "interface.update" : "interface.down";
  1132. blob_buf_init(&b, 0);
  1133. blobmsg_add_string(&b, "interface", iface->name);
  1134. netifd_dump_status(iface);
  1135. ubus_notify(ubus_ctx, &iface_object, event, b.head, -1);
  1136. ubus_notify(ubus_ctx, &iface->ubus, event, b.head, -1);
  1137. }
  1138. void
  1139. netifd_ubus_wireless_notify(struct wireless_device *wdev, bool up)
  1140. {
  1141. const char *event = (up) ? "wireless.update" : "wireless.down";
  1142. blob_buf_init(&b, 0);
  1143. wireless_device_status(wdev, &b);
  1144. ubus_notify(ubus_ctx, &wireless_object, event, b.head, -1);
  1145. }
  1146. void
  1147. netifd_ubus_add_interface(struct interface *iface)
  1148. {
  1149. struct ubus_object *obj = &iface->ubus;
  1150. char *name = NULL;
  1151. if (asprintf(&name, "%s.interface.%s", main_object.name, iface->name) == -1)
  1152. return;
  1153. obj->name = name;
  1154. obj->type = &iface_object_type;
  1155. obj->methods = iface_object_methods;
  1156. obj->n_methods = ARRAY_SIZE(iface_object_methods);
  1157. if (ubus_add_object(ubus_ctx, &iface->ubus)) {
  1158. D(SYSTEM, "failed to publish ubus object for interface '%s'", iface->name);
  1159. free(name);
  1160. obj->name = NULL;
  1161. }
  1162. }
  1163. void
  1164. netifd_ubus_remove_interface(struct interface *iface)
  1165. {
  1166. if (!iface->ubus.name)
  1167. return;
  1168. ubus_remove_object(ubus_ctx, &iface->ubus);
  1169. free((void *) iface->ubus.name);
  1170. }
  1171. static void
  1172. netifd_ubus_data_cb(struct ubus_request *req, int type, struct blob_attr *msg)
  1173. {
  1174. struct blob_attr *srv, *in, *t, *data;
  1175. procd_data_cb cb = req->priv;
  1176. size_t rem, rem2, rem3, rem4;
  1177. blobmsg_for_each_attr(srv, msg, rem) {
  1178. if (!blobmsg_check_attr(srv, true) ||
  1179. blobmsg_type(srv) != BLOBMSG_TYPE_TABLE)
  1180. continue;
  1181. blobmsg_for_each_attr(in, srv, rem2) {
  1182. if (!blobmsg_check_attr(in , true) ||
  1183. blobmsg_type(in) != BLOBMSG_TYPE_TABLE)
  1184. continue;
  1185. blobmsg_for_each_attr(t, in, rem3) {
  1186. if (!blobmsg_check_attr(t, true) ||
  1187. blobmsg_type(t) != BLOBMSG_TYPE_TABLE)
  1188. continue;
  1189. blobmsg_for_each_attr(data, t, rem4) {
  1190. if (!blobmsg_check_attr(t, true) ||
  1191. blobmsg_type(t) != BLOBMSG_TYPE_TABLE)
  1192. continue;
  1193. cb(data);
  1194. }
  1195. }
  1196. }
  1197. }
  1198. }
  1199. void netifd_ubus_get_procd_data(const char *type, procd_data_cb cb)
  1200. {
  1201. uint32_t id;
  1202. if (ubus_lookup_id(ubus_ctx, "service", &id))
  1203. return;
  1204. blob_buf_init(&b, 0);
  1205. blobmsg_add_string(&b, "type", type);
  1206. ubus_invoke(ubus_ctx, id, "get_data", b.head, netifd_ubus_data_cb, cb, 30000);
  1207. }