fib6_rules.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. #ifdef CONFIG_IPV6_ROUTE_FWMARK
  26. u32 fwmark;
  27. u32 fwmask;
  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 (r->dst.plen &&
  102. !ipv6_prefix_equal(&fl->fl6_dst, &r->dst.addr, r->dst.plen))
  103. return 0;
  104. if (r->src.plen) {
  105. if (!(flags & RT6_LOOKUP_F_HAS_SADDR) ||
  106. !ipv6_prefix_equal(&fl->fl6_src, &r->src.addr, r->src.plen))
  107. return 0;
  108. }
  109. if (r->tclass && r->tclass != ((ntohl(fl->fl6_flowlabel) >> 20) & 0xff))
  110. return 0;
  111. #ifdef CONFIG_IPV6_ROUTE_FWMARK
  112. if ((r->fwmark ^ fl->fl6_fwmark) & r->fwmask)
  113. return 0;
  114. #endif
  115. return 1;
  116. }
  117. static struct nla_policy fib6_rule_policy[FRA_MAX+1] __read_mostly = {
  118. [FRA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ - 1 },
  119. [FRA_PRIORITY] = { .type = NLA_U32 },
  120. [FRA_SRC] = { .len = sizeof(struct in6_addr) },
  121. [FRA_DST] = { .len = sizeof(struct in6_addr) },
  122. [FRA_FWMARK] = { .type = NLA_U32 },
  123. [FRA_FWMASK] = { .type = NLA_U32 },
  124. [FRA_TABLE] = { .type = NLA_U32 },
  125. };
  126. static int fib6_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
  127. struct nlmsghdr *nlh, struct fib_rule_hdr *frh,
  128. struct nlattr **tb)
  129. {
  130. int err = -EINVAL;
  131. struct fib6_rule *rule6 = (struct fib6_rule *) rule;
  132. if (frh->src_len > 128 || frh->dst_len > 128 ||
  133. (frh->tos & ~IPV6_FLOWINFO_MASK))
  134. goto errout;
  135. if (rule->action == FR_ACT_TO_TBL) {
  136. if (rule->table == RT6_TABLE_UNSPEC)
  137. goto errout;
  138. if (fib6_new_table(rule->table) == NULL) {
  139. err = -ENOBUFS;
  140. goto errout;
  141. }
  142. }
  143. if (tb[FRA_SRC])
  144. nla_memcpy(&rule6->src.addr, tb[FRA_SRC],
  145. sizeof(struct in6_addr));
  146. if (tb[FRA_DST])
  147. nla_memcpy(&rule6->dst.addr, tb[FRA_DST],
  148. sizeof(struct in6_addr));
  149. #ifdef CONFIG_IPV6_ROUTE_FWMARK
  150. if (tb[FRA_FWMARK]) {
  151. rule6->fwmark = nla_get_u32(tb[FRA_FWMARK]);
  152. if (rule6->fwmark) {
  153. /*
  154. * if the mark value is non-zero,
  155. * all bits are compared by default
  156. * unless a mask is explicitly specified.
  157. */
  158. rule6->fwmask = 0xFFFFFFFF;
  159. }
  160. }
  161. if (tb[FRA_FWMASK])
  162. rule6->fwmask = nla_get_u32(tb[FRA_FWMASK]);
  163. #endif
  164. rule6->src.plen = frh->src_len;
  165. rule6->dst.plen = frh->dst_len;
  166. rule6->tclass = frh->tos;
  167. err = 0;
  168. errout:
  169. return err;
  170. }
  171. static int fib6_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
  172. struct nlattr **tb)
  173. {
  174. struct fib6_rule *rule6 = (struct fib6_rule *) rule;
  175. if (frh->src_len && (rule6->src.plen != frh->src_len))
  176. return 0;
  177. if (frh->dst_len && (rule6->dst.plen != frh->dst_len))
  178. return 0;
  179. if (frh->tos && (rule6->tclass != frh->tos))
  180. return 0;
  181. if (tb[FRA_SRC] &&
  182. nla_memcmp(tb[FRA_SRC], &rule6->src.addr, sizeof(struct in6_addr)))
  183. return 0;
  184. if (tb[FRA_DST] &&
  185. nla_memcmp(tb[FRA_DST], &rule6->dst.addr, sizeof(struct in6_addr)))
  186. return 0;
  187. #ifdef CONFIG_IPV6_ROUTE_FWMARK
  188. if (tb[FRA_FWMARK] && (rule6->fwmark != nla_get_u32(tb[FRA_FWMARK])))
  189. return 0;
  190. if (tb[FRA_FWMASK] && (rule6->fwmask != nla_get_u32(tb[FRA_FWMASK])))
  191. return 0;
  192. #endif
  193. return 1;
  194. }
  195. static int fib6_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
  196. struct nlmsghdr *nlh, struct fib_rule_hdr *frh)
  197. {
  198. struct fib6_rule *rule6 = (struct fib6_rule *) rule;
  199. frh->family = AF_INET6;
  200. frh->dst_len = rule6->dst.plen;
  201. frh->src_len = rule6->src.plen;
  202. frh->tos = rule6->tclass;
  203. if (rule6->dst.plen)
  204. NLA_PUT(skb, FRA_DST, sizeof(struct in6_addr),
  205. &rule6->dst.addr);
  206. if (rule6->src.plen)
  207. NLA_PUT(skb, FRA_SRC, sizeof(struct in6_addr),
  208. &rule6->src.addr);
  209. #ifdef CONFIG_IPV6_ROUTE_FWMARK
  210. if (rule6->fwmark)
  211. NLA_PUT_U32(skb, FRA_FWMARK, rule6->fwmark);
  212. if (rule6->fwmask || rule6->fwmark)
  213. NLA_PUT_U32(skb, FRA_FWMASK, rule6->fwmask);
  214. #endif
  215. return 0;
  216. nla_put_failure:
  217. return -ENOBUFS;
  218. }
  219. int fib6_rules_dump(struct sk_buff *skb, struct netlink_callback *cb)
  220. {
  221. return fib_rules_dump(skb, cb, AF_INET6);
  222. }
  223. static u32 fib6_rule_default_pref(void)
  224. {
  225. return 0x3FFF;
  226. }
  227. static struct fib_rules_ops fib6_rules_ops = {
  228. .family = AF_INET6,
  229. .rule_size = sizeof(struct fib6_rule),
  230. .action = fib6_rule_action,
  231. .match = fib6_rule_match,
  232. .configure = fib6_rule_configure,
  233. .compare = fib6_rule_compare,
  234. .fill = fib6_rule_fill,
  235. .default_pref = fib6_rule_default_pref,
  236. .nlgroup = RTNLGRP_IPV6_RULE,
  237. .policy = fib6_rule_policy,
  238. .rules_list = &fib6_rules,
  239. .owner = THIS_MODULE,
  240. };
  241. void __init fib6_rules_init(void)
  242. {
  243. list_add_tail(&local_rule.common.list, &fib6_rules);
  244. list_add_tail(&main_rule.common.list, &fib6_rules);
  245. fib_rules_register(&fib6_rules_ops);
  246. }
  247. void fib6_rules_cleanup(void)
  248. {
  249. fib_rules_unregister(&fib6_rules_ops);
  250. }