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 | ||
917b6fbd | 16 | #include <linux/netfilter.h> |
c4b88513 PM |
17 | #include <linux/netfilter/xt_policy.h> |
18 | #include <linux/netfilter/x_tables.h> | |
19 | ||
20 | MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); | |
2ae15b64 | 21 | MODULE_DESCRIPTION("Xtables: IPsec policy match"); |
c4b88513 PM |
22 | MODULE_LICENSE("GPL"); |
23 | ||
1d93a9cb | 24 | static inline bool |
917b6fbd JE |
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) | |
c4b88513 PM |
27 | { |
28 | switch (family) { | |
ee999d8b | 29 | case NFPROTO_IPV4: |
917b6fbd | 30 | return ((a1->ip ^ a2->ip) & m->ip) == 0; |
ee999d8b | 31 | case NFPROTO_IPV6: |
917b6fbd | 32 | return ipv6_masked_addr_cmp(&a1->in6, &m->in6, &a2->in6) == 0; |
c4b88513 | 33 | } |
1d93a9cb | 34 | return false; |
c4b88513 PM |
35 | } |
36 | ||
b9aed455 | 37 | static bool |
a47362a2 | 38 | match_xfrm_state(const struct xfrm_state *x, const struct xt_policy_elem *e, |
c4b88513 PM |
39 | unsigned short family) |
40 | { | |
41 | #define MATCH_ADDR(x,y,z) (!e->match.x || \ | |
917b6fbd | 42 | (xt_addr_cmp(&e->x, &e->y, (const union nf_inet_addr *)(z), family) \ |
c4b88513 PM |
43 | ^ e->invert.x)) |
44 | #define MATCH(x,y) (!e->match.x || ((e->x == (y)) ^ e->invert.x)) | |
45 | ||
917b6fbd JE |
46 | return MATCH_ADDR(saddr, smask, &x->props.saddr) && |
47 | MATCH_ADDR(daddr, dmask, &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 | ||
54 | static int | |
55 | match_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; | |
a47362a2 | 59 | const struct sec_path *sp = skb->sp; |
c4b88513 PM |
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 | ||
dbe5b4aa | 74 | if (match_xfrm_state(sp->xvec[i], e, family)) { |
c4b88513 PM |
75 | if (!strict) |
76 | return 1; | |
77 | } else if (strict) | |
78 | return 0; | |
79 | } | |
80 | ||
81 | return strict ? 1 : 0; | |
82 | } | |
83 | ||
84 | static int | |
85 | match_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; | |
a47362a2 | 89 | const struct dst_entry *dst = skb->dst; |
c4b88513 PM |
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 | ||
d3c5ee6d JE |
112 | static bool |
113 | policy_mt(const struct sk_buff *skb, const struct net_device *in, | |
114 | const struct net_device *out, const struct xt_match *match, | |
115 | const void *matchinfo, int offset, unsigned int protoff, | |
116 | bool *hotdrop) | |
c4b88513 PM |
117 | { |
118 | const struct xt_policy_info *info = matchinfo; | |
119 | int ret; | |
120 | ||
121 | if (info->flags & XT_POLICY_MATCH_IN) | |
122 | ret = match_policy_in(skb, info, match->family); | |
123 | else | |
124 | ret = match_policy_out(skb, info, match->family); | |
125 | ||
126 | if (ret < 0) | |
1d93a9cb | 127 | ret = info->flags & XT_POLICY_MATCH_NONE ? true : false; |
c4b88513 | 128 | else if (info->flags & XT_POLICY_MATCH_NONE) |
1d93a9cb | 129 | ret = false; |
c4b88513 PM |
130 | |
131 | return ret; | |
132 | } | |
133 | ||
d3c5ee6d JE |
134 | static bool |
135 | policy_mt_check(const char *tablename, const void *ip_void, | |
136 | const struct xt_match *match, void *matchinfo, | |
137 | unsigned int hook_mask) | |
c4b88513 | 138 | { |
3cf93c96 | 139 | const struct xt_policy_info *info = matchinfo; |
c4b88513 PM |
140 | |
141 | if (!(info->flags & (XT_POLICY_MATCH_IN|XT_POLICY_MATCH_OUT))) { | |
142 | printk(KERN_ERR "xt_policy: neither incoming nor " | |
601e68e1 | 143 | "outgoing policy selected\n"); |
ccb79bdc | 144 | return false; |
c4b88513 | 145 | } |
6e23ae2a | 146 | if (hook_mask & (1 << NF_INET_PRE_ROUTING | 1 << NF_INET_LOCAL_IN) |
c4b88513 PM |
147 | && info->flags & XT_POLICY_MATCH_OUT) { |
148 | printk(KERN_ERR "xt_policy: output policy not valid in " | |
601e68e1 | 149 | "PRE_ROUTING and INPUT\n"); |
ccb79bdc | 150 | return false; |
c4b88513 | 151 | } |
6e23ae2a | 152 | if (hook_mask & (1 << NF_INET_POST_ROUTING | 1 << NF_INET_LOCAL_OUT) |
c4b88513 PM |
153 | && info->flags & XT_POLICY_MATCH_IN) { |
154 | printk(KERN_ERR "xt_policy: input policy not valid in " | |
601e68e1 | 155 | "POST_ROUTING and OUTPUT\n"); |
ccb79bdc | 156 | return false; |
c4b88513 PM |
157 | } |
158 | if (info->len > XT_POLICY_MAX_ELEM) { | |
159 | printk(KERN_ERR "xt_policy: too many policy elements\n"); | |
ccb79bdc | 160 | return false; |
c4b88513 | 161 | } |
ccb79bdc | 162 | return true; |
c4b88513 PM |
163 | } |
164 | ||
d3c5ee6d | 165 | static struct xt_match policy_mt_reg[] __read_mostly = { |
4470bbc7 PM |
166 | { |
167 | .name = "policy", | |
ee999d8b | 168 | .family = NFPROTO_IPV4, |
d3c5ee6d JE |
169 | .checkentry = policy_mt_check, |
170 | .match = policy_mt, | |
4470bbc7 | 171 | .matchsize = sizeof(struct xt_policy_info), |
4470bbc7 PM |
172 | .me = THIS_MODULE, |
173 | }, | |
174 | { | |
175 | .name = "policy", | |
ee999d8b | 176 | .family = NFPROTO_IPV6, |
d3c5ee6d JE |
177 | .checkentry = policy_mt_check, |
178 | .match = policy_mt, | |
4470bbc7 | 179 | .matchsize = sizeof(struct xt_policy_info), |
4470bbc7 PM |
180 | .me = THIS_MODULE, |
181 | }, | |
c4b88513 PM |
182 | }; |
183 | ||
d3c5ee6d | 184 | static int __init policy_mt_init(void) |
c4b88513 | 185 | { |
d3c5ee6d | 186 | return xt_register_matches(policy_mt_reg, ARRAY_SIZE(policy_mt_reg)); |
c4b88513 PM |
187 | } |
188 | ||
d3c5ee6d | 189 | static void __exit policy_mt_exit(void) |
c4b88513 | 190 | { |
d3c5ee6d | 191 | xt_unregister_matches(policy_mt_reg, ARRAY_SIZE(policy_mt_reg)); |
c4b88513 PM |
192 | } |
193 | ||
d3c5ee6d JE |
194 | module_init(policy_mt_init); |
195 | module_exit(policy_mt_exit); | |
c4b88513 PM |
196 | MODULE_ALIAS("ipt_policy"); |
197 | MODULE_ALIAS("ip6t_policy"); |