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/config.h>
12 #include <linux/module.h>
13 #include <linux/skbuff.h>
14 #include <linux/init.h>
17 #include <linux/netfilter_ipv4.h>
18 #include <linux/netfilter_ipv4/ip_tables.h>
19 #include <linux/netfilter_ipv4/ipt_policy.h>
21 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
22 MODULE_DESCRIPTION("IPtables IPsec policy matching module");
23 MODULE_LICENSE("GPL");
27 match_xfrm_state(struct xfrm_state *x, const struct ipt_policy_elem *e)
29 #define MATCH(x,y) (!e->match.x || ((e->x == (y)) ^ e->invert.x))
31 return MATCH(saddr, x->props.saddr.a4 & e->smask) &&
32 MATCH(daddr, x->id.daddr.a4 & e->dmask) &&
33 MATCH(proto, x->id.proto) &&
34 MATCH(mode, x->props.mode) &&
35 MATCH(spi, x->id.spi) &&
36 MATCH(reqid, x->props.reqid);
40 match_policy_in(const struct sk_buff *skb, const struct ipt_policy_info *info)
42 const struct ipt_policy_elem *e;
43 struct sec_path *sp = skb->sp;
44 int strict = info->flags & IPT_POLICY_MATCH_STRICT;
49 if (strict && info->len != sp->len)
52 for (i = sp->len - 1; i >= 0; i--) {
53 pos = strict ? i - sp->len + 1 : 0;
58 if (match_xfrm_state(sp->x[i].xvec, e)) {
65 return strict ? 1 : 0;
69 match_policy_out(const struct sk_buff *skb, const struct ipt_policy_info *info)
71 const struct ipt_policy_elem *e;
72 struct dst_entry *dst = skb->dst;
73 int strict = info->flags & IPT_POLICY_MATCH_STRICT;
76 if (dst->xfrm == NULL)
79 for (i = 0; dst && dst->xfrm; dst = dst->child, i++) {
85 if (match_xfrm_state(dst->xfrm, e)) {
92 return strict ? 1 : 0;
95 static int match(const struct sk_buff *skb,
96 const struct net_device *in,
97 const struct net_device *out,
98 const void *matchinfo,
100 unsigned int protoff,
103 const struct ipt_policy_info *info = matchinfo;
106 if (info->flags & IPT_POLICY_MATCH_IN)
107 ret = match_policy_in(skb, info);
109 ret = match_policy_out(skb, info);
112 ret = info->flags & IPT_POLICY_MATCH_NONE ? 1 : 0;
113 else if (info->flags & IPT_POLICY_MATCH_NONE)
119 static int checkentry(const char *tablename, const void *ip_void,
120 void *matchinfo, unsigned int matchsize,
121 unsigned int hook_mask)
123 struct ipt_policy_info *info = matchinfo;
125 if (matchsize != IPT_ALIGN(sizeof(*info))) {
126 printk(KERN_ERR "ipt_policy: matchsize %u != %zu\n",
127 matchsize, IPT_ALIGN(sizeof(*info)));
130 if (!(info->flags & (IPT_POLICY_MATCH_IN|IPT_POLICY_MATCH_OUT))) {
131 printk(KERN_ERR "ipt_policy: neither incoming nor "
132 "outgoing policy selected\n");
135 if (hook_mask & (1 << NF_IP_PRE_ROUTING | 1 << NF_IP_LOCAL_IN)
136 && info->flags & IPT_POLICY_MATCH_OUT) {
137 printk(KERN_ERR "ipt_policy: output policy not valid in "
138 "PRE_ROUTING and INPUT\n");
141 if (hook_mask & (1 << NF_IP_POST_ROUTING | 1 << NF_IP_LOCAL_OUT)
142 && info->flags & IPT_POLICY_MATCH_IN) {
143 printk(KERN_ERR "ipt_policy: input policy not valid in "
144 "POST_ROUTING and OUTPUT\n");
147 if (info->len > IPT_POLICY_MAX_ELEM) {
148 printk(KERN_ERR "ipt_policy: too many policy elements\n");
155 static struct ipt_match policy_match = {
158 .checkentry = checkentry,
162 static int __init init(void)
164 return ipt_register_match(&policy_match);
167 static void __exit fini(void)
169 ipt_unregister_match(&policy_match);