summaryrefslogtreecommitdiffstats
path: root/src/expr.c
blob: aeb717e549153ca9aefec19f5a8f54f040898602 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
 * (C) 2012 by Pablo Neira Ayuso <pablo@netfilter.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This code has been sponsored by Sophos Astaro <http://www.sophos.com>
 */
#include "internal.h"
#include "expr_ops.h"

#include <time.h>
#include <endian.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>

#include <libmnl/libmnl.h>
#include <linux/netfilter/nfnetlink.h>
#include <linux/netfilter/nf_tables.h>

#include <libnftables/expr.h>

#include "linux_list.h"

struct nft_rule_expr *nft_rule_expr_alloc(const char *name)
{
	struct nft_rule_expr *expr;
	struct expr_ops *ops;

	ops = nft_expr_ops_lookup(name);
	if (ops == NULL)
		return NULL;

	expr = calloc(1, sizeof(struct nft_rule_expr) + ops->alloc_len);
	if (expr == NULL)
		return NULL;

	/* Manually set expression name attribute */
	expr->flags |= (1 << NFT_RULE_EXPR_ATTR_NAME);
	expr->ops = ops;

	return expr;
}
EXPORT_SYMBOL(nft_rule_expr_alloc);

void nft_rule_expr_free(struct nft_rule_expr *expr)
{
	if (expr->ops->free)
		expr->ops->free(expr);

	xfree(expr);
}
EXPORT_SYMBOL(nft_rule_expr_free);

bool nft_rule_expr_is_set(const struct nft_rule_expr *expr, uint16_t type)
{
	return expr->flags & (1 << type);
}
EXPORT_SYMBOL(nft_rule_expr_is_set);

void
nft_rule_expr_set(struct nft_rule_expr *expr, uint16_t type,
		  const void *data, uint32_t data_len)
{
	switch(type) {
	case NFT_RULE_EXPR_ATTR_NAME:	/* cannot be modified */
		return;
	default:
		if (expr->ops->set(expr, type, data, data_len) < 0)
			return;
	}
	expr->flags |= (1 << type);
}
EXPORT_SYMBOL(nft_rule_expr_set);

void
nft_rule_expr_set_u8(struct nft_rule_expr *expr, uint16_t type, uint8_t data)
{
	nft_rule_expr_set(expr, type, &data, sizeof(uint8_t));
}
EXPORT_SYMBOL(nft_rule_expr_set_u8);

void
nft_rule_expr_set_u16(struct nft_rule_expr *expr, uint16_t type, uint16_t data)
{
	nft_rule_expr_set(expr, type, &data, sizeof(uint16_t));
}
EXPORT_SYMBOL(nft_rule_expr_set_u16);

void
nft_rule_expr_set_u32(struct nft_rule_expr *expr, uint16_t type, uint32_t data)
{
	nft_rule_expr_set(expr, type, &data, sizeof(uint32_t));
}
EXPORT_SYMBOL(nft_rule_expr_set_u32);

void
nft_rule_expr_set_u64(struct nft_rule_expr *expr, uint16_t type, uint64_t data)
{
	nft_rule_expr_set(expr, type, &data, sizeof(uint64_t));
}
EXPORT_SYMBOL(nft_rule_expr_set_u64);

void
nft_rule_expr_set_str(struct nft_rule_expr *expr, uint16_t type, const char *str)
{
	nft_rule_expr_set(expr, type, str, strlen(str)+1);
}
EXPORT_SYMBOL(nft_rule_expr_set_str);

const void *nft_rule_expr_get(const struct nft_rule_expr *expr,
			      uint16_t type, uint32_t *data_len)
{
	const void *ret;

	if (!(expr->flags & (1 << type)))
		return NULL;

	switch(type) {
	case NFT_RULE_EXPR_ATTR_NAME:
		ret = expr->ops->name;
		break;
	default:
		ret = expr->ops->get(expr, type, data_len);
		break;
	}

	return ret;
}
EXPORT_SYMBOL(nft_rule_expr_get);

uint8_t nft_rule_expr_get_u8(const struct nft_rule_expr *expr, uint16_t type)
{
	const void *data;
	uint32_t data_len;

	data = nft_rule_expr_get(expr, type, &data_len);
	if (data == NULL)
		return 0;

	if (data_len != sizeof(uint8_t))
		return 0;

	return *((uint8_t *)data);
}
EXPORT_SYMBOL(nft_rule_expr_get_u8);

uint16_t nft_rule_expr_get_u16(const struct nft_rule_expr *expr, uint16_t type)
{
	const void *data;
	uint32_t data_len;

	data = nft_rule_expr_get(expr, type, &data_len);
	if (data == NULL)
		return 0;

	if (data_len != sizeof(uint16_t))
		return 0;

	return *((uint16_t *)data);
}
EXPORT_SYMBOL(nft_rule_expr_get_u16);

uint32_t nft_rule_expr_get_u32(const struct nft_rule_expr *expr, uint16_t type)
{
	const void *data;
	uint32_t data_len;

	data = nft_rule_expr_get(expr, type, &data_len);
	if (data == NULL)
		return 0;

	if (data_len != sizeof(uint32_t))
		return 0;

	return *((uint32_t *)data);
}
EXPORT_SYMBOL(nft_rule_expr_get_u32);

uint64_t nft_rule_expr_get_u64(const struct nft_rule_expr *expr, uint16_t type)
{
	const void *data;
	uint32_t data_len;

	data = nft_rule_expr_get(expr, type, &data_len);
	if (data == NULL)
		return 0;

	if (data_len != sizeof(uint64_t))
		return 0;

	return *((uint64_t *)data);
}
EXPORT_SYMBOL(nft_rule_expr_get_u64);

const char *nft_rule_expr_get_str(const struct nft_rule_expr *expr, uint16_t type)
{
	uint32_t data_len;

	return (const char *)nft_rule_expr_get(expr, type, &data_len);
}
EXPORT_SYMBOL(nft_rule_expr_get_str);

void
nft_rule_expr_build_payload(struct nlmsghdr *nlh, struct nft_rule_expr *expr)
{
	struct nlattr *nest1, *nest2;

	nest1 = mnl_attr_nest_start(nlh, NFTA_LIST_ELEM);
	mnl_attr_put_strz(nlh, NFTA_EXPR_NAME, expr->ops->name);

	nest2 = mnl_attr_nest_start(nlh, NFTA_EXPR_DATA);
	expr->ops->build(nlh, expr);
	mnl_attr_nest_end(nlh, nest2);

	mnl_attr_nest_end(nlh, nest1);
}
EXPORT_SYMBOL(nft_rule_expr_build_payload);

int nft_rule_expr_snprintf(char *buf, size_t size, struct nft_rule_expr *expr,
			   uint32_t type, uint32_t flags)
{
	int ret;
	unsigned int offset = 0, len = size;

	ret = expr->ops->snprintf(buf+offset, len, type, flags, expr);
	SNPRINTF_BUFFER_SIZE(ret, size, len, offset);

	return offset;
}
EXPORT_SYMBOL(nft_rule_expr_snprintf);