interface.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2021 Felix Fietkau <nbd@nbd.name>
  4. */
  5. #include <sys/types.h>
  6. #include <sys/socket.h>
  7. #include <sys/ioctl.h>
  8. #include <net/if_arp.h>
  9. #include <net/if.h>
  10. #include <unistd.h>
  11. #include <errno.h>
  12. #include <libubox/vlist.h>
  13. #include <libubox/avl-cmp.h>
  14. #include <libubox/uloop.h>
  15. #include "qosify.h"
  16. static void interface_update_cb(struct vlist_tree *tree,
  17. struct vlist_node *node_new,
  18. struct vlist_node *node_old);
  19. static VLIST_TREE(devices, avl_strcmp, interface_update_cb, true, false);
  20. static VLIST_TREE(interfaces, avl_strcmp, interface_update_cb, true, false);
  21. static int socket_fd;
  22. #define APPEND(_buf, _ofs, _format, ...) _ofs += snprintf(_buf + _ofs, sizeof(_buf) - _ofs, _format, ##__VA_ARGS__)
  23. struct qosify_iface_config {
  24. struct blob_attr *data;
  25. bool ingress;
  26. bool egress;
  27. bool nat;
  28. bool host_isolate;
  29. bool autorate_ingress;
  30. const char *bandwidth_up;
  31. const char *bandwidth_down;
  32. const char *mode;
  33. const char *common_opts;
  34. const char *ingress_opts;
  35. const char *egress_opts;
  36. };
  37. struct qosify_iface {
  38. struct vlist_node node;
  39. char ifname[IFNAMSIZ];
  40. bool active;
  41. bool device;
  42. struct blob_attr *config_data;
  43. struct qosify_iface_config config;
  44. };
  45. enum {
  46. IFACE_ATTR_BW_UP,
  47. IFACE_ATTR_BW_DOWN,
  48. IFACE_ATTR_INGRESS,
  49. IFACE_ATTR_EGRESS,
  50. IFACE_ATTR_MODE,
  51. IFACE_ATTR_NAT,
  52. IFACE_ATTR_HOST_ISOLATE,
  53. IFACE_ATTR_AUTORATE_IN,
  54. IFACE_ATTR_INGRESS_OPTS,
  55. IFACE_ATTR_EGRESS_OPTS,
  56. IFACE_ATTR_OPTS,
  57. __IFACE_ATTR_MAX
  58. };
  59. static inline const char *qosify_iface_name(struct qosify_iface *iface)
  60. {
  61. return iface->node.avl.key;
  62. }
  63. static void
  64. iface_config_parse(struct blob_attr *attr, struct blob_attr **tb)
  65. {
  66. static const struct blobmsg_policy policy[__IFACE_ATTR_MAX] = {
  67. [IFACE_ATTR_BW_UP] = { "bandwidth_up", BLOBMSG_TYPE_STRING },
  68. [IFACE_ATTR_BW_DOWN] = { "bandwidth_down", BLOBMSG_TYPE_STRING },
  69. [IFACE_ATTR_INGRESS] = { "ingress", BLOBMSG_TYPE_BOOL },
  70. [IFACE_ATTR_EGRESS] = { "egress", BLOBMSG_TYPE_BOOL },
  71. [IFACE_ATTR_MODE] = { "mode", BLOBMSG_TYPE_STRING },
  72. [IFACE_ATTR_NAT] = { "nat", BLOBMSG_TYPE_BOOL },
  73. [IFACE_ATTR_HOST_ISOLATE] = { "host_isolate", BLOBMSG_TYPE_BOOL },
  74. [IFACE_ATTR_AUTORATE_IN] = { "autorate_ingress", BLOBMSG_TYPE_BOOL },
  75. [IFACE_ATTR_INGRESS_OPTS] = { "ingress_options", BLOBMSG_TYPE_STRING },
  76. [IFACE_ATTR_EGRESS_OPTS] = { "egress_options", BLOBMSG_TYPE_STRING },
  77. [IFACE_ATTR_OPTS] = { "options", BLOBMSG_TYPE_STRING },
  78. };
  79. blobmsg_parse(policy, __IFACE_ATTR_MAX, tb, blobmsg_data(attr), blobmsg_len(attr));
  80. }
  81. static bool
  82. iface_config_equal(struct qosify_iface *if1, struct qosify_iface *if2)
  83. {
  84. struct blob_attr *tb1[__IFACE_ATTR_MAX], *tb2[__IFACE_ATTR_MAX];
  85. int i;
  86. iface_config_parse(if1->config_data, tb1);
  87. iface_config_parse(if2->config_data, tb2);
  88. for (i = 0; i < __IFACE_ATTR_MAX; i++) {
  89. if (!!tb1[i] != !!tb2[i])
  90. return false;
  91. if (!tb1[i])
  92. continue;
  93. if (blob_raw_len(tb1[i]) != blob_raw_len(tb2[i]))
  94. return false;
  95. if (memcmp(tb1[i], tb2[i], blob_raw_len(tb1[i])) != 0)
  96. return false;
  97. }
  98. return true;
  99. }
  100. static const char *check_str(struct blob_attr *attr)
  101. {
  102. const char *str = blobmsg_get_string(attr);
  103. if (strchr(str, '\''))
  104. return NULL;
  105. return str;
  106. }
  107. static void
  108. iface_config_set(struct qosify_iface *iface, struct blob_attr *attr)
  109. {
  110. struct qosify_iface_config *cfg = &iface->config;
  111. struct blob_attr *tb[__IFACE_ATTR_MAX];
  112. struct blob_attr *cur;
  113. iface_config_parse(attr, tb);
  114. memset(cfg, 0, sizeof(*cfg));
  115. /* defaults */
  116. cfg->mode = "diffserv4";
  117. cfg->ingress = true;
  118. cfg->egress = true;
  119. cfg->host_isolate = true;
  120. cfg->autorate_ingress = false;
  121. cfg->nat = !iface->device;
  122. if ((cur = tb[IFACE_ATTR_BW_UP]) != NULL)
  123. cfg->bandwidth_up = check_str(cur);
  124. if ((cur = tb[IFACE_ATTR_BW_DOWN]) != NULL)
  125. cfg->bandwidth_down = check_str(cur);
  126. if ((cur = tb[IFACE_ATTR_MODE]) != NULL)
  127. cfg->mode = check_str(cur);
  128. if ((cur = tb[IFACE_ATTR_OPTS]) != NULL)
  129. cfg->common_opts = check_str(cur);
  130. if ((cur = tb[IFACE_ATTR_EGRESS_OPTS]) != NULL)
  131. cfg->egress_opts = check_str(cur);
  132. if ((cur = tb[IFACE_ATTR_INGRESS_OPTS]) != NULL)
  133. cfg->ingress_opts = check_str(cur);
  134. if ((cur = tb[IFACE_ATTR_INGRESS]) != NULL)
  135. cfg->ingress = blobmsg_get_bool(cur);
  136. if ((cur = tb[IFACE_ATTR_EGRESS]) != NULL)
  137. cfg->egress = blobmsg_get_bool(cur);
  138. if ((cur = tb[IFACE_ATTR_NAT]) != NULL)
  139. cfg->nat = blobmsg_get_bool(cur);
  140. if ((cur = tb[IFACE_ATTR_HOST_ISOLATE]) != NULL)
  141. cfg->host_isolate = blobmsg_get_bool(cur);
  142. if ((cur = tb[IFACE_ATTR_AUTORATE_IN]) != NULL)
  143. cfg->autorate_ingress = blobmsg_get_bool(cur);
  144. }
  145. static const char *
  146. interface_ifb_name(struct qosify_iface *iface)
  147. {
  148. static char ifname[IFNAMSIZ + 1] = "ifb-";
  149. int len = strlen(iface->ifname);
  150. if (len + 4 < IFNAMSIZ) {
  151. snprintf(ifname + 4, IFNAMSIZ - 4, "%s", iface->ifname);
  152. return ifname;
  153. }
  154. ifname[4] = iface->ifname[0];
  155. ifname[5] = iface->ifname[1];
  156. snprintf(ifname + 6, IFNAMSIZ - 6, "%s", iface->ifname + len - (IFNAMSIZ + 6) - 1);
  157. return ifname;
  158. }
  159. static int
  160. prepare_tc_cmd(char *buf, int len, const char *type, const char *cmd,
  161. const char *dev, const char *extra)
  162. {
  163. return snprintf(buf, len, "tc %s %s dev '%s' %s", type, cmd, dev, extra);
  164. }
  165. static int
  166. __cmd_add_del_qdisc(const char *ifname, const char *type, bool add)
  167. {
  168. char buf[64];
  169. prepare_tc_cmd(buf, sizeof(buf), "qdisc", add ? "add" : "del", ifname, type);
  170. return qosify_run_cmd(buf, true);
  171. }
  172. static int
  173. cmd_del_qdisc(const char *ifname, const char *type)
  174. {
  175. return __cmd_add_del_qdisc(ifname, type, false);
  176. }
  177. static int
  178. cmd_add_qdisc(struct qosify_iface *iface, const char *ifname, bool egress, bool eth)
  179. {
  180. struct qosify_iface_config *cfg = &iface->config;
  181. const char *bw = egress ? cfg->bandwidth_up : cfg->bandwidth_down;
  182. const char *dir_opts = egress ? cfg->egress_opts : cfg->ingress_opts;
  183. char buf[512];
  184. int ofs;
  185. __cmd_add_del_qdisc(ifname, "clsact", true);
  186. ofs = prepare_tc_cmd(buf, sizeof(buf), "qdisc", "add", ifname, "root cake");
  187. if (bw)
  188. APPEND(buf, ofs, " bandwidth %s", bw);
  189. APPEND(buf, ofs, " %s %sgress", cfg->mode, egress ? "e" : "in");
  190. if (!egress && cfg->autorate_ingress)
  191. APPEND(buf, ofs, " autorate-ingress");
  192. if (cfg->host_isolate)
  193. APPEND(buf, ofs, " %snat dual-%shost",
  194. cfg->nat ? "" : "no",
  195. egress ? "src" : "dst");
  196. else
  197. APPEND(buf, ofs, " flows");
  198. APPEND(buf, ofs, " %s %s",
  199. cfg->common_opts ? cfg->common_opts : "",
  200. dir_opts ? dir_opts : "");
  201. qosify_run_cmd(buf, false);
  202. ofs = prepare_tc_cmd(buf, sizeof(buf), "filter", "add", ifname, "egress bpf");
  203. APPEND(buf, ofs, " object-pinned /sys/fs/bpf/qosify_%sgress_%s verbose direct-action",
  204. egress ? "e" : "in",
  205. eth ? "eth" : "ip");
  206. return qosify_run_cmd(buf, false);
  207. }
  208. static int
  209. cmd_del_ingress(struct qosify_iface *iface)
  210. {
  211. char buf[256];
  212. snprintf(buf, sizeof(buf), "ip link del '%s'", interface_ifb_name(iface));
  213. return qosify_run_cmd(buf, true);
  214. }
  215. static int
  216. cmd_add_ingress(struct qosify_iface *iface, bool eth)
  217. {
  218. const char *ifbdev = interface_ifb_name(iface);
  219. char buf[256];
  220. int ofs;
  221. ofs = prepare_tc_cmd(buf, sizeof(buf), "filter", "add", iface->ifname, " ingress");
  222. APPEND(buf, ofs, " protocol ip prio 5 u32 match ip sport 53 0xffff "
  223. "flowid 1:1 action mirred egress redirect dev ifb-dns");
  224. qosify_run_cmd(buf, false);
  225. ofs = prepare_tc_cmd(buf, sizeof(buf), "filter", "add", iface->ifname, " ingress");
  226. APPEND(buf, ofs, " protocol 802.1Q prio 6 u32 offset plus 4 match ip sport 53 0xffff "
  227. "flowid 1:1 action mirred egress redirect dev ifb-dns");
  228. qosify_run_cmd(buf, false);
  229. ofs = prepare_tc_cmd(buf, sizeof(buf), "filter", "add", iface->ifname, " ingress");
  230. APPEND(buf, ofs, " protocol ipv6 prio 7 u32 match ip6 sport 53 0xffff "
  231. "flowid 1:1 action mirred egress redirect dev ifb-dns");
  232. qosify_run_cmd(buf, false);
  233. ofs = prepare_tc_cmd(buf, sizeof(buf), "filter", "add", iface->ifname, " ingress");
  234. APPEND(buf, ofs, " protocol ipv6 prio 8 u32 offset plus 4 match ip6 sport 53 0xffff "
  235. "flowid 1:1 action mirred egress redirect dev ifb-dns");
  236. qosify_run_cmd(buf, false);
  237. if (!iface->config.ingress)
  238. return 0;
  239. snprintf(buf, sizeof(buf), "ip link add '%s' type ifb", ifbdev);
  240. qosify_run_cmd(buf, false);
  241. cmd_add_qdisc(iface, ifbdev, false, eth);
  242. snprintf(buf, sizeof(buf), "ip link set dev '%s' up", ifbdev);
  243. qosify_run_cmd(buf, false);
  244. ofs = prepare_tc_cmd(buf, sizeof(buf), "filter", "add", iface->ifname, " ingress");
  245. APPEND(buf, ofs, " protocol all prio 10 u32 match u32 0 0 "
  246. "flowid 1:1 action mirred egress redirect dev '%s'", ifbdev);
  247. return qosify_run_cmd(buf, false);
  248. }
  249. static void
  250. interface_clear_qdisc(struct qosify_iface *iface)
  251. {
  252. cmd_del_qdisc(iface->ifname, "root");
  253. cmd_del_qdisc(iface->ifname, "clsact");
  254. cmd_del_ingress(iface);
  255. }
  256. static void
  257. interface_start(struct qosify_iface *iface)
  258. {
  259. struct ifreq ifr = {};
  260. bool eth;
  261. if (!iface->ifname[0] || iface->active)
  262. return;
  263. ULOG_INFO("start interface %s\n", iface->ifname);
  264. strncpy(ifr.ifr_name, iface->ifname, sizeof(ifr.ifr_name));
  265. if (ioctl(socket_fd, SIOCGIFHWADDR, &ifr) < 0) {
  266. ULOG_ERR("ioctl(SIOCGIFHWADDR, %s) failed: %s\n", iface->ifname, strerror(errno));
  267. return;
  268. }
  269. eth = ifr.ifr_hwaddr.sa_family == ARPHRD_ETHER;
  270. interface_clear_qdisc(iface);
  271. if (iface->config.egress)
  272. cmd_add_qdisc(iface, iface->ifname, true, eth);
  273. cmd_add_ingress(iface, eth);
  274. iface->active = true;
  275. }
  276. static void
  277. interface_stop(struct qosify_iface *iface)
  278. {
  279. if (!iface->ifname[0] || !iface->active)
  280. return;
  281. ULOG_INFO("stop interface %s\n", iface->ifname);
  282. iface->active = false;
  283. interface_clear_qdisc(iface);
  284. }
  285. static void
  286. interface_set_config(struct qosify_iface *iface, struct blob_attr *config)
  287. {
  288. iface->config_data = blob_memdup(config);
  289. iface_config_set(iface, iface->config_data);
  290. interface_start(iface);
  291. }
  292. static void
  293. interface_update_cb(struct vlist_tree *tree,
  294. struct vlist_node *node_new, struct vlist_node *node_old)
  295. {
  296. struct qosify_iface *if_new = NULL, *if_old = NULL;
  297. if (node_new)
  298. if_new = container_of(node_new, struct qosify_iface, node);
  299. if (node_old)
  300. if_old = container_of(node_old, struct qosify_iface, node);
  301. if (if_new && if_old) {
  302. if (!iface_config_equal(if_old, if_new)) {
  303. interface_stop(if_old);
  304. free(if_old->config_data);
  305. interface_set_config(if_old, if_new->config_data);
  306. }
  307. free(if_new);
  308. return;
  309. }
  310. if (if_old) {
  311. interface_stop(if_old);
  312. free(if_old->config_data);
  313. free(if_old);
  314. }
  315. if (if_new)
  316. interface_set_config(if_new, if_new->config_data);
  317. }
  318. static void
  319. interface_create(struct blob_attr *attr, bool device)
  320. {
  321. struct qosify_iface *iface;
  322. const char *name = blobmsg_name(attr);
  323. int name_len = strlen(name);
  324. char *name_buf;
  325. if (strchr(name, '\''))
  326. return;
  327. if (name_len >= IFNAMSIZ)
  328. return;
  329. if (blobmsg_type(attr) != BLOBMSG_TYPE_TABLE)
  330. return;
  331. iface = calloc_a(sizeof(*iface), &name_buf, name_len + 1);
  332. strcpy(name_buf, blobmsg_name(attr));
  333. iface->config_data = attr;
  334. iface->device = device;
  335. vlist_add(device ? &devices : &interfaces, &iface->node, name_buf);
  336. }
  337. void qosify_iface_config_update(struct blob_attr *ifaces, struct blob_attr *devs)
  338. {
  339. struct blob_attr *cur;
  340. int rem;
  341. vlist_update(&devices);
  342. blobmsg_for_each_attr(cur, devs, rem)
  343. interface_create(cur, true);
  344. vlist_flush(&devices);
  345. vlist_update(&interfaces);
  346. blobmsg_for_each_attr(cur, ifaces, rem)
  347. interface_create(cur, false);
  348. vlist_flush(&interfaces);
  349. }
  350. static void
  351. qosify_iface_check_device(struct qosify_iface *iface)
  352. {
  353. const char *name = qosify_iface_name(iface);
  354. int ifindex;
  355. ifindex = if_nametoindex(name);
  356. if (!ifindex) {
  357. interface_stop(iface);
  358. iface->ifname[0] = 0;
  359. } else {
  360. snprintf(iface->ifname, sizeof(iface->ifname), "%s", name);
  361. interface_start(iface);
  362. }
  363. }
  364. static void
  365. qosify_iface_check_interface(struct qosify_iface *iface)
  366. {
  367. const char *name = qosify_iface_name(iface);
  368. char ifname[IFNAMSIZ];
  369. if (qosify_ubus_check_interface(name, ifname, sizeof(ifname)) == 0) {
  370. snprintf(iface->ifname, sizeof(iface->ifname), "%s", ifname);
  371. interface_start(iface);
  372. } else {
  373. interface_stop(iface);
  374. iface->ifname[0] = 0;
  375. }
  376. }
  377. static void qos_iface_check_cb(struct uloop_timeout *t)
  378. {
  379. struct qosify_iface *iface;
  380. vlist_for_each_element(&devices, iface, node)
  381. qosify_iface_check_device(iface);
  382. vlist_for_each_element(&interfaces, iface, node)
  383. qosify_iface_check_interface(iface);
  384. }
  385. void qosify_iface_check(void)
  386. {
  387. static struct uloop_timeout timer = {
  388. .cb = qos_iface_check_cb,
  389. };
  390. uloop_timeout_set(&timer, 10);
  391. }
  392. static void
  393. __qosify_iface_status(struct blob_buf *b, struct qosify_iface *iface)
  394. {
  395. void *c;
  396. c = blobmsg_open_table(b, qosify_iface_name(iface));
  397. blobmsg_add_u8(b, "active", iface->active);
  398. if (iface->ifname)
  399. blobmsg_add_string(b, "ifname", iface->ifname);
  400. blobmsg_add_u8(b, "egress", iface->config.egress);
  401. blobmsg_add_u8(b, "ingress", iface->config.ingress);
  402. blobmsg_close_table(b, c);
  403. }
  404. void qosify_iface_status(struct blob_buf *b)
  405. {
  406. struct qosify_iface *iface;
  407. void *c;
  408. c = blobmsg_open_table(b, "devices");
  409. vlist_for_each_element(&devices, iface, node)
  410. __qosify_iface_status(b, iface);
  411. blobmsg_close_table(b, c);
  412. c = blobmsg_open_table(b, "interfaces");
  413. vlist_for_each_element(&interfaces, iface, node)
  414. __qosify_iface_status(b, iface);
  415. blobmsg_close_table(b, c);
  416. }
  417. int qosify_iface_init(void)
  418. {
  419. socket_fd = socket(AF_UNIX, SOCK_DGRAM, 0);
  420. if (socket < 0)
  421. return -1;
  422. return 0;
  423. }
  424. void qosify_iface_stop(void)
  425. {
  426. struct qosify_iface *iface;
  427. vlist_for_each_element(&interfaces, iface, node)
  428. interface_stop(iface);
  429. vlist_for_each_element(&devices, iface, node)
  430. interface_stop(iface);
  431. }