xt_policy.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /* IP tables module for matching IPsec policy
  2. *
  3. * Copyright (c) 2004,2005 Patrick McHardy, <kaber@trash.net>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/skbuff.h>
  13. #include <linux/init.h>
  14. #include <net/xfrm.h>
  15. #include <linux/netfilter.h>
  16. #include <linux/netfilter/xt_policy.h>
  17. #include <linux/netfilter/x_tables.h>
  18. MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
  19. MODULE_DESCRIPTION("Xtables: IPsec policy match");
  20. MODULE_LICENSE("GPL");
  21. static inline bool
  22. xt_addr_cmp(const union nf_inet_addr *a1, const union nf_inet_addr *m,
  23. const union nf_inet_addr *a2, unsigned short family)
  24. {
  25. switch (family) {
  26. case NFPROTO_IPV4:
  27. return ((a1->ip ^ a2->ip) & m->ip) == 0;
  28. case NFPROTO_IPV6:
  29. return ipv6_masked_addr_cmp(&a1->in6, &m->in6, &a2->in6) == 0;
  30. }
  31. return false;
  32. }
  33. static bool
  34. match_xfrm_state(const struct xfrm_state *x, const struct xt_policy_elem *e,
  35. unsigned short family)
  36. {
  37. #define MATCH_ADDR(x,y,z) (!e->match.x || \
  38. (xt_addr_cmp(&e->x, &e->y, (const union nf_inet_addr *)(z), family) \
  39. ^ e->invert.x))
  40. #define MATCH(x,y) (!e->match.x || ((e->x == (y)) ^ e->invert.x))
  41. return MATCH_ADDR(saddr, smask, &x->props.saddr) &&
  42. MATCH_ADDR(daddr, dmask, &x->id.daddr) &&
  43. MATCH(proto, x->id.proto) &&
  44. MATCH(mode, x->props.mode) &&
  45. MATCH(spi, x->id.spi) &&
  46. MATCH(reqid, x->props.reqid);
  47. }
  48. static int
  49. match_policy_in(const struct sk_buff *skb, const struct xt_policy_info *info,
  50. unsigned short family)
  51. {
  52. const struct xt_policy_elem *e;
  53. const struct sec_path *sp = skb->sp;
  54. int strict = info->flags & XT_POLICY_MATCH_STRICT;
  55. int i, pos;
  56. if (sp == NULL)
  57. return -1;
  58. if (strict && info->len != sp->len)
  59. return 0;
  60. for (i = sp->len - 1; i >= 0; i--) {
  61. pos = strict ? i - sp->len + 1 : 0;
  62. if (pos >= info->len)
  63. return 0;
  64. e = &info->pol[pos];
  65. if (match_xfrm_state(sp->xvec[i], e, family)) {
  66. if (!strict)
  67. return 1;
  68. } else if (strict)
  69. return 0;
  70. }
  71. return strict ? 1 : 0;
  72. }
  73. static int
  74. match_policy_out(const struct sk_buff *skb, const struct xt_policy_info *info,
  75. unsigned short family)
  76. {
  77. const struct xt_policy_elem *e;
  78. const struct dst_entry *dst = skb_dst(skb);
  79. int strict = info->flags & XT_POLICY_MATCH_STRICT;
  80. int i, pos;
  81. if (dst->xfrm == NULL)
  82. return -1;
  83. for (i = 0; dst && dst->xfrm; dst = dst->child, i++) {
  84. pos = strict ? i : 0;
  85. if (pos >= info->len)
  86. return 0;
  87. e = &info->pol[pos];
  88. if (match_xfrm_state(dst->xfrm, e, family)) {
  89. if (!strict)
  90. return 1;
  91. } else if (strict)
  92. return 0;
  93. }
  94. return strict ? i == info->len : 0;
  95. }
  96. static bool
  97. policy_mt(const struct sk_buff *skb, struct xt_action_param *par)
  98. {
  99. const struct xt_policy_info *info = par->matchinfo;
  100. int ret;
  101. if (info->flags & XT_POLICY_MATCH_IN)
  102. ret = match_policy_in(skb, info, par->family);
  103. else
  104. ret = match_policy_out(skb, info, par->family);
  105. if (ret < 0)
  106. ret = info->flags & XT_POLICY_MATCH_NONE ? true : false;
  107. else if (info->flags & XT_POLICY_MATCH_NONE)
  108. ret = false;
  109. return ret;
  110. }
  111. static int policy_mt_check(const struct xt_mtchk_param *par)
  112. {
  113. const struct xt_policy_info *info = par->matchinfo;
  114. if (!(info->flags & (XT_POLICY_MATCH_IN|XT_POLICY_MATCH_OUT))) {
  115. pr_info("neither incoming nor outgoing policy selected\n");
  116. return -EINVAL;
  117. }
  118. if (par->hook_mask & ((1 << NF_INET_PRE_ROUTING) |
  119. (1 << NF_INET_LOCAL_IN)) && info->flags & XT_POLICY_MATCH_OUT) {
  120. pr_info("output policy not valid in PREROUTING and INPUT\n");
  121. return -EINVAL;
  122. }
  123. if (par->hook_mask & ((1 << NF_INET_POST_ROUTING) |
  124. (1 << NF_INET_LOCAL_OUT)) && info->flags & XT_POLICY_MATCH_IN) {
  125. pr_info("input policy not valid in POSTROUTING and OUTPUT\n");
  126. return -EINVAL;
  127. }
  128. if (info->len > XT_POLICY_MAX_ELEM) {
  129. pr_info("too many policy elements\n");
  130. return -EINVAL;
  131. }
  132. return 0;
  133. }
  134. static struct xt_match policy_mt_reg[] __read_mostly = {
  135. {
  136. .name = "policy",
  137. .family = NFPROTO_IPV4,
  138. .checkentry = policy_mt_check,
  139. .match = policy_mt,
  140. .matchsize = sizeof(struct xt_policy_info),
  141. .me = THIS_MODULE,
  142. },
  143. {
  144. .name = "policy",
  145. .family = NFPROTO_IPV6,
  146. .checkentry = policy_mt_check,
  147. .match = policy_mt,
  148. .matchsize = sizeof(struct xt_policy_info),
  149. .me = THIS_MODULE,
  150. },
  151. };
  152. static int __init policy_mt_init(void)
  153. {
  154. return xt_register_matches(policy_mt_reg, ARRAY_SIZE(policy_mt_reg));
  155. }
  156. static void __exit policy_mt_exit(void)
  157. {
  158. xt_unregister_matches(policy_mt_reg, ARRAY_SIZE(policy_mt_reg));
  159. }
  160. module_init(policy_mt_init);
  161. module_exit(policy_mt_exit);
  162. MODULE_ALIAS("ipt_policy");
  163. MODULE_ALIAS("ip6t_policy");