fib6_rules.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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/ip6_route.h>
  19. #include <net/netlink.h>
  20. struct fib6_rule
  21. {
  22. struct fib_rule common;
  23. struct rt6key src;
  24. struct rt6key dst;
  25. u8 tclass;
  26. };
  27. static struct fib_rules_ops fib6_rules_ops;
  28. static struct fib6_rule main_rule = {
  29. .common = {
  30. .refcnt = ATOMIC_INIT(2),
  31. .pref = 0x7FFE,
  32. .action = FR_ACT_TO_TBL,
  33. .table = RT6_TABLE_MAIN,
  34. },
  35. };
  36. static struct fib6_rule local_rule = {
  37. .common = {
  38. .refcnt = ATOMIC_INIT(2),
  39. .pref = 0,
  40. .action = FR_ACT_TO_TBL,
  41. .table = RT6_TABLE_LOCAL,
  42. .flags = FIB_RULE_PERMANENT,
  43. },
  44. };
  45. static LIST_HEAD(fib6_rules);
  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. goto out;
  85. dst_release(&rt->u.dst);
  86. rt = NULL;
  87. goto out;
  88. discard_pkt:
  89. dst_hold(&rt->u.dst);
  90. out:
  91. arg->result = rt;
  92. return rt == NULL ? -EAGAIN : 0;
  93. }
  94. static int fib6_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
  95. {
  96. struct fib6_rule *r = (struct fib6_rule *) rule;
  97. if (r->dst.plen &&
  98. !ipv6_prefix_equal(&fl->fl6_dst, &r->dst.addr, r->dst.plen))
  99. return 0;
  100. if (r->src.plen) {
  101. if (!(flags & RT6_LOOKUP_F_HAS_SADDR) ||
  102. !ipv6_prefix_equal(&fl->fl6_src, &r->src.addr, r->src.plen))
  103. return 0;
  104. }
  105. if (r->tclass && r->tclass != ((ntohl(fl->fl6_flowlabel) >> 20) & 0xff))
  106. return 0;
  107. return 1;
  108. }
  109. static struct nla_policy fib6_rule_policy[FRA_MAX+1] __read_mostly = {
  110. FRA_GENERIC_POLICY,
  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 (rule->action == FR_ACT_TO_TBL) {
  119. if (rule->table == RT6_TABLE_UNSPEC)
  120. goto errout;
  121. if (fib6_new_table(rule->table) == NULL) {
  122. err = -ENOBUFS;
  123. goto errout;
  124. }
  125. }
  126. if (frh->src_len)
  127. nla_memcpy(&rule6->src.addr, tb[FRA_SRC],
  128. sizeof(struct in6_addr));
  129. if (frh->dst_len)
  130. nla_memcpy(&rule6->dst.addr, tb[FRA_DST],
  131. sizeof(struct in6_addr));
  132. rule6->src.plen = frh->src_len;
  133. rule6->dst.plen = frh->dst_len;
  134. rule6->tclass = frh->tos;
  135. err = 0;
  136. errout:
  137. return err;
  138. }
  139. static int fib6_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
  140. struct nlattr **tb)
  141. {
  142. struct fib6_rule *rule6 = (struct fib6_rule *) rule;
  143. if (frh->src_len && (rule6->src.plen != frh->src_len))
  144. return 0;
  145. if (frh->dst_len && (rule6->dst.plen != frh->dst_len))
  146. return 0;
  147. if (frh->tos && (rule6->tclass != frh->tos))
  148. return 0;
  149. if (frh->src_len &&
  150. nla_memcmp(tb[FRA_SRC], &rule6->src.addr, sizeof(struct in6_addr)))
  151. return 0;
  152. if (frh->dst_len &&
  153. nla_memcmp(tb[FRA_DST], &rule6->dst.addr, sizeof(struct in6_addr)))
  154. return 0;
  155. return 1;
  156. }
  157. static int fib6_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
  158. struct nlmsghdr *nlh, struct fib_rule_hdr *frh)
  159. {
  160. struct fib6_rule *rule6 = (struct fib6_rule *) rule;
  161. frh->family = AF_INET6;
  162. frh->dst_len = rule6->dst.plen;
  163. frh->src_len = rule6->src.plen;
  164. frh->tos = rule6->tclass;
  165. if (rule6->dst.plen)
  166. NLA_PUT(skb, FRA_DST, sizeof(struct in6_addr),
  167. &rule6->dst.addr);
  168. if (rule6->src.plen)
  169. NLA_PUT(skb, FRA_SRC, sizeof(struct in6_addr),
  170. &rule6->src.addr);
  171. return 0;
  172. nla_put_failure:
  173. return -ENOBUFS;
  174. }
  175. int fib6_rules_dump(struct sk_buff *skb, struct netlink_callback *cb)
  176. {
  177. return fib_rules_dump(skb, cb, AF_INET6);
  178. }
  179. static u32 fib6_rule_default_pref(void)
  180. {
  181. return 0x3FFF;
  182. }
  183. static size_t fib6_rule_nlmsg_payload(struct fib_rule *rule)
  184. {
  185. return nla_total_size(16) /* dst */
  186. + nla_total_size(16); /* src */
  187. }
  188. static struct fib_rules_ops fib6_rules_ops = {
  189. .family = AF_INET6,
  190. .rule_size = sizeof(struct fib6_rule),
  191. .addr_size = sizeof(struct in6_addr),
  192. .action = fib6_rule_action,
  193. .match = fib6_rule_match,
  194. .configure = fib6_rule_configure,
  195. .compare = fib6_rule_compare,
  196. .fill = fib6_rule_fill,
  197. .default_pref = fib6_rule_default_pref,
  198. .nlmsg_payload = fib6_rule_nlmsg_payload,
  199. .nlgroup = RTNLGRP_IPV6_RULE,
  200. .policy = fib6_rule_policy,
  201. .rules_list = &fib6_rules,
  202. .owner = THIS_MODULE,
  203. };
  204. void __init fib6_rules_init(void)
  205. {
  206. list_add_tail(&local_rule.common.list, &fib6_rules);
  207. list_add_tail(&main_rule.common.list, &fib6_rules);
  208. fib_rules_register(&fib6_rules_ops);
  209. }
  210. void fib6_rules_cleanup(void)
  211. {
  212. fib_rules_unregister(&fib6_rules_ops);
  213. }