fib6_rules.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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 <net/fib_rules.h>
  17. #include <net/ipv6.h>
  18. #include <net/addrconf.h>
  19. #include <net/ip6_route.h>
  20. #include <net/netlink.h>
  21. struct fib6_rule
  22. {
  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. switch (rule->action) {
  51. case FR_ACT_TO_TBL:
  52. break;
  53. case FR_ACT_UNREACHABLE:
  54. rt = net->ipv6.ip6_null_entry;
  55. goto discard_pkt;
  56. default:
  57. case FR_ACT_BLACKHOLE:
  58. rt = net->ipv6.ip6_blk_hole_entry;
  59. goto discard_pkt;
  60. case FR_ACT_PROHIBIT:
  61. rt = net->ipv6.ip6_prohibit_entry;
  62. goto discard_pkt;
  63. }
  64. table = fib6_get_table(net, rule->table);
  65. if (table)
  66. rt = lookup(net, table, flp6, flags);
  67. if (rt != net->ipv6.ip6_null_entry) {
  68. struct fib6_rule *r = (struct fib6_rule *)rule;
  69. /*
  70. * If we need to find a source address for this traffic,
  71. * we check the result if it meets requirement of the rule.
  72. */
  73. if ((rule->flags & FIB_RULE_FIND_SADDR) &&
  74. r->src.plen && !(flags & RT6_LOOKUP_F_HAS_SADDR)) {
  75. struct in6_addr saddr;
  76. if (ipv6_dev_get_saddr(net,
  77. ip6_dst_idev(&rt->dst)->dev,
  78. &flp6->daddr,
  79. rt6_flags2srcprefs(flags),
  80. &saddr))
  81. goto again;
  82. if (!ipv6_prefix_equal(&saddr, &r->src.addr,
  83. r->src.plen))
  84. goto again;
  85. ipv6_addr_copy(&flp6->saddr, &saddr);
  86. }
  87. goto out;
  88. }
  89. again:
  90. dst_release(&rt->dst);
  91. rt = NULL;
  92. goto out;
  93. discard_pkt:
  94. dst_hold(&rt->dst);
  95. out:
  96. arg->result = rt;
  97. return rt == NULL ? -EAGAIN : 0;
  98. }
  99. static int fib6_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
  100. {
  101. struct fib6_rule *r = (struct fib6_rule *) rule;
  102. struct flowi6 *fl6 = &fl->u.ip6;
  103. if (r->dst.plen &&
  104. !ipv6_prefix_equal(&fl6->daddr, &r->dst.addr, r->dst.plen))
  105. return 0;
  106. /*
  107. * If FIB_RULE_FIND_SADDR is set and we do not have a
  108. * source address for the traffic, we defer check for
  109. * source address.
  110. */
  111. if (r->src.plen) {
  112. if (flags & RT6_LOOKUP_F_HAS_SADDR) {
  113. if (!ipv6_prefix_equal(&fl6->saddr, &r->src.addr,
  114. r->src.plen))
  115. return 0;
  116. } else if (!(r->common.flags & FIB_RULE_FIND_SADDR))
  117. return 0;
  118. }
  119. if (r->tclass && r->tclass != ((ntohl(fl6->flowlabel) >> 20) & 0xff))
  120. return 0;
  121. return 1;
  122. }
  123. static const struct nla_policy fib6_rule_policy[FRA_MAX+1] = {
  124. FRA_GENERIC_POLICY,
  125. };
  126. static int fib6_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
  127. struct fib_rule_hdr *frh,
  128. struct nlattr **tb)
  129. {
  130. int err = -EINVAL;
  131. struct net *net = sock_net(skb->sk);
  132. struct fib6_rule *rule6 = (struct fib6_rule *) rule;
  133. if (rule->action == FR_ACT_TO_TBL) {
  134. if (rule->table == RT6_TABLE_UNSPEC)
  135. goto errout;
  136. if (fib6_new_table(net, rule->table) == NULL) {
  137. err = -ENOBUFS;
  138. goto errout;
  139. }
  140. }
  141. if (frh->src_len)
  142. nla_memcpy(&rule6->src.addr, tb[FRA_SRC],
  143. sizeof(struct in6_addr));
  144. if (frh->dst_len)
  145. nla_memcpy(&rule6->dst.addr, tb[FRA_DST],
  146. sizeof(struct in6_addr));
  147. rule6->src.plen = frh->src_len;
  148. rule6->dst.plen = frh->dst_len;
  149. rule6->tclass = frh->tos;
  150. err = 0;
  151. errout:
  152. return err;
  153. }
  154. static int fib6_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
  155. struct nlattr **tb)
  156. {
  157. struct fib6_rule *rule6 = (struct fib6_rule *) rule;
  158. if (frh->src_len && (rule6->src.plen != frh->src_len))
  159. return 0;
  160. if (frh->dst_len && (rule6->dst.plen != frh->dst_len))
  161. return 0;
  162. if (frh->tos && (rule6->tclass != frh->tos))
  163. return 0;
  164. if (frh->src_len &&
  165. nla_memcmp(tb[FRA_SRC], &rule6->src.addr, sizeof(struct in6_addr)))
  166. return 0;
  167. if (frh->dst_len &&
  168. nla_memcmp(tb[FRA_DST], &rule6->dst.addr, sizeof(struct in6_addr)))
  169. return 0;
  170. return 1;
  171. }
  172. static int fib6_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
  173. struct fib_rule_hdr *frh)
  174. {
  175. struct fib6_rule *rule6 = (struct fib6_rule *) rule;
  176. frh->dst_len = rule6->dst.plen;
  177. frh->src_len = rule6->src.plen;
  178. frh->tos = rule6->tclass;
  179. if (rule6->dst.plen)
  180. NLA_PUT(skb, FRA_DST, sizeof(struct in6_addr),
  181. &rule6->dst.addr);
  182. if (rule6->src.plen)
  183. NLA_PUT(skb, FRA_SRC, sizeof(struct in6_addr),
  184. &rule6->src.addr);
  185. return 0;
  186. nla_put_failure:
  187. return -ENOBUFS;
  188. }
  189. static u32 fib6_rule_default_pref(struct fib_rules_ops *ops)
  190. {
  191. return 0x3FFF;
  192. }
  193. static size_t fib6_rule_nlmsg_payload(struct fib_rule *rule)
  194. {
  195. return nla_total_size(16) /* dst */
  196. + nla_total_size(16); /* src */
  197. }
  198. static const struct fib_rules_ops __net_initdata fib6_rules_ops_template = {
  199. .family = AF_INET6,
  200. .rule_size = sizeof(struct fib6_rule),
  201. .addr_size = sizeof(struct in6_addr),
  202. .action = fib6_rule_action,
  203. .match = fib6_rule_match,
  204. .configure = fib6_rule_configure,
  205. .compare = fib6_rule_compare,
  206. .fill = fib6_rule_fill,
  207. .default_pref = fib6_rule_default_pref,
  208. .nlmsg_payload = fib6_rule_nlmsg_payload,
  209. .nlgroup = RTNLGRP_IPV6_RULE,
  210. .policy = fib6_rule_policy,
  211. .owner = THIS_MODULE,
  212. .fro_net = &init_net,
  213. };
  214. static int __net_init fib6_rules_net_init(struct net *net)
  215. {
  216. struct fib_rules_ops *ops;
  217. int err = -ENOMEM;
  218. ops = fib_rules_register(&fib6_rules_ops_template, net);
  219. if (IS_ERR(ops))
  220. return PTR_ERR(ops);
  221. net->ipv6.fib6_rules_ops = ops;
  222. err = fib_default_rule_add(net->ipv6.fib6_rules_ops, 0,
  223. RT6_TABLE_LOCAL, 0);
  224. if (err)
  225. goto out_fib6_rules_ops;
  226. err = fib_default_rule_add(net->ipv6.fib6_rules_ops,
  227. 0x7FFE, RT6_TABLE_MAIN, 0);
  228. if (err)
  229. goto out_fib6_rules_ops;
  230. out:
  231. return err;
  232. out_fib6_rules_ops:
  233. fib_rules_unregister(ops);
  234. goto out;
  235. }
  236. static void __net_exit fib6_rules_net_exit(struct net *net)
  237. {
  238. fib_rules_unregister(net->ipv6.fib6_rules_ops);
  239. }
  240. static struct pernet_operations fib6_rules_net_ops = {
  241. .init = fib6_rules_net_init,
  242. .exit = fib6_rules_net_exit,
  243. };
  244. int __init fib6_rules_init(void)
  245. {
  246. return register_pernet_subsys(&fib6_rules_net_ops);
  247. }
  248. void fib6_rules_cleanup(void)
  249. {
  250. unregister_pernet_subsys(&fib6_rules_net_ops);
  251. }