fib6_rules.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
  55. int flags, pol_lookup_t lookup)
  56. {
  57. if (net->ipv6.fib6_has_custom_rules) {
  58. struct fib_lookup_arg arg = {
  59. .lookup_ptr = lookup,
  60. .flags = FIB_LOOKUP_NOREF,
  61. };
  62. /* update flow if oif or iif point to device enslaved to l3mdev */
  63. l3mdev_update_flow(net, flowi6_to_flowi(fl6));
  64. fib_rules_lookup(net->ipv6.fib6_rules_ops,
  65. flowi6_to_flowi(fl6), flags, &arg);
  66. if (arg.result)
  67. return arg.result;
  68. } else {
  69. struct rt6_info *rt;
  70. rt = lookup(net, net->ipv6.fib6_local_tbl, fl6, flags);
  71. if (rt != net->ipv6.ip6_null_entry && rt->dst.error != -EAGAIN)
  72. return &rt->dst;
  73. ip6_rt_put(rt);
  74. rt = lookup(net, net->ipv6.fib6_main_tbl, fl6, flags);
  75. if (rt->dst.error != -EAGAIN)
  76. return &rt->dst;
  77. ip6_rt_put(rt);
  78. }
  79. dst_hold(&net->ipv6.ip6_null_entry->dst);
  80. return &net->ipv6.ip6_null_entry->dst;
  81. }
  82. static int fib6_rule_action(struct fib_rule *rule, struct flowi *flp,
  83. int flags, struct fib_lookup_arg *arg)
  84. {
  85. struct flowi6 *flp6 = &flp->u.ip6;
  86. struct rt6_info *rt = NULL;
  87. struct fib6_table *table;
  88. struct net *net = rule->fr_net;
  89. pol_lookup_t lookup = arg->lookup_ptr;
  90. int err = 0;
  91. u32 tb_id;
  92. switch (rule->action) {
  93. case FR_ACT_TO_TBL:
  94. break;
  95. case FR_ACT_UNREACHABLE:
  96. err = -ENETUNREACH;
  97. rt = net->ipv6.ip6_null_entry;
  98. goto discard_pkt;
  99. default:
  100. case FR_ACT_BLACKHOLE:
  101. err = -EINVAL;
  102. rt = net->ipv6.ip6_blk_hole_entry;
  103. goto discard_pkt;
  104. case FR_ACT_PROHIBIT:
  105. err = -EACCES;
  106. rt = net->ipv6.ip6_prohibit_entry;
  107. goto discard_pkt;
  108. }
  109. tb_id = fib_rule_get_table(rule, arg);
  110. table = fib6_get_table(net, tb_id);
  111. if (!table) {
  112. err = -EAGAIN;
  113. goto out;
  114. }
  115. rt = lookup(net, table, flp6, flags);
  116. if (rt != net->ipv6.ip6_null_entry) {
  117. struct fib6_rule *r = (struct fib6_rule *)rule;
  118. /*
  119. * If we need to find a source address for this traffic,
  120. * we check the result if it meets requirement of the rule.
  121. */
  122. if ((rule->flags & FIB_RULE_FIND_SADDR) &&
  123. r->src.plen && !(flags & RT6_LOOKUP_F_HAS_SADDR)) {
  124. struct in6_addr saddr;
  125. if (ipv6_dev_get_saddr(net,
  126. ip6_dst_idev(&rt->dst)->dev,
  127. &flp6->daddr,
  128. rt6_flags2srcprefs(flags),
  129. &saddr))
  130. goto again;
  131. if (!ipv6_prefix_equal(&saddr, &r->src.addr,
  132. r->src.plen))
  133. goto again;
  134. flp6->saddr = saddr;
  135. }
  136. err = rt->dst.error;
  137. if (err != -EAGAIN)
  138. goto out;
  139. }
  140. again:
  141. ip6_rt_put(rt);
  142. err = -EAGAIN;
  143. rt = NULL;
  144. goto out;
  145. discard_pkt:
  146. dst_hold(&rt->dst);
  147. out:
  148. arg->result = rt;
  149. return err;
  150. }
  151. static bool fib6_rule_suppress(struct fib_rule *rule, struct fib_lookup_arg *arg)
  152. {
  153. struct rt6_info *rt = (struct rt6_info *) arg->result;
  154. struct net_device *dev = NULL;
  155. if (rt->rt6i_idev)
  156. dev = rt->rt6i_idev->dev;
  157. /* do not accept result if the route does
  158. * not meet the required prefix length
  159. */
  160. if (rt->rt6i_dst.plen <= rule->suppress_prefixlen)
  161. goto suppress_route;
  162. /* do not accept result if the route uses a device
  163. * belonging to a forbidden interface group
  164. */
  165. if (rule->suppress_ifgroup != -1 && dev && dev->group == rule->suppress_ifgroup)
  166. goto suppress_route;
  167. return false;
  168. suppress_route:
  169. ip6_rt_put(rt);
  170. return true;
  171. }
  172. static int fib6_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
  173. {
  174. struct fib6_rule *r = (struct fib6_rule *) rule;
  175. struct flowi6 *fl6 = &fl->u.ip6;
  176. if (r->dst.plen &&
  177. !ipv6_prefix_equal(&fl6->daddr, &r->dst.addr, r->dst.plen))
  178. return 0;
  179. /*
  180. * If FIB_RULE_FIND_SADDR is set and we do not have a
  181. * source address for the traffic, we defer check for
  182. * source address.
  183. */
  184. if (r->src.plen) {
  185. if (flags & RT6_LOOKUP_F_HAS_SADDR) {
  186. if (!ipv6_prefix_equal(&fl6->saddr, &r->src.addr,
  187. r->src.plen))
  188. return 0;
  189. } else if (!(r->common.flags & FIB_RULE_FIND_SADDR))
  190. return 0;
  191. }
  192. if (r->tclass && r->tclass != ip6_tclass(fl6->flowlabel))
  193. return 0;
  194. return 1;
  195. }
  196. static const struct nla_policy fib6_rule_policy[FRA_MAX+1] = {
  197. FRA_GENERIC_POLICY,
  198. };
  199. static int fib6_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
  200. struct fib_rule_hdr *frh,
  201. struct nlattr **tb)
  202. {
  203. int err = -EINVAL;
  204. struct net *net = sock_net(skb->sk);
  205. struct fib6_rule *rule6 = (struct fib6_rule *) rule;
  206. if (rule->action == FR_ACT_TO_TBL && !rule->l3mdev) {
  207. if (rule->table == RT6_TABLE_UNSPEC)
  208. goto errout;
  209. if (fib6_new_table(net, rule->table) == NULL) {
  210. err = -ENOBUFS;
  211. goto errout;
  212. }
  213. }
  214. if (frh->src_len)
  215. rule6->src.addr = nla_get_in6_addr(tb[FRA_SRC]);
  216. if (frh->dst_len)
  217. rule6->dst.addr = nla_get_in6_addr(tb[FRA_DST]);
  218. rule6->src.plen = frh->src_len;
  219. rule6->dst.plen = frh->dst_len;
  220. rule6->tclass = frh->tos;
  221. net->ipv6.fib6_has_custom_rules = true;
  222. err = 0;
  223. errout:
  224. return err;
  225. }
  226. static int fib6_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
  227. struct nlattr **tb)
  228. {
  229. struct fib6_rule *rule6 = (struct fib6_rule *) rule;
  230. if (frh->src_len && (rule6->src.plen != frh->src_len))
  231. return 0;
  232. if (frh->dst_len && (rule6->dst.plen != frh->dst_len))
  233. return 0;
  234. if (frh->tos && (rule6->tclass != frh->tos))
  235. return 0;
  236. if (frh->src_len &&
  237. nla_memcmp(tb[FRA_SRC], &rule6->src.addr, sizeof(struct in6_addr)))
  238. return 0;
  239. if (frh->dst_len &&
  240. nla_memcmp(tb[FRA_DST], &rule6->dst.addr, sizeof(struct in6_addr)))
  241. return 0;
  242. return 1;
  243. }
  244. static int fib6_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
  245. struct fib_rule_hdr *frh)
  246. {
  247. struct fib6_rule *rule6 = (struct fib6_rule *) rule;
  248. frh->dst_len = rule6->dst.plen;
  249. frh->src_len = rule6->src.plen;
  250. frh->tos = rule6->tclass;
  251. if ((rule6->dst.plen &&
  252. nla_put_in6_addr(skb, FRA_DST, &rule6->dst.addr)) ||
  253. (rule6->src.plen &&
  254. nla_put_in6_addr(skb, FRA_SRC, &rule6->src.addr)))
  255. goto nla_put_failure;
  256. return 0;
  257. nla_put_failure:
  258. return -ENOBUFS;
  259. }
  260. static size_t fib6_rule_nlmsg_payload(struct fib_rule *rule)
  261. {
  262. return nla_total_size(16) /* dst */
  263. + nla_total_size(16); /* src */
  264. }
  265. static const struct fib_rules_ops __net_initconst fib6_rules_ops_template = {
  266. .family = AF_INET6,
  267. .rule_size = sizeof(struct fib6_rule),
  268. .addr_size = sizeof(struct in6_addr),
  269. .action = fib6_rule_action,
  270. .match = fib6_rule_match,
  271. .suppress = fib6_rule_suppress,
  272. .configure = fib6_rule_configure,
  273. .compare = fib6_rule_compare,
  274. .fill = fib6_rule_fill,
  275. .nlmsg_payload = fib6_rule_nlmsg_payload,
  276. .nlgroup = RTNLGRP_IPV6_RULE,
  277. .policy = fib6_rule_policy,
  278. .owner = THIS_MODULE,
  279. .fro_net = &init_net,
  280. };
  281. static int __net_init fib6_rules_net_init(struct net *net)
  282. {
  283. struct fib_rules_ops *ops;
  284. int err = -ENOMEM;
  285. ops = fib_rules_register(&fib6_rules_ops_template, net);
  286. if (IS_ERR(ops))
  287. return PTR_ERR(ops);
  288. err = fib_default_rule_add(ops, 0, RT6_TABLE_LOCAL, 0);
  289. if (err)
  290. goto out_fib6_rules_ops;
  291. err = fib_default_rule_add(ops, 0x7FFE, RT6_TABLE_MAIN, 0);
  292. if (err)
  293. goto out_fib6_rules_ops;
  294. net->ipv6.fib6_rules_ops = ops;
  295. out:
  296. return err;
  297. out_fib6_rules_ops:
  298. fib_rules_unregister(ops);
  299. goto out;
  300. }
  301. static void __net_exit fib6_rules_net_exit(struct net *net)
  302. {
  303. rtnl_lock();
  304. fib_rules_unregister(net->ipv6.fib6_rules_ops);
  305. rtnl_unlock();
  306. }
  307. static struct pernet_operations fib6_rules_net_ops = {
  308. .init = fib6_rules_net_init,
  309. .exit = fib6_rules_net_exit,
  310. };
  311. int __init fib6_rules_init(void)
  312. {
  313. return register_pernet_subsys(&fib6_rules_net_ops);
  314. }
  315. void fib6_rules_cleanup(void)
  316. {
  317. unregister_pernet_subsys(&fib6_rules_net_ops);
  318. }