1 /* IP tables module for matching IPsec policy
3 * Copyright (c) 2004,2005 Patrick McHardy, <kaber@trash.net>
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.
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/skbuff.h>
13 #include <linux/init.h>
16 #include <linux/netfilter/xt_policy.h>
17 #include <linux/netfilter/x_tables.h>
19 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
20 MODULE_DESCRIPTION("Xtables IPsec policy matching module");
21 MODULE_LICENSE("GPL");
24 xt_addr_cmp(const union xt_policy_addr *a1, const union xt_policy_addr *m,
25 const union xt_policy_addr *a2, unsigned short family)
29 return !((a1->a4.s_addr ^ a2->a4.s_addr) & m->a4.s_addr);
31 return !ipv6_masked_addr_cmp(&a1->a6, &m->a6, &a2->a6);
37 match_xfrm_state(struct xfrm_state *x, const struct xt_policy_elem *e,
38 unsigned short family)
40 #define MATCH_ADDR(x,y,z) (!e->match.x || \
41 (xt_addr_cmp(&e->x, &e->y, z, family) \
43 #define MATCH(x,y) (!e->match.x || ((e->x == (y)) ^ e->invert.x))
45 return MATCH_ADDR(saddr, smask, (union xt_policy_addr *)&x->props.saddr) &&
46 MATCH_ADDR(daddr, dmask, (union xt_policy_addr *)&x->id.daddr) &&
47 MATCH(proto, x->id.proto) &&
48 MATCH(mode, x->props.mode) &&
49 MATCH(spi, x->id.spi) &&
50 MATCH(reqid, x->props.reqid);
54 match_policy_in(const struct sk_buff *skb, const struct xt_policy_info *info,
55 unsigned short family)
57 const struct xt_policy_elem *e;
58 struct sec_path *sp = skb->sp;
59 int strict = info->flags & XT_POLICY_MATCH_STRICT;
64 if (strict && info->len != sp->len)
67 for (i = sp->len - 1; i >= 0; i--) {
68 pos = strict ? i - sp->len + 1 : 0;
73 if (match_xfrm_state(sp->xvec[i], e, family)) {
80 return strict ? 1 : 0;
84 match_policy_out(const struct sk_buff *skb, const struct xt_policy_info *info,
85 unsigned short family)
87 const struct xt_policy_elem *e;
88 struct dst_entry *dst = skb->dst;
89 int strict = info->flags & XT_POLICY_MATCH_STRICT;
92 if (dst->xfrm == NULL)
95 for (i = 0; dst && dst->xfrm; dst = dst->child, i++) {
101 if (match_xfrm_state(dst->xfrm, e, family)) {
108 return strict ? i == info->len : 0;
111 static int match(const struct sk_buff *skb,
112 const struct net_device *in,
113 const struct net_device *out,
114 const struct xt_match *match,
115 const void *matchinfo,
117 unsigned int protoff,
120 const struct xt_policy_info *info = matchinfo;
123 if (info->flags & XT_POLICY_MATCH_IN)
124 ret = match_policy_in(skb, info, match->family);
126 ret = match_policy_out(skb, info, match->family);
129 ret = info->flags & XT_POLICY_MATCH_NONE ? 1 : 0;
130 else if (info->flags & XT_POLICY_MATCH_NONE)
136 static int checkentry(const char *tablename, const void *ip_void,
137 const struct xt_match *match,
138 void *matchinfo, unsigned int hook_mask)
140 struct xt_policy_info *info = matchinfo;
142 if (!(info->flags & (XT_POLICY_MATCH_IN|XT_POLICY_MATCH_OUT))) {
143 printk(KERN_ERR "xt_policy: neither incoming nor "
144 "outgoing policy selected\n");
147 /* hook values are equal for IPv4 and IPv6 */
148 if (hook_mask & (1 << NF_IP_PRE_ROUTING | 1 << NF_IP_LOCAL_IN)
149 && info->flags & XT_POLICY_MATCH_OUT) {
150 printk(KERN_ERR "xt_policy: output policy not valid in "
151 "PRE_ROUTING and INPUT\n");
154 if (hook_mask & (1 << NF_IP_POST_ROUTING | 1 << NF_IP_LOCAL_OUT)
155 && info->flags & XT_POLICY_MATCH_IN) {
156 printk(KERN_ERR "xt_policy: input policy not valid in "
157 "POST_ROUTING and OUTPUT\n");
160 if (info->len > XT_POLICY_MAX_ELEM) {
161 printk(KERN_ERR "xt_policy: too many policy elements\n");
167 static struct xt_match xt_policy_match[] = {
171 .checkentry = checkentry,
173 .matchsize = sizeof(struct xt_policy_info),
179 .checkentry = checkentry,
181 .matchsize = sizeof(struct xt_policy_info),
186 static int __init init(void)
188 return xt_register_matches(xt_policy_match,
189 ARRAY_SIZE(xt_policy_match));
192 static void __exit fini(void)
194 xt_unregister_matches(xt_policy_match, ARRAY_SIZE(xt_policy_match));
199 MODULE_ALIAS("ipt_policy");
200 MODULE_ALIAS("ip6t_policy");