xt_policy.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* IP tables module for matching IPsec policy
  3. *
  4. * Copyright (c) 2004,2005 Patrick McHardy, <kaber@trash.net>
  5. */
  6. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/skbuff.h>
  10. #include <linux/init.h>
  11. #include <net/xfrm.h>
  12. #include <linux/netfilter.h>
  13. #include <linux/netfilter/xt_policy.h>
  14. #include <linux/netfilter/x_tables.h>
  15. MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
  16. MODULE_DESCRIPTION("Xtables: IPsec policy match");
  17. MODULE_LICENSE("GPL");
  18. static inline bool
  19. xt_addr_cmp(const union nf_inet_addr *a1, const union nf_inet_addr *m,
  20. const union nf_inet_addr *a2, unsigned short family)
  21. {
  22. switch (family) {
  23. case NFPROTO_IPV4:
  24. return ((a1->ip ^ a2->ip) & m->ip) == 0;
  25. case NFPROTO_IPV6:
  26. return ipv6_masked_addr_cmp(&a1->in6, &m->in6, &a2->in6) == 0;
  27. }
  28. return false;
  29. }
  30. static bool
  31. match_xfrm_state(const struct xfrm_state *x, const struct xt_policy_elem *e,
  32. unsigned short family)
  33. {
  34. #define MATCH_ADDR(x,y,z) (!e->match.x || \
  35. (xt_addr_cmp(&e->x, &e->y, (const union nf_inet_addr *)(z), family) \
  36. ^ e->invert.x))
  37. #define MATCH(x,y) (!e->match.x || ((e->x == (y)) ^ e->invert.x))
  38. return MATCH_ADDR(saddr, smask, &x->props.saddr) &&
  39. MATCH_ADDR(daddr, dmask, &x->id.daddr) &&
  40. MATCH(proto, x->id.proto) &&
  41. MATCH(mode, x->props.mode) &&
  42. MATCH(spi, x->id.spi) &&
  43. MATCH(reqid, x->props.reqid);
  44. }
  45. static int
  46. match_policy_in(const struct sk_buff *skb, const struct xt_policy_info *info,
  47. unsigned short family)
  48. {
  49. const struct xt_policy_elem *e;
  50. const struct sec_path *sp = skb_sec_path(skb);
  51. int strict = info->flags & XT_POLICY_MATCH_STRICT;
  52. int i, pos;
  53. if (sp == NULL)
  54. return -1;
  55. if (strict && info->len != sp->len)
  56. return 0;
  57. for (i = sp->len - 1; i >= 0; i--) {
  58. pos = strict ? i - sp->len + 1 : 0;
  59. if (pos >= info->len)
  60. return 0;
  61. e = &info->pol[pos];
  62. if (match_xfrm_state(sp->xvec[i], e, family)) {
  63. if (!strict)
  64. return 1;
  65. } else if (strict)
  66. return 0;
  67. }
  68. return strict ? 1 : 0;
  69. }
  70. static int
  71. match_policy_out(const struct sk_buff *skb, const struct xt_policy_info *info,
  72. unsigned short family)
  73. {
  74. const struct xt_policy_elem *e;
  75. const struct dst_entry *dst = skb_dst(skb);
  76. int strict = info->flags & XT_POLICY_MATCH_STRICT;
  77. int i, pos;
  78. if (dst->xfrm == NULL)
  79. return -1;
  80. for (i = 0; dst && dst->xfrm;
  81. dst = ((struct 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 bool
  95. policy_mt(const struct sk_buff *skb, struct xt_action_param *par)
  96. {
  97. const struct xt_policy_info *info = par->matchinfo;
  98. int ret;
  99. if (info->flags & XT_POLICY_MATCH_IN)
  100. ret = match_policy_in(skb, info, xt_family(par));
  101. else
  102. ret = match_policy_out(skb, info, xt_family(par));
  103. if (ret < 0)
  104. ret = info->flags & XT_POLICY_MATCH_NONE ? true : false;
  105. else if (info->flags & XT_POLICY_MATCH_NONE)
  106. ret = false;
  107. return ret;
  108. }
  109. static int policy_mt_check(const struct xt_mtchk_param *par)
  110. {
  111. const struct xt_policy_info *info = par->matchinfo;
  112. const char *errmsg = "neither incoming nor outgoing policy selected";
  113. if (!(info->flags & (XT_POLICY_MATCH_IN|XT_POLICY_MATCH_OUT)))
  114. goto err;
  115. if (par->hook_mask & ((1 << NF_INET_PRE_ROUTING) |
  116. (1 << NF_INET_LOCAL_IN)) && info->flags & XT_POLICY_MATCH_OUT) {
  117. errmsg = "output policy not valid in PREROUTING and INPUT";
  118. goto err;
  119. }
  120. if (par->hook_mask & ((1 << NF_INET_POST_ROUTING) |
  121. (1 << NF_INET_LOCAL_OUT)) && info->flags & XT_POLICY_MATCH_IN) {
  122. errmsg = "input policy not valid in POSTROUTING and OUTPUT";
  123. goto err;
  124. }
  125. if (info->len > XT_POLICY_MAX_ELEM) {
  126. errmsg = "too many policy elements";
  127. goto err;
  128. }
  129. return 0;
  130. err:
  131. pr_info_ratelimited("%s\n", errmsg);
  132. return -EINVAL;
  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");