fib6_rules.c 12 KB

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