]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/netfilter/xt_policy.c
[IPSEC]: Kill unused decap state argument
[net-next-2.6.git] / net / netfilter / xt_policy.c
CommitLineData
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>
11#include <linux/config.h>
12#include <linux/module.h>
13#include <linux/skbuff.h>
14#include <linux/init.h>
15#include <net/xfrm.h>
16
17#include <linux/netfilter/xt_policy.h>
18#include <linux/netfilter/x_tables.h>
19
20MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
21MODULE_DESCRIPTION("Xtables IPsec policy matching module");
22MODULE_LICENSE("GPL");
23
24static inline int
25xt_addr_cmp(const union xt_policy_addr *a1, const union xt_policy_addr *m,
26 const union xt_policy_addr *a2, unsigned short family)
27{
28 switch (family) {
29 case AF_INET:
81fbfd69 30 return !((a1->a4.s_addr ^ a2->a4.s_addr) & m->a4.s_addr);
c4b88513 31 case AF_INET6:
81fbfd69 32 return !ipv6_masked_addr_cmp(&a1->a6, &m->a6, &a2->a6);
c4b88513
PM
33 }
34 return 0;
35}
36
37static inline int
38match_xfrm_state(struct xfrm_state *x, const struct xt_policy_elem *e,
39 unsigned short family)
40{
41#define MATCH_ADDR(x,y,z) (!e->match.x || \
42 (xt_addr_cmp(&e->x, &e->y, z, family) \
43 ^ e->invert.x))
44#define MATCH(x,y) (!e->match.x || ((e->x == (y)) ^ e->invert.x))
45
46 return MATCH_ADDR(saddr, smask, (union xt_policy_addr *)&x->props.saddr) &&
81fbfd69 47 MATCH_ADDR(daddr, dmask, (union xt_policy_addr *)&x->id.daddr) &&
c4b88513
PM
48 MATCH(proto, x->id.proto) &&
49 MATCH(mode, x->props.mode) &&
50 MATCH(spi, x->id.spi) &&
51 MATCH(reqid, x->props.reqid);
52}
53
54static int
55match_policy_in(const struct sk_buff *skb, const struct xt_policy_info *info,
56 unsigned short family)
57{
58 const struct xt_policy_elem *e;
59 struct sec_path *sp = skb->sp;
60 int strict = info->flags & XT_POLICY_MATCH_STRICT;
61 int i, pos;
62
63 if (sp == NULL)
64 return -1;
65 if (strict && info->len != sp->len)
66 return 0;
67
68 for (i = sp->len - 1; i >= 0; i--) {
69 pos = strict ? i - sp->len + 1 : 0;
70 if (pos >= info->len)
71 return 0;
72 e = &info->pol[pos];
73
74 if (match_xfrm_state(sp->x[i].xvec, e, family)) {
75 if (!strict)
76 return 1;
77 } else if (strict)
78 return 0;
79 }
80
81 return strict ? 1 : 0;
82}
83
84static int
85match_policy_out(const struct sk_buff *skb, const struct xt_policy_info *info,
86 unsigned short family)
87{
88 const struct xt_policy_elem *e;
89 struct dst_entry *dst = skb->dst;
90 int strict = info->flags & XT_POLICY_MATCH_STRICT;
91 int i, pos;
92
93 if (dst->xfrm == NULL)
94 return -1;
95
96 for (i = 0; dst && dst->xfrm; dst = dst->child, i++) {
97 pos = strict ? i : 0;
98 if (pos >= info->len)
99 return 0;
100 e = &info->pol[pos];
101
102 if (match_xfrm_state(dst->xfrm, e, family)) {
103 if (!strict)
104 return 1;
105 } else if (strict)
106 return 0;
107 }
108
109 return strict ? i == info->len : 0;
110}
111
112static int match(const struct sk_buff *skb,
113 const struct net_device *in,
114 const struct net_device *out,
115 const struct xt_match *match,
116 const void *matchinfo,
117 int offset,
118 unsigned int protoff,
119 int *hotdrop)
120{
121 const struct xt_policy_info *info = matchinfo;
122 int ret;
123
124 if (info->flags & XT_POLICY_MATCH_IN)
125 ret = match_policy_in(skb, info, match->family);
126 else
127 ret = match_policy_out(skb, info, match->family);
128
129 if (ret < 0)
130 ret = info->flags & XT_POLICY_MATCH_NONE ? 1 : 0;
131 else if (info->flags & XT_POLICY_MATCH_NONE)
132 ret = 0;
133
134 return ret;
135}
136
137static int checkentry(const char *tablename, const void *ip_void,
138 const struct xt_match *match,
139 void *matchinfo, unsigned int matchsize,
140 unsigned int hook_mask)
141{
142 struct xt_policy_info *info = matchinfo;
143
144 if (!(info->flags & (XT_POLICY_MATCH_IN|XT_POLICY_MATCH_OUT))) {
145 printk(KERN_ERR "xt_policy: neither incoming nor "
146 "outgoing policy selected\n");
147 return 0;
148 }
149 /* hook values are equal for IPv4 and IPv6 */
150 if (hook_mask & (1 << NF_IP_PRE_ROUTING | 1 << NF_IP_LOCAL_IN)
151 && info->flags & XT_POLICY_MATCH_OUT) {
152 printk(KERN_ERR "xt_policy: output policy not valid in "
153 "PRE_ROUTING and INPUT\n");
154 return 0;
155 }
156 if (hook_mask & (1 << NF_IP_POST_ROUTING | 1 << NF_IP_LOCAL_OUT)
157 && info->flags & XT_POLICY_MATCH_IN) {
158 printk(KERN_ERR "xt_policy: input policy not valid in "
159 "POST_ROUTING and OUTPUT\n");
160 return 0;
161 }
162 if (info->len > XT_POLICY_MAX_ELEM) {
163 printk(KERN_ERR "xt_policy: too many policy elements\n");
164 return 0;
165 }
166 return 1;
167}
168
169static struct xt_match policy_match = {
170 .name = "policy",
171 .family = AF_INET,
172 .match = match,
173 .matchsize = sizeof(struct xt_policy_info),
174 .checkentry = checkentry,
a45049c5 175 .family = AF_INET,
c4b88513
PM
176 .me = THIS_MODULE,
177};
178
179static struct xt_match policy6_match = {
180 .name = "policy",
181 .family = AF_INET6,
182 .match = match,
183 .matchsize = sizeof(struct xt_policy_info),
184 .checkentry = checkentry,
a45049c5 185 .family = AF_INET6,
c4b88513
PM
186 .me = THIS_MODULE,
187};
188
189static int __init init(void)
190{
191 int ret;
192
a45049c5 193 ret = xt_register_match(&policy_match);
c4b88513
PM
194 if (ret)
195 return ret;
a45049c5 196 ret = xt_register_match(&policy6_match);
c4b88513 197 if (ret)
a45049c5 198 xt_unregister_match(&policy_match);
c4b88513
PM
199 return ret;
200}
201
202static void __exit fini(void)
203{
a45049c5
PNA
204 xt_unregister_match(&policy6_match);
205 xt_unregister_match(&policy_match);
c4b88513
PM
206}
207
208module_init(init);
209module_exit(fini);
210MODULE_ALIAS("ipt_policy");
211MODULE_ALIAS("ip6t_policy");