summaryrefslogtreecommitdiffstats
path: root/src/libsystemd/sd-netlink/netlink-message-nfnl.c
blob: fd3055d879fcdb27a7da2cf86c4e5c5baa6cc27c (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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/* SPDX-License-Identifier: LGPL-2.1-or-later */

#include <netinet/in.h>
#include <linux/netfilter/nfnetlink.h>
#include <linux/netfilter/nf_tables.h>
#include <linux/netfilter.h>

#include "sd-netlink.h"

#include "iovec-util.h"
#include "netlink-internal.h"
#include "netlink-types.h"
#include "netlink-util.h"

bool nfproto_is_valid(int nfproto) {
        return IN_SET(nfproto,
                      NFPROTO_UNSPEC,
                      NFPROTO_INET,
                      NFPROTO_IPV4,
                      NFPROTO_ARP,
                      NFPROTO_NETDEV,
                      NFPROTO_BRIDGE,
                      NFPROTO_IPV6);
}

int sd_nfnl_message_new(sd_netlink *nfnl, sd_netlink_message **ret, int nfproto, uint16_t subsys, uint16_t msg_type, uint16_t flags) {
        _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
        int r;

        assert_return(nfnl, -EINVAL);
        assert_return(ret, -EINVAL);
        assert_return(nfproto_is_valid(nfproto), -EINVAL);
        assert_return(NFNL_MSG_TYPE(msg_type) == msg_type, -EINVAL);

        r = message_new(nfnl, &m, subsys << 8 | msg_type);
        if (r < 0)
                return r;

        m->hdr->nlmsg_flags |= flags;

        *(struct nfgenmsg*) NLMSG_DATA(m->hdr) = (struct nfgenmsg) {
                .nfgen_family = nfproto,
                .version = NFNETLINK_V0,
        };

        *ret = TAKE_PTR(m);
        return 0;
}

static int nfnl_message_set_res_id(sd_netlink_message *m, uint16_t res_id) {
        struct nfgenmsg *nfgen;

        assert(m);
        assert(m->hdr);

        nfgen = NLMSG_DATA(m->hdr);
        nfgen->res_id = htobe16(res_id);

        return 0;
}

static int nfnl_message_get_subsys(sd_netlink_message *m, uint16_t *ret) {
        uint16_t t;
        int r;

        assert(m);
        assert(ret);

        r = sd_netlink_message_get_type(m, &t);
        if (r < 0)
                return r;

        *ret = NFNL_SUBSYS_ID(t);
        return 0;
}

static int nfnl_message_new_batch(sd_netlink *nfnl, sd_netlink_message **ret, uint16_t subsys, uint16_t msg_type) {
        _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
        int r;

        assert_return(nfnl, -EINVAL);
        assert_return(ret, -EINVAL);
        assert_return(NFNL_MSG_TYPE(msg_type) == msg_type, -EINVAL);

        r = sd_nfnl_message_new(nfnl, &m, NFPROTO_UNSPEC, NFNL_SUBSYS_NONE, msg_type, 0);
        if (r < 0)
                return r;

        r = nfnl_message_set_res_id(m, subsys);
        if (r < 0)
                return r;

        *ret = TAKE_PTR(m);
        return 0;
}

int sd_nfnl_send_batch(
                sd_netlink *nfnl,
                sd_netlink_message **messages,
                size_t n_messages,
                uint32_t **ret_serials) {

        /* iovs refs batch_begin and batch_end, hence, free iovs first, then free batch_begin and batch_end. */
        _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *batch_begin = NULL, *batch_end = NULL;
        _cleanup_free_ struct iovec *iovs = NULL;
        _cleanup_free_ uint32_t *serials = NULL;
        uint16_t subsys;
        ssize_t k;
        size_t c = 0;
        int r;

        assert_return(nfnl, -EINVAL);
        assert_return(!netlink_pid_changed(nfnl), -ECHILD);
        assert_return(messages, -EINVAL);
        assert_return(n_messages > 0, -EINVAL);

        iovs = new(struct iovec, n_messages + 2);
        if (!iovs)
                return -ENOMEM;

        if (ret_serials) {
                serials = new(uint32_t, n_messages);
                if (!serials)
                        return -ENOMEM;
        }

        r = nfnl_message_get_subsys(messages[0], &subsys);
        if (r < 0)
                return r;

        r = nfnl_message_new_batch(nfnl, &batch_begin, subsys, NFNL_MSG_BATCH_BEGIN);
        if (r < 0)
                return r;

        netlink_seal_message(nfnl, batch_begin);
        iovs[c++] = IOVEC_MAKE(batch_begin->hdr, batch_begin->hdr->nlmsg_len);

        for (size_t i = 0; i < n_messages; i++) {
                uint16_t s;

                r = nfnl_message_get_subsys(messages[i], &s);
                if (r < 0)
                        return r;

                if (s != subsys)
                        return -EINVAL;

                netlink_seal_message(nfnl, messages[i]);
                if (serials)
                        serials[i] = message_get_serial(messages[i]);

                /* It seems that the kernel accepts an arbitrary number. Let's set the lower 16 bits of the
                 * serial of the first message. */
                nfnl_message_set_res_id(messages[i], (uint16_t) (message_get_serial(batch_begin) & UINT16_MAX));

                iovs[c++] = IOVEC_MAKE(messages[i]->hdr, messages[i]->hdr->nlmsg_len);
        }

        r = nfnl_message_new_batch(nfnl, &batch_end, subsys, NFNL_MSG_BATCH_END);
        if (r < 0)
                return r;

        netlink_seal_message(nfnl, batch_end);
        iovs[c++] = IOVEC_MAKE(batch_end->hdr, batch_end->hdr->nlmsg_len);

        assert(c == n_messages + 2);
        k = writev(nfnl->fd, iovs, n_messages + 2);
        if (k < 0)
                return -errno;

        if (ret_serials)
                *ret_serials = TAKE_PTR(serials);

        return 0;
}

int sd_nfnl_call_batch(
                sd_netlink *nfnl,
                sd_netlink_message **messages,
                size_t n_messages,
                uint64_t usec,
                sd_netlink_message ***ret_messages) {

        _cleanup_free_ sd_netlink_message **replies = NULL;
        _cleanup_free_ uint32_t *serials = NULL;
        int r;

        assert_return(nfnl, -EINVAL);
        assert_return(!netlink_pid_changed(nfnl), -ECHILD);
        assert_return(messages, -EINVAL);
        assert_return(n_messages > 0, -EINVAL);

        if (ret_messages) {
                replies = new0(sd_netlink_message*, n_messages);
                if (!replies)
                        return -ENOMEM;
        }

        r = sd_nfnl_send_batch(nfnl, messages, n_messages, &serials);
        if (r < 0)
                return r;

        for (size_t i = 0; i < n_messages; i++)
                RET_GATHER(r,
                           sd_netlink_read(nfnl, serials[i], usec, ret_messages ? replies + i : NULL));
        if (r < 0)
                return r;

        if (ret_messages)
                *ret_messages = TAKE_PTR(replies);

        return 0;
}

int sd_nfnl_nft_message_new_basechain(
                sd_netlink *nfnl,
                sd_netlink_message **ret,
                int nfproto,
                const char *table,
                const char *chain,
                const char *type,
                uint8_t hook,
                int prio) {

        _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
        int r;

        r = sd_nfnl_message_new(nfnl, &m, nfproto, NFNL_SUBSYS_NFTABLES, NFT_MSG_NEWCHAIN, NLM_F_CREATE);
        if (r < 0)
                return r;

        r = sd_netlink_message_append_string(m, NFTA_CHAIN_TABLE, table);
        if (r < 0)
                return r;

        r = sd_netlink_message_append_string(m, NFTA_CHAIN_NAME, chain);
        if (r < 0)
                return r;

        r = sd_netlink_message_append_string(m, NFTA_CHAIN_TYPE, type);
        if (r < 0)
                return r;

        r = sd_netlink_message_open_container(m, NFTA_CHAIN_HOOK);
        if (r < 0)
                return r;

        r = sd_netlink_message_append_u32(m, NFTA_HOOK_HOOKNUM, htobe32(hook));
        if (r < 0)
                return r;

        r = sd_netlink_message_append_u32(m, NFTA_HOOK_PRIORITY, htobe32(prio));
        if (r < 0)
                return r;

        r = sd_netlink_message_close_container(m);
        if (r < 0)
                return r;

        *ret = TAKE_PTR(m);
        return 0;
}

int sd_nfnl_nft_message_new_table(
                sd_netlink *nfnl,
                sd_netlink_message **ret,
                int nfproto,
                const char *table) {

        _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
        int r;

        r = sd_nfnl_message_new(nfnl, &m, nfproto, NFNL_SUBSYS_NFTABLES, NFT_MSG_NEWTABLE, NLM_F_CREATE | NLM_F_EXCL);
        if (r < 0)
                return r;

        r = sd_netlink_message_append_string(m, NFTA_TABLE_NAME, table);
        if (r < 0)
                return r;

        *ret = TAKE_PTR(m);
        return r;
}

int sd_nfnl_nft_message_new_rule(
                sd_netlink *nfnl,
                sd_netlink_message **ret,
                int nfproto,
                const char *table,
                const char *chain) {

        _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
        int r;

        r = sd_nfnl_message_new(nfnl, &m, nfproto, NFNL_SUBSYS_NFTABLES, NFT_MSG_NEWRULE, NLM_F_CREATE);
        if (r < 0)
                return r;

        r = sd_netlink_message_append_string(m, NFTA_RULE_TABLE, table);
        if (r < 0)
                return r;

        r = sd_netlink_message_append_string(m, NFTA_RULE_CHAIN, chain);
        if (r < 0)
                return r;

        *ret = TAKE_PTR(m);
        return r;
}

int sd_nfnl_nft_message_new_set(
                sd_netlink *nfnl,
                sd_netlink_message **ret,
                int nfproto,
                const char *table,
                const char *set_name,
                uint32_t set_id,
                uint32_t klen) {

        _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
        int r;

        r = sd_nfnl_message_new(nfnl, &m, nfproto, NFNL_SUBSYS_NFTABLES, NFT_MSG_NEWSET, NLM_F_CREATE);
        if (r < 0)
                return r;

        r = sd_netlink_message_append_string(m, NFTA_SET_TABLE, table);
        if (r < 0)
                return r;

        r = sd_netlink_message_append_string(m, NFTA_SET_NAME, set_name);
        if (r < 0)
                return r;

        r = sd_netlink_message_append_u32(m, NFTA_SET_ID, ++set_id);
        if (r < 0)
                return r;

        r = sd_netlink_message_append_u32(m, NFTA_SET_KEY_LEN, htobe32(klen));
        if (r < 0)
                return r;

        *ret = TAKE_PTR(m);
        return r;
}

int sd_nfnl_nft_message_new_setelems(
                sd_netlink *nfnl,
                sd_netlink_message **ret,
                int add, /* boolean */
                int nfproto,
                const char *table,
                const char *set_name) {

        _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
        int r;

        if (add)
                r = sd_nfnl_message_new(nfnl, &m, nfproto, NFNL_SUBSYS_NFTABLES, NFT_MSG_NEWSETELEM, NLM_F_CREATE);
        else
                r = sd_nfnl_message_new(nfnl, &m, nfproto, NFNL_SUBSYS_NFTABLES, NFT_MSG_DELSETELEM, 0);
        if (r < 0)
                return r;

        r = sd_netlink_message_append_string(m, NFTA_SET_ELEM_LIST_TABLE, table);
        if (r < 0)
                return r;

        r = sd_netlink_message_append_string(m, NFTA_SET_ELEM_LIST_SET, set_name);
        if (r < 0)
                return r;

        *ret = TAKE_PTR(m);
        return r;
}

int sd_nfnl_nft_message_append_setelem(
                sd_netlink_message *m,
                uint32_t index,
                const void *key,
                size_t key_len,
                const void *data,
                size_t data_len,
                uint32_t flags) {

        int r;

        r = sd_netlink_message_open_array(m, index);
        if (r < 0)
                return r;

        r = sd_netlink_message_append_container_data(m, NFTA_SET_ELEM_KEY, NFTA_DATA_VALUE, key, key_len);
        if (r < 0)
                goto cancel;

        if (data) {
                r = sd_netlink_message_append_container_data(m, NFTA_SET_ELEM_DATA, NFTA_DATA_VALUE, data, data_len);
                if (r < 0)
                        goto cancel;
        }

        if (flags != 0) {
                r = sd_netlink_message_append_u32(m, NFTA_SET_ELEM_FLAGS, htobe32(flags));
                if (r < 0)
                        goto cancel;
        }

        return sd_netlink_message_close_container(m); /* array */

cancel:
        (void) sd_netlink_message_cancel_array(m);
        return r;
}

int sd_nfnl_socket_open(sd_netlink **ret) {
        return netlink_open_family(ret, NETLINK_NETFILTER);
}