fib6_rules.c 7.7 KB

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