Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 1 | /* |
| 2 | * (C) 2012-2013 by Pablo Neira Ayuso <pablo@netfilter.org> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * This software has been sponsored by Sophos Astaro <http://www.sophos.com> |
| 9 | */ |
| 10 | |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/init.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/netlink.h> |
| 15 | #include <linux/netfilter.h> |
| 16 | #include <linux/netfilter/nfnetlink.h> |
| 17 | #include <linux/netfilter/nf_tables.h> |
| 18 | #include <linux/netfilter/nf_tables_compat.h> |
| 19 | #include <linux/netfilter/x_tables.h> |
| 20 | #include <linux/netfilter_ipv4/ip_tables.h> |
| 21 | #include <linux/netfilter_ipv6/ip6_tables.h> |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 22 | #include <net/netfilter/nf_tables.h> |
| 23 | |
Pablo Neira Ayuso | f3f5dde | 2014-10-14 10:13:48 +0200 | [diff] [blame] | 24 | static int nft_compat_chain_validate_dependency(const char *tablename, |
| 25 | const struct nft_chain *chain) |
| 26 | { |
Pablo Neira Ayuso | f3f5dde | 2014-10-14 10:13:48 +0200 | [diff] [blame] | 27 | const struct nft_base_chain *basechain; |
| 28 | |
| 29 | if (!tablename || !(chain->flags & NFT_BASE_CHAIN)) |
| 30 | return 0; |
| 31 | |
Pablo Neira Ayuso | f3f5dde | 2014-10-14 10:13:48 +0200 | [diff] [blame] | 32 | basechain = nft_base_chain(chain); |
Pablo Neira Ayuso | c918687 | 2014-11-10 20:53:55 +0100 | [diff] [blame] | 33 | if (strcmp(tablename, "nat") == 0 && |
| 34 | basechain->type->type != NFT_CHAIN_T_NAT) |
Pablo Neira Ayuso | f3f5dde | 2014-10-14 10:13:48 +0200 | [diff] [blame] | 35 | return -EINVAL; |
| 36 | |
| 37 | return 0; |
| 38 | } |
| 39 | |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 40 | union nft_entry { |
| 41 | struct ipt_entry e4; |
| 42 | struct ip6t_entry e6; |
| 43 | }; |
| 44 | |
| 45 | static inline void |
| 46 | nft_compat_set_par(struct xt_action_param *par, void *xt, const void *xt_info) |
| 47 | { |
| 48 | par->target = xt; |
| 49 | par->targinfo = xt_info; |
| 50 | par->hotdrop = false; |
| 51 | } |
| 52 | |
| 53 | static void nft_target_eval(const struct nft_expr *expr, |
| 54 | struct nft_data data[NFT_REG_MAX + 1], |
| 55 | const struct nft_pktinfo *pkt) |
| 56 | { |
| 57 | void *info = nft_expr_priv(expr); |
| 58 | struct xt_target *target = expr->ops->data; |
| 59 | struct sk_buff *skb = pkt->skb; |
| 60 | int ret; |
| 61 | |
| 62 | nft_compat_set_par((struct xt_action_param *)&pkt->xt, target, info); |
| 63 | |
| 64 | ret = target->target(skb, &pkt->xt); |
| 65 | |
| 66 | if (pkt->xt.hotdrop) |
| 67 | ret = NF_DROP; |
| 68 | |
| 69 | switch(ret) { |
| 70 | case XT_CONTINUE: |
| 71 | data[NFT_REG_VERDICT].verdict = NFT_CONTINUE; |
| 72 | break; |
| 73 | default: |
| 74 | data[NFT_REG_VERDICT].verdict = ret; |
| 75 | break; |
| 76 | } |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | static const struct nla_policy nft_target_policy[NFTA_TARGET_MAX + 1] = { |
| 81 | [NFTA_TARGET_NAME] = { .type = NLA_NUL_STRING }, |
| 82 | [NFTA_TARGET_REV] = { .type = NLA_U32 }, |
| 83 | [NFTA_TARGET_INFO] = { .type = NLA_BINARY }, |
| 84 | }; |
| 85 | |
| 86 | static void |
| 87 | nft_target_set_tgchk_param(struct xt_tgchk_param *par, |
| 88 | const struct nft_ctx *ctx, |
| 89 | struct xt_target *target, void *info, |
| 90 | union nft_entry *entry, u8 proto, bool inv) |
| 91 | { |
Pablo Neira Ayuso | 2daf1b4 | 2014-11-07 18:48:33 +0100 | [diff] [blame] | 92 | par->net = ctx->net; |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 93 | par->table = ctx->table->name; |
| 94 | switch (ctx->afi->family) { |
| 95 | case AF_INET: |
| 96 | entry->e4.ip.proto = proto; |
| 97 | entry->e4.ip.invflags = inv ? IPT_INV_PROTO : 0; |
| 98 | break; |
| 99 | case AF_INET6: |
| 100 | entry->e6.ipv6.proto = proto; |
| 101 | entry->e6.ipv6.invflags = inv ? IP6T_INV_PROTO : 0; |
| 102 | break; |
| 103 | } |
| 104 | par->entryinfo = entry; |
| 105 | par->target = target; |
| 106 | par->targinfo = info; |
| 107 | if (ctx->chain->flags & NFT_BASE_CHAIN) { |
| 108 | const struct nft_base_chain *basechain = |
| 109 | nft_base_chain(ctx->chain); |
Patrick McHardy | 115a60b | 2014-01-03 12:16:15 +0000 | [diff] [blame] | 110 | const struct nf_hook_ops *ops = &basechain->ops[0]; |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 111 | |
| 112 | par->hook_mask = 1 << ops->hooknum; |
Pablo Neira Ayuso | 493618a | 2014-10-14 12:43:50 +0200 | [diff] [blame] | 113 | } else { |
| 114 | par->hook_mask = 0; |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 115 | } |
| 116 | par->family = ctx->afi->family; |
| 117 | } |
| 118 | |
| 119 | static void target_compat_from_user(struct xt_target *t, void *in, void *out) |
| 120 | { |
Pablo Neira Ayuso | 756c1b1 | 2014-06-17 21:18:44 +0200 | [diff] [blame] | 121 | int pad; |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 122 | |
Pablo Neira Ayuso | 756c1b1 | 2014-06-17 21:18:44 +0200 | [diff] [blame] | 123 | memcpy(out, in, t->targetsize); |
| 124 | pad = XT_ALIGN(t->targetsize) - t->targetsize; |
| 125 | if (pad > 0) |
| 126 | memset(out + t->targetsize, 0, pad); |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | static const struct nla_policy nft_rule_compat_policy[NFTA_RULE_COMPAT_MAX + 1] = { |
| 130 | [NFTA_RULE_COMPAT_PROTO] = { .type = NLA_U32 }, |
| 131 | [NFTA_RULE_COMPAT_FLAGS] = { .type = NLA_U32 }, |
| 132 | }; |
| 133 | |
Pablo Neira Ayuso | 8691a9a | 2013-11-16 22:16:47 +0100 | [diff] [blame] | 134 | static int nft_parse_compat(const struct nlattr *attr, u8 *proto, bool *inv) |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 135 | { |
| 136 | struct nlattr *tb[NFTA_RULE_COMPAT_MAX+1]; |
| 137 | u32 flags; |
| 138 | int err; |
| 139 | |
| 140 | err = nla_parse_nested(tb, NFTA_RULE_COMPAT_MAX, attr, |
| 141 | nft_rule_compat_policy); |
| 142 | if (err < 0) |
| 143 | return err; |
| 144 | |
| 145 | if (!tb[NFTA_RULE_COMPAT_PROTO] || !tb[NFTA_RULE_COMPAT_FLAGS]) |
| 146 | return -EINVAL; |
| 147 | |
| 148 | flags = ntohl(nla_get_be32(tb[NFTA_RULE_COMPAT_FLAGS])); |
| 149 | if (flags & ~NFT_RULE_COMPAT_F_MASK) |
| 150 | return -EINVAL; |
| 151 | if (flags & NFT_RULE_COMPAT_F_INV) |
| 152 | *inv = true; |
| 153 | |
Pablo Neira Ayuso | 8691a9a | 2013-11-16 22:16:47 +0100 | [diff] [blame] | 154 | *proto = ntohl(nla_get_be32(tb[NFTA_RULE_COMPAT_PROTO])); |
| 155 | return 0; |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | static int |
| 159 | nft_target_init(const struct nft_ctx *ctx, const struct nft_expr *expr, |
| 160 | const struct nlattr * const tb[]) |
| 161 | { |
| 162 | void *info = nft_expr_priv(expr); |
| 163 | struct xt_target *target = expr->ops->data; |
| 164 | struct xt_tgchk_param par; |
| 165 | size_t size = XT_ALIGN(nla_len(tb[NFTA_TARGET_INFO])); |
| 166 | u8 proto = 0; |
| 167 | bool inv = false; |
| 168 | union nft_entry e = {}; |
| 169 | int ret; |
| 170 | |
Pablo Neira Ayuso | f3f5dde | 2014-10-14 10:13:48 +0200 | [diff] [blame] | 171 | ret = nft_compat_chain_validate_dependency(target->table, ctx->chain); |
| 172 | if (ret < 0) |
| 173 | goto err; |
| 174 | |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 175 | target_compat_from_user(target, nla_data(tb[NFTA_TARGET_INFO]), info); |
| 176 | |
Pablo Neira Ayuso | 8691a9a | 2013-11-16 22:16:47 +0100 | [diff] [blame] | 177 | if (ctx->nla[NFTA_RULE_COMPAT]) { |
| 178 | ret = nft_parse_compat(ctx->nla[NFTA_RULE_COMPAT], &proto, &inv); |
| 179 | if (ret < 0) |
| 180 | goto err; |
| 181 | } |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 182 | |
| 183 | nft_target_set_tgchk_param(&par, ctx, target, info, &e, proto, inv); |
| 184 | |
| 185 | ret = xt_check_target(&par, size, proto, inv); |
| 186 | if (ret < 0) |
| 187 | goto err; |
| 188 | |
| 189 | /* The standard target cannot be used */ |
| 190 | if (target->target == NULL) { |
| 191 | ret = -EINVAL; |
| 192 | goto err; |
| 193 | } |
| 194 | |
| 195 | return 0; |
| 196 | err: |
| 197 | module_put(target->me); |
| 198 | return ret; |
| 199 | } |
| 200 | |
| 201 | static void |
Patrick McHardy | 62472bc | 2014-03-07 19:08:30 +0100 | [diff] [blame] | 202 | nft_target_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr) |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 203 | { |
| 204 | struct xt_target *target = expr->ops->data; |
Pablo Neira Ayuso | 3d9b142 | 2014-06-11 14:27:46 +0200 | [diff] [blame] | 205 | void *info = nft_expr_priv(expr); |
| 206 | struct xt_tgdtor_param par; |
| 207 | |
| 208 | par.net = ctx->net; |
| 209 | par.target = target; |
| 210 | par.targinfo = info; |
| 211 | par.family = ctx->afi->family; |
| 212 | if (par.target->destroy != NULL) |
| 213 | par.target->destroy(&par); |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 214 | |
| 215 | module_put(target->me); |
| 216 | } |
| 217 | |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 218 | static int nft_target_dump(struct sk_buff *skb, const struct nft_expr *expr) |
| 219 | { |
| 220 | const struct xt_target *target = expr->ops->data; |
| 221 | void *info = nft_expr_priv(expr); |
| 222 | |
| 223 | if (nla_put_string(skb, NFTA_TARGET_NAME, target->name) || |
| 224 | nla_put_be32(skb, NFTA_TARGET_REV, htonl(target->revision)) || |
Pablo Neira Ayuso | 756c1b1 | 2014-06-17 21:18:44 +0200 | [diff] [blame] | 225 | nla_put(skb, NFTA_TARGET_INFO, XT_ALIGN(target->targetsize), info)) |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 226 | goto nla_put_failure; |
| 227 | |
| 228 | return 0; |
| 229 | |
| 230 | nla_put_failure: |
| 231 | return -1; |
| 232 | } |
| 233 | |
| 234 | static int nft_target_validate(const struct nft_ctx *ctx, |
| 235 | const struct nft_expr *expr, |
| 236 | const struct nft_data **data) |
| 237 | { |
| 238 | struct xt_target *target = expr->ops->data; |
| 239 | unsigned int hook_mask = 0; |
Pablo Neira Ayuso | f3f5dde | 2014-10-14 10:13:48 +0200 | [diff] [blame] | 240 | int ret; |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 241 | |
| 242 | if (ctx->chain->flags & NFT_BASE_CHAIN) { |
| 243 | const struct nft_base_chain *basechain = |
| 244 | nft_base_chain(ctx->chain); |
Patrick McHardy | 115a60b | 2014-01-03 12:16:15 +0000 | [diff] [blame] | 245 | const struct nf_hook_ops *ops = &basechain->ops[0]; |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 246 | |
| 247 | hook_mask = 1 << ops->hooknum; |
Pablo Neira Ayuso | f3f5dde | 2014-10-14 10:13:48 +0200 | [diff] [blame] | 248 | if (!(hook_mask & target->hooks)) |
| 249 | return -EINVAL; |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 250 | |
Pablo Neira Ayuso | f3f5dde | 2014-10-14 10:13:48 +0200 | [diff] [blame] | 251 | ret = nft_compat_chain_validate_dependency(target->table, |
| 252 | ctx->chain); |
| 253 | if (ret < 0) |
| 254 | return ret; |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 255 | } |
| 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | static void nft_match_eval(const struct nft_expr *expr, |
| 260 | struct nft_data data[NFT_REG_MAX + 1], |
| 261 | const struct nft_pktinfo *pkt) |
| 262 | { |
| 263 | void *info = nft_expr_priv(expr); |
| 264 | struct xt_match *match = expr->ops->data; |
| 265 | struct sk_buff *skb = pkt->skb; |
| 266 | bool ret; |
| 267 | |
| 268 | nft_compat_set_par((struct xt_action_param *)&pkt->xt, match, info); |
| 269 | |
| 270 | ret = match->match(skb, (struct xt_action_param *)&pkt->xt); |
| 271 | |
| 272 | if (pkt->xt.hotdrop) { |
| 273 | data[NFT_REG_VERDICT].verdict = NF_DROP; |
| 274 | return; |
| 275 | } |
| 276 | |
| 277 | switch(ret) { |
| 278 | case true: |
| 279 | data[NFT_REG_VERDICT].verdict = NFT_CONTINUE; |
| 280 | break; |
| 281 | case false: |
| 282 | data[NFT_REG_VERDICT].verdict = NFT_BREAK; |
| 283 | break; |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | static const struct nla_policy nft_match_policy[NFTA_MATCH_MAX + 1] = { |
| 288 | [NFTA_MATCH_NAME] = { .type = NLA_NUL_STRING }, |
| 289 | [NFTA_MATCH_REV] = { .type = NLA_U32 }, |
| 290 | [NFTA_MATCH_INFO] = { .type = NLA_BINARY }, |
| 291 | }; |
| 292 | |
| 293 | /* struct xt_mtchk_param and xt_tgchk_param look very similar */ |
| 294 | static void |
| 295 | nft_match_set_mtchk_param(struct xt_mtchk_param *par, const struct nft_ctx *ctx, |
| 296 | struct xt_match *match, void *info, |
| 297 | union nft_entry *entry, u8 proto, bool inv) |
| 298 | { |
Pablo Neira Ayuso | 2daf1b4 | 2014-11-07 18:48:33 +0100 | [diff] [blame] | 299 | par->net = ctx->net; |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 300 | par->table = ctx->table->name; |
| 301 | switch (ctx->afi->family) { |
| 302 | case AF_INET: |
| 303 | entry->e4.ip.proto = proto; |
| 304 | entry->e4.ip.invflags = inv ? IPT_INV_PROTO : 0; |
| 305 | break; |
| 306 | case AF_INET6: |
| 307 | entry->e6.ipv6.proto = proto; |
| 308 | entry->e6.ipv6.invflags = inv ? IP6T_INV_PROTO : 0; |
| 309 | break; |
| 310 | } |
| 311 | par->entryinfo = entry; |
| 312 | par->match = match; |
| 313 | par->matchinfo = info; |
| 314 | if (ctx->chain->flags & NFT_BASE_CHAIN) { |
| 315 | const struct nft_base_chain *basechain = |
| 316 | nft_base_chain(ctx->chain); |
Patrick McHardy | 115a60b | 2014-01-03 12:16:15 +0000 | [diff] [blame] | 317 | const struct nf_hook_ops *ops = &basechain->ops[0]; |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 318 | |
| 319 | par->hook_mask = 1 << ops->hooknum; |
Pablo Neira Ayuso | 493618a | 2014-10-14 12:43:50 +0200 | [diff] [blame] | 320 | } else { |
| 321 | par->hook_mask = 0; |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 322 | } |
| 323 | par->family = ctx->afi->family; |
| 324 | } |
| 325 | |
| 326 | static void match_compat_from_user(struct xt_match *m, void *in, void *out) |
| 327 | { |
Pablo Neira Ayuso | 756c1b1 | 2014-06-17 21:18:44 +0200 | [diff] [blame] | 328 | int pad; |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 329 | |
Pablo Neira Ayuso | 756c1b1 | 2014-06-17 21:18:44 +0200 | [diff] [blame] | 330 | memcpy(out, in, m->matchsize); |
| 331 | pad = XT_ALIGN(m->matchsize) - m->matchsize; |
| 332 | if (pad > 0) |
| 333 | memset(out + m->matchsize, 0, pad); |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | static int |
| 337 | nft_match_init(const struct nft_ctx *ctx, const struct nft_expr *expr, |
| 338 | const struct nlattr * const tb[]) |
| 339 | { |
| 340 | void *info = nft_expr_priv(expr); |
| 341 | struct xt_match *match = expr->ops->data; |
| 342 | struct xt_mtchk_param par; |
| 343 | size_t size = XT_ALIGN(nla_len(tb[NFTA_MATCH_INFO])); |
| 344 | u8 proto = 0; |
| 345 | bool inv = false; |
| 346 | union nft_entry e = {}; |
| 347 | int ret; |
| 348 | |
Pablo Neira Ayuso | afefb6f | 2014-11-10 19:08:21 +0100 | [diff] [blame] | 349 | ret = nft_compat_chain_validate_dependency(match->table, ctx->chain); |
Pablo Neira Ayuso | f3f5dde | 2014-10-14 10:13:48 +0200 | [diff] [blame] | 350 | if (ret < 0) |
| 351 | goto err; |
| 352 | |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 353 | match_compat_from_user(match, nla_data(tb[NFTA_MATCH_INFO]), info); |
| 354 | |
Pablo Neira Ayuso | 8691a9a | 2013-11-16 22:16:47 +0100 | [diff] [blame] | 355 | if (ctx->nla[NFTA_RULE_COMPAT]) { |
| 356 | ret = nft_parse_compat(ctx->nla[NFTA_RULE_COMPAT], &proto, &inv); |
| 357 | if (ret < 0) |
| 358 | goto err; |
| 359 | } |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 360 | |
| 361 | nft_match_set_mtchk_param(&par, ctx, match, info, &e, proto, inv); |
| 362 | |
| 363 | ret = xt_check_match(&par, size, proto, inv); |
| 364 | if (ret < 0) |
| 365 | goto err; |
| 366 | |
| 367 | return 0; |
| 368 | err: |
| 369 | module_put(match->me); |
| 370 | return ret; |
| 371 | } |
| 372 | |
| 373 | static void |
Patrick McHardy | 62472bc | 2014-03-07 19:08:30 +0100 | [diff] [blame] | 374 | nft_match_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr) |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 375 | { |
| 376 | struct xt_match *match = expr->ops->data; |
Pablo Neira Ayuso | 3d9b142 | 2014-06-11 14:27:46 +0200 | [diff] [blame] | 377 | void *info = nft_expr_priv(expr); |
| 378 | struct xt_mtdtor_param par; |
| 379 | |
| 380 | par.net = ctx->net; |
| 381 | par.match = match; |
| 382 | par.matchinfo = info; |
| 383 | par.family = ctx->afi->family; |
| 384 | if (par.match->destroy != NULL) |
| 385 | par.match->destroy(&par); |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 386 | |
| 387 | module_put(match->me); |
| 388 | } |
| 389 | |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 390 | static int nft_match_dump(struct sk_buff *skb, const struct nft_expr *expr) |
| 391 | { |
| 392 | void *info = nft_expr_priv(expr); |
| 393 | struct xt_match *match = expr->ops->data; |
| 394 | |
| 395 | if (nla_put_string(skb, NFTA_MATCH_NAME, match->name) || |
| 396 | nla_put_be32(skb, NFTA_MATCH_REV, htonl(match->revision)) || |
Pablo Neira Ayuso | 756c1b1 | 2014-06-17 21:18:44 +0200 | [diff] [blame] | 397 | nla_put(skb, NFTA_MATCH_INFO, XT_ALIGN(match->matchsize), info)) |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 398 | goto nla_put_failure; |
| 399 | |
| 400 | return 0; |
| 401 | |
| 402 | nla_put_failure: |
| 403 | return -1; |
| 404 | } |
| 405 | |
| 406 | static int nft_match_validate(const struct nft_ctx *ctx, |
| 407 | const struct nft_expr *expr, |
| 408 | const struct nft_data **data) |
| 409 | { |
| 410 | struct xt_match *match = expr->ops->data; |
| 411 | unsigned int hook_mask = 0; |
Pablo Neira Ayuso | f3f5dde | 2014-10-14 10:13:48 +0200 | [diff] [blame] | 412 | int ret; |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 413 | |
| 414 | if (ctx->chain->flags & NFT_BASE_CHAIN) { |
| 415 | const struct nft_base_chain *basechain = |
| 416 | nft_base_chain(ctx->chain); |
Patrick McHardy | 115a60b | 2014-01-03 12:16:15 +0000 | [diff] [blame] | 417 | const struct nf_hook_ops *ops = &basechain->ops[0]; |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 418 | |
| 419 | hook_mask = 1 << ops->hooknum; |
Pablo Neira Ayuso | f3f5dde | 2014-10-14 10:13:48 +0200 | [diff] [blame] | 420 | if (!(hook_mask & match->hooks)) |
| 421 | return -EINVAL; |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 422 | |
Pablo Neira Ayuso | afefb6f | 2014-11-10 19:08:21 +0100 | [diff] [blame] | 423 | ret = nft_compat_chain_validate_dependency(match->table, |
Pablo Neira Ayuso | f3f5dde | 2014-10-14 10:13:48 +0200 | [diff] [blame] | 424 | ctx->chain); |
| 425 | if (ret < 0) |
| 426 | return ret; |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 427 | } |
| 428 | return 0; |
| 429 | } |
| 430 | |
| 431 | static int |
| 432 | nfnl_compat_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type, |
| 433 | int event, u16 family, const char *name, |
| 434 | int rev, int target) |
| 435 | { |
| 436 | struct nlmsghdr *nlh; |
| 437 | struct nfgenmsg *nfmsg; |
| 438 | unsigned int flags = portid ? NLM_F_MULTI : 0; |
| 439 | |
| 440 | event |= NFNL_SUBSYS_NFT_COMPAT << 8; |
| 441 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags); |
| 442 | if (nlh == NULL) |
| 443 | goto nlmsg_failure; |
| 444 | |
| 445 | nfmsg = nlmsg_data(nlh); |
| 446 | nfmsg->nfgen_family = family; |
| 447 | nfmsg->version = NFNETLINK_V0; |
| 448 | nfmsg->res_id = 0; |
| 449 | |
| 450 | if (nla_put_string(skb, NFTA_COMPAT_NAME, name) || |
| 451 | nla_put_be32(skb, NFTA_COMPAT_REV, htonl(rev)) || |
| 452 | nla_put_be32(skb, NFTA_COMPAT_TYPE, htonl(target))) |
| 453 | goto nla_put_failure; |
| 454 | |
| 455 | nlmsg_end(skb, nlh); |
| 456 | return skb->len; |
| 457 | |
| 458 | nlmsg_failure: |
| 459 | nla_put_failure: |
| 460 | nlmsg_cancel(skb, nlh); |
| 461 | return -1; |
| 462 | } |
| 463 | |
| 464 | static int |
| 465 | nfnl_compat_get(struct sock *nfnl, struct sk_buff *skb, |
| 466 | const struct nlmsghdr *nlh, const struct nlattr * const tb[]) |
| 467 | { |
| 468 | int ret = 0, target; |
| 469 | struct nfgenmsg *nfmsg; |
| 470 | const char *fmt; |
| 471 | const char *name; |
| 472 | u32 rev; |
| 473 | struct sk_buff *skb2; |
| 474 | |
| 475 | if (tb[NFTA_COMPAT_NAME] == NULL || |
| 476 | tb[NFTA_COMPAT_REV] == NULL || |
| 477 | tb[NFTA_COMPAT_TYPE] == NULL) |
| 478 | return -EINVAL; |
| 479 | |
| 480 | name = nla_data(tb[NFTA_COMPAT_NAME]); |
| 481 | rev = ntohl(nla_get_be32(tb[NFTA_COMPAT_REV])); |
| 482 | target = ntohl(nla_get_be32(tb[NFTA_COMPAT_TYPE])); |
| 483 | |
| 484 | nfmsg = nlmsg_data(nlh); |
| 485 | |
| 486 | switch(nfmsg->nfgen_family) { |
| 487 | case AF_INET: |
| 488 | fmt = "ipt_%s"; |
| 489 | break; |
| 490 | case AF_INET6: |
| 491 | fmt = "ip6t_%s"; |
| 492 | break; |
| 493 | default: |
| 494 | pr_err("nft_compat: unsupported protocol %d\n", |
| 495 | nfmsg->nfgen_family); |
| 496 | return -EINVAL; |
| 497 | } |
| 498 | |
| 499 | try_then_request_module(xt_find_revision(nfmsg->nfgen_family, name, |
| 500 | rev, target, &ret), |
| 501 | fmt, name); |
| 502 | |
| 503 | if (ret < 0) |
| 504 | return ret; |
| 505 | |
| 506 | skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 507 | if (skb2 == NULL) |
| 508 | return -ENOMEM; |
| 509 | |
| 510 | /* include the best revision for this extension in the message */ |
| 511 | if (nfnl_compat_fill_info(skb2, NETLINK_CB(skb).portid, |
| 512 | nlh->nlmsg_seq, |
| 513 | NFNL_MSG_TYPE(nlh->nlmsg_type), |
| 514 | NFNL_MSG_COMPAT_GET, |
| 515 | nfmsg->nfgen_family, |
| 516 | name, ret, target) <= 0) { |
| 517 | kfree_skb(skb2); |
| 518 | return -ENOSPC; |
| 519 | } |
| 520 | |
| 521 | ret = netlink_unicast(nfnl, skb2, NETLINK_CB(skb).portid, |
| 522 | MSG_DONTWAIT); |
| 523 | if (ret > 0) |
| 524 | ret = 0; |
| 525 | |
| 526 | return ret == -EAGAIN ? -ENOBUFS : ret; |
| 527 | } |
| 528 | |
| 529 | static const struct nla_policy nfnl_compat_policy_get[NFTA_COMPAT_MAX+1] = { |
| 530 | [NFTA_COMPAT_NAME] = { .type = NLA_NUL_STRING, |
| 531 | .len = NFT_COMPAT_NAME_MAX-1 }, |
| 532 | [NFTA_COMPAT_REV] = { .type = NLA_U32 }, |
| 533 | [NFTA_COMPAT_TYPE] = { .type = NLA_U32 }, |
| 534 | }; |
| 535 | |
| 536 | static const struct nfnl_callback nfnl_nft_compat_cb[NFNL_MSG_COMPAT_MAX] = { |
| 537 | [NFNL_MSG_COMPAT_GET] = { .call = nfnl_compat_get, |
| 538 | .attr_count = NFTA_COMPAT_MAX, |
| 539 | .policy = nfnl_compat_policy_get }, |
| 540 | }; |
| 541 | |
| 542 | static const struct nfnetlink_subsystem nfnl_compat_subsys = { |
| 543 | .name = "nft-compat", |
| 544 | .subsys_id = NFNL_SUBSYS_NFT_COMPAT, |
| 545 | .cb_count = NFNL_MSG_COMPAT_MAX, |
| 546 | .cb = nfnl_nft_compat_cb, |
| 547 | }; |
| 548 | |
| 549 | static LIST_HEAD(nft_match_list); |
| 550 | |
| 551 | struct nft_xt { |
| 552 | struct list_head head; |
| 553 | struct nft_expr_ops ops; |
| 554 | }; |
| 555 | |
| 556 | static struct nft_expr_type nft_match_type; |
| 557 | |
| 558 | static const struct nft_expr_ops * |
| 559 | nft_match_select_ops(const struct nft_ctx *ctx, |
| 560 | const struct nlattr * const tb[]) |
| 561 | { |
| 562 | struct nft_xt *nft_match; |
| 563 | struct xt_match *match; |
| 564 | char *mt_name; |
| 565 | __u32 rev, family; |
| 566 | |
| 567 | if (tb[NFTA_MATCH_NAME] == NULL || |
| 568 | tb[NFTA_MATCH_REV] == NULL || |
| 569 | tb[NFTA_MATCH_INFO] == NULL) |
| 570 | return ERR_PTR(-EINVAL); |
| 571 | |
| 572 | mt_name = nla_data(tb[NFTA_MATCH_NAME]); |
| 573 | rev = ntohl(nla_get_be32(tb[NFTA_MATCH_REV])); |
| 574 | family = ctx->afi->family; |
| 575 | |
| 576 | /* Re-use the existing match if it's already loaded. */ |
| 577 | list_for_each_entry(nft_match, &nft_match_list, head) { |
| 578 | struct xt_match *match = nft_match->ops.data; |
| 579 | |
| 580 | if (strcmp(match->name, mt_name) == 0 && |
Pablo Neira Ayuso | 520aa74 | 2015-02-12 22:15:31 +0100 | [diff] [blame^] | 581 | match->revision == rev && match->family == family) { |
| 582 | if (!try_module_get(match->me)) |
| 583 | return ERR_PTR(-ENOENT); |
| 584 | |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 585 | return &nft_match->ops; |
Pablo Neira Ayuso | 520aa74 | 2015-02-12 22:15:31 +0100 | [diff] [blame^] | 586 | } |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | match = xt_request_find_match(family, mt_name, rev); |
| 590 | if (IS_ERR(match)) |
| 591 | return ERR_PTR(-ENOENT); |
| 592 | |
| 593 | /* This is the first time we use this match, allocate operations */ |
| 594 | nft_match = kzalloc(sizeof(struct nft_xt), GFP_KERNEL); |
| 595 | if (nft_match == NULL) |
| 596 | return ERR_PTR(-ENOMEM); |
| 597 | |
| 598 | nft_match->ops.type = &nft_match_type; |
Pablo Neira Ayuso | 756c1b1 | 2014-06-17 21:18:44 +0200 | [diff] [blame] | 599 | nft_match->ops.size = NFT_EXPR_SIZE(XT_ALIGN(match->matchsize)); |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 600 | nft_match->ops.eval = nft_match_eval; |
| 601 | nft_match->ops.init = nft_match_init; |
| 602 | nft_match->ops.destroy = nft_match_destroy; |
| 603 | nft_match->ops.dump = nft_match_dump; |
| 604 | nft_match->ops.validate = nft_match_validate; |
| 605 | nft_match->ops.data = match; |
| 606 | |
| 607 | list_add(&nft_match->head, &nft_match_list); |
| 608 | |
| 609 | return &nft_match->ops; |
| 610 | } |
| 611 | |
| 612 | static void nft_match_release(void) |
| 613 | { |
Dan Carpenter | c359c41 | 2013-11-04 15:58:56 +0300 | [diff] [blame] | 614 | struct nft_xt *nft_match, *tmp; |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 615 | |
Dan Carpenter | c359c41 | 2013-11-04 15:58:56 +0300 | [diff] [blame] | 616 | list_for_each_entry_safe(nft_match, tmp, &nft_match_list, head) |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 617 | kfree(nft_match); |
| 618 | } |
| 619 | |
| 620 | static struct nft_expr_type nft_match_type __read_mostly = { |
| 621 | .name = "match", |
| 622 | .select_ops = nft_match_select_ops, |
| 623 | .policy = nft_match_policy, |
| 624 | .maxattr = NFTA_MATCH_MAX, |
| 625 | .owner = THIS_MODULE, |
| 626 | }; |
| 627 | |
| 628 | static LIST_HEAD(nft_target_list); |
| 629 | |
| 630 | static struct nft_expr_type nft_target_type; |
| 631 | |
| 632 | static const struct nft_expr_ops * |
| 633 | nft_target_select_ops(const struct nft_ctx *ctx, |
| 634 | const struct nlattr * const tb[]) |
| 635 | { |
| 636 | struct nft_xt *nft_target; |
| 637 | struct xt_target *target; |
| 638 | char *tg_name; |
| 639 | __u32 rev, family; |
| 640 | |
| 641 | if (tb[NFTA_TARGET_NAME] == NULL || |
| 642 | tb[NFTA_TARGET_REV] == NULL || |
| 643 | tb[NFTA_TARGET_INFO] == NULL) |
| 644 | return ERR_PTR(-EINVAL); |
| 645 | |
| 646 | tg_name = nla_data(tb[NFTA_TARGET_NAME]); |
| 647 | rev = ntohl(nla_get_be32(tb[NFTA_TARGET_REV])); |
| 648 | family = ctx->afi->family; |
| 649 | |
| 650 | /* Re-use the existing target if it's already loaded. */ |
Arturo Borrero | 7965ee9 | 2014-10-26 12:22:40 +0100 | [diff] [blame] | 651 | list_for_each_entry(nft_target, &nft_target_list, head) { |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 652 | struct xt_target *target = nft_target->ops.data; |
| 653 | |
| 654 | if (strcmp(target->name, tg_name) == 0 && |
Pablo Neira Ayuso | 520aa74 | 2015-02-12 22:15:31 +0100 | [diff] [blame^] | 655 | target->revision == rev && target->family == family) { |
| 656 | if (!try_module_get(target->me)) |
| 657 | return ERR_PTR(-ENOENT); |
| 658 | |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 659 | return &nft_target->ops; |
Pablo Neira Ayuso | 520aa74 | 2015-02-12 22:15:31 +0100 | [diff] [blame^] | 660 | } |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | target = xt_request_find_target(family, tg_name, rev); |
| 664 | if (IS_ERR(target)) |
| 665 | return ERR_PTR(-ENOENT); |
| 666 | |
| 667 | /* This is the first time we use this target, allocate operations */ |
| 668 | nft_target = kzalloc(sizeof(struct nft_xt), GFP_KERNEL); |
| 669 | if (nft_target == NULL) |
| 670 | return ERR_PTR(-ENOMEM); |
| 671 | |
| 672 | nft_target->ops.type = &nft_target_type; |
Pablo Neira Ayuso | 756c1b1 | 2014-06-17 21:18:44 +0200 | [diff] [blame] | 673 | nft_target->ops.size = NFT_EXPR_SIZE(XT_ALIGN(target->targetsize)); |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 674 | nft_target->ops.eval = nft_target_eval; |
| 675 | nft_target->ops.init = nft_target_init; |
| 676 | nft_target->ops.destroy = nft_target_destroy; |
| 677 | nft_target->ops.dump = nft_target_dump; |
| 678 | nft_target->ops.validate = nft_target_validate; |
| 679 | nft_target->ops.data = target; |
| 680 | |
| 681 | list_add(&nft_target->head, &nft_target_list); |
| 682 | |
| 683 | return &nft_target->ops; |
| 684 | } |
| 685 | |
| 686 | static void nft_target_release(void) |
| 687 | { |
Dan Carpenter | c359c41 | 2013-11-04 15:58:56 +0300 | [diff] [blame] | 688 | struct nft_xt *nft_target, *tmp; |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 689 | |
Dan Carpenter | c359c41 | 2013-11-04 15:58:56 +0300 | [diff] [blame] | 690 | list_for_each_entry_safe(nft_target, tmp, &nft_target_list, head) |
Pablo Neira Ayuso | 0ca743a | 2013-10-14 00:06:06 +0200 | [diff] [blame] | 691 | kfree(nft_target); |
| 692 | } |
| 693 | |
| 694 | static struct nft_expr_type nft_target_type __read_mostly = { |
| 695 | .name = "target", |
| 696 | .select_ops = nft_target_select_ops, |
| 697 | .policy = nft_target_policy, |
| 698 | .maxattr = NFTA_TARGET_MAX, |
| 699 | .owner = THIS_MODULE, |
| 700 | }; |
| 701 | |
| 702 | static int __init nft_compat_module_init(void) |
| 703 | { |
| 704 | int ret; |
| 705 | |
| 706 | ret = nft_register_expr(&nft_match_type); |
| 707 | if (ret < 0) |
| 708 | return ret; |
| 709 | |
| 710 | ret = nft_register_expr(&nft_target_type); |
| 711 | if (ret < 0) |
| 712 | goto err_match; |
| 713 | |
| 714 | ret = nfnetlink_subsys_register(&nfnl_compat_subsys); |
| 715 | if (ret < 0) { |
| 716 | pr_err("nft_compat: cannot register with nfnetlink.\n"); |
| 717 | goto err_target; |
| 718 | } |
| 719 | |
| 720 | pr_info("nf_tables_compat: (c) 2012 Pablo Neira Ayuso <pablo@netfilter.org>\n"); |
| 721 | |
| 722 | return ret; |
| 723 | |
| 724 | err_target: |
| 725 | nft_unregister_expr(&nft_target_type); |
| 726 | err_match: |
| 727 | nft_unregister_expr(&nft_match_type); |
| 728 | return ret; |
| 729 | } |
| 730 | |
| 731 | static void __exit nft_compat_module_exit(void) |
| 732 | { |
| 733 | nfnetlink_subsys_unregister(&nfnl_compat_subsys); |
| 734 | nft_unregister_expr(&nft_target_type); |
| 735 | nft_unregister_expr(&nft_match_type); |
| 736 | nft_match_release(); |
| 737 | nft_target_release(); |
| 738 | } |
| 739 | |
| 740 | MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFT_COMPAT); |
| 741 | |
| 742 | module_init(nft_compat_module_init); |
| 743 | module_exit(nft_compat_module_exit); |
| 744 | |
| 745 | MODULE_LICENSE("GPL"); |
| 746 | MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>"); |
| 747 | MODULE_ALIAS_NFT_EXPR("match"); |
| 748 | MODULE_ALIAS_NFT_EXPR("target"); |