summaryrefslogtreecommitdiff
path: root/lib/route/act/nat.c
blob: ffc9d2be8298e2b7cb03b160da8e36fb761a9a42 (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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/* SPDX-License-Identifier: LGPL-2.1-only */
/*
 * Copyright (c) 2016 Magnus Öberg <magnus.oberg@westermo.se>
 */

/**
 * @ingroup act
 * @defgroup act_nat NAT
 *
 * @{
 */

#include "nl-default.h"

#include <netlink/netlink.h>
#include <netlink/attr.h>
#include <netlink/utils.h>
#include <netlink/route/act/nat.h>
#include <netlink/route/tc.h>

#include "tc-api.h"

static struct nla_policy nat_policy[TCA_NAT_MAX + 1] = {
	[TCA_NAT_PARMS] = { .minlen = sizeof(struct tc_nat) },
};

/**
 * nat operations
 */

static int nat_msg_parser(struct rtnl_tc *tc, void *data)
{
	struct tc_nat *nat = data;
	struct nlattr *tb[TCA_NAT_MAX + 1];
	int err;

	err = tca_parse(tb, TCA_NAT_MAX, tc, nat_policy);
	if (err < 0)
		return err;

	if (!tb[TCA_NAT_PARMS])
		return -NLE_MISSING_ATTR;

	nla_memcpy(nat, tb[TCA_NAT_PARMS], sizeof(*nat));

	return NLE_SUCCESS;
}

static void nat_free_data(struct rtnl_tc *tc, void *data)
{
}

static int nat_msg_fill(struct rtnl_tc *tc, void *data, struct nl_msg *msg)
{
	struct tc_nat *nat = data;

	if (!nat)
		return -NLE_OBJ_NOTFOUND;

	NLA_PUT(msg, TCA_NAT_PARMS, sizeof(*nat), nat);

	return NLE_SUCCESS;

nla_put_failure:
	return -NLE_NOMEM;
}

static void nat_dump_line(struct rtnl_tc *tc, void *data,
			  struct nl_dump_params *p)
{
	struct tc_nat *nat = data;
	char buf[32];
	uint32_t mask;
	int pfx = 0;

	if (!nat)
		return;

	if (nat->flags & TCA_NAT_FLAG_EGRESS)
		nl_dump(p, " egress");
	else
		nl_dump(p, " ingress");

	mask = nat->mask;
	while (mask > 0) {
		mask = mask >> 1;
		pfx++;
	}

	inet_ntop(AF_INET, &nat->old_addr, buf, sizeof(buf));
	nl_dump(p, " %s", buf);
	if (pfx < 32)
		nl_dump(p, "/%d", pfx);

	inet_ntop(AF_INET, &nat->new_addr, buf, sizeof(buf));
	nl_dump(p, " %s", buf);
	if (pfx < 32)
		nl_dump(p, "/%d", pfx);
}

/**
 * @name Attribute Modifications
 * @{
 */

/**
 * Set old IPv4 address on a netlink NAT action object
 * @arg act        Action object
 * @arg addr       Binary IPv4 address in host byte order
 *
 * @return 0 on success or negative error code in case of an error.
 */
int rtnl_nat_set_old_addr(struct rtnl_act *act, in_addr_t addr)
{
	struct tc_nat *nat;

	if (!(nat = (struct tc_nat *)rtnl_tc_data(TC_CAST(act))))
		return -NLE_NOMEM;

	nat->old_addr = addr;

	return NLE_SUCCESS;
}

int rtnl_nat_get_old_addr(struct rtnl_act *act, in_addr_t *addr)
{
	struct tc_nat *nat;

	if (!(nat = (struct tc_nat *)rtnl_tc_data_peek(TC_CAST(act))))
		return -NLE_NOATTR;

	*addr = nat->old_addr;

	return NLE_SUCCESS;
}

/**
 * Set new IPv4 address on a netlink NAT action object
 * @arg act        Action object
 * @arg addr       Binary IPv4 address in host byte order
 *
 * @return 0 on success or negative error code in case of an error.
 */
int rtnl_nat_set_new_addr(struct rtnl_act *act, in_addr_t addr)
{
	struct tc_nat *nat;

	if (!(nat = (struct tc_nat *)rtnl_tc_data(TC_CAST(act))))
		return -NLE_NOMEM;

	nat->new_addr = addr;

	return NLE_SUCCESS;
}

int rtnl_nat_get_new_addr(struct rtnl_act *act, in_addr_t *addr)
{
	struct tc_nat *nat;

	if (!(nat = (struct tc_nat *)rtnl_tc_data_peek(TC_CAST(act))))
		return -NLE_NOATTR;

	*addr = nat->new_addr;

	return NLE_SUCCESS;
}

/**
 * Set IPv4 address mask on a netlink NAT action object
 * @arg act        Action object
 * @arg mask       IPv4 address mask
 *
 * @return 0 on success or negative error code in case of an error.
 */
int rtnl_nat_set_mask(struct rtnl_act *act, in_addr_t bitmask)
{
	struct tc_nat *nat;

	if (!(nat = (struct tc_nat *)rtnl_tc_data(TC_CAST(act))))
		return -NLE_NOMEM;

	nat->mask = bitmask;

	return NLE_SUCCESS;
}

int rtnl_nat_get_mask(struct rtnl_act *act, in_addr_t *bitmask)
{
	struct tc_nat *nat;

	if (!(nat = (struct tc_nat *)rtnl_tc_data_peek(TC_CAST(act))))
		return -NLE_NOATTR;

	*bitmask = nat->mask;

	return NLE_SUCCESS;
}

/**
 * Set flags for a netlink NAT action object
 * @arg act        Action object
 * @arg flags      TCA_NAT_FLAG_* flags.
 *
 * Currently only TCA_NAT_FLAG_EGRESS is defined. Selects NAT on
 * egress/IP src if set, ingress/IP dst otherwise.
 *
 * @return 0 on success or negative error code in case of an error.
 */
int rtnl_nat_set_flags(struct rtnl_act *act, uint32_t flags)
{
	struct tc_nat *nat;

	if (!(nat = (struct tc_nat *)rtnl_tc_data(TC_CAST(act))))
		return -NLE_NOMEM;

	nat->flags = flags;

	return NLE_SUCCESS;
}

int rtnl_nat_get_flags(struct rtnl_act *act, uint32_t *flags)
{
	struct tc_nat *nat;

	if (!(nat = (struct tc_nat *)rtnl_tc_data_peek(TC_CAST(act))))
		return -NLE_NOATTR;

	*flags = nat->flags;

	return NLE_SUCCESS;
}

int rtnl_nat_set_action(struct rtnl_act *act, int action)
{
	struct tc_nat *nat;

	if (!(nat = (struct tc_nat *)rtnl_tc_data(TC_CAST(act))))
		return -NLE_NOMEM;

	if (action < TC_ACT_UNSPEC)
		return -NLE_INVAL;

	nat->action = action;

	return NLE_SUCCESS;
}

int rtnl_nat_get_action(struct rtnl_act *act, int *action)
{
	struct tc_nat *nat;

	if (!(nat = (struct tc_nat *)rtnl_tc_data_peek(TC_CAST(act))))
		return -NLE_NOATTR;

	*action = nat->action;

	return NLE_SUCCESS;
}

/**
 * @}
 */

static struct rtnl_tc_ops nat_ops = {
	.to_kind                = "nat",
	.to_type                = RTNL_TC_TYPE_ACT,
	.to_size                = sizeof(struct tc_nat),
	.to_msg_parser          = nat_msg_parser,
	.to_free_data           = nat_free_data,
	.to_clone               = NULL,
	.to_msg_fill            = nat_msg_fill,
	.to_dump = {
		[NL_DUMP_LINE]  = nat_dump_line,
	},
};

static void _nl_init nat_init(void)
{
	rtnl_tc_register(&nat_ops);
}

static void _nl_exit nat_exit(void)
{
	rtnl_tc_unregister(&nat_ops);
}

/**
 * @}
 */