fib6_rules.c 6.3 KB

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