fib_rules.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 <linux/export.h>
  29. #include <net/ip.h>
  30. #include <net/route.h>
  31. #include <net/tcp.h>
  32. #include <net/ip_fib.h>
  33. #include <net/fib_rules.h>
  34. struct fib4_rule {
  35. struct fib_rule common;
  36. u8 dst_len;
  37. u8 src_len;
  38. u8 tos;
  39. __be32 src;
  40. __be32 srcmask;
  41. __be32 dst;
  42. __be32 dstmask;
  43. #ifdef CONFIG_IP_ROUTE_CLASSID
  44. u32 tclassid;
  45. #endif
  46. };
  47. #ifdef CONFIG_IP_ROUTE_CLASSID
  48. u32 fib_rules_tclass(const struct fib_result *res)
  49. {
  50. return res->r ? ((struct fib4_rule *) res->r)->tclassid : 0;
  51. }
  52. #endif
  53. int fib_lookup(struct net *net, struct flowi4 *flp, struct fib_result *res)
  54. {
  55. struct fib_lookup_arg arg = {
  56. .result = res,
  57. .flags = FIB_LOOKUP_NOREF,
  58. };
  59. int err;
  60. err = fib_rules_lookup(net->ipv4.rules_ops, flowi4_to_flowi(flp), 0, &arg);
  61. res->r = arg.rule;
  62. return err;
  63. }
  64. EXPORT_SYMBOL_GPL(fib_lookup);
  65. static int fib4_rule_action(struct fib_rule *rule, struct flowi *flp,
  66. int flags, struct fib_lookup_arg *arg)
  67. {
  68. int err = -EAGAIN;
  69. struct fib_table *tbl;
  70. switch (rule->action) {
  71. case FR_ACT_TO_TBL:
  72. break;
  73. case FR_ACT_UNREACHABLE:
  74. err = -ENETUNREACH;
  75. goto errout;
  76. case FR_ACT_PROHIBIT:
  77. err = -EACCES;
  78. goto errout;
  79. case FR_ACT_BLACKHOLE:
  80. default:
  81. err = -EINVAL;
  82. goto errout;
  83. }
  84. tbl = fib_get_table(rule->fr_net, rule->table);
  85. if (!tbl)
  86. goto errout;
  87. err = fib_table_lookup(tbl, &flp->u.ip4, (struct fib_result *) arg->result, arg->flags);
  88. if (err > 0)
  89. err = -EAGAIN;
  90. errout:
  91. return err;
  92. }
  93. static int fib4_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
  94. {
  95. struct fib4_rule *r = (struct fib4_rule *) rule;
  96. struct flowi4 *fl4 = &fl->u.ip4;
  97. __be32 daddr = fl4->daddr;
  98. __be32 saddr = fl4->saddr;
  99. if (((saddr ^ r->src) & r->srcmask) ||
  100. ((daddr ^ r->dst) & r->dstmask))
  101. return 0;
  102. if (r->tos && (r->tos != fl4->flowi4_tos))
  103. return 0;
  104. return 1;
  105. }
  106. static struct fib_table *fib_empty_table(struct net *net)
  107. {
  108. u32 id;
  109. for (id = 1; id <= RT_TABLE_MAX; id++)
  110. if (fib_get_table(net, id) == NULL)
  111. return fib_new_table(net, id);
  112. return NULL;
  113. }
  114. static const struct nla_policy fib4_rule_policy[FRA_MAX+1] = {
  115. FRA_GENERIC_POLICY,
  116. [FRA_FLOW] = { .type = NLA_U32 },
  117. };
  118. static int fib4_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
  119. struct fib_rule_hdr *frh,
  120. struct nlattr **tb)
  121. {
  122. struct net *net = sock_net(skb->sk);
  123. int err = -EINVAL;
  124. struct fib4_rule *rule4 = (struct fib4_rule *) rule;
  125. if (frh->tos & ~IPTOS_TOS_MASK)
  126. goto errout;
  127. if (rule->table == RT_TABLE_UNSPEC) {
  128. if (rule->action == FR_ACT_TO_TBL) {
  129. struct fib_table *table;
  130. table = fib_empty_table(net);
  131. if (table == NULL) {
  132. err = -ENOBUFS;
  133. goto errout;
  134. }
  135. rule->table = table->tb_id;
  136. }
  137. }
  138. if (frh->src_len)
  139. rule4->src = nla_get_be32(tb[FRA_SRC]);
  140. if (frh->dst_len)
  141. rule4->dst = nla_get_be32(tb[FRA_DST]);
  142. #ifdef CONFIG_IP_ROUTE_CLASSID
  143. if (tb[FRA_FLOW])
  144. rule4->tclassid = nla_get_u32(tb[FRA_FLOW]);
  145. #endif
  146. rule4->src_len = frh->src_len;
  147. rule4->srcmask = inet_make_mask(rule4->src_len);
  148. rule4->dst_len = frh->dst_len;
  149. rule4->dstmask = inet_make_mask(rule4->dst_len);
  150. rule4->tos = frh->tos;
  151. err = 0;
  152. errout:
  153. return err;
  154. }
  155. static int fib4_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
  156. struct nlattr **tb)
  157. {
  158. struct fib4_rule *rule4 = (struct fib4_rule *) rule;
  159. if (frh->src_len && (rule4->src_len != frh->src_len))
  160. return 0;
  161. if (frh->dst_len && (rule4->dst_len != frh->dst_len))
  162. return 0;
  163. if (frh->tos && (rule4->tos != frh->tos))
  164. return 0;
  165. #ifdef CONFIG_IP_ROUTE_CLASSID
  166. if (tb[FRA_FLOW] && (rule4->tclassid != nla_get_u32(tb[FRA_FLOW])))
  167. return 0;
  168. #endif
  169. if (frh->src_len && (rule4->src != nla_get_be32(tb[FRA_SRC])))
  170. return 0;
  171. if (frh->dst_len && (rule4->dst != nla_get_be32(tb[FRA_DST])))
  172. return 0;
  173. return 1;
  174. }
  175. static int fib4_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
  176. struct fib_rule_hdr *frh)
  177. {
  178. struct fib4_rule *rule4 = (struct fib4_rule *) rule;
  179. frh->dst_len = rule4->dst_len;
  180. frh->src_len = rule4->src_len;
  181. frh->tos = rule4->tos;
  182. if (rule4->dst_len)
  183. NLA_PUT_BE32(skb, FRA_DST, rule4->dst);
  184. if (rule4->src_len)
  185. NLA_PUT_BE32(skb, FRA_SRC, rule4->src);
  186. #ifdef CONFIG_IP_ROUTE_CLASSID
  187. if (rule4->tclassid)
  188. NLA_PUT_U32(skb, FRA_FLOW, rule4->tclassid);
  189. #endif
  190. return 0;
  191. nla_put_failure:
  192. return -ENOBUFS;
  193. }
  194. static size_t fib4_rule_nlmsg_payload(struct fib_rule *rule)
  195. {
  196. return nla_total_size(4) /* dst */
  197. + nla_total_size(4) /* src */
  198. + nla_total_size(4); /* flow */
  199. }
  200. static void fib4_rule_flush_cache(struct fib_rules_ops *ops)
  201. {
  202. rt_cache_flush(ops->fro_net, -1);
  203. }
  204. static const struct fib_rules_ops __net_initdata fib4_rules_ops_template = {
  205. .family = AF_INET,
  206. .rule_size = sizeof(struct fib4_rule),
  207. .addr_size = sizeof(u32),
  208. .action = fib4_rule_action,
  209. .match = fib4_rule_match,
  210. .configure = fib4_rule_configure,
  211. .compare = fib4_rule_compare,
  212. .fill = fib4_rule_fill,
  213. .default_pref = fib_default_rule_pref,
  214. .nlmsg_payload = fib4_rule_nlmsg_payload,
  215. .flush_cache = fib4_rule_flush_cache,
  216. .nlgroup = RTNLGRP_IPV4_RULE,
  217. .policy = fib4_rule_policy,
  218. .owner = THIS_MODULE,
  219. };
  220. static int fib_default_rules_init(struct fib_rules_ops *ops)
  221. {
  222. int err;
  223. err = fib_default_rule_add(ops, 0, RT_TABLE_LOCAL, 0);
  224. if (err < 0)
  225. return err;
  226. err = fib_default_rule_add(ops, 0x7FFE, RT_TABLE_MAIN, 0);
  227. if (err < 0)
  228. return err;
  229. err = fib_default_rule_add(ops, 0x7FFF, RT_TABLE_DEFAULT, 0);
  230. if (err < 0)
  231. return err;
  232. return 0;
  233. }
  234. int __net_init fib4_rules_init(struct net *net)
  235. {
  236. int err;
  237. struct fib_rules_ops *ops;
  238. ops = fib_rules_register(&fib4_rules_ops_template, net);
  239. if (IS_ERR(ops))
  240. return PTR_ERR(ops);
  241. err = fib_default_rules_init(ops);
  242. if (err < 0)
  243. goto fail;
  244. net->ipv4.rules_ops = ops;
  245. return 0;
  246. fail:
  247. /* also cleans all rules already added */
  248. fib_rules_unregister(ops);
  249. return err;
  250. }
  251. void __net_exit fib4_rules_exit(struct net *net)
  252. {
  253. fib_rules_unregister(net->ipv4.rules_ops);
  254. }