rule.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. Copyright (c) 2015 by Matthieu Boutier and Juliusz Chroboczek.
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <errno.h>
  23. #include <sys/time.h>
  24. #include "babeld.h"
  25. #include "util.h"
  26. #include "kernel.h"
  27. #include "configuration.h"
  28. #include "rule.h"
  29. int src_table_idx = 10;
  30. int src_table_prio = 100;
  31. /* The table used for non-specific routes is "export_table", therefore, we can
  32. take the convention of plen == 0 <=> empty table. */
  33. struct rule {
  34. unsigned char src[16];
  35. unsigned char plen;
  36. unsigned char table;
  37. };
  38. /* rules contains informations about the rules we installed. It is an array
  39. indexed by: <table priority> - src_table_prio.
  40. (First entries are the most specific, since they have priority.) */
  41. static struct rule rules[SRC_TABLE_NUM];
  42. /* used tables is indexed by: <table number> - src_table_idx
  43. used_tables[i] == 1 <=> the table number (i + src_table_idx) is used */
  44. static char used_tables[SRC_TABLE_NUM] = {0};
  45. static int
  46. get_free_table(void)
  47. {
  48. int i;
  49. for(i = 0; i < SRC_TABLE_NUM; i++)
  50. if(!used_tables[i]) {
  51. used_tables[i] = 1;
  52. return i + src_table_idx;
  53. }
  54. return -1;
  55. }
  56. static void
  57. release_table(int i)
  58. {
  59. used_tables[i - src_table_idx] = 0;
  60. }
  61. static int
  62. find_hole_around(int i)
  63. {
  64. int j;
  65. for(j = i; j < SRC_TABLE_NUM; j++)
  66. if(rules[j].plen == 0)
  67. return j;
  68. for(j = i - 1; j >= 0; j--)
  69. if(rules[j].plen == 0)
  70. return j;
  71. return -1;
  72. }
  73. static int
  74. shift_rule(int from, int to)
  75. {
  76. int dec = (from < to) ? 1 /* right */ : -1 /* left */;
  77. int rc;
  78. while(to != from) {
  79. to -= dec;
  80. rc = change_rule(to + dec + src_table_prio, to + src_table_prio,
  81. rules[to].src, rules[to].plen, rules[to].table);
  82. if(rc < 0) {
  83. perror("change_table_priority");
  84. return -1;
  85. }
  86. rules[to+dec] = rules[to];
  87. rules[to].plen = 0;
  88. }
  89. return 0;
  90. }
  91. /* Return a new table at index [idx] of rules. If cell at that index is not
  92. free, we need to shift cells (and rules). If it's full, return NULL. */
  93. static struct rule *
  94. insert_table(const unsigned char *src, unsigned short src_plen, int idx)
  95. {
  96. int table;
  97. int rc;
  98. int hole;
  99. if(idx < 0 || idx >= SRC_TABLE_NUM) {
  100. fprintf(stderr, "Incorrect table number %d\n", idx);
  101. return NULL;
  102. }
  103. table = get_free_table();
  104. if(table < 0) {
  105. kdebugf("All allowed routing tables are used!\n");
  106. return NULL;
  107. }
  108. hole = find_hole_around(idx);
  109. if(hole < 0) {
  110. fprintf(stderr, "Have free table but not free rule.\n");
  111. goto fail;
  112. }
  113. rc = shift_rule(idx, hole);
  114. if(rc < 0)
  115. goto fail;
  116. rc = add_rule(idx + src_table_prio, src, src_plen, table);
  117. if(rc < 0) {
  118. perror("add rule");
  119. goto fail;
  120. }
  121. memcpy(rules[idx].src, src, 16);
  122. rules[idx].plen = src_plen;
  123. rules[idx].table = table;
  124. return &rules[idx];
  125. fail:
  126. release_table(table);
  127. return NULL;
  128. }
  129. /* Sorting rules in a well ordered fashion will increase code complexity and
  130. decrease performances, because more rule shifts will be required, so more
  131. system calls invoked. */
  132. static int
  133. find_table_slot(const unsigned char *src, unsigned short src_plen, int *found)
  134. {
  135. struct rule *kr = NULL;
  136. int i;
  137. *found = 0;
  138. for(i = 0; i < SRC_TABLE_NUM; i++) {
  139. kr = &rules[i];
  140. if(kr->plen == 0)
  141. return i;
  142. switch(prefix_cmp(src, src_plen, kr->src, kr->plen)) {
  143. case PST_LESS_SPECIFIC:
  144. case PST_DISJOINT:
  145. continue;
  146. case PST_MORE_SPECIFIC:
  147. return i;
  148. case PST_EQUALS:
  149. *found = 1;
  150. return i;
  151. }
  152. }
  153. return -1;
  154. }
  155. int
  156. find_table(const unsigned char *dest, unsigned short plen,
  157. const unsigned char *src, unsigned short src_plen)
  158. {
  159. struct filter_result filter_result = {0};
  160. struct rule *kr = NULL;
  161. int i, found;
  162. install_filter(dest, plen, src, src_plen, &filter_result);
  163. if(filter_result.table) {
  164. return filter_result.table;
  165. } else if(src_plen == 0) {
  166. return export_table;
  167. } else if(kernel_disambiguate(v4mapped(dest))) {
  168. return export_table;
  169. }
  170. i = find_table_slot(src, src_plen, &found);
  171. if(found)
  172. return rules[i].table;
  173. if(i < 0)
  174. return -1;
  175. kr = insert_table(src, src_plen, i);
  176. return kr == NULL ? -1 : kr->table;
  177. }
  178. void
  179. release_tables(void)
  180. {
  181. int i;
  182. for(i = 0; i < SRC_TABLE_NUM; i++) {
  183. if(rules[i].plen != 0) {
  184. flush_rule(i + src_table_prio,
  185. v4mapped(rules[i].src) ? AF_INET : AF_INET6);
  186. rules[i].plen = 0;
  187. }
  188. used_tables[i] = 0;
  189. }
  190. }
  191. static int
  192. filter_rule(struct kernel_rule *rule, void *data)
  193. {
  194. int i;
  195. char (*rule_exists)[2][SRC_TABLE_NUM] = data;
  196. int is_v4 = v4mapped(rule->src);
  197. int r = is_v4 ? 0 : 1;
  198. if(martian_prefix(rule->src, rule->src_plen))
  199. return 0;
  200. i = rule->priority - src_table_prio;
  201. if(i < 0 || SRC_TABLE_NUM <= i)
  202. return 0;
  203. if(prefix_cmp(rule->src, rule->src_plen,
  204. rules[i].src, rules[i].plen) == PST_EQUALS &&
  205. rule->table == rules[i].table &&
  206. (*rule_exists)[r][i] == 0)
  207. (*rule_exists)[r][i] = 1;
  208. else
  209. (*rule_exists)[r][i] = -1;
  210. return 1;
  211. }
  212. /* This functions should be executed wrt the code just bellow: [rule_exists]
  213. contains is a boolean array telling whether the rules we should have
  214. installed in the kernel are installed or not. If they aren't, then reinstall
  215. them (this can append when rules are modified by third parties). */
  216. static void
  217. install_missing_rules(char rule_exists[SRC_TABLE_NUM], int v4)
  218. {
  219. int i, rc;
  220. for(i = 0; i < SRC_TABLE_NUM; i++) {
  221. int priority = i + src_table_prio;
  222. if(rule_exists[i] == 1)
  223. continue;
  224. if(rule_exists[i] != 0) {
  225. int rc;
  226. do {
  227. rc = flush_rule(priority, v4 ? AF_INET : AF_INET6);
  228. } while(rc >= 0);
  229. if(errno != ENOENT && errno != EEXIST)
  230. fprintf(stderr,
  231. "Cannot remove rule %d: from %s table %d (%s)\n",
  232. priority,
  233. format_prefix(rules[i].src, rules[i].plen),
  234. rules[i].table, strerror(errno));
  235. }
  236. /* Be wise, our priority are both for v4 and v6 (does not overlap). */
  237. if(!!v4mapped(rules[i].src) == !!v4 && rules[i].plen != 0) {
  238. rc = add_rule(priority, rules[i].src,
  239. rules[i].plen, rules[i].table);
  240. if(rc < 0)
  241. fprintf(stderr,
  242. "Cannot install rule %d: from %s table %d (%s)\n",
  243. priority,
  244. format_prefix(rules[i].src, rules[i].plen),
  245. rules[i].table, strerror(errno));
  246. }
  247. }
  248. }
  249. int
  250. check_rules(void)
  251. {
  252. int rc;
  253. char rule_exists[2][SRC_TABLE_NUM]; /* v4, v6 */
  254. struct kernel_filter filter = {0};
  255. filter.rule = filter_rule;
  256. filter.rule_closure = (void*) rule_exists;
  257. memset(rule_exists, 0, sizeof(rule_exists));
  258. rc = kernel_dump(CHANGE_RULE, &filter);
  259. if(rc < 0)
  260. return -1;
  261. install_missing_rules(rule_exists[0], 1);
  262. install_missing_rules(rule_exists[1], 0);
  263. return 0;
  264. }