fib6_rules.c 6.9 KB

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