fib_rules.c 8.2 KB

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