fib6_rules.c 7.7 KB

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