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(&e->x.a6, &e->y.a6, z)) \
32 #define MATCH(x,y) (!e->match.x || ((e->x == (y)) ^ e->invert.x))
34 return MATCH_ADDR(saddr, smask, (struct in6_addr *)&x->props.saddr.a6) &&
35 MATCH_ADDR(daddr, dmask, (struct in6_addr *)&x->id.daddr.a6) &&
36 MATCH(proto, x->id.proto) &&
37 MATCH(mode, x->props.mode) &&
38 MATCH(spi, x->id.spi) &&
39 MATCH(reqid, x->props.reqid);
43 match_policy_in(const struct sk_buff *skb, const struct ip6t_policy_info *info)
45 const struct ip6t_policy_elem *e;
46 struct sec_path *sp = skb->sp;
47 int strict = info->flags & IP6T_POLICY_MATCH_STRICT;
52 if (strict && info->len != sp->len)
55 for (i = sp->len - 1; i >= 0; i--) {
56 pos = strict ? i - sp->len + 1 : 0;
61 if (match_xfrm_state(sp->x[i].xvec, e)) {
68 return strict ? 1 : 0;
72 match_policy_out(const struct sk_buff *skb, const struct ip6t_policy_info *info)
74 const struct ip6t_policy_elem *e;
75 struct dst_entry *dst = skb->dst;
76 int strict = info->flags & IP6T_POLICY_MATCH_STRICT;
79 if (dst->xfrm == NULL)
82 for (i = 0; dst && dst->xfrm; dst = dst->child, i++) {
88 if (match_xfrm_state(dst->xfrm, e)) {
95 return strict ? i == info->len : 0;
98 static int match(const struct sk_buff *skb,
99 const struct net_device *in,
100 const struct net_device *out,
101 const struct xt_match *match,
102 const void *matchinfo,
104 unsigned int protoff,
107 const struct ip6t_policy_info *info = matchinfo;
110 if (info->flags & IP6T_POLICY_MATCH_IN)
111 ret = match_policy_in(skb, info);
113 ret = match_policy_out(skb, info);
116 ret = info->flags & IP6T_POLICY_MATCH_NONE ? 1 : 0;
117 else if (info->flags & IP6T_POLICY_MATCH_NONE)
123 static int checkentry(const char *tablename, const void *ip_void,
124 const struct xt_match *match, void *matchinfo,
125 unsigned int matchsize, unsigned int hook_mask)
127 struct ip6t_policy_info *info = matchinfo;
129 if (!(info->flags & (IP6T_POLICY_MATCH_IN|IP6T_POLICY_MATCH_OUT))) {
130 printk(KERN_ERR "ip6t_policy: neither incoming nor "
131 "outgoing policy selected\n");
134 if (hook_mask & (1 << NF_IP6_PRE_ROUTING | 1 << NF_IP6_LOCAL_IN)
135 && info->flags & IP6T_POLICY_MATCH_OUT) {
136 printk(KERN_ERR "ip6t_policy: output policy not valid in "
137 "PRE_ROUTING and INPUT\n");
140 if (hook_mask & (1 << NF_IP6_POST_ROUTING | 1 << NF_IP6_LOCAL_OUT)
141 && info->flags & IP6T_POLICY_MATCH_IN) {
142 printk(KERN_ERR "ip6t_policy: input policy not valid in "
143 "POST_ROUTING and OUTPUT\n");
146 if (info->len > IP6T_POLICY_MAX_ELEM) {
147 printk(KERN_ERR "ip6t_policy: too many policy elements\n");
154 static struct ip6t_match policy_match = {
157 .matchsize = sizeof(struct ip6t_policy_info),
158 .checkentry = checkentry,
162 static int __init init(void)
164 return ip6t_register_match(&policy_match);
167 static void __exit fini(void)
169 ip6t_unregister_match(&policy_match);