Blame src/hash.c

Packit c5a612
/*
Packit c5a612
 * Hash expression definitions.
Packit c5a612
 *
Packit c5a612
 * Copyright (c) 2016 Pablo Neira Ayuso <pablo@netfilter.org>
Packit c5a612
 *
Packit c5a612
 * This program is free software; you can redistribute it and/or modify
Packit c5a612
 * it under the terms of the GNU General Public License version 2 as
Packit c5a612
 * published by the Free Software Foundation.
Packit c5a612
 */
Packit c5a612
Packit c5a612
#include <nftables.h>
Packit c5a612
#include <expression.h>
Packit c5a612
#include <datatype.h>
Packit c5a612
#include <gmputil.h>
Packit c5a612
#include <hash.h>
Packit c5a612
#include <utils.h>
Packit c5a612
Packit c5a612
static void hash_expr_print(const struct expr *expr, struct output_ctx *octx)
Packit c5a612
{
Packit c5a612
	switch (expr->hash.type) {
Packit c5a612
	case NFT_HASH_SYM:
Packit c5a612
		nft_print(octx, "symhash");
Packit c5a612
		break;
Packit c5a612
	case NFT_HASH_JENKINS:
Packit c5a612
	default:
Packit c5a612
		nft_print(octx, "jhash ");
Packit c5a612
		expr_print(expr->hash.expr, octx);
Packit c5a612
	}
Packit c5a612
Packit c5a612
	nft_print(octx, " mod %u", expr->hash.mod);
Packit c5a612
	if (expr->hash.seed_set)
Packit c5a612
		nft_print(octx, " seed 0x%x", expr->hash.seed);
Packit c5a612
	if (expr->hash.offset)
Packit c5a612
		nft_print(octx, " offset %u", expr->hash.offset);
Packit c5a612
}
Packit c5a612
Packit c5a612
static bool hash_expr_cmp(const struct expr *e1, const struct expr *e2)
Packit c5a612
{
Packit c5a612
	return (!e1->hash.expr ||
Packit c5a612
		expr_cmp(e1->hash.expr, e2->hash.expr)) &&
Packit c5a612
	       e1->hash.mod == e2->hash.mod &&
Packit c5a612
	       e1->hash.seed_set == e2->hash.seed_set &&
Packit c5a612
	       e1->hash.seed == e2->hash.seed &&
Packit c5a612
	       e1->hash.offset == e2->hash.offset &&
Packit c5a612
	       e1->hash.type == e2->hash.type;
Packit c5a612
}
Packit c5a612
Packit c5a612
static void hash_expr_clone(struct expr *new, const struct expr *expr)
Packit c5a612
{
Packit c5a612
	if (expr->hash.expr)
Packit c5a612
		new->hash.expr = expr_clone(expr->hash.expr);
Packit c5a612
	new->hash.mod = expr->hash.mod;
Packit c5a612
	new->hash.seed_set = expr->hash.seed_set;
Packit c5a612
	new->hash.seed = expr->hash.seed;
Packit c5a612
	new->hash.offset = expr->hash.offset;
Packit c5a612
	new->hash.type = expr->hash.type;
Packit c5a612
}
Packit c5a612
Packit c5a612
static void hash_expr_destroy(struct expr *expr)
Packit c5a612
{
Packit c5a612
	expr_free(expr->hash.expr);
Packit c5a612
}
Packit c5a612
Packit c5a612
const struct expr_ops hash_expr_ops = {
Packit c5a612
	.type		= EXPR_HASH,
Packit c5a612
	.name		= "hash",
Packit c5a612
	.print		= hash_expr_print,
Packit c5a612
	.json		= hash_expr_json,
Packit c5a612
	.cmp		= hash_expr_cmp,
Packit c5a612
	.clone		= hash_expr_clone,
Packit c5a612
	.destroy	= hash_expr_destroy,
Packit c5a612
};
Packit c5a612
Packit c5a612
struct expr *hash_expr_alloc(const struct location *loc,
Packit c5a612
			     uint32_t mod,
Packit c5a612
			     bool seed_set, uint32_t seed,
Packit c5a612
			     uint32_t offset,
Packit c5a612
			     enum nft_hash_types type)
Packit c5a612
{
Packit c5a612
	struct expr *expr;
Packit c5a612
Packit c5a612
	expr = expr_alloc(loc, EXPR_HASH, &integer_type,
Packit c5a612
			  BYTEORDER_HOST_ENDIAN, 4 * BITS_PER_BYTE);
Packit c5a612
	expr->hash.mod  = mod;
Packit c5a612
	expr->hash.seed_set = seed_set;
Packit c5a612
	expr->hash.seed = seed;
Packit c5a612
	expr->hash.offset = offset;
Packit c5a612
	expr->hash.type = type;
Packit c5a612
Packit c5a612
	return expr;
Packit c5a612
}