fib_rules.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. static struct fib4_rule default_rule = {
  49. .common = {
  50. .refcnt = ATOMIC_INIT(2),
  51. .pref = 0x7FFF,
  52. .table = RT_TABLE_DEFAULT,
  53. .action = FR_ACT_TO_TBL,
  54. },
  55. };
  56. static struct fib4_rule main_rule = {
  57. .common = {
  58. .refcnt = ATOMIC_INIT(2),
  59. .pref = 0x7FFE,
  60. .table = RT_TABLE_MAIN,
  61. .action = FR_ACT_TO_TBL,
  62. },
  63. };
  64. static struct fib4_rule local_rule = {
  65. .common = {
  66. .refcnt = ATOMIC_INIT(2),
  67. .table = RT_TABLE_LOCAL,
  68. .action = FR_ACT_TO_TBL,
  69. .flags = FIB_RULE_PERMANENT,
  70. },
  71. };
  72. #ifdef CONFIG_NET_CLS_ROUTE
  73. u32 fib_rules_tclass(struct fib_result *res)
  74. {
  75. return res->r ? ((struct fib4_rule *) res->r)->tclassid : 0;
  76. }
  77. #endif
  78. int fib_lookup(struct flowi *flp, struct fib_result *res)
  79. {
  80. struct fib_lookup_arg arg = {
  81. .result = res,
  82. };
  83. int err;
  84. err = fib_rules_lookup(&fib4_rules_ops, flp, 0, &arg);
  85. res->r = arg.rule;
  86. return err;
  87. }
  88. static int fib4_rule_action(struct fib_rule *rule, struct flowi *flp,
  89. int flags, struct fib_lookup_arg *arg)
  90. {
  91. int err = -EAGAIN;
  92. struct fib_table *tbl;
  93. switch (rule->action) {
  94. case FR_ACT_TO_TBL:
  95. break;
  96. case FR_ACT_UNREACHABLE:
  97. err = -ENETUNREACH;
  98. goto errout;
  99. case FR_ACT_PROHIBIT:
  100. err = -EACCES;
  101. goto errout;
  102. case FR_ACT_BLACKHOLE:
  103. default:
  104. err = -EINVAL;
  105. goto errout;
  106. }
  107. if ((tbl = fib_get_table(rule->table)) == NULL)
  108. goto errout;
  109. err = tbl->tb_lookup(tbl, flp, (struct fib_result *) arg->result);
  110. if (err > 0)
  111. err = -EAGAIN;
  112. errout:
  113. return err;
  114. }
  115. void fib_select_default(const struct flowi *flp, struct fib_result *res)
  116. {
  117. if (res->r && res->r->action == FR_ACT_TO_TBL &&
  118. FIB_RES_GW(*res) && FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK) {
  119. struct fib_table *tb;
  120. if ((tb = fib_get_table(res->r->table)) != NULL)
  121. tb->tb_select_default(tb, flp, res);
  122. }
  123. }
  124. static int fib4_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
  125. {
  126. struct fib4_rule *r = (struct fib4_rule *) rule;
  127. __be32 daddr = fl->fl4_dst;
  128. __be32 saddr = fl->fl4_src;
  129. if (((saddr ^ r->src) & r->srcmask) ||
  130. ((daddr ^ r->dst) & r->dstmask))
  131. return 0;
  132. if (r->tos && (r->tos != fl->fl4_tos))
  133. return 0;
  134. return 1;
  135. }
  136. static struct fib_table *fib_empty_table(void)
  137. {
  138. u32 id;
  139. for (id = 1; id <= RT_TABLE_MAX; id++)
  140. if (fib_get_table(id) == NULL)
  141. return fib_new_table(id);
  142. return NULL;
  143. }
  144. static const struct nla_policy fib4_rule_policy[FRA_MAX+1] = {
  145. FRA_GENERIC_POLICY,
  146. [FRA_FLOW] = { .type = NLA_U32 },
  147. };
  148. static int fib4_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
  149. struct nlmsghdr *nlh, struct fib_rule_hdr *frh,
  150. struct nlattr **tb)
  151. {
  152. int err = -EINVAL;
  153. struct fib4_rule *rule4 = (struct fib4_rule *) rule;
  154. if (frh->tos & ~IPTOS_TOS_MASK)
  155. goto errout;
  156. if (rule->table == RT_TABLE_UNSPEC) {
  157. if (rule->action == FR_ACT_TO_TBL) {
  158. struct fib_table *table;
  159. table = fib_empty_table();
  160. if (table == NULL) {
  161. err = -ENOBUFS;
  162. goto errout;
  163. }
  164. rule->table = table->tb_id;
  165. }
  166. }
  167. if (frh->src_len)
  168. rule4->src = nla_get_be32(tb[FRA_SRC]);
  169. if (frh->dst_len)
  170. rule4->dst = nla_get_be32(tb[FRA_DST]);
  171. #ifdef CONFIG_NET_CLS_ROUTE
  172. if (tb[FRA_FLOW])
  173. rule4->tclassid = nla_get_u32(tb[FRA_FLOW]);
  174. #endif
  175. rule4->src_len = frh->src_len;
  176. rule4->srcmask = inet_make_mask(rule4->src_len);
  177. rule4->dst_len = frh->dst_len;
  178. rule4->dstmask = inet_make_mask(rule4->dst_len);
  179. rule4->tos = frh->tos;
  180. err = 0;
  181. errout:
  182. return err;
  183. }
  184. static int fib4_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
  185. struct nlattr **tb)
  186. {
  187. struct fib4_rule *rule4 = (struct fib4_rule *) rule;
  188. if (frh->src_len && (rule4->src_len != frh->src_len))
  189. return 0;
  190. if (frh->dst_len && (rule4->dst_len != frh->dst_len))
  191. return 0;
  192. if (frh->tos && (rule4->tos != frh->tos))
  193. return 0;
  194. #ifdef CONFIG_NET_CLS_ROUTE
  195. if (tb[FRA_FLOW] && (rule4->tclassid != nla_get_u32(tb[FRA_FLOW])))
  196. return 0;
  197. #endif
  198. if (frh->src_len && (rule4->src != nla_get_be32(tb[FRA_SRC])))
  199. return 0;
  200. if (frh->dst_len && (rule4->dst != nla_get_be32(tb[FRA_DST])))
  201. return 0;
  202. return 1;
  203. }
  204. static int fib4_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
  205. struct nlmsghdr *nlh, struct fib_rule_hdr *frh)
  206. {
  207. struct fib4_rule *rule4 = (struct fib4_rule *) rule;
  208. frh->family = AF_INET;
  209. frh->dst_len = rule4->dst_len;
  210. frh->src_len = rule4->src_len;
  211. frh->tos = rule4->tos;
  212. if (rule4->dst_len)
  213. NLA_PUT_BE32(skb, FRA_DST, rule4->dst);
  214. if (rule4->src_len)
  215. NLA_PUT_BE32(skb, FRA_SRC, rule4->src);
  216. #ifdef CONFIG_NET_CLS_ROUTE
  217. if (rule4->tclassid)
  218. NLA_PUT_U32(skb, FRA_FLOW, rule4->tclassid);
  219. #endif
  220. return 0;
  221. nla_put_failure:
  222. return -ENOBUFS;
  223. }
  224. static u32 fib4_rule_default_pref(void)
  225. {
  226. struct list_head *pos;
  227. struct fib_rule *rule;
  228. if (!list_empty(&fib4_rules_ops.rules_list)) {
  229. pos = fib4_rules_ops.rules_list.next;
  230. if (pos->next != &fib4_rules_ops.rules_list) {
  231. rule = list_entry(pos->next, struct fib_rule, list);
  232. if (rule->pref)
  233. return rule->pref - 1;
  234. }
  235. }
  236. return 0;
  237. }
  238. static size_t fib4_rule_nlmsg_payload(struct fib_rule *rule)
  239. {
  240. return nla_total_size(4) /* dst */
  241. + nla_total_size(4) /* src */
  242. + nla_total_size(4); /* flow */
  243. }
  244. static void fib4_rule_flush_cache(void)
  245. {
  246. rt_cache_flush(-1);
  247. }
  248. static struct fib_rules_ops fib4_rules_ops = {
  249. .family = AF_INET,
  250. .rule_size = sizeof(struct fib4_rule),
  251. .addr_size = sizeof(u32),
  252. .action = fib4_rule_action,
  253. .match = fib4_rule_match,
  254. .configure = fib4_rule_configure,
  255. .compare = fib4_rule_compare,
  256. .fill = fib4_rule_fill,
  257. .default_pref = fib4_rule_default_pref,
  258. .nlmsg_payload = fib4_rule_nlmsg_payload,
  259. .flush_cache = fib4_rule_flush_cache,
  260. .nlgroup = RTNLGRP_IPV4_RULE,
  261. .policy = fib4_rule_policy,
  262. .rules_list = LIST_HEAD_INIT(fib4_rules_ops.rules_list),
  263. .owner = THIS_MODULE,
  264. };
  265. void __init fib4_rules_init(void)
  266. {
  267. list_add_tail(&local_rule.common.list, &fib4_rules_ops.rules_list);
  268. list_add_tail(&main_rule.common.list, &fib4_rules_ops.rules_list);
  269. list_add_tail(&default_rule.common.list, &fib4_rules_ops.rules_list);
  270. fib_rules_register(&fib4_rules_ops);
  271. }