fib6_rules.c 6.8 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/addrconf.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. u8 tclass;
  27. };
  28. struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi *fl,
  29. int flags, pol_lookup_t lookup)
  30. {
  31. struct fib_lookup_arg arg = {
  32. .lookup_ptr = lookup,
  33. .flags = FIB_LOOKUP_NOREF,
  34. };
  35. fib_rules_lookup(net->ipv6.fib6_rules_ops, fl, flags, &arg);
  36. if (arg.result)
  37. return arg.result;
  38. dst_hold(&net->ipv6.ip6_null_entry->dst);
  39. return &net->ipv6.ip6_null_entry->dst;
  40. }
  41. static int fib6_rule_action(struct fib_rule *rule, struct flowi *flp,
  42. int flags, struct fib_lookup_arg *arg)
  43. {
  44. struct rt6_info *rt = NULL;
  45. struct fib6_table *table;
  46. struct net *net = rule->fr_net;
  47. pol_lookup_t lookup = arg->lookup_ptr;
  48. switch (rule->action) {
  49. case FR_ACT_TO_TBL:
  50. break;
  51. case FR_ACT_UNREACHABLE:
  52. rt = net->ipv6.ip6_null_entry;
  53. goto discard_pkt;
  54. default:
  55. case FR_ACT_BLACKHOLE:
  56. rt = net->ipv6.ip6_blk_hole_entry;
  57. goto discard_pkt;
  58. case FR_ACT_PROHIBIT:
  59. rt = net->ipv6.ip6_prohibit_entry;
  60. goto discard_pkt;
  61. }
  62. table = fib6_get_table(net, rule->table);
  63. if (table)
  64. rt = lookup(net, table, flp, flags);
  65. if (rt != net->ipv6.ip6_null_entry) {
  66. struct fib6_rule *r = (struct fib6_rule *)rule;
  67. /*
  68. * If we need to find a source address for this traffic,
  69. * we check the result if it meets requirement of the rule.
  70. */
  71. if ((rule->flags & FIB_RULE_FIND_SADDR) &&
  72. r->src.plen && !(flags & RT6_LOOKUP_F_HAS_SADDR)) {
  73. struct in6_addr saddr;
  74. if (ipv6_dev_get_saddr(net,
  75. ip6_dst_idev(&rt->dst)->dev,
  76. &flp->fl6_dst,
  77. rt6_flags2srcprefs(flags),
  78. &saddr))
  79. goto again;
  80. if (!ipv6_prefix_equal(&saddr, &r->src.addr,
  81. r->src.plen))
  82. goto again;
  83. ipv6_addr_copy(&flp->fl6_src, &saddr);
  84. }
  85. goto out;
  86. }
  87. again:
  88. dst_release(&rt->dst);
  89. rt = NULL;
  90. goto out;
  91. discard_pkt:
  92. dst_hold(&rt->dst);
  93. out:
  94. arg->result = rt;
  95. return rt == NULL ? -EAGAIN : 0;
  96. }
  97. static int fib6_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
  98. {
  99. struct fib6_rule *r = (struct fib6_rule *) rule;
  100. if (r->dst.plen &&
  101. !ipv6_prefix_equal(&fl->fl6_dst, &r->dst.addr, r->dst.plen))
  102. return 0;
  103. /*
  104. * If FIB_RULE_FIND_SADDR is set and we do not have a
  105. * source address for the traffic, we defer check for
  106. * source address.
  107. */
  108. if (r->src.plen) {
  109. if (flags & RT6_LOOKUP_F_HAS_SADDR) {
  110. if (!ipv6_prefix_equal(&fl->fl6_src, &r->src.addr,
  111. r->src.plen))
  112. return 0;
  113. } else if (!(r->common.flags & FIB_RULE_FIND_SADDR))
  114. return 0;
  115. }
  116. if (r->tclass && r->tclass != ((ntohl(fl->fl6_flowlabel) >> 20) & 0xff))
  117. return 0;
  118. return 1;
  119. }
  120. static const struct nla_policy fib6_rule_policy[FRA_MAX+1] = {
  121. FRA_GENERIC_POLICY,
  122. };
  123. static int fib6_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
  124. struct fib_rule_hdr *frh,
  125. struct nlattr **tb)
  126. {
  127. int err = -EINVAL;
  128. struct net *net = sock_net(skb->sk);
  129. struct fib6_rule *rule6 = (struct fib6_rule *) rule;
  130. if (rule->action == FR_ACT_TO_TBL) {
  131. if (rule->table == RT6_TABLE_UNSPEC)
  132. goto errout;
  133. if (fib6_new_table(net, rule->table) == NULL) {
  134. err = -ENOBUFS;
  135. goto errout;
  136. }
  137. }
  138. if (frh->src_len)
  139. nla_memcpy(&rule6->src.addr, tb[FRA_SRC],
  140. sizeof(struct in6_addr));
  141. if (frh->dst_len)
  142. nla_memcpy(&rule6->dst.addr, tb[FRA_DST],
  143. sizeof(struct in6_addr));
  144. rule6->src.plen = frh->src_len;
  145. rule6->dst.plen = frh->dst_len;
  146. rule6->tclass = frh->tos;
  147. err = 0;
  148. errout:
  149. return err;
  150. }
  151. static int fib6_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
  152. struct nlattr **tb)
  153. {
  154. struct fib6_rule *rule6 = (struct fib6_rule *) rule;
  155. if (frh->src_len && (rule6->src.plen != frh->src_len))
  156. return 0;
  157. if (frh->dst_len && (rule6->dst.plen != frh->dst_len))
  158. return 0;
  159. if (frh->tos && (rule6->tclass != frh->tos))
  160. return 0;
  161. if (frh->src_len &&
  162. nla_memcmp(tb[FRA_SRC], &rule6->src.addr, sizeof(struct in6_addr)))
  163. return 0;
  164. if (frh->dst_len &&
  165. nla_memcmp(tb[FRA_DST], &rule6->dst.addr, sizeof(struct in6_addr)))
  166. return 0;
  167. return 1;
  168. }
  169. static int fib6_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
  170. struct fib_rule_hdr *frh)
  171. {
  172. struct fib6_rule *rule6 = (struct fib6_rule *) rule;
  173. frh->dst_len = rule6->dst.plen;
  174. frh->src_len = rule6->src.plen;
  175. frh->tos = rule6->tclass;
  176. if (rule6->dst.plen)
  177. NLA_PUT(skb, FRA_DST, sizeof(struct in6_addr),
  178. &rule6->dst.addr);
  179. if (rule6->src.plen)
  180. NLA_PUT(skb, FRA_SRC, sizeof(struct in6_addr),
  181. &rule6->src.addr);
  182. return 0;
  183. nla_put_failure:
  184. return -ENOBUFS;
  185. }
  186. static u32 fib6_rule_default_pref(struct fib_rules_ops *ops)
  187. {
  188. return 0x3FFF;
  189. }
  190. static size_t fib6_rule_nlmsg_payload(struct fib_rule *rule)
  191. {
  192. return nla_total_size(16) /* dst */
  193. + nla_total_size(16); /* src */
  194. }
  195. static const struct fib_rules_ops __net_initdata fib6_rules_ops_template = {
  196. .family = AF_INET6,
  197. .rule_size = sizeof(struct fib6_rule),
  198. .addr_size = sizeof(struct in6_addr),
  199. .action = fib6_rule_action,
  200. .match = fib6_rule_match,
  201. .configure = fib6_rule_configure,
  202. .compare = fib6_rule_compare,
  203. .fill = fib6_rule_fill,
  204. .default_pref = fib6_rule_default_pref,
  205. .nlmsg_payload = fib6_rule_nlmsg_payload,
  206. .nlgroup = RTNLGRP_IPV6_RULE,
  207. .policy = fib6_rule_policy,
  208. .owner = THIS_MODULE,
  209. .fro_net = &init_net,
  210. };
  211. static int __net_init fib6_rules_net_init(struct net *net)
  212. {
  213. struct fib_rules_ops *ops;
  214. int err = -ENOMEM;
  215. ops = fib_rules_register(&fib6_rules_ops_template, net);
  216. if (IS_ERR(ops))
  217. return PTR_ERR(ops);
  218. net->ipv6.fib6_rules_ops = ops;
  219. err = fib_default_rule_add(net->ipv6.fib6_rules_ops, 0,
  220. RT6_TABLE_LOCAL, 0);
  221. if (err)
  222. goto out_fib6_rules_ops;
  223. err = fib_default_rule_add(net->ipv6.fib6_rules_ops,
  224. 0x7FFE, RT6_TABLE_MAIN, 0);
  225. if (err)
  226. goto out_fib6_rules_ops;
  227. out:
  228. return err;
  229. out_fib6_rules_ops:
  230. fib_rules_unregister(ops);
  231. goto out;
  232. }
  233. static void __net_exit fib6_rules_net_exit(struct net *net)
  234. {
  235. fib_rules_unregister(net->ipv6.fib6_rules_ops);
  236. }
  237. static struct pernet_operations fib6_rules_net_ops = {
  238. .init = fib6_rules_net_init,
  239. .exit = fib6_rules_net_exit,
  240. };
  241. int __init fib6_rules_init(void)
  242. {
  243. return register_pernet_subsys(&fib6_rules_net_ops);
  244. }
  245. void fib6_rules_cleanup(void)
  246. {
  247. unregister_pernet_subsys(&fib6_rules_net_ops);
  248. }