| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <linux/netfilter.h> |
| 4 |
#include <linux/netfilter/nf_tables.h> |
| 5 |
#include <linux/netfilter/nfnetlink.h> |
| 6 |
|
| 7 |
#include "sd-netlink.h" |
| 8 |
|
| 9 |
#include "alloc-util.h" |
| 10 |
#include "iovec-util.h" |
| 11 |
#include "netlink-internal.h" |
| 12 |
#include "netlink-util.h" |
| 13 |
|
| 14 |
bool nfproto_is_valid(int nfproto) { |
| 15 |
return IN_SET(nfproto, |
| 16 |
NFPROTO_UNSPEC, |
| 17 |
NFPROTO_INET, |
| 18 |
NFPROTO_IPV4, |
| 19 |
NFPROTO_ARP, |
| 20 |
NFPROTO_NETDEV, |
| 21 |
NFPROTO_BRIDGE, |
| 22 |
NFPROTO_IPV6); |
| 23 |
} |
| 24 |
|
| 25 |
int sd_nfnl_message_new(sd_netlink *nfnl, sd_netlink_message **ret, int nfproto, uint16_t subsys, uint16_t msg_type, uint16_t flags) { |
| 26 |
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL; |
| 27 |
int r; |
| 28 |
|
| 29 |
assert_return(nfnl, -EINVAL); |
| 30 |
assert_return(ret, -EINVAL); |
| 31 |
assert_return(nfproto_is_valid(nfproto), -EINVAL); |
| 32 |
assert_return(NFNL_MSG_TYPE(msg_type) == msg_type, -EINVAL); |
| 33 |
|
| 34 |
r = message_new(nfnl, &m, subsys << 8 | msg_type, NLM_F_REQUEST | NLM_F_ACK); |
| 35 |
if (r < 0) |
| 36 |
return r; |
| 37 |
|
| 38 |
m->hdr->nlmsg_flags |= flags; |
| 39 |
|
| 40 |
*(struct nfgenmsg*) NLMSG_DATA(m->hdr) = (struct nfgenmsg) { |
| 41 |
.nfgen_family = nfproto, |
| 42 |
.version = NFNETLINK_V0, |
| 43 |
}; |
| 44 |
|
| 45 |
*ret = TAKE_PTR(m); |
| 46 |
return 0; |
| 47 |
} |
| 48 |
|
| 49 |
static int nfnl_message_set_res_id(sd_netlink_message *m, uint16_t res_id) { |
| 50 |
struct nfgenmsg *nfgen; |
| 51 |
|
| 52 |
assert(m); |
| 53 |
assert(m->hdr); |
| 54 |
|
| 55 |
nfgen = NLMSG_DATA(m->hdr); |
| 56 |
nfgen->res_id = htobe16(res_id); |
| 57 |
|
| 58 |
return 0; |
| 59 |
} |
| 60 |
|
| 61 |
static int nfnl_message_get_subsys(sd_netlink_message *m, uint16_t *ret) { |
| 62 |
uint16_t t; |
| 63 |
int r; |
| 64 |
|
| 65 |
assert(m); |
| 66 |
assert(ret); |
| 67 |
|
| 68 |
r = sd_netlink_message_get_type(m, &t); |
| 69 |
if (r < 0) |
| 70 |
return r; |
| 71 |
|
| 72 |
*ret = NFNL_SUBSYS_ID(t); |
| 73 |
return 0; |
| 74 |
} |
| 75 |
|
| 76 |
static int nfnl_message_new_batch(sd_netlink *nfnl, sd_netlink_message **ret, uint16_t subsys, uint16_t msg_type) { |
| 77 |
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL; |
| 78 |
int r; |
| 79 |
|
| 80 |
assert_return(nfnl, -EINVAL); |
| 81 |
assert_return(ret, -EINVAL); |
| 82 |
assert_return(NFNL_MSG_TYPE(msg_type) == msg_type, -EINVAL); |
| 83 |
|
| 84 |
r = sd_nfnl_message_new(nfnl, &m, NFPROTO_UNSPEC, NFNL_SUBSYS_NONE, msg_type, 0); |
| 85 |
if (r < 0) |
| 86 |
return r; |
| 87 |
|
| 88 |
r = nfnl_message_set_res_id(m, subsys); |
| 89 |
if (r < 0) |
| 90 |
return r; |
| 91 |
|
| 92 |
*ret = TAKE_PTR(m); |
| 93 |
return 0; |
| 94 |
} |
| 95 |
|
| 96 |
int sd_nfnl_send_batch( |
| 97 |
sd_netlink *nfnl, |
| 98 |
sd_netlink_message **messages, |
| 99 |
size_t n_messages, |
| 100 |
uint32_t **ret_serials) { |
| 101 |
|
| 102 |
/* iovs refs batch_begin and batch_end, hence, free iovs first, then free batch_begin and batch_end. */ |
| 103 |
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *batch_begin = NULL, *batch_end = NULL; |
| 104 |
_cleanup_free_ struct iovec *iovs = NULL; |
| 105 |
_cleanup_free_ uint32_t *serials = NULL; |
| 106 |
uint16_t subsys; |
| 107 |
ssize_t k; |
| 108 |
size_t c = 0; |
| 109 |
int r; |
| 110 |
|
| 111 |
assert_return(nfnl, -EINVAL); |
| 112 |
assert_return(!netlink_pid_changed(nfnl), -ECHILD); |
| 113 |
assert_return(messages, -EINVAL); |
| 114 |
assert_return(n_messages > 0, -EINVAL); |
| 115 |
|
| 116 |
iovs = new(struct iovec, n_messages + 2); |
| 117 |
if (!iovs) |
| 118 |
return -ENOMEM; |
| 119 |
|
| 120 |
if (ret_serials) { |
| 121 |
serials = new(uint32_t, n_messages + 2); |
| 122 |
if (!serials) |
| 123 |
return -ENOMEM; |
| 124 |
} |
| 125 |
|
| 126 |
r = nfnl_message_get_subsys(messages[0], &subsys); |
| 127 |
if (r < 0) |
| 128 |
return r; |
| 129 |
|
| 130 |
r = nfnl_message_new_batch(nfnl, &batch_begin, subsys, NFNL_MSG_BATCH_BEGIN); |
| 131 |
if (r < 0) |
| 132 |
return r; |
| 133 |
|
| 134 |
netlink_seal_message(nfnl, batch_begin); |
| 135 |
if (serials) |
| 136 |
serials[c] = message_get_serial(batch_begin); |
| 137 |
|
| 138 |
iovs[c++] = IOVEC_MAKE(batch_begin->hdr, batch_begin->hdr->nlmsg_len); |
| 139 |
|
| 140 |
for (size_t i = 0; i < n_messages; i++) { |
| 141 |
uint16_t s; |
| 142 |
|
| 143 |
r = nfnl_message_get_subsys(messages[i], &s); |
| 144 |
if (r < 0) |
| 145 |
return r; |
| 146 |
|
| 147 |
if (s != subsys) |
| 148 |
return -EINVAL; |
| 149 |
|
| 150 |
netlink_seal_message(nfnl, messages[i]); |
| 151 |
if (serials) |
| 152 |
serials[c] = message_get_serial(messages[i]); |
| 153 |
|
| 154 |
/* It seems that the kernel accepts an arbitrary number. Let's set the lower 16 bits of the |
| 155 |
* serial of the first message. */ |
| 156 |
nfnl_message_set_res_id(messages[i], (uint16_t) (message_get_serial(batch_begin) & UINT16_MAX)); |
| 157 |
|
| 158 |
iovs[c++] = IOVEC_MAKE(messages[i]->hdr, messages[i]->hdr->nlmsg_len); |
| 159 |
} |
| 160 |
|
| 161 |
r = nfnl_message_new_batch(nfnl, &batch_end, subsys, NFNL_MSG_BATCH_END); |
| 162 |
if (r < 0) |
| 163 |
return r; |
| 164 |
|
| 165 |
netlink_seal_message(nfnl, batch_end); |
| 166 |
if (serials) |
| 167 |
serials[c] = message_get_serial(batch_end); |
| 168 |
|
| 169 |
iovs[c++] = IOVEC_MAKE(batch_end->hdr, batch_end->hdr->nlmsg_len); |
| 170 |
|
| 171 |
assert(c == n_messages + 2); |
| 172 |
k = writev(nfnl->fd, iovs, n_messages + 2); |
| 173 |
if (k < 0) |
| 174 |
return -errno; |
| 175 |
|
| 176 |
if (ret_serials) |
| 177 |
*ret_serials = TAKE_PTR(serials); |
| 178 |
|
| 179 |
return 0; |
| 180 |
} |
| 181 |
|
| 182 |
int sd_nfnl_call_batch( |
| 183 |
sd_netlink *nfnl, |
| 184 |
sd_netlink_message **messages, |
| 185 |
size_t n_messages, |
| 186 |
uint64_t usec) { |
| 187 |
|
| 188 |
_cleanup_free_ uint32_t *serials = NULL; |
| 189 |
int r; |
| 190 |
|
| 191 |
assert_return(nfnl, -EINVAL); |
| 192 |
assert_return(!netlink_pid_changed(nfnl), -ECHILD); |
| 193 |
assert_return(messages, -EINVAL); |
| 194 |
assert_return(n_messages > 0, -EINVAL); |
| 195 |
|
| 196 |
r = sd_nfnl_send_batch(nfnl, messages, n_messages, &serials); |
| 197 |
if (r < 0) |
| 198 |
return r; |
| 199 |
|
| 200 |
for (size_t i = 1; i <= n_messages; i++) { |
| 201 |
/* If we have received an error, kernel may not send replies for later messages. Let's ignore |
| 202 |
* remaining replies. */ |
| 203 |
if (r < 0) { |
| 204 |
(void) sd_netlink_ignore_serial(nfnl, serials[i], usec); |
| 205 |
continue; |
| 206 |
} |
| 207 |
|
| 208 |
r = sd_netlink_read(nfnl, serials[i], usec, /* ret= */ NULL); |
| 209 |
if (r != -ETIMEDOUT) |
| 210 |
continue; |
| 211 |
|
| 212 |
/* The kernel returns some errors, e.g. unprivileged, to the BATCH_BEGIN. Hence, if we have |
| 213 |
* not received any replies for the batch body, try to read an error in the reply for the |
| 214 |
* batch begin. Note, since v6.10 (bf2ac490d28c21a349e9eef81edc45320fca4a3c), we can expect |
| 215 |
* that the kernel always replies the batch begin and end. When we bump the kernel baseline, |
| 216 |
* we can read the reply for the batch begin at first. */ |
| 217 |
int k = sd_netlink_read(nfnl, serials[0], usec, /* ret= */ NULL); |
| 218 |
if (k < 0) |
| 219 |
r = k; |
| 220 |
|
| 221 |
serials[0] = 0; /* indicates that we have read the reply. */ |
| 222 |
} |
| 223 |
|
| 224 |
/* Ignore replies for batch begin and end if we have not read them. */ |
| 225 |
if (serials[0] != 0) |
| 226 |
(void) sd_netlink_ignore_serial(nfnl, serials[0], usec); |
| 227 |
(void) sd_netlink_ignore_serial(nfnl, serials[n_messages + 1], usec); |
| 228 |
|
| 229 |
return r; |
| 230 |
} |
| 231 |
|
| 232 |
int sd_nfnl_nft_message_new_basechain( |
| 233 |
sd_netlink *nfnl, |
| 234 |
sd_netlink_message **ret, |
| 235 |
int nfproto, |
| 236 |
const char *table, |
| 237 |
const char *chain, |
| 238 |
const char *type, |
| 239 |
uint8_t hook, |
| 240 |
int prio) { |
| 241 |
|
| 242 |
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL; |
| 243 |
int r; |
| 244 |
|
| 245 |
assert(ret); |
| 246 |
|
| 247 |
r = sd_nfnl_message_new(nfnl, &m, nfproto, NFNL_SUBSYS_NFTABLES, NFT_MSG_NEWCHAIN, NLM_F_CREATE); |
| 248 |
if (r < 0) |
| 249 |
return r; |
| 250 |
|
| 251 |
r = sd_netlink_message_append_string(m, NFTA_CHAIN_TABLE, table); |
| 252 |
if (r < 0) |
| 253 |
return r; |
| 254 |
|
| 255 |
r = sd_netlink_message_append_string(m, NFTA_CHAIN_NAME, chain); |
| 256 |
if (r < 0) |
| 257 |
return r; |
| 258 |
|
| 259 |
r = sd_netlink_message_append_string(m, NFTA_CHAIN_TYPE, type); |
| 260 |
if (r < 0) |
| 261 |
return r; |
| 262 |
|
| 263 |
r = sd_netlink_message_open_container(m, NFTA_CHAIN_HOOK); |
| 264 |
if (r < 0) |
| 265 |
return r; |
| 266 |
|
| 267 |
r = sd_netlink_message_append_u32(m, NFTA_HOOK_HOOKNUM, htobe32(hook)); |
| 268 |
if (r < 0) |
| 269 |
return r; |
| 270 |
|
| 271 |
r = sd_netlink_message_append_u32(m, NFTA_HOOK_PRIORITY, htobe32(prio)); |
| 272 |
if (r < 0) |
| 273 |
return r; |
| 274 |
|
| 275 |
r = sd_netlink_message_close_container(m); |
| 276 |
if (r < 0) |
| 277 |
return r; |
| 278 |
|
| 279 |
*ret = TAKE_PTR(m); |
| 280 |
return 0; |
| 281 |
} |
| 282 |
|
| 283 |
int sd_nfnl_nft_message_new_table( |
| 284 |
sd_netlink *nfnl, |
| 285 |
sd_netlink_message **ret, |
| 286 |
int nfproto, |
| 287 |
const char *table) { |
| 288 |
|
| 289 |
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL; |
| 290 |
int r; |
| 291 |
|
| 292 |
assert(ret); |
| 293 |
|
| 294 |
r = sd_nfnl_message_new(nfnl, &m, nfproto, NFNL_SUBSYS_NFTABLES, NFT_MSG_NEWTABLE, NLM_F_CREATE | NLM_F_EXCL); |
| 295 |
if (r < 0) |
| 296 |
return r; |
| 297 |
|
| 298 |
r = sd_netlink_message_append_string(m, NFTA_TABLE_NAME, table); |
| 299 |
if (r < 0) |
| 300 |
return r; |
| 301 |
|
| 302 |
*ret = TAKE_PTR(m); |
| 303 |
return r; |
| 304 |
} |
| 305 |
|
| 306 |
int sd_nfnl_nft_message_new_rule( |
| 307 |
sd_netlink *nfnl, |
| 308 |
sd_netlink_message **ret, |
| 309 |
int nfproto, |
| 310 |
const char *table, |
| 311 |
const char *chain) { |
| 312 |
|
| 313 |
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL; |
| 314 |
int r; |
| 315 |
|
| 316 |
assert(ret); |
| 317 |
|
| 318 |
r = sd_nfnl_message_new(nfnl, &m, nfproto, NFNL_SUBSYS_NFTABLES, NFT_MSG_NEWRULE, NLM_F_CREATE); |
| 319 |
if (r < 0) |
| 320 |
return r; |
| 321 |
|
| 322 |
r = sd_netlink_message_append_string(m, NFTA_RULE_TABLE, table); |
| 323 |
if (r < 0) |
| 324 |
return r; |
| 325 |
|
| 326 |
r = sd_netlink_message_append_string(m, NFTA_RULE_CHAIN, chain); |
| 327 |
if (r < 0) |
| 328 |
return r; |
| 329 |
|
| 330 |
*ret = TAKE_PTR(m); |
| 331 |
return r; |
| 332 |
} |
| 333 |
|
| 334 |
int sd_nfnl_nft_message_new_set( |
| 335 |
sd_netlink *nfnl, |
| 336 |
sd_netlink_message **ret, |
| 337 |
int nfproto, |
| 338 |
const char *table, |
| 339 |
const char *set_name, |
| 340 |
uint32_t set_id, |
| 341 |
uint32_t klen) { |
| 342 |
|
| 343 |
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL; |
| 344 |
int r; |
| 345 |
|
| 346 |
assert(ret); |
| 347 |
|
| 348 |
r = sd_nfnl_message_new(nfnl, &m, nfproto, NFNL_SUBSYS_NFTABLES, NFT_MSG_NEWSET, NLM_F_CREATE); |
| 349 |
if (r < 0) |
| 350 |
return r; |
| 351 |
|
| 352 |
r = sd_netlink_message_append_string(m, NFTA_SET_TABLE, table); |
| 353 |
if (r < 0) |
| 354 |
return r; |
| 355 |
|
| 356 |
r = sd_netlink_message_append_string(m, NFTA_SET_NAME, set_name); |
| 357 |
if (r < 0) |
| 358 |
return r; |
| 359 |
|
| 360 |
r = sd_netlink_message_append_u32(m, NFTA_SET_ID, ++set_id); |
| 361 |
if (r < 0) |
| 362 |
return r; |
| 363 |
|
| 364 |
r = sd_netlink_message_append_u32(m, NFTA_SET_KEY_LEN, htobe32(klen)); |
| 365 |
if (r < 0) |
| 366 |
return r; |
| 367 |
|
| 368 |
*ret = TAKE_PTR(m); |
| 369 |
return r; |
| 370 |
} |
| 371 |
|
| 372 |
int sd_nfnl_nft_message_new_setelems( |
| 373 |
sd_netlink *nfnl, |
| 374 |
sd_netlink_message **ret, |
| 375 |
int add, /* boolean */ |
| 376 |
int nfproto, |
| 377 |
const char *table, |
| 378 |
const char *set_name) { |
| 379 |
|
| 380 |
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL; |
| 381 |
int r; |
| 382 |
|
| 383 |
assert(ret); |
| 384 |
|
| 385 |
if (add) |
| 386 |
r = sd_nfnl_message_new(nfnl, &m, nfproto, NFNL_SUBSYS_NFTABLES, NFT_MSG_NEWSETELEM, NLM_F_CREATE); |
| 387 |
else |
| 388 |
r = sd_nfnl_message_new(nfnl, &m, nfproto, NFNL_SUBSYS_NFTABLES, NFT_MSG_DELSETELEM, 0); |
| 389 |
if (r < 0) |
| 390 |
return r; |
| 391 |
|
| 392 |
r = sd_netlink_message_append_string(m, NFTA_SET_ELEM_LIST_TABLE, table); |
| 393 |
if (r < 0) |
| 394 |
return r; |
| 395 |
|
| 396 |
r = sd_netlink_message_append_string(m, NFTA_SET_ELEM_LIST_SET, set_name); |
| 397 |
if (r < 0) |
| 398 |
return r; |
| 399 |
|
| 400 |
*ret = TAKE_PTR(m); |
| 401 |
return r; |
| 402 |
} |
| 403 |
|
| 404 |
int sd_nfnl_nft_message_append_setelem( |
| 405 |
sd_netlink_message *m, |
| 406 |
uint32_t index, |
| 407 |
const void *key, |
| 408 |
size_t key_len, |
| 409 |
const void *data, |
| 410 |
size_t data_len, |
| 411 |
uint32_t flags) { |
| 412 |
|
| 413 |
int r; |
| 414 |
|
| 415 |
r = sd_netlink_message_open_array(m, index); |
| 416 |
if (r < 0) |
| 417 |
return r; |
| 418 |
|
| 419 |
r = sd_netlink_message_append_container_data(m, NFTA_SET_ELEM_KEY, NFTA_DATA_VALUE, key, key_len); |
| 420 |
if (r < 0) |
| 421 |
goto cancel; |
| 422 |
|
| 423 |
if (data) { |
| 424 |
r = sd_netlink_message_append_container_data(m, NFTA_SET_ELEM_DATA, NFTA_DATA_VALUE, data, data_len); |
| 425 |
if (r < 0) |
| 426 |
goto cancel; |
| 427 |
} |
| 428 |
|
| 429 |
if (flags != 0) { |
| 430 |
r = sd_netlink_message_append_u32(m, NFTA_SET_ELEM_FLAGS, htobe32(flags)); |
| 431 |
if (r < 0) |
| 432 |
goto cancel; |
| 433 |
} |
| 434 |
|
| 435 |
return sd_netlink_message_close_container(m); /* array */ |
| 436 |
|
| 437 |
cancel: |
| 438 |
(void) sd_netlink_message_cancel_array(m); |
| 439 |
return r; |
| 440 |
} |
| 441 |
|
| 442 |
int sd_nfnl_socket_open(sd_netlink **ret) { |
| 443 |
return netlink_open_family(ret, NETLINK_NETFILTER); |
| 444 |
} |