Blame view
net/netfilter/xt_policy.c
4.73 KB
c4b885139 [NETFILTER]: x_ta... |
1 2 3 4 5 6 7 8 |
/* IP tables module for matching IPsec policy * * Copyright (c) 2004,2005 Patrick McHardy, <kaber@trash.net> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. */ |
8bee4bad0 netfilter: xt ext... |
9 |
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
c4b885139 [NETFILTER]: x_ta... |
10 |
#include <linux/kernel.h> |
c4b885139 [NETFILTER]: x_ta... |
11 12 13 14 |
#include <linux/module.h> #include <linux/skbuff.h> #include <linux/init.h> #include <net/xfrm.h> |
917b6fbd6 [NETFILTER]: xt_p... |
15 |
#include <linux/netfilter.h> |
c4b885139 [NETFILTER]: x_ta... |
16 17 18 19 |
#include <linux/netfilter/xt_policy.h> #include <linux/netfilter/x_tables.h> MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); |
2ae15b64e [NETFILTER]: Upda... |
20 |
MODULE_DESCRIPTION("Xtables: IPsec policy match"); |
c4b885139 [NETFILTER]: x_ta... |
21 |
MODULE_LICENSE("GPL"); |
1d93a9cba [NETFILTER]: x_ta... |
22 |
static inline bool |
917b6fbd6 [NETFILTER]: xt_p... |
23 24 |
xt_addr_cmp(const union nf_inet_addr *a1, const union nf_inet_addr *m, const union nf_inet_addr *a2, unsigned short family) |
c4b885139 [NETFILTER]: x_ta... |
25 26 |
{ switch (family) { |
ee999d8b9 netfilter: x_tabl... |
27 |
case NFPROTO_IPV4: |
917b6fbd6 [NETFILTER]: xt_p... |
28 |
return ((a1->ip ^ a2->ip) & m->ip) == 0; |
ee999d8b9 netfilter: x_tabl... |
29 |
case NFPROTO_IPV6: |
917b6fbd6 [NETFILTER]: xt_p... |
30 |
return ipv6_masked_addr_cmp(&a1->in6, &m->in6, &a2->in6) == 0; |
c4b885139 [NETFILTER]: x_ta... |
31 |
} |
1d93a9cba [NETFILTER]: x_ta... |
32 |
return false; |
c4b885139 [NETFILTER]: x_ta... |
33 |
} |
b9aed4550 [NETFILTER] xt_po... |
34 |
static bool |
a47362a22 [NETFILTER]: add ... |
35 |
match_xfrm_state(const struct xfrm_state *x, const struct xt_policy_elem *e, |
c4b885139 [NETFILTER]: x_ta... |
36 37 38 |
unsigned short family) { #define MATCH_ADDR(x,y,z) (!e->match.x || \ |
917b6fbd6 [NETFILTER]: xt_p... |
39 |
(xt_addr_cmp(&e->x, &e->y, (const union nf_inet_addr *)(z), family) \ |
c4b885139 [NETFILTER]: x_ta... |
40 41 |
^ e->invert.x)) #define MATCH(x,y) (!e->match.x || ((e->x == (y)) ^ e->invert.x)) |
917b6fbd6 [NETFILTER]: xt_p... |
42 43 |
return MATCH_ADDR(saddr, smask, &x->props.saddr) && MATCH_ADDR(daddr, dmask, &x->id.daddr) && |
c4b885139 [NETFILTER]: x_ta... |
44 45 46 47 48 49 50 51 52 53 54 |
MATCH(proto, x->id.proto) && MATCH(mode, x->props.mode) && MATCH(spi, x->id.spi) && MATCH(reqid, x->props.reqid); } static int match_policy_in(const struct sk_buff *skb, const struct xt_policy_info *info, unsigned short family) { const struct xt_policy_elem *e; |
a47362a22 [NETFILTER]: add ... |
55 |
const struct sec_path *sp = skb->sp; |
c4b885139 [NETFILTER]: x_ta... |
56 57 58 59 60 61 62 63 64 65 66 67 68 |
int strict = info->flags & XT_POLICY_MATCH_STRICT; int i, pos; if (sp == NULL) return -1; if (strict && info->len != sp->len) return 0; for (i = sp->len - 1; i >= 0; i--) { pos = strict ? i - sp->len + 1 : 0; if (pos >= info->len) return 0; e = &info->pol[pos]; |
dbe5b4aaa [IPSEC]: Kill unu... |
69 |
if (match_xfrm_state(sp->xvec[i], e, family)) { |
c4b885139 [NETFILTER]: x_ta... |
70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
if (!strict) return 1; } else if (strict) return 0; } return strict ? 1 : 0; } static int match_policy_out(const struct sk_buff *skb, const struct xt_policy_info *info, unsigned short family) { const struct xt_policy_elem *e; |
adf30907d net: skb->dst acc... |
84 |
const struct dst_entry *dst = skb_dst(skb); |
c4b885139 [NETFILTER]: x_ta... |
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
int strict = info->flags & XT_POLICY_MATCH_STRICT; int i, pos; if (dst->xfrm == NULL) return -1; for (i = 0; dst && dst->xfrm; dst = dst->child, i++) { pos = strict ? i : 0; if (pos >= info->len) return 0; e = &info->pol[pos]; if (match_xfrm_state(dst->xfrm, e, family)) { if (!strict) return 1; } else if (strict) return 0; } return strict ? i == info->len : 0; } |
d3c5ee6d5 [NETFILTER]: x_ta... |
106 |
static bool |
62fc80510 netfilter: xtable... |
107 |
policy_mt(const struct sk_buff *skb, struct xt_action_param *par) |
c4b885139 [NETFILTER]: x_ta... |
108 |
{ |
f7108a20d netfilter: xtable... |
109 |
const struct xt_policy_info *info = par->matchinfo; |
c4b885139 [NETFILTER]: x_ta... |
110 111 112 |
int ret; if (info->flags & XT_POLICY_MATCH_IN) |
aa5fa3185 netfilter: xtable... |
113 |
ret = match_policy_in(skb, info, par->family); |
c4b885139 [NETFILTER]: x_ta... |
114 |
else |
aa5fa3185 netfilter: xtable... |
115 |
ret = match_policy_out(skb, info, par->family); |
c4b885139 [NETFILTER]: x_ta... |
116 117 |
if (ret < 0) |
1d93a9cba [NETFILTER]: x_ta... |
118 |
ret = info->flags & XT_POLICY_MATCH_NONE ? true : false; |
c4b885139 [NETFILTER]: x_ta... |
119 |
else if (info->flags & XT_POLICY_MATCH_NONE) |
1d93a9cba [NETFILTER]: x_ta... |
120 |
ret = false; |
c4b885139 [NETFILTER]: x_ta... |
121 122 123 |
return ret; } |
b0f38452f netfilter: xtable... |
124 |
static int policy_mt_check(const struct xt_mtchk_param *par) |
c4b885139 [NETFILTER]: x_ta... |
125 |
{ |
9b4fce7a3 netfilter: xtable... |
126 |
const struct xt_policy_info *info = par->matchinfo; |
c4b885139 [NETFILTER]: x_ta... |
127 128 |
if (!(info->flags & (XT_POLICY_MATCH_IN|XT_POLICY_MATCH_OUT))) { |
8bee4bad0 netfilter: xt ext... |
129 130 |
pr_info("neither incoming nor outgoing policy selected "); |
bd414ee60 netfilter: xtable... |
131 |
return -EINVAL; |
c4b885139 [NETFILTER]: x_ta... |
132 |
} |
9b4fce7a3 netfilter: xtable... |
133 134 |
if (par->hook_mask & ((1 << NF_INET_PRE_ROUTING) | (1 << NF_INET_LOCAL_IN)) && info->flags & XT_POLICY_MATCH_OUT) { |
8bee4bad0 netfilter: xt ext... |
135 136 |
pr_info("output policy not valid in PREROUTING and INPUT "); |
bd414ee60 netfilter: xtable... |
137 |
return -EINVAL; |
c4b885139 [NETFILTER]: x_ta... |
138 |
} |
9b4fce7a3 netfilter: xtable... |
139 140 |
if (par->hook_mask & ((1 << NF_INET_POST_ROUTING) | (1 << NF_INET_LOCAL_OUT)) && info->flags & XT_POLICY_MATCH_IN) { |
8bee4bad0 netfilter: xt ext... |
141 142 |
pr_info("input policy not valid in POSTROUTING and OUTPUT "); |
bd414ee60 netfilter: xtable... |
143 |
return -EINVAL; |
c4b885139 [NETFILTER]: x_ta... |
144 145 |
} if (info->len > XT_POLICY_MAX_ELEM) { |
8bee4bad0 netfilter: xt ext... |
146 147 |
pr_info("too many policy elements "); |
bd414ee60 netfilter: xtable... |
148 |
return -EINVAL; |
c4b885139 [NETFILTER]: x_ta... |
149 |
} |
bd414ee60 netfilter: xtable... |
150 |
return 0; |
c4b885139 [NETFILTER]: x_ta... |
151 |
} |
d3c5ee6d5 [NETFILTER]: x_ta... |
152 |
static struct xt_match policy_mt_reg[] __read_mostly = { |
4470bbc74 [NETFILTER]: x_ta... |
153 154 |
{ .name = "policy", |
ee999d8b9 netfilter: x_tabl... |
155 |
.family = NFPROTO_IPV4, |
d3c5ee6d5 [NETFILTER]: x_ta... |
156 157 |
.checkentry = policy_mt_check, .match = policy_mt, |
4470bbc74 [NETFILTER]: x_ta... |
158 |
.matchsize = sizeof(struct xt_policy_info), |
4470bbc74 [NETFILTER]: x_ta... |
159 160 161 162 |
.me = THIS_MODULE, }, { .name = "policy", |
ee999d8b9 netfilter: x_tabl... |
163 |
.family = NFPROTO_IPV6, |
d3c5ee6d5 [NETFILTER]: x_ta... |
164 165 |
.checkentry = policy_mt_check, .match = policy_mt, |
4470bbc74 [NETFILTER]: x_ta... |
166 |
.matchsize = sizeof(struct xt_policy_info), |
4470bbc74 [NETFILTER]: x_ta... |
167 168 |
.me = THIS_MODULE, }, |
c4b885139 [NETFILTER]: x_ta... |
169 |
}; |
d3c5ee6d5 [NETFILTER]: x_ta... |
170 |
static int __init policy_mt_init(void) |
c4b885139 [NETFILTER]: x_ta... |
171 |
{ |
d3c5ee6d5 [NETFILTER]: x_ta... |
172 |
return xt_register_matches(policy_mt_reg, ARRAY_SIZE(policy_mt_reg)); |
c4b885139 [NETFILTER]: x_ta... |
173 |
} |
d3c5ee6d5 [NETFILTER]: x_ta... |
174 |
static void __exit policy_mt_exit(void) |
c4b885139 [NETFILTER]: x_ta... |
175 |
{ |
d3c5ee6d5 [NETFILTER]: x_ta... |
176 |
xt_unregister_matches(policy_mt_reg, ARRAY_SIZE(policy_mt_reg)); |
c4b885139 [NETFILTER]: x_ta... |
177 |
} |
d3c5ee6d5 [NETFILTER]: x_ta... |
178 179 |
module_init(policy_mt_init); module_exit(policy_mt_exit); |
c4b885139 [NETFILTER]: x_ta... |
180 181 |
MODULE_ALIAS("ipt_policy"); MODULE_ALIAS("ip6t_policy"); |