fib_rules.c 7.1 KB

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