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.h>
17 #include <linux/netfilter/xt_policy.h>
18 #include <linux/netfilter/x_tables.h>
20 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
21 MODULE_DESCRIPTION("Xtables: IPsec policy match");
22 MODULE_LICENSE("GPL");
25 xt_addr_cmp(const union nf_inet_addr
*a1
, const union nf_inet_addr
*m
,
26 const union nf_inet_addr
*a2
, unsigned short family
)
30 return ((a1
->ip
^ a2
->ip
) & m
->ip
) == 0;
32 return ipv6_masked_addr_cmp(&a1
->in6
, &m
->in6
, &a2
->in6
) == 0;
38 match_xfrm_state(const struct xfrm_state
*x
, const struct xt_policy_elem
*e
,
39 unsigned short family
)
41 #define MATCH_ADDR(x,y,z) (!e->match.x || \
42 (xt_addr_cmp(&e->x, &e->y, (const union nf_inet_addr *)(z), family) \
44 #define MATCH(x,y) (!e->match.x || ((e->x == (y)) ^ e->invert.x))
46 return MATCH_ADDR(saddr
, smask
, &x
->props
.saddr
) &&
47 MATCH_ADDR(daddr
, dmask
, &x
->id
.daddr
) &&
48 MATCH(proto
, x
->id
.proto
) &&
49 MATCH(mode
, x
->props
.mode
) &&
50 MATCH(spi
, x
->id
.spi
) &&
51 MATCH(reqid
, x
->props
.reqid
);
55 match_policy_in(const struct sk_buff
*skb
, const struct xt_policy_info
*info
,
56 unsigned short family
)
58 const struct xt_policy_elem
*e
;
59 const struct sec_path
*sp
= skb
->sp
;
60 int strict
= info
->flags
& XT_POLICY_MATCH_STRICT
;
65 if (strict
&& info
->len
!= sp
->len
)
68 for (i
= sp
->len
- 1; i
>= 0; i
--) {
69 pos
= strict
? i
- sp
->len
+ 1 : 0;
74 if (match_xfrm_state(sp
->xvec
[i
], e
, family
)) {
81 return strict
? 1 : 0;
85 match_policy_out(const struct sk_buff
*skb
, const struct xt_policy_info
*info
,
86 unsigned short family
)
88 const struct xt_policy_elem
*e
;
89 const struct dst_entry
*dst
= skb_dst(skb
);
90 int strict
= info
->flags
& XT_POLICY_MATCH_STRICT
;
93 if (dst
->xfrm
== NULL
)
96 for (i
= 0; dst
&& dst
->xfrm
; dst
= dst
->child
, i
++) {
102 if (match_xfrm_state(dst
->xfrm
, e
, family
)) {
109 return strict
? i
== info
->len
: 0;
113 policy_mt(const struct sk_buff
*skb
, const struct xt_match_param
*par
)
115 const struct xt_policy_info
*info
= par
->matchinfo
;
118 if (info
->flags
& XT_POLICY_MATCH_IN
)
119 ret
= match_policy_in(skb
, info
, par
->match
->family
);
121 ret
= match_policy_out(skb
, info
, par
->match
->family
);
124 ret
= info
->flags
& XT_POLICY_MATCH_NONE
? true : false;
125 else if (info
->flags
& XT_POLICY_MATCH_NONE
)
131 static bool policy_mt_check(const struct xt_mtchk_param
*par
)
133 const struct xt_policy_info
*info
= par
->matchinfo
;
135 if (!(info
->flags
& (XT_POLICY_MATCH_IN
|XT_POLICY_MATCH_OUT
))) {
136 printk(KERN_ERR
"xt_policy: neither incoming nor "
137 "outgoing policy selected\n");
140 if (par
->hook_mask
& ((1 << NF_INET_PRE_ROUTING
) |
141 (1 << NF_INET_LOCAL_IN
)) && info
->flags
& XT_POLICY_MATCH_OUT
) {
142 printk(KERN_ERR
"xt_policy: output policy not valid in "
143 "PRE_ROUTING and INPUT\n");
146 if (par
->hook_mask
& ((1 << NF_INET_POST_ROUTING
) |
147 (1 << NF_INET_LOCAL_OUT
)) && info
->flags
& XT_POLICY_MATCH_IN
) {
148 printk(KERN_ERR
"xt_policy: input policy not valid in "
149 "POST_ROUTING and OUTPUT\n");
152 if (info
->len
> XT_POLICY_MAX_ELEM
) {
153 printk(KERN_ERR
"xt_policy: too many policy elements\n");
159 static struct xt_match policy_mt_reg
[] __read_mostly
= {
162 .family
= NFPROTO_IPV4
,
163 .checkentry
= policy_mt_check
,
165 .matchsize
= sizeof(struct xt_policy_info
),
170 .family
= NFPROTO_IPV6
,
171 .checkentry
= policy_mt_check
,
173 .matchsize
= sizeof(struct xt_policy_info
),
178 static int __init
policy_mt_init(void)
180 return xt_register_matches(policy_mt_reg
, ARRAY_SIZE(policy_mt_reg
));
183 static void __exit
policy_mt_exit(void)
185 xt_unregister_matches(policy_mt_reg
, ARRAY_SIZE(policy_mt_reg
));
188 module_init(policy_mt_init
);
189 module_exit(policy_mt_exit
);
190 MODULE_ALIAS("ipt_policy");
191 MODULE_ALIAS("ip6t_policy");
This page took 0.034347 seconds and 5 git commands to generate.