fib6_rules.c 6.7 KB

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