iprule.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2
  8. * as published by the Free Software Foundation
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <string.h>
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <unistd.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_MAX
  43. };
  44. static const struct blobmsg_policy rule_attr[__RULE_MAX] = {
  45. [RULE_INTERFACE_IN] = { .name = "in", .type = BLOBMSG_TYPE_STRING },
  46. [RULE_INTERFACE_OUT] = { .name = "out", .type = BLOBMSG_TYPE_STRING },
  47. [RULE_INVERT] = { .name = "invert", .type = BLOBMSG_TYPE_BOOL },
  48. [RULE_SRC] = { .name = "src", .type = BLOBMSG_TYPE_STRING },
  49. [RULE_DEST] = { .name = "dest", .type = BLOBMSG_TYPE_STRING },
  50. [RULE_PRIORITY] = { .name = "priority", .type = BLOBMSG_TYPE_INT32 },
  51. [RULE_TOS] = { .name = "tos", .type = BLOBMSG_TYPE_INT32 },
  52. [RULE_FWMARK] = { .name = "mark", .type = BLOBMSG_TYPE_STRING },
  53. [RULE_LOOKUP] = { .name = "lookup", .type = BLOBMSG_TYPE_STRING },
  54. [RULE_ACTION] = { .name = "action", .type = BLOBMSG_TYPE_STRING },
  55. [RULE_GOTO] = { .name = "goto", .type = BLOBMSG_TYPE_INT32 },
  56. };
  57. const struct uci_blob_param_list rule_attr_list = {
  58. .n_params = __RULE_MAX,
  59. .params = rule_attr,
  60. };
  61. static bool
  62. iprule_parse_mark(const char *mark, struct iprule *rule)
  63. {
  64. char *s, *e;
  65. unsigned int n;
  66. if ((s = strchr(mark, '/')) != NULL)
  67. *s++ = 0;
  68. n = strtoul(mark, &e, 0);
  69. if (e == mark || *e)
  70. return false;
  71. rule->fwmark = n;
  72. rule->flags |= IPRULE_FWMARK;
  73. if (s) {
  74. n = strtoul(s, &e, 0);
  75. if (e == s || *e)
  76. return false;
  77. rule->fwmask = n;
  78. rule->flags |= IPRULE_FWMASK;
  79. }
  80. return true;
  81. }
  82. void
  83. iprule_add(struct blob_attr *attr, bool v6)
  84. {
  85. struct interface *iif = NULL, *oif = NULL;
  86. struct blob_attr *tb[__RULE_MAX], *cur;
  87. struct interface *iface;
  88. struct iprule *rule;
  89. int af = v6 ? AF_INET6 : AF_INET;
  90. blobmsg_parse(rule_attr, __RULE_MAX, tb, blobmsg_data(attr), blobmsg_data_len(attr));
  91. rule = calloc(1, sizeof(*rule));
  92. if (!rule)
  93. return;
  94. rule->flags = v6 ? IPRULE_INET6 : IPRULE_INET4;
  95. rule->order = iprules_counter[rule->flags]++;
  96. if ((cur = tb[RULE_INVERT]) != NULL)
  97. rule->invert = blobmsg_get_bool(cur);
  98. if ((cur = tb[RULE_INTERFACE_IN]) != NULL) {
  99. iif = vlist_find(&interfaces, blobmsg_data(cur), iface, node);
  100. if (!iif || !iif->l3_dev.dev) {
  101. DPRINTF("Failed to resolve device of network: %s\n", (char *) blobmsg_data(cur));
  102. goto error;
  103. }
  104. memcpy(rule->in_dev, iif->l3_dev.dev->ifname, sizeof(rule->in_dev));
  105. rule->flags |= IPRULE_IN;
  106. }
  107. if ((cur = tb[RULE_INTERFACE_OUT]) != NULL) {
  108. oif = vlist_find(&interfaces, blobmsg_data(cur), iface, node);
  109. if (!oif || !oif->l3_dev.dev) {
  110. DPRINTF("Failed to resolve device of network: %s\n", (char *) blobmsg_data(cur));
  111. goto error;
  112. }
  113. memcpy(rule->out_dev, oif->l3_dev.dev->ifname, sizeof(rule->out_dev));
  114. rule->flags |= IPRULE_OUT;
  115. }
  116. if ((cur = tb[RULE_SRC]) != NULL) {
  117. if (!parse_ip_and_netmask(af, blobmsg_data(cur), &rule->src_addr, &rule->src_mask)) {
  118. DPRINTF("Failed to parse rule source: %s\n", (char *) blobmsg_data(cur));
  119. goto error;
  120. }
  121. rule->flags |= IPRULE_SRC;
  122. }
  123. if ((cur = tb[RULE_DEST]) != NULL) {
  124. if (!parse_ip_and_netmask(af, blobmsg_data(cur), &rule->dest_addr, &rule->dest_mask)) {
  125. DPRINTF("Failed to parse rule destination: %s\n", (char *) blobmsg_data(cur));
  126. goto error;
  127. }
  128. rule->flags |= IPRULE_DEST;
  129. }
  130. if ((cur = tb[RULE_PRIORITY]) != NULL) {
  131. rule->priority = blobmsg_get_u32(cur);
  132. rule->flags |= IPRULE_PRIORITY;
  133. }
  134. if ((cur = tb[RULE_TOS]) != NULL) {
  135. if ((rule->tos = blobmsg_get_u32(cur)) > 255) {
  136. DPRINTF("Invalid TOS value: %u\n", blobmsg_get_u32(cur));
  137. goto error;
  138. }
  139. rule->flags |= IPRULE_TOS;
  140. }
  141. if ((cur = tb[RULE_FWMARK]) != NULL) {
  142. if (!iprule_parse_mark(blobmsg_data(cur), rule)) {
  143. DPRINTF("Failed to parse rule fwmark: %s\n", (char *) blobmsg_data(cur));
  144. goto error;
  145. }
  146. /* flags set by iprule_parse_mark() */
  147. }
  148. if ((cur = tb[RULE_LOOKUP]) != NULL) {
  149. if (!system_resolve_rt_table(blobmsg_data(cur), &rule->lookup)) {
  150. DPRINTF("Failed to parse rule lookup table: %s\n", (char *) blobmsg_data(cur));
  151. goto error;
  152. }
  153. rule->flags |= IPRULE_LOOKUP;
  154. }
  155. if ((cur = tb[RULE_ACTION]) != NULL) {
  156. if (!system_resolve_iprule_action(blobmsg_data(cur), &rule->action)) {
  157. DPRINTF("Failed to parse rule action: %s\n", (char *) blobmsg_data(cur));
  158. goto error;
  159. }
  160. rule->flags |= IPRULE_ACTION;
  161. }
  162. if ((cur = tb[RULE_GOTO]) != NULL) {
  163. rule->gotoid = blobmsg_get_u32(cur);
  164. rule->flags |= IPRULE_GOTO;
  165. }
  166. vlist_add(&iprules, &rule->node, &rule->flags);
  167. return;
  168. error:
  169. free(rule);
  170. }
  171. void
  172. iprule_update_start(void)
  173. {
  174. if (!iprules_flushed) {
  175. system_flush_iprules();
  176. iprules_flushed = true;
  177. }
  178. iprules_counter[0] = 1;
  179. iprules_counter[1] = 1;
  180. vlist_update(&iprules);
  181. }
  182. void
  183. iprule_update_complete(void)
  184. {
  185. vlist_flush(&iprules);
  186. }
  187. static int
  188. rule_cmp(const void *k1, const void *k2, void *ptr)
  189. {
  190. return memcmp(k1, k2, sizeof(struct iprule)-offsetof(struct iprule, flags));
  191. }
  192. static void
  193. iprule_update_rule(struct vlist_tree *tree,
  194. struct vlist_node *node_new, struct vlist_node *node_old)
  195. {
  196. struct iprule *rule_old, *rule_new;
  197. rule_old = container_of(node_old, struct iprule, node);
  198. rule_new = container_of(node_new, struct iprule, node);
  199. if (node_old) {
  200. system_del_iprule(rule_old);
  201. free(rule_old);
  202. }
  203. if (node_new)
  204. system_add_iprule(rule_new);
  205. }
  206. static void __init
  207. iprule_init_list(void)
  208. {
  209. vlist_init(&iprules, rule_cmp, iprule_update_rule);
  210. }