fib6_rules.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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/config.h>
  16. #include <linux/netdevice.h>
  17. #include <net/fib_rules.h>
  18. #include <net/ipv6.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. static LIST_HEAD(fib6_rules);
  47. struct dst_entry *fib6_rule_lookup(struct flowi *fl, int flags,
  48. pol_lookup_t lookup)
  49. {
  50. struct fib_lookup_arg arg = {
  51. .lookup_ptr = lookup,
  52. };
  53. fib_rules_lookup(&fib6_rules_ops, fl, flags, &arg);
  54. if (arg.rule)
  55. fib_rule_put(arg.rule);
  56. return (struct dst_entry *) arg.result;
  57. }
  58. int fib6_rule_action(struct fib_rule *rule, struct flowi *flp,
  59. int flags, struct fib_lookup_arg *arg)
  60. {
  61. struct rt6_info *rt = NULL;
  62. struct fib6_table *table;
  63. pol_lookup_t lookup = arg->lookup_ptr;
  64. switch (rule->action) {
  65. case FR_ACT_TO_TBL:
  66. break;
  67. case FR_ACT_UNREACHABLE:
  68. rt = &ip6_null_entry;
  69. goto discard_pkt;
  70. default:
  71. case FR_ACT_BLACKHOLE:
  72. rt = &ip6_blk_hole_entry;
  73. goto discard_pkt;
  74. case FR_ACT_PROHIBIT:
  75. rt = &ip6_prohibit_entry;
  76. goto discard_pkt;
  77. }
  78. table = fib6_get_table(rule->table);
  79. if (table)
  80. rt = lookup(table, flp, flags);
  81. if (rt != &ip6_null_entry)
  82. goto out;
  83. dst_release(&rt->u.dst);
  84. rt = NULL;
  85. goto out;
  86. discard_pkt:
  87. dst_hold(&rt->u.dst);
  88. out:
  89. arg->result = rt;
  90. return rt == NULL ? -EAGAIN : 0;
  91. }
  92. static int fib6_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
  93. {
  94. struct fib6_rule *r = (struct fib6_rule *) rule;
  95. if (!ipv6_prefix_equal(&fl->fl6_dst, &r->dst.addr, r->dst.plen))
  96. return 0;
  97. if ((flags & RT6_F_HAS_SADDR) &&
  98. !ipv6_prefix_equal(&fl->fl6_src, &r->src.addr, r->src.plen))
  99. return 0;
  100. return 1;
  101. }
  102. static struct nla_policy fib6_rule_policy[RTA_MAX+1] __read_mostly = {
  103. [FRA_IFNAME] = { .type = NLA_STRING },
  104. [FRA_PRIORITY] = { .type = NLA_U32 },
  105. [FRA_SRC] = { .minlen = sizeof(struct in6_addr) },
  106. [FRA_DST] = { .minlen = sizeof(struct in6_addr) },
  107. };
  108. static int fib6_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
  109. struct nlmsghdr *nlh, struct fib_rule_hdr *frh,
  110. struct nlattr **tb)
  111. {
  112. int err = -EINVAL;
  113. struct fib6_rule *rule6 = (struct fib6_rule *) rule;
  114. if (frh->src_len > 128 || frh->dst_len > 128 ||
  115. (frh->tos & ~IPV6_FLOWINFO_MASK))
  116. goto errout;
  117. if (rule->action == FR_ACT_TO_TBL) {
  118. if (rule->table == RT6_TABLE_UNSPEC)
  119. goto errout;
  120. if (fib6_new_table(rule->table) == NULL) {
  121. err = -ENOBUFS;
  122. goto errout;
  123. }
  124. }
  125. if (tb[FRA_SRC])
  126. nla_memcpy(&rule6->src.addr, tb[FRA_SRC],
  127. sizeof(struct in6_addr));
  128. if (tb[FRA_DST])
  129. nla_memcpy(&rule6->dst.addr, tb[FRA_DST],
  130. sizeof(struct in6_addr));
  131. rule6->src.plen = frh->src_len;
  132. rule6->dst.plen = frh->dst_len;
  133. rule6->tclass = frh->tos;
  134. err = 0;
  135. errout:
  136. return err;
  137. }
  138. static int fib6_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
  139. struct nlattr **tb)
  140. {
  141. struct fib6_rule *rule6 = (struct fib6_rule *) rule;
  142. if (frh->src_len && (rule6->src.plen != frh->src_len))
  143. return 0;
  144. if (frh->dst_len && (rule6->dst.plen != frh->dst_len))
  145. return 0;
  146. if (frh->tos && (rule6->tclass != frh->tos))
  147. return 0;
  148. if (tb[FRA_SRC] &&
  149. nla_memcmp(tb[FRA_SRC], &rule6->src.addr, sizeof(struct in6_addr)))
  150. return 0;
  151. if (tb[FRA_DST] &&
  152. nla_memcmp(tb[FRA_DST], &rule6->dst.addr, sizeof(struct in6_addr)))
  153. return 0;
  154. return 1;
  155. }
  156. static int fib6_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
  157. struct nlmsghdr *nlh, struct fib_rule_hdr *frh)
  158. {
  159. struct fib6_rule *rule6 = (struct fib6_rule *) rule;
  160. frh->family = AF_INET6;
  161. frh->dst_len = rule6->dst.plen;
  162. frh->src_len = rule6->src.plen;
  163. frh->tos = rule6->tclass;
  164. if (rule6->dst.plen)
  165. NLA_PUT(skb, FRA_DST, sizeof(struct in6_addr),
  166. &rule6->dst.addr);
  167. if (rule6->src.plen)
  168. NLA_PUT(skb, FRA_SRC, sizeof(struct in6_addr),
  169. &rule6->src.addr);
  170. return 0;
  171. nla_put_failure:
  172. return -ENOBUFS;
  173. }
  174. int fib6_rules_dump(struct sk_buff *skb, struct netlink_callback *cb)
  175. {
  176. return fib_rules_dump(skb, cb, AF_INET6);
  177. }
  178. static u32 fib6_rule_default_pref(void)
  179. {
  180. return 0x3FFF;
  181. }
  182. static struct fib_rules_ops fib6_rules_ops = {
  183. .family = AF_INET6,
  184. .rule_size = sizeof(struct fib6_rule),
  185. .action = fib6_rule_action,
  186. .match = fib6_rule_match,
  187. .configure = fib6_rule_configure,
  188. .compare = fib6_rule_compare,
  189. .fill = fib6_rule_fill,
  190. .default_pref = fib6_rule_default_pref,
  191. .nlgroup = RTNLGRP_IPV6_RULE,
  192. .policy = fib6_rule_policy,
  193. .rules_list = &fib6_rules,
  194. .owner = THIS_MODULE,
  195. };
  196. void __init fib6_rules_init(void)
  197. {
  198. list_add_tail(&local_rule.common.list, &fib6_rules);
  199. list_add_tail(&main_rule.common.list, &fib6_rules);
  200. fib_rules_register(&fib6_rules_ops);
  201. }
  202. void fib6_rules_cleanup(void)
  203. {
  204. fib_rules_unregister(&fib6_rules_ops);
  205. }