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