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 | ||
23 | static inline int | |
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 PM |
32 | } |
33 | return 0; | |
34 | } | |
35 | ||
36 | static inline int | |
37 | match_xfrm_state(struct xfrm_state *x, const struct xt_policy_elem *e, | |
38 | unsigned short family) | |
39 | { | |
40 | #define MATCH_ADDR(x,y,z) (!e->match.x || \ | |
41 | (xt_addr_cmp(&e->x, &e->y, z, family) \ | |
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; | |
58 | struct sec_path *sp = skb->sp; | |
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; | |
88 | struct dst_entry *dst = skb->dst; | |
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 | ||
111 | static int match(const struct sk_buff *skb, | |
112 | const struct net_device *in, | |
113 | const struct net_device *out, | |
114 | const struct xt_match *match, | |
115 | const void *matchinfo, | |
116 | int offset, | |
117 | unsigned int protoff, | |
118 | int *hotdrop) | |
119 | { | |
120 | const struct xt_policy_info *info = matchinfo; | |
121 | int ret; | |
122 | ||
123 | if (info->flags & XT_POLICY_MATCH_IN) | |
124 | ret = match_policy_in(skb, info, match->family); | |
125 | else | |
126 | ret = match_policy_out(skb, info, match->family); | |
127 | ||
128 | if (ret < 0) | |
129 | ret = info->flags & XT_POLICY_MATCH_NONE ? 1 : 0; | |
130 | else if (info->flags & XT_POLICY_MATCH_NONE) | |
131 | ret = 0; | |
132 | ||
133 | return ret; | |
134 | } | |
135 | ||
136 | static int checkentry(const char *tablename, const void *ip_void, | |
137 | const struct xt_match *match, | |
138 | void *matchinfo, unsigned int matchsize, | |
139 | unsigned int hook_mask) | |
140 | { | |
141 | struct xt_policy_info *info = matchinfo; | |
142 | ||
143 | if (!(info->flags & (XT_POLICY_MATCH_IN|XT_POLICY_MATCH_OUT))) { | |
144 | printk(KERN_ERR "xt_policy: neither incoming nor " | |
145 | "outgoing policy selected\n"); | |
146 | return 0; | |
147 | } | |
148 | /* hook values are equal for IPv4 and IPv6 */ | |
149 | if (hook_mask & (1 << NF_IP_PRE_ROUTING | 1 << NF_IP_LOCAL_IN) | |
150 | && info->flags & XT_POLICY_MATCH_OUT) { | |
151 | printk(KERN_ERR "xt_policy: output policy not valid in " | |
152 | "PRE_ROUTING and INPUT\n"); | |
153 | return 0; | |
154 | } | |
155 | if (hook_mask & (1 << NF_IP_POST_ROUTING | 1 << NF_IP_LOCAL_OUT) | |
156 | && info->flags & XT_POLICY_MATCH_IN) { | |
157 | printk(KERN_ERR "xt_policy: input policy not valid in " | |
158 | "POST_ROUTING and OUTPUT\n"); | |
159 | return 0; | |
160 | } | |
161 | if (info->len > XT_POLICY_MAX_ELEM) { | |
162 | printk(KERN_ERR "xt_policy: too many policy elements\n"); | |
163 | return 0; | |
164 | } | |
165 | return 1; | |
166 | } | |
167 | ||
168 | static struct xt_match policy_match = { | |
169 | .name = "policy", | |
170 | .family = AF_INET, | |
171 | .match = match, | |
172 | .matchsize = sizeof(struct xt_policy_info), | |
173 | .checkentry = checkentry, | |
a45049c5 | 174 | .family = AF_INET, |
c4b88513 PM |
175 | .me = THIS_MODULE, |
176 | }; | |
177 | ||
178 | static struct xt_match policy6_match = { | |
179 | .name = "policy", | |
180 | .family = AF_INET6, | |
181 | .match = match, | |
182 | .matchsize = sizeof(struct xt_policy_info), | |
183 | .checkentry = checkentry, | |
a45049c5 | 184 | .family = AF_INET6, |
c4b88513 PM |
185 | .me = THIS_MODULE, |
186 | }; | |
187 | ||
188 | static int __init init(void) | |
189 | { | |
190 | int ret; | |
191 | ||
a45049c5 | 192 | ret = xt_register_match(&policy_match); |
c4b88513 PM |
193 | if (ret) |
194 | return ret; | |
a45049c5 | 195 | ret = xt_register_match(&policy6_match); |
c4b88513 | 196 | if (ret) |
a45049c5 | 197 | xt_unregister_match(&policy_match); |
c4b88513 PM |
198 | return ret; |
199 | } | |
200 | ||
201 | static void __exit fini(void) | |
202 | { | |
a45049c5 PNA |
203 | xt_unregister_match(&policy6_match); |
204 | xt_unregister_match(&policy_match); | |
c4b88513 PM |
205 | } |
206 | ||
207 | module_init(init); | |
208 | module_exit(fini); | |
209 | MODULE_ALIAS("ipt_policy"); | |
210 | MODULE_ALIAS("ip6t_policy"); |