fib_rules.c 9.8 KB

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