1 /* 2 * Copyright (c) 2009 Patrick McHardy <kaber@trash.net> 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 * Development of this code funded by Astaro AG (http://www.astaro.com/) 9 */ 10 11 #include <linux/kernel.h> 12 #include <linux/init.h> 13 #include <linux/list.h> 14 #include <linux/rbtree.h> 15 #include <linux/netlink.h> 16 #include <linux/netfilter.h> 17 #include <linux/netfilter/nf_tables.h> 18 #include <net/netfilter/nf_tables.h> 19 20 struct nft_lookup { 21 struct nft_set *set; 22 enum nft_registers sreg:8; 23 enum nft_registers dreg:8; 24 struct nft_set_binding binding; 25 }; 26 27 static void nft_lookup_eval(const struct nft_expr *expr, 28 struct nft_data data[NFT_REG_MAX + 1], 29 const struct nft_pktinfo *pkt) 30 { 31 const struct nft_lookup *priv = nft_expr_priv(expr); 32 const struct nft_set *set = priv->set; 33 34 if (set->ops->lookup(set, &data[priv->sreg], &data[priv->dreg])) 35 return; 36 data[NFT_REG_VERDICT].verdict = NFT_BREAK; 37 } 38 39 static const struct nla_policy nft_lookup_policy[NFTA_LOOKUP_MAX + 1] = { 40 [NFTA_LOOKUP_SET] = { .type = NLA_STRING }, 41 [NFTA_LOOKUP_SREG] = { .type = NLA_U32 }, 42 [NFTA_LOOKUP_DREG] = { .type = NLA_U32 }, 43 }; 44 45 static int nft_lookup_init(const struct nft_ctx *ctx, 46 const struct nft_expr *expr, 47 const struct nlattr * const tb[]) 48 { 49 struct nft_lookup *priv = nft_expr_priv(expr); 50 struct nft_set *set; 51 int err; 52 53 if (tb[NFTA_LOOKUP_SET] == NULL || 54 tb[NFTA_LOOKUP_SREG] == NULL) 55 return -EINVAL; 56 57 set = nf_tables_set_lookup(ctx->table, tb[NFTA_LOOKUP_SET]); 58 if (IS_ERR(set)) 59 return PTR_ERR(set); 60 61 priv->sreg = ntohl(nla_get_be32(tb[NFTA_LOOKUP_SREG])); 62 err = nft_validate_input_register(priv->sreg); 63 if (err < 0) 64 return err; 65 66 if (tb[NFTA_LOOKUP_DREG] != NULL) { 67 if (!(set->flags & NFT_SET_MAP)) 68 return -EINVAL; 69 70 priv->dreg = ntohl(nla_get_be32(tb[NFTA_LOOKUP_DREG])); 71 err = nft_validate_output_register(priv->dreg); 72 if (err < 0) 73 return err; 74 75 if (priv->dreg == NFT_REG_VERDICT) { 76 if (set->dtype != NFT_DATA_VERDICT) 77 return -EINVAL; 78 } else if (set->dtype == NFT_DATA_VERDICT) 79 return -EINVAL; 80 } else if (set->flags & NFT_SET_MAP) 81 return -EINVAL; 82 83 err = nf_tables_bind_set(ctx, set, &priv->binding); 84 if (err < 0) 85 return err; 86 87 priv->set = set; 88 return 0; 89 } 90 91 static void nft_lookup_destroy(const struct nft_expr *expr) 92 { 93 struct nft_lookup *priv = nft_expr_priv(expr); 94 95 nf_tables_unbind_set(NULL, priv->set, &priv->binding); 96 } 97 98 static int nft_lookup_dump(struct sk_buff *skb, const struct nft_expr *expr) 99 { 100 const struct nft_lookup *priv = nft_expr_priv(expr); 101 102 if (nla_put_string(skb, NFTA_LOOKUP_SET, priv->set->name)) 103 goto nla_put_failure; 104 if (nla_put_be32(skb, NFTA_LOOKUP_SREG, htonl(priv->sreg))) 105 goto nla_put_failure; 106 if (priv->set->flags & NFT_SET_MAP) 107 if (nla_put_be32(skb, NFTA_LOOKUP_DREG, htonl(priv->dreg))) 108 goto nla_put_failure; 109 return 0; 110 111 nla_put_failure: 112 return -1; 113 } 114 115 static struct nft_expr_type nft_lookup_type; 116 static const struct nft_expr_ops nft_lookup_ops = { 117 .type = &nft_lookup_type, 118 .size = NFT_EXPR_SIZE(sizeof(struct nft_lookup)), 119 .eval = nft_lookup_eval, 120 .init = nft_lookup_init, 121 .destroy = nft_lookup_destroy, 122 .dump = nft_lookup_dump, 123 }; 124 125 static struct nft_expr_type nft_lookup_type __read_mostly = { 126 .name = "lookup", 127 .ops = &nft_lookup_ops, 128 .policy = nft_lookup_policy, 129 .maxattr = NFTA_LOOKUP_MAX, 130 .owner = THIS_MODULE, 131 }; 132 133 int __init nft_lookup_module_init(void) 134 { 135 return nft_register_expr(&nft_lookup_type); 136 } 137 138 void nft_lookup_module_exit(void) 139 { 140 nft_unregister_expr(&nft_lookup_type); 141 } 142