1 /* 2 * Copyright (c) 2016 Laura Garcia <nevola@gmail.com> 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 */ 9 10 #include <linux/kernel.h> 11 #include <linux/init.h> 12 #include <linux/module.h> 13 #include <linux/netlink.h> 14 #include <linux/netfilter.h> 15 #include <linux/netfilter/nf_tables.h> 16 #include <net/netfilter/nf_tables.h> 17 #include <net/netfilter/nf_tables_core.h> 18 #include <linux/jhash.h> 19 20 struct nft_jhash { 21 enum nft_registers sreg:8; 22 enum nft_registers dreg:8; 23 u8 len; 24 bool autogen_seed:1; 25 u32 modulus; 26 u32 seed; 27 u32 offset; 28 }; 29 30 static void nft_jhash_eval(const struct nft_expr *expr, 31 struct nft_regs *regs, 32 const struct nft_pktinfo *pkt) 33 { 34 struct nft_jhash *priv = nft_expr_priv(expr); 35 const void *data = ®s->data[priv->sreg]; 36 u32 h; 37 38 h = reciprocal_scale(jhash(data, priv->len, priv->seed), 39 priv->modulus); 40 41 regs->data[priv->dreg] = h + priv->offset; 42 } 43 44 struct nft_symhash { 45 enum nft_registers dreg:8; 46 u32 modulus; 47 u32 offset; 48 }; 49 50 static void nft_symhash_eval(const struct nft_expr *expr, 51 struct nft_regs *regs, 52 const struct nft_pktinfo *pkt) 53 { 54 struct nft_symhash *priv = nft_expr_priv(expr); 55 struct sk_buff *skb = pkt->skb; 56 u32 h; 57 58 h = reciprocal_scale(__skb_get_hash_symmetric(skb), priv->modulus); 59 60 regs->data[priv->dreg] = h + priv->offset; 61 } 62 63 static const struct nla_policy nft_hash_policy[NFTA_HASH_MAX + 1] = { 64 [NFTA_HASH_SREG] = { .type = NLA_U32 }, 65 [NFTA_HASH_DREG] = { .type = NLA_U32 }, 66 [NFTA_HASH_LEN] = { .type = NLA_U32 }, 67 [NFTA_HASH_MODULUS] = { .type = NLA_U32 }, 68 [NFTA_HASH_SEED] = { .type = NLA_U32 }, 69 [NFTA_HASH_OFFSET] = { .type = NLA_U32 }, 70 [NFTA_HASH_TYPE] = { .type = NLA_U32 }, 71 }; 72 73 static int nft_jhash_init(const struct nft_ctx *ctx, 74 const struct nft_expr *expr, 75 const struct nlattr * const tb[]) 76 { 77 struct nft_jhash *priv = nft_expr_priv(expr); 78 u32 len; 79 int err; 80 81 if (!tb[NFTA_HASH_SREG] || 82 !tb[NFTA_HASH_DREG] || 83 !tb[NFTA_HASH_LEN] || 84 !tb[NFTA_HASH_MODULUS]) 85 return -EINVAL; 86 87 if (tb[NFTA_HASH_OFFSET]) 88 priv->offset = ntohl(nla_get_be32(tb[NFTA_HASH_OFFSET])); 89 90 priv->sreg = nft_parse_register(tb[NFTA_HASH_SREG]); 91 priv->dreg = nft_parse_register(tb[NFTA_HASH_DREG]); 92 93 err = nft_parse_u32_check(tb[NFTA_HASH_LEN], U8_MAX, &len); 94 if (err < 0) 95 return err; 96 if (len == 0) 97 return -ERANGE; 98 99 priv->len = len; 100 101 priv->modulus = ntohl(nla_get_be32(tb[NFTA_HASH_MODULUS])); 102 if (priv->modulus < 1) 103 return -ERANGE; 104 105 if (priv->offset + priv->modulus - 1 < priv->offset) 106 return -EOVERFLOW; 107 108 if (tb[NFTA_HASH_SEED]) { 109 priv->seed = ntohl(nla_get_be32(tb[NFTA_HASH_SEED])); 110 } else { 111 priv->autogen_seed = true; 112 get_random_bytes(&priv->seed, sizeof(priv->seed)); 113 } 114 115 return nft_validate_register_load(priv->sreg, len) && 116 nft_validate_register_store(ctx, priv->dreg, NULL, 117 NFT_DATA_VALUE, sizeof(u32)); 118 } 119 120 static int nft_symhash_init(const struct nft_ctx *ctx, 121 const struct nft_expr *expr, 122 const struct nlattr * const tb[]) 123 { 124 struct nft_symhash *priv = nft_expr_priv(expr); 125 126 if (!tb[NFTA_HASH_DREG] || 127 !tb[NFTA_HASH_MODULUS]) 128 return -EINVAL; 129 130 if (tb[NFTA_HASH_OFFSET]) 131 priv->offset = ntohl(nla_get_be32(tb[NFTA_HASH_OFFSET])); 132 133 priv->dreg = nft_parse_register(tb[NFTA_HASH_DREG]); 134 135 priv->modulus = ntohl(nla_get_be32(tb[NFTA_HASH_MODULUS])); 136 if (priv->modulus <= 1) 137 return -ERANGE; 138 139 if (priv->offset + priv->modulus - 1 < priv->offset) 140 return -EOVERFLOW; 141 142 return nft_validate_register_store(ctx, priv->dreg, NULL, 143 NFT_DATA_VALUE, sizeof(u32)); 144 } 145 146 static int nft_jhash_dump(struct sk_buff *skb, 147 const struct nft_expr *expr) 148 { 149 const struct nft_jhash *priv = nft_expr_priv(expr); 150 151 if (nft_dump_register(skb, NFTA_HASH_SREG, priv->sreg)) 152 goto nla_put_failure; 153 if (nft_dump_register(skb, NFTA_HASH_DREG, priv->dreg)) 154 goto nla_put_failure; 155 if (nla_put_be32(skb, NFTA_HASH_LEN, htonl(priv->len))) 156 goto nla_put_failure; 157 if (nla_put_be32(skb, NFTA_HASH_MODULUS, htonl(priv->modulus))) 158 goto nla_put_failure; 159 if (!priv->autogen_seed && 160 nla_put_be32(skb, NFTA_HASH_SEED, htonl(priv->seed))) 161 goto nla_put_failure; 162 if (priv->offset != 0) 163 if (nla_put_be32(skb, NFTA_HASH_OFFSET, htonl(priv->offset))) 164 goto nla_put_failure; 165 if (nla_put_be32(skb, NFTA_HASH_TYPE, htonl(NFT_HASH_JENKINS))) 166 goto nla_put_failure; 167 return 0; 168 169 nla_put_failure: 170 return -1; 171 } 172 173 static int nft_symhash_dump(struct sk_buff *skb, 174 const struct nft_expr *expr) 175 { 176 const struct nft_symhash *priv = nft_expr_priv(expr); 177 178 if (nft_dump_register(skb, NFTA_HASH_DREG, priv->dreg)) 179 goto nla_put_failure; 180 if (nla_put_be32(skb, NFTA_HASH_MODULUS, htonl(priv->modulus))) 181 goto nla_put_failure; 182 if (priv->offset != 0) 183 if (nla_put_be32(skb, NFTA_HASH_OFFSET, htonl(priv->offset))) 184 goto nla_put_failure; 185 if (nla_put_be32(skb, NFTA_HASH_TYPE, htonl(NFT_HASH_SYM))) 186 goto nla_put_failure; 187 return 0; 188 189 nla_put_failure: 190 return -1; 191 } 192 193 static struct nft_expr_type nft_hash_type; 194 static const struct nft_expr_ops nft_jhash_ops = { 195 .type = &nft_hash_type, 196 .size = NFT_EXPR_SIZE(sizeof(struct nft_jhash)), 197 .eval = nft_jhash_eval, 198 .init = nft_jhash_init, 199 .dump = nft_jhash_dump, 200 }; 201 202 static const struct nft_expr_ops nft_symhash_ops = { 203 .type = &nft_hash_type, 204 .size = NFT_EXPR_SIZE(sizeof(struct nft_symhash)), 205 .eval = nft_symhash_eval, 206 .init = nft_symhash_init, 207 .dump = nft_symhash_dump, 208 }; 209 210 static const struct nft_expr_ops * 211 nft_hash_select_ops(const struct nft_ctx *ctx, 212 const struct nlattr * const tb[]) 213 { 214 u32 type; 215 216 if (!tb[NFTA_HASH_TYPE]) 217 return &nft_jhash_ops; 218 219 type = ntohl(nla_get_be32(tb[NFTA_HASH_TYPE])); 220 switch (type) { 221 case NFT_HASH_SYM: 222 return &nft_symhash_ops; 223 case NFT_HASH_JENKINS: 224 return &nft_jhash_ops; 225 default: 226 break; 227 } 228 return ERR_PTR(-EOPNOTSUPP); 229 } 230 231 static struct nft_expr_type nft_hash_type __read_mostly = { 232 .name = "hash", 233 .select_ops = nft_hash_select_ops, 234 .policy = nft_hash_policy, 235 .maxattr = NFTA_HASH_MAX, 236 .owner = THIS_MODULE, 237 }; 238 239 static int __init nft_hash_module_init(void) 240 { 241 return nft_register_expr(&nft_hash_type); 242 } 243 244 static void __exit nft_hash_module_exit(void) 245 { 246 nft_unregister_expr(&nft_hash_type); 247 } 248 249 module_init(nft_hash_module_init); 250 module_exit(nft_hash_module_exit); 251 252 MODULE_LICENSE("GPL"); 253 MODULE_AUTHOR("Laura Garcia <nevola@gmail.com>"); 254 MODULE_ALIAS_NFT_EXPR("hash"); 255