fib_rules.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. * INET An implementation of the TCP/IP protocol suite for the LINUX
  3. * operating system. INET is implemented using the BSD Socket
  4. * interface as the means of communication with the user level.
  5. *
  6. * IPv4 Forwarding Information Base: policy rules.
  7. *
  8. * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  9. * Thomas Graf <tgraf@suug.ch>
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version
  14. * 2 of the License, or (at your option) any later version.
  15. *
  16. * Fixes:
  17. * Rani Assaf : local_rule cannot be deleted
  18. * Marc Boucher : routing by fwmark
  19. */
  20. #include <linux/types.h>
  21. #include <linux/kernel.h>
  22. #include <linux/netdevice.h>
  23. #include <linux/netlink.h>
  24. #include <linux/inetdevice.h>
  25. #include <linux/init.h>
  26. #include <linux/list.h>
  27. #include <linux/rcupdate.h>
  28. #include <net/ip.h>
  29. #include <net/route.h>
  30. #include <net/tcp.h>
  31. #include <net/ip_fib.h>
  32. #include <net/fib_rules.h>
  33. static struct fib_rules_ops fib4_rules_ops;
  34. struct fib4_rule
  35. {
  36. struct fib_rule common;
  37. u8 dst_len;
  38. u8 src_len;
  39. u8 tos;
  40. __be32 src;
  41. __be32 srcmask;
  42. __be32 dst;
  43. __be32 dstmask;
  44. #ifdef CONFIG_NET_CLS_ROUTE
  45. u32 tclassid;
  46. #endif
  47. };
  48. #ifdef CONFIG_NET_CLS_ROUTE
  49. u32 fib_rules_tclass(struct fib_result *res)
  50. {
  51. return res->r ? ((struct fib4_rule *) res->r)->tclassid : 0;
  52. }
  53. #endif
  54. int fib_lookup(struct flowi *flp, struct fib_result *res)
  55. {
  56. struct fib_lookup_arg arg = {
  57. .result = res,
  58. };
  59. int err;
  60. err = fib_rules_lookup(&fib4_rules_ops, flp, 0, &arg);
  61. res->r = arg.rule;
  62. return err;
  63. }
  64. static int fib4_rule_action(struct fib_rule *rule, struct flowi *flp,
  65. int flags, struct fib_lookup_arg *arg)
  66. {
  67. int err = -EAGAIN;
  68. struct fib_table *tbl;
  69. switch (rule->action) {
  70. case FR_ACT_TO_TBL:
  71. break;
  72. case FR_ACT_UNREACHABLE:
  73. err = -ENETUNREACH;
  74. goto errout;
  75. case FR_ACT_PROHIBIT:
  76. err = -EACCES;
  77. goto errout;
  78. case FR_ACT_BLACKHOLE:
  79. default:
  80. err = -EINVAL;
  81. goto errout;
  82. }
  83. if ((tbl = fib_get_table(rule->table)) == NULL)
  84. goto errout;
  85. err = tbl->tb_lookup(tbl, flp, (struct fib_result *) arg->result);
  86. if (err > 0)
  87. err = -EAGAIN;
  88. errout:
  89. return err;
  90. }
  91. void fib_select_default(const struct flowi *flp, struct fib_result *res)
  92. {
  93. if (res->r && res->r->action == FR_ACT_TO_TBL &&
  94. FIB_RES_GW(*res) && FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK) {
  95. struct fib_table *tb;
  96. if ((tb = fib_get_table(res->r->table)) != NULL)
  97. tb->tb_select_default(tb, flp, res);
  98. }
  99. }
  100. static int fib4_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
  101. {
  102. struct fib4_rule *r = (struct fib4_rule *) rule;
  103. __be32 daddr = fl->fl4_dst;
  104. __be32 saddr = fl->fl4_src;
  105. if (((saddr ^ r->src) & r->srcmask) ||
  106. ((daddr ^ r->dst) & r->dstmask))
  107. return 0;
  108. if (r->tos && (r->tos != fl->fl4_tos))
  109. return 0;
  110. return 1;
  111. }
  112. static struct fib_table *fib_empty_table(void)
  113. {
  114. u32 id;
  115. for (id = 1; id <= RT_TABLE_MAX; id++)
  116. if (fib_get_table(id) == NULL)
  117. return fib_new_table(id);
  118. return NULL;
  119. }
  120. static const struct nla_policy fib4_rule_policy[FRA_MAX+1] = {
  121. FRA_GENERIC_POLICY,
  122. [FRA_FLOW] = { .type = NLA_U32 },
  123. };
  124. static int fib4_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
  125. struct nlmsghdr *nlh, struct fib_rule_hdr *frh,
  126. struct nlattr **tb)
  127. {
  128. int err = -EINVAL;
  129. struct fib4_rule *rule4 = (struct fib4_rule *) rule;
  130. if (frh->tos & ~IPTOS_TOS_MASK)
  131. goto errout;
  132. if (rule->table == RT_TABLE_UNSPEC) {
  133. if (rule->action == FR_ACT_TO_TBL) {
  134. struct fib_table *table;
  135. table = fib_empty_table();
  136. if (table == NULL) {
  137. err = -ENOBUFS;
  138. goto errout;
  139. }
  140. rule->table = table->tb_id;
  141. }
  142. }
  143. if (frh->src_len)
  144. rule4->src = nla_get_be32(tb[FRA_SRC]);
  145. if (frh->dst_len)
  146. rule4->dst = nla_get_be32(tb[FRA_DST]);
  147. #ifdef CONFIG_NET_CLS_ROUTE
  148. if (tb[FRA_FLOW])
  149. rule4->tclassid = nla_get_u32(tb[FRA_FLOW]);
  150. #endif
  151. rule4->src_len = frh->src_len;
  152. rule4->srcmask = inet_make_mask(rule4->src_len);
  153. rule4->dst_len = frh->dst_len;
  154. rule4->dstmask = inet_make_mask(rule4->dst_len);
  155. rule4->tos = frh->tos;
  156. err = 0;
  157. errout:
  158. return err;
  159. }
  160. static int fib4_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
  161. struct nlattr **tb)
  162. {
  163. struct fib4_rule *rule4 = (struct fib4_rule *) rule;
  164. if (frh->src_len && (rule4->src_len != frh->src_len))
  165. return 0;
  166. if (frh->dst_len && (rule4->dst_len != frh->dst_len))
  167. return 0;
  168. if (frh->tos && (rule4->tos != frh->tos))
  169. return 0;
  170. #ifdef CONFIG_NET_CLS_ROUTE
  171. if (tb[FRA_FLOW] && (rule4->tclassid != nla_get_u32(tb[FRA_FLOW])))
  172. return 0;
  173. #endif
  174. if (frh->src_len && (rule4->src != nla_get_be32(tb[FRA_SRC])))
  175. return 0;
  176. if (frh->dst_len && (rule4->dst != nla_get_be32(tb[FRA_DST])))
  177. return 0;
  178. return 1;
  179. }
  180. static int fib4_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
  181. struct nlmsghdr *nlh, struct fib_rule_hdr *frh)
  182. {
  183. struct fib4_rule *rule4 = (struct fib4_rule *) rule;
  184. frh->family = AF_INET;
  185. frh->dst_len = rule4->dst_len;
  186. frh->src_len = rule4->src_len;
  187. frh->tos = rule4->tos;
  188. if (rule4->dst_len)
  189. NLA_PUT_BE32(skb, FRA_DST, rule4->dst);
  190. if (rule4->src_len)
  191. NLA_PUT_BE32(skb, FRA_SRC, rule4->src);
  192. #ifdef CONFIG_NET_CLS_ROUTE
  193. if (rule4->tclassid)
  194. NLA_PUT_U32(skb, FRA_FLOW, rule4->tclassid);
  195. #endif
  196. return 0;
  197. nla_put_failure:
  198. return -ENOBUFS;
  199. }
  200. static u32 fib4_rule_default_pref(void)
  201. {
  202. struct list_head *pos;
  203. struct fib_rule *rule;
  204. if (!list_empty(&fib4_rules_ops.rules_list)) {
  205. pos = fib4_rules_ops.rules_list.next;
  206. if (pos->next != &fib4_rules_ops.rules_list) {
  207. rule = list_entry(pos->next, struct fib_rule, list);
  208. if (rule->pref)
  209. return rule->pref - 1;
  210. }
  211. }
  212. return 0;
  213. }
  214. static size_t fib4_rule_nlmsg_payload(struct fib_rule *rule)
  215. {
  216. return nla_total_size(4) /* dst */
  217. + nla_total_size(4) /* src */
  218. + nla_total_size(4); /* flow */
  219. }
  220. static void fib4_rule_flush_cache(void)
  221. {
  222. rt_cache_flush(-1);
  223. }
  224. static struct fib_rules_ops fib4_rules_ops = {
  225. .family = AF_INET,
  226. .rule_size = sizeof(struct fib4_rule),
  227. .addr_size = sizeof(u32),
  228. .action = fib4_rule_action,
  229. .match = fib4_rule_match,
  230. .configure = fib4_rule_configure,
  231. .compare = fib4_rule_compare,
  232. .fill = fib4_rule_fill,
  233. .default_pref = fib4_rule_default_pref,
  234. .nlmsg_payload = fib4_rule_nlmsg_payload,
  235. .flush_cache = fib4_rule_flush_cache,
  236. .nlgroup = RTNLGRP_IPV4_RULE,
  237. .policy = fib4_rule_policy,
  238. .rules_list = LIST_HEAD_INIT(fib4_rules_ops.rules_list),
  239. .owner = THIS_MODULE,
  240. };
  241. static int __init fib_default_rules_init(void)
  242. {
  243. int err;
  244. err = fib_default_rule_add(&fib4_rules_ops, 0,
  245. RT_TABLE_LOCAL, FIB_RULE_PERMANENT);
  246. if (err < 0)
  247. return err;
  248. err = fib_default_rule_add(&fib4_rules_ops, 0x7FFE,
  249. RT_TABLE_MAIN, 0);
  250. if (err < 0)
  251. return err;
  252. err = fib_default_rule_add(&fib4_rules_ops, 0x7FFF,
  253. RT_TABLE_DEFAULT, 0);
  254. if (err < 0)
  255. return err;
  256. return 0;
  257. }
  258. void __init fib4_rules_init(void)
  259. {
  260. BUG_ON(fib_default_rules_init());
  261. fib_rules_register(&fib4_rules_ops);
  262. }