fib_rules.c 6.7 KB

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