fib6_rules.c 7.8 KB

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