fib6_rules.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. static struct fib_rules_ops fib6_rules_ops;
  29. struct dst_entry *fib6_rule_lookup(struct flowi *fl, int flags,
  30. pol_lookup_t lookup)
  31. {
  32. struct fib_lookup_arg arg = {
  33. .lookup_ptr = lookup,
  34. };
  35. fib_rules_lookup(&fib6_rules_ops, fl, flags, &arg);
  36. if (arg.rule)
  37. fib_rule_put(arg.rule);
  38. if (arg.result)
  39. return arg.result;
  40. dst_hold(&ip6_null_entry.u.dst);
  41. return &ip6_null_entry.u.dst;
  42. }
  43. static int fib6_rule_action(struct fib_rule *rule, struct flowi *flp,
  44. int flags, struct fib_lookup_arg *arg)
  45. {
  46. struct rt6_info *rt = NULL;
  47. struct fib6_table *table;
  48. pol_lookup_t lookup = arg->lookup_ptr;
  49. switch (rule->action) {
  50. case FR_ACT_TO_TBL:
  51. break;
  52. case FR_ACT_UNREACHABLE:
  53. rt = &ip6_null_entry;
  54. goto discard_pkt;
  55. default:
  56. case FR_ACT_BLACKHOLE:
  57. rt = &ip6_blk_hole_entry;
  58. goto discard_pkt;
  59. case FR_ACT_PROHIBIT:
  60. rt = &ip6_prohibit_entry;
  61. goto discard_pkt;
  62. }
  63. table = fib6_get_table(rule->table);
  64. if (table)
  65. rt = lookup(table, flp, flags);
  66. if (rt != &ip6_null_entry) {
  67. struct fib6_rule *r = (struct fib6_rule *)rule;
  68. /*
  69. * If we need to find a source address for this traffic,
  70. * we check the result if it meets requirement of the rule.
  71. */
  72. if ((rule->flags & FIB_RULE_FIND_SADDR) &&
  73. r->src.plen && !(flags & RT6_LOOKUP_F_HAS_SADDR)) {
  74. struct in6_addr saddr;
  75. if (ipv6_get_saddr(&rt->u.dst, &flp->fl6_dst,
  76. &saddr))
  77. goto again;
  78. if (!ipv6_prefix_equal(&saddr, &r->src.addr,
  79. r->src.plen))
  80. goto again;
  81. ipv6_addr_copy(&flp->fl6_src, &saddr);
  82. }
  83. goto out;
  84. }
  85. again:
  86. dst_release(&rt->u.dst);
  87. rt = NULL;
  88. goto out;
  89. discard_pkt:
  90. dst_hold(&rt->u.dst);
  91. out:
  92. arg->result = rt;
  93. return rt == NULL ? -EAGAIN : 0;
  94. }
  95. static int fib6_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
  96. {
  97. struct fib6_rule *r = (struct fib6_rule *) rule;
  98. if (r->dst.plen &&
  99. !ipv6_prefix_equal(&fl->fl6_dst, &r->dst.addr, r->dst.plen))
  100. return 0;
  101. /*
  102. * If FIB_RULE_FIND_SADDR is set and we do not have a
  103. * source address for the traffic, we defer check for
  104. * source address.
  105. */
  106. if (r->src.plen) {
  107. if (flags & RT6_LOOKUP_F_HAS_SADDR) {
  108. if (!ipv6_prefix_equal(&fl->fl6_src, &r->src.addr,
  109. r->src.plen))
  110. return 0;
  111. } else if (!(r->common.flags & FIB_RULE_FIND_SADDR))
  112. return 0;
  113. }
  114. if (r->tclass && r->tclass != ((ntohl(fl->fl6_flowlabel) >> 20) & 0xff))
  115. return 0;
  116. return 1;
  117. }
  118. static const struct nla_policy fib6_rule_policy[FRA_MAX+1] = {
  119. FRA_GENERIC_POLICY,
  120. };
  121. static int fib6_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
  122. struct nlmsghdr *nlh, struct fib_rule_hdr *frh,
  123. struct nlattr **tb)
  124. {
  125. int err = -EINVAL;
  126. struct fib6_rule *rule6 = (struct fib6_rule *) rule;
  127. if (rule->action == FR_ACT_TO_TBL) {
  128. if (rule->table == RT6_TABLE_UNSPEC)
  129. goto errout;
  130. if (fib6_new_table(rule->table) == NULL) {
  131. err = -ENOBUFS;
  132. goto errout;
  133. }
  134. }
  135. if (frh->src_len)
  136. nla_memcpy(&rule6->src.addr, tb[FRA_SRC],
  137. sizeof(struct in6_addr));
  138. if (frh->dst_len)
  139. nla_memcpy(&rule6->dst.addr, tb[FRA_DST],
  140. sizeof(struct in6_addr));
  141. rule6->src.plen = frh->src_len;
  142. rule6->dst.plen = frh->dst_len;
  143. rule6->tclass = frh->tos;
  144. err = 0;
  145. errout:
  146. return err;
  147. }
  148. static int fib6_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
  149. struct nlattr **tb)
  150. {
  151. struct fib6_rule *rule6 = (struct fib6_rule *) rule;
  152. if (frh->src_len && (rule6->src.plen != frh->src_len))
  153. return 0;
  154. if (frh->dst_len && (rule6->dst.plen != frh->dst_len))
  155. return 0;
  156. if (frh->tos && (rule6->tclass != frh->tos))
  157. return 0;
  158. if (frh->src_len &&
  159. nla_memcmp(tb[FRA_SRC], &rule6->src.addr, sizeof(struct in6_addr)))
  160. return 0;
  161. if (frh->dst_len &&
  162. nla_memcmp(tb[FRA_DST], &rule6->dst.addr, sizeof(struct in6_addr)))
  163. return 0;
  164. return 1;
  165. }
  166. static int fib6_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
  167. struct nlmsghdr *nlh, struct fib_rule_hdr *frh)
  168. {
  169. struct fib6_rule *rule6 = (struct fib6_rule *) rule;
  170. frh->family = AF_INET6;
  171. frh->dst_len = rule6->dst.plen;
  172. frh->src_len = rule6->src.plen;
  173. frh->tos = rule6->tclass;
  174. if (rule6->dst.plen)
  175. NLA_PUT(skb, FRA_DST, sizeof(struct in6_addr),
  176. &rule6->dst.addr);
  177. if (rule6->src.plen)
  178. NLA_PUT(skb, FRA_SRC, sizeof(struct in6_addr),
  179. &rule6->src.addr);
  180. return 0;
  181. nla_put_failure:
  182. return -ENOBUFS;
  183. }
  184. static u32 fib6_rule_default_pref(void)
  185. {
  186. return 0x3FFF;
  187. }
  188. static size_t fib6_rule_nlmsg_payload(struct fib_rule *rule)
  189. {
  190. return nla_total_size(16) /* dst */
  191. + nla_total_size(16); /* src */
  192. }
  193. static struct fib_rules_ops fib6_rules_ops = {
  194. .family = AF_INET6,
  195. .rule_size = sizeof(struct fib6_rule),
  196. .addr_size = sizeof(struct in6_addr),
  197. .action = fib6_rule_action,
  198. .match = fib6_rule_match,
  199. .configure = fib6_rule_configure,
  200. .compare = fib6_rule_compare,
  201. .fill = fib6_rule_fill,
  202. .default_pref = fib6_rule_default_pref,
  203. .nlmsg_payload = fib6_rule_nlmsg_payload,
  204. .nlgroup = RTNLGRP_IPV6_RULE,
  205. .policy = fib6_rule_policy,
  206. .rules_list = LIST_HEAD_INIT(fib6_rules_ops.rules_list),
  207. .owner = THIS_MODULE,
  208. };
  209. static int __init fib6_default_rules_init(void)
  210. {
  211. int err;
  212. err = fib_default_rule_add(&fib6_rules_ops, 0,
  213. RT6_TABLE_LOCAL, FIB_RULE_PERMANENT);
  214. if (err < 0)
  215. return err;
  216. err = fib_default_rule_add(&fib6_rules_ops, 0x7FFE, RT6_TABLE_MAIN, 0);
  217. if (err < 0)
  218. return err;
  219. return 0;
  220. }
  221. void __init fib6_rules_init(void)
  222. {
  223. BUG_ON(fib6_default_rules_init());
  224. fib_rules_register(&fib6_rules_ops);
  225. }
  226. void fib6_rules_cleanup(void)
  227. {
  228. fib_rules_unregister(&fib6_rules_ops);
  229. }