xt_policy.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/skbuff.h>
  12. #include <linux/init.h>
  13. #include <net/xfrm.h>
  14. #include <linux/netfilter/xt_policy.h>
  15. #include <linux/netfilter/x_tables.h>
  16. MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
  17. MODULE_DESCRIPTION("Xtables IPsec policy matching module");
  18. MODULE_LICENSE("GPL");
  19. static inline int
  20. xt_addr_cmp(const union xt_policy_addr *a1, const union xt_policy_addr *m,
  21. const union xt_policy_addr *a2, unsigned short family)
  22. {
  23. switch (family) {
  24. case AF_INET:
  25. return !((a1->a4.s_addr ^ a2->a4.s_addr) & m->a4.s_addr);
  26. case AF_INET6:
  27. return !ipv6_masked_addr_cmp(&a1->a6, &m->a6, &a2->a6);
  28. }
  29. return 0;
  30. }
  31. static inline int
  32. match_xfrm_state(struct xfrm_state *x, const struct xt_policy_elem *e,
  33. unsigned short family)
  34. {
  35. #define MATCH_ADDR(x,y,z) (!e->match.x || \
  36. (xt_addr_cmp(&e->x, &e->y, z, family) \
  37. ^ e->invert.x))
  38. #define MATCH(x,y) (!e->match.x || ((e->x == (y)) ^ e->invert.x))
  39. return MATCH_ADDR(saddr, smask, (union xt_policy_addr *)&x->props.saddr) &&
  40. MATCH_ADDR(daddr, dmask, (union xt_policy_addr *)&x->id.daddr) &&
  41. MATCH(proto, x->id.proto) &&
  42. MATCH(mode, x->props.mode) &&
  43. MATCH(spi, x->id.spi) &&
  44. MATCH(reqid, x->props.reqid);
  45. }
  46. static int
  47. match_policy_in(const struct sk_buff *skb, const struct xt_policy_info *info,
  48. unsigned short family)
  49. {
  50. const struct xt_policy_elem *e;
  51. struct sec_path *sp = skb->sp;
  52. int strict = info->flags & XT_POLICY_MATCH_STRICT;
  53. int i, pos;
  54. if (sp == NULL)
  55. return -1;
  56. if (strict && info->len != sp->len)
  57. return 0;
  58. for (i = sp->len - 1; i >= 0; i--) {
  59. pos = strict ? i - sp->len + 1 : 0;
  60. if (pos >= info->len)
  61. return 0;
  62. e = &info->pol[pos];
  63. if (match_xfrm_state(sp->xvec[i], e, family)) {
  64. if (!strict)
  65. return 1;
  66. } else if (strict)
  67. return 0;
  68. }
  69. return strict ? 1 : 0;
  70. }
  71. static int
  72. match_policy_out(const struct sk_buff *skb, const struct xt_policy_info *info,
  73. unsigned short family)
  74. {
  75. const struct xt_policy_elem *e;
  76. struct dst_entry *dst = skb->dst;
  77. int strict = info->flags & XT_POLICY_MATCH_STRICT;
  78. int i, pos;
  79. if (dst->xfrm == NULL)
  80. return -1;
  81. for (i = 0; dst && dst->xfrm; dst = dst->child, i++) {
  82. pos = strict ? i : 0;
  83. if (pos >= info->len)
  84. return 0;
  85. e = &info->pol[pos];
  86. if (match_xfrm_state(dst->xfrm, e, family)) {
  87. if (!strict)
  88. return 1;
  89. } else if (strict)
  90. return 0;
  91. }
  92. return strict ? i == info->len : 0;
  93. }
  94. static int match(const struct sk_buff *skb,
  95. const struct net_device *in,
  96. const struct net_device *out,
  97. const struct xt_match *match,
  98. const void *matchinfo,
  99. int offset,
  100. unsigned int protoff,
  101. int *hotdrop)
  102. {
  103. const struct xt_policy_info *info = matchinfo;
  104. int ret;
  105. if (info->flags & XT_POLICY_MATCH_IN)
  106. ret = match_policy_in(skb, info, match->family);
  107. else
  108. ret = match_policy_out(skb, info, match->family);
  109. if (ret < 0)
  110. ret = info->flags & XT_POLICY_MATCH_NONE ? 1 : 0;
  111. else if (info->flags & XT_POLICY_MATCH_NONE)
  112. ret = 0;
  113. return ret;
  114. }
  115. static int checkentry(const char *tablename, const void *ip_void,
  116. const struct xt_match *match,
  117. void *matchinfo, unsigned int hook_mask)
  118. {
  119. struct xt_policy_info *info = matchinfo;
  120. if (!(info->flags & (XT_POLICY_MATCH_IN|XT_POLICY_MATCH_OUT))) {
  121. printk(KERN_ERR "xt_policy: neither incoming nor "
  122. "outgoing policy selected\n");
  123. return 0;
  124. }
  125. /* hook values are equal for IPv4 and IPv6 */
  126. if (hook_mask & (1 << NF_IP_PRE_ROUTING | 1 << NF_IP_LOCAL_IN)
  127. && info->flags & XT_POLICY_MATCH_OUT) {
  128. printk(KERN_ERR "xt_policy: output policy not valid in "
  129. "PRE_ROUTING and INPUT\n");
  130. return 0;
  131. }
  132. if (hook_mask & (1 << NF_IP_POST_ROUTING | 1 << NF_IP_LOCAL_OUT)
  133. && info->flags & XT_POLICY_MATCH_IN) {
  134. printk(KERN_ERR "xt_policy: input policy not valid in "
  135. "POST_ROUTING and OUTPUT\n");
  136. return 0;
  137. }
  138. if (info->len > XT_POLICY_MAX_ELEM) {
  139. printk(KERN_ERR "xt_policy: too many policy elements\n");
  140. return 0;
  141. }
  142. return 1;
  143. }
  144. static struct xt_match xt_policy_match[] = {
  145. {
  146. .name = "policy",
  147. .family = AF_INET,
  148. .checkentry = checkentry,
  149. .match = match,
  150. .matchsize = sizeof(struct xt_policy_info),
  151. .me = THIS_MODULE,
  152. },
  153. {
  154. .name = "policy",
  155. .family = AF_INET6,
  156. .checkentry = checkentry,
  157. .match = match,
  158. .matchsize = sizeof(struct xt_policy_info),
  159. .me = THIS_MODULE,
  160. },
  161. };
  162. static int __init init(void)
  163. {
  164. return xt_register_matches(xt_policy_match,
  165. ARRAY_SIZE(xt_policy_match));
  166. }
  167. static void __exit fini(void)
  168. {
  169. xt_unregister_matches(xt_policy_match, ARRAY_SIZE(xt_policy_match));
  170. }
  171. module_init(init);
  172. module_exit(fini);
  173. MODULE_ALIAS("ipt_policy");
  174. MODULE_ALIAS("ip6t_policy");