iprule.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /*
  2. * netifd - network interface daemon
  3. * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
  4. * Copyright (C) 2013 Jo-Philipp Wich <jow@openwrt.org>
  5. * Copyright (C) 2018 Alexander Couzens <lynxis@fe80.eu>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2
  9. * as published by the Free Software Foundation
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <string.h>
  17. #include <stdlib.h>
  18. #include <stdio.h>
  19. #include <arpa/inet.h>
  20. #include "netifd.h"
  21. #include "device.h"
  22. #include "interface.h"
  23. #include "iprule.h"
  24. #include "proto.h"
  25. #include "ubus.h"
  26. #include "system.h"
  27. struct vlist_tree iprules;
  28. static bool iprules_flushed = false;
  29. static unsigned int iprules_counter[2];
  30. enum {
  31. RULE_INTERFACE_IN,
  32. RULE_INTERFACE_OUT,
  33. RULE_INVERT,
  34. RULE_SRC,
  35. RULE_DEST,
  36. RULE_PRIORITY,
  37. RULE_TOS,
  38. RULE_FWMARK,
  39. RULE_LOOKUP,
  40. RULE_ACTION,
  41. RULE_GOTO,
  42. RULE_SUP_PREFIXLEN,
  43. RULE_UIDRANGE,
  44. RULE_DISABLED,
  45. __RULE_MAX
  46. };
  47. static const struct blobmsg_policy rule_attr[__RULE_MAX] = {
  48. [RULE_INTERFACE_IN] = { .name = "in", .type = BLOBMSG_TYPE_STRING },
  49. [RULE_INTERFACE_OUT] = { .name = "out", .type = BLOBMSG_TYPE_STRING },
  50. [RULE_INVERT] = { .name = "invert", .type = BLOBMSG_TYPE_BOOL },
  51. [RULE_SRC] = { .name = "src", .type = BLOBMSG_TYPE_STRING },
  52. [RULE_DEST] = { .name = "dest", .type = BLOBMSG_TYPE_STRING },
  53. [RULE_PRIORITY] = { .name = "priority", .type = BLOBMSG_TYPE_INT32 },
  54. [RULE_TOS] = { .name = "tos", .type = BLOBMSG_TYPE_INT32 },
  55. [RULE_FWMARK] = { .name = "mark", .type = BLOBMSG_TYPE_STRING },
  56. [RULE_LOOKUP] = { .name = "lookup", .type = BLOBMSG_TYPE_STRING },
  57. [RULE_SUP_PREFIXLEN] = { .name = "suppress_prefixlength", .type = BLOBMSG_TYPE_INT32 },
  58. [RULE_UIDRANGE] = { .name = "uidrange", .type = BLOBMSG_TYPE_STRING },
  59. [RULE_ACTION] = { .name = "action", .type = BLOBMSG_TYPE_STRING },
  60. [RULE_GOTO] = { .name = "goto", .type = BLOBMSG_TYPE_INT32 },
  61. [RULE_DISABLED] = { .name = "disabled", .type = BLOBMSG_TYPE_BOOL },
  62. };
  63. const struct uci_blob_param_list rule_attr_list = {
  64. .n_params = __RULE_MAX,
  65. .params = rule_attr,
  66. };
  67. /* interface based rules are dynamic. */
  68. static bool
  69. rule_ready(struct iprule *rule)
  70. {
  71. if (rule->flags & IPRULE_OUT && !rule->out_dev[0])
  72. return false;
  73. if (rule->flags & IPRULE_IN && !rule->in_dev[0])
  74. return false;
  75. return true;
  76. }
  77. static bool
  78. iprule_parse_mark(const char *mark, struct iprule *rule)
  79. {
  80. char *s, *e;
  81. unsigned int n;
  82. if ((s = strchr(mark, '/')) != NULL)
  83. *s++ = 0;
  84. n = strtoul(mark, &e, 0);
  85. if (e == mark || *e)
  86. return false;
  87. rule->fwmark = n;
  88. rule->flags |= IPRULE_FWMARK;
  89. if (s) {
  90. n = strtoul(s, &e, 0);
  91. if (e == s || *e)
  92. return false;
  93. rule->fwmask = n;
  94. rule->flags |= IPRULE_FWMASK;
  95. }
  96. return true;
  97. }
  98. /* called on interface changes of the incoming interface */
  99. static void
  100. rule_in_cb(struct interface_user *dep, struct interface *iface, enum interface_event ev)
  101. {
  102. struct iprule *rule = container_of(dep, struct iprule, in_iface_user);
  103. switch (ev) {
  104. case IFEV_UP:
  105. if (!iface->l3_dev.dev)
  106. break;
  107. strcpy(rule->in_dev, iface->l3_dev.dev->ifname);
  108. if (rule_ready(rule))
  109. system_add_iprule(rule);
  110. break;
  111. case IFEV_DOWN:
  112. case IFEV_UP_FAILED:
  113. case IFEV_FREE:
  114. if (rule_ready(rule))
  115. system_del_iprule(rule);
  116. rule->in_dev[0] = 0;
  117. break;
  118. default:
  119. break;
  120. }
  121. }
  122. /* called on interface changes of the outgoing interface */
  123. static void
  124. rule_out_cb(struct interface_user *dep, struct interface *iface, enum interface_event ev)
  125. {
  126. struct iprule *rule = container_of(dep, struct iprule, out_iface_user);
  127. switch (ev) {
  128. case IFEV_UP:
  129. if (!iface->l3_dev.dev)
  130. break;
  131. strcpy(rule->out_dev, iface->l3_dev.dev->ifname);
  132. if (rule_ready(rule))
  133. system_add_iprule(rule);
  134. break;
  135. case IFEV_DOWN:
  136. case IFEV_UP_FAILED:
  137. case IFEV_FREE:
  138. if (rule_ready(rule))
  139. system_del_iprule(rule);
  140. rule->out_dev[0] = 0;
  141. break;
  142. default:
  143. break;
  144. }
  145. }
  146. /* called on all interface events */
  147. static void
  148. generic_interface_cb(struct interface_user *dep,
  149. struct interface *iface, enum interface_event ev)
  150. {
  151. struct iprule *rule;
  152. if (ev != IFEV_CREATE)
  153. return;
  154. /* add new interfaces to rules */
  155. vlist_for_each_element(&iprules, rule, node) {
  156. if (rule_ready(rule))
  157. continue;
  158. if ((rule->flags & IPRULE_OUT) && !strcmp(rule->out_iface, iface->name))
  159. interface_add_user(&rule->out_iface_user, iface);
  160. if ((rule->flags & IPRULE_IN) && !strcmp(rule->in_iface, iface->name))
  161. interface_add_user(&rule->in_iface_user, iface);
  162. }
  163. }
  164. struct interface_user generic_listener = {
  165. .cb = generic_interface_cb
  166. };
  167. void
  168. iprule_add(struct blob_attr *attr, bool v6)
  169. {
  170. struct blob_attr *tb[__RULE_MAX], *cur;
  171. struct iprule *rule;
  172. char *iface_name;
  173. int af = v6 ? AF_INET6 : AF_INET;
  174. blobmsg_parse(rule_attr, __RULE_MAX, tb, blobmsg_data(attr), blobmsg_data_len(attr));
  175. if ((cur = tb[RULE_DISABLED]) != NULL && blobmsg_get_bool(cur))
  176. return;
  177. rule = calloc(1, sizeof(*rule));
  178. if (!rule)
  179. return;
  180. rule->flags = v6 ? IPRULE_INET6 : IPRULE_INET4;
  181. rule->order = iprules_counter[rule->flags]++;
  182. if ((cur = tb[RULE_INVERT]) != NULL)
  183. rule->invert = blobmsg_get_bool(cur);
  184. if ((cur = tb[RULE_INTERFACE_IN]) != NULL) {
  185. iface_name = calloc(1, strlen(blobmsg_data(cur)) + 1);
  186. rule->in_iface = strcpy(iface_name, blobmsg_data(cur));
  187. rule->in_iface_user.cb = &rule_in_cb;
  188. rule->flags |= IPRULE_IN;
  189. }
  190. if ((cur = tb[RULE_INTERFACE_OUT]) != NULL) {
  191. iface_name = calloc(1, strlen(blobmsg_data(cur)) + 1);
  192. rule->out_iface = strcpy(iface_name, blobmsg_data(cur));
  193. rule->out_iface_user.cb = &rule_out_cb;
  194. rule->flags |= IPRULE_OUT;
  195. }
  196. if ((cur = tb[RULE_SRC]) != NULL) {
  197. if (!parse_ip_and_netmask(af, blobmsg_data(cur), &rule->src_addr, &rule->src_mask)) {
  198. D(INTERFACE, "Failed to parse rule source: %s", (char *) blobmsg_data(cur));
  199. goto error;
  200. }
  201. rule->flags |= IPRULE_SRC;
  202. }
  203. if ((cur = tb[RULE_DEST]) != NULL) {
  204. if (!parse_ip_and_netmask(af, blobmsg_data(cur), &rule->dest_addr, &rule->dest_mask)) {
  205. D(INTERFACE, "Failed to parse rule destination: %s", (char *) blobmsg_data(cur));
  206. goto error;
  207. }
  208. rule->flags |= IPRULE_DEST;
  209. }
  210. if ((cur = tb[RULE_PRIORITY]) != NULL) {
  211. rule->priority = blobmsg_get_u32(cur);
  212. rule->flags |= IPRULE_PRIORITY;
  213. }
  214. if ((cur = tb[RULE_TOS]) != NULL) {
  215. if ((rule->tos = blobmsg_get_u32(cur)) > 255) {
  216. D(INTERFACE, "Invalid TOS value: %u", blobmsg_get_u32(cur));
  217. goto error;
  218. }
  219. rule->flags |= IPRULE_TOS;
  220. }
  221. if ((cur = tb[RULE_FWMARK]) != NULL) {
  222. if (!iprule_parse_mark(blobmsg_data(cur), rule)) {
  223. D(INTERFACE, "Failed to parse rule fwmark: %s", (char *) blobmsg_data(cur));
  224. goto error;
  225. }
  226. /* flags set by iprule_parse_mark() */
  227. }
  228. if ((cur = tb[RULE_LOOKUP]) != NULL) {
  229. if (!system_resolve_rt_table(blobmsg_data(cur), &rule->lookup)) {
  230. D(INTERFACE, "Failed to parse rule lookup table: %s", (char *) blobmsg_data(cur));
  231. goto error;
  232. }
  233. rule->flags |= IPRULE_LOOKUP;
  234. }
  235. if ((cur = tb[RULE_SUP_PREFIXLEN]) != NULL) {
  236. rule->sup_prefixlen = blobmsg_get_u32(cur);
  237. rule->flags |= IPRULE_SUP_PREFIXLEN;
  238. }
  239. if ((cur = tb[RULE_UIDRANGE]) != NULL) {
  240. int ret = sscanf(blobmsg_get_string(cur), "%u-%u", &rule->uidrange_start, &rule->uidrange_end);
  241. if (ret == 1)
  242. rule->uidrange_end = rule->uidrange_start;
  243. else if (ret != 2) {
  244. D(INTERFACE, "Failed to parse UID range: %s", (char *) blobmsg_data(cur));
  245. goto error;
  246. }
  247. rule->flags |= IPRULE_UIDRANGE;
  248. }
  249. if ((cur = tb[RULE_ACTION]) != NULL) {
  250. if (!system_resolve_iprule_action(blobmsg_data(cur), &rule->action)) {
  251. D(INTERFACE, "Failed to parse rule action: %s", (char *) blobmsg_data(cur));
  252. goto error;
  253. }
  254. rule->flags |= IPRULE_ACTION;
  255. }
  256. if ((cur = tb[RULE_GOTO]) != NULL) {
  257. rule->gotoid = blobmsg_get_u32(cur);
  258. rule->flags |= IPRULE_GOTO;
  259. }
  260. vlist_add(&iprules, &rule->node, rule);
  261. return;
  262. error:
  263. free(rule);
  264. }
  265. void
  266. iprule_update_start(void)
  267. {
  268. if (!iprules_flushed) {
  269. system_flush_iprules();
  270. iprules_flushed = true;
  271. }
  272. iprules_counter[0] = 1;
  273. iprules_counter[1] = 1;
  274. vlist_update(&iprules);
  275. }
  276. void
  277. iprule_update_complete(void)
  278. {
  279. vlist_flush(&iprules);
  280. }
  281. static int
  282. rule_cmp(const void *k1, const void *k2, void *ptr)
  283. {
  284. const struct iprule *r1 = k1, *r2 = k2;
  285. int ret;
  286. /* First compare the interface names */
  287. if (r1->flags & IPRULE_IN || r2->flags & IPRULE_IN) {
  288. char *str1 = r1->flags & IPRULE_IN ? r1->in_iface : "";
  289. char *str2 = r2->flags & IPRULE_IN ? r2->in_iface : "";
  290. ret = strcmp(str1, str2);
  291. if (ret)
  292. return ret;
  293. }
  294. if (r1->flags & IPRULE_OUT || r2->flags & IPRULE_OUT) {
  295. char *str1 = r1->flags & IPRULE_OUT ? r1->out_iface : "";
  296. char *str2 = r2->flags & IPRULE_OUT ? r2->out_iface : "";
  297. ret = strcmp(str1, str2);
  298. if (ret)
  299. return ret;
  300. }
  301. /* Next compare everything after the flags field */
  302. return memcmp(k1 + offsetof(struct iprule, flags),
  303. k2 + offsetof(struct iprule, flags),
  304. sizeof(struct iprule) - offsetof(struct iprule, flags));
  305. }
  306. static void deregister_interfaces(struct iprule *rule)
  307. {
  308. if (rule->flags & IPRULE_IN && rule->in_iface_user.iface)
  309. interface_remove_user(&rule->in_iface_user);
  310. if (rule->flags & IPRULE_OUT && rule->out_iface_user.iface)
  311. interface_remove_user(&rule->out_iface_user);
  312. }
  313. static void register_interfaces(struct iprule *rule)
  314. {
  315. struct interface *iface, *tmp;
  316. if (rule->flags & IPRULE_IN) {
  317. tmp = vlist_find(&interfaces, rule->in_iface, iface, node);
  318. if (tmp)
  319. interface_add_user(&rule->in_iface_user, tmp);
  320. }
  321. if (rule->flags & IPRULE_OUT) {
  322. tmp = vlist_find(&interfaces, rule->out_iface, iface, node);
  323. if (tmp)
  324. interface_add_user(&rule->out_iface_user, tmp);
  325. }
  326. }
  327. static void
  328. iprule_update_rule(struct vlist_tree *tree,
  329. struct vlist_node *node_new, struct vlist_node *node_old)
  330. {
  331. struct iprule *rule_old, *rule_new;
  332. rule_old = container_of(node_old, struct iprule, node);
  333. rule_new = container_of(node_new, struct iprule, node);
  334. if (node_old) {
  335. if (rule_ready(rule_old))
  336. system_del_iprule(rule_old);
  337. if (rule_old->flags & (IPRULE_IN | IPRULE_OUT))
  338. deregister_interfaces(rule_old);
  339. if (rule_old->in_iface)
  340. free(rule_old->in_iface);
  341. if (rule_old->out_iface)
  342. free(rule_old->out_iface);
  343. free(rule_old);
  344. }
  345. if (node_new) {
  346. /* interface based rules calls system_add_iprule over the event cb */
  347. if (rule_new->flags & (IPRULE_IN | IPRULE_OUT)) {
  348. register_interfaces(rule_new);
  349. } else {
  350. system_add_iprule(rule_new);
  351. }
  352. }
  353. }
  354. static void __init
  355. iprule_init_list(void)
  356. {
  357. vlist_init(&iprules, rule_cmp, iprule_update_rule);
  358. interface_add_user(&generic_listener, NULL);
  359. }