fib6_rules.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * net/ipv6/fib6_rules.c IPv6 Routing Policy Rules
  4. *
  5. * Copyright (C)2003-2006 Helsinki University of Technology
  6. * Copyright (C)2003-2006 USAGI/WIDE Project
  7. *
  8. * Authors
  9. * Thomas Graf <[email protected]>
  10. * Ville Nuorvala <[email protected]>
  11. */
  12. #include <linux/netdevice.h>
  13. #include <linux/notifier.h>
  14. #include <linux/export.h>
  15. #include <linux/indirect_call_wrapper.h>
  16. #include <net/fib_rules.h>
  17. #include <net/inet_dscp.h>
  18. #include <net/ipv6.h>
  19. #include <net/addrconf.h>
  20. #include <net/ip6_route.h>
  21. #include <net/netlink.h>
  22. struct fib6_rule {
  23. struct fib_rule common;
  24. struct rt6key src;
  25. struct rt6key dst;
  26. dscp_t dscp;
  27. };
  28. static bool fib6_rule_matchall(const struct fib_rule *rule)
  29. {
  30. struct fib6_rule *r = container_of(rule, struct fib6_rule, common);
  31. if (r->dst.plen || r->src.plen || r->dscp)
  32. return false;
  33. return fib_rule_matchall(rule);
  34. }
  35. bool fib6_rule_default(const struct fib_rule *rule)
  36. {
  37. if (!fib6_rule_matchall(rule) || rule->action != FR_ACT_TO_TBL ||
  38. rule->l3mdev)
  39. return false;
  40. if (rule->table != RT6_TABLE_LOCAL && rule->table != RT6_TABLE_MAIN)
  41. return false;
  42. return true;
  43. }
  44. EXPORT_SYMBOL_GPL(fib6_rule_default);
  45. int fib6_rules_dump(struct net *net, struct notifier_block *nb,
  46. struct netlink_ext_ack *extack)
  47. {
  48. return fib_rules_dump(net, nb, AF_INET6, extack);
  49. }
  50. unsigned int fib6_rules_seq_read(struct net *net)
  51. {
  52. return fib_rules_seq_read(net, AF_INET6);
  53. }
  54. /* called with rcu lock held; no reference taken on fib6_info */
  55. int fib6_lookup(struct net *net, int oif, struct flowi6 *fl6,
  56. struct fib6_result *res, int flags)
  57. {
  58. int err;
  59. if (net->ipv6.fib6_has_custom_rules) {
  60. struct fib_lookup_arg arg = {
  61. .lookup_ptr = fib6_table_lookup,
  62. .lookup_data = &oif,
  63. .result = res,
  64. .flags = FIB_LOOKUP_NOREF,
  65. };
  66. l3mdev_update_flow(net, flowi6_to_flowi(fl6));
  67. err = fib_rules_lookup(net->ipv6.fib6_rules_ops,
  68. flowi6_to_flowi(fl6), flags, &arg);
  69. } else {
  70. err = fib6_table_lookup(net, net->ipv6.fib6_local_tbl, oif,
  71. fl6, res, flags);
  72. if (err || res->f6i == net->ipv6.fib6_null_entry)
  73. err = fib6_table_lookup(net, net->ipv6.fib6_main_tbl,
  74. oif, fl6, res, flags);
  75. }
  76. return err;
  77. }
  78. struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
  79. const struct sk_buff *skb,
  80. int flags, pol_lookup_t lookup)
  81. {
  82. if (net->ipv6.fib6_has_custom_rules) {
  83. struct fib6_result res = {};
  84. struct fib_lookup_arg arg = {
  85. .lookup_ptr = lookup,
  86. .lookup_data = skb,
  87. .result = &res,
  88. .flags = FIB_LOOKUP_NOREF,
  89. };
  90. /* update flow if oif or iif point to device enslaved to l3mdev */
  91. l3mdev_update_flow(net, flowi6_to_flowi(fl6));
  92. fib_rules_lookup(net->ipv6.fib6_rules_ops,
  93. flowi6_to_flowi(fl6), flags, &arg);
  94. if (res.rt6)
  95. return &res.rt6->dst;
  96. } else {
  97. struct rt6_info *rt;
  98. rt = pol_lookup_func(lookup,
  99. net, net->ipv6.fib6_local_tbl, fl6, skb, flags);
  100. if (rt != net->ipv6.ip6_null_entry && rt->dst.error != -EAGAIN)
  101. return &rt->dst;
  102. ip6_rt_put_flags(rt, flags);
  103. rt = pol_lookup_func(lookup,
  104. net, net->ipv6.fib6_main_tbl, fl6, skb, flags);
  105. if (rt->dst.error != -EAGAIN)
  106. return &rt->dst;
  107. ip6_rt_put_flags(rt, flags);
  108. }
  109. if (!(flags & RT6_LOOKUP_F_DST_NOREF))
  110. dst_hold(&net->ipv6.ip6_null_entry->dst);
  111. return &net->ipv6.ip6_null_entry->dst;
  112. }
  113. static int fib6_rule_saddr(struct net *net, struct fib_rule *rule, int flags,
  114. struct flowi6 *flp6, const struct net_device *dev)
  115. {
  116. struct fib6_rule *r = (struct fib6_rule *)rule;
  117. /* If we need to find a source address for this traffic,
  118. * we check the result if it meets requirement of the rule.
  119. */
  120. if ((rule->flags & FIB_RULE_FIND_SADDR) &&
  121. r->src.plen && !(flags & RT6_LOOKUP_F_HAS_SADDR)) {
  122. struct in6_addr saddr;
  123. if (ipv6_dev_get_saddr(net, dev, &flp6->daddr,
  124. rt6_flags2srcprefs(flags), &saddr))
  125. return -EAGAIN;
  126. if (!ipv6_prefix_equal(&saddr, &r->src.addr, r->src.plen))
  127. return -EAGAIN;
  128. flp6->saddr = saddr;
  129. }
  130. return 0;
  131. }
  132. static int fib6_rule_action_alt(struct fib_rule *rule, struct flowi *flp,
  133. int flags, struct fib_lookup_arg *arg)
  134. {
  135. struct fib6_result *res = arg->result;
  136. struct flowi6 *flp6 = &flp->u.ip6;
  137. struct net *net = rule->fr_net;
  138. struct fib6_table *table;
  139. int err, *oif;
  140. u32 tb_id;
  141. switch (rule->action) {
  142. case FR_ACT_TO_TBL:
  143. break;
  144. case FR_ACT_UNREACHABLE:
  145. return -ENETUNREACH;
  146. case FR_ACT_PROHIBIT:
  147. return -EACCES;
  148. case FR_ACT_BLACKHOLE:
  149. default:
  150. return -EINVAL;
  151. }
  152. tb_id = fib_rule_get_table(rule, arg);
  153. table = fib6_get_table(net, tb_id);
  154. if (!table)
  155. return -EAGAIN;
  156. oif = (int *)arg->lookup_data;
  157. err = fib6_table_lookup(net, table, *oif, flp6, res, flags);
  158. if (!err && res->f6i != net->ipv6.fib6_null_entry)
  159. err = fib6_rule_saddr(net, rule, flags, flp6,
  160. res->nh->fib_nh_dev);
  161. else
  162. err = -EAGAIN;
  163. return err;
  164. }
  165. static int __fib6_rule_action(struct fib_rule *rule, struct flowi *flp,
  166. int flags, struct fib_lookup_arg *arg)
  167. {
  168. struct fib6_result *res = arg->result;
  169. struct flowi6 *flp6 = &flp->u.ip6;
  170. struct rt6_info *rt = NULL;
  171. struct fib6_table *table;
  172. struct net *net = rule->fr_net;
  173. pol_lookup_t lookup = arg->lookup_ptr;
  174. int err = 0;
  175. u32 tb_id;
  176. switch (rule->action) {
  177. case FR_ACT_TO_TBL:
  178. break;
  179. case FR_ACT_UNREACHABLE:
  180. err = -ENETUNREACH;
  181. rt = net->ipv6.ip6_null_entry;
  182. goto discard_pkt;
  183. default:
  184. case FR_ACT_BLACKHOLE:
  185. err = -EINVAL;
  186. rt = net->ipv6.ip6_blk_hole_entry;
  187. goto discard_pkt;
  188. case FR_ACT_PROHIBIT:
  189. err = -EACCES;
  190. rt = net->ipv6.ip6_prohibit_entry;
  191. goto discard_pkt;
  192. }
  193. tb_id = fib_rule_get_table(rule, arg);
  194. table = fib6_get_table(net, tb_id);
  195. if (!table) {
  196. err = -EAGAIN;
  197. goto out;
  198. }
  199. rt = pol_lookup_func(lookup,
  200. net, table, flp6, arg->lookup_data, flags);
  201. if (rt != net->ipv6.ip6_null_entry) {
  202. err = fib6_rule_saddr(net, rule, flags, flp6,
  203. ip6_dst_idev(&rt->dst)->dev);
  204. if (err == -EAGAIN)
  205. goto again;
  206. err = rt->dst.error;
  207. if (err != -EAGAIN)
  208. goto out;
  209. }
  210. again:
  211. ip6_rt_put_flags(rt, flags);
  212. err = -EAGAIN;
  213. rt = NULL;
  214. goto out;
  215. discard_pkt:
  216. if (!(flags & RT6_LOOKUP_F_DST_NOREF))
  217. dst_hold(&rt->dst);
  218. out:
  219. res->rt6 = rt;
  220. return err;
  221. }
  222. INDIRECT_CALLABLE_SCOPE int fib6_rule_action(struct fib_rule *rule,
  223. struct flowi *flp, int flags,
  224. struct fib_lookup_arg *arg)
  225. {
  226. if (arg->lookup_ptr == fib6_table_lookup)
  227. return fib6_rule_action_alt(rule, flp, flags, arg);
  228. return __fib6_rule_action(rule, flp, flags, arg);
  229. }
  230. INDIRECT_CALLABLE_SCOPE bool fib6_rule_suppress(struct fib_rule *rule,
  231. int flags,
  232. struct fib_lookup_arg *arg)
  233. {
  234. struct fib6_result *res = arg->result;
  235. struct rt6_info *rt = res->rt6;
  236. struct net_device *dev = NULL;
  237. if (!rt)
  238. return false;
  239. if (rt->rt6i_idev)
  240. dev = rt->rt6i_idev->dev;
  241. /* do not accept result if the route does
  242. * not meet the required prefix length
  243. */
  244. if (rt->rt6i_dst.plen <= rule->suppress_prefixlen)
  245. goto suppress_route;
  246. /* do not accept result if the route uses a device
  247. * belonging to a forbidden interface group
  248. */
  249. if (rule->suppress_ifgroup != -1 && dev && dev->group == rule->suppress_ifgroup)
  250. goto suppress_route;
  251. return false;
  252. suppress_route:
  253. ip6_rt_put_flags(rt, flags);
  254. return true;
  255. }
  256. INDIRECT_CALLABLE_SCOPE int fib6_rule_match(struct fib_rule *rule,
  257. struct flowi *fl, int flags)
  258. {
  259. struct fib6_rule *r = (struct fib6_rule *) rule;
  260. struct flowi6 *fl6 = &fl->u.ip6;
  261. if (r->dst.plen &&
  262. !ipv6_prefix_equal(&fl6->daddr, &r->dst.addr, r->dst.plen))
  263. return 0;
  264. /*
  265. * If FIB_RULE_FIND_SADDR is set and we do not have a
  266. * source address for the traffic, we defer check for
  267. * source address.
  268. */
  269. if (r->src.plen) {
  270. if (flags & RT6_LOOKUP_F_HAS_SADDR) {
  271. if (!ipv6_prefix_equal(&fl6->saddr, &r->src.addr,
  272. r->src.plen))
  273. return 0;
  274. } else if (!(r->common.flags & FIB_RULE_FIND_SADDR))
  275. return 0;
  276. }
  277. if (r->dscp && r->dscp != ip6_dscp(fl6->flowlabel))
  278. return 0;
  279. if (rule->ip_proto && (rule->ip_proto != fl6->flowi6_proto))
  280. return 0;
  281. if (fib_rule_port_range_set(&rule->sport_range) &&
  282. !fib_rule_port_inrange(&rule->sport_range, fl6->fl6_sport))
  283. return 0;
  284. if (fib_rule_port_range_set(&rule->dport_range) &&
  285. !fib_rule_port_inrange(&rule->dport_range, fl6->fl6_dport))
  286. return 0;
  287. return 1;
  288. }
  289. static int fib6_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
  290. struct fib_rule_hdr *frh,
  291. struct nlattr **tb,
  292. struct netlink_ext_ack *extack)
  293. {
  294. int err = -EINVAL;
  295. struct net *net = sock_net(skb->sk);
  296. struct fib6_rule *rule6 = (struct fib6_rule *) rule;
  297. if (!inet_validate_dscp(frh->tos)) {
  298. NL_SET_ERR_MSG(extack,
  299. "Invalid dsfield (tos): ECN bits must be 0");
  300. goto errout;
  301. }
  302. rule6->dscp = inet_dsfield_to_dscp(frh->tos);
  303. if (rule->action == FR_ACT_TO_TBL && !rule->l3mdev) {
  304. if (rule->table == RT6_TABLE_UNSPEC) {
  305. NL_SET_ERR_MSG(extack, "Invalid table");
  306. goto errout;
  307. }
  308. if (fib6_new_table(net, rule->table) == NULL) {
  309. err = -ENOBUFS;
  310. goto errout;
  311. }
  312. }
  313. if (frh->src_len)
  314. rule6->src.addr = nla_get_in6_addr(tb[FRA_SRC]);
  315. if (frh->dst_len)
  316. rule6->dst.addr = nla_get_in6_addr(tb[FRA_DST]);
  317. rule6->src.plen = frh->src_len;
  318. rule6->dst.plen = frh->dst_len;
  319. if (fib_rule_requires_fldissect(rule))
  320. net->ipv6.fib6_rules_require_fldissect++;
  321. net->ipv6.fib6_has_custom_rules = true;
  322. err = 0;
  323. errout:
  324. return err;
  325. }
  326. static int fib6_rule_delete(struct fib_rule *rule)
  327. {
  328. struct net *net = rule->fr_net;
  329. if (net->ipv6.fib6_rules_require_fldissect &&
  330. fib_rule_requires_fldissect(rule))
  331. net->ipv6.fib6_rules_require_fldissect--;
  332. return 0;
  333. }
  334. static int fib6_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
  335. struct nlattr **tb)
  336. {
  337. struct fib6_rule *rule6 = (struct fib6_rule *) rule;
  338. if (frh->src_len && (rule6->src.plen != frh->src_len))
  339. return 0;
  340. if (frh->dst_len && (rule6->dst.plen != frh->dst_len))
  341. return 0;
  342. if (frh->tos && inet_dscp_to_dsfield(rule6->dscp) != frh->tos)
  343. return 0;
  344. if (frh->src_len &&
  345. nla_memcmp(tb[FRA_SRC], &rule6->src.addr, sizeof(struct in6_addr)))
  346. return 0;
  347. if (frh->dst_len &&
  348. nla_memcmp(tb[FRA_DST], &rule6->dst.addr, sizeof(struct in6_addr)))
  349. return 0;
  350. return 1;
  351. }
  352. static int fib6_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
  353. struct fib_rule_hdr *frh)
  354. {
  355. struct fib6_rule *rule6 = (struct fib6_rule *) rule;
  356. frh->dst_len = rule6->dst.plen;
  357. frh->src_len = rule6->src.plen;
  358. frh->tos = inet_dscp_to_dsfield(rule6->dscp);
  359. if ((rule6->dst.plen &&
  360. nla_put_in6_addr(skb, FRA_DST, &rule6->dst.addr)) ||
  361. (rule6->src.plen &&
  362. nla_put_in6_addr(skb, FRA_SRC, &rule6->src.addr)))
  363. goto nla_put_failure;
  364. return 0;
  365. nla_put_failure:
  366. return -ENOBUFS;
  367. }
  368. static size_t fib6_rule_nlmsg_payload(struct fib_rule *rule)
  369. {
  370. return nla_total_size(16) /* dst */
  371. + nla_total_size(16); /* src */
  372. }
  373. static const struct fib_rules_ops __net_initconst fib6_rules_ops_template = {
  374. .family = AF_INET6,
  375. .rule_size = sizeof(struct fib6_rule),
  376. .addr_size = sizeof(struct in6_addr),
  377. .action = fib6_rule_action,
  378. .match = fib6_rule_match,
  379. .suppress = fib6_rule_suppress,
  380. .configure = fib6_rule_configure,
  381. .delete = fib6_rule_delete,
  382. .compare = fib6_rule_compare,
  383. .fill = fib6_rule_fill,
  384. .nlmsg_payload = fib6_rule_nlmsg_payload,
  385. .nlgroup = RTNLGRP_IPV6_RULE,
  386. .owner = THIS_MODULE,
  387. .fro_net = &init_net,
  388. };
  389. static int __net_init fib6_rules_net_init(struct net *net)
  390. {
  391. struct fib_rules_ops *ops;
  392. int err;
  393. ops = fib_rules_register(&fib6_rules_ops_template, net);
  394. if (IS_ERR(ops))
  395. return PTR_ERR(ops);
  396. err = fib_default_rule_add(ops, 0, RT6_TABLE_LOCAL, 0);
  397. if (err)
  398. goto out_fib6_rules_ops;
  399. err = fib_default_rule_add(ops, 0x7FFE, RT6_TABLE_MAIN, 0);
  400. if (err)
  401. goto out_fib6_rules_ops;
  402. net->ipv6.fib6_rules_ops = ops;
  403. net->ipv6.fib6_rules_require_fldissect = 0;
  404. out:
  405. return err;
  406. out_fib6_rules_ops:
  407. fib_rules_unregister(ops);
  408. goto out;
  409. }
  410. static void __net_exit fib6_rules_net_exit_batch(struct list_head *net_list)
  411. {
  412. struct net *net;
  413. rtnl_lock();
  414. list_for_each_entry(net, net_list, exit_list) {
  415. fib_rules_unregister(net->ipv6.fib6_rules_ops);
  416. cond_resched();
  417. }
  418. rtnl_unlock();
  419. }
  420. static struct pernet_operations fib6_rules_net_ops = {
  421. .init = fib6_rules_net_init,
  422. .exit_batch = fib6_rules_net_exit_batch,
  423. };
  424. int __init fib6_rules_init(void)
  425. {
  426. return register_pernet_subsys(&fib6_rules_net_ops);
  427. }
  428. void fib6_rules_cleanup(void)
  429. {
  430. unregister_pernet_subsys(&fib6_rules_net_ops);
  431. }