fib6_rules.c 5.6 KB

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