fib_rules.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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/flow.h>
  26. #include <net/inet_dscp.h>
  27. #include <net/ip.h>
  28. #include <net/route.h>
  29. #include <net/tcp.h>
  30. #include <net/ip_fib.h>
  31. #include <net/nexthop.h>
  32. #include <net/fib_rules.h>
  33. #include <linux/indirect_call_wrapper.h>
  34. struct fib4_rule {
  35. struct fib_rule common;
  36. u8 dst_len;
  37. u8 src_len;
  38. dscp_t dscp;
  39. dscp_t dscp_mask;
  40. u8 dscp_full:1; /* DSCP or TOS selector */
  41. __be32 src;
  42. __be32 srcmask;
  43. __be32 dst;
  44. __be32 dstmask;
  45. #ifdef CONFIG_IP_ROUTE_CLASSID
  46. u32 tclassid;
  47. #endif
  48. };
  49. static bool fib4_rule_matchall(const struct fib_rule *rule)
  50. {
  51. struct fib4_rule *r = container_of(rule, struct fib4_rule, common);
  52. if (r->dst_len || r->src_len || r->dscp)
  53. return false;
  54. return fib_rule_matchall(rule);
  55. }
  56. bool fib4_rule_default(const struct fib_rule *rule)
  57. {
  58. if (!fib4_rule_matchall(rule) || rule->action != FR_ACT_TO_TBL ||
  59. rule->l3mdev)
  60. return false;
  61. if (rule->table != RT_TABLE_LOCAL && rule->table != RT_TABLE_MAIN &&
  62. rule->table != RT_TABLE_DEFAULT)
  63. return false;
  64. return true;
  65. }
  66. EXPORT_SYMBOL_GPL(fib4_rule_default);
  67. int fib4_rules_dump(struct net *net, struct notifier_block *nb,
  68. struct netlink_ext_ack *extack)
  69. {
  70. return fib_rules_dump(net, nb, AF_INET, extack);
  71. }
  72. unsigned int fib4_rules_seq_read(const struct net *net)
  73. {
  74. return fib_rules_seq_read(net, AF_INET);
  75. }
  76. int __fib_lookup(struct net *net, struct flowi4 *flp,
  77. struct fib_result *res, unsigned int flags)
  78. {
  79. struct fib_lookup_arg arg = {
  80. .result = res,
  81. .flags = flags,
  82. };
  83. int err;
  84. /* update flow if oif or iif point to device enslaved to l3mdev */
  85. l3mdev_update_flow(net, flowi4_to_flowi(flp));
  86. err = fib_rules_lookup(net->ipv4.rules_ops, flowi4_to_flowi(flp), 0, &arg);
  87. #ifdef CONFIG_IP_ROUTE_CLASSID
  88. if (arg.rule)
  89. res->tclassid = ((struct fib4_rule *)arg.rule)->tclassid;
  90. else
  91. res->tclassid = 0;
  92. #endif
  93. if (err == -ESRCH)
  94. err = -ENETUNREACH;
  95. return err;
  96. }
  97. EXPORT_SYMBOL_GPL(__fib_lookup);
  98. INDIRECT_CALLABLE_SCOPE int fib4_rule_action(struct fib_rule *rule,
  99. struct flowi *flp, int flags,
  100. struct fib_lookup_arg *arg)
  101. {
  102. int err = -EAGAIN;
  103. struct fib_table *tbl;
  104. u32 tb_id;
  105. switch (rule->action) {
  106. case FR_ACT_TO_TBL:
  107. break;
  108. case FR_ACT_UNREACHABLE:
  109. return -ENETUNREACH;
  110. case FR_ACT_PROHIBIT:
  111. return -EACCES;
  112. case FR_ACT_BLACKHOLE:
  113. default:
  114. return -EINVAL;
  115. }
  116. rcu_read_lock();
  117. tb_id = fib_rule_get_table(rule, arg);
  118. tbl = fib_get_table(rule->fr_net, tb_id);
  119. if (tbl)
  120. err = fib_table_lookup(tbl, &flp->u.ip4,
  121. (struct fib_result *)arg->result,
  122. arg->flags);
  123. rcu_read_unlock();
  124. return err;
  125. }
  126. INDIRECT_CALLABLE_SCOPE bool fib4_rule_suppress(struct fib_rule *rule,
  127. int flags,
  128. struct fib_lookup_arg *arg)
  129. {
  130. struct fib_result *result = arg->result;
  131. struct net_device *dev = NULL;
  132. if (result->fi) {
  133. struct fib_nh_common *nhc = fib_info_nhc(result->fi, 0);
  134. dev = nhc->nhc_dev;
  135. }
  136. /* do not accept result if the route does
  137. * not meet the required prefix length
  138. */
  139. if (result->prefixlen <= rule->suppress_prefixlen)
  140. goto suppress_route;
  141. /* do not accept result if the route uses a device
  142. * belonging to a forbidden interface group
  143. */
  144. if (rule->suppress_ifgroup != -1 && dev && dev->group == rule->suppress_ifgroup)
  145. goto suppress_route;
  146. return false;
  147. suppress_route:
  148. if (!(arg->flags & FIB_LOOKUP_NOREF))
  149. fib_info_put(result->fi);
  150. return true;
  151. }
  152. INDIRECT_CALLABLE_SCOPE int fib4_rule_match(struct fib_rule *rule,
  153. struct flowi *fl, int flags)
  154. {
  155. struct fib4_rule *r = (struct fib4_rule *) rule;
  156. struct flowi4 *fl4 = &fl->u.ip4;
  157. __be32 daddr = fl4->daddr;
  158. __be32 saddr = fl4->saddr;
  159. if (((saddr ^ r->src) & r->srcmask) ||
  160. ((daddr ^ r->dst) & r->dstmask))
  161. return 0;
  162. /* When DSCP selector is used we need to match on the entire DSCP field
  163. * in the flow information structure. When TOS selector is used we need
  164. * to mask the upper three DSCP bits prior to matching to maintain
  165. * legacy behavior.
  166. */
  167. if (r->dscp_full && (r->dscp ^ fl4->flowi4_dscp) & r->dscp_mask)
  168. return 0;
  169. else if (!r->dscp_full && r->dscp &&
  170. !fib_dscp_masked_match(r->dscp, fl4))
  171. return 0;
  172. if (rule->ip_proto && (rule->ip_proto != fl4->flowi4_proto))
  173. return 0;
  174. if (!fib_rule_port_match(&rule->sport_range, rule->sport_mask,
  175. fl4->fl4_sport))
  176. return 0;
  177. if (!fib_rule_port_match(&rule->dport_range, rule->dport_mask,
  178. fl4->fl4_dport))
  179. return 0;
  180. return 1;
  181. }
  182. static struct fib_table *fib_empty_table(struct net *net)
  183. {
  184. u32 id = 1;
  185. while (1) {
  186. if (!fib_get_table(net, id))
  187. return fib_new_table(net, id);
  188. if (id++ == RT_TABLE_MAX)
  189. break;
  190. }
  191. return NULL;
  192. }
  193. static int fib4_nl2rule_dscp(const struct nlattr *nla, struct fib4_rule *rule4,
  194. struct netlink_ext_ack *extack)
  195. {
  196. if (rule4->dscp) {
  197. NL_SET_ERR_MSG(extack, "Cannot specify both TOS and DSCP");
  198. return -EINVAL;
  199. }
  200. rule4->dscp = inet_dsfield_to_dscp(nla_get_u8(nla) << 2);
  201. rule4->dscp_mask = inet_dsfield_to_dscp(INET_DSCP_MASK);
  202. rule4->dscp_full = true;
  203. return 0;
  204. }
  205. static int fib4_nl2rule_dscp_mask(const struct nlattr *nla,
  206. struct fib4_rule *rule4,
  207. struct netlink_ext_ack *extack)
  208. {
  209. dscp_t dscp_mask;
  210. if (!rule4->dscp_full) {
  211. NL_SET_ERR_MSG_ATTR(extack, nla,
  212. "Cannot specify DSCP mask without DSCP value");
  213. return -EINVAL;
  214. }
  215. dscp_mask = inet_dsfield_to_dscp(nla_get_u8(nla) << 2);
  216. if (rule4->dscp & ~dscp_mask) {
  217. NL_SET_ERR_MSG_ATTR(extack, nla, "Invalid DSCP mask");
  218. return -EINVAL;
  219. }
  220. rule4->dscp_mask = dscp_mask;
  221. return 0;
  222. }
  223. static int fib4_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
  224. struct fib_rule_hdr *frh,
  225. struct nlattr **tb,
  226. struct netlink_ext_ack *extack)
  227. {
  228. struct fib4_rule *rule4 = (struct fib4_rule *)rule;
  229. struct net *net = rule->fr_net;
  230. int err = -EINVAL;
  231. if (tb[FRA_FLOWLABEL] || tb[FRA_FLOWLABEL_MASK]) {
  232. NL_SET_ERR_MSG(extack,
  233. "Flow label cannot be specified for IPv4 FIB rules");
  234. goto errout;
  235. }
  236. if (!inet_validate_dscp(frh->tos)) {
  237. NL_SET_ERR_MSG(extack,
  238. "Invalid dsfield (tos): ECN bits must be 0");
  239. goto errout;
  240. }
  241. /* IPv4 currently doesn't handle high order DSCP bits correctly */
  242. if (frh->tos & ~IPTOS_TOS_MASK) {
  243. NL_SET_ERR_MSG(extack, "Invalid tos");
  244. goto errout;
  245. }
  246. rule4->dscp = inet_dsfield_to_dscp(frh->tos);
  247. if (tb[FRA_DSCP] &&
  248. fib4_nl2rule_dscp(tb[FRA_DSCP], rule4, extack) < 0)
  249. goto errout;
  250. if (tb[FRA_DSCP_MASK] &&
  251. fib4_nl2rule_dscp_mask(tb[FRA_DSCP_MASK], rule4, extack) < 0)
  252. goto errout;
  253. /* split local/main if they are not already split */
  254. err = fib_unmerge(net);
  255. if (err)
  256. goto errout;
  257. if (rule->table == RT_TABLE_UNSPEC && !rule->l3mdev) {
  258. if (rule->action == FR_ACT_TO_TBL) {
  259. struct fib_table *table;
  260. table = fib_empty_table(net);
  261. if (!table) {
  262. err = -ENOBUFS;
  263. goto errout;
  264. }
  265. rule->table = table->tb_id;
  266. }
  267. }
  268. if (frh->src_len)
  269. rule4->src = nla_get_in_addr(tb[FRA_SRC]);
  270. if (frh->dst_len)
  271. rule4->dst = nla_get_in_addr(tb[FRA_DST]);
  272. #ifdef CONFIG_IP_ROUTE_CLASSID
  273. if (tb[FRA_FLOW]) {
  274. rule4->tclassid = nla_get_u32(tb[FRA_FLOW]);
  275. if (rule4->tclassid)
  276. atomic_inc(&net->ipv4.fib_num_tclassid_users);
  277. }
  278. #endif
  279. if (fib_rule_requires_fldissect(rule))
  280. net->ipv4.fib_rules_require_fldissect++;
  281. rule4->src_len = frh->src_len;
  282. rule4->srcmask = inet_make_mask(rule4->src_len);
  283. rule4->dst_len = frh->dst_len;
  284. rule4->dstmask = inet_make_mask(rule4->dst_len);
  285. net->ipv4.fib_has_custom_rules = true;
  286. err = 0;
  287. errout:
  288. return err;
  289. }
  290. static int fib4_rule_delete(struct fib_rule *rule)
  291. {
  292. struct net *net = rule->fr_net;
  293. int err;
  294. /* split local/main if they are not already split */
  295. err = fib_unmerge(net);
  296. if (err)
  297. goto errout;
  298. #ifdef CONFIG_IP_ROUTE_CLASSID
  299. if (((struct fib4_rule *)rule)->tclassid)
  300. atomic_dec(&net->ipv4.fib_num_tclassid_users);
  301. #endif
  302. net->ipv4.fib_has_custom_rules = true;
  303. if (net->ipv4.fib_rules_require_fldissect &&
  304. fib_rule_requires_fldissect(rule))
  305. net->ipv4.fib_rules_require_fldissect--;
  306. errout:
  307. return err;
  308. }
  309. static int fib4_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
  310. struct nlattr **tb)
  311. {
  312. struct fib4_rule *rule4 = (struct fib4_rule *) rule;
  313. if (frh->src_len && (rule4->src_len != frh->src_len))
  314. return 0;
  315. if (frh->dst_len && (rule4->dst_len != frh->dst_len))
  316. return 0;
  317. if (frh->tos &&
  318. (rule4->dscp_full ||
  319. inet_dscp_to_dsfield(rule4->dscp) != frh->tos))
  320. return 0;
  321. if (tb[FRA_DSCP]) {
  322. dscp_t dscp;
  323. dscp = inet_dsfield_to_dscp(nla_get_u8(tb[FRA_DSCP]) << 2);
  324. if (!rule4->dscp_full || rule4->dscp != dscp)
  325. return 0;
  326. }
  327. if (tb[FRA_DSCP_MASK]) {
  328. dscp_t dscp_mask;
  329. dscp_mask = inet_dsfield_to_dscp(nla_get_u8(tb[FRA_DSCP_MASK]) << 2);
  330. if (!rule4->dscp_full || rule4->dscp_mask != dscp_mask)
  331. return 0;
  332. }
  333. #ifdef CONFIG_IP_ROUTE_CLASSID
  334. if (tb[FRA_FLOW] && (rule4->tclassid != nla_get_u32(tb[FRA_FLOW])))
  335. return 0;
  336. #endif
  337. if (frh->src_len && (rule4->src != nla_get_in_addr(tb[FRA_SRC])))
  338. return 0;
  339. if (frh->dst_len && (rule4->dst != nla_get_in_addr(tb[FRA_DST])))
  340. return 0;
  341. return 1;
  342. }
  343. static int fib4_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
  344. struct fib_rule_hdr *frh)
  345. {
  346. struct fib4_rule *rule4 = (struct fib4_rule *) rule;
  347. frh->dst_len = rule4->dst_len;
  348. frh->src_len = rule4->src_len;
  349. if (rule4->dscp_full) {
  350. frh->tos = 0;
  351. if (nla_put_u8(skb, FRA_DSCP,
  352. inet_dscp_to_dsfield(rule4->dscp) >> 2) ||
  353. nla_put_u8(skb, FRA_DSCP_MASK,
  354. inet_dscp_to_dsfield(rule4->dscp_mask) >> 2))
  355. goto nla_put_failure;
  356. } else {
  357. frh->tos = inet_dscp_to_dsfield(rule4->dscp);
  358. }
  359. if ((rule4->dst_len &&
  360. nla_put_in_addr(skb, FRA_DST, rule4->dst)) ||
  361. (rule4->src_len &&
  362. nla_put_in_addr(skb, FRA_SRC, rule4->src)))
  363. goto nla_put_failure;
  364. #ifdef CONFIG_IP_ROUTE_CLASSID
  365. if (rule4->tclassid &&
  366. nla_put_u32(skb, FRA_FLOW, rule4->tclassid))
  367. goto nla_put_failure;
  368. #endif
  369. return 0;
  370. nla_put_failure:
  371. return -ENOBUFS;
  372. }
  373. static size_t fib4_rule_nlmsg_payload(struct fib_rule *rule)
  374. {
  375. return nla_total_size(4) /* dst */
  376. + nla_total_size(4) /* src */
  377. + nla_total_size(4) /* flow */
  378. + nla_total_size(1) /* dscp */
  379. + nla_total_size(1); /* dscp mask */
  380. }
  381. static void fib4_rule_flush_cache(struct fib_rules_ops *ops)
  382. {
  383. rt_cache_flush(ops->fro_net);
  384. }
  385. static const struct fib_rules_ops __net_initconst fib4_rules_ops_template = {
  386. .family = AF_INET,
  387. .rule_size = sizeof(struct fib4_rule),
  388. .addr_size = sizeof(u32),
  389. .action = fib4_rule_action,
  390. .suppress = fib4_rule_suppress,
  391. .match = fib4_rule_match,
  392. .configure = fib4_rule_configure,
  393. .delete = fib4_rule_delete,
  394. .compare = fib4_rule_compare,
  395. .fill = fib4_rule_fill,
  396. .nlmsg_payload = fib4_rule_nlmsg_payload,
  397. .flush_cache = fib4_rule_flush_cache,
  398. .nlgroup = RTNLGRP_IPV4_RULE,
  399. .owner = THIS_MODULE,
  400. };
  401. static int fib_default_rules_init(struct fib_rules_ops *ops)
  402. {
  403. int err;
  404. err = fib_default_rule_add(ops, 0, RT_TABLE_LOCAL);
  405. if (err < 0)
  406. return err;
  407. err = fib_default_rule_add(ops, 0x7FFE, RT_TABLE_MAIN);
  408. if (err < 0)
  409. return err;
  410. err = fib_default_rule_add(ops, 0x7FFF, RT_TABLE_DEFAULT);
  411. if (err < 0)
  412. return err;
  413. return 0;
  414. }
  415. int __net_init fib4_rules_init(struct net *net)
  416. {
  417. int err;
  418. struct fib_rules_ops *ops;
  419. ops = fib_rules_register(&fib4_rules_ops_template, net);
  420. if (IS_ERR(ops))
  421. return PTR_ERR(ops);
  422. err = fib_default_rules_init(ops);
  423. if (err < 0)
  424. goto fail;
  425. net->ipv4.rules_ops = ops;
  426. net->ipv4.fib_has_custom_rules = false;
  427. net->ipv4.fib_rules_require_fldissect = 0;
  428. return 0;
  429. fail:
  430. /* also cleans all rules already added */
  431. fib_rules_unregister(ops);
  432. return err;
  433. }
  434. void __net_exit fib4_rules_exit(struct net *net)
  435. {
  436. fib_rules_unregister(net->ipv4.rules_ops);
  437. }