fib6_rules.c 6.2 KB

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