summaryrefslogtreecommitdiffstats
path: root/src/socket.c
blob: d90b0416e62c7b858dd14f17574ab16d0619417f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
 * Socket expression/statement related definition and types.
 *
 * Copyright (c) 2018 Máté Eckl <ecklm94@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <nftables.h>
#include <expression.h>
#include <socket.h>
#include <json.h>

const struct socket_template socket_templates[] = {
	[NFT_SOCKET_TRANSPARENT] = {
		.token		= "transparent",
		.dtype		= &integer_type,
		.len		= BITS_PER_BYTE,
		.byteorder	= BYTEORDER_HOST_ENDIAN,
	},
	[NFT_SOCKET_MARK] = {
		.token		= "mark",
		.dtype		= &mark_type,
		.len		= 4 * BITS_PER_BYTE,
		.byteorder	= BYTEORDER_HOST_ENDIAN,
	},
};

static void socket_expr_print(const struct expr *expr, struct output_ctx *octx)
{
	nft_print(octx, "socket %s", socket_templates[expr->socket.key].token);
}

static bool socket_expr_cmp(const struct expr *e1, const struct expr *e2)
{
	return e1->socket.key == e2->socket.key;
}

static void socket_expr_clone(struct expr *new, const struct expr *expr)
{
	new->socket.key = expr->socket.key;
}

static const struct expr_ops socket_expr_ops = {
	.type		= EXPR_SOCKET,
	.name		= "socket",
	.print		= socket_expr_print,
	.json		= socket_expr_json,
	.cmp		= socket_expr_cmp,
	.clone		= socket_expr_clone,
};

struct expr *socket_expr_alloc(const struct location *loc, enum nft_socket_keys key)
{
	const struct socket_template *tmpl = &socket_templates[key];
	struct expr *expr;

	expr = expr_alloc(loc, &socket_expr_ops, tmpl->dtype,
			  tmpl->byteorder, tmpl->len);
	expr->socket.key = key;

	return expr;
}