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