fib_rules.c 8.6 KB

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