2
0

interface.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2021 Felix Fietkau <nbd@nbd.name>
  4. */
  5. #define _GNU_SOURCE
  6. #include <sys/types.h>
  7. #include <sys/socket.h>
  8. #include <sys/ioctl.h>
  9. #include <net/if_arp.h>
  10. #include <net/if.h>
  11. #include <netinet/if_ether.h>
  12. #include <unistd.h>
  13. #include <errno.h>
  14. #include <netlink/msg.h>
  15. #include <netlink/attr.h>
  16. #include <netlink/socket.h>
  17. #include <linux/rtnetlink.h>
  18. #include <linux/pkt_cls.h>
  19. #include <libubox/vlist.h>
  20. #include <libubox/avl-cmp.h>
  21. #include <libubox/uloop.h>
  22. #include "qosify.h"
  23. static void interface_update_cb(struct vlist_tree *tree,
  24. struct vlist_node *node_new,
  25. struct vlist_node *node_old);
  26. static VLIST_TREE(devices, avl_strcmp, interface_update_cb, true, false);
  27. static VLIST_TREE(interfaces, avl_strcmp, interface_update_cb, true, false);
  28. static int socket_fd;
  29. static struct nl_sock *rtnl_sock;
  30. #define APPEND(_buf, _ofs, _format, ...) _ofs += snprintf(_buf + _ofs, sizeof(_buf) - _ofs, _format, ##__VA_ARGS__)
  31. struct qosify_iface_config {
  32. struct blob_attr *data;
  33. bool ingress;
  34. bool egress;
  35. bool nat;
  36. bool host_isolate;
  37. bool autorate_ingress;
  38. const char *bandwidth_up;
  39. const char *bandwidth_down;
  40. const char *mode;
  41. const char *common_opts;
  42. const char *ingress_opts;
  43. const char *egress_opts;
  44. };
  45. struct qosify_iface {
  46. struct vlist_node node;
  47. char ifname[IFNAMSIZ];
  48. bool active;
  49. bool device;
  50. struct blob_attr *config_data;
  51. struct qosify_iface_config config;
  52. };
  53. enum {
  54. IFACE_ATTR_BW_UP,
  55. IFACE_ATTR_BW_DOWN,
  56. IFACE_ATTR_INGRESS,
  57. IFACE_ATTR_EGRESS,
  58. IFACE_ATTR_MODE,
  59. IFACE_ATTR_NAT,
  60. IFACE_ATTR_HOST_ISOLATE,
  61. IFACE_ATTR_AUTORATE_IN,
  62. IFACE_ATTR_INGRESS_OPTS,
  63. IFACE_ATTR_EGRESS_OPTS,
  64. IFACE_ATTR_OPTS,
  65. __IFACE_ATTR_MAX
  66. };
  67. static inline const char *qosify_iface_name(struct qosify_iface *iface)
  68. {
  69. return iface->node.avl.key;
  70. }
  71. static void
  72. iface_config_parse(struct blob_attr *attr, struct blob_attr **tb)
  73. {
  74. static const struct blobmsg_policy policy[__IFACE_ATTR_MAX] = {
  75. [IFACE_ATTR_BW_UP] = { "bandwidth_up", BLOBMSG_TYPE_STRING },
  76. [IFACE_ATTR_BW_DOWN] = { "bandwidth_down", BLOBMSG_TYPE_STRING },
  77. [IFACE_ATTR_INGRESS] = { "ingress", BLOBMSG_TYPE_BOOL },
  78. [IFACE_ATTR_EGRESS] = { "egress", BLOBMSG_TYPE_BOOL },
  79. [IFACE_ATTR_MODE] = { "mode", BLOBMSG_TYPE_STRING },
  80. [IFACE_ATTR_NAT] = { "nat", BLOBMSG_TYPE_BOOL },
  81. [IFACE_ATTR_HOST_ISOLATE] = { "host_isolate", BLOBMSG_TYPE_BOOL },
  82. [IFACE_ATTR_AUTORATE_IN] = { "autorate_ingress", BLOBMSG_TYPE_BOOL },
  83. [IFACE_ATTR_INGRESS_OPTS] = { "ingress_options", BLOBMSG_TYPE_STRING },
  84. [IFACE_ATTR_EGRESS_OPTS] = { "egress_options", BLOBMSG_TYPE_STRING },
  85. [IFACE_ATTR_OPTS] = { "options", BLOBMSG_TYPE_STRING },
  86. };
  87. blobmsg_parse(policy, __IFACE_ATTR_MAX, tb, blobmsg_data(attr), blobmsg_len(attr));
  88. }
  89. static bool
  90. iface_config_equal(struct qosify_iface *if1, struct qosify_iface *if2)
  91. {
  92. struct blob_attr *tb1[__IFACE_ATTR_MAX], *tb2[__IFACE_ATTR_MAX];
  93. int i;
  94. iface_config_parse(if1->config_data, tb1);
  95. iface_config_parse(if2->config_data, tb2);
  96. for (i = 0; i < __IFACE_ATTR_MAX; i++) {
  97. if (!!tb1[i] != !!tb2[i])
  98. return false;
  99. if (!tb1[i])
  100. continue;
  101. if (blob_raw_len(tb1[i]) != blob_raw_len(tb2[i]))
  102. return false;
  103. if (memcmp(tb1[i], tb2[i], blob_raw_len(tb1[i])) != 0)
  104. return false;
  105. }
  106. return true;
  107. }
  108. static const char *check_str(struct blob_attr *attr)
  109. {
  110. const char *str = blobmsg_get_string(attr);
  111. if (strchr(str, '\''))
  112. return NULL;
  113. return str;
  114. }
  115. static void
  116. iface_config_set(struct qosify_iface *iface, struct blob_attr *attr)
  117. {
  118. struct qosify_iface_config *cfg = &iface->config;
  119. struct blob_attr *tb[__IFACE_ATTR_MAX];
  120. struct blob_attr *cur;
  121. iface_config_parse(attr, tb);
  122. memset(cfg, 0, sizeof(*cfg));
  123. /* defaults */
  124. cfg->mode = "diffserv4";
  125. cfg->ingress = true;
  126. cfg->egress = true;
  127. cfg->host_isolate = true;
  128. cfg->autorate_ingress = false;
  129. cfg->nat = !iface->device;
  130. if ((cur = tb[IFACE_ATTR_BW_UP]) != NULL)
  131. cfg->bandwidth_up = check_str(cur);
  132. if ((cur = tb[IFACE_ATTR_BW_DOWN]) != NULL)
  133. cfg->bandwidth_down = check_str(cur);
  134. if ((cur = tb[IFACE_ATTR_MODE]) != NULL)
  135. cfg->mode = check_str(cur);
  136. if ((cur = tb[IFACE_ATTR_OPTS]) != NULL)
  137. cfg->common_opts = check_str(cur);
  138. if ((cur = tb[IFACE_ATTR_EGRESS_OPTS]) != NULL)
  139. cfg->egress_opts = check_str(cur);
  140. if ((cur = tb[IFACE_ATTR_INGRESS_OPTS]) != NULL)
  141. cfg->ingress_opts = check_str(cur);
  142. if ((cur = tb[IFACE_ATTR_INGRESS]) != NULL)
  143. cfg->ingress = blobmsg_get_bool(cur);
  144. if ((cur = tb[IFACE_ATTR_EGRESS]) != NULL)
  145. cfg->egress = blobmsg_get_bool(cur);
  146. if ((cur = tb[IFACE_ATTR_NAT]) != NULL)
  147. cfg->nat = blobmsg_get_bool(cur);
  148. if ((cur = tb[IFACE_ATTR_HOST_ISOLATE]) != NULL)
  149. cfg->host_isolate = blobmsg_get_bool(cur);
  150. if ((cur = tb[IFACE_ATTR_AUTORATE_IN]) != NULL)
  151. cfg->autorate_ingress = blobmsg_get_bool(cur);
  152. }
  153. static const char *
  154. interface_ifb_name(struct qosify_iface *iface)
  155. {
  156. static char ifname[IFNAMSIZ + 1] = "ifb-";
  157. int len = strlen(iface->ifname);
  158. if (len + 4 < IFNAMSIZ) {
  159. snprintf(ifname + 4, IFNAMSIZ - 4, "%s", iface->ifname);
  160. return ifname;
  161. }
  162. ifname[4] = iface->ifname[0];
  163. ifname[5] = iface->ifname[1];
  164. snprintf(ifname + 6, IFNAMSIZ - 6, "%s", iface->ifname + len - (IFNAMSIZ + 6) - 1);
  165. return ifname;
  166. }
  167. static int
  168. prepare_qdisc_cmd(char *buf, int len, const char *dev, bool add, const char *type)
  169. {
  170. return snprintf(buf, len, "tc qdisc %s dev '%s' %s",
  171. add ? "add" : "del", dev, type);
  172. }
  173. static int
  174. prepare_filter_cmd(char *buf, int len, const char *dev, int prio, bool add, bool egress)
  175. {
  176. return snprintf(buf, len, "tc filter %s dev '%s' %sgress prio %d",
  177. add ? "add" : "del", dev, egress ? "e" : "in", prio);
  178. }
  179. static int
  180. cmd_add_bpf_filter(const char *ifname, int prio, bool egress, bool eth)
  181. {
  182. struct tcmsg tcmsg = {
  183. .tcm_family = AF_UNSPEC,
  184. .tcm_ifindex = if_nametoindex(ifname),
  185. };
  186. struct nl_msg *msg;
  187. struct nlattr *opts;
  188. const char *suffix;
  189. int prog_fd = -1;
  190. char name[32];
  191. suffix = qosify_get_program(!egress * QOSIFY_INGRESS + !eth * QOSIFY_IP_ONLY, &prog_fd);
  192. if (!suffix)
  193. return -1;
  194. snprintf(name, sizeof(name), "qosify_%s", suffix);
  195. if (egress)
  196. tcmsg.tcm_parent = TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_EGRESS);
  197. else
  198. tcmsg.tcm_parent = TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_INGRESS);
  199. tcmsg.tcm_info = TC_H_MAKE(prio << 16, htons(ETH_P_ALL));
  200. msg = nlmsg_alloc_simple(RTM_NEWTFILTER, NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL);
  201. nlmsg_append(msg, &tcmsg, sizeof(tcmsg), NLMSG_ALIGNTO);
  202. nla_put_string(msg, TCA_KIND, "bpf");
  203. opts = nla_nest_start(msg, TCA_OPTIONS);
  204. nla_put_u32(msg, TCA_BPF_FD, prog_fd);
  205. nla_put_string(msg, TCA_BPF_NAME, name);
  206. nla_put_u32(msg, TCA_BPF_FLAGS, TCA_BPF_FLAG_ACT_DIRECT);
  207. nla_put_u32(msg, TCA_BPF_FLAGS_GEN, TCA_CLS_FLAGS_SKIP_HW);
  208. nla_nest_end(msg, opts);
  209. nl_send_auto_complete(rtnl_sock, msg);
  210. nlmsg_free(msg);
  211. return nl_wait_for_ack(rtnl_sock);
  212. }
  213. static int
  214. cmd_add_qdisc(struct qosify_iface *iface, const char *ifname, bool egress, bool eth)
  215. {
  216. struct qosify_iface_config *cfg = &iface->config;
  217. const char *bw = egress ? cfg->bandwidth_up : cfg->bandwidth_down;
  218. const char *dir_opts = egress ? cfg->egress_opts : cfg->ingress_opts;
  219. char buf[512];
  220. int ofs;
  221. ofs = prepare_qdisc_cmd(buf, sizeof(buf), ifname, true, "clsact");
  222. qosify_run_cmd(buf, true);
  223. ofs = prepare_qdisc_cmd(buf, sizeof(buf), ifname, true, "root cake");
  224. if (bw)
  225. APPEND(buf, ofs, " bandwidth %s", bw);
  226. APPEND(buf, ofs, " %s %sgress", cfg->mode, egress ? "e" : "in");
  227. if (!egress && cfg->autorate_ingress)
  228. APPEND(buf, ofs, " autorate-ingress");
  229. if (cfg->host_isolate)
  230. APPEND(buf, ofs, " %snat dual-%shost",
  231. cfg->nat ? "" : "no",
  232. egress ? "src" : "dst");
  233. else
  234. APPEND(buf, ofs, " flows");
  235. APPEND(buf, ofs, " %s %s",
  236. cfg->common_opts ? cfg->common_opts : "",
  237. dir_opts ? dir_opts : "");
  238. return qosify_run_cmd(buf, false);
  239. }
  240. static int
  241. cmd_add_ingress(struct qosify_iface *iface, bool eth)
  242. {
  243. const char *ifbdev = interface_ifb_name(iface);
  244. char buf[256];
  245. int prio = QOSIFY_PRIO_BASE;
  246. int ofs;
  247. cmd_add_bpf_filter(iface->ifname, prio++, false, eth);
  248. ofs = prepare_filter_cmd(buf, sizeof(buf), iface->ifname, prio++, true, false);
  249. APPEND(buf, ofs, " protocol ip u32 match ip sport 53 0xffff "
  250. "flowid 1:1 action mirred egress redirect dev " QOSIFY_DNS_IFNAME);
  251. qosify_run_cmd(buf, false);
  252. ofs = prepare_filter_cmd(buf, sizeof(buf), iface->ifname, prio++, true, false);
  253. APPEND(buf, ofs, " protocol 802.1Q u32 offset plus 4 match ip sport 53 0xffff "
  254. "flowid 1:1 action mirred egress redirect dev " QOSIFY_DNS_IFNAME);
  255. qosify_run_cmd(buf, false);
  256. ofs = prepare_filter_cmd(buf, sizeof(buf), iface->ifname, prio++, true, false);
  257. APPEND(buf, ofs, " protocol ipv6 u32 match ip6 sport 53 0xffff "
  258. "flowid 1:1 action mirred egress redirect dev " QOSIFY_DNS_IFNAME);
  259. qosify_run_cmd(buf, false);
  260. ofs = prepare_filter_cmd(buf, sizeof(buf), iface->ifname, prio++, true, false);
  261. APPEND(buf, ofs, " protocol ipv6 u32 offset plus 4 match ip6 sport 53 0xffff "
  262. "flowid 1:1 action mirred egress redirect dev " QOSIFY_DNS_IFNAME);
  263. qosify_run_cmd(buf, false);
  264. if (!iface->config.ingress)
  265. return 0;
  266. snprintf(buf, sizeof(buf), "ip link add '%s' type ifb", ifbdev);
  267. qosify_run_cmd(buf, false);
  268. cmd_add_qdisc(iface, ifbdev, false, eth);
  269. snprintf(buf, sizeof(buf), "ip link set dev '%s' up", ifbdev);
  270. qosify_run_cmd(buf, false);
  271. ofs = prepare_filter_cmd(buf, sizeof(buf), iface->ifname, prio++, true, false);
  272. APPEND(buf, ofs, " protocol all u32 match u32 0 0 flowid 1:1"
  273. " action mirred egress redirect dev '%s'", ifbdev);
  274. return qosify_run_cmd(buf, false);
  275. }
  276. static int cmd_add_egress(struct qosify_iface *iface, bool eth)
  277. {
  278. if (!iface->config.egress)
  279. return 0;
  280. cmd_add_qdisc(iface, iface->ifname, true, eth);
  281. return cmd_add_bpf_filter(iface->ifname, QOSIFY_PRIO_BASE, true, eth);
  282. }
  283. static void
  284. interface_clear_qdisc(struct qosify_iface *iface)
  285. {
  286. char buf[64];
  287. int i;
  288. prepare_qdisc_cmd(buf, sizeof(buf), iface->ifname, false, "root");
  289. qosify_run_cmd(buf, true);
  290. for (i = 0; i < 6; i++) {
  291. prepare_filter_cmd(buf, sizeof(buf), iface->ifname, QOSIFY_PRIO_BASE + i, false, false);
  292. qosify_run_cmd(buf, true);
  293. }
  294. prepare_filter_cmd(buf, sizeof(buf), iface->ifname, QOSIFY_PRIO_BASE, false, true);
  295. qosify_run_cmd(buf, true);
  296. snprintf(buf, sizeof(buf), "ip link del '%s'", interface_ifb_name(iface));
  297. qosify_run_cmd(buf, true);
  298. }
  299. static void
  300. interface_start(struct qosify_iface *iface)
  301. {
  302. struct ifreq ifr = {};
  303. bool eth;
  304. if (!iface->ifname[0] || iface->active)
  305. return;
  306. ULOG_INFO("start interface %s\n", iface->ifname);
  307. strncpy(ifr.ifr_name, iface->ifname, sizeof(ifr.ifr_name));
  308. if (ioctl(socket_fd, SIOCGIFHWADDR, &ifr) < 0) {
  309. ULOG_ERR("ioctl(SIOCGIFHWADDR, %s) failed: %s\n", iface->ifname, strerror(errno));
  310. return;
  311. }
  312. eth = ifr.ifr_hwaddr.sa_family == ARPHRD_ETHER;
  313. interface_clear_qdisc(iface);
  314. cmd_add_egress(iface, eth);
  315. cmd_add_ingress(iface, eth);
  316. iface->active = true;
  317. }
  318. static void
  319. interface_stop(struct qosify_iface *iface)
  320. {
  321. if (!iface->ifname[0] || !iface->active)
  322. return;
  323. ULOG_INFO("stop interface %s\n", iface->ifname);
  324. iface->active = false;
  325. interface_clear_qdisc(iface);
  326. }
  327. static void
  328. interface_set_config(struct qosify_iface *iface, struct blob_attr *config)
  329. {
  330. iface->config_data = blob_memdup(config);
  331. iface_config_set(iface, iface->config_data);
  332. interface_start(iface);
  333. }
  334. static void
  335. interface_update_cb(struct vlist_tree *tree,
  336. struct vlist_node *node_new, struct vlist_node *node_old)
  337. {
  338. struct qosify_iface *if_new = NULL, *if_old = NULL;
  339. if (node_new)
  340. if_new = container_of(node_new, struct qosify_iface, node);
  341. if (node_old)
  342. if_old = container_of(node_old, struct qosify_iface, node);
  343. if (if_new && if_old) {
  344. if (!iface_config_equal(if_old, if_new)) {
  345. interface_stop(if_old);
  346. free(if_old->config_data);
  347. interface_set_config(if_old, if_new->config_data);
  348. }
  349. free(if_new);
  350. return;
  351. }
  352. if (if_old) {
  353. interface_stop(if_old);
  354. free(if_old->config_data);
  355. free(if_old);
  356. }
  357. if (if_new)
  358. interface_set_config(if_new, if_new->config_data);
  359. }
  360. static void
  361. interface_create(struct blob_attr *attr, bool device)
  362. {
  363. struct qosify_iface *iface;
  364. const char *name = blobmsg_name(attr);
  365. int name_len = strlen(name);
  366. char *name_buf;
  367. if (strchr(name, '\''))
  368. return;
  369. if (name_len >= IFNAMSIZ)
  370. return;
  371. if (blobmsg_type(attr) != BLOBMSG_TYPE_TABLE)
  372. return;
  373. iface = calloc_a(sizeof(*iface), &name_buf, name_len + 1);
  374. strcpy(name_buf, blobmsg_name(attr));
  375. iface->config_data = attr;
  376. iface->device = device;
  377. vlist_add(device ? &devices : &interfaces, &iface->node, name_buf);
  378. }
  379. void qosify_iface_config_update(struct blob_attr *ifaces, struct blob_attr *devs)
  380. {
  381. struct blob_attr *cur;
  382. int rem;
  383. vlist_update(&devices);
  384. blobmsg_for_each_attr(cur, devs, rem)
  385. interface_create(cur, true);
  386. vlist_flush(&devices);
  387. vlist_update(&interfaces);
  388. blobmsg_for_each_attr(cur, ifaces, rem)
  389. interface_create(cur, false);
  390. vlist_flush(&interfaces);
  391. }
  392. static void
  393. qosify_iface_check_device(struct qosify_iface *iface)
  394. {
  395. const char *name = qosify_iface_name(iface);
  396. int ifindex;
  397. ifindex = if_nametoindex(name);
  398. if (!ifindex) {
  399. interface_stop(iface);
  400. iface->ifname[0] = 0;
  401. } else {
  402. snprintf(iface->ifname, sizeof(iface->ifname), "%s", name);
  403. interface_start(iface);
  404. }
  405. }
  406. static void
  407. qosify_iface_check_interface(struct qosify_iface *iface)
  408. {
  409. const char *name = qosify_iface_name(iface);
  410. char ifname[IFNAMSIZ];
  411. if (qosify_ubus_check_interface(name, ifname, sizeof(ifname)) == 0) {
  412. snprintf(iface->ifname, sizeof(iface->ifname), "%s", ifname);
  413. interface_start(iface);
  414. } else {
  415. interface_stop(iface);
  416. iface->ifname[0] = 0;
  417. }
  418. }
  419. static void qos_iface_check_cb(struct uloop_timeout *t)
  420. {
  421. struct qosify_iface *iface;
  422. vlist_for_each_element(&devices, iface, node)
  423. qosify_iface_check_device(iface);
  424. vlist_for_each_element(&interfaces, iface, node)
  425. qosify_iface_check_interface(iface);
  426. qosify_ubus_update_bridger(false);
  427. }
  428. void qosify_iface_check(void)
  429. {
  430. static struct uloop_timeout timer = {
  431. .cb = qos_iface_check_cb,
  432. };
  433. uloop_timeout_set(&timer, 10);
  434. }
  435. static void
  436. __qosify_iface_status(struct blob_buf *b, struct qosify_iface *iface)
  437. {
  438. void *c;
  439. c = blobmsg_open_table(b, qosify_iface_name(iface));
  440. blobmsg_add_u8(b, "active", iface->active);
  441. if (iface->ifname[0])
  442. blobmsg_add_string(b, "ifname", iface->ifname);
  443. blobmsg_add_u8(b, "egress", iface->config.egress);
  444. blobmsg_add_u8(b, "ingress", iface->config.ingress);
  445. blobmsg_close_table(b, c);
  446. }
  447. void qosify_iface_status(struct blob_buf *b)
  448. {
  449. struct qosify_iface *iface;
  450. void *c;
  451. c = blobmsg_open_table(b, "devices");
  452. vlist_for_each_element(&devices, iface, node)
  453. __qosify_iface_status(b, iface);
  454. blobmsg_close_table(b, c);
  455. c = blobmsg_open_table(b, "interfaces");
  456. vlist_for_each_element(&interfaces, iface, node)
  457. __qosify_iface_status(b, iface);
  458. blobmsg_close_table(b, c);
  459. }
  460. static int
  461. qosify_nl_error_cb(struct sockaddr_nl *nla, struct nlmsgerr *err,
  462. void *arg)
  463. {
  464. struct nlmsghdr *nlh = (struct nlmsghdr *) err - 1;
  465. struct nlattr *tb[NLMSGERR_ATTR_MAX + 1];
  466. struct nlattr *attrs;
  467. int ack_len = sizeof(*nlh) + sizeof(int) + sizeof(*nlh);
  468. int len = nlh->nlmsg_len;
  469. const char *errstr = "(unknown)";
  470. if (!(nlh->nlmsg_flags & NLM_F_ACK_TLVS))
  471. return NL_STOP;
  472. if (!(nlh->nlmsg_flags & NLM_F_CAPPED))
  473. ack_len += err->msg.nlmsg_len - sizeof(*nlh);
  474. attrs = (void *) ((unsigned char *) nlh + ack_len);
  475. len -= ack_len;
  476. nla_parse(tb, NLMSGERR_ATTR_MAX, attrs, len, NULL);
  477. if (tb[NLMSGERR_ATTR_MSG])
  478. errstr = nla_data(tb[NLMSGERR_ATTR_MSG]);
  479. ULOG_ERR("Netlink error(%d): %s\n", err->error, errstr);
  480. return NL_STOP;
  481. }
  482. static void
  483. __qosify_iface_get_device(struct blob_buf *b, struct qosify_iface *iface)
  484. {
  485. if (!iface->ifname[0] || !iface->active)
  486. return;
  487. blobmsg_add_string(b, NULL, iface->ifname);
  488. }
  489. void qosify_iface_get_devices(struct blob_buf *b)
  490. {
  491. struct qosify_iface *iface;
  492. vlist_for_each_element(&devices, iface, node)
  493. __qosify_iface_get_device(b, iface);
  494. vlist_for_each_element(&interfaces, iface, node)
  495. __qosify_iface_get_device(b, iface);
  496. }
  497. int qosify_iface_init(void)
  498. {
  499. int fd, opt;
  500. socket_fd = socket(AF_UNIX, SOCK_DGRAM, 0);
  501. if (socket < 0)
  502. return -1;
  503. rtnl_sock = nl_socket_alloc();
  504. if (!rtnl_sock)
  505. return -1;
  506. if (nl_connect(rtnl_sock, NETLINK_ROUTE))
  507. return -1;
  508. nl_cb_err(nl_socket_get_cb(rtnl_sock), NL_CB_CUSTOM,
  509. qosify_nl_error_cb, NULL);
  510. fd = nl_socket_get_fd(rtnl_sock);
  511. opt = 1;
  512. setsockopt(fd, SOL_NETLINK, NETLINK_EXT_ACK, &opt, sizeof(opt));
  513. opt = 1;
  514. setsockopt(fd, SOL_NETLINK, NETLINK_CAP_ACK, &opt, sizeof(opt));
  515. return 0;
  516. }
  517. void qosify_iface_stop(void)
  518. {
  519. struct qosify_iface *iface;
  520. vlist_for_each_element(&interfaces, iface, node)
  521. interface_stop(iface);
  522. vlist_for_each_element(&devices, iface, node)
  523. interface_stop(iface);
  524. nl_socket_free(rtnl_sock);
  525. }