fib6_rules.c 12 KB

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