fib6_rules.c 6.7 KB

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