Commit | Line | Data |
---|---|---|
c4b88513 PM |
1 | /* IP tables module for matching IPsec policy |
2 | * | |
3 | * Copyright (c) 2004,2005 Patrick McHardy, <kaber@trash.net> | |
4 | * | |
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. | |
8 | */ | |
9 | ||
10 | #include <linux/kernel.h> | |
c4b88513 PM |
11 | #include <linux/module.h> |
12 | #include <linux/skbuff.h> | |
13 | #include <linux/init.h> | |
14 | #include <net/xfrm.h> | |
15 | ||
16 | #include <linux/netfilter/xt_policy.h> | |
17 | #include <linux/netfilter/x_tables.h> | |
18 | ||
19 | MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); | |
20 | MODULE_DESCRIPTION("Xtables IPsec policy matching module"); | |
21 | MODULE_LICENSE("GPL"); | |
22 | ||
1d93a9cb | 23 | static inline bool |
c4b88513 PM |
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) | |
26 | { | |
27 | switch (family) { | |
28 | case AF_INET: | |
81fbfd69 | 29 | return !((a1->a4.s_addr ^ a2->a4.s_addr) & m->a4.s_addr); |
c4b88513 | 30 | case AF_INET6: |
81fbfd69 | 31 | return !ipv6_masked_addr_cmp(&a1->a6, &m->a6, &a2->a6); |
c4b88513 | 32 | } |
1d93a9cb | 33 | return false; |
c4b88513 PM |
34 | } |
35 | ||
1d93a9cb | 36 | static inline bool |
a47362a2 | 37 | match_xfrm_state(const struct xfrm_state *x, const struct xt_policy_elem *e, |
c4b88513 PM |
38 | unsigned short family) |
39 | { | |
40 | #define MATCH_ADDR(x,y,z) (!e->match.x || \ | |
e79ec50b | 41 | (xt_addr_cmp(&e->x, &e->y, (z), family) \ |
c4b88513 PM |
42 | ^ e->invert.x)) |
43 | #define MATCH(x,y) (!e->match.x || ((e->x == (y)) ^ e->invert.x)) | |
44 | ||
45 | return MATCH_ADDR(saddr, smask, (union xt_policy_addr *)&x->props.saddr) && | |
81fbfd69 | 46 | MATCH_ADDR(daddr, dmask, (union xt_policy_addr *)&x->id.daddr) && |
c4b88513 PM |
47 | MATCH(proto, x->id.proto) && |
48 | MATCH(mode, x->props.mode) && | |
49 | MATCH(spi, x->id.spi) && | |
50 | MATCH(reqid, x->props.reqid); | |
51 | } | |
52 | ||
53 | static int | |
54 | match_policy_in(const struct sk_buff *skb, const struct xt_policy_info *info, | |
55 | unsigned short family) | |
56 | { | |
57 | const struct xt_policy_elem *e; | |
a47362a2 | 58 | const struct sec_path *sp = skb->sp; |
c4b88513 PM |
59 | int strict = info->flags & XT_POLICY_MATCH_STRICT; |
60 | int i, pos; | |
61 | ||
62 | if (sp == NULL) | |
63 | return -1; | |
64 | if (strict && info->len != sp->len) | |
65 | return 0; | |
66 | ||
67 | for (i = sp->len - 1; i >= 0; i--) { | |
68 | pos = strict ? i - sp->len + 1 : 0; | |
69 | if (pos >= info->len) | |
70 | return 0; | |
71 | e = &info->pol[pos]; | |
72 | ||
dbe5b4aa | 73 | if (match_xfrm_state(sp->xvec[i], e, family)) { |
c4b88513 PM |
74 | if (!strict) |
75 | return 1; | |
76 | } else if (strict) | |
77 | return 0; | |
78 | } | |
79 | ||
80 | return strict ? 1 : 0; | |
81 | } | |
82 | ||
83 | static int | |
84 | match_policy_out(const struct sk_buff *skb, const struct xt_policy_info *info, | |
85 | unsigned short family) | |
86 | { | |
87 | const struct xt_policy_elem *e; | |
a47362a2 | 88 | const struct dst_entry *dst = skb->dst; |
c4b88513 PM |
89 | int strict = info->flags & XT_POLICY_MATCH_STRICT; |
90 | int i, pos; | |
91 | ||
92 | if (dst->xfrm == NULL) | |
93 | return -1; | |
94 | ||
95 | for (i = 0; dst && dst->xfrm; dst = dst->child, i++) { | |
96 | pos = strict ? i : 0; | |
97 | if (pos >= info->len) | |
98 | return 0; | |
99 | e = &info->pol[pos]; | |
100 | ||
101 | if (match_xfrm_state(dst->xfrm, e, family)) { | |
102 | if (!strict) | |
103 | return 1; | |
104 | } else if (strict) | |
105 | return 0; | |
106 | } | |
107 | ||
108 | return strict ? i == info->len : 0; | |
109 | } | |
110 | ||
d3c5ee6d JE |
111 | static bool |
112 | policy_mt(const struct sk_buff *skb, const struct net_device *in, | |
113 | const struct net_device *out, const struct xt_match *match, | |
114 | const void *matchinfo, int offset, unsigned int protoff, | |
115 | bool *hotdrop) | |
c4b88513 PM |
116 | { |
117 | const struct xt_policy_info *info = matchinfo; | |
118 | int ret; | |
119 | ||
120 | if (info->flags & XT_POLICY_MATCH_IN) | |
121 | ret = match_policy_in(skb, info, match->family); | |
122 | else | |
123 | ret = match_policy_out(skb, info, match->family); | |
124 | ||
125 | if (ret < 0) | |
1d93a9cb | 126 | ret = info->flags & XT_POLICY_MATCH_NONE ? true : false; |
c4b88513 | 127 | else if (info->flags & XT_POLICY_MATCH_NONE) |
1d93a9cb | 128 | ret = false; |
c4b88513 PM |
129 | |
130 | return ret; | |
131 | } | |
132 | ||
d3c5ee6d JE |
133 | static bool |
134 | policy_mt_check(const char *tablename, const void *ip_void, | |
135 | const struct xt_match *match, void *matchinfo, | |
136 | unsigned int hook_mask) | |
c4b88513 PM |
137 | { |
138 | struct xt_policy_info *info = matchinfo; | |
139 | ||
140 | if (!(info->flags & (XT_POLICY_MATCH_IN|XT_POLICY_MATCH_OUT))) { | |
141 | printk(KERN_ERR "xt_policy: neither incoming nor " | |
601e68e1 | 142 | "outgoing policy selected\n"); |
ccb79bdc | 143 | return false; |
c4b88513 | 144 | } |
6e23ae2a | 145 | if (hook_mask & (1 << NF_INET_PRE_ROUTING | 1 << NF_INET_LOCAL_IN) |
c4b88513 PM |
146 | && info->flags & XT_POLICY_MATCH_OUT) { |
147 | printk(KERN_ERR "xt_policy: output policy not valid in " | |
601e68e1 | 148 | "PRE_ROUTING and INPUT\n"); |
ccb79bdc | 149 | return false; |
c4b88513 | 150 | } |
6e23ae2a | 151 | if (hook_mask & (1 << NF_INET_POST_ROUTING | 1 << NF_INET_LOCAL_OUT) |
c4b88513 PM |
152 | && info->flags & XT_POLICY_MATCH_IN) { |
153 | printk(KERN_ERR "xt_policy: input policy not valid in " | |
601e68e1 | 154 | "POST_ROUTING and OUTPUT\n"); |
ccb79bdc | 155 | return false; |
c4b88513 PM |
156 | } |
157 | if (info->len > XT_POLICY_MAX_ELEM) { | |
158 | printk(KERN_ERR "xt_policy: too many policy elements\n"); | |
ccb79bdc | 159 | return false; |
c4b88513 | 160 | } |
ccb79bdc | 161 | return true; |
c4b88513 PM |
162 | } |
163 | ||
d3c5ee6d | 164 | static struct xt_match policy_mt_reg[] __read_mostly = { |
4470bbc7 PM |
165 | { |
166 | .name = "policy", | |
167 | .family = AF_INET, | |
d3c5ee6d JE |
168 | .checkentry = policy_mt_check, |
169 | .match = policy_mt, | |
4470bbc7 | 170 | .matchsize = sizeof(struct xt_policy_info), |
4470bbc7 PM |
171 | .me = THIS_MODULE, |
172 | }, | |
173 | { | |
174 | .name = "policy", | |
175 | .family = AF_INET6, | |
d3c5ee6d JE |
176 | .checkentry = policy_mt_check, |
177 | .match = policy_mt, | |
4470bbc7 | 178 | .matchsize = sizeof(struct xt_policy_info), |
4470bbc7 PM |
179 | .me = THIS_MODULE, |
180 | }, | |
c4b88513 PM |
181 | }; |
182 | ||
d3c5ee6d | 183 | static int __init policy_mt_init(void) |
c4b88513 | 184 | { |
d3c5ee6d | 185 | return xt_register_matches(policy_mt_reg, ARRAY_SIZE(policy_mt_reg)); |
c4b88513 PM |
186 | } |
187 | ||
d3c5ee6d | 188 | static void __exit policy_mt_exit(void) |
c4b88513 | 189 | { |
d3c5ee6d | 190 | xt_unregister_matches(policy_mt_reg, ARRAY_SIZE(policy_mt_reg)); |
c4b88513 PM |
191 | } |
192 | ||
d3c5ee6d JE |
193 | module_init(policy_mt_init); |
194 | module_exit(policy_mt_exit); | |
c4b88513 PM |
195 | MODULE_ALIAS("ipt_policy"); |
196 | MODULE_ALIAS("ip6t_policy"); |