fib_rules.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * INET An implementation of the TCP/IP protocol suite for the LINUX
  4. * operating system. INET is implemented using the BSD Socket
  5. * interface as the means of communication with the user level.
  6. *
  7. * IPv4 Forwarding Information Base: policy rules.
  8. *
  9. * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  10. * Thomas Graf <tgraf@suug.ch>
  11. *
  12. * Fixes:
  13. * Rani Assaf : local_rule cannot be deleted
  14. * Marc Boucher : routing by fwmark
  15. */
  16. #include <linux/types.h>
  17. #include <linux/kernel.h>
  18. #include <linux/netdevice.h>
  19. #include <linux/netlink.h>
  20. #include <linux/inetdevice.h>
  21. #include <linux/init.h>
  22. #include <linux/list.h>
  23. #include <linux/rcupdate.h>
  24. #include <linux/export.h>
  25. #include <net/inet_dscp.h>
  26. #include <net/ip.h>
  27. #include <net/route.h>
  28. #include <net/tcp.h>
  29. #include <net/ip_fib.h>
  30. #include <net/nexthop.h>
  31. #include <net/fib_rules.h>
  32. #include <linux/indirect_call_wrapper.h>
  33. struct fib4_rule {
  34. struct fib_rule common;
  35. u8 dst_len;
  36. u8 src_len;
  37. dscp_t dscp;
  38. u8 dscp_full:1; /* DSCP or TOS selector */
  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->dscp)
  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. struct netlink_ext_ack *extack)
  67. {
  68. return fib_rules_dump(net, nb, AF_INET, extack);
  69. }
  70. unsigned int fib4_rules_seq_read(struct net *net)
  71. {
  72. return fib_rules_seq_read(net, AF_INET);
  73. }
  74. int __fib_lookup(struct net *net, struct flowi4 *flp,
  75. struct fib_result *res, unsigned int flags)
  76. {
  77. struct fib_lookup_arg arg = {
  78. .result = res,
  79. .flags = flags,
  80. };
  81. int err;
  82. /* update flow if oif or iif point to device enslaved to l3mdev */
  83. l3mdev_update_flow(net, flowi4_to_flowi(flp));
  84. err = fib_rules_lookup(net->ipv4.rules_ops, flowi4_to_flowi(flp), 0, &arg);
  85. #ifdef CONFIG_IP_ROUTE_CLASSID
  86. if (arg.rule)
  87. res->tclassid = ((struct fib4_rule *)arg.rule)->tclassid;
  88. else
  89. res->tclassid = 0;
  90. #endif
  91. if (err == -ESRCH)
  92. err = -ENETUNREACH;
  93. return err;
  94. }
  95. EXPORT_SYMBOL_GPL(__fib_lookup);
  96. INDIRECT_CALLABLE_SCOPE int fib4_rule_action(struct fib_rule *rule,
  97. struct flowi *flp, int flags,
  98. struct fib_lookup_arg *arg)
  99. {
  100. int err = -EAGAIN;
  101. struct fib_table *tbl;
  102. u32 tb_id;
  103. switch (rule->action) {
  104. case FR_ACT_TO_TBL:
  105. break;
  106. case FR_ACT_UNREACHABLE:
  107. return -ENETUNREACH;
  108. case FR_ACT_PROHIBIT:
  109. return -EACCES;
  110. case FR_ACT_BLACKHOLE:
  111. default:
  112. return -EINVAL;
  113. }
  114. rcu_read_lock();
  115. tb_id = fib_rule_get_table(rule, arg);
  116. tbl = fib_get_table(rule->fr_net, tb_id);
  117. if (tbl)
  118. err = fib_table_lookup(tbl, &flp->u.ip4,
  119. (struct fib_result *)arg->result,
  120. arg->flags);
  121. rcu_read_unlock();
  122. return err;
  123. }
  124. INDIRECT_CALLABLE_SCOPE bool fib4_rule_suppress(struct fib_rule *rule,
  125. int flags,
  126. struct fib_lookup_arg *arg)
  127. {
  128. struct fib_result *result = arg->result;
  129. struct net_device *dev = NULL;
  130. if (result->fi) {
  131. struct fib_nh_common *nhc = fib_info_nhc(result->fi, 0);
  132. dev = nhc->nhc_dev;
  133. }
  134. /* do not accept result if the route does
  135. * not meet the required prefix length
  136. */
  137. if (result->prefixlen <= rule->suppress_prefixlen)
  138. goto suppress_route;
  139. /* do not accept result if the route uses a device
  140. * belonging to a forbidden interface group
  141. */
  142. if (rule->suppress_ifgroup != -1 && dev && dev->group == rule->suppress_ifgroup)
  143. goto suppress_route;
  144. return false;
  145. suppress_route:
  146. if (!(arg->flags & FIB_LOOKUP_NOREF))
  147. fib_info_put(result->fi);
  148. return true;
  149. }
  150. INDIRECT_CALLABLE_SCOPE int fib4_rule_match(struct fib_rule *rule,
  151. struct flowi *fl, int flags)
  152. {
  153. struct fib4_rule *r = (struct fib4_rule *) rule;
  154. struct flowi4 *fl4 = &fl->u.ip4;
  155. __be32 daddr = fl4->daddr;
  156. __be32 saddr = fl4->saddr;
  157. if (((saddr ^ r->src) & r->srcmask) ||
  158. ((daddr ^ r->dst) & r->dstmask))
  159. return 0;
  160. /* When DSCP selector is used we need to match on the entire DSCP field
  161. * in the flow information structure. When TOS selector is used we need
  162. * to mask the upper three DSCP bits prior to matching to maintain
  163. * legacy behavior.
  164. */
  165. if (r->dscp_full && r->dscp != inet_dsfield_to_dscp(fl4->flowi4_tos))
  166. return 0;
  167. else if (!r->dscp_full && r->dscp &&
  168. !fib_dscp_masked_match(r->dscp, fl4))
  169. return 0;
  170. if (rule->ip_proto && (rule->ip_proto != fl4->flowi4_proto))
  171. return 0;
  172. if (fib_rule_port_range_set(&rule->sport_range) &&
  173. !fib_rule_port_inrange(&rule->sport_range, fl4->fl4_sport))
  174. return 0;
  175. if (fib_rule_port_range_set(&rule->dport_range) &&
  176. !fib_rule_port_inrange(&rule->dport_range, fl4->fl4_dport))
  177. return 0;
  178. return 1;
  179. }
  180. static struct fib_table *fib_empty_table(struct net *net)
  181. {
  182. u32 id = 1;
  183. while (1) {
  184. if (!fib_get_table(net, id))
  185. return fib_new_table(net, id);
  186. if (id++ == RT_TABLE_MAX)
  187. break;
  188. }
  189. return NULL;
  190. }
  191. static int fib4_nl2rule_dscp(const struct nlattr *nla, struct fib4_rule *rule4,
  192. struct netlink_ext_ack *extack)
  193. {
  194. if (rule4->dscp) {
  195. NL_SET_ERR_MSG(extack, "Cannot specify both TOS and DSCP");
  196. return -EINVAL;
  197. }
  198. rule4->dscp = inet_dsfield_to_dscp(nla_get_u8(nla) << 2);
  199. rule4->dscp_full = true;
  200. return 0;
  201. }
  202. static int fib4_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
  203. struct fib_rule_hdr *frh,
  204. struct nlattr **tb,
  205. struct netlink_ext_ack *extack)
  206. {
  207. struct fib4_rule *rule4 = (struct fib4_rule *)rule;
  208. struct net *net = rule->fr_net;
  209. int err = -EINVAL;
  210. if (!inet_validate_dscp(frh->tos)) {
  211. NL_SET_ERR_MSG(extack,
  212. "Invalid dsfield (tos): ECN bits must be 0");
  213. goto errout;
  214. }
  215. /* IPv4 currently doesn't handle high order DSCP bits correctly */
  216. if (frh->tos & ~IPTOS_TOS_MASK) {
  217. NL_SET_ERR_MSG(extack, "Invalid tos");
  218. goto errout;
  219. }
  220. rule4->dscp = inet_dsfield_to_dscp(frh->tos);
  221. if (tb[FRA_DSCP] &&
  222. fib4_nl2rule_dscp(tb[FRA_DSCP], rule4, extack) < 0)
  223. goto errout;
  224. /* split local/main if they are not already split */
  225. err = fib_unmerge(net);
  226. if (err)
  227. goto errout;
  228. if (rule->table == RT_TABLE_UNSPEC && !rule->l3mdev) {
  229. if (rule->action == FR_ACT_TO_TBL) {
  230. struct fib_table *table;
  231. table = fib_empty_table(net);
  232. if (!table) {
  233. err = -ENOBUFS;
  234. goto errout;
  235. }
  236. rule->table = table->tb_id;
  237. }
  238. }
  239. if (frh->src_len)
  240. rule4->src = nla_get_in_addr(tb[FRA_SRC]);
  241. if (frh->dst_len)
  242. rule4->dst = nla_get_in_addr(tb[FRA_DST]);
  243. #ifdef CONFIG_IP_ROUTE_CLASSID
  244. if (tb[FRA_FLOW]) {
  245. rule4->tclassid = nla_get_u32(tb[FRA_FLOW]);
  246. if (rule4->tclassid)
  247. atomic_inc(&net->ipv4.fib_num_tclassid_users);
  248. }
  249. #endif
  250. if (fib_rule_requires_fldissect(rule))
  251. net->ipv4.fib_rules_require_fldissect++;
  252. rule4->src_len = frh->src_len;
  253. rule4->srcmask = inet_make_mask(rule4->src_len);
  254. rule4->dst_len = frh->dst_len;
  255. rule4->dstmask = inet_make_mask(rule4->dst_len);
  256. net->ipv4.fib_has_custom_rules = true;
  257. err = 0;
  258. errout:
  259. return err;
  260. }
  261. static int fib4_rule_delete(struct fib_rule *rule)
  262. {
  263. struct net *net = rule->fr_net;
  264. int err;
  265. /* split local/main if they are not already split */
  266. err = fib_unmerge(net);
  267. if (err)
  268. goto errout;
  269. #ifdef CONFIG_IP_ROUTE_CLASSID
  270. if (((struct fib4_rule *)rule)->tclassid)
  271. atomic_dec(&net->ipv4.fib_num_tclassid_users);
  272. #endif
  273. net->ipv4.fib_has_custom_rules = true;
  274. if (net->ipv4.fib_rules_require_fldissect &&
  275. fib_rule_requires_fldissect(rule))
  276. net->ipv4.fib_rules_require_fldissect--;
  277. errout:
  278. return err;
  279. }
  280. static int fib4_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
  281. struct nlattr **tb)
  282. {
  283. struct fib4_rule *rule4 = (struct fib4_rule *) rule;
  284. if (frh->src_len && (rule4->src_len != frh->src_len))
  285. return 0;
  286. if (frh->dst_len && (rule4->dst_len != frh->dst_len))
  287. return 0;
  288. if (frh->tos &&
  289. (rule4->dscp_full ||
  290. inet_dscp_to_dsfield(rule4->dscp) != frh->tos))
  291. return 0;
  292. if (tb[FRA_DSCP]) {
  293. dscp_t dscp;
  294. dscp = inet_dsfield_to_dscp(nla_get_u8(tb[FRA_DSCP]) << 2);
  295. if (!rule4->dscp_full || rule4->dscp != dscp)
  296. return 0;
  297. }
  298. #ifdef CONFIG_IP_ROUTE_CLASSID
  299. if (tb[FRA_FLOW] && (rule4->tclassid != nla_get_u32(tb[FRA_FLOW])))
  300. return 0;
  301. #endif
  302. if (frh->src_len && (rule4->src != nla_get_in_addr(tb[FRA_SRC])))
  303. return 0;
  304. if (frh->dst_len && (rule4->dst != nla_get_in_addr(tb[FRA_DST])))
  305. return 0;
  306. return 1;
  307. }
  308. static int fib4_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
  309. struct fib_rule_hdr *frh)
  310. {
  311. struct fib4_rule *rule4 = (struct fib4_rule *) rule;
  312. frh->dst_len = rule4->dst_len;
  313. frh->src_len = rule4->src_len;
  314. if (rule4->dscp_full) {
  315. frh->tos = 0;
  316. if (nla_put_u8(skb, FRA_DSCP,
  317. inet_dscp_to_dsfield(rule4->dscp) >> 2))
  318. goto nla_put_failure;
  319. } else {
  320. frh->tos = inet_dscp_to_dsfield(rule4->dscp);
  321. }
  322. if ((rule4->dst_len &&
  323. nla_put_in_addr(skb, FRA_DST, rule4->dst)) ||
  324. (rule4->src_len &&
  325. nla_put_in_addr(skb, FRA_SRC, rule4->src)))
  326. goto nla_put_failure;
  327. #ifdef CONFIG_IP_ROUTE_CLASSID
  328. if (rule4->tclassid &&
  329. nla_put_u32(skb, FRA_FLOW, rule4->tclassid))
  330. goto nla_put_failure;
  331. #endif
  332. return 0;
  333. nla_put_failure:
  334. return -ENOBUFS;
  335. }
  336. static size_t fib4_rule_nlmsg_payload(struct fib_rule *rule)
  337. {
  338. return nla_total_size(4) /* dst */
  339. + nla_total_size(4) /* src */
  340. + nla_total_size(4) /* flow */
  341. + nla_total_size(1); /* dscp */
  342. }
  343. static void fib4_rule_flush_cache(struct fib_rules_ops *ops)
  344. {
  345. rt_cache_flush(ops->fro_net);
  346. }
  347. static const struct fib_rules_ops __net_initconst fib4_rules_ops_template = {
  348. .family = AF_INET,
  349. .rule_size = sizeof(struct fib4_rule),
  350. .addr_size = sizeof(u32),
  351. .action = fib4_rule_action,
  352. .suppress = fib4_rule_suppress,
  353. .match = fib4_rule_match,
  354. .configure = fib4_rule_configure,
  355. .delete = fib4_rule_delete,
  356. .compare = fib4_rule_compare,
  357. .fill = fib4_rule_fill,
  358. .nlmsg_payload = fib4_rule_nlmsg_payload,
  359. .flush_cache = fib4_rule_flush_cache,
  360. .nlgroup = RTNLGRP_IPV4_RULE,
  361. .owner = THIS_MODULE,
  362. };
  363. static int fib_default_rules_init(struct fib_rules_ops *ops)
  364. {
  365. int err;
  366. err = fib_default_rule_add(ops, 0, RT_TABLE_LOCAL);
  367. if (err < 0)
  368. return err;
  369. err = fib_default_rule_add(ops, 0x7FFE, RT_TABLE_MAIN);
  370. if (err < 0)
  371. return err;
  372. err = fib_default_rule_add(ops, 0x7FFF, RT_TABLE_DEFAULT);
  373. if (err < 0)
  374. return err;
  375. return 0;
  376. }
  377. int __net_init fib4_rules_init(struct net *net)
  378. {
  379. int err;
  380. struct fib_rules_ops *ops;
  381. ops = fib_rules_register(&fib4_rules_ops_template, net);
  382. if (IS_ERR(ops))
  383. return PTR_ERR(ops);
  384. err = fib_default_rules_init(ops);
  385. if (err < 0)
  386. goto fail;
  387. net->ipv4.rules_ops = ops;
  388. net->ipv4.fib_has_custom_rules = false;
  389. net->ipv4.fib_rules_require_fldissect = 0;
  390. return 0;
  391. fail:
  392. /* also cleans all rules already added */
  393. fib_rules_unregister(ops);
  394. return err;
  395. }
  396. void __net_exit fib4_rules_exit(struct net *net)
  397. {
  398. fib_rules_unregister(net->ipv4.rules_ops);
  399. }