fib6_rules.c 7.7 KB

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