fib_rules.c 9.7 KB

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