iprule.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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_MAX
  44. };
  45. static const struct blobmsg_policy rule_attr[__RULE_MAX] = {
  46. [RULE_INTERFACE_IN] = { .name = "in", .type = BLOBMSG_TYPE_STRING },
  47. [RULE_INTERFACE_OUT] = { .name = "out", .type = BLOBMSG_TYPE_STRING },
  48. [RULE_INVERT] = { .name = "invert", .type = BLOBMSG_TYPE_BOOL },
  49. [RULE_SRC] = { .name = "src", .type = BLOBMSG_TYPE_STRING },
  50. [RULE_DEST] = { .name = "dest", .type = BLOBMSG_TYPE_STRING },
  51. [RULE_PRIORITY] = { .name = "priority", .type = BLOBMSG_TYPE_INT32 },
  52. [RULE_TOS] = { .name = "tos", .type = BLOBMSG_TYPE_INT32 },
  53. [RULE_FWMARK] = { .name = "mark", .type = BLOBMSG_TYPE_STRING },
  54. [RULE_LOOKUP] = { .name = "lookup", .type = BLOBMSG_TYPE_STRING },
  55. [RULE_SUP_PREFIXLEN] = { .name = "suppress_prefixlength", .type = BLOBMSG_TYPE_INT32 },
  56. [RULE_ACTION] = { .name = "action", .type = BLOBMSG_TYPE_STRING },
  57. [RULE_GOTO] = { .name = "goto", .type = BLOBMSG_TYPE_INT32 },
  58. };
  59. const struct uci_blob_param_list rule_attr_list = {
  60. .n_params = __RULE_MAX,
  61. .params = rule_attr,
  62. };
  63. /* interface based rules are dynamic. */
  64. static bool
  65. rule_ready(struct iprule *rule)
  66. {
  67. if (rule->flags & IPRULE_OUT && !rule->out_dev[0])
  68. return false;
  69. if (rule->flags & IPRULE_IN && !rule->in_dev[0])
  70. return false;
  71. return true;
  72. }
  73. static bool
  74. iprule_parse_mark(const char *mark, struct iprule *rule)
  75. {
  76. char *s, *e;
  77. unsigned int n;
  78. if ((s = strchr(mark, '/')) != NULL)
  79. *s++ = 0;
  80. n = strtoul(mark, &e, 0);
  81. if (e == mark || *e)
  82. return false;
  83. rule->fwmark = n;
  84. rule->flags |= IPRULE_FWMARK;
  85. if (s) {
  86. n = strtoul(s, &e, 0);
  87. if (e == s || *e)
  88. return false;
  89. rule->fwmask = n;
  90. rule->flags |= IPRULE_FWMASK;
  91. }
  92. return true;
  93. }
  94. /* called on interface changes of the incoming interface */
  95. static void
  96. rule_in_cb(struct interface_user *dep, struct interface *iface, enum interface_event ev)
  97. {
  98. struct iprule *rule = container_of(dep, struct iprule, in_iface_user);
  99. switch (ev) {
  100. case IFEV_UP:
  101. if (!iface->l3_dev.dev)
  102. break;
  103. strcpy(rule->in_dev, iface->l3_dev.dev->ifname);
  104. if (rule_ready(rule))
  105. system_add_iprule(rule);
  106. break;
  107. case IFEV_DOWN:
  108. case IFEV_UP_FAILED:
  109. case IFEV_FREE:
  110. if (rule_ready(rule))
  111. system_del_iprule(rule);
  112. rule->in_dev[0] = 0;
  113. break;
  114. default:
  115. break;
  116. }
  117. }
  118. /* called on interface changes of the outgoing interface */
  119. static void
  120. rule_out_cb(struct interface_user *dep, struct interface *iface, enum interface_event ev)
  121. {
  122. struct iprule *rule = container_of(dep, struct iprule, out_iface_user);
  123. switch (ev) {
  124. case IFEV_UP:
  125. if (!iface->l3_dev.dev)
  126. break;
  127. strcpy(rule->out_dev, iface->l3_dev.dev->ifname);
  128. if (rule_ready(rule))
  129. system_add_iprule(rule);
  130. break;
  131. case IFEV_DOWN:
  132. case IFEV_UP_FAILED:
  133. case IFEV_FREE:
  134. if (rule_ready(rule))
  135. system_del_iprule(rule);
  136. rule->out_dev[0] = 0;
  137. break;
  138. default:
  139. break;
  140. }
  141. }
  142. /* called on all interface events */
  143. static void
  144. generic_interface_cb(struct interface_user *dep,
  145. struct interface *iface, enum interface_event ev)
  146. {
  147. struct iprule *rule;
  148. if (ev != IFEV_CREATE)
  149. return;
  150. /* add new interfaces to rules */
  151. vlist_for_each_element(&iprules, rule, node) {
  152. if (rule_ready(rule))
  153. continue;
  154. if ((rule->flags & IPRULE_OUT) && !strcmp(rule->out_iface, iface->name))
  155. interface_add_user(&rule->out_iface_user, iface);
  156. if ((rule->flags & IPRULE_IN) && !strcmp(rule->in_iface, iface->name))
  157. interface_add_user(&rule->in_iface_user, iface);
  158. }
  159. }
  160. struct interface_user generic_listener = {
  161. .cb = generic_interface_cb
  162. };
  163. void
  164. iprule_add(struct blob_attr *attr, bool v6)
  165. {
  166. struct blob_attr *tb[__RULE_MAX], *cur;
  167. struct iprule *rule;
  168. char *iface_name;
  169. int af = v6 ? AF_INET6 : AF_INET;
  170. blobmsg_parse(rule_attr, __RULE_MAX, tb, blobmsg_data(attr), blobmsg_data_len(attr));
  171. rule = calloc(1, sizeof(*rule));
  172. if (!rule)
  173. return;
  174. rule->flags = v6 ? IPRULE_INET6 : IPRULE_INET4;
  175. rule->order = iprules_counter[rule->flags]++;
  176. if ((cur = tb[RULE_INVERT]) != NULL)
  177. rule->invert = blobmsg_get_bool(cur);
  178. if ((cur = tb[RULE_INTERFACE_IN]) != NULL) {
  179. iface_name = calloc(1, strlen(blobmsg_data(cur)) + 1);
  180. rule->in_iface = strcpy(iface_name, blobmsg_data(cur));
  181. rule->in_iface_user.cb = &rule_in_cb;
  182. rule->flags |= IPRULE_IN;
  183. }
  184. if ((cur = tb[RULE_INTERFACE_OUT]) != NULL) {
  185. iface_name = calloc(1, strlen(blobmsg_data(cur)) + 1);
  186. rule->out_iface = strcpy(iface_name, blobmsg_data(cur));
  187. rule->out_iface_user.cb = &rule_out_cb;
  188. rule->flags |= IPRULE_OUT;
  189. }
  190. if ((cur = tb[RULE_SRC]) != NULL) {
  191. if (!parse_ip_and_netmask(af, blobmsg_data(cur), &rule->src_addr, &rule->src_mask)) {
  192. DPRINTF("Failed to parse rule source: %s\n", (char *) blobmsg_data(cur));
  193. goto error;
  194. }
  195. rule->flags |= IPRULE_SRC;
  196. }
  197. if ((cur = tb[RULE_DEST]) != NULL) {
  198. if (!parse_ip_and_netmask(af, blobmsg_data(cur), &rule->dest_addr, &rule->dest_mask)) {
  199. DPRINTF("Failed to parse rule destination: %s\n", (char *) blobmsg_data(cur));
  200. goto error;
  201. }
  202. rule->flags |= IPRULE_DEST;
  203. }
  204. if ((cur = tb[RULE_PRIORITY]) != NULL) {
  205. rule->priority = blobmsg_get_u32(cur);
  206. rule->flags |= IPRULE_PRIORITY;
  207. }
  208. if ((cur = tb[RULE_TOS]) != NULL) {
  209. if ((rule->tos = blobmsg_get_u32(cur)) > 255) {
  210. DPRINTF("Invalid TOS value: %u\n", blobmsg_get_u32(cur));
  211. goto error;
  212. }
  213. rule->flags |= IPRULE_TOS;
  214. }
  215. if ((cur = tb[RULE_FWMARK]) != NULL) {
  216. if (!iprule_parse_mark(blobmsg_data(cur), rule)) {
  217. DPRINTF("Failed to parse rule fwmark: %s\n", (char *) blobmsg_data(cur));
  218. goto error;
  219. }
  220. /* flags set by iprule_parse_mark() */
  221. }
  222. if ((cur = tb[RULE_LOOKUP]) != NULL) {
  223. if (!system_resolve_rt_table(blobmsg_data(cur), &rule->lookup)) {
  224. DPRINTF("Failed to parse rule lookup table: %s\n", (char *) blobmsg_data(cur));
  225. goto error;
  226. }
  227. rule->flags |= IPRULE_LOOKUP;
  228. }
  229. if ((cur = tb[RULE_SUP_PREFIXLEN]) != NULL) {
  230. rule->sup_prefixlen = blobmsg_get_u32(cur);
  231. rule->flags |= IPRULE_SUP_PREFIXLEN;
  232. }
  233. if ((cur = tb[RULE_ACTION]) != NULL) {
  234. if (!system_resolve_iprule_action(blobmsg_data(cur), &rule->action)) {
  235. DPRINTF("Failed to parse rule action: %s\n", (char *) blobmsg_data(cur));
  236. goto error;
  237. }
  238. rule->flags |= IPRULE_ACTION;
  239. }
  240. if ((cur = tb[RULE_GOTO]) != NULL) {
  241. rule->gotoid = blobmsg_get_u32(cur);
  242. rule->flags |= IPRULE_GOTO;
  243. }
  244. vlist_add(&iprules, &rule->node, rule);
  245. return;
  246. error:
  247. free(rule);
  248. }
  249. void
  250. iprule_update_start(void)
  251. {
  252. if (!iprules_flushed) {
  253. system_flush_iprules();
  254. iprules_flushed = true;
  255. }
  256. iprules_counter[0] = 1;
  257. iprules_counter[1] = 1;
  258. vlist_update(&iprules);
  259. }
  260. void
  261. iprule_update_complete(void)
  262. {
  263. vlist_flush(&iprules);
  264. }
  265. static int
  266. rule_cmp(const void *k1, const void *k2, void *ptr)
  267. {
  268. const struct iprule *r1 = k1, *r2 = k2;
  269. int ret;
  270. /* First compare the interface names */
  271. if (r1->flags & IPRULE_IN || r2->flags & IPRULE_IN) {
  272. char *str1 = r1->flags & IPRULE_IN ? r1->in_iface : "";
  273. char *str2 = r2->flags & IPRULE_IN ? r2->in_iface : "";
  274. ret = strcmp(str1, str2);
  275. if (ret)
  276. return ret;
  277. }
  278. if (r1->flags & IPRULE_OUT || r2->flags & IPRULE_OUT) {
  279. char *str1 = r1->flags & IPRULE_OUT ? r1->out_iface : "";
  280. char *str2 = r2->flags & IPRULE_OUT ? r2->out_iface : "";
  281. ret = strcmp(str1, str2);
  282. if (ret)
  283. return ret;
  284. }
  285. /* Next compare everything after the flags field */
  286. return memcmp(k1 + offsetof(struct iprule, flags),
  287. k2 + offsetof(struct iprule, flags),
  288. sizeof(struct iprule) - offsetof(struct iprule, flags));
  289. }
  290. static void deregister_interfaces(struct iprule *rule)
  291. {
  292. if (rule->flags & IPRULE_IN && rule->in_iface_user.iface)
  293. interface_remove_user(&rule->in_iface_user);
  294. if (rule->flags & IPRULE_OUT && rule->out_iface_user.iface)
  295. interface_remove_user(&rule->out_iface_user);
  296. }
  297. static void register_interfaces(struct iprule *rule)
  298. {
  299. struct interface *iface, *tmp;
  300. if (rule->flags & IPRULE_IN) {
  301. tmp = vlist_find(&interfaces, rule->in_iface, iface, node);
  302. if (tmp)
  303. interface_add_user(&rule->in_iface_user, tmp);
  304. }
  305. if (rule->flags & IPRULE_OUT) {
  306. tmp = vlist_find(&interfaces, rule->out_iface, iface, node);
  307. if (tmp)
  308. interface_add_user(&rule->out_iface_user, tmp);
  309. }
  310. }
  311. static void
  312. iprule_update_rule(struct vlist_tree *tree,
  313. struct vlist_node *node_new, struct vlist_node *node_old)
  314. {
  315. struct iprule *rule_old, *rule_new;
  316. rule_old = container_of(node_old, struct iprule, node);
  317. rule_new = container_of(node_new, struct iprule, node);
  318. if (node_old) {
  319. if (rule_ready(rule_old))
  320. system_del_iprule(rule_old);
  321. if (rule_old->flags & (IPRULE_IN | IPRULE_OUT))
  322. deregister_interfaces(rule_old);
  323. if (rule_old->in_iface)
  324. free(rule_old->in_iface);
  325. if (rule_old->out_iface)
  326. free(rule_old->out_iface);
  327. free(rule_old);
  328. }
  329. if (node_new) {
  330. /* interface based rules calls system_add_iprule over the event cb */
  331. if (rule_new->flags & (IPRULE_IN | IPRULE_OUT)) {
  332. register_interfaces(rule_new);
  333. } else {
  334. system_add_iprule(rule_new);
  335. }
  336. }
  337. }
  338. static void __init
  339. iprule_init_list(void)
  340. {
  341. vlist_init(&iprules, rule_cmp, iprule_update_rule);
  342. interface_add_user(&generic_listener, NULL);
  343. }