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_ipv6.h>
18 #include <linux/netfilter_ipv6/ip6_tables.h>
19 #include <linux/netfilter_ipv6/ip6t_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 ip6t_policy_elem *e)
29 #define MATCH_ADDR(x,y,z) (!e->match.x || \
30 ((ip6_masked_addrcmp((z), &e->x, &e->y)) == 0) ^ e->invert.x)
31 #define MATCH(x,y) (!e->match.x || ((e->x == (y)) ^ e->invert.x))
33 return MATCH_ADDR(saddr, smask, (struct in6_addr *)&x->props.saddr.a6) &&
34 MATCH_ADDR(daddr, dmask, (struct in6_addr *)&x->id.daddr.a6) &&
35 MATCH(proto, x->id.proto) &&
36 MATCH(mode, x->props.mode) &&
37 MATCH(spi, x->id.spi) &&
38 MATCH(reqid, x->props.reqid);
42 match_policy_in(const struct sk_buff *skb, const struct ip6t_policy_info *info)
44 const struct ip6t_policy_elem *e;
45 struct sec_path *sp = skb->sp;
46 int strict = info->flags & IP6T_POLICY_MATCH_STRICT;
51 if (strict && info->len != sp->len)
54 for (i = sp->len - 1; i >= 0; i--) {
55 pos = strict ? i - sp->len + 1 : 0;
60 if (match_xfrm_state(sp->x[i].xvec, e)) {
67 return strict ? 1 : 0;
71 match_policy_out(const struct sk_buff *skb, const struct ip6t_policy_info *info)
73 const struct ip6t_policy_elem *e;
74 struct dst_entry *dst = skb->dst;
75 int strict = info->flags & IP6T_POLICY_MATCH_STRICT;
78 if (dst->xfrm == NULL)
81 for (i = 0; dst && dst->xfrm; dst = dst->child, i++) {
87 if (match_xfrm_state(dst->xfrm, e)) {
94 return strict ? 1 : 0;
97 static int match(const struct sk_buff *skb,
98 const struct net_device *in,
99 const struct net_device *out,
100 const void *matchinfo,
102 unsigned int protoff,
105 const struct ip6t_policy_info *info = matchinfo;
108 if (info->flags & IP6T_POLICY_MATCH_IN)
109 ret = match_policy_in(skb, info);
111 ret = match_policy_out(skb, info);
114 ret = info->flags & IP6T_POLICY_MATCH_NONE ? 1 : 0;
115 else if (info->flags & IP6T_POLICY_MATCH_NONE)
121 static int checkentry(const char *tablename, const void *ip_void,
122 void *matchinfo, unsigned int matchsize,
123 unsigned int hook_mask)
125 struct ip6t_policy_info *info = matchinfo;
127 if (matchsize != IP6T_ALIGN(sizeof(*info))) {
128 printk(KERN_ERR "ip6t_policy: matchsize %u != %zu\n",
129 matchsize, IP6T_ALIGN(sizeof(*info)));
132 if (!(info->flags & (IP6T_POLICY_MATCH_IN|IP6T_POLICY_MATCH_OUT))) {
133 printk(KERN_ERR "ip6t_policy: neither incoming nor "
134 "outgoing policy selected\n");
137 if (hook_mask & (1 << NF_IP6_PRE_ROUTING | 1 << NF_IP6_LOCAL_IN)
138 && info->flags & IP6T_POLICY_MATCH_OUT) {
139 printk(KERN_ERR "ip6t_policy: output policy not valid in "
140 "PRE_ROUTING and INPUT\n");
143 if (hook_mask & (1 << NF_IP6_POST_ROUTING | 1 << NF_IP6_LOCAL_OUT)
144 && info->flags & IP6T_POLICY_MATCH_IN) {
145 printk(KERN_ERR "ip6t_policy: input policy not valid in "
146 "POST_ROUTING and OUTPUT\n");
149 if (info->len > IP6T_POLICY_MAX_ELEM) {
150 printk(KERN_ERR "ip6t_policy: too many policy elements\n");
157 static struct ip6t_match policy_match = {
160 .checkentry = checkentry,
164 static int __init init(void)
166 return ip6t_register_match(&policy_match);
169 static void __exit fini(void)
171 ip6t_unregister_match(&policy_match);