fib_rules.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. int __fib_lookup(struct net *net, struct flowi4 *flp, struct fib_result *res)
  48. {
  49. struct fib_lookup_arg arg = {
  50. .result = res,
  51. .flags = FIB_LOOKUP_NOREF,
  52. };
  53. int err;
  54. err = fib_rules_lookup(net->ipv4.rules_ops, flowi4_to_flowi(flp), 0, &arg);
  55. #ifdef CONFIG_IP_ROUTE_CLASSID
  56. if (arg.rule)
  57. res->tclassid = ((struct fib4_rule *)arg.rule)->tclassid;
  58. else
  59. res->tclassid = 0;
  60. #endif
  61. if (err == -ESRCH)
  62. err = -ENETUNREACH;
  63. return err;
  64. }
  65. EXPORT_SYMBOL_GPL(__fib_lookup);
  66. static int fib4_rule_action(struct fib_rule *rule, struct flowi *flp,
  67. int flags, struct fib_lookup_arg *arg)
  68. {
  69. int err = -EAGAIN;
  70. struct fib_table *tbl;
  71. switch (rule->action) {
  72. case FR_ACT_TO_TBL:
  73. break;
  74. case FR_ACT_UNREACHABLE:
  75. return -ENETUNREACH;
  76. case FR_ACT_PROHIBIT:
  77. return -EACCES;
  78. case FR_ACT_BLACKHOLE:
  79. default:
  80. return -EINVAL;
  81. }
  82. rcu_read_lock();
  83. tbl = fib_get_table(rule->fr_net, rule->table);
  84. if (tbl)
  85. err = fib_table_lookup(tbl, &flp->u.ip4,
  86. (struct fib_result *)arg->result,
  87. arg->flags);
  88. rcu_read_unlock();
  89. return err;
  90. }
  91. static bool fib4_rule_suppress(struct fib_rule *rule, struct fib_lookup_arg *arg)
  92. {
  93. struct fib_result *result = (struct fib_result *) arg->result;
  94. struct net_device *dev = NULL;
  95. if (result->fi)
  96. dev = result->fi->fib_dev;
  97. /* do not accept result if the route does
  98. * not meet the required prefix length
  99. */
  100. if (result->prefixlen <= rule->suppress_prefixlen)
  101. goto suppress_route;
  102. /* do not accept result if the route uses a device
  103. * belonging to a forbidden interface group
  104. */
  105. if (rule->suppress_ifgroup != -1 && dev && dev->group == rule->suppress_ifgroup)
  106. goto suppress_route;
  107. return false;
  108. suppress_route:
  109. if (!(arg->flags & FIB_LOOKUP_NOREF))
  110. fib_info_put(result->fi);
  111. return true;
  112. }
  113. static int fib4_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
  114. {
  115. struct fib4_rule *r = (struct fib4_rule *) rule;
  116. struct flowi4 *fl4 = &fl->u.ip4;
  117. __be32 daddr = fl4->daddr;
  118. __be32 saddr = fl4->saddr;
  119. if (((saddr ^ r->src) & r->srcmask) ||
  120. ((daddr ^ r->dst) & r->dstmask))
  121. return 0;
  122. if (r->tos && (r->tos != fl4->flowi4_tos))
  123. return 0;
  124. return 1;
  125. }
  126. static struct fib_table *fib_empty_table(struct net *net)
  127. {
  128. u32 id;
  129. for (id = 1; id <= RT_TABLE_MAX; id++)
  130. if (fib_get_table(net, id) == NULL)
  131. return fib_new_table(net, id);
  132. return NULL;
  133. }
  134. static const struct nla_policy fib4_rule_policy[FRA_MAX+1] = {
  135. FRA_GENERIC_POLICY,
  136. [FRA_FLOW] = { .type = NLA_U32 },
  137. };
  138. static int fib4_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
  139. struct fib_rule_hdr *frh,
  140. struct nlattr **tb)
  141. {
  142. struct net *net = sock_net(skb->sk);
  143. int err = -EINVAL;
  144. struct fib4_rule *rule4 = (struct fib4_rule *) rule;
  145. if (frh->tos & ~IPTOS_TOS_MASK)
  146. goto errout;
  147. if (rule->table == RT_TABLE_UNSPEC) {
  148. if (rule->action == FR_ACT_TO_TBL) {
  149. struct fib_table *table;
  150. table = fib_empty_table(net);
  151. if (table == NULL) {
  152. err = -ENOBUFS;
  153. goto errout;
  154. }
  155. rule->table = table->tb_id;
  156. }
  157. }
  158. if (frh->src_len)
  159. rule4->src = nla_get_be32(tb[FRA_SRC]);
  160. if (frh->dst_len)
  161. rule4->dst = nla_get_be32(tb[FRA_DST]);
  162. #ifdef CONFIG_IP_ROUTE_CLASSID
  163. if (tb[FRA_FLOW]) {
  164. rule4->tclassid = nla_get_u32(tb[FRA_FLOW]);
  165. if (rule4->tclassid)
  166. net->ipv4.fib_num_tclassid_users++;
  167. }
  168. #endif
  169. rule4->src_len = frh->src_len;
  170. rule4->srcmask = inet_make_mask(rule4->src_len);
  171. rule4->dst_len = frh->dst_len;
  172. rule4->dstmask = inet_make_mask(rule4->dst_len);
  173. rule4->tos = frh->tos;
  174. net->ipv4.fib_has_custom_rules = true;
  175. err = 0;
  176. errout:
  177. return err;
  178. }
  179. static void fib4_rule_delete(struct fib_rule *rule)
  180. {
  181. struct net *net = rule->fr_net;
  182. #ifdef CONFIG_IP_ROUTE_CLASSID
  183. struct fib4_rule *rule4 = (struct fib4_rule *) rule;
  184. if (rule4->tclassid)
  185. net->ipv4.fib_num_tclassid_users--;
  186. #endif
  187. net->ipv4.fib_has_custom_rules = true;
  188. }
  189. static int fib4_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
  190. struct nlattr **tb)
  191. {
  192. struct fib4_rule *rule4 = (struct fib4_rule *) rule;
  193. if (frh->src_len && (rule4->src_len != frh->src_len))
  194. return 0;
  195. if (frh->dst_len && (rule4->dst_len != frh->dst_len))
  196. return 0;
  197. if (frh->tos && (rule4->tos != frh->tos))
  198. return 0;
  199. #ifdef CONFIG_IP_ROUTE_CLASSID
  200. if (tb[FRA_FLOW] && (rule4->tclassid != nla_get_u32(tb[FRA_FLOW])))
  201. return 0;
  202. #endif
  203. if (frh->src_len && (rule4->src != nla_get_be32(tb[FRA_SRC])))
  204. return 0;
  205. if (frh->dst_len && (rule4->dst != nla_get_be32(tb[FRA_DST])))
  206. return 0;
  207. return 1;
  208. }
  209. static int fib4_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
  210. struct fib_rule_hdr *frh)
  211. {
  212. struct fib4_rule *rule4 = (struct fib4_rule *) rule;
  213. frh->dst_len = rule4->dst_len;
  214. frh->src_len = rule4->src_len;
  215. frh->tos = rule4->tos;
  216. if ((rule4->dst_len &&
  217. nla_put_be32(skb, FRA_DST, rule4->dst)) ||
  218. (rule4->src_len &&
  219. nla_put_be32(skb, FRA_SRC, rule4->src)))
  220. goto nla_put_failure;
  221. #ifdef CONFIG_IP_ROUTE_CLASSID
  222. if (rule4->tclassid &&
  223. nla_put_u32(skb, FRA_FLOW, rule4->tclassid))
  224. goto nla_put_failure;
  225. #endif
  226. return 0;
  227. nla_put_failure:
  228. return -ENOBUFS;
  229. }
  230. static size_t fib4_rule_nlmsg_payload(struct fib_rule *rule)
  231. {
  232. return nla_total_size(4) /* dst */
  233. + nla_total_size(4) /* src */
  234. + nla_total_size(4); /* flow */
  235. }
  236. static void fib4_rule_flush_cache(struct fib_rules_ops *ops)
  237. {
  238. rt_cache_flush(ops->fro_net);
  239. }
  240. static const struct fib_rules_ops __net_initconst fib4_rules_ops_template = {
  241. .family = AF_INET,
  242. .rule_size = sizeof(struct fib4_rule),
  243. .addr_size = sizeof(u32),
  244. .action = fib4_rule_action,
  245. .suppress = fib4_rule_suppress,
  246. .match = fib4_rule_match,
  247. .configure = fib4_rule_configure,
  248. .delete = fib4_rule_delete,
  249. .compare = fib4_rule_compare,
  250. .fill = fib4_rule_fill,
  251. .default_pref = fib_default_rule_pref,
  252. .nlmsg_payload = fib4_rule_nlmsg_payload,
  253. .flush_cache = fib4_rule_flush_cache,
  254. .nlgroup = RTNLGRP_IPV4_RULE,
  255. .policy = fib4_rule_policy,
  256. .owner = THIS_MODULE,
  257. };
  258. static int fib_default_rules_init(struct fib_rules_ops *ops)
  259. {
  260. int err;
  261. err = fib_default_rule_add(ops, 0, RT_TABLE_LOCAL, 0);
  262. if (err < 0)
  263. return err;
  264. err = fib_default_rule_add(ops, 0x7FFE, RT_TABLE_MAIN, 0);
  265. if (err < 0)
  266. return err;
  267. err = fib_default_rule_add(ops, 0x7FFF, RT_TABLE_DEFAULT, 0);
  268. if (err < 0)
  269. return err;
  270. return 0;
  271. }
  272. int __net_init fib4_rules_init(struct net *net)
  273. {
  274. int err;
  275. struct fib_rules_ops *ops;
  276. ops = fib_rules_register(&fib4_rules_ops_template, net);
  277. if (IS_ERR(ops))
  278. return PTR_ERR(ops);
  279. err = fib_default_rules_init(ops);
  280. if (err < 0)
  281. goto fail;
  282. net->ipv4.rules_ops = ops;
  283. net->ipv4.fib_has_custom_rules = false;
  284. return 0;
  285. fail:
  286. /* also cleans all rules already added */
  287. fib_rules_unregister(ops);
  288. return err;
  289. }
  290. void __net_exit fib4_rules_exit(struct net *net)
  291. {
  292. fib_rules_unregister(net->ipv4.rules_ops);
  293. }