| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2013-2017 Pablo Neira Ayuso <pablo@netfilter.org> | ||
| 3 | * | ||
| 4 | * This program is free software; you can redistribute it and/or modify | ||
| 5 | * it under the terms of the GNU General Public License version 2 (or any | ||
| 6 | * later) as published by the Free Software Foundation. | ||
| 7 | * | ||
| 8 | * Development of this code funded by Astaro AG (http://www.astaro.com/) | ||
| 9 | */ | ||
| 10 | |||
| 11 | #include <nft.h> | ||
| 12 | #include <iface.h> | ||
| 13 | #include <nftversion.h> | ||
| 14 | |||
| 15 | #include <libmnl/libmnl.h> | ||
| 16 | #include <libnftnl/common.h> | ||
| 17 | #include <libnftnl/ruleset.h> | ||
| 18 | #include <libnftnl/table.h> | ||
| 19 | #include <libnftnl/chain.h> | ||
| 20 | #include <libnftnl/rule.h> | ||
| 21 | #include <libnftnl/expr.h> | ||
| 22 | #include <libnftnl/set.h> | ||
| 23 | #include <libnftnl/object.h> | ||
| 24 | #include <libnftnl/flowtable.h> | ||
| 25 | #include <libnftnl/batch.h> | ||
| 26 | #include <libnftnl/udata.h> | ||
| 27 | |||
| 28 | #include <linux/netfilter/nfnetlink.h> | ||
| 29 | #include <linux/netfilter/nfnetlink_hook.h> | ||
| 30 | #include <linux/netfilter/nf_tables.h> | ||
| 31 | |||
| 32 | #include <mnl.h> | ||
| 33 | #include <cmd.h> | ||
| 34 | #include <intervals.h> | ||
| 35 | #include <net/if.h> | ||
| 36 | #include <sys/socket.h> | ||
| 37 | #include <arpa/inet.h> | ||
| 38 | #include <fcntl.h> | ||
| 39 | #include <errno.h> | ||
| 40 | #include <utils.h> | ||
| 41 | #include <nftables.h> | ||
| 42 | #include <linux/netfilter.h> | ||
| 43 | #include <linux/netfilter_arp.h> | ||
| 44 | |||
| 45 | struct basehook { | ||
| 46 | struct list_head list; | ||
| 47 | const char *module_name; | ||
| 48 | const char *hookfn; | ||
| 49 | const char *table; | ||
| 50 | const char *chain; | ||
| 51 | const char *devname; | ||
| 52 | const char *objtype; | ||
| 53 | int family; | ||
| 54 | int chain_family; | ||
| 55 | uint32_t num; | ||
| 56 | int prio; | ||
| 57 | }; | ||
| 58 | |||
| 59 | 13022 | struct mnl_socket *nft_mnl_socket_open(void) | |
| 60 | { | ||
| 61 | struct mnl_socket *nf_sock; | ||
| 62 | 13022 | int one = 1; | |
| 63 | |||
| 64 | 13022 | nf_sock = mnl_socket_open(NETLINK_NETFILTER); | |
| 65 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→6) taken 13022 times.
|
13022 | if (!nf_sock) |
| 66 | ✗ | netlink_init_error(); | |
| 67 | |||
| 68 |
1/2✗ Branch 0 (8→9) not taken.
✓ Branch 1 (8→11) taken 13022 times.
|
13022 | if (fcntl(mnl_socket_get_fd(nf_sock), F_SETFL, O_NONBLOCK)) |
| 69 | ✗ | netlink_init_error(); | |
| 70 | |||
| 71 | 13022 | mnl_socket_setsockopt(nf_sock, NETLINK_EXT_ACK, &one, sizeof(one)); | |
| 72 | |||
| 73 | 13022 | return nf_sock; | |
| 74 | } | ||
| 75 | |||
| 76 | 480621 | uint32_t mnl_seqnum_inc(unsigned int *seqnum) | |
| 77 | { | ||
| 78 | 480621 | return (*seqnum)++; | |
| 79 | } | ||
| 80 | |||
| 81 | /* The largest nf_tables netlink message is the set element message, which | ||
| 82 | * contains the NFTA_SET_ELEM_LIST_ELEMENTS attribute. This attribute is | ||
| 83 | * a nest that describes the set elements. Given that the netlink attribute | ||
| 84 | * length (nla_len) is 16 bits, the largest message is a bit larger than | ||
| 85 | * 64 KBytes. | ||
| 86 | */ | ||
| 87 | #define NFT_NLMSG_MAXSIZE (UINT16_MAX + getpagesize()) | ||
| 88 | |||
| 89 | static int | ||
| 90 | 282796 | nft_mnl_recv(struct netlink_ctx *ctx, uint32_t portid, | |
| 91 | int (*cb)(const struct nlmsghdr *nlh, void *data), void *cb_data) | ||
| 92 | 282796 | { | |
| 93 | 490672 | char buf[NFT_NLMSG_MAXSIZE]; | |
| 94 | 282796 | bool eintr = false; | |
| 95 | int ret; | ||
| 96 | |||
| 97 | 282796 | ret = mnl_socket_recvfrom(ctx->nft->nf_sock, buf, sizeof(buf)); | |
| 98 |
2/2✓ Branch 0 (15→4) taken 379386 times.
✓ Branch 1 (15→16) taken 111286 times.
|
490672 | while (ret > 0) { |
| 99 | 379386 | ret = mnl_cb_run(buf, ret, ctx->seqnum, portid, cb, cb_data); | |
| 100 |
2/2✓ Branch 0 (5→6) taken 169809 times.
✓ Branch 1 (5→7) taken 209577 times.
|
379386 | if (ret == 0) |
| 101 | 169809 | break; | |
| 102 |
2/2✓ Branch 0 (7→8) taken 1703 times.
✓ Branch 1 (7→13) taken 207874 times.
|
209577 | if (ret < 0) { |
| 103 |
1/2✗ Branch 0 (8→9) not taken.
✓ Branch 1 (8→10) taken 1703 times.
|
1703 | if (errno == EAGAIN) { |
| 104 | ✗ | ret = 0; | |
| 105 | ✗ | break; | |
| 106 | } | ||
| 107 |
2/2✓ Branch 0 (10→11) taken 1701 times.
✓ Branch 1 (10→12) taken 2 times.
|
1703 | if (errno != EINTR) |
| 108 | 1701 | break; | |
| 109 | |||
| 110 | /* process all pending messages before reporting EINTR */ | ||
| 111 | 2 | eintr = true; | |
| 112 | } | ||
| 113 | 207876 | ret = mnl_socket_recvfrom(ctx->nft->nf_sock, buf, sizeof(buf)); | |
| 114 | } | ||
| 115 |
2/2✓ Branch 0 (16→17) taken 2 times.
✓ Branch 1 (16→18) taken 282794 times.
|
282796 | if (eintr) { |
| 116 | 2 | ret = -1; | |
| 117 | 2 | errno = EINTR; | |
| 118 | } | ||
| 119 | 282796 | return ret; | |
| 120 | } | ||
| 121 | |||
| 122 | int | ||
| 123 | 282796 | nft_mnl_talk(struct netlink_ctx *ctx, const void *data, unsigned int len, | |
| 124 | int (*cb)(const struct nlmsghdr *nlh, void *data), void *cb_data) | ||
| 125 | { | ||
| 126 | 282796 | uint32_t portid = mnl_socket_get_portid(ctx->nft->nf_sock); | |
| 127 | |||
| 128 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 282796 times.
|
282796 | if (ctx->nft->debug_mask & NFT_DEBUG_MNL) |
| 129 | ✗ | mnl_nlmsg_fprintf(ctx->nft->output.output_fp, data, len, | |
| 130 | sizeof(struct nfgenmsg)); | ||
| 131 | |||
| 132 |
1/2✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→8) taken 282796 times.
|
282796 | if (mnl_socket_sendto(ctx->nft->nf_sock, data, len) < 0) |
| 133 | ✗ | return -1; | |
| 134 | |||
| 135 | 282796 | return nft_mnl_recv(ctx, portid, cb, cb_data); | |
| 136 | } | ||
| 137 | |||
| 138 | /* | ||
| 139 | * Rule-set consistency check across several netlink dumps | ||
| 140 | */ | ||
| 141 | static uint32_t nft_genid; | ||
| 142 | |||
| 143 | 111286 | static int genid_cb(const struct nlmsghdr *nlh, void *data) | |
| 144 | { | ||
| 145 | 111286 | struct nfgenmsg *nfh = mnl_nlmsg_get_payload(nlh); | |
| 146 | |||
| 147 | 111286 | nft_genid = ntohs(nfh->res_id); | |
| 148 | |||
| 149 | 111286 | return MNL_CB_OK; | |
| 150 | } | ||
| 151 | |||
| 152 | 111286 | uint32_t mnl_genid_get(struct netlink_ctx *ctx) | |
| 153 | 111286 | { | |
| 154 |
1/2✓ Branch 0 (3→4) taken 111286 times.
✗ Branch 1 (3→5) not taken.
|
111286 | char buf[MNL_SOCKET_BUFFER_SIZE]; |
| 155 | struct nlmsghdr *nlh; | ||
| 156 | |||
| 157 | 111286 | nlh = nftnl_nlmsg_build_hdr(buf, NFT_MSG_GETGEN, AF_UNSPEC, 0, ctx->seqnum); | |
| 158 | /* Skip error checking, old kernels sets res_id field to zero. */ | ||
| 159 | 111286 | nft_mnl_talk(ctx, nlh, nlh->nlmsg_len, genid_cb, NULL); | |
| 160 | |||
| 161 | 111286 | return nft_genid; | |
| 162 | } | ||
| 163 | |||
| 164 | 262750 | static uint16_t nft_genid_u16(uint32_t genid) | |
| 165 | { | ||
| 166 | 262750 | return genid & 0xffff; | |
| 167 | } | ||
| 168 | |||
| 169 | 262750 | static int check_genid(const struct nlmsghdr *nlh) | |
| 170 | { | ||
| 171 | 262750 | struct nfgenmsg *nfh = mnl_nlmsg_get_payload(nlh); | |
| 172 | |||
| 173 |
2/2✓ Branch 0 (4→5) taken 2 times.
✓ Branch 1 (4→6) taken 262748 times.
|
262750 | if (nft_genid_u16(nft_genid) != ntohs(nfh->res_id)) { |
| 174 | 2 | errno = EINTR; | |
| 175 | 2 | return -1; | |
| 176 | } | ||
| 177 | 262748 | return 0; | |
| 178 | } | ||
| 179 | |||
| 180 | /* | ||
| 181 | * Batching | ||
| 182 | */ | ||
| 183 | |||
| 184 | /* Selected batch page is 2 Mbytes long to support loading a ruleset of 3.5M | ||
| 185 | * rules matching on source and destination address as well as input and output | ||
| 186 | * interfaces. This is what legacy iptables supports. | ||
| 187 | */ | ||
| 188 | #define BATCH_PAGE_SIZE 2 * 1024 * 1024 | ||
| 189 | |||
| 190 | 54279 | struct nftnl_batch *mnl_batch_init(void) | |
| 191 | { | ||
| 192 | struct nftnl_batch *batch; | ||
| 193 | |||
| 194 | 54279 | batch = nftnl_batch_alloc(BATCH_PAGE_SIZE, NFT_NLMSG_MAXSIZE); | |
| 195 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 54279 times.
|
54279 | if (batch == NULL) |
| 196 | ✗ | memory_allocation_error(); | |
| 197 | |||
| 198 | 54279 | return batch; | |
| 199 | } | ||
| 200 | |||
| 201 | 280894 | static void mnl_nft_batch_continue(struct nftnl_batch *batch) | |
| 202 | { | ||
| 203 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 280894 times.
|
280894 | if (nftnl_batch_update(batch) < 0) |
| 204 | ✗ | memory_allocation_error(); | |
| 205 | 280894 | } | |
| 206 | |||
| 207 | 53974 | uint32_t mnl_batch_begin(struct nftnl_batch *batch, uint32_t seqnum) | |
| 208 | { | ||
| 209 | 53974 | nftnl_batch_begin(nftnl_batch_buffer(batch), seqnum); | |
| 210 | 53974 | mnl_nft_batch_continue(batch); | |
| 211 | |||
| 212 | 53974 | return seqnum; | |
| 213 | } | ||
| 214 | |||
| 215 | 52749 | void mnl_batch_end(struct nftnl_batch *batch, uint32_t seqnum) | |
| 216 | { | ||
| 217 | 52749 | nftnl_batch_end(nftnl_batch_buffer(batch), seqnum); | |
| 218 | 52749 | mnl_nft_batch_continue(batch); | |
| 219 | 52749 | } | |
| 220 | |||
| 221 | 53947 | bool mnl_batch_ready(struct nftnl_batch *batch) | |
| 222 | { | ||
| 223 | /* Check if the batch only contains the initial and trailing batch | ||
| 224 | * messages. In that case, the batch is empty. | ||
| 225 | */ | ||
| 226 | 53947 | return nftnl_batch_buffer_len(batch) != | |
| 227 | (NLMSG_HDRLEN + sizeof(struct nfgenmsg)) * 2; | ||
| 228 | } | ||
| 229 | |||
| 230 | 54279 | void mnl_batch_reset(struct nftnl_batch *batch) | |
| 231 | { | ||
| 232 | 54279 | nftnl_batch_free(batch); | |
| 233 | 54279 | } | |
| 234 | |||
| 235 | 941 | static void mnl_err_list_node_add(struct list_head *err_list, int error, | |
| 236 | int seqnum, uint32_t offset, | ||
| 237 | const char *errmsg) | ||
| 238 | { | ||
| 239 | 941 | struct mnl_err *err = xmalloc(sizeof(struct mnl_err)); | |
| 240 | |||
| 241 | 941 | err->seqnum = seqnum; | |
| 242 | 941 | err->offset = offset; | |
| 243 | 941 | err->err = error; | |
| 244 | 941 | list_add_tail(&err->head, err_list); | |
| 245 | 941 | } | |
| 246 | |||
| 247 | 941 | void mnl_err_list_free(struct mnl_err *err) | |
| 248 | { | ||
| 249 | 941 | list_del(&err->head); | |
| 250 | 941 | free(err); | |
| 251 | 941 | } | |
| 252 | |||
| 253 | 36814 | static void mnl_set_sndbuffer(struct netlink_ctx *ctx) | |
| 254 | { | ||
| 255 | 36814 | struct mnl_socket *nl = ctx->nft->nf_sock; | |
| 256 | 36814 | struct nftnl_batch *batch = ctx->batch; | |
| 257 | 36814 | socklen_t len = sizeof(int); | |
| 258 | 36814 | int sndnlbuffsiz = 0; | |
| 259 | int newbuffsiz; | ||
| 260 | |||
| 261 | 36814 | getsockopt(mnl_socket_get_fd(nl), SOL_SOCKET, SO_SNDBUF, | |
| 262 | &sndnlbuffsiz, &len); | ||
| 263 | |||
| 264 | 36814 | newbuffsiz = nftnl_batch_iovec_len(batch) * BATCH_PAGE_SIZE; | |
| 265 |
2/2✓ Branch 0 (5→6) taken 28536 times.
✓ Branch 1 (5→7) taken 8278 times.
|
36814 | if (newbuffsiz <= sndnlbuffsiz) |
| 266 | 28536 | return; | |
| 267 | |||
| 268 | /* Rise sender buffer length to avoid hitting -EMSGSIZE */ | ||
| 269 | 8278 | setsockopt(mnl_socket_get_fd(nl), SOL_SOCKET, SO_SNDBUF, | |
| 270 | &newbuffsiz, sizeof(socklen_t)); | ||
| 271 | |||
| 272 | /* unpriviledged containers check for CAP_NET_ADMIN on the init_user_ns. */ | ||
| 273 |
1/2✗ Branch 0 (11→12) not taken.
✓ Branch 1 (11→14) taken 8278 times.
|
8278 | if (setsockopt(mnl_socket_get_fd(nl), SOL_SOCKET, SO_SNDBUFFORCE, |
| 274 | &newbuffsiz, sizeof(socklen_t)) < 0) { | ||
| 275 | ✗ | if (errno == EPERM) | |
| 276 | ✗ | ctx->maybe_emsgsize = newbuffsiz; | |
| 277 | } | ||
| 278 | } | ||
| 279 | |||
| 280 | static unsigned int nlsndbufsiz; | ||
| 281 | |||
| 282 | 36814 | static int mnl_set_rcvbuffer(const struct mnl_socket *nl, socklen_t bufsiz) | |
| 283 | { | ||
| 284 | 36814 | socklen_t len = sizeof(nlsndbufsiz); | |
| 285 | int ret; | ||
| 286 | |||
| 287 |
2/2✓ Branch 0 (2→3) taken 8278 times.
✓ Branch 1 (2→5) taken 28536 times.
|
36814 | if (!nlsndbufsiz) { |
| 288 | 8278 | getsockopt(mnl_socket_get_fd(nl), SOL_SOCKET, SO_RCVBUF, | |
| 289 | &nlsndbufsiz, &len); | ||
| 290 | } | ||
| 291 | |||
| 292 |
2/2✓ Branch 0 (5→6) taken 36650 times.
✓ Branch 1 (5→7) taken 164 times.
|
36814 | if (nlsndbufsiz >= bufsiz) |
| 293 | 36650 | return 0; | |
| 294 | |||
| 295 | 164 | ret = setsockopt(mnl_socket_get_fd(nl), SOL_SOCKET, SO_RCVBUFFORCE, | |
| 296 | &bufsiz, sizeof(socklen_t)); | ||
| 297 |
1/2✗ Branch 0 (9→10) not taken.
✓ Branch 1 (9→12) taken 164 times.
|
164 | if (ret < 0) { |
| 298 | /* If this doesn't work, try to reach the system wide maximum | ||
| 299 | * (or whatever the user requested). | ||
| 300 | */ | ||
| 301 | ✗ | ret = setsockopt(mnl_socket_get_fd(nl), SOL_SOCKET, SO_RCVBUF, | |
| 302 | &bufsiz, sizeof(socklen_t)); | ||
| 303 | } | ||
| 304 | |||
| 305 | 164 | return ret; | |
| 306 | } | ||
| 307 | |||
| 308 | 36814 | static void mnl_nft_batch_to_msg(struct netlink_ctx *ctx, struct msghdr *msg, | |
| 309 | const struct sockaddr_nl *snl, | ||
| 310 | struct iovec *iov, unsigned int iov_len) | ||
| 311 | { | ||
| 312 | 36814 | msg->msg_name = (struct sockaddr_nl *)snl; | |
| 313 | 36814 | msg->msg_namelen = sizeof(*snl); | |
| 314 | 36814 | msg->msg_iov = iov; | |
| 315 | 36814 | msg->msg_iovlen = iov_len; | |
| 316 | |||
| 317 | 36814 | nftnl_batch_iovec(ctx->batch, iov, iov_len); | |
| 318 | 36814 | } | |
| 319 | |||
| 320 | 36814 | static ssize_t mnl_nft_socket_sendmsg(struct netlink_ctx *ctx, | |
| 321 | const struct msghdr *msg) | ||
| 322 | { | ||
| 323 | 36814 | uint32_t iov_len = msg->msg_iovlen; | |
| 324 | 36814 | struct iovec *iov = msg->msg_iov; | |
| 325 | unsigned int i; | ||
| 326 | |||
| 327 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→7) taken 36814 times.
|
36814 | if (ctx->nft->debug_mask & NFT_DEBUG_MNL) { |
| 328 | ✗ | for (i = 0; i < iov_len; i++) { | |
| 329 | ✗ | mnl_nlmsg_fprintf(ctx->nft->output.output_fp, | |
| 330 | ✗ | iov[i].iov_base, iov[i].iov_len, | |
| 331 | sizeof(struct nfgenmsg)); | ||
| 332 | } | ||
| 333 | } | ||
| 334 | |||
| 335 | 36814 | return sendmsg(mnl_socket_get_fd(ctx->nft->nf_sock), msg, 0); | |
| 336 | } | ||
| 337 | |||
| 338 | 899 | static int err_attr_cb(const struct nlattr *attr, void *data) | |
| 339 | { | ||
| 340 | 899 | const struct nlattr **tb = data; | |
| 341 | uint16_t type; | ||
| 342 | |||
| 343 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 899 times.
|
899 | if (mnl_attr_type_valid(attr, NLMSGERR_ATTR_MAX) < 0) |
| 344 | ✗ | return MNL_CB_ERROR; | |
| 345 | |||
| 346 | 899 | type = mnl_attr_get_type(attr); | |
| 347 |
1/2✓ Branch 0 (6→7) taken 899 times.
✗ Branch 1 (6→11) not taken.
|
899 | switch (type) { |
| 348 | 899 | case NLMSGERR_ATTR_OFFS: | |
| 349 |
1/2✗ Branch 0 (8→9) not taken.
✓ Branch 1 (8→10) taken 899 times.
|
899 | if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) |
| 350 | ✗ | return MNL_CB_ERROR; | |
| 351 | 899 | break; | |
| 352 | } | ||
| 353 | |||
| 354 | 899 | tb[type] = attr; | |
| 355 | 899 | return MNL_CB_OK; | |
| 356 | } | ||
| 357 | |||
| 358 | 941 | static int mnl_batch_extack_cb(const struct nlmsghdr *nlh, void *data) | |
| 359 | { | ||
| 360 | 941 | struct netlink_cb_data *cb_data = data; | |
| 361 | 941 | struct nlattr *tb[NLMSGERR_ATTR_MAX + 1] = {}; | |
| 362 | 941 | const struct nlmsgerr *err = mnl_nlmsg_get_payload(nlh); | |
| 363 | 941 | unsigned int hlen = sizeof(*err); | |
| 364 | 941 | const char *msg = NULL; | |
| 365 | 941 | uint32_t off = 0; | |
| 366 | int errval; | ||
| 367 | |||
| 368 |
1/2✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→6) taken 941 times.
|
941 | if (nlh->nlmsg_len < mnl_nlmsg_size(sizeof(struct nlmsgerr))) |
| 369 | ✗ | return MNL_CB_ERROR; | |
| 370 | |||
| 371 |
1/2✓ Branch 0 (6→7) taken 941 times.
✗ Branch 1 (6→8) not taken.
|
941 | if (err->error < 0) |
| 372 | 941 | errval = -err->error; | |
| 373 | else | ||
| 374 | ✗ | errval = err->error; | |
| 375 | |||
| 376 |
1/2✗ Branch 0 (9→10) not taken.
✓ Branch 1 (9→11) taken 941 times.
|
941 | if (errval == 0) |
| 377 | ✗ | return MNL_CB_STOP; | |
| 378 | |||
| 379 |
1/2✓ Branch 0 (11→12) taken 941 times.
✗ Branch 1 (11→14) not taken.
|
941 | if (!(nlh->nlmsg_flags & NLM_F_CAPPED)) |
| 380 | 941 | hlen += mnl_nlmsg_get_payload_len(&err->msg); | |
| 381 | |||
| 382 |
1/2✗ Branch 0 (15→16) not taken.
✓ Branch 1 (15→17) taken 941 times.
|
941 | if (mnl_attr_parse(nlh, hlen, err_attr_cb, tb) != MNL_CB_OK) |
| 383 | ✗ | return MNL_CB_ERROR; | |
| 384 | |||
| 385 |
2/2✓ Branch 0 (17→18) taken 899 times.
✓ Branch 1 (17→19) taken 42 times.
|
941 | if (tb[NLMSGERR_ATTR_OFFS]) |
| 386 | 899 | off = mnl_attr_get_u32(tb[NLMSGERR_ATTR_OFFS]); | |
| 387 | |||
| 388 | 941 | mnl_err_list_node_add(cb_data->err_list, errval, | |
| 389 | 941 | nlh->nlmsg_seq, off, msg); | |
| 390 | 941 | return MNL_CB_ERROR; | |
| 391 | } | ||
| 392 | |||
| 393 | #define NFT_MNL_ECHO_RCVBUFF_DEFAULT (MNL_SOCKET_BUFFER_SIZE * 1024U) | ||
| 394 | #define NFT_MNL_ACK_MAXSIZE ((sizeof(struct nlmsghdr) + \ | ||
| 395 | sizeof(struct nfgenmsg) + (1 << 16)) + \ | ||
| 396 | MNL_SOCKET_BUFFER_SIZE) | ||
| 397 | |||
| 398 | 36814 | int mnl_batch_talk(struct netlink_ctx *ctx, struct list_head *err_list, | |
| 399 | uint32_t num_cmds) | ||
| 400 | 36814 | { | |
| 401 | 36814 | struct mnl_socket *nl = ctx->nft->nf_sock; | |
| 402 | 36814 | int ret, fd = mnl_socket_get_fd(nl), portid = mnl_socket_get_portid(nl); | |
| 403 | 36814 | uint32_t iov_len = nftnl_batch_iovec_len(ctx->batch); | |
| 404 |
1/2✓ Branch 0 (6→7) taken 36814 times.
✗ Branch 1 (6→9) not taken.
|
36814 | char rcv_buf[NFT_MNL_ACK_MAXSIZE]; |
| 405 | 36814 | const struct sockaddr_nl snl = { | |
| 406 | .nl_family = AF_NETLINK | ||
| 407 | }; | ||
| 408 | 36814 | struct timeval tv = { | |
| 409 | .tv_sec = 0, | ||
| 410 | .tv_usec = 0 | ||
| 411 | }; | ||
| 412 | 36814 | struct iovec iov[iov_len]; | |
| 413 | 36814 | struct msghdr msg = {}; | |
| 414 | unsigned int rcvbufsiz; | ||
| 415 | fd_set readfds; | ||
| 416 | static mnl_cb_t cb_ctl_array[NLMSG_MIN_TYPE] = { | ||
| 417 | [NLMSG_ERROR] = mnl_batch_extack_cb, | ||
| 418 | }; | ||
| 419 | 36814 | struct netlink_cb_data cb_data = { | |
| 420 | .err_list = err_list, | ||
| 421 | .nl_ctx = ctx, | ||
| 422 | }; | ||
| 423 | |||
| 424 | 36814 | mnl_set_sndbuffer(ctx); | |
| 425 | |||
| 426 | 36814 | mnl_nft_batch_to_msg(ctx, &msg, &snl, iov, iov_len); | |
| 427 | |||
| 428 | 36814 | rcvbufsiz = num_cmds * 1024; | |
| 429 |
2/2✓ Branch 0 (13→14) taken 147 times.
✓ Branch 1 (13→25) taken 36667 times.
|
36814 | if (nft_output_echo(&ctx->nft->output)) { |
| 430 |
2/4✓ Branch 0 (15→16) taken 147 times.
✗ Branch 1 (15→18) not taken.
✓ Branch 2 (19→20) taken 147 times.
✗ Branch 3 (19→25) not taken.
|
147 | if (rcvbufsiz < NFT_MNL_ECHO_RCVBUFF_DEFAULT) |
| 431 |
1/2✓ Branch 0 (21→22) taken 147 times.
✗ Branch 1 (21→24) not taken.
|
147 | rcvbufsiz = NFT_MNL_ECHO_RCVBUFF_DEFAULT; |
| 432 | } | ||
| 433 | |||
| 434 | 36814 | mnl_set_rcvbuffer(ctx->nft->nf_sock, rcvbufsiz); | |
| 435 | |||
| 436 | 36814 | ret = mnl_nft_socket_sendmsg(ctx, &msg); | |
| 437 |
1/2✗ Branch 0 (27→28) not taken.
✓ Branch 1 (27→29) taken 36814 times.
|
36814 | if (ret == -1) |
| 438 | ✗ | return -1; | |
| 439 | |||
| 440 | /* receive and digest all the acknowledgments from the kernel. */ | ||
| 441 | while (true) { | ||
| 442 |
2/2✓ Branch 0 (31→30) taken 611168 times.
✓ Branch 1 (31→32) taken 38198 times.
|
649366 | FD_ZERO(&readfds); |
| 443 | 38198 | FD_SET(fd, &readfds); | |
| 444 | |||
| 445 | 38198 | ret = select(fd + 1, &readfds, NULL, NULL, &tv); | |
| 446 |
1/2✗ Branch 0 (33→34) not taken.
✓ Branch 1 (33→35) taken 38198 times.
|
38198 | if (ret == -1) |
| 447 | ✗ | return -1; | |
| 448 | |||
| 449 |
2/2✓ Branch 0 (35→36) taken 36814 times.
✓ Branch 1 (35→37) taken 1384 times.
|
38198 | if (!FD_ISSET(fd, &readfds)) |
| 450 | 36814 | break; | |
| 451 | |||
| 452 | 1384 | ret = mnl_socket_recvfrom(nl, rcv_buf, sizeof(rcv_buf)); | |
| 453 |
1/2✗ Branch 0 (38→39) not taken.
✓ Branch 1 (38→42) taken 1384 times.
|
1384 | if (ret == -1) { |
| 454 | /* Too many errors, not all errors are displayed. */ | ||
| 455 | ✗ | if (errno == ENOBUFS) | |
| 456 | ✗ | continue; | |
| 457 | |||
| 458 | ✗ | return -1; | |
| 459 | } | ||
| 460 | |||
| 461 | /* Continue on error, make sure we get all acknowledgments */ | ||
| 462 | 1384 | ret = mnl_cb_run2(rcv_buf, ret, 0, portid, | |
| 463 | netlink_echo_callback, &cb_data, | ||
| 464 | cb_ctl_array, MNL_ARRAY_SIZE(cb_ctl_array)); | ||
| 465 | } | ||
| 466 | 36814 | return 0; | |
| 467 | } | ||
| 468 | |||
| 469 | struct mnl_nft_rule_build_ctx { | ||
| 470 | struct netlink_linearize_ctx *lctx; | ||
| 471 | struct nlmsghdr *nlh; | ||
| 472 | struct cmd *cmd; | ||
| 473 | }; | ||
| 474 | |||
| 475 | 251904 | static int mnl_nft_expr_build_cb(struct nftnl_expr *nle, void *data) | |
| 476 | { | ||
| 477 | 251904 | struct mnl_nft_rule_build_ctx *ctx = data; | |
| 478 | 251904 | struct nlmsghdr *nlh = ctx->nlh; | |
| 479 | 251904 | struct cmd *cmd = ctx->cmd; | |
| 480 | struct nft_expr_loc *eloc; | ||
| 481 | struct nlattr *nest; | ||
| 482 | |||
| 483 | 251904 | eloc = nft_expr_loc_find(nle, ctx->lctx); | |
| 484 |
2/2✓ Branch 0 (3→4) taken 251819 times.
✓ Branch 1 (3→5) taken 85 times.
|
251904 | if (eloc) |
| 485 | 251819 | cmd_add_loc(cmd, nlh, eloc->loc); | |
| 486 | |||
| 487 | 251904 | nest = mnl_attr_nest_start(nlh, NFTA_LIST_ELEM); | |
| 488 | 251904 | nftnl_expr_build_payload(nlh, nle); | |
| 489 | 251904 | mnl_attr_nest_end(nlh, nest); | |
| 490 | |||
| 491 | 251904 | nftnl_rule_del_expr(nle); | |
| 492 | 251904 | nftnl_expr_free(nle); | |
| 493 | |||
| 494 | 251904 | return 0; | |
| 495 | } | ||
| 496 | |||
| 497 | 108372 | static void mnl_nft_rule_build_ctx_init(struct mnl_nft_rule_build_ctx *rule_ctx, | |
| 498 | struct nlmsghdr *nlh, | ||
| 499 | struct cmd *cmd, | ||
| 500 | struct netlink_linearize_ctx *lctx) | ||
| 501 | { | ||
| 502 | 108372 | memset(rule_ctx, 0, sizeof(*rule_ctx)); | |
| 503 | 108372 | rule_ctx->nlh = nlh; | |
| 504 | 108372 | rule_ctx->cmd = cmd; | |
| 505 | 108372 | rule_ctx->lctx = lctx; | |
| 506 | 108372 | } | |
| 507 | |||
| 508 | 108361 | int mnl_nft_rule_add(struct netlink_ctx *ctx, struct cmd *cmd, | |
| 509 | unsigned int flags) | ||
| 510 | { | ||
| 511 | struct mnl_nft_rule_build_ctx rule_ctx; | ||
| 512 | struct netlink_linearize_ctx lctx; | ||
| 513 | 108361 | struct rule *rule = cmd->rule; | |
| 514 | 108361 | struct handle *h = &rule->handle; | |
| 515 | struct nftnl_rule *nlr; | ||
| 516 | struct nlmsghdr *nlh; | ||
| 517 | struct nlattr *nest; | ||
| 518 | |||
| 519 | 108361 | nlr = nftnl_rule_alloc(); | |
| 520 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 108361 times.
|
108361 | if (!nlr) |
| 521 | ✗ | memory_allocation_error(); | |
| 522 | |||
| 523 | 108361 | nftnl_rule_set_u32(nlr, NFTNL_RULE_FAMILY, h->family); | |
| 524 |
2/2✓ Branch 0 (6→7) taken 29 times.
✓ Branch 1 (6→8) taken 108332 times.
|
108361 | if (h->position.id) |
| 525 | 29 | nftnl_rule_set_u64(nlr, NFTNL_RULE_POSITION, h->position.id); | |
| 526 |
2/2✓ Branch 0 (8→9) taken 15 times.
✓ Branch 1 (8→10) taken 108346 times.
|
108361 | if (h->rule_id) |
| 527 | 15 | nftnl_rule_set_u32(nlr, NFTNL_RULE_ID, h->rule_id); | |
| 528 |
2/2✓ Branch 0 (10→11) taken 15 times.
✓ Branch 1 (10→12) taken 108346 times.
|
108361 | if (h->position_id) |
| 529 | 15 | nftnl_rule_set_u32(nlr, NFTNL_RULE_POSITION_ID, h->position_id); | |
| 530 | |||
| 531 | 108361 | netlink_linearize_init(&lctx, nlr); | |
| 532 | 108361 | netlink_linearize_rule(ctx, rule, &lctx); | |
| 533 | 108361 | nlh = nftnl_nlmsg_build_hdr(nftnl_batch_buffer(ctx->batch), | |
| 534 | NFT_MSG_NEWRULE, | ||
| 535 | 108361 | cmd->handle.family, | |
| 536 | 108361 | NLM_F_CREATE | flags, ctx->seqnum); | |
| 537 | |||
| 538 | 108361 | cmd_add_loc(cmd, nlh, &h->table.location); | |
| 539 | 108361 | mnl_attr_put_strz(nlh, NFTA_RULE_TABLE, h->table.name); | |
| 540 | 108361 | cmd_add_loc(cmd, nlh, &h->chain.location); | |
| 541 | |||
| 542 |
2/2✓ Branch 0 (19→20) taken 112 times.
✓ Branch 1 (19→21) taken 108249 times.
|
108361 | if (h->chain_id) |
| 543 | 112 | mnl_attr_put_u32(nlh, NFTA_RULE_CHAIN_ID, htonl(h->chain_id)); | |
| 544 | else | ||
| 545 | 108249 | mnl_attr_put_strz(nlh, NFTA_RULE_CHAIN, h->chain.name); | |
| 546 | |||
| 547 | 108361 | mnl_nft_rule_build_ctx_init(&rule_ctx, nlh, cmd, &lctx); | |
| 548 | |||
| 549 | 108361 | nest = mnl_attr_nest_start(nlh, NFTA_RULE_EXPRESSIONS); | |
| 550 | 108361 | nftnl_expr_foreach(nlr, mnl_nft_expr_build_cb, &rule_ctx); | |
| 551 | 108361 | mnl_attr_nest_end(nlh, nest); | |
| 552 | |||
| 553 | 108361 | nftnl_rule_nlmsg_build_payload(nlh, nlr); | |
| 554 | 108361 | nftnl_rule_free(nlr); | |
| 555 | 108361 | netlink_linearize_fini(&lctx); | |
| 556 | |||
| 557 | 108361 | mnl_nft_batch_continue(ctx->batch); | |
| 558 | |||
| 559 | 108361 | return 0; | |
| 560 | } | ||
| 561 | |||
| 562 | 11 | int mnl_nft_rule_replace(struct netlink_ctx *ctx, struct cmd *cmd) | |
| 563 | { | ||
| 564 | struct mnl_nft_rule_build_ctx rule_ctx; | ||
| 565 | struct netlink_linearize_ctx lctx; | ||
| 566 | 11 | struct rule *rule = cmd->rule; | |
| 567 | 11 | struct handle *h = &rule->handle; | |
| 568 | 11 | unsigned int flags = 0; | |
| 569 | struct nftnl_rule *nlr; | ||
| 570 | struct nlmsghdr *nlh; | ||
| 571 | struct nlattr *nest; | ||
| 572 | |||
| 573 |
2/2✓ Branch 0 (3→4) taken 2 times.
✓ Branch 1 (3→5) taken 9 times.
|
11 | if (nft_output_echo(&ctx->nft->output)) |
| 574 | 2 | flags |= NLM_F_ECHO; | |
| 575 | |||
| 576 | 11 | nlr = nftnl_rule_alloc(); | |
| 577 |
1/2✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→8) taken 11 times.
|
11 | if (!nlr) |
| 578 | ✗ | memory_allocation_error(); | |
| 579 | |||
| 580 | 11 | nftnl_rule_set_u32(nlr, NFTNL_RULE_FAMILY, h->family); | |
| 581 | |||
| 582 | 11 | netlink_linearize_init(&lctx, nlr); | |
| 583 | 11 | netlink_linearize_rule(ctx, rule, &lctx); | |
| 584 | 11 | nlh = nftnl_nlmsg_build_hdr(nftnl_batch_buffer(ctx->batch), | |
| 585 | NFT_MSG_NEWRULE, | ||
| 586 | 11 | cmd->handle.family, | |
| 587 | 11 | NLM_F_REPLACE | flags, ctx->seqnum); | |
| 588 | |||
| 589 | 11 | cmd_add_loc(cmd, nlh, &h->table.location); | |
| 590 | 11 | mnl_attr_put_strz(nlh, NFTA_RULE_TABLE, h->table.name); | |
| 591 | 11 | cmd_add_loc(cmd, nlh, &h->chain.location); | |
| 592 | 11 | mnl_attr_put_strz(nlh, NFTA_RULE_CHAIN, h->chain.name); | |
| 593 | 11 | cmd_add_loc(cmd, nlh, &h->handle.location); | |
| 594 | 11 | mnl_attr_put_u64(nlh, NFTA_RULE_HANDLE, htobe64(h->handle.id)); | |
| 595 | |||
| 596 | 11 | mnl_nft_rule_build_ctx_init(&rule_ctx, nlh, cmd, &lctx); | |
| 597 | |||
| 598 | 11 | nest = mnl_attr_nest_start(nlh, NFTA_RULE_EXPRESSIONS); | |
| 599 | 11 | nftnl_expr_foreach(nlr, mnl_nft_expr_build_cb, &rule_ctx); | |
| 600 | 11 | mnl_attr_nest_end(nlh, nest); | |
| 601 | |||
| 602 | 11 | nftnl_rule_nlmsg_build_payload(nlh, nlr); | |
| 603 | 11 | nftnl_rule_free(nlr); | |
| 604 | 11 | netlink_linearize_fini(&lctx); | |
| 605 | |||
| 606 | 11 | mnl_nft_batch_continue(ctx->batch); | |
| 607 | |||
| 608 | 11 | return 0; | |
| 609 | } | ||
| 610 | |||
| 611 | 14114 | int mnl_nft_rule_del(struct netlink_ctx *ctx, struct cmd *cmd) | |
| 612 | { | ||
| 613 | 14114 | enum nf_tables_msg_types msg_type = NFT_MSG_DELRULE; | |
| 614 | 14114 | struct handle *h = &cmd->handle; | |
| 615 | struct nftnl_rule *nlr; | ||
| 616 | struct nlmsghdr *nlh; | ||
| 617 | |||
| 618 | 14114 | nlr = nftnl_rule_alloc(); | |
| 619 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 14114 times.
|
14114 | if (!nlr) |
| 620 | ✗ | memory_allocation_error(); | |
| 621 | |||
| 622 | 14114 | nftnl_rule_set_u32(nlr, NFTNL_RULE_FAMILY, h->family); | |
| 623 | |||
| 624 |
2/2✓ Branch 0 (6→7) taken 2 times.
✓ Branch 1 (6→8) taken 14112 times.
|
14114 | if (cmd->op == CMD_DESTROY) |
| 625 | 2 | msg_type = NFT_MSG_DESTROYRULE; | |
| 626 | |||
| 627 | 14114 | nlh = nftnl_nlmsg_build_hdr(nftnl_batch_buffer(ctx->batch), | |
| 628 | msg_type, | ||
| 629 | 14114 | nftnl_rule_get_u32(nlr, NFTNL_RULE_FAMILY), | |
| 630 | 0, ctx->seqnum); | ||
| 631 | |||
| 632 | 14114 | cmd_add_loc(cmd, nlh, &h->table.location); | |
| 633 | 14114 | mnl_attr_put_strz(nlh, NFTA_RULE_TABLE, h->table.name); | |
| 634 |
2/2✓ Branch 0 (13→14) taken 439 times.
✓ Branch 1 (13→16) taken 13675 times.
|
14114 | if (h->chain.name) { |
| 635 | 439 | cmd_add_loc(cmd, nlh, &h->chain.location); | |
| 636 | 439 | mnl_attr_put_strz(nlh, NFTA_RULE_CHAIN, h->chain.name); | |
| 637 | } | ||
| 638 |
2/2✓ Branch 0 (16→17) taken 11 times.
✓ Branch 1 (16→20) taken 14103 times.
|
14114 | if (h->handle.id) { |
| 639 | 11 | cmd_add_loc(cmd, nlh, &h->handle.location); | |
| 640 | 11 | mnl_attr_put_u64(nlh, NFTA_RULE_HANDLE, htobe64(h->handle.id)); | |
| 641 | } | ||
| 642 | |||
| 643 | 14114 | nftnl_rule_nlmsg_build_payload(nlh, nlr); | |
| 644 | 14114 | nftnl_rule_free(nlr); | |
| 645 | |||
| 646 | 14114 | mnl_nft_batch_continue(ctx->batch); | |
| 647 | |||
| 648 | 14114 | return 0; | |
| 649 | } | ||
| 650 | |||
| 651 | /* | ||
| 652 | * Rule | ||
| 653 | */ | ||
| 654 | |||
| 655 | 56643 | static int rule_cb(const struct nlmsghdr *nlh, void *data) | |
| 656 | { | ||
| 657 | 56643 | struct nftnl_rule_list *nlr_list = data; | |
| 658 | struct nftnl_rule *r; | ||
| 659 | |||
| 660 |
2/2✓ Branch 0 (3→4) taken 1 times.
✓ Branch 1 (3→5) taken 56642 times.
|
56643 | if (check_genid(nlh) < 0) |
| 661 | 1 | return MNL_CB_ERROR; | |
| 662 | |||
| 663 | 56642 | r = nftnl_rule_alloc(); | |
| 664 |
1/2✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→8) taken 56642 times.
|
56642 | if (r == NULL) |
| 665 | ✗ | memory_allocation_error(); | |
| 666 | |||
| 667 |
1/2✗ Branch 0 (9→10) not taken.
✓ Branch 1 (9→11) taken 56642 times.
|
56642 | if (nftnl_rule_nlmsg_parse(nlh, r) < 0) |
| 668 | ✗ | goto err_free; | |
| 669 | |||
| 670 | 56642 | nftnl_rule_list_add_tail(r, nlr_list); | |
| 671 | 56642 | return MNL_CB_OK; | |
| 672 | |||
| 673 | ✗ | err_free: | |
| 674 | ✗ | nftnl_rule_free(r); | |
| 675 | ✗ | return MNL_CB_OK; | |
| 676 | } | ||
| 677 | |||
| 678 | 15503 | struct nftnl_rule_list *mnl_nft_rule_dump(struct netlink_ctx *ctx, int family, | |
| 679 | const char *table, const char *chain, | ||
| 680 | uint64_t rule_handle, | ||
| 681 | bool dump, bool reset) | ||
| 682 | 15503 | { | |
| 683 |
2/2✓ Branch 0 (2→3) taken 15502 times.
✓ Branch 1 (2→4) taken 1 times.
|
15503 | uint16_t nl_flags = dump ? NLM_F_DUMP : NLM_F_ACK; |
| 684 |
1/2✓ Branch 0 (6→7) taken 15503 times.
✗ Branch 1 (6→8) not taken.
|
15503 | char buf[MNL_SOCKET_BUFFER_SIZE]; |
| 685 | struct nftnl_rule_list *nlr_list; | ||
| 686 | 15503 | struct nftnl_rule *nlr = NULL; | |
| 687 | struct nlmsghdr *nlh; | ||
| 688 | int msg_type, ret; | ||
| 689 | |||
| 690 |
2/2✓ Branch 0 (9→10) taken 11 times.
✓ Branch 1 (9→11) taken 15492 times.
|
15503 | if (reset) |
| 691 | 11 | msg_type = NFT_MSG_GETRULE_RESET; | |
| 692 | else | ||
| 693 | 15492 | msg_type = NFT_MSG_GETRULE; | |
| 694 | |||
| 695 |
1/2✓ Branch 0 (12→13) taken 15503 times.
✗ Branch 1 (12→21) not taken.
|
15503 | if (table) { |
| 696 | 15503 | nlr = nftnl_rule_alloc(); | |
| 697 |
1/2✗ Branch 0 (14→15) not taken.
✓ Branch 1 (14→16) taken 15503 times.
|
15503 | if (!nlr) |
| 698 | ✗ | memory_allocation_error(); | |
| 699 | |||
| 700 | 15503 | nftnl_rule_set_str(nlr, NFTNL_RULE_TABLE, table); | |
| 701 |
2/2✓ Branch 0 (17→18) taken 1283 times.
✓ Branch 1 (17→19) taken 14220 times.
|
15503 | if (chain) |
| 702 | 1283 | nftnl_rule_set_str(nlr, NFTNL_RULE_CHAIN, chain); | |
| 703 |
2/2✓ Branch 0 (19→20) taken 1 times.
✓ Branch 1 (19→21) taken 15502 times.
|
15503 | if (rule_handle) |
| 704 | 1 | nftnl_rule_set_u64(nlr, NFTNL_RULE_HANDLE, rule_handle); | |
| 705 | } | ||
| 706 | |||
| 707 | 15503 | nlr_list = nftnl_rule_list_alloc(); | |
| 708 |
1/2✗ Branch 0 (22→23) not taken.
✓ Branch 1 (22→24) taken 15503 times.
|
15503 | if (nlr_list == NULL) |
| 709 | ✗ | memory_allocation_error(); | |
| 710 | |||
| 711 | 15503 | nlh = nftnl_nlmsg_build_hdr(buf, msg_type, family, | |
| 712 | nl_flags, ctx->seqnum); | ||
| 713 |
1/2✓ Branch 0 (25→26) taken 15503 times.
✗ Branch 1 (25→28) not taken.
|
15503 | if (nlr) { |
| 714 | 15503 | nftnl_rule_nlmsg_build_payload(nlh, nlr); | |
| 715 | 15503 | nftnl_rule_free(nlr); | |
| 716 | } | ||
| 717 | |||
| 718 | 15503 | ret = nft_mnl_talk(ctx, nlh, nlh->nlmsg_len, rule_cb, nlr_list); | |
| 719 |
2/2✓ Branch 0 (29→30) taken 1 times.
✓ Branch 1 (29→31) taken 15502 times.
|
15503 | if (ret < 0) |
| 720 | 1 | goto err; | |
| 721 | |||
| 722 | 15502 | return nlr_list; | |
| 723 | 1 | err: | |
| 724 | 1 | nftnl_rule_list_free(nlr_list); | |
| 725 | 1 | return NULL; | |
| 726 | } | ||
| 727 | |||
| 728 | /* | ||
| 729 | * Chain | ||
| 730 | */ | ||
| 731 | |||
| 732 | struct nft_dev { | ||
| 733 | const char *ifname; | ||
| 734 | const struct location *location; | ||
| 735 | }; | ||
| 736 | |||
| 737 | 1362 | static void nft_dev_add(struct nft_dev *dev_array, const struct expr *expr, int i) | |
| 738 | { | ||
| 739 | unsigned int ifname_len; | ||
| 740 | char ifname[IFNAMSIZ]; | ||
| 741 | |||
| 742 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→6) taken 1362 times.
|
1362 | if (expr->etype != EXPR_VALUE) |
| 743 | ✗ | BUG("Must be a value, not %s", expr_name(expr)); | |
| 744 | |||
| 745 | 1362 | ifname_len = div_round_up(expr->len, BITS_PER_BYTE); | |
| 746 | 1362 | memset(ifname, 0, sizeof(ifname)); | |
| 747 | |||
| 748 |
1/2✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→9) taken 1362 times.
|
1362 | if (ifname_len > sizeof(ifname)) |
| 749 | ✗ | BUG("Interface length %u exceeds limit", ifname_len); | |
| 750 | |||
| 751 |
1/2✗ Branch 0 (9→10) not taken.
✓ Branch 1 (9→11) taken 1362 times.
|
1362 | mpz_export_data(ifname, expr->value, BYTEORDER_HOST_ENDIAN, ifname_len); |
| 752 | |||
| 753 |
1/2✗ Branch 0 (12→13) not taken.
✓ Branch 1 (12→15) taken 1362 times.
|
1362 | if (strnlen(ifname, IFNAMSIZ) >= IFNAMSIZ) |
| 754 | ✗ | BUG("Interface length %zu exceeds limit, no NUL byte", strnlen(ifname, IFNAMSIZ)); | |
| 755 | |||
| 756 | 1362 | dev_array[i].ifname = xstrdup(ifname); | |
| 757 | 1362 | dev_array[i].location = &expr->location; | |
| 758 | 1362 | } | |
| 759 | |||
| 760 | 1311 | static struct nft_dev *nft_dev_array(const struct expr *dev_expr, int *num_devs) | |
| 761 | { | ||
| 762 | struct nft_dev *dev_array; | ||
| 763 | 1311 | int i = 0, len = 1; | |
| 764 | struct expr *expr; | ||
| 765 | |||
| 766 |
1/3✓ Branch 0 (2→3) taken 1311 times.
✗ Branch 1 (2→20) not taken.
✗ Branch 2 (2→23) not taken.
|
1311 | switch (dev_expr->etype) { |
| 767 | 1311 | case EXPR_LIST: | |
| 768 |
4/6✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 1311 times.
✗ Branch 2 (7→8) not taken.
✓ Branch 3 (7→9) taken 2673 times.
✓ Branch 4 (9→6) taken 1362 times.
✓ Branch 5 (9→10) taken 1311 times.
|
2673 | list_for_each_entry(expr, &expr_list(dev_expr)->expressions, list) |
| 769 | 1362 | len++; | |
| 770 | |||
| 771 | 1311 | dev_array = xmalloc(sizeof(struct nft_dev) * len); | |
| 772 | |||
| 773 |
4/6✗ Branch 0 (11→12) not taken.
✓ Branch 1 (11→13) taken 1311 times.
✗ Branch 2 (16→17) not taken.
✓ Branch 3 (16→18) taken 2673 times.
✓ Branch 4 (18→14) taken 1362 times.
✓ Branch 5 (18→19) taken 1311 times.
|
2673 | list_for_each_entry(expr, &expr_list(dev_expr)->expressions, list) { |
| 774 | 1362 | nft_dev_add(dev_array, expr, i); | |
| 775 | 1362 | i++; | |
| 776 | } | ||
| 777 | 1311 | break; | |
| 778 | ✗ | case EXPR_VALUE: | |
| 779 | ✗ | len++; | |
| 780 | ✗ | dev_array = xmalloc(sizeof(struct nft_dev) * len); | |
| 781 | ✗ | nft_dev_add(dev_array, dev_expr, i); | |
| 782 | ✗ | i++; | |
| 783 | ✗ | break; | |
| 784 | ✗ | default: | |
| 785 | ✗ | assert(0); | |
| 786 | } | ||
| 787 | |||
| 788 | 1311 | dev_array[i].ifname = NULL; | |
| 789 | 1311 | *num_devs = i; | |
| 790 | |||
| 791 | 1311 | return dev_array; | |
| 792 | } | ||
| 793 | |||
| 794 | 1311 | static void nft_dev_array_free(const struct nft_dev *dev_array) | |
| 795 | { | ||
| 796 | 1311 | int i = 0; | |
| 797 | |||
| 798 |
2/2✓ Branch 0 (4→3) taken 1362 times.
✓ Branch 1 (4→5) taken 1311 times.
|
2673 | while (dev_array[i].ifname != NULL) |
| 799 | 1362 | free_const(dev_array[i++].ifname); | |
| 800 | |||
| 801 | 1311 | free_const(dev_array); | |
| 802 | 1311 | } | |
| 803 | |||
| 804 | 1362 | static bool is_wildcard_str(const char *str) | |
| 805 | { | ||
| 806 | 1362 | size_t len = strlen(str); | |
| 807 | |||
| 808 |
3/4✓ Branch 0 (2→3) taken 1362 times.
✗ Branch 1 (2→4) not taken.
✓ Branch 2 (3→4) taken 1354 times.
✓ Branch 3 (3→5) taken 8 times.
|
1362 | if (len < 1 || str[len - 1] != '*') |
| 809 | 1354 | return false; | |
| 810 |
2/4✓ Branch 0 (5→6) taken 8 times.
✗ Branch 1 (5→7) not taken.
✓ Branch 2 (6→7) taken 8 times.
✗ Branch 3 (6→8) not taken.
|
8 | if (len < 2 || str[len - 2] != '\\') |
| 811 | 8 | return true; | |
| 812 | /* XXX: ignore backslash escaping for now */ | ||
| 813 | ✗ | return false; | |
| 814 | } | ||
| 815 | |||
| 816 | 176 | static void mnl_nft_attr_put_ifname(struct nlmsghdr *nlh, const char *ifname) | |
| 817 | { | ||
| 818 | 176 | uint16_t attr = NFTA_DEVICE_NAME; | |
| 819 | char pfx[IFNAMSIZ]; | ||
| 820 | |||
| 821 |
2/2✓ Branch 0 (3→4) taken 8 times.
✓ Branch 1 (3→5) taken 168 times.
|
176 | if (is_wildcard_str(ifname)) { |
| 822 | 8 | snprintf(pfx, IFNAMSIZ, "%s", ifname); | |
| 823 | 8 | pfx[strlen(pfx) - 1] = '\0'; | |
| 824 | |||
| 825 | 8 | attr = NFTA_DEVICE_PREFIX; | |
| 826 | 8 | ifname = pfx; | |
| 827 | } | ||
| 828 | 176 | mnl_attr_put_strz(nlh, attr, ifname); | |
| 829 | 176 | } | |
| 830 | |||
| 831 | 1213 | static void mnl_nft_chain_devs_build(struct nlmsghdr *nlh, struct cmd *cmd) | |
| 832 | { | ||
| 833 | 1213 | const struct expr *dev_expr = cmd->chain->dev_expr; | |
| 834 | const struct nft_dev *dev_array; | ||
| 835 | struct nlattr *nest_dev; | ||
| 836 | 1213 | int i, num_devs = 0; | |
| 837 | |||
| 838 | 1213 | dev_array = nft_dev_array(dev_expr, &num_devs); | |
| 839 |
3/4✓ Branch 0 (3→4) taken 1186 times.
✓ Branch 1 (3→8) taken 27 times.
✓ Branch 2 (5→6) taken 1186 times.
✗ Branch 3 (5→8) not taken.
|
1213 | if (num_devs == 1 && !is_wildcard_str(dev_array[0].ifname)) { |
| 840 | 1186 | cmd_add_loc(cmd, nlh, dev_array[0].location); | |
| 841 | 1186 | mnl_attr_put_strz(nlh, NFTA_HOOK_DEV, dev_array[0].ifname); | |
| 842 | } else { | ||
| 843 | 27 | nest_dev = mnl_attr_nest_start(nlh, NFTA_HOOK_DEVS); | |
| 844 |
2/2✓ Branch 0 (13→10) taken 63 times.
✓ Branch 1 (13→14) taken 27 times.
|
90 | for (i = 0; i < num_devs; i++) { |
| 845 | 63 | cmd_add_loc(cmd, nlh, dev_array[i].location); | |
| 846 | 63 | mnl_nft_attr_put_ifname(nlh, dev_array[i].ifname); | |
| 847 | } | ||
| 848 | 27 | mnl_attr_nest_end(nlh, nest_dev); | |
| 849 | } | ||
| 850 | 1213 | nft_dev_array_free(dev_array); | |
| 851 | 1213 | } | |
| 852 | |||
| 853 | 7362 | int mnl_nft_chain_add(struct netlink_ctx *ctx, struct cmd *cmd, | |
| 854 | unsigned int flags) | ||
| 855 | { | ||
| 856 | struct nftnl_udata_buf *udbuf; | ||
| 857 | struct nftnl_chain *nlc; | ||
| 858 | struct nlmsghdr *nlh; | ||
| 859 | int priority, policy; | ||
| 860 | |||
| 861 | 7362 | nlc = nftnl_chain_alloc(); | |
| 862 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 7362 times.
|
7362 | if (nlc == NULL) |
| 863 | ✗ | memory_allocation_error(); | |
| 864 | |||
| 865 | 7362 | nftnl_chain_set_u32(nlc, NFTNL_CHAIN_FAMILY, cmd->handle.family); | |
| 866 | |||
| 867 |
2/2✓ Branch 0 (6→7) taken 5360 times.
✓ Branch 1 (6→20) taken 2002 times.
|
7362 | if (cmd->chain) { |
| 868 |
1/2✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→9) taken 5360 times.
|
5360 | if (cmd->chain->flags & CHAIN_F_HW_OFFLOAD) { |
| 869 | ✗ | nftnl_chain_set_u32(nlc, NFTNL_CHAIN_FLAGS, | |
| 870 | CHAIN_F_HW_OFFLOAD); | ||
| 871 | } | ||
| 872 |
2/2✓ Branch 0 (9→10) taken 5 times.
✓ Branch 1 (9→20) taken 5355 times.
|
5360 | if (cmd->chain->comment) { |
| 873 | 5 | udbuf = nftnl_udata_buf_alloc(NFT_USERDATA_MAXLEN); | |
| 874 |
1/2✗ Branch 0 (11→12) not taken.
✓ Branch 1 (11→13) taken 5 times.
|
5 | if (!udbuf) |
| 875 | ✗ | memory_allocation_error(); | |
| 876 |
1/2✗ Branch 0 (14→15) not taken.
✓ Branch 1 (14→16) taken 5 times.
|
5 | if (!nftnl_udata_put_strz(udbuf, NFTNL_UDATA_CHAIN_COMMENT, cmd->chain->comment)) |
| 877 | ✗ | memory_allocation_error(); | |
| 878 | 5 | nftnl_chain_set_data(nlc, NFTNL_CHAIN_USERDATA, nftnl_udata_buf_data(udbuf), | |
| 879 | nftnl_udata_buf_len(udbuf)); | ||
| 880 | 5 | nftnl_udata_buf_free(udbuf); | |
| 881 | } | ||
| 882 | } | ||
| 883 | |||
| 884 | 7362 | nftnl_chain_set_str(nlc, NFTNL_CHAIN_TABLE, cmd->handle.table.name); | |
| 885 |
2/2✓ Branch 0 (21→22) taken 7300 times.
✓ Branch 1 (21→23) taken 62 times.
|
7362 | if (cmd->handle.chain.name) |
| 886 | 7300 | nftnl_chain_set_str(nlc, NFTNL_CHAIN_NAME, cmd->handle.chain.name); | |
| 887 | |||
| 888 | 7362 | netlink_dump_chain(nlc, ctx); | |
| 889 | |||
| 890 | 7362 | nftnl_chain_unset(nlc, NFTNL_CHAIN_TABLE); | |
| 891 | 7362 | nftnl_chain_unset(nlc, NFTNL_CHAIN_NAME); | |
| 892 | |||
| 893 | 7362 | nlh = nftnl_nlmsg_build_hdr(nftnl_batch_buffer(ctx->batch), | |
| 894 | NFT_MSG_NEWCHAIN, | ||
| 895 | 7362 | cmd->handle.family, | |
| 896 | 7362 | NLM_F_CREATE | flags, ctx->seqnum); | |
| 897 | |||
| 898 | 7362 | cmd_add_loc(cmd, nlh, &cmd->handle.table.location); | |
| 899 | 7362 | mnl_attr_put_strz(nlh, NFTA_CHAIN_TABLE, cmd->handle.table.name); | |
| 900 | 7362 | cmd_add_loc(cmd, nlh, &cmd->handle.chain.location); | |
| 901 | |||
| 902 |
4/4✓ Branch 0 (31→32) taken 5360 times.
✓ Branch 1 (31→33) taken 2002 times.
✓ Branch 2 (32→33) taken 5298 times.
✓ Branch 3 (32→34) taken 62 times.
|
7362 | if (!cmd->chain || !(cmd->chain->flags & CHAIN_F_BINDING)) { |
| 903 | 7300 | mnl_attr_put_strz(nlh, NFTA_CHAIN_NAME, cmd->handle.chain.name); | |
| 904 | } else { | ||
| 905 |
1/2✗ Branch 0 (34→35) not taken.
✓ Branch 1 (34→36) taken 62 times.
|
62 | if (cmd->handle.chain.name) |
| 906 | ✗ | mnl_attr_put_strz(nlh, NFTA_CHAIN_NAME, | |
| 907 | cmd->handle.chain.name); | ||
| 908 | |||
| 909 | 62 | mnl_attr_put_u32(nlh, NFTA_CHAIN_ID, htonl(cmd->handle.chain_id)); | |
| 910 |
1/2✓ Branch 0 (37→38) taken 62 times.
✗ Branch 1 (37→39) not taken.
|
62 | if (cmd->chain->flags) |
| 911 | 62 | nftnl_chain_set_u32(nlc, NFTNL_CHAIN_FLAGS, cmd->chain->flags); | |
| 912 | } | ||
| 913 | |||
| 914 |
4/4✓ Branch 0 (39→40) taken 5360 times.
✓ Branch 1 (39→44) taken 2002 times.
✓ Branch 2 (40→41) taken 3074 times.
✓ Branch 3 (40→44) taken 2286 times.
|
7362 | if (cmd->chain && cmd->chain->policy) { |
| 915 | 3074 | mpz_export_data(&policy, cmd->chain->policy->value, | |
| 916 | BYTEORDER_HOST_ENDIAN, sizeof(int)); | ||
| 917 | 3074 | cmd_add_loc(cmd, nlh, &cmd->chain->policy->location); | |
| 918 | 3074 | mnl_attr_put_u32(nlh, NFTA_CHAIN_POLICY, htonl(policy)); | |
| 919 | } | ||
| 920 | |||
| 921 | 7362 | nftnl_chain_unset(nlc, NFTNL_CHAIN_TYPE); | |
| 922 | |||
| 923 | 7362 | nftnl_chain_nlmsg_build_payload(nlh, nlc); | |
| 924 | |||
| 925 |
4/4✓ Branch 0 (46→47) taken 5360 times.
✓ Branch 1 (46→64) taken 2002 times.
✓ Branch 2 (47→48) taken 3933 times.
✓ Branch 3 (47→64) taken 1427 times.
|
7362 | if (cmd->chain && cmd->chain->flags & CHAIN_F_BASECHAIN) { |
| 926 | 3933 | struct nlattr *nest = NULL; | |
| 927 | |||
| 928 |
2/2✓ Branch 0 (48→49) taken 3925 times.
✓ Branch 1 (48→51) taken 8 times.
|
3933 | if (cmd->chain->type.str) { |
| 929 | 3925 | cmd_add_loc(cmd, nlh, &cmd->chain->type.loc); | |
| 930 | 3925 | mnl_attr_put_strz(nlh, NFTA_CHAIN_TYPE, cmd->chain->type.str); | |
| 931 | } | ||
| 932 | |||
| 933 |
2/2✓ Branch 0 (51→52) taken 8 times.
✓ Branch 1 (51→54) taken 3925 times.
|
3933 | if (cmd->chain->type.str || |
| 934 |
3/4✓ Branch 0 (52→53) taken 8 times.
✗ Branch 1 (52→55) not taken.
✓ Branch 2 (53→54) taken 4 times.
✓ Branch 3 (53→55) taken 4 times.
|
8 | (cmd->chain && cmd->chain->dev_expr)) |
| 935 | 3929 | nest = mnl_attr_nest_start(nlh, NFTA_CHAIN_HOOK); | |
| 936 | |||
| 937 |
2/2✓ Branch 0 (55→56) taken 3925 times.
✓ Branch 1 (55→59) taken 8 times.
|
3933 | if (cmd->chain->type.str) { |
| 938 | 3925 | mnl_attr_put_u32(nlh, NFTA_HOOK_HOOKNUM, htonl(cmd->chain->hook.num)); | |
| 939 | 3925 | mpz_export_data(&priority, cmd->chain->priority.expr->value, | |
| 940 | BYTEORDER_HOST_ENDIAN, sizeof(int)); | ||
| 941 | 3925 | mnl_attr_put_u32(nlh, NFTA_HOOK_PRIORITY, htonl(priority)); | |
| 942 | } | ||
| 943 | |||
| 944 |
3/4✓ Branch 0 (59→60) taken 3933 times.
✗ Branch 1 (59→62) not taken.
✓ Branch 2 (60→61) taken 1207 times.
✓ Branch 3 (60→62) taken 2726 times.
|
3933 | if (cmd->chain && cmd->chain->dev_expr) |
| 945 | 1207 | mnl_nft_chain_devs_build(nlh, cmd); | |
| 946 | |||
| 947 |
2/2✓ Branch 0 (62→63) taken 3929 times.
✓ Branch 1 (62→64) taken 4 times.
|
3933 | if (nest) |
| 948 | 3929 | mnl_attr_nest_end(nlh, nest); | |
| 949 | } | ||
| 950 | |||
| 951 | 7362 | nftnl_chain_free(nlc); | |
| 952 | |||
| 953 | 7362 | mnl_nft_batch_continue(ctx->batch); | |
| 954 | |||
| 955 | 7362 | return 0; | |
| 956 | } | ||
| 957 | |||
| 958 | 4 | int mnl_nft_chain_rename(struct netlink_ctx *ctx, const struct cmd *cmd, | |
| 959 | const struct chain *chain) | ||
| 960 | { | ||
| 961 | 4 | const char *name = cmd->arg; | |
| 962 | struct nftnl_chain *nlc; | ||
| 963 | struct nlmsghdr *nlh; | ||
| 964 | |||
| 965 | 4 | nlc = nftnl_chain_alloc(); | |
| 966 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 4 times.
|
4 | if (nlc == NULL) |
| 967 | ✗ | memory_allocation_error(); | |
| 968 | |||
| 969 | 4 | nftnl_chain_set_u32(nlc, NFTNL_CHAIN_FAMILY, cmd->handle.family); | |
| 970 | 4 | nftnl_chain_set_str(nlc, NFTNL_CHAIN_TABLE, cmd->handle.table.name); | |
| 971 | 4 | nftnl_chain_set_u64(nlc, NFTNL_CHAIN_HANDLE, chain->handle.handle.id); | |
| 972 | 4 | nftnl_chain_set_str(nlc, NFTNL_CHAIN_NAME, name); | |
| 973 | |||
| 974 | 4 | netlink_dump_chain(nlc, ctx); | |
| 975 | |||
| 976 | 4 | nlh = nftnl_nlmsg_build_hdr(nftnl_batch_buffer(ctx->batch), | |
| 977 | NFT_MSG_NEWCHAIN, | ||
| 978 | 4 | cmd->handle.family, | |
| 979 | 0, ctx->seqnum); | ||
| 980 | 4 | nftnl_chain_nlmsg_build_payload(nlh, nlc); | |
| 981 | 4 | nftnl_chain_free(nlc); | |
| 982 | |||
| 983 | 4 | mnl_nft_batch_continue(ctx->batch); | |
| 984 | |||
| 985 | 4 | return 0; | |
| 986 | } | ||
| 987 | |||
| 988 | 354 | int mnl_nft_chain_del(struct netlink_ctx *ctx, struct cmd *cmd) | |
| 989 | { | ||
| 990 | 354 | enum nf_tables_msg_types msg_type = NFT_MSG_DELCHAIN; | |
| 991 | struct nftnl_chain *nlc; | ||
| 992 | struct nlmsghdr *nlh; | ||
| 993 | |||
| 994 | 354 | nlc = nftnl_chain_alloc(); | |
| 995 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 354 times.
|
354 | if (nlc == NULL) |
| 996 | ✗ | memory_allocation_error(); | |
| 997 | |||
| 998 | 354 | nftnl_chain_set_u32(nlc, NFTNL_CHAIN_FAMILY, cmd->handle.family); | |
| 999 | |||
| 1000 |
2/2✓ Branch 0 (6→7) taken 2 times.
✓ Branch 1 (6→8) taken 352 times.
|
354 | if (cmd->op == CMD_DESTROY) |
| 1001 | 2 | msg_type = NFT_MSG_DESTROYCHAIN; | |
| 1002 | |||
| 1003 | 354 | nlh = nftnl_nlmsg_build_hdr(nftnl_batch_buffer(ctx->batch), | |
| 1004 | msg_type, | ||
| 1005 | 354 | cmd->handle.family, | |
| 1006 | 0, ctx->seqnum); | ||
| 1007 | |||
| 1008 | 354 | cmd_add_loc(cmd, nlh, &cmd->handle.table.location); | |
| 1009 | 354 | mnl_attr_put_strz(nlh, NFTA_CHAIN_TABLE, cmd->handle.table.name); | |
| 1010 |
2/2✓ Branch 0 (12→13) taken 350 times.
✓ Branch 1 (12→15) taken 4 times.
|
354 | if (cmd->handle.chain.name) { |
| 1011 | 350 | cmd_add_loc(cmd, nlh, &cmd->handle.chain.location); | |
| 1012 | 350 | mnl_attr_put_strz(nlh, NFTA_CHAIN_NAME, cmd->handle.chain.name); | |
| 1013 |
1/2✓ Branch 0 (15→16) taken 4 times.
✗ Branch 1 (15→19) not taken.
|
4 | } else if (cmd->handle.handle.id) { |
| 1014 | 4 | cmd_add_loc(cmd, nlh, &cmd->handle.handle.location); | |
| 1015 | 4 | mnl_attr_put_u64(nlh, NFTA_CHAIN_HANDLE, | |
| 1016 | 4 | htobe64(cmd->handle.handle.id)); | |
| 1017 | } | ||
| 1018 | |||
| 1019 |
2/2✓ Branch 0 (19→20) taken 352 times.
✓ Branch 1 (19→26) taken 2 times.
|
354 | if (cmd->op == CMD_DELETE && |
| 1020 |
3/4✓ Branch 0 (20→21) taken 6 times.
✓ Branch 1 (20→26) taken 346 times.
✓ Branch 2 (21→22) taken 6 times.
✗ Branch 3 (21→26) not taken.
|
352 | cmd->chain && cmd->chain->dev_expr) { |
| 1021 | struct nlattr *nest; | ||
| 1022 | |||
| 1023 | 6 | nest = mnl_attr_nest_start(nlh, NFTA_CHAIN_HOOK); | |
| 1024 | 6 | mnl_attr_put_u32(nlh, NFTA_HOOK_HOOKNUM, | |
| 1025 | 6 | htonl(cmd->chain->hook.num)); | |
| 1026 | 6 | mnl_nft_chain_devs_build(nlh, cmd); | |
| 1027 | 6 | mnl_attr_nest_end(nlh, nest); | |
| 1028 | } | ||
| 1029 | |||
| 1030 | 354 | nftnl_chain_nlmsg_build_payload(nlh, nlc); | |
| 1031 | 354 | nftnl_chain_free(nlc); | |
| 1032 | |||
| 1033 | 354 | mnl_nft_batch_continue(ctx->batch); | |
| 1034 | |||
| 1035 | 354 | return 0; | |
| 1036 | } | ||
| 1037 | |||
| 1038 | 59618 | static int chain_cb(const struct nlmsghdr *nlh, void *data) | |
| 1039 | { | ||
| 1040 | 59618 | struct nftnl_chain_list *nlc_list = data; | |
| 1041 | struct nftnl_chain *c; | ||
| 1042 | |||
| 1043 |
2/2✓ Branch 0 (3→4) taken 1 times.
✓ Branch 1 (3→5) taken 59617 times.
|
59618 | if (check_genid(nlh) < 0) |
| 1044 | 1 | return MNL_CB_ERROR; | |
| 1045 | |||
| 1046 | 59617 | c = nftnl_chain_alloc(); | |
| 1047 |
1/2✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→8) taken 59617 times.
|
59617 | if (c == NULL) |
| 1048 | ✗ | memory_allocation_error(); | |
| 1049 | |||
| 1050 |
1/2✗ Branch 0 (9→10) not taken.
✓ Branch 1 (9→11) taken 59617 times.
|
59617 | if (nftnl_chain_nlmsg_parse(nlh, c) < 0) |
| 1051 | ✗ | goto err_free; | |
| 1052 | |||
| 1053 | 59617 | nftnl_chain_list_add_tail(c, nlc_list); | |
| 1054 | 59617 | return MNL_CB_OK; | |
| 1055 | |||
| 1056 | ✗ | err_free: | |
| 1057 | ✗ | nftnl_chain_free(c); | |
| 1058 | ✗ | return MNL_CB_OK; | |
| 1059 | } | ||
| 1060 | |||
| 1061 | 15921 | struct nftnl_chain_list *mnl_nft_chain_dump(struct netlink_ctx *ctx, | |
| 1062 | int family, const char *table, | ||
| 1063 | const char *chain) | ||
| 1064 | 15921 | { | |
| 1065 |
1/2✓ Branch 0 (3→4) taken 15921 times.
✗ Branch 1 (3→5) not taken.
|
15921 | char buf[MNL_SOCKET_BUFFER_SIZE]; |
| 1066 | struct nftnl_chain_list *nlc_list; | ||
| 1067 | 15921 | struct nftnl_chain *nlc = NULL; | |
| 1068 | struct nlmsghdr *nlh; | ||
| 1069 | int ret; | ||
| 1070 | |||
| 1071 | 15921 | nlc_list = nftnl_chain_list_alloc(); | |
| 1072 |
1/2✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→9) taken 15921 times.
|
15921 | if (nlc_list == NULL) |
| 1073 | ✗ | memory_allocation_error(); | |
| 1074 | |||
| 1075 |
3/4✓ Branch 0 (9→10) taken 1284 times.
✓ Branch 1 (9→16) taken 14637 times.
✓ Branch 2 (10→11) taken 1284 times.
✗ Branch 3 (10→16) not taken.
|
15921 | if (table && chain) { |
| 1076 | 1284 | nlc = nftnl_chain_alloc(); | |
| 1077 |
1/2✗ Branch 0 (12→13) not taken.
✓ Branch 1 (12→14) taken 1284 times.
|
1284 | if (!nlc) |
| 1078 | ✗ | memory_allocation_error(); | |
| 1079 | |||
| 1080 | 1284 | nftnl_chain_set_str(nlc, NFTNL_CHAIN_TABLE, table); | |
| 1081 | 1284 | nftnl_chain_set_str(nlc, NFTNL_CHAIN_NAME, chain); | |
| 1082 | } | ||
| 1083 | |||
| 1084 |
2/2✓ Branch 0 (16→17) taken 1284 times.
✓ Branch 1 (16→18) taken 14637 times.
|
15921 | nlh = nftnl_nlmsg_build_hdr(buf, NFT_MSG_GETCHAIN, family, |
| 1085 | nlc ? NLM_F_ACK : NLM_F_DUMP, ctx->seqnum); | ||
| 1086 |
2/2✓ Branch 0 (20→21) taken 1284 times.
✓ Branch 1 (20→23) taken 14637 times.
|
15921 | if (nlc) { |
| 1087 | 1284 | nftnl_chain_nlmsg_build_payload(nlh, nlc); | |
| 1088 | 1284 | nftnl_chain_free(nlc); | |
| 1089 | } | ||
| 1090 | |||
| 1091 | 15921 | ret = nft_mnl_talk(ctx, nlh, nlh->nlmsg_len, chain_cb, nlc_list); | |
| 1092 |
4/4✓ Branch 0 (24→25) taken 571 times.
✓ Branch 1 (24→27) taken 15350 times.
✓ Branch 2 (25→26) taken 1 times.
✓ Branch 3 (25→27) taken 570 times.
|
15921 | if (ret < 0 && errno != ENOENT) |
| 1093 | 1 | goto err; | |
| 1094 | |||
| 1095 | 15920 | return nlc_list; | |
| 1096 | 1 | err: | |
| 1097 | 1 | nftnl_chain_list_free(nlc_list); | |
| 1098 | 1 | return NULL; | |
| 1099 | } | ||
| 1100 | |||
| 1101 | /* | ||
| 1102 | * Table | ||
| 1103 | */ | ||
| 1104 | 3394 | int mnl_nft_table_add(struct netlink_ctx *ctx, struct cmd *cmd, | |
| 1105 | unsigned int flags) | ||
| 1106 | { | ||
| 1107 | struct nftnl_udata_buf *udbuf; | ||
| 1108 | struct nftnl_table *nlt; | ||
| 1109 | struct nlmsghdr *nlh; | ||
| 1110 | |||
| 1111 | 3394 | nlt = nftnl_table_alloc(); | |
| 1112 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 3394 times.
|
3394 | if (nlt == NULL) |
| 1113 | ✗ | memory_allocation_error(); | |
| 1114 | |||
| 1115 | 3394 | udbuf = nftnl_udata_buf_alloc(NFT_USERDATA_MAXLEN); | |
| 1116 |
1/2✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→8) taken 3394 times.
|
3394 | if (!udbuf) |
| 1117 | ✗ | memory_allocation_error(); | |
| 1118 | |||
| 1119 | 3394 | nftnl_table_set_u32(nlt, NFTNL_TABLE_FAMILY, cmd->handle.family); | |
| 1120 |
2/2✓ Branch 0 (9→10) taken 1363 times.
✓ Branch 1 (9→15) taken 2031 times.
|
3394 | if (cmd->table) { |
| 1121 | 1363 | nftnl_table_set_u32(nlt, NFTNL_TABLE_FLAGS, cmd->table->flags); | |
| 1122 | |||
| 1123 |
2/2✓ Branch 0 (11→12) taken 8 times.
✓ Branch 1 (11→16) taken 1355 times.
|
1363 | if (cmd->table->comment) { |
| 1124 |
1/2✗ Branch 0 (13→14) not taken.
✓ Branch 1 (13→16) taken 8 times.
|
8 | if (!nftnl_udata_put_strz(udbuf, NFTNL_UDATA_TABLE_COMMENT, cmd->table->comment)) |
| 1125 | ✗ | memory_allocation_error(); | |
| 1126 | } | ||
| 1127 | } else { | ||
| 1128 | 2031 | nftnl_table_set_u32(nlt, NFTNL_TABLE_FLAGS, 0); | |
| 1129 | } | ||
| 1130 | |||
| 1131 |
1/2✓ Branch 0 (17→18) taken 3394 times.
✗ Branch 1 (17→20) not taken.
|
3394 | if (!nftnl_udata_put(udbuf, NFTNL_UDATA_TABLE_NFTVER, |
| 1132 | 3394 | sizeof(nftversion), nftversion) || | |
| 1133 |
1/2✗ Branch 0 (19→20) not taken.
✓ Branch 1 (19→21) taken 3394 times.
|
3394 | !nftnl_udata_put(udbuf, NFTNL_UDATA_TABLE_NFTBLD, |
| 1134 | sizeof(nftbuildstamp), nftbuildstamp)) | ||
| 1135 | ✗ | memory_allocation_error(); | |
| 1136 | 6788 | nftnl_table_set_data(nlt, NFTNL_TABLE_USERDATA, | |
| 1137 | 3394 | nftnl_udata_buf_data(udbuf), | |
| 1138 | nftnl_udata_buf_len(udbuf)); | ||
| 1139 | 3394 | nftnl_udata_buf_free(udbuf); | |
| 1140 | |||
| 1141 | 3394 | nlh = nftnl_nlmsg_build_hdr(nftnl_batch_buffer(ctx->batch), | |
| 1142 | NFT_MSG_NEWTABLE, | ||
| 1143 | 3394 | cmd->handle.family, | |
| 1144 | flags, ctx->seqnum); | ||
| 1145 | |||
| 1146 | 3394 | cmd_add_loc(cmd, nlh, &cmd->handle.table.location); | |
| 1147 | 3394 | mnl_attr_put_strz(nlh, NFTA_TABLE_NAME, cmd->handle.table.name); | |
| 1148 | 3394 | nftnl_table_nlmsg_build_payload(nlh, nlt); | |
| 1149 | 3394 | nftnl_table_free(nlt); | |
| 1150 | |||
| 1151 | 3394 | mnl_nft_batch_continue(ctx->batch); | |
| 1152 | |||
| 1153 | 3394 | return 0; | |
| 1154 | } | ||
| 1155 | |||
| 1156 | 1646 | int mnl_nft_table_del(struct netlink_ctx *ctx, struct cmd *cmd) | |
| 1157 | { | ||
| 1158 | 1646 | enum nf_tables_msg_types msg_type = NFT_MSG_DELTABLE; | |
| 1159 | struct nftnl_table *nlt; | ||
| 1160 | struct nlmsghdr *nlh; | ||
| 1161 | |||
| 1162 | 1646 | nlt = nftnl_table_alloc(); | |
| 1163 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 1646 times.
|
1646 | if (nlt == NULL) |
| 1164 | ✗ | memory_allocation_error(); | |
| 1165 | |||
| 1166 | 1646 | nftnl_table_set_u32(nlt, NFTNL_TABLE_FAMILY, cmd->handle.family); | |
| 1167 | |||
| 1168 |
2/2✓ Branch 0 (6→7) taken 1 times.
✓ Branch 1 (6→8) taken 1645 times.
|
1646 | if (cmd->op == CMD_DESTROY) |
| 1169 | 1 | msg_type = NFT_MSG_DESTROYTABLE; | |
| 1170 | |||
| 1171 | 1646 | nlh = nftnl_nlmsg_build_hdr(nftnl_batch_buffer(ctx->batch), msg_type, | |
| 1172 | 1646 | cmd->handle.family, 0, ctx->seqnum); | |
| 1173 | |||
| 1174 |
2/2✓ Branch 0 (10→11) taken 267 times.
✓ Branch 1 (10→13) taken 1379 times.
|
1646 | if (cmd->handle.table.name) { |
| 1175 | 267 | cmd_add_loc(cmd, nlh, &cmd->handle.table.location); | |
| 1176 | 267 | mnl_attr_put_strz(nlh, NFTA_TABLE_NAME, cmd->handle.table.name); | |
| 1177 |
2/2✓ Branch 0 (13→14) taken 3 times.
✓ Branch 1 (13→17) taken 1376 times.
|
1379 | } else if (cmd->handle.handle.id) { |
| 1178 | 3 | cmd_add_loc(cmd, nlh, &cmd->handle.handle.location); | |
| 1179 | 3 | mnl_attr_put_u64(nlh, NFTA_TABLE_HANDLE, | |
| 1180 | 3 | htobe64(cmd->handle.handle.id)); | |
| 1181 | } | ||
| 1182 | 1646 | nftnl_table_nlmsg_build_payload(nlh, nlt); | |
| 1183 | 1646 | nftnl_table_free(nlt); | |
| 1184 | |||
| 1185 | 1646 | mnl_nft_batch_continue(ctx->batch); | |
| 1186 | |||
| 1187 | 1646 | return 0; | |
| 1188 | } | ||
| 1189 | |||
| 1190 | 78944 | static int table_cb(const struct nlmsghdr *nlh, void *data) | |
| 1191 | { | ||
| 1192 | 78944 | struct nftnl_table_list *nlt_list = data; | |
| 1193 | struct nftnl_table *t; | ||
| 1194 | |||
| 1195 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 78944 times.
|
78944 | if (check_genid(nlh) < 0) |
| 1196 | ✗ | return MNL_CB_ERROR; | |
| 1197 | |||
| 1198 | 78944 | t = nftnl_table_alloc(); | |
| 1199 |
1/2✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→8) taken 78944 times.
|
78944 | if (t == NULL) |
| 1200 | ✗ | memory_allocation_error(); | |
| 1201 | |||
| 1202 |
1/2✗ Branch 0 (9→10) not taken.
✓ Branch 1 (9→11) taken 78944 times.
|
78944 | if (nftnl_table_nlmsg_parse(nlh, t) < 0) |
| 1203 | ✗ | goto err_free; | |
| 1204 | |||
| 1205 | 78944 | nftnl_table_list_add_tail(t, nlt_list); | |
| 1206 | 78944 | return MNL_CB_OK; | |
| 1207 | |||
| 1208 | ✗ | err_free: | |
| 1209 | ✗ | nftnl_table_free(t); | |
| 1210 | ✗ | return MNL_CB_OK; | |
| 1211 | } | ||
| 1212 | |||
| 1213 | 39982 | struct nftnl_table_list *mnl_nft_table_dump(struct netlink_ctx *ctx, | |
| 1214 | int family, const char *table) | ||
| 1215 | 39982 | { | |
| 1216 |
1/2✓ Branch 0 (3→4) taken 39982 times.
✗ Branch 1 (3→5) not taken.
|
39982 | char buf[MNL_SOCKET_BUFFER_SIZE]; |
| 1217 | struct nftnl_table_list *nlt_list; | ||
| 1218 | 39982 | struct nftnl_table *nlt = NULL; | |
| 1219 | 39982 | int flags = NLM_F_DUMP; | |
| 1220 | struct nlmsghdr *nlh; | ||
| 1221 | int ret; | ||
| 1222 | |||
| 1223 | 39982 | nlt_list = nftnl_table_list_alloc(); | |
| 1224 |
1/2✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→9) taken 39982 times.
|
39982 | if (nlt_list == NULL) |
| 1225 | ✗ | return NULL; | |
| 1226 | |||
| 1227 |
2/2✓ Branch 0 (9→10) taken 15317 times.
✓ Branch 1 (9→18) taken 24665 times.
|
39982 | if (table) { |
| 1228 | 15317 | nlt = nftnl_table_alloc(); | |
| 1229 |
1/2✗ Branch 0 (11→12) not taken.
✓ Branch 1 (11→13) taken 15317 times.
|
15317 | if (!nlt) |
| 1230 | ✗ | memory_allocation_error(); | |
| 1231 | |||
| 1232 |
1/2✓ Branch 0 (13→14) taken 15317 times.
✗ Branch 1 (13→15) not taken.
|
15317 | if (family != NFPROTO_UNSPEC) |
| 1233 | 15317 | nftnl_table_set_u32(nlt, NFTNL_TABLE_FAMILY, family); | |
| 1234 |
1/2✓ Branch 0 (15→16) taken 15317 times.
✗ Branch 1 (15→17) not taken.
|
15317 | if (table) |
| 1235 | 15317 | nftnl_table_set_str(nlt, NFTNL_TABLE_NAME, table); | |
| 1236 | |||
| 1237 | 15317 | flags = NLM_F_ACK; | |
| 1238 | } | ||
| 1239 | |||
| 1240 | 39982 | nlh = nftnl_nlmsg_build_hdr(buf, NFT_MSG_GETTABLE, family, | |
| 1241 | flags, ctx->seqnum); | ||
| 1242 |
2/2✓ Branch 0 (19→20) taken 15317 times.
✓ Branch 1 (19→22) taken 24665 times.
|
39982 | if (nlt) { |
| 1243 | 15317 | nftnl_table_nlmsg_build_payload(nlh, nlt); | |
| 1244 | 15317 | nftnl_table_free(nlt); | |
| 1245 | } | ||
| 1246 | |||
| 1247 | 39982 | ret = nft_mnl_talk(ctx, nlh, nlh->nlmsg_len, table_cb, nlt_list); | |
| 1248 |
3/4✓ Branch 0 (23→24) taken 459 times.
✓ Branch 1 (23→26) taken 39523 times.
✗ Branch 2 (24→25) not taken.
✓ Branch 3 (24→26) taken 459 times.
|
39982 | if (ret < 0 && errno != ENOENT) |
| 1249 | ✗ | goto err; | |
| 1250 | |||
| 1251 | 39982 | return nlt_list; | |
| 1252 | ✗ | err: | |
| 1253 | ✗ | nftnl_table_list_free(nlt_list); | |
| 1254 | ✗ | return NULL; | |
| 1255 | } | ||
| 1256 | |||
| 1257 | 8621 | static void set_key_expression(struct netlink_ctx *ctx, | |
| 1258 | struct expr *expr, uint32_t set_flags, | ||
| 1259 | struct nftnl_udata_buf *udbuf, | ||
| 1260 | unsigned int type) | ||
| 1261 | { | ||
| 1262 | struct nftnl_udata *nest1, *nest2; | ||
| 1263 | |||
| 1264 |
2/2✓ Branch 0 (3→4) taken 104 times.
✓ Branch 1 (3→5) taken 8517 times.
|
8621 | if (!expr_ops(expr)->build_udata) |
| 1265 | 104 | return; | |
| 1266 | |||
| 1267 | 8517 | nest1 = nftnl_udata_nest_start(udbuf, type); | |
| 1268 | 8517 | nftnl_udata_put_u32(udbuf, NFTNL_UDATA_SET_TYPEOF_EXPR, expr->etype); | |
| 1269 | 8517 | nest2 = nftnl_udata_nest_start(udbuf, NFTNL_UDATA_SET_TYPEOF_DATA); | |
| 1270 | 8517 | expr_ops(expr)->build_udata(udbuf, expr); | |
| 1271 | 8517 | nftnl_udata_nest_end(udbuf, nest2); | |
| 1272 | 8517 | nftnl_udata_nest_end(udbuf, nest1); | |
| 1273 | } | ||
| 1274 | |||
| 1275 | /* | ||
| 1276 | * Set | ||
| 1277 | */ | ||
| 1278 | 6970 | int mnl_nft_set_add(struct netlink_ctx *ctx, struct cmd *cmd, | |
| 1279 | unsigned int flags) | ||
| 1280 | { | ||
| 1281 | 6970 | struct handle *h = &cmd->handle; | |
| 1282 | struct nftnl_udata_buf *udbuf; | ||
| 1283 | 6970 | struct set *set = cmd->set; | |
| 1284 | struct nftnl_set *nls; | ||
| 1285 | struct nlmsghdr *nlh; | ||
| 1286 | struct stmt *stmt; | ||
| 1287 | 6970 | int num_stmts = 0; | |
| 1288 | |||
| 1289 | 6970 | nls = nftnl_set_alloc(); | |
| 1290 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 6970 times.
|
6970 | if (!nls) |
| 1291 | ✗ | memory_allocation_error(); | |
| 1292 | |||
| 1293 | 6970 | nftnl_set_set_u32(nls, NFTNL_SET_FAMILY, h->family); | |
| 1294 | 6970 | nftnl_set_set_str(nls, NFTNL_SET_TABLE, h->table.name); | |
| 1295 | 6970 | nftnl_set_set_str(nls, NFTNL_SET_NAME, h->set.name); | |
| 1296 | 6970 | nftnl_set_set_u32(nls, NFTNL_SET_ID, h->set_id); | |
| 1297 | |||
| 1298 | 6970 | nftnl_set_set_u32(nls, NFTNL_SET_FLAGS, set->flags); | |
| 1299 | 6970 | nftnl_set_set_u32(nls, NFTNL_SET_KEY_TYPE, | |
| 1300 | 6970 | dtype_map_to_kernel(set->key->dtype)); | |
| 1301 | 6970 | nftnl_set_set_u32(nls, NFTNL_SET_KEY_LEN, | |
| 1302 | 6970 | div_round_up(set->key->len, BITS_PER_BYTE)); | |
| 1303 |
2/2✓ Branch 0 (14→15) taken 1524 times.
✓ Branch 1 (14→18) taken 5446 times.
|
6970 | if (set_is_datamap(set->flags)) { |
| 1304 | 1524 | nftnl_set_set_u32(nls, NFTNL_SET_DATA_TYPE, | |
| 1305 | 1524 | dtype_map_to_kernel(set->data->dtype)); | |
| 1306 | 1524 | nftnl_set_set_u32(nls, NFTNL_SET_DATA_LEN, | |
| 1307 | 1524 | div_round_up(set->data->len, BITS_PER_BYTE)); | |
| 1308 | } | ||
| 1309 |
2/2✓ Branch 0 (19→20) taken 127 times.
✓ Branch 1 (19→21) taken 6843 times.
|
6970 | if (set_is_objmap(set->flags)) |
| 1310 | 127 | nftnl_set_set_u32(nls, NFTNL_SET_OBJ_TYPE, set->objtype); | |
| 1311 | |||
| 1312 |
2/2✓ Branch 0 (21→22) taken 171 times.
✓ Branch 1 (21→23) taken 6799 times.
|
6970 | if (set->timeout) |
| 1313 | 171 | nftnl_set_set_u64(nls, NFTNL_SET_TIMEOUT, set->timeout); | |
| 1314 |
2/2✓ Branch 0 (23→24) taken 17 times.
✓ Branch 1 (23→25) taken 6953 times.
|
6970 | if (set->gc_int) |
| 1315 | 17 | nftnl_set_set_u32(nls, NFTNL_SET_GC_INTERVAL, set->gc_int); | |
| 1316 | |||
| 1317 | 6970 | nftnl_set_set_u32(nls, NFTNL_SET_ID, set->handle.set_id); | |
| 1318 | |||
| 1319 |
2/2✓ Branch 0 (26→27) taken 2791 times.
✓ Branch 1 (26→31) taken 4179 times.
|
6970 | if (!(set->flags & NFT_SET_CONSTANT)) { |
| 1320 |
1/2✗ Branch 0 (27→28) not taken.
✓ Branch 1 (27→29) taken 2791 times.
|
2791 | if (set->policy != NFT_SET_POL_PERFORMANCE) |
| 1321 | ✗ | nftnl_set_set_u32(nls, NFTNL_SET_POLICY, set->policy); | |
| 1322 | |||
| 1323 |
2/2✓ Branch 0 (29→30) taken 1208 times.
✓ Branch 1 (29→31) taken 1583 times.
|
2791 | if (set->desc.size != 0) |
| 1324 | 1208 | nftnl_set_set_u32(nls, NFTNL_SET_DESC_SIZE, | |
| 1325 | set->desc.size); | ||
| 1326 | } | ||
| 1327 | |||
| 1328 | 6970 | udbuf = nftnl_udata_buf_alloc(NFT_USERDATA_MAXLEN); | |
| 1329 |
1/2✗ Branch 0 (32→33) not taken.
✓ Branch 1 (32→34) taken 6970 times.
|
6970 | if (!udbuf) |
| 1330 | ✗ | memory_allocation_error(); | |
| 1331 |
1/2✗ Branch 0 (35→36) not taken.
✓ Branch 1 (35→37) taken 6970 times.
|
6970 | if (!nftnl_udata_put_u32(udbuf, NFTNL_UDATA_SET_KEYBYTEORDER, |
| 1332 | 6970 | set->key->byteorder)) | |
| 1333 | ✗ | memory_allocation_error(); | |
| 1334 | |||
| 1335 |
2/2✓ Branch 0 (38→39) taken 1524 times.
✓ Branch 1 (38→42) taken 5446 times.
|
6970 | if (set_is_datamap(set->flags) && |
| 1336 |
1/2✗ Branch 0 (40→41) not taken.
✓ Branch 1 (40→42) taken 1524 times.
|
1524 | !nftnl_udata_put_u32(udbuf, NFTNL_UDATA_SET_DATABYTEORDER, |
| 1337 | 1524 | set->data->byteorder)) | |
| 1338 | ✗ | memory_allocation_error(); | |
| 1339 | |||
| 1340 |
2/2✓ Branch 0 (42→43) taken 371 times.
✓ Branch 1 (42→46) taken 6599 times.
|
6970 | if (set->automerge && |
| 1341 |
1/2✗ Branch 0 (44→45) not taken.
✓ Branch 1 (44→46) taken 371 times.
|
371 | !nftnl_udata_put_u32(udbuf, NFTNL_UDATA_SET_MERGE_ELEMENTS, |
| 1342 | 371 | set->automerge)) | |
| 1343 | ✗ | memory_allocation_error(); | |
| 1344 | |||
| 1345 | 6970 | set_key_expression(ctx, set->key, set->flags, udbuf, NFTNL_UDATA_SET_KEY_TYPEOF); | |
| 1346 |
2/2✓ Branch 0 (47→48) taken 1651 times.
✓ Branch 1 (47→50) taken 5319 times.
|
6970 | if (set->data) { |
| 1347 | 1651 | set_key_expression(ctx, set->data, set->flags, udbuf, NFTNL_UDATA_SET_DATA_TYPEOF); | |
| 1348 | 1651 | nftnl_udata_put_u32(udbuf, NFTNL_UDATA_SET_DATA_INTERVAL, | |
| 1349 | 1651 | !!(set->data->flags & EXPR_F_INTERVAL)); | |
| 1350 | } | ||
| 1351 | |||
| 1352 |
2/2✓ Branch 0 (50→51) taken 430 times.
✓ Branch 1 (50→52) taken 6540 times.
|
6970 | if (set->desc.field_len[0]) { |
| 1353 | 430 | nftnl_set_set_data(nls, NFTNL_SET_DESC_CONCAT, | |
| 1354 | 430 | set->desc.field_len, | |
| 1355 | 430 | set->desc.field_count * | |
| 1356 | sizeof(set->desc.field_len[0])); | ||
| 1357 | } | ||
| 1358 | |||
| 1359 |
2/2✓ Branch 0 (52→53) taken 4 times.
✓ Branch 1 (52→56) taken 6966 times.
|
6970 | if (set->comment) { |
| 1360 |
1/2✗ Branch 0 (54→55) not taken.
✓ Branch 1 (54→56) taken 4 times.
|
4 | if (!nftnl_udata_put_strz(udbuf, NFTNL_UDATA_SET_COMMENT, set->comment)) |
| 1361 | ✗ | memory_allocation_error(); | |
| 1362 | } | ||
| 1363 | |||
| 1364 | 6970 | nftnl_set_set_data(nls, NFTNL_SET_USERDATA, nftnl_udata_buf_data(udbuf), | |
| 1365 | nftnl_udata_buf_len(udbuf)); | ||
| 1366 | 6970 | nftnl_udata_buf_free(udbuf); | |
| 1367 | |||
| 1368 |
2/2✓ Branch 0 (62→61) taken 126 times.
✓ Branch 1 (62→63) taken 6970 times.
|
7096 | list_for_each_entry(stmt, &set->stmt_list, list) |
| 1369 | 126 | num_stmts++; | |
| 1370 | |||
| 1371 |
2/2✓ Branch 0 (63→64) taken 108 times.
✓ Branch 1 (63→70) taken 6862 times.
|
6970 | if (num_stmts == 1) { |
| 1372 |
1/2✓ Branch 0 (68→65) taken 108 times.
✗ Branch 1 (68→69) not taken.
|
108 | list_for_each_entry(stmt, &set->stmt_list, list) { |
| 1373 | 108 | nftnl_set_set_data(nls, NFTNL_SET_EXPR, | |
| 1374 | 108 | netlink_gen_stmt_stateful(stmt), 0); | |
| 1375 | 108 | break; | |
| 1376 | } | ||
| 1377 |
2/2✓ Branch 0 (70→71) taken 9 times.
✓ Branch 1 (70→76) taken 6853 times.
|
6862 | } else if (num_stmts > 1) { |
| 1378 |
2/2✓ Branch 0 (75→72) taken 18 times.
✓ Branch 1 (75→76) taken 9 times.
|
27 | list_for_each_entry(stmt, &set->stmt_list, list) |
| 1379 | 18 | nftnl_set_add_expr(nls, netlink_gen_stmt_stateful(stmt)); | |
| 1380 | } | ||
| 1381 | |||
| 1382 | 6970 | netlink_dump_set(nls, ctx); | |
| 1383 | |||
| 1384 | 6970 | nftnl_set_unset(nls, NFTNL_SET_TABLE); | |
| 1385 | 6970 | nftnl_set_unset(nls, NFTNL_SET_NAME); | |
| 1386 | |||
| 1387 | 6970 | nlh = nftnl_nlmsg_build_hdr(nftnl_batch_buffer(ctx->batch), | |
| 1388 | NFT_MSG_NEWSET, | ||
| 1389 | 6970 | h->family, | |
| 1390 | 6970 | NLM_F_CREATE | flags, ctx->seqnum); | |
| 1391 | |||
| 1392 | 6970 | cmd_add_loc(cmd, nlh, &h->table.location); | |
| 1393 | 6970 | mnl_attr_put_strz(nlh, NFTA_SET_TABLE, h->table.name); | |
| 1394 | 6970 | cmd_add_loc(cmd, nlh, &h->set.location); | |
| 1395 | 6970 | mnl_attr_put_strz(nlh, NFTA_SET_NAME, h->set.name); | |
| 1396 | |||
| 1397 | 6970 | nftnl_set_nlmsg_build_payload(nlh, nls); | |
| 1398 | 6970 | nftnl_set_free(nls); | |
| 1399 | |||
| 1400 | 6970 | mnl_nft_batch_continue(ctx->batch); | |
| 1401 | |||
| 1402 | 6970 | return 0; | |
| 1403 | } | ||
| 1404 | |||
| 1405 | 88 | int mnl_nft_set_del(struct netlink_ctx *ctx, struct cmd *cmd) | |
| 1406 | { | ||
| 1407 | 88 | enum nf_tables_msg_types msg_type = NFT_MSG_DELSET; | |
| 1408 | 88 | const struct handle *h = &cmd->handle; | |
| 1409 | struct nftnl_set *nls; | ||
| 1410 | struct nlmsghdr *nlh; | ||
| 1411 | |||
| 1412 | 88 | nls = nftnl_set_alloc(); | |
| 1413 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 88 times.
|
88 | if (!nls) |
| 1414 | ✗ | memory_allocation_error(); | |
| 1415 | |||
| 1416 | 88 | nftnl_set_set_u32(nls, NFTNL_SET_FAMILY, h->family); | |
| 1417 | |||
| 1418 |
2/2✓ Branch 0 (6→7) taken 4 times.
✓ Branch 1 (6→8) taken 84 times.
|
88 | if (cmd->op == CMD_DESTROY) |
| 1419 | 4 | msg_type = NFT_MSG_DESTROYSET; | |
| 1420 | |||
| 1421 | 88 | nlh = nftnl_nlmsg_build_hdr(nftnl_batch_buffer(ctx->batch), | |
| 1422 | msg_type, | ||
| 1423 | 88 | h->family, | |
| 1424 | 0, ctx->seqnum); | ||
| 1425 | |||
| 1426 | 88 | cmd_add_loc(cmd, nlh, &cmd->handle.table.location); | |
| 1427 | 88 | mnl_attr_put_strz(nlh, NFTA_SET_TABLE, cmd->handle.table.name); | |
| 1428 |
2/2✓ Branch 0 (12→13) taken 83 times.
✓ Branch 1 (12→15) taken 5 times.
|
88 | if (h->set.name) { |
| 1429 | 83 | cmd_add_loc(cmd, nlh, &cmd->handle.set.location); | |
| 1430 | 83 | mnl_attr_put_strz(nlh, NFTA_SET_NAME, cmd->handle.set.name); | |
| 1431 |
1/2✓ Branch 0 (15→16) taken 5 times.
✗ Branch 1 (15→19) not taken.
|
5 | } else if (h->handle.id) { |
| 1432 | 5 | cmd_add_loc(cmd, nlh, &cmd->handle.handle.location); | |
| 1433 | 5 | mnl_attr_put_u64(nlh, NFTA_SET_HANDLE, | |
| 1434 | 5 | htobe64(cmd->handle.handle.id)); | |
| 1435 | } | ||
| 1436 | |||
| 1437 | 88 | nftnl_set_nlmsg_build_payload(nlh, nls); | |
| 1438 | 88 | nftnl_set_free(nls); | |
| 1439 | |||
| 1440 | 88 | mnl_nft_batch_continue(ctx->batch); | |
| 1441 | |||
| 1442 | 88 | return 0; | |
| 1443 | } | ||
| 1444 | |||
| 1445 | struct set_cb_args { | ||
| 1446 | struct netlink_ctx *ctx; | ||
| 1447 | struct nftnl_set_list *list; | ||
| 1448 | }; | ||
| 1449 | |||
| 1450 | 37334 | static int set_cb(const struct nlmsghdr *nlh, void *data) | |
| 1451 | { | ||
| 1452 | 37334 | struct set_cb_args *args = data; | |
| 1453 | 37334 | struct nftnl_set_list *nls_list = args->list; | |
| 1454 | struct nftnl_set *s; | ||
| 1455 | |||
| 1456 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 37334 times.
|
37334 | if (check_genid(nlh) < 0) |
| 1457 | ✗ | return MNL_CB_ERROR; | |
| 1458 | |||
| 1459 | 37334 | s = nftnl_set_alloc(); | |
| 1460 |
1/2✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→8) taken 37334 times.
|
37334 | if (s == NULL) |
| 1461 | ✗ | memory_allocation_error(); | |
| 1462 | |||
| 1463 |
1/2✗ Branch 0 (9→10) not taken.
✓ Branch 1 (9→11) taken 37334 times.
|
37334 | if (nftnl_set_nlmsg_parse(nlh, s) < 0) |
| 1464 | ✗ | goto err_free; | |
| 1465 | |||
| 1466 | 37334 | netlink_dump_set(s, args->ctx); | |
| 1467 | |||
| 1468 | 37334 | nftnl_set_list_add_tail(s, nls_list); | |
| 1469 | 37334 | return MNL_CB_OK; | |
| 1470 | |||
| 1471 | ✗ | err_free: | |
| 1472 | ✗ | nftnl_set_free(s); | |
| 1473 | ✗ | return MNL_CB_OK; | |
| 1474 | } | ||
| 1475 | |||
| 1476 | struct nftnl_set_list * | ||
| 1477 | 37688 | mnl_nft_set_dump(struct netlink_ctx *ctx, int family, | |
| 1478 | const char *table, const char *set) | ||
| 1479 | 37688 | { | |
| 1480 |
1/2✓ Branch 0 (3→4) taken 37688 times.
✗ Branch 1 (3→5) not taken.
|
37688 | char buf[MNL_SOCKET_BUFFER_SIZE]; |
| 1481 | struct nftnl_set_list *nls_list; | ||
| 1482 | 37688 | int flags = NLM_F_DUMP; | |
| 1483 | struct nlmsghdr *nlh; | ||
| 1484 | struct nftnl_set *s; | ||
| 1485 | int ret; | ||
| 1486 | struct set_cb_args args; | ||
| 1487 | |||
| 1488 | 37688 | s = nftnl_set_alloc(); | |
| 1489 |
1/2✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→9) taken 37688 times.
|
37688 | if (s == NULL) |
| 1490 | ✗ | memory_allocation_error(); | |
| 1491 | |||
| 1492 |
2/2✓ Branch 0 (9→10) taken 14910 times.
✓ Branch 1 (9→11) taken 22778 times.
|
37688 | if (table != NULL) |
| 1493 | 14910 | nftnl_set_set_str(s, NFTNL_SET_TABLE, table); | |
| 1494 |
2/2✓ Branch 0 (11→12) taken 1025 times.
✓ Branch 1 (11→14) taken 36663 times.
|
37688 | if (set) { |
| 1495 | 1025 | nftnl_set_set_str(s, NFTNL_SET_NAME, set); | |
| 1496 | 1025 | flags = NLM_F_ACK; | |
| 1497 | } | ||
| 1498 | |||
| 1499 | 37688 | nlh = nftnl_nlmsg_build_hdr(buf, NFT_MSG_GETSET, family, | |
| 1500 | flags, ctx->seqnum); | ||
| 1501 | 37688 | nftnl_set_nlmsg_build_payload(nlh, s); | |
| 1502 | 37688 | nftnl_set_free(s); | |
| 1503 | |||
| 1504 | 37688 | nls_list = nftnl_set_list_alloc(); | |
| 1505 |
1/2✗ Branch 0 (18→19) not taken.
✓ Branch 1 (18→20) taken 37688 times.
|
37688 | if (nls_list == NULL) |
| 1506 | ✗ | memory_allocation_error(); | |
| 1507 | |||
| 1508 | 37688 | args.list = nls_list; | |
| 1509 | 37688 | args.ctx = ctx; | |
| 1510 | 37688 | ret = nft_mnl_talk(ctx, nlh, nlh->nlmsg_len, set_cb, &args); | |
| 1511 |
3/4✓ Branch 0 (21→22) taken 618 times.
✓ Branch 1 (21→24) taken 37070 times.
✗ Branch 2 (22→23) not taken.
✓ Branch 3 (22→24) taken 618 times.
|
37688 | if (ret < 0 && errno != ENOENT) |
| 1512 | ✗ | goto err; | |
| 1513 | |||
| 1514 | 37688 | return nls_list; | |
| 1515 | ✗ | err: | |
| 1516 | ✗ | nftnl_set_list_free(nls_list); | |
| 1517 | ✗ | return NULL; | |
| 1518 | } | ||
| 1519 | |||
| 1520 | 18 | static void obj_tunnel_add_opts(struct nftnl_obj *nlo, struct tunnel *tunnel) | |
| 1521 | { | ||
| 1522 | struct nftnl_tunnel_opts *opts; | ||
| 1523 | struct nftnl_tunnel_opt *opt; | ||
| 1524 | |||
| 1525 |
4/5✓ Branch 0 (2→3) taken 8 times.
✓ Branch 1 (2→19) taken 4 times.
✓ Branch 2 (2→29) taken 5 times.
✓ Branch 3 (2→44) taken 1 times.
✗ Branch 4 (2→45) not taken.
|
18 | switch (tunnel->type) { |
| 1526 | 8 | case TUNNEL_ERSPAN: | |
| 1527 | 8 | opts = nftnl_tunnel_opts_alloc(NFTNL_TUNNEL_TYPE_ERSPAN); | |
| 1528 |
1/2✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→6) taken 8 times.
|
8 | if (!opts) |
| 1529 | ✗ | memory_allocation_error(); | |
| 1530 | |||
| 1531 | 8 | opt = nftnl_tunnel_opt_alloc(NFTNL_TUNNEL_TYPE_ERSPAN); | |
| 1532 |
1/2✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→9) taken 8 times.
|
8 | if (!opt) |
| 1533 | ✗ | memory_allocation_error(); | |
| 1534 | |||
| 1535 | 8 | nftnl_tunnel_opt_set(opt, NFTNL_TUNNEL_ERSPAN_VERSION, | |
| 1536 | 8 | &tunnel->erspan.version, | |
| 1537 | sizeof(tunnel->erspan.version)); | ||
| 1538 |
2/3✓ Branch 0 (10→11) taken 4 times.
✓ Branch 1 (10→13) taken 4 times.
✗ Branch 2 (10→16) not taken.
|
8 | switch (tunnel->erspan.version) { |
| 1539 | 4 | case 1: | |
| 1540 | 4 | nftnl_tunnel_opt_set(opt, NFTNL_TUNNEL_ERSPAN_V1_INDEX, | |
| 1541 | 4 | &tunnel->erspan.v1.index, | |
| 1542 | sizeof(tunnel->erspan.v1.index)); | ||
| 1543 | 4 | break; | |
| 1544 | 4 | case 2: | |
| 1545 | 4 | nftnl_tunnel_opt_set(opt, NFTNL_TUNNEL_ERSPAN_V2_DIR, | |
| 1546 | 4 | &tunnel->erspan.v2.direction, | |
| 1547 | sizeof(tunnel->erspan.v2.direction)); | ||
| 1548 | 4 | nftnl_tunnel_opt_set(opt, NFTNL_TUNNEL_ERSPAN_V2_HWID, | |
| 1549 | 4 | &tunnel->erspan.v2.hwid, | |
| 1550 | sizeof(tunnel->erspan.v2.hwid)); | ||
| 1551 | 4 | break; | |
| 1552 | } | ||
| 1553 | |||
| 1554 | 8 | nftnl_tunnel_opts_add(opts, opt); | |
| 1555 | 8 | nftnl_obj_set_data(nlo, NFTNL_OBJ_TUNNEL_OPTS, &opts, sizeof(struct nftnl_tunnel_opts *)); | |
| 1556 | 8 | break; | |
| 1557 | 4 | case TUNNEL_VXLAN: | |
| 1558 | 4 | opts = nftnl_tunnel_opts_alloc(NFTNL_TUNNEL_TYPE_VXLAN); | |
| 1559 |
1/2✗ Branch 0 (20→21) not taken.
✓ Branch 1 (20→22) taken 4 times.
|
4 | if (!opts) |
| 1560 | ✗ | memory_allocation_error(); | |
| 1561 | |||
| 1562 | 4 | opt = nftnl_tunnel_opt_alloc(NFTNL_TUNNEL_TYPE_VXLAN); | |
| 1563 |
1/2✗ Branch 0 (23→24) not taken.
✓ Branch 1 (23→25) taken 4 times.
|
4 | if (!opt) |
| 1564 | ✗ | memory_allocation_error(); | |
| 1565 | |||
| 1566 | 4 | nftnl_tunnel_opt_set(opt, NFTNL_TUNNEL_VXLAN_GBP, | |
| 1567 | 4 | &tunnel->vxlan.gbp, | |
| 1568 | sizeof(tunnel->vxlan.gbp)); | ||
| 1569 | |||
| 1570 | 4 | nftnl_tunnel_opts_add(opts, opt); | |
| 1571 | 4 | nftnl_obj_set_data(nlo, NFTNL_OBJ_TUNNEL_OPTS, &opts, sizeof(struct nftnl_tunnel_opts *)); | |
| 1572 | 4 | break; | |
| 1573 | 5 | case TUNNEL_GENEVE: | |
| 1574 | struct tunnel_geneve *geneve; | ||
| 1575 | |||
| 1576 | 5 | opts = nftnl_tunnel_opts_alloc(NFTNL_TUNNEL_TYPE_GENEVE); | |
| 1577 |
1/2✗ Branch 0 (30→31) not taken.
✓ Branch 1 (30→32) taken 5 times.
|
5 | if (!opts) |
| 1578 | ✗ | memory_allocation_error(); | |
| 1579 | |||
| 1580 |
2/2✓ Branch 0 (41→33) taken 15 times.
✓ Branch 1 (41→42) taken 5 times.
|
20 | list_for_each_entry(geneve, &tunnel->geneve_opts, list) { |
| 1581 | 15 | opt = nftnl_tunnel_opt_alloc(NFTNL_TUNNEL_TYPE_GENEVE); | |
| 1582 |
1/2✗ Branch 0 (34→35) not taken.
✓ Branch 1 (34→36) taken 15 times.
|
15 | if (!opt) |
| 1583 | ✗ | memory_allocation_error(); | |
| 1584 | |||
| 1585 | 15 | nftnl_tunnel_opt_set(opt, | |
| 1586 | NFTNL_TUNNEL_GENEVE_TYPE, | ||
| 1587 | 15 | &geneve->type, sizeof(geneve->type)); | |
| 1588 | 15 | nftnl_tunnel_opt_set(opt, | |
| 1589 | NFTNL_TUNNEL_GENEVE_CLASS, | ||
| 1590 | 15 | &geneve->geneve_class, sizeof(geneve->geneve_class)); | |
| 1591 | 15 | nftnl_tunnel_opt_set(opt, | |
| 1592 | NFTNL_TUNNEL_GENEVE_DATA, | ||
| 1593 | 15 | &geneve->data, geneve->data_len); | |
| 1594 | 15 | nftnl_tunnel_opts_add(opts, opt); | |
| 1595 | } | ||
| 1596 | 5 | nftnl_obj_set_data(nlo, NFTNL_OBJ_TUNNEL_OPTS, &opts, sizeof(struct nftnl_tunnel_opts *)); | |
| 1597 | 5 | break; | |
| 1598 | 1 | case TUNNEL_UNSPEC: | |
| 1599 | 1 | break; | |
| 1600 | } | ||
| 1601 | 18 | } | |
| 1602 | |||
| 1603 | 457 | int mnl_nft_obj_add(struct netlink_ctx *ctx, struct cmd *cmd, | |
| 1604 | unsigned int flags) | ||
| 1605 | { | ||
| 1606 | 457 | struct obj *obj = cmd->object; | |
| 1607 | struct nft_data_linearize nld; | ||
| 1608 | struct nftnl_udata_buf *udbuf; | ||
| 1609 | struct nftnl_obj *nlo; | ||
| 1610 | struct nlmsghdr *nlh; | ||
| 1611 | |||
| 1612 | 457 | nlo = nftnl_obj_alloc(); | |
| 1613 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 457 times.
|
457 | if (!nlo) |
| 1614 | ✗ | memory_allocation_error(); | |
| 1615 | |||
| 1616 | 457 | nftnl_obj_set_u32(nlo, NFTNL_OBJ_FAMILY, cmd->handle.family); | |
| 1617 | 457 | nftnl_obj_set_u32(nlo, NFTNL_OBJ_TYPE, obj->type); | |
| 1618 | |||
| 1619 |
2/2✓ Branch 0 (7→8) taken 33 times.
✓ Branch 1 (7→18) taken 424 times.
|
457 | if (obj->comment) { |
| 1620 | 33 | udbuf = nftnl_udata_buf_alloc(NFT_USERDATA_MAXLEN); | |
| 1621 |
1/2✗ Branch 0 (9→10) not taken.
✓ Branch 1 (9→11) taken 33 times.
|
33 | if (!udbuf) |
| 1622 | ✗ | memory_allocation_error(); | |
| 1623 |
1/2✗ Branch 0 (12→13) not taken.
✓ Branch 1 (12→14) taken 33 times.
|
33 | if (!nftnl_udata_put_strz(udbuf, NFTNL_UDATA_OBJ_COMMENT, obj->comment)) |
| 1624 | ✗ | memory_allocation_error(); | |
| 1625 | 33 | nftnl_obj_set_data(nlo, NFTNL_OBJ_USERDATA, nftnl_udata_buf_data(udbuf), | |
| 1626 | nftnl_udata_buf_len(udbuf)); | ||
| 1627 | 33 | nftnl_udata_buf_free(udbuf); | |
| 1628 | } | ||
| 1629 | |||
| 1630 |
9/10✓ Branch 0 (18→19) taken 236 times.
✓ Branch 1 (18→21) taken 66 times.
✓ Branch 2 (18→25) taken 32 times.
✓ Branch 3 (18→31) taken 37 times.
✓ Branch 4 (18→36) taken 20 times.
✓ Branch 5 (18→41) taken 23 times.
✓ Branch 6 (18→48) taken 5 times.
✓ Branch 7 (18→50) taken 20 times.
✓ Branch 8 (18→54) taken 18 times.
✗ Branch 9 (18→75) not taken.
|
457 | switch (obj->type) { |
| 1631 | 236 | case NFT_OBJECT_COUNTER: | |
| 1632 | 236 | nftnl_obj_set_u64(nlo, NFTNL_OBJ_CTR_PKTS, | |
| 1633 | obj->counter.packets); | ||
| 1634 | 236 | nftnl_obj_set_u64(nlo, NFTNL_OBJ_CTR_BYTES, | |
| 1635 | obj->counter.bytes); | ||
| 1636 | 236 | break; | |
| 1637 | 66 | case NFT_OBJECT_QUOTA: | |
| 1638 | 66 | nftnl_obj_set_u64(nlo, NFTNL_OBJ_QUOTA_BYTES, | |
| 1639 | obj->quota.bytes); | ||
| 1640 | 66 | nftnl_obj_set_u64(nlo, NFTNL_OBJ_QUOTA_CONSUMED, | |
| 1641 | obj->quota.used); | ||
| 1642 | 66 | nftnl_obj_set_u32(nlo, NFTNL_OBJ_QUOTA_FLAGS, | |
| 1643 | obj->quota.flags); | ||
| 1644 | 66 | break; | |
| 1645 | 32 | case NFT_OBJECT_LIMIT: | |
| 1646 | 32 | nftnl_obj_set_u64(nlo, NFTNL_OBJ_LIMIT_RATE, obj->limit.rate); | |
| 1647 | 32 | nftnl_obj_set_u64(nlo, NFTNL_OBJ_LIMIT_UNIT, obj->limit.unit); | |
| 1648 | 32 | nftnl_obj_set_u32(nlo, NFTNL_OBJ_LIMIT_BURST, obj->limit.burst); | |
| 1649 | 32 | nftnl_obj_set_u32(nlo, NFTNL_OBJ_LIMIT_TYPE, obj->limit.type); | |
| 1650 | 32 | nftnl_obj_set_u32(nlo, NFTNL_OBJ_LIMIT_FLAGS, obj->limit.flags); | |
| 1651 | 32 | break; | |
| 1652 | 37 | case NFT_OBJECT_CT_HELPER: | |
| 1653 | 37 | nftnl_obj_set_str(nlo, NFTNL_OBJ_CT_HELPER_NAME, | |
| 1654 | 37 | obj->ct_helper.name); | |
| 1655 | 37 | nftnl_obj_set_u8(nlo, NFTNL_OBJ_CT_HELPER_L4PROTO, | |
| 1656 | 37 | obj->ct_helper.l4proto); | |
| 1657 |
2/2✓ Branch 0 (33→34) taken 26 times.
✓ Branch 1 (33→35) taken 11 times.
|
37 | if (obj->ct_helper.l3proto) |
| 1658 | 26 | nftnl_obj_set_u16(nlo, NFTNL_OBJ_CT_HELPER_L3PROTO, | |
| 1659 | 26 | obj->ct_helper.l3proto); | |
| 1660 | 37 | break; | |
| 1661 | 20 | case NFT_OBJECT_CT_TIMEOUT: | |
| 1662 | 20 | nftnl_obj_set_u8(nlo, NFTNL_OBJ_CT_TIMEOUT_L4PROTO, | |
| 1663 | 20 | obj->ct_timeout.l4proto); | |
| 1664 |
2/2✓ Branch 0 (37→38) taken 16 times.
✓ Branch 1 (37→39) taken 4 times.
|
20 | if (obj->ct_timeout.l3proto) |
| 1665 | 16 | nftnl_obj_set_u16(nlo, NFTNL_OBJ_CT_TIMEOUT_L3PROTO, | |
| 1666 | 16 | obj->ct_timeout.l3proto); | |
| 1667 | 20 | nftnl_obj_set_data(nlo, NFTNL_OBJ_CT_TIMEOUT_ARRAY, | |
| 1668 | 20 | obj->ct_timeout.timeout, | |
| 1669 | sizeof(obj->ct_timeout.timeout)); | ||
| 1670 | 20 | break; | |
| 1671 | 23 | case NFT_OBJECT_CT_EXPECT: | |
| 1672 |
2/2✓ Branch 0 (41→42) taken 20 times.
✓ Branch 1 (41→43) taken 3 times.
|
23 | if (obj->ct_expect.l3proto) |
| 1673 | 20 | nftnl_obj_set_u16(nlo, NFTNL_OBJ_CT_EXPECT_L3PROTO, | |
| 1674 | 20 | obj->ct_expect.l3proto); | |
| 1675 | 23 | nftnl_obj_set_u8(nlo, NFTNL_OBJ_CT_EXPECT_L4PROTO, | |
| 1676 | 23 | obj->ct_expect.l4proto); | |
| 1677 | 23 | nftnl_obj_set_u16(nlo, NFTNL_OBJ_CT_EXPECT_DPORT, | |
| 1678 | 23 | obj->ct_expect.dport); | |
| 1679 | 23 | nftnl_obj_set_u32(nlo, NFTNL_OBJ_CT_EXPECT_TIMEOUT, | |
| 1680 | obj->ct_expect.timeout); | ||
| 1681 | 23 | nftnl_obj_set_u8(nlo, NFTNL_OBJ_CT_EXPECT_SIZE, | |
| 1682 | 23 | obj->ct_expect.size); | |
| 1683 | 23 | break; | |
| 1684 | 5 | case NFT_OBJECT_SECMARK: | |
| 1685 | 5 | nftnl_obj_set_str(nlo, NFTNL_OBJ_SECMARK_CTX, | |
| 1686 | 5 | obj->secmark.ctx); | |
| 1687 | 5 | break; | |
| 1688 | 20 | case NFT_OBJECT_SYNPROXY: | |
| 1689 | 20 | nftnl_obj_set_u16(nlo, NFTNL_OBJ_SYNPROXY_MSS, | |
| 1690 | 20 | obj->synproxy.mss); | |
| 1691 | 20 | nftnl_obj_set_u8(nlo, NFTNL_OBJ_SYNPROXY_WSCALE, | |
| 1692 | 20 | obj->synproxy.wscale); | |
| 1693 | 20 | nftnl_obj_set_u32(nlo, NFTNL_OBJ_SYNPROXY_FLAGS, | |
| 1694 | obj->synproxy.flags); | ||
| 1695 | 20 | break; | |
| 1696 | 18 | case NFT_OBJECT_TUNNEL: | |
| 1697 | 18 | nftnl_obj_set_u32(nlo, NFTNL_OBJ_TUNNEL_ID, obj->tunnel.id); | |
| 1698 |
2/2✓ Branch 0 (55→56) taken 17 times.
✓ Branch 1 (55→57) taken 1 times.
|
18 | if (obj->tunnel.sport) |
| 1699 | 17 | nftnl_obj_set_u16(nlo, NFTNL_OBJ_TUNNEL_SPORT, | |
| 1700 | 17 | obj->tunnel.sport); | |
| 1701 |
2/2✓ Branch 0 (57→58) taken 9 times.
✓ Branch 1 (57→59) taken 9 times.
|
18 | if (obj->tunnel.dport) |
| 1702 | 9 | nftnl_obj_set_u16(nlo, NFTNL_OBJ_TUNNEL_DPORT, | |
| 1703 | 9 | obj->tunnel.dport); | |
| 1704 |
2/2✓ Branch 0 (59→60) taken 16 times.
✓ Branch 1 (59→61) taken 2 times.
|
18 | if (obj->tunnel.tos) |
| 1705 | 16 | nftnl_obj_set_u8(nlo, NFTNL_OBJ_TUNNEL_TOS, | |
| 1706 | 16 | obj->tunnel.tos); | |
| 1707 |
2/2✓ Branch 0 (61→62) taken 17 times.
✓ Branch 1 (61→63) taken 1 times.
|
18 | if (obj->tunnel.ttl) |
| 1708 | 17 | nftnl_obj_set_u8(nlo, NFTNL_OBJ_TUNNEL_TTL, | |
| 1709 | 17 | obj->tunnel.ttl); | |
| 1710 |
2/2✓ Branch 0 (63→64) taken 17 times.
✓ Branch 1 (63→68) taken 1 times.
|
18 | if (obj->tunnel.src) { |
| 1711 | 17 | netlink_gen_data(obj->tunnel.src, &nld); | |
| 1712 |
1/2✓ Branch 0 (65→66) taken 17 times.
✗ Branch 1 (65→67) not taken.
|
17 | if (nld.len == sizeof(struct in_addr)) { |
| 1713 | 17 | nftnl_obj_set_u32(nlo, | |
| 1714 | NFTNL_OBJ_TUNNEL_IPV4_SRC, | ||
| 1715 | nld.value[0]); | ||
| 1716 | } else { | ||
| 1717 | ✗ | nftnl_obj_set_data(nlo, | |
| 1718 | NFTNL_OBJ_TUNNEL_IPV6_SRC, | ||
| 1719 | nld.value, nld.len); | ||
| 1720 | } | ||
| 1721 | } | ||
| 1722 |
2/2✓ Branch 0 (68→69) taken 17 times.
✓ Branch 1 (68→73) taken 1 times.
|
18 | if (obj->tunnel.dst) { |
| 1723 | 17 | netlink_gen_data(obj->tunnel.dst, &nld); | |
| 1724 |
1/2✓ Branch 0 (70→71) taken 17 times.
✗ Branch 1 (70→72) not taken.
|
17 | if (nld.len == sizeof(struct in_addr)) { |
| 1725 | 17 | nftnl_obj_set_u32(nlo, | |
| 1726 | NFTNL_OBJ_TUNNEL_IPV4_DST, | ||
| 1727 | nld.value[0]); | ||
| 1728 | } else { | ||
| 1729 | ✗ | nftnl_obj_set_data(nlo, | |
| 1730 | NFTNL_OBJ_TUNNEL_IPV6_DST, | ||
| 1731 | nld.value, nld.len); | ||
| 1732 | } | ||
| 1733 | } | ||
| 1734 | 18 | obj_tunnel_add_opts(nlo, &obj->tunnel); | |
| 1735 | 18 | break; | |
| 1736 | ✗ | default: | |
| 1737 | ✗ | BUG("Unknown type %d", obj->type); | |
| 1738 | break; | ||
| 1739 | } | ||
| 1740 | 457 | netlink_dump_obj(nlo, ctx); | |
| 1741 | |||
| 1742 | 457 | nlh = nftnl_nlmsg_build_hdr(nftnl_batch_buffer(ctx->batch), | |
| 1743 | 457 | NFT_MSG_NEWOBJ, cmd->handle.family, | |
| 1744 | 457 | NLM_F_CREATE | flags, ctx->seqnum); | |
| 1745 | |||
| 1746 | 457 | cmd_add_loc(cmd, nlh, &cmd->handle.table.location); | |
| 1747 | 457 | mnl_attr_put_strz(nlh, NFTA_OBJ_TABLE, cmd->handle.table.name); | |
| 1748 | 457 | cmd_add_loc(cmd, nlh, &cmd->handle.obj.location); | |
| 1749 | 457 | mnl_attr_put_strz(nlh, NFTA_OBJ_NAME, cmd->handle.obj.name); | |
| 1750 | |||
| 1751 | 457 | nftnl_obj_nlmsg_build_payload(nlh, nlo); | |
| 1752 | 457 | nftnl_obj_free(nlo); | |
| 1753 | |||
| 1754 | 457 | mnl_nft_batch_continue(ctx->batch); | |
| 1755 | |||
| 1756 | 457 | return 0; | |
| 1757 | } | ||
| 1758 | |||
| 1759 | 28 | int mnl_nft_obj_del(struct netlink_ctx *ctx, struct cmd *cmd, int type) | |
| 1760 | { | ||
| 1761 | 28 | enum nf_tables_msg_types msg_type = NFT_MSG_DELOBJ; | |
| 1762 | struct nftnl_obj *nlo; | ||
| 1763 | struct nlmsghdr *nlh; | ||
| 1764 | |||
| 1765 | 28 | nlo = nftnl_obj_alloc(); | |
| 1766 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 28 times.
|
28 | if (!nlo) |
| 1767 | ✗ | memory_allocation_error(); | |
| 1768 | |||
| 1769 | 28 | nftnl_obj_set_u32(nlo, NFTNL_OBJ_FAMILY, cmd->handle.family); | |
| 1770 | 28 | nftnl_obj_set_u32(nlo, NFTNL_OBJ_TYPE, type); | |
| 1771 | |||
| 1772 |
1/2✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→9) taken 28 times.
|
28 | if (cmd->op == CMD_DESTROY) |
| 1773 | ✗ | msg_type = NFT_MSG_DESTROYOBJ; | |
| 1774 | |||
| 1775 | 28 | nlh = nftnl_nlmsg_build_hdr(nftnl_batch_buffer(ctx->batch), | |
| 1776 | 28 | msg_type, cmd->handle.family, | |
| 1777 | 0, ctx->seqnum); | ||
| 1778 | |||
| 1779 | 28 | cmd_add_loc(cmd, nlh, &cmd->handle.table.location); | |
| 1780 | 28 | mnl_attr_put_strz(nlh, NFTA_OBJ_TABLE, cmd->handle.table.name); | |
| 1781 | |||
| 1782 |
2/2✓ Branch 0 (13→14) taken 24 times.
✓ Branch 1 (13→16) taken 4 times.
|
28 | if (cmd->handle.obj.name) { |
| 1783 | 24 | cmd_add_loc(cmd, nlh, &cmd->handle.obj.location); | |
| 1784 | 24 | mnl_attr_put_strz(nlh, NFTA_OBJ_NAME, cmd->handle.obj.name); | |
| 1785 |
1/2✓ Branch 0 (16→17) taken 4 times.
✗ Branch 1 (16→20) not taken.
|
4 | } else if (cmd->handle.handle.id) { |
| 1786 | 4 | cmd_add_loc(cmd, nlh, &cmd->handle.handle.location); | |
| 1787 | 4 | mnl_attr_put_u64(nlh, NFTA_OBJ_HANDLE, | |
| 1788 | 4 | htobe64(cmd->handle.handle.id)); | |
| 1789 | } | ||
| 1790 | |||
| 1791 | 28 | nftnl_obj_nlmsg_build_payload(nlh, nlo); | |
| 1792 | 28 | nftnl_obj_free(nlo); | |
| 1793 | |||
| 1794 | 28 | mnl_nft_batch_continue(ctx->batch); | |
| 1795 | |||
| 1796 | 28 | return 0; | |
| 1797 | } | ||
| 1798 | |||
| 1799 | 2496 | static int obj_cb(const struct nlmsghdr *nlh, void *data) | |
| 1800 | { | ||
| 1801 | 2496 | struct nftnl_obj_list *nln_list = data; | |
| 1802 | struct nftnl_obj *n; | ||
| 1803 | |||
| 1804 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 2496 times.
|
2496 | if (check_genid(nlh) < 0) |
| 1805 | ✗ | return MNL_CB_ERROR; | |
| 1806 | |||
| 1807 | 2496 | n = nftnl_obj_alloc(); | |
| 1808 |
1/2✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→8) taken 2496 times.
|
2496 | if (n == NULL) |
| 1809 | ✗ | memory_allocation_error(); | |
| 1810 | |||
| 1811 |
1/2✗ Branch 0 (9→10) not taken.
✓ Branch 1 (9→11) taken 2496 times.
|
2496 | if (nftnl_obj_nlmsg_parse(nlh, n) < 0) |
| 1812 | ✗ | goto err_free; | |
| 1813 | |||
| 1814 | 2496 | nftnl_obj_list_add_tail(n, nln_list); | |
| 1815 | 2496 | return MNL_CB_OK; | |
| 1816 | |||
| 1817 | ✗ | err_free: | |
| 1818 | ✗ | nftnl_obj_free(n); | |
| 1819 | ✗ | return MNL_CB_OK; | |
| 1820 | } | ||
| 1821 | |||
| 1822 | |||
| 1823 | struct nftnl_obj_list * | ||
| 1824 | 16314 | mnl_nft_obj_dump(struct netlink_ctx *ctx, int family, | |
| 1825 | const char *table, const char *name, uint32_t type, bool dump, | ||
| 1826 | bool reset) | ||
| 1827 | 16314 | { | |
| 1828 |
2/2✓ Branch 0 (2→3) taken 15910 times.
✓ Branch 1 (2→4) taken 404 times.
|
16314 | uint16_t nl_flags = dump ? NLM_F_DUMP : NLM_F_ACK; |
| 1829 | struct nftnl_obj_list *nln_list; | ||
| 1830 |
1/2✓ Branch 0 (6→7) taken 16314 times.
✗ Branch 1 (6→8) not taken.
|
16314 | char buf[MNL_SOCKET_BUFFER_SIZE]; |
| 1831 | struct nlmsghdr *nlh; | ||
| 1832 | struct nftnl_obj *n; | ||
| 1833 | int msg_type, ret; | ||
| 1834 | |||
| 1835 |
2/2✓ Branch 0 (9→10) taken 83 times.
✓ Branch 1 (9→11) taken 16231 times.
|
16314 | if (reset) |
| 1836 | 83 | msg_type = NFT_MSG_GETOBJ_RESET; | |
| 1837 | else | ||
| 1838 | 16231 | msg_type = NFT_MSG_GETOBJ; | |
| 1839 | |||
| 1840 | 16314 | n = nftnl_obj_alloc(); | |
| 1841 |
1/2✗ Branch 0 (13→14) not taken.
✓ Branch 1 (13→15) taken 16314 times.
|
16314 | if (n == NULL) |
| 1842 | ✗ | memory_allocation_error(); | |
| 1843 | |||
| 1844 | 16314 | nlh = nftnl_nlmsg_build_hdr(buf, msg_type, family, | |
| 1845 | nl_flags, ctx->seqnum); | ||
| 1846 |
2/2✓ Branch 0 (16→17) taken 14289 times.
✓ Branch 1 (16→18) taken 2025 times.
|
16314 | if (table != NULL) |
| 1847 | 14289 | nftnl_obj_set_str(n, NFTNL_OBJ_TABLE, table); | |
| 1848 |
2/2✓ Branch 0 (18→19) taken 404 times.
✓ Branch 1 (18→20) taken 15910 times.
|
16314 | if (name != NULL) |
| 1849 | 404 | nftnl_obj_set_str(n, NFTNL_OBJ_NAME, name); | |
| 1850 |
2/2✓ Branch 0 (20→21) taken 416 times.
✓ Branch 1 (20→22) taken 15898 times.
|
16314 | if (type != NFT_OBJECT_UNSPEC) |
| 1851 | 416 | nftnl_obj_set_u32(n, NFTNL_OBJ_TYPE, type); | |
| 1852 | 16314 | nftnl_obj_nlmsg_build_payload(nlh, n); | |
| 1853 | 16314 | nftnl_obj_free(n); | |
| 1854 | |||
| 1855 | 16314 | nln_list = nftnl_obj_list_alloc(); | |
| 1856 |
1/2✗ Branch 0 (25→26) not taken.
✓ Branch 1 (25→27) taken 16314 times.
|
16314 | if (nln_list == NULL) |
| 1857 | ✗ | memory_allocation_error(); | |
| 1858 | |||
| 1859 | 16314 | ret = nft_mnl_talk(ctx, nlh, nlh->nlmsg_len, obj_cb, nln_list); | |
| 1860 |
2/2✓ Branch 0 (28→29) taken 31 times.
✓ Branch 1 (28→30) taken 16283 times.
|
16314 | if (ret < 0) |
| 1861 | 31 | goto err; | |
| 1862 | |||
| 1863 | 16283 | return nln_list; | |
| 1864 | 31 | err: | |
| 1865 | 31 | nftnl_obj_list_free(nln_list); | |
| 1866 | 31 | return NULL; | |
| 1867 | } | ||
| 1868 | |||
| 1869 | /* | ||
| 1870 | * Set elements | ||
| 1871 | */ | ||
| 1872 | 27668 | static int set_elem_cb(const struct nlmsghdr *nlh, void *data) | |
| 1873 | { | ||
| 1874 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 27668 times.
|
27668 | if (check_genid(nlh) < 0) |
| 1875 | ✗ | return MNL_CB_ERROR; | |
| 1876 | |||
| 1877 | 27668 | nftnl_set_elems_nlmsg_parse(nlh, data); | |
| 1878 | 27668 | return MNL_CB_OK; | |
| 1879 | } | ||
| 1880 | |||
| 1881 | 1370698 | static bool mnl_nft_attr_nest_overflow(struct nlmsghdr *nlh, | |
| 1882 | const struct nlattr *from, | ||
| 1883 | const struct nlattr *to, | ||
| 1884 | unsigned int nest_len) | ||
| 1885 | { | ||
| 1886 | 1370698 | int len = (void *)to + nest_len - (void *)from; | |
| 1887 | |||
| 1888 | /* The attribute length field is 16 bits long, thus the maximum payload | ||
| 1889 | * that an attribute can convey is UINT16_MAX. In case of overflow, | ||
| 1890 | * discard the last attribute that did not fit into the nest. | ||
| 1891 | */ | ||
| 1892 |
2/2✓ Branch 0 (2→3) taken 525 times.
✓ Branch 1 (2→4) taken 1370173 times.
|
1370698 | if (len > UINT16_MAX) { |
| 1893 | 525 | nlh->nlmsg_len -= nest_len; | |
| 1894 | 525 | return true; | |
| 1895 | } | ||
| 1896 | 1370173 | return false; | |
| 1897 | } | ||
| 1898 | |||
| 1899 | 2048843 | static void netlink_dump_setelem(const struct nftnl_set_elem *nlse, | |
| 1900 | struct netlink_ctx *ctx) | ||
| 1901 | { | ||
| 1902 | 2048843 | FILE *fp = ctx->nft->output.output_fp; | |
| 1903 | char buf[4096]; | ||
| 1904 | |||
| 1905 |
3/4✓ Branch 0 (2→3) taken 10510 times.
✓ Branch 1 (2→4) taken 2038333 times.
✗ Branch 2 (3→4) not taken.
✓ Branch 3 (3→5) taken 10510 times.
|
2048843 | if (!(ctx->nft->debug_mask & NFT_DEBUG_NETLINK) || !fp) |
| 1906 | 2038333 | return; | |
| 1907 | |||
| 1908 | 10510 | nftnl_set_elem_snprintf(buf, sizeof(buf), nlse, NFTNL_OUTPUT_DEFAULT, 0); | |
| 1909 | 10510 | fprintf(fp, "\t%s", buf); | |
| 1910 | } | ||
| 1911 | |||
| 1912 | 30527 | static void netlink_dump_setelem_done(struct netlink_ctx *ctx) | |
| 1913 | { | ||
| 1914 | 30527 | FILE *fp = ctx->nft->output.output_fp; | |
| 1915 | |||
| 1916 |
3/4✓ Branch 0 (2→3) taken 3173 times.
✓ Branch 1 (2→4) taken 27354 times.
✗ Branch 2 (3→4) not taken.
✓ Branch 3 (3→5) taken 3173 times.
|
30527 | if (!(ctx->nft->debug_mask & NFT_DEBUG_NETLINK) || !fp) |
| 1917 | 27354 | return; | |
| 1918 | |||
| 1919 | 3173 | fprintf(fp, "\n"); | |
| 1920 | } | ||
| 1921 | |||
| 1922 | static struct nftnl_set_elem * | ||
| 1923 | 683061 | alloc_nftnl_setelem_interval(const struct set *set, const struct expr *init, | |
| 1924 | struct expr *elem, struct expr *next_elem, | ||
| 1925 | struct nftnl_set_elem **nlse_high) | ||
| 1926 | { | ||
| 1927 | 683061 | struct nftnl_set_elem *nlse[2] = {}; | |
| 1928 | 683061 | LIST_HEAD(interval_list); | |
| 1929 | struct expr *expr, *next; | ||
| 1930 | 683061 | int i = 0; | |
| 1931 | |||
| 1932 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 683061 times.
|
683061 | if (setelem_to_interval(set, elem, next_elem, &interval_list) < 0) |
| 1933 | ✗ | memory_allocation_error(); | |
| 1934 | |||
| 1935 |
2/2✓ Branch 0 (6→7) taken 20 times.
✓ Branch 1 (6→9) taken 683041 times.
|
683061 | if (list_empty(&interval_list)) { |
| 1936 | 20 | *nlse_high = NULL; | |
| 1937 | 20 | nlse[i++] = alloc_nftnl_setelem(init, elem); | |
| 1938 | 20 | return nlse[0]; | |
| 1939 | } | ||
| 1940 | |||
| 1941 |
2/2✓ Branch 0 (14→10) taken 1361186 times.
✓ Branch 1 (14→15) taken 683041 times.
|
2044227 | list_for_each_entry_safe(expr, next, &interval_list, list) { |
| 1942 | 1361186 | nlse[i++] = alloc_nftnl_setelem(init, expr); | |
| 1943 | 1361186 | list_del(&expr->list); | |
| 1944 | 1361186 | expr_free(expr); | |
| 1945 | } | ||
| 1946 | 683041 | *nlse_high = nlse[1]; | |
| 1947 | |||
| 1948 | 683041 | return nlse[0]; | |
| 1949 | } | ||
| 1950 | |||
| 1951 | 30527 | static int mnl_nft_setelem_batch(const struct nftnl_set *nls, struct cmd *cmd, | |
| 1952 | struct nftnl_batch *batch, | ||
| 1953 | enum nf_tables_msg_types msg_type, | ||
| 1954 | unsigned int flags, uint32_t *seqnum, | ||
| 1955 | const struct set *set, const struct expr *init, | ||
| 1956 | struct netlink_ctx *ctx) | ||
| 1957 | { | ||
| 1958 | 30527 | struct nftnl_set_elem *nlse, *nlse_high = NULL; | |
| 1959 | struct nlattr *nest1, *nest2, *nest3; | ||
| 1960 | 30527 | struct expr *expr = NULL, *next; | |
| 1961 | 30527 | unsigned int nest_len = 0; | |
| 1962 | struct nlmsghdr *nlh; | ||
| 1963 | 30527 | int i = 0; | |
| 1964 | |||
| 1965 |
2/2✓ Branch 0 (2→3) taken 28448 times.
✓ Branch 1 (2→4) taken 2079 times.
|
30527 | if (msg_type == NFT_MSG_NEWSETELEM) |
| 1966 | 28448 | flags |= NLM_F_CREATE; | |
| 1967 | |||
| 1968 |
1/2✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→6) taken 30527 times.
|
30527 | if (init) |
| 1969 |
1/2✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→8) taken 30527 times.
|
30527 | expr = list_first_entry(&expr_set(init)->expressions, struct expr, list); |
| 1970 | |||
| 1971 | 30527 | next: | |
| 1972 | 31052 | nlh = nftnl_nlmsg_build_hdr(nftnl_batch_buffer(batch), msg_type, | |
| 1973 | 31052 | nftnl_set_get_u32(nls, NFTNL_SET_FAMILY), | |
| 1974 | flags, *seqnum); | ||
| 1975 | |||
| 1976 |
1/2✓ Branch 0 (14→15) taken 31052 times.
✗ Branch 1 (14→17) not taken.
|
31052 | if (nftnl_set_is_set(nls, NFTNL_SET_TABLE)) { |
| 1977 | 31052 | mnl_attr_put_strz(nlh, NFTA_SET_ELEM_LIST_TABLE, | |
| 1978 | nftnl_set_get_str(nls, NFTNL_SET_TABLE)); | ||
| 1979 | } | ||
| 1980 |
1/2✓ Branch 0 (18→19) taken 31052 times.
✗ Branch 1 (18→21) not taken.
|
31052 | if (nftnl_set_is_set(nls, NFTNL_SET_NAME)) { |
| 1981 | 31052 | mnl_attr_put_strz(nlh, NFTA_SET_ELEM_LIST_SET, | |
| 1982 | nftnl_set_get_str(nls, NFTNL_SET_NAME)); | ||
| 1983 | } | ||
| 1984 |
2/2✓ Branch 0 (22→23) taken 28943 times.
✓ Branch 1 (22→25) taken 2109 times.
|
31052 | if (nftnl_set_is_set(nls, NFTNL_SET_ID)) { |
| 1985 | 28943 | mnl_attr_put_u32(nlh, NFTA_SET_ELEM_LIST_SET_ID, | |
| 1986 | htonl(nftnl_set_get_u32(nls, NFTNL_SET_ID))); | ||
| 1987 | } | ||
| 1988 | |||
| 1989 |
3/6✓ Branch 0 (25→26) taken 31052 times.
✗ Branch 1 (25→30) not taken.
✗ Branch 2 (26→27) not taken.
✓ Branch 3 (26→28) taken 31052 times.
✗ Branch 4 (29→30) not taken.
✓ Branch 5 (29→31) taken 31052 times.
|
31052 | if (!init || list_empty(&expr_set(init)->expressions)) |
| 1990 | ✗ | return 0; | |
| 1991 | |||
| 1992 |
1/2✗ Branch 0 (31→32) not taken.
✓ Branch 1 (31→33) taken 31052 times.
|
31052 | assert(expr); |
| 1993 | 31052 | nest1 = mnl_attr_nest_start(nlh, NFTA_SET_ELEM_LIST_ELEMENTS); | |
| 1994 |
3/4✗ Branch 0 (67→68) not taken.
✓ Branch 1 (67→69) taken 1401225 times.
✓ Branch 2 (69→35) taken 1370698 times.
✓ Branch 3 (69→70) taken 30527 times.
|
1401225 | list_for_each_entry_from(expr, &expr_set(init)->expressions, list) { |
| 1995 | |||
| 1996 |
2/2✓ Branch 0 (36→37) taken 683061 times.
✓ Branch 1 (36→46) taken 687637 times.
|
1370698 | if (set_is_non_concat_range(set)) { |
| 1997 |
4/4✓ Branch 0 (38→39) taken 1461 times.
✓ Branch 1 (38→44) taken 681600 times.
✓ Branch 2 (42→43) taken 1014 times.
✓ Branch 3 (42→44) taken 447 times.
|
684522 | if (set_is_anonymous(set->flags) && |
| 1998 |
1/2✗ Branch 0 (39→40) not taken.
✓ Branch 1 (39→41) taken 1461 times.
|
1461 | !list_is_last(&expr->list, &expr_set(init)->expressions)) |
| 1999 | 1014 | next = list_next_entry(expr, list); | |
| 2000 | else | ||
| 2001 | 682047 | next = NULL; | |
| 2002 | |||
| 2003 | 683061 | nlse = alloc_nftnl_setelem_interval(set, init, expr, next, &nlse_high); | |
| 2004 | } else { | ||
| 2005 | 687637 | nlse = alloc_nftnl_setelem(init, expr); | |
| 2006 | } | ||
| 2007 | |||
| 2008 | 1370698 | cmd_add_loc(cmd, nlh, &expr->location); | |
| 2009 | |||
| 2010 | 1370698 | nest2 = mnl_attr_nest_start(nlh, ++i); | |
| 2011 | 1370698 | nftnl_set_elem_nlmsg_build_payload(nlh, nlse); | |
| 2012 | 1370698 | mnl_attr_nest_end(nlh, nest2); | |
| 2013 | |||
| 2014 | 1370698 | netlink_dump_setelem(nlse, ctx); | |
| 2015 | 1370698 | nftnl_set_elem_free(nlse); | |
| 2016 | |||
| 2017 | 1370698 | nest_len = nest2->nla_len; | |
| 2018 | |||
| 2019 |
2/2✓ Branch 0 (53→54) taken 678145 times.
✓ Branch 1 (53→60) taken 692553 times.
|
1370698 | if (nlse_high) { |
| 2020 | 678145 | nest3 = mnl_attr_nest_start(nlh, ++i); | |
| 2021 | 678145 | nftnl_set_elem_nlmsg_build_payload(nlh, nlse_high); | |
| 2022 | 678145 | mnl_attr_nest_end(nlh, nest3); | |
| 2023 | |||
| 2024 | 678145 | netlink_dump_setelem(nlse_high, ctx); | |
| 2025 | 678145 | nftnl_set_elem_free(nlse_high); | |
| 2026 | 678145 | nlse_high = NULL; | |
| 2027 | |||
| 2028 | 678145 | nest_len += nest3->nla_len; | |
| 2029 | } | ||
| 2030 | |||
| 2031 |
2/2✓ Branch 0 (61→62) taken 525 times.
✓ Branch 1 (61→66) taken 1370173 times.
|
1370698 | if (mnl_nft_attr_nest_overflow(nlh, nest1, nest2, nest_len)) { |
| 2032 | 525 | mnl_attr_nest_end(nlh, nest1); | |
| 2033 | 525 | mnl_nft_batch_continue(batch); | |
| 2034 | 525 | mnl_seqnum_inc(seqnum); | |
| 2035 | 525 | goto next; | |
| 2036 | } | ||
| 2037 | } | ||
| 2038 | 30527 | mnl_attr_nest_end(nlh, nest1); | |
| 2039 | 30527 | mnl_nft_batch_continue(batch); | |
| 2040 | 30527 | netlink_dump_setelem_done(ctx); | |
| 2041 | |||
| 2042 | 30527 | return 0; | |
| 2043 | } | ||
| 2044 | |||
| 2045 | 28448 | int mnl_nft_setelem_add(struct netlink_ctx *ctx, struct cmd *cmd, | |
| 2046 | const struct set *set, const struct expr *expr, | ||
| 2047 | unsigned int flags) | ||
| 2048 | { | ||
| 2049 | 28448 | const struct handle *h = &set->handle; | |
| 2050 | struct nftnl_set *nls; | ||
| 2051 | int err; | ||
| 2052 | |||
| 2053 | 28448 | nls = nftnl_set_alloc(); | |
| 2054 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 28448 times.
|
28448 | if (nls == NULL) |
| 2055 | ✗ | memory_allocation_error(); | |
| 2056 | |||
| 2057 | 28448 | nftnl_set_set_u32(nls, NFTNL_SET_FAMILY, h->family); | |
| 2058 | 28448 | nftnl_set_set_str(nls, NFTNL_SET_TABLE, h->table.name); | |
| 2059 | 28448 | nftnl_set_set_str(nls, NFTNL_SET_NAME, h->set.name); | |
| 2060 |
1/2✓ Branch 0 (8→9) taken 28448 times.
✗ Branch 1 (8→10) not taken.
|
28448 | if (h->set_id) |
| 2061 | 28448 | nftnl_set_set_u32(nls, NFTNL_SET_ID, h->set_id); | |
| 2062 |
2/2✓ Branch 0 (11→12) taken 15720 times.
✓ Branch 1 (11→14) taken 12728 times.
|
28448 | if (set_is_datamap(set->flags)) |
| 2063 | 15720 | nftnl_set_set_u32(nls, NFTNL_SET_DATA_TYPE, | |
| 2064 | 15720 | dtype_map_to_kernel(set->data->dtype)); | |
| 2065 | |||
| 2066 | 28448 | netlink_dump_set(nls, ctx); | |
| 2067 | |||
| 2068 | 28448 | err = mnl_nft_setelem_batch(nls, cmd, ctx->batch, NFT_MSG_NEWSETELEM, | |
| 2069 | flags, &ctx->seqnum, set, expr, ctx); | ||
| 2070 | 28448 | nftnl_set_free(nls); | |
| 2071 | |||
| 2072 | 28448 | return err; | |
| 2073 | } | ||
| 2074 | |||
| 2075 | 201 | int mnl_nft_setelem_flush(struct netlink_ctx *ctx, const struct cmd *cmd) | |
| 2076 | { | ||
| 2077 | 201 | const struct handle *h = &cmd->handle; | |
| 2078 | struct nftnl_set *nls; | ||
| 2079 | struct nlmsghdr *nlh; | ||
| 2080 | |||
| 2081 | 201 | nls = nftnl_set_alloc(); | |
| 2082 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 201 times.
|
201 | if (nls == NULL) |
| 2083 | ✗ | memory_allocation_error(); | |
| 2084 | |||
| 2085 | 201 | nftnl_set_set_u32(nls, NFTNL_SET_FAMILY, h->family); | |
| 2086 | 201 | nftnl_set_set_str(nls, NFTNL_SET_TABLE, h->table.name); | |
| 2087 | 201 | nftnl_set_set_str(nls, NFTNL_SET_NAME, h->set.name); | |
| 2088 |
1/2✗ Branch 0 (8→9) not taken.
✓ Branch 1 (8→10) taken 201 times.
|
201 | if (h->handle.id) |
| 2089 | ✗ | nftnl_set_set_u64(nls, NFTNL_SET_HANDLE, h->handle.id); | |
| 2090 | |||
| 2091 | 201 | netlink_dump_set(nls, ctx); | |
| 2092 | |||
| 2093 | 201 | nlh = nftnl_nlmsg_build_hdr(nftnl_batch_buffer(ctx->batch), | |
| 2094 | NFT_MSG_DELSETELEM, | ||
| 2095 | 201 | h->family, | |
| 2096 | 0, ctx->seqnum); | ||
| 2097 | 201 | nftnl_set_elems_nlmsg_build_payload(nlh, nls); | |
| 2098 | 201 | nftnl_set_free(nls); | |
| 2099 | |||
| 2100 | 201 | mnl_nft_batch_continue(ctx->batch); | |
| 2101 | |||
| 2102 | 201 | return 0; | |
| 2103 | } | ||
| 2104 | |||
| 2105 | 2079 | int mnl_nft_setelem_del(struct netlink_ctx *ctx, struct cmd *cmd, | |
| 2106 | const struct handle *h, const struct set *set, | ||
| 2107 | const struct expr *init) | ||
| 2108 | { | ||
| 2109 | 2079 | enum nf_tables_msg_types msg_type = NFT_MSG_DELSETELEM; | |
| 2110 | struct nftnl_set *nls; | ||
| 2111 | int err; | ||
| 2112 | |||
| 2113 | 2079 | nls = nftnl_set_alloc(); | |
| 2114 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 2079 times.
|
2079 | if (nls == NULL) |
| 2115 | ✗ | memory_allocation_error(); | |
| 2116 | |||
| 2117 | 2079 | nftnl_set_set_u32(nls, NFTNL_SET_FAMILY, h->family); | |
| 2118 | 2079 | nftnl_set_set_str(nls, NFTNL_SET_TABLE, h->table.name); | |
| 2119 |
1/2✓ Branch 0 (7→8) taken 2079 times.
✗ Branch 1 (7→9) not taken.
|
2079 | if (h->set.name) |
| 2120 | 2079 | nftnl_set_set_str(nls, NFTNL_SET_NAME, h->set.name); | |
| 2121 | ✗ | else if (h->handle.id) | |
| 2122 | ✗ | nftnl_set_set_u64(nls, NFTNL_SET_HANDLE, h->handle.id); | |
| 2123 | |||
| 2124 | 2079 | netlink_dump_set(nls, ctx); | |
| 2125 | |||
| 2126 |
1/2✗ Branch 0 (12→13) not taken.
✓ Branch 1 (12→14) taken 2079 times.
|
2079 | if (cmd->op == CMD_DESTROY) |
| 2127 | ✗ | msg_type = NFT_MSG_DESTROYSETELEM; | |
| 2128 | |||
| 2129 | 2079 | err = mnl_nft_setelem_batch(nls, cmd, ctx->batch, msg_type, 0, | |
| 2130 | &ctx->seqnum, set, init, ctx); | ||
| 2131 | 2079 | nftnl_set_free(nls); | |
| 2132 | |||
| 2133 | 2079 | return err; | |
| 2134 | } | ||
| 2135 | |||
| 2136 | 1885 | struct nftnl_set *mnl_nft_setelem_get_one(struct netlink_ctx *ctx, | |
| 2137 | struct nftnl_set *nls_in, | ||
| 2138 | bool reset) | ||
| 2139 | 1885 | { | |
| 2140 |
1/2✓ Branch 0 (3→4) taken 1885 times.
✗ Branch 1 (3→5) not taken.
|
1885 | char buf[MNL_SOCKET_BUFFER_SIZE]; |
| 2141 | struct nftnl_set *nls_out; | ||
| 2142 | struct nlmsghdr *nlh; | ||
| 2143 | int msg_type; | ||
| 2144 | int err; | ||
| 2145 | |||
| 2146 |
2/2✓ Branch 0 (6→7) taken 821 times.
✓ Branch 1 (6→8) taken 1064 times.
|
1885 | if (reset) |
| 2147 | 821 | msg_type = NFT_MSG_GETSETELEM_RESET; | |
| 2148 | else | ||
| 2149 | 1064 | msg_type = NFT_MSG_GETSETELEM; | |
| 2150 | |||
| 2151 | 1885 | nlh = nftnl_nlmsg_build_hdr(buf, msg_type, | |
| 2152 | 1885 | nftnl_set_get_u32(nls_in, NFTNL_SET_FAMILY), | |
| 2153 | NLM_F_ACK, ctx->seqnum); | ||
| 2154 | 1885 | nftnl_set_elems_nlmsg_build_payload(nlh, nls_in); | |
| 2155 | |||
| 2156 | 1885 | nls_out = nftnl_set_alloc(); | |
| 2157 |
1/2✗ Branch 0 (13→14) not taken.
✓ Branch 1 (13→15) taken 1885 times.
|
1885 | if (!nls_out) |
| 2158 | ✗ | return NULL; | |
| 2159 | |||
| 2160 | 1885 | nftnl_set_set_str(nls_out, NFTNL_SET_TABLE, | |
| 2161 | nftnl_set_get_str(nls_in, NFTNL_SET_TABLE)); | ||
| 2162 | 1885 | nftnl_set_set_str(nls_out, NFTNL_SET_NAME, | |
| 2163 | nftnl_set_get_str(nls_in, NFTNL_SET_NAME)); | ||
| 2164 | |||
| 2165 | 1885 | err = nft_mnl_talk(ctx, nlh, nlh->nlmsg_len, set_elem_cb, nls_out); | |
| 2166 |
2/2✓ Branch 0 (20→21) taken 19 times.
✓ Branch 1 (20→23) taken 1866 times.
|
1885 | if (err < 0) { |
| 2167 | 19 | nftnl_set_free(nls_out); | |
| 2168 | 19 | return NULL; | |
| 2169 | } | ||
| 2170 | |||
| 2171 | 1866 | return nls_out; | |
| 2172 | } | ||
| 2173 | |||
| 2174 | 12373 | int mnl_nft_setelem_get(struct netlink_ctx *ctx, struct nftnl_set *nls, | |
| 2175 | bool reset) | ||
| 2176 | 12373 | { | |
| 2177 |
1/2✓ Branch 0 (3→4) taken 12373 times.
✗ Branch 1 (3→5) not taken.
|
12373 | char buf[MNL_SOCKET_BUFFER_SIZE]; |
| 2178 | struct nlmsghdr *nlh; | ||
| 2179 | int msg_type; | ||
| 2180 | |||
| 2181 |
2/2✓ Branch 0 (6→7) taken 4 times.
✓ Branch 1 (6→8) taken 12369 times.
|
12373 | if (reset) |
| 2182 | 4 | msg_type = NFT_MSG_GETSETELEM_RESET; | |
| 2183 | else | ||
| 2184 | 12369 | msg_type = NFT_MSG_GETSETELEM; | |
| 2185 | |||
| 2186 | 12373 | nlh = nftnl_nlmsg_build_hdr(buf, msg_type, | |
| 2187 | 12373 | nftnl_set_get_u32(nls, NFTNL_SET_FAMILY), | |
| 2188 | NLM_F_DUMP, ctx->seqnum); | ||
| 2189 | 12373 | nftnl_set_elems_nlmsg_build_payload(nlh, nls); | |
| 2190 | |||
| 2191 | 12373 | return nft_mnl_talk(ctx, nlh, nlh->nlmsg_len, set_elem_cb, nls); | |
| 2192 | } | ||
| 2193 | |||
| 2194 | 47 | static int flowtable_cb(const struct nlmsghdr *nlh, void *data) | |
| 2195 | { | ||
| 2196 | 47 | struct nftnl_flowtable_list *nln_list = data; | |
| 2197 | struct nftnl_flowtable *n; | ||
| 2198 | |||
| 2199 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 47 times.
|
47 | if (check_genid(nlh) < 0) |
| 2200 | ✗ | return MNL_CB_ERROR; | |
| 2201 | |||
| 2202 | 47 | n = nftnl_flowtable_alloc(); | |
| 2203 |
1/2✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→8) taken 47 times.
|
47 | if (n == NULL) |
| 2204 | ✗ | memory_allocation_error(); | |
| 2205 | |||
| 2206 |
1/2✗ Branch 0 (9→10) not taken.
✓ Branch 1 (9→11) taken 47 times.
|
47 | if (nftnl_flowtable_nlmsg_parse(nlh, n) < 0) |
| 2207 | ✗ | goto err_free; | |
| 2208 | |||
| 2209 | 47 | nftnl_flowtable_list_add_tail(n, nln_list); | |
| 2210 | 47 | return MNL_CB_OK; | |
| 2211 | |||
| 2212 | ✗ | err_free: | |
| 2213 | ✗ | nftnl_flowtable_free(n); | |
| 2214 | ✗ | return MNL_CB_OK; | |
| 2215 | } | ||
| 2216 | |||
| 2217 | struct nftnl_flowtable_list * | ||
| 2218 | 15902 | mnl_nft_flowtable_dump(struct netlink_ctx *ctx, int family, | |
| 2219 | const char *table, const char *ft) | ||
| 2220 | 15902 | { | |
| 2221 | struct nftnl_flowtable_list *nln_list; | ||
| 2222 |
1/2✓ Branch 0 (3→4) taken 15902 times.
✗ Branch 1 (3→5) not taken.
|
15902 | char buf[MNL_SOCKET_BUFFER_SIZE]; |
| 2223 | struct nftnl_flowtable *n; | ||
| 2224 | 15902 | int flags = NLM_F_DUMP; | |
| 2225 | struct nlmsghdr *nlh; | ||
| 2226 | int ret; | ||
| 2227 | |||
| 2228 | 15902 | n = nftnl_flowtable_alloc(); | |
| 2229 |
1/2✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→9) taken 15902 times.
|
15902 | if (n == NULL) |
| 2230 | ✗ | memory_allocation_error(); | |
| 2231 | |||
| 2232 |
2/2✓ Branch 0 (9→10) taken 13887 times.
✓ Branch 1 (9→11) taken 2015 times.
|
15902 | if (table != NULL) |
| 2233 | 13887 | nftnl_flowtable_set_str(n, NFTNL_FLOWTABLE_TABLE, table); | |
| 2234 |
2/2✓ Branch 0 (11→12) taken 2 times.
✓ Branch 1 (11→14) taken 15900 times.
|
15902 | if (ft) { |
| 2235 | 2 | nftnl_flowtable_set_str(n, NFTNL_FLOWTABLE_NAME, ft); | |
| 2236 | 2 | flags = NLM_F_ACK; | |
| 2237 | } | ||
| 2238 | 15902 | nlh = nftnl_nlmsg_build_hdr(buf, NFT_MSG_GETFLOWTABLE, family, | |
| 2239 | flags, ctx->seqnum); | ||
| 2240 | 15902 | nftnl_flowtable_nlmsg_build_payload(nlh, n); | |
| 2241 | 15902 | nftnl_flowtable_free(n); | |
| 2242 | |||
| 2243 | 15902 | nln_list = nftnl_flowtable_list_alloc(); | |
| 2244 |
1/2✗ Branch 0 (18→19) not taken.
✓ Branch 1 (18→20) taken 15902 times.
|
15902 | if (nln_list == NULL) |
| 2245 | ✗ | memory_allocation_error(); | |
| 2246 | |||
| 2247 | 15902 | ret = nft_mnl_talk(ctx, nlh, nlh->nlmsg_len, flowtable_cb, nln_list); | |
| 2248 |
1/4✗ Branch 0 (21→22) not taken.
✓ Branch 1 (21→24) taken 15902 times.
✗ Branch 2 (22→23) not taken.
✗ Branch 3 (22→24) not taken.
|
15902 | if (ret < 0 && errno != ENOENT) |
| 2249 | ✗ | goto err; | |
| 2250 | |||
| 2251 | 15902 | return nln_list; | |
| 2252 | ✗ | err: | |
| 2253 | ✗ | nftnl_flowtable_list_free(nln_list); | |
| 2254 | ✗ | return NULL; | |
| 2255 | } | ||
| 2256 | |||
| 2257 | 98 | static void mnl_nft_ft_devs_build(struct nlmsghdr *nlh, struct cmd *cmd) | |
| 2258 | { | ||
| 2259 | 98 | const struct expr *dev_expr = cmd->flowtable->dev_expr; | |
| 2260 | const struct nft_dev *dev_array; | ||
| 2261 | struct nlattr *nest_dev; | ||
| 2262 | 98 | int i, num_devs= 0; | |
| 2263 | |||
| 2264 | 98 | dev_array = nft_dev_array(dev_expr, &num_devs); | |
| 2265 | 98 | nest_dev = mnl_attr_nest_start(nlh, NFTA_FLOWTABLE_HOOK_DEVS); | |
| 2266 |
2/2✓ Branch 0 (8→5) taken 113 times.
✓ Branch 1 (8→9) taken 98 times.
|
211 | for (i = 0; i < num_devs; i++) { |
| 2267 | 113 | cmd_add_loc(cmd, nlh, dev_array[i].location); | |
| 2268 | 113 | mnl_nft_attr_put_ifname(nlh, dev_array[i].ifname); | |
| 2269 | } | ||
| 2270 | |||
| 2271 | 98 | mnl_attr_nest_end(nlh, nest_dev); | |
| 2272 | 98 | nft_dev_array_free(dev_array); | |
| 2273 | 98 | } | |
| 2274 | |||
| 2275 | 93 | int mnl_nft_flowtable_add(struct netlink_ctx *ctx, struct cmd *cmd, | |
| 2276 | unsigned int flags) | ||
| 2277 | { | ||
| 2278 | struct nftnl_flowtable *flo; | ||
| 2279 | struct nlmsghdr *nlh; | ||
| 2280 | struct nlattr *nest; | ||
| 2281 | int priority; | ||
| 2282 | |||
| 2283 | 93 | flo = nftnl_flowtable_alloc(); | |
| 2284 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 93 times.
|
93 | if (!flo) |
| 2285 | ✗ | memory_allocation_error(); | |
| 2286 | |||
| 2287 | 93 | nftnl_flowtable_set_u32(flo, NFTNL_FLOWTABLE_FAMILY, | |
| 2288 | cmd->handle.family); | ||
| 2289 | |||
| 2290 | 93 | nftnl_flowtable_set_u32(flo, NFTNL_FLOWTABLE_FLAGS, | |
| 2291 | 93 | cmd->flowtable->flags); | |
| 2292 | |||
| 2293 | 93 | netlink_dump_flowtable(flo, ctx); | |
| 2294 | |||
| 2295 | 93 | nlh = nftnl_nlmsg_build_hdr(nftnl_batch_buffer(ctx->batch), | |
| 2296 | 93 | NFT_MSG_NEWFLOWTABLE, cmd->handle.family, | |
| 2297 | 93 | NLM_F_CREATE | flags, ctx->seqnum); | |
| 2298 | |||
| 2299 | 93 | cmd_add_loc(cmd, nlh, &cmd->handle.table.location); | |
| 2300 | 93 | mnl_attr_put_strz(nlh, NFTA_FLOWTABLE_TABLE, cmd->handle.table.name); | |
| 2301 | 93 | cmd_add_loc(cmd, nlh, &cmd->handle.flowtable.location); | |
| 2302 | 93 | mnl_attr_put_strz(nlh, NFTA_FLOWTABLE_NAME, cmd->handle.flowtable.name); | |
| 2303 | |||
| 2304 | 93 | nftnl_flowtable_nlmsg_build_payload(nlh, flo); | |
| 2305 | |||
| 2306 | 93 | nest = mnl_attr_nest_start(nlh, NFTA_FLOWTABLE_HOOK); | |
| 2307 | |||
| 2308 |
3/4✓ Branch 0 (16→17) taken 93 times.
✗ Branch 1 (16→21) not taken.
✓ Branch 2 (17→18) taken 90 times.
✓ Branch 3 (17→21) taken 3 times.
|
93 | if (cmd->flowtable && cmd->flowtable->priority.expr) { |
| 2309 | 90 | mnl_attr_put_u32(nlh, NFTA_FLOWTABLE_HOOK_NUM, htonl(cmd->flowtable->hook.num)); | |
| 2310 | 90 | mpz_export_data(&priority, cmd->flowtable->priority.expr->value, | |
| 2311 | BYTEORDER_HOST_ENDIAN, sizeof(int)); | ||
| 2312 | 90 | mnl_attr_put_u32(nlh, NFTA_FLOWTABLE_HOOK_PRIORITY, htonl(priority)); | |
| 2313 | } | ||
| 2314 | |||
| 2315 |
2/2✓ Branch 0 (21→22) taken 86 times.
✓ Branch 1 (21→23) taken 7 times.
|
93 | if (cmd->flowtable->dev_expr) |
| 2316 | 86 | mnl_nft_ft_devs_build(nlh, cmd); | |
| 2317 | |||
| 2318 | 93 | mnl_attr_nest_end(nlh, nest); | |
| 2319 | |||
| 2320 | 93 | nftnl_flowtable_free(flo); | |
| 2321 | |||
| 2322 | 93 | mnl_nft_batch_continue(ctx->batch); | |
| 2323 | |||
| 2324 | 93 | return 0; | |
| 2325 | } | ||
| 2326 | |||
| 2327 | 36 | int mnl_nft_flowtable_del(struct netlink_ctx *ctx, struct cmd *cmd) | |
| 2328 | { | ||
| 2329 | 36 | enum nf_tables_msg_types msg_type = NFT_MSG_DELFLOWTABLE; | |
| 2330 | struct nftnl_flowtable *flo; | ||
| 2331 | struct nlmsghdr *nlh; | ||
| 2332 | struct nlattr *nest; | ||
| 2333 | |||
| 2334 | 36 | flo = nftnl_flowtable_alloc(); | |
| 2335 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 36 times.
|
36 | if (!flo) |
| 2336 | ✗ | memory_allocation_error(); | |
| 2337 | |||
| 2338 | 36 | nftnl_flowtable_set_u32(flo, NFTNL_FLOWTABLE_FAMILY, | |
| 2339 | cmd->handle.family); | ||
| 2340 | |||
| 2341 |
2/2✓ Branch 0 (6→7) taken 2 times.
✓ Branch 1 (6→8) taken 34 times.
|
36 | if (cmd->op == CMD_DESTROY) |
| 2342 | 2 | msg_type = NFT_MSG_DESTROYFLOWTABLE; | |
| 2343 | |||
| 2344 | 36 | nlh = nftnl_nlmsg_build_hdr(nftnl_batch_buffer(ctx->batch), | |
| 2345 | 36 | msg_type, cmd->handle.family, | |
| 2346 | 0, ctx->seqnum); | ||
| 2347 | |||
| 2348 | 36 | cmd_add_loc(cmd, nlh, &cmd->handle.table.location); | |
| 2349 | 36 | mnl_attr_put_strz(nlh, NFTA_FLOWTABLE_TABLE, cmd->handle.table.name); | |
| 2350 | |||
| 2351 |
2/2✓ Branch 0 (12→13) taken 33 times.
✓ Branch 1 (12→15) taken 3 times.
|
36 | if (cmd->handle.flowtable.name) { |
| 2352 | 33 | cmd_add_loc(cmd, nlh, &cmd->handle.flowtable.location); | |
| 2353 | 33 | mnl_attr_put_strz(nlh, NFTA_FLOWTABLE_NAME, | |
| 2354 | cmd->handle.flowtable.name); | ||
| 2355 |
1/2✓ Branch 0 (15→16) taken 3 times.
✗ Branch 1 (15→19) not taken.
|
3 | } else if (cmd->handle.handle.id) { |
| 2356 | 3 | cmd_add_loc(cmd, nlh, &cmd->handle.handle.location); | |
| 2357 | 3 | mnl_attr_put_u64(nlh, NFTA_FLOWTABLE_HANDLE, | |
| 2358 | 3 | htobe64(cmd->handle.handle.id)); | |
| 2359 | } | ||
| 2360 | |||
| 2361 | 36 | nftnl_flowtable_nlmsg_build_payload(nlh, flo); | |
| 2362 | |||
| 2363 |
2/2✓ Branch 0 (20→21) taken 34 times.
✓ Branch 1 (20→26) taken 2 times.
|
36 | if (cmd->op == CMD_DELETE && |
| 2364 |
3/4✓ Branch 0 (21→22) taken 12 times.
✓ Branch 1 (21→26) taken 22 times.
✓ Branch 2 (22→23) taken 12 times.
✗ Branch 3 (22→26) not taken.
|
34 | cmd->flowtable && cmd->flowtable->dev_expr) { |
| 2365 | 12 | nest = mnl_attr_nest_start(nlh, NFTA_FLOWTABLE_HOOK); | |
| 2366 | 12 | mnl_nft_ft_devs_build(nlh, cmd); | |
| 2367 | 12 | mnl_attr_nest_end(nlh, nest); | |
| 2368 | } | ||
| 2369 | |||
| 2370 | 36 | nftnl_flowtable_free(flo); | |
| 2371 | |||
| 2372 | 36 | mnl_nft_batch_continue(ctx->batch); | |
| 2373 | |||
| 2374 | 36 | return 0; | |
| 2375 | } | ||
| 2376 | |||
| 2377 | /* | ||
| 2378 | * events | ||
| 2379 | */ | ||
| 2380 | #define NFTABLES_NLEVENT_BUFSIZ (1 << 24) | ||
| 2381 | |||
| 2382 | ✗ | int mnl_nft_event_listener(struct mnl_socket *nf_sock, unsigned int debug_mask, | |
| 2383 | struct output_ctx *octx, | ||
| 2384 | int (*cb)(const struct nlmsghdr *nlh, void *data), | ||
| 2385 | void *cb_data) | ||
| 2386 | ✗ | { | |
| 2387 | /* Set netlink socket buffer size to 16 Mbytes to reduce chances of | ||
| 2388 | * message loss due to ENOBUFS. | ||
| 2389 | */ | ||
| 2390 | ✗ | unsigned int bufsiz = NFTABLES_NLEVENT_BUFSIZ; | |
| 2391 | ✗ | int fd = mnl_socket_get_fd(nf_sock); | |
| 2392 | ✗ | char buf[NFT_NLMSG_MAXSIZE]; | |
| 2393 | fd_set readfds; | ||
| 2394 | int ret; | ||
| 2395 | |||
| 2396 | ✗ | ret = mnl_set_rcvbuffer(nf_sock, bufsiz); | |
| 2397 | ✗ | if (ret < 0) | |
| 2398 | ✗ | nft_print(octx, "# Cannot set up netlink receive socket buffer size to %u bytes, falling back to %u bytes\n", | |
| 2399 | NFTABLES_NLEVENT_BUFSIZ, bufsiz); | ||
| 2400 | |||
| 2401 | while (1) { | ||
| 2402 | ✗ | FD_ZERO(&readfds); | |
| 2403 | ✗ | FD_SET(fd, &readfds); | |
| 2404 | |||
| 2405 | ✗ | ret = select(fd + 1, &readfds, NULL, NULL, NULL); | |
| 2406 | ✗ | if (ret < 0) | |
| 2407 | ✗ | return -1; | |
| 2408 | |||
| 2409 | ✗ | if (FD_ISSET(fd, &readfds)) { | |
| 2410 | ✗ | ret = mnl_socket_recvfrom(nf_sock, buf, sizeof(buf)); | |
| 2411 | ✗ | if (ret < 0) { | |
| 2412 | ✗ | if (errno == ENOBUFS) { | |
| 2413 | ✗ | nft_print(octx, "# ERROR: We lost some netlink events!\n"); | |
| 2414 | ✗ | continue; | |
| 2415 | } | ||
| 2416 | ✗ | nft_print(octx, "# ERROR: %s\n", | |
| 2417 | ✗ | strerror(errno)); | |
| 2418 | ✗ | break; | |
| 2419 | } | ||
| 2420 | } | ||
| 2421 | |||
| 2422 | ✗ | if (debug_mask & NFT_DEBUG_MNL) { | |
| 2423 | ✗ | mnl_nlmsg_fprintf(octx->output_fp, buf, sizeof(buf), | |
| 2424 | sizeof(struct nfgenmsg)); | ||
| 2425 | } | ||
| 2426 | ✗ | ret = mnl_cb_run(buf, ret, 0, 0, cb, cb_data); | |
| 2427 | ✗ | if (ret <= 0) | |
| 2428 | ✗ | break; | |
| 2429 | } | ||
| 2430 | ✗ | return ret; | |
| 2431 | } | ||
| 2432 | |||
| 2433 | 263 | static struct basehook *basehook_alloc(void) | |
| 2434 | { | ||
| 2435 | 263 | return xzalloc(sizeof(struct basehook)); | |
| 2436 | } | ||
| 2437 | |||
| 2438 | 263 | static void basehook_free(struct basehook *b) | |
| 2439 | { | ||
| 2440 | 263 | list_del(&b->list); | |
| 2441 | 263 | free_const(b->module_name); | |
| 2442 | 263 | free_const(b->hookfn); | |
| 2443 | 263 | free_const(b->chain); | |
| 2444 | 263 | free_const(b->table); | |
| 2445 | 263 | free_const(b->devname); | |
| 2446 | 263 | free(b); | |
| 2447 | 263 | } | |
| 2448 | |||
| 2449 | 10384 | static bool basehook_eq(const struct basehook *prev, const struct basehook *hook) | |
| 2450 | { | ||
| 2451 |
2/2✓ Branch 0 (2→3) taken 80 times.
✓ Branch 1 (2→4) taken 10304 times.
|
10384 | if (prev->num != hook->num) |
| 2452 | 80 | return false; | |
| 2453 | |||
| 2454 |
2/4✓ Branch 0 (4→5) taken 10304 times.
✗ Branch 1 (4→7) not taken.
✓ Branch 2 (5→6) taken 10304 times.
✗ Branch 3 (5→7) not taken.
|
10304 | if (prev->devname != NULL && hook->devname != NULL) |
| 2455 | 10304 | return strcmp(prev->devname, hook->devname) == 0; | |
| 2456 | |||
| 2457 | ✗ | if (prev->devname == NULL && hook->devname == NULL) | |
| 2458 | ✗ | return true; | |
| 2459 | |||
| 2460 | ✗ | return false; | |
| 2461 | } | ||
| 2462 | |||
| 2463 | 263 | static void basehook_list_add_tail(struct basehook *b, struct list_head *head) | |
| 2464 | { | ||
| 2465 | struct basehook *hook; | ||
| 2466 | |||
| 2467 |
2/2✓ Branch 0 (13→3) taken 10150 times.
✓ Branch 1 (13→14) taken 263 times.
|
10413 | list_for_each_entry(hook, head, list) { |
| 2468 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 10150 times.
|
10150 | if (hook->family != b->family) |
| 2469 | ✗ | continue; | |
| 2470 |
1/2✓ Branch 0 (6→7) taken 10150 times.
✗ Branch 1 (6→8) not taken.
|
10150 | if (!basehook_eq(hook, b)) |
| 2471 | 10150 | continue; | |
| 2472 | ✗ | if (hook->prio < b->prio) | |
| 2473 | ✗ | continue; | |
| 2474 | |||
| 2475 | ✗ | list_add(&b->list, &hook->list); | |
| 2476 | ✗ | return; | |
| 2477 | } | ||
| 2478 | |||
| 2479 | 263 | list_add_tail(&b->list, head); | |
| 2480 | } | ||
| 2481 | |||
| 2482 | 1219 | static int dump_nf_attr_cb(const struct nlattr *attr, void *data) | |
| 2483 | { | ||
| 2484 | 1219 | int type = mnl_attr_get_type(attr); | |
| 2485 | 1219 | const struct nlattr **tb = data; | |
| 2486 | |||
| 2487 |
1/2✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→6) taken 1219 times.
|
1219 | if (mnl_attr_type_valid(attr, NFNLA_HOOK_MAX) < 0) |
| 2488 | ✗ | return MNL_CB_OK; | |
| 2489 | |||
| 2490 |
3/5✓ Branch 0 (6→7) taken 526 times.
✗ Branch 1 (6→11) not taken.
✓ Branch 2 (6→15) taken 478 times.
✓ Branch 3 (6→19) taken 215 times.
✗ Branch 4 (6→23) not taken.
|
1219 | switch(type) { |
| 2491 | 526 | case NFNLA_HOOK_HOOKNUM: | |
| 2492 | case NFNLA_HOOK_PRIORITY: | ||
| 2493 |
1/2✗ Branch 0 (8→9) not taken.
✓ Branch 1 (8→10) taken 526 times.
|
526 | if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) |
| 2494 | ✗ | return MNL_CB_ERROR; | |
| 2495 | 526 | break; | |
| 2496 | ✗ | case NFNLA_HOOK_DEV: | |
| 2497 | ✗ | if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0) | |
| 2498 | ✗ | return MNL_CB_ERROR; | |
| 2499 | ✗ | break; | |
| 2500 | 478 | case NFNLA_HOOK_MODULE_NAME: | |
| 2501 | case NFNLA_HOOK_FUNCTION_NAME: | ||
| 2502 |
1/2✗ Branch 0 (16→17) not taken.
✓ Branch 1 (16→18) taken 478 times.
|
478 | if (mnl_attr_validate(attr, MNL_TYPE_NUL_STRING) < 0) |
| 2503 | ✗ | return MNL_CB_ERROR; | |
| 2504 | 478 | break; | |
| 2505 | 215 | case NFNLA_HOOK_CHAIN_INFO: | |
| 2506 |
1/2✗ Branch 0 (20→21) not taken.
✓ Branch 1 (20→22) taken 215 times.
|
215 | if (mnl_attr_validate(attr, MNL_TYPE_NESTED) < 0) |
| 2507 | ✗ | return MNL_CB_ERROR; | |
| 2508 | 215 | break; | |
| 2509 | ✗ | default: | |
| 2510 | ✗ | return MNL_CB_OK; | |
| 2511 | } | ||
| 2512 | |||
| 2513 | 1219 | tb[type] = attr; | |
| 2514 | 1219 | return MNL_CB_OK; | |
| 2515 | } | ||
| 2516 | |||
| 2517 | 430 | static int dump_nf_chain_info_cb(const struct nlattr *attr, void *data) | |
| 2518 | { | ||
| 2519 | 430 | int type = mnl_attr_get_type(attr); | |
| 2520 | 430 | const struct nlattr **tb = data; | |
| 2521 | |||
| 2522 |
1/2✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→6) taken 430 times.
|
430 | if (mnl_attr_type_valid(attr, NFNLA_HOOK_INFO_MAX) < 0) |
| 2523 | ✗ | return MNL_CB_OK; | |
| 2524 | |||
| 2525 |
2/3✓ Branch 0 (6→7) taken 215 times.
✓ Branch 1 (6→11) taken 215 times.
✗ Branch 2 (6→15) not taken.
|
430 | switch(type) { |
| 2526 | 215 | case NFNLA_HOOK_INFO_DESC: | |
| 2527 |
1/2✗ Branch 0 (8→9) not taken.
✓ Branch 1 (8→10) taken 215 times.
|
215 | if (mnl_attr_validate(attr, MNL_TYPE_NESTED) < 0) |
| 2528 | ✗ | return MNL_CB_ERROR; | |
| 2529 | 215 | break; | |
| 2530 | 215 | case NFNLA_HOOK_INFO_TYPE: | |
| 2531 |
1/2✗ Branch 0 (12→13) not taken.
✓ Branch 1 (12→14) taken 215 times.
|
215 | if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) |
| 2532 | ✗ | return MNL_CB_ERROR; | |
| 2533 | 215 | break; | |
| 2534 | ✗ | default: | |
| 2535 | ✗ | return MNL_CB_OK; | |
| 2536 | } | ||
| 2537 | |||
| 2538 | 430 | tb[type] = attr; | |
| 2539 | 430 | return MNL_CB_OK; | |
| 2540 | } | ||
| 2541 | |||
| 2542 | 645 | static int dump_nf_attr_chain_cb(const struct nlattr *attr, void *data) | |
| 2543 | { | ||
| 2544 | 645 | int type = mnl_attr_get_type(attr); | |
| 2545 | 645 | const struct nlattr **tb = data; | |
| 2546 | |||
| 2547 |
1/2✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→6) taken 645 times.
|
645 | if (mnl_attr_type_valid(attr, NFNLA_CHAIN_MAX) < 0) |
| 2548 | ✗ | return MNL_CB_OK; | |
| 2549 | |||
| 2550 |
2/3✓ Branch 0 (6→7) taken 430 times.
✓ Branch 1 (6→11) taken 215 times.
✗ Branch 2 (6→15) not taken.
|
645 | switch(type) { |
| 2551 | 430 | case NFNLA_CHAIN_TABLE: | |
| 2552 | case NFNLA_CHAIN_NAME: | ||
| 2553 |
1/2✗ Branch 0 (8→9) not taken.
✓ Branch 1 (8→10) taken 430 times.
|
430 | if (mnl_attr_validate(attr, MNL_TYPE_NUL_STRING) < 0) |
| 2554 | ✗ | return MNL_CB_ERROR; | |
| 2555 | 430 | break; | |
| 2556 | 215 | case NFNLA_CHAIN_FAMILY: | |
| 2557 |
1/2✗ Branch 0 (12→13) not taken.
✓ Branch 1 (12→14) taken 215 times.
|
215 | if (mnl_attr_validate(attr, MNL_TYPE_U8) < 0) |
| 2558 | ✗ | return MNL_CB_ERROR; | |
| 2559 | 215 | break; | |
| 2560 | ✗ | default: | |
| 2561 | ✗ | return MNL_CB_OK; | |
| 2562 | } | ||
| 2563 | |||
| 2564 | 645 | tb[type] = attr; | |
| 2565 | 645 | return MNL_CB_OK; | |
| 2566 | } | ||
| 2567 | |||
| 2568 | ✗ | static int dump_nf_attr_bpf_cb(const struct nlattr *attr, void *data) | |
| 2569 | { | ||
| 2570 | ✗ | int type = mnl_attr_get_type(attr); | |
| 2571 | ✗ | const struct nlattr **tb = data; | |
| 2572 | |||
| 2573 | ✗ | if (mnl_attr_type_valid(attr, NFNLA_HOOK_BPF_MAX) < 0) | |
| 2574 | ✗ | return MNL_CB_OK; | |
| 2575 | |||
| 2576 | ✗ | switch(type) { | |
| 2577 | ✗ | case NFNLA_HOOK_BPF_ID: | |
| 2578 | ✗ | if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) | |
| 2579 | ✗ | return MNL_CB_ERROR; | |
| 2580 | ✗ | break; | |
| 2581 | ✗ | default: | |
| 2582 | ✗ | return MNL_CB_OK; | |
| 2583 | } | ||
| 2584 | |||
| 2585 | ✗ | tb[type] = attr; | |
| 2586 | ✗ | return MNL_CB_OK; | |
| 2587 | } | ||
| 2588 | |||
| 2589 | struct dump_nf_hook_data { | ||
| 2590 | struct list_head *hook_list; | ||
| 2591 | const char *devname; | ||
| 2592 | int family; | ||
| 2593 | }; | ||
| 2594 | |||
| 2595 | 263 | static int dump_nf_hooks(const struct nlmsghdr *nlh, void *_data) | |
| 2596 | { | ||
| 2597 | 263 | const struct nfgenmsg *nfg = mnl_nlmsg_get_payload(nlh); | |
| 2598 | 263 | struct nlattr *tb[NFNLA_HOOK_MAX + 1] = {}; | |
| 2599 | 263 | struct dump_nf_hook_data *data = _data; | |
| 2600 | struct basehook *hook; | ||
| 2601 | |||
| 2602 | /* NB: Don't check the nft generation ID, this is not | ||
| 2603 | * an nftables subsystem. | ||
| 2604 | */ | ||
| 2605 |
1/2✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→6) taken 263 times.
|
263 | if (mnl_attr_parse(nlh, sizeof(*nfg), dump_nf_attr_cb, tb) < 0) |
| 2606 | ✗ | return -1; | |
| 2607 | |||
| 2608 |
1/2✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→9) taken 263 times.
|
263 | if (!tb[NFNLA_HOOK_PRIORITY]) |
| 2609 | ✗ | netlink_abi_error(); | |
| 2610 | |||
| 2611 | 263 | hook = basehook_alloc(); | |
| 2612 | 263 | hook->prio = ntohl(mnl_attr_get_u32(tb[NFNLA_HOOK_PRIORITY])); | |
| 2613 |
2/2✓ Branch 0 (11→12) taken 215 times.
✓ Branch 1 (11→13) taken 48 times.
|
263 | hook->devname = data->devname ? xstrdup(data->devname) : NULL; |
| 2614 | |||
| 2615 |
1/2✓ Branch 0 (14→15) taken 263 times.
✗ Branch 1 (14→18) not taken.
|
263 | if (tb[NFNLA_HOOK_FUNCTION_NAME]) |
| 2616 | 263 | hook->hookfn = xstrdup(mnl_attr_get_str(tb[NFNLA_HOOK_FUNCTION_NAME])); | |
| 2617 | |||
| 2618 |
2/2✓ Branch 0 (18→19) taken 215 times.
✓ Branch 1 (18→22) taken 48 times.
|
263 | if (tb[NFNLA_HOOK_MODULE_NAME]) |
| 2619 | 215 | hook->module_name = xstrdup(mnl_attr_get_str(tb[NFNLA_HOOK_MODULE_NAME])); | |
| 2620 | |||
| 2621 |
2/2✓ Branch 0 (22→23) taken 215 times.
✓ Branch 1 (22→58) taken 48 times.
|
263 | if (tb[NFNLA_HOOK_CHAIN_INFO]) { |
| 2622 | 215 | struct nlattr *nested[NFNLA_HOOK_INFO_MAX + 1] = {}; | |
| 2623 | uint32_t type; | ||
| 2624 | |||
| 2625 |
1/2✗ Branch 0 (24→25) not taken.
✓ Branch 1 (24→27) taken 215 times.
|
215 | if (mnl_attr_parse_nested(tb[NFNLA_HOOK_CHAIN_INFO], |
| 2626 | dump_nf_chain_info_cb, nested) < 0) { | ||
| 2627 | ✗ | basehook_free(hook); | |
| 2628 | ✗ | return -1; | |
| 2629 | } | ||
| 2630 | |||
| 2631 | 215 | type = ntohl(mnl_attr_get_u32(nested[NFNLA_HOOK_INFO_TYPE])); | |
| 2632 |
3/4✓ Branch 0 (28→29) taken 108 times.
✓ Branch 1 (28→30) taken 107 times.
✓ Branch 2 (29→30) taken 108 times.
✗ Branch 3 (29→46) not taken.
|
215 | if (type == NFNL_HOOK_TYPE_NFTABLES || |
| 2633 | 215 | type == NFNL_HOOK_TYPE_NFT_FLOWTABLE) { | |
| 2634 | 215 | struct nlattr *info[NFNLA_CHAIN_MAX + 1] = {}; | |
| 2635 | const char *tablename, *chainname; | ||
| 2636 | |||
| 2637 |
1/2✗ Branch 0 (31→32) not taken.
✓ Branch 1 (31→34) taken 215 times.
|
215 | if (mnl_attr_parse_nested(nested[NFNLA_HOOK_INFO_DESC], |
| 2638 | dump_nf_attr_chain_cb, | ||
| 2639 | info) < 0) { | ||
| 2640 | ✗ | basehook_free(hook); | |
| 2641 | ✗ | return -1; | |
| 2642 | } | ||
| 2643 | |||
| 2644 | 215 | tablename = mnl_attr_get_str(info[NFNLA_CHAIN_TABLE]); | |
| 2645 | 215 | chainname = mnl_attr_get_str(info[NFNLA_CHAIN_NAME]); | |
| 2646 |
2/4✓ Branch 0 (36→37) taken 215 times.
✗ Branch 1 (36→41) not taken.
✓ Branch 2 (37→38) taken 215 times.
✗ Branch 3 (37→41) not taken.
|
215 | if (tablename && chainname) { |
| 2647 | 215 | hook->table = xstrdup(tablename); | |
| 2648 | 215 | hook->chain = xstrdup(chainname); | |
| 2649 | } | ||
| 2650 | 215 | hook->chain_family = mnl_attr_get_u8(info[NFNLA_CHAIN_FAMILY]); | |
| 2651 |
2/2✓ Branch 0 (42→43) taken 108 times.
✓ Branch 1 (42→44) taken 107 times.
|
215 | if (type == NFNL_HOOK_TYPE_NFT_FLOWTABLE) |
| 2652 | 108 | hook->objtype = "flowtable"; | |
| 2653 | else | ||
| 2654 | 107 | hook->objtype = "chain"; | |
| 2655 | ✗ | } else if (type == NFNL_HOOK_TYPE_BPF) { | |
| 2656 | ✗ | struct nlattr *info[NFNLA_HOOK_BPF_MAX + 1] = {}; | |
| 2657 | |||
| 2658 | ✗ | if (mnl_attr_parse_nested(nested[NFNLA_HOOK_INFO_DESC], | |
| 2659 | dump_nf_attr_bpf_cb, info) < 0) { | ||
| 2660 | ✗ | basehook_free(hook); | |
| 2661 | ✗ | return -1; | |
| 2662 | } | ||
| 2663 | |||
| 2664 | ✗ | if (info[NFNLA_HOOK_BPF_ID]) { | |
| 2665 | char tmpbuf[16]; | ||
| 2666 | |||
| 2667 | ✗ | snprintf(tmpbuf, sizeof(tmpbuf), "id %u", | |
| 2668 | ✗ | ntohl(mnl_attr_get_u32(info[NFNLA_HOOK_BPF_ID]))); | |
| 2669 | |||
| 2670 | ✗ | hook->chain = xstrdup(tmpbuf); | |
| 2671 | } | ||
| 2672 | } | ||
| 2673 | } | ||
| 2674 |
1/2✓ Branch 0 (58→59) taken 263 times.
✗ Branch 1 (58→61) not taken.
|
263 | if (tb[NFNLA_HOOK_HOOKNUM]) |
| 2675 | 263 | hook->num = ntohl(mnl_attr_get_u32(tb[NFNLA_HOOK_HOOKNUM])); | |
| 2676 | |||
| 2677 | 263 | hook->family = nfg->nfgen_family; | |
| 2678 | |||
| 2679 | 263 | basehook_list_add_tail(hook, data->hook_list); | |
| 2680 | |||
| 2681 | 263 | return MNL_CB_OK; | |
| 2682 | } | ||
| 2683 | |||
| 2684 | 570 | static struct nlmsghdr *nf_hook_dump_request(char *buf, uint8_t family, uint32_t seq) | |
| 2685 | { | ||
| 2686 | 570 | struct nlmsghdr *nlh = mnl_nlmsg_put_header(buf); | |
| 2687 | struct nfgenmsg *nfg; | ||
| 2688 | |||
| 2689 | 570 | nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP; | |
| 2690 | 570 | nlh->nlmsg_type = NFNL_SUBSYS_HOOK << 8; | |
| 2691 | 570 | nlh->nlmsg_seq = seq; | |
| 2692 | |||
| 2693 | 570 | nfg = mnl_nlmsg_put_extra_header(nlh, sizeof(*nfg)); | |
| 2694 | 570 | nfg->nfgen_family = family; | |
| 2695 | 570 | nfg->version = NFNETLINK_V0; | |
| 2696 | |||
| 2697 | 570 | return nlh; | |
| 2698 | } | ||
| 2699 | |||
| 2700 | − | static int __mnl_nft_dump_nf_hooks(struct netlink_ctx *ctx, uint8_t query_family, | |
| 2701 | uint8_t family, uint8_t hooknum, | ||
| 2702 | const char *devname, | ||
| 2703 | struct list_head *hook_list) | ||
| 2704 | − | { | |
| 2705 | − | char buf[MNL_SOCKET_BUFFER_SIZE]; | |
| 2706 | − | struct dump_nf_hook_data data = { | |
| 2707 | .hook_list = hook_list, | ||
| 2708 | .devname = devname, | ||
| 2709 | .family = query_family, | ||
| 2710 | }; | ||
| 2711 | struct nlmsghdr *nlh; | ||
| 2712 | |||
| 2713 | − | nlh = nf_hook_dump_request(buf, family, ctx->seqnum); | |
| 2714 | − | if (devname) | |
| 2715 | − | mnl_attr_put_strz(nlh, NFNLA_HOOK_DEV, devname); | |
| 2716 | |||
| 2717 | − | mnl_attr_put_u32(nlh, NFNLA_HOOK_HOOKNUM, htonl(hooknum)); | |
| 2718 | |||
| 2719 | − | return nft_mnl_talk(ctx, nlh, nlh->nlmsg_len, dump_nf_hooks, &data); | |
| 2720 | } | ||
| 2721 | |||
| 2722 | 55 | static void print_hooks(struct netlink_ctx *ctx, int family, struct list_head *hook_list) | |
| 2723 | { | ||
| 2724 | 55 | struct basehook *hook, *tmp, *prev = NULL; | |
| 2725 | 55 | bool same, family_in_use = false; | |
| 2726 | int prio; | ||
| 2727 | FILE *fp; | ||
| 2728 | |||
| 2729 | 55 | fp = ctx->nft->output.output_fp; | |
| 2730 | |||
| 2731 |
2/2✓ Branch 0 (6→3) taken 29 times.
✓ Branch 1 (6→7) taken 26 times.
|
55 | list_for_each_entry_safe(hook, tmp, hook_list, list) { |
| 2732 |
1/2✓ Branch 0 (3→4) taken 29 times.
✗ Branch 1 (3→5) not taken.
|
29 | if (hook->family == family) { |
| 2733 | 29 | family_in_use = true; | |
| 2734 | 29 | break; | |
| 2735 | } | ||
| 2736 | } | ||
| 2737 | |||
| 2738 |
2/2✓ Branch 0 (7→8) taken 26 times.
✓ Branch 1 (7→9) taken 29 times.
|
55 | if (!family_in_use) |
| 2739 | 26 | return; | |
| 2740 | |||
| 2741 | 29 | fprintf(fp, "family %s {\n", family2str(family)); | |
| 2742 | |||
| 2743 |
2/2✓ Branch 0 (44→12) taken 263 times.
✓ Branch 1 (44→45) taken 29 times.
|
292 | list_for_each_entry_safe(hook, tmp, hook_list, list) { |
| 2744 |
1/2✗ Branch 0 (12→13) not taken.
✓ Branch 1 (12→14) taken 263 times.
|
263 | if (hook->family != family) |
| 2745 | ✗ | continue; | |
| 2746 | |||
| 2747 |
2/2✓ Branch 0 (14→15) taken 234 times.
✓ Branch 1 (14→20) taken 29 times.
|
263 | if (prev) { |
| 2748 |
1/2✗ Branch 0 (16→17) not taken.
✓ Branch 1 (16→19) taken 234 times.
|
234 | if (basehook_eq(prev, hook)) { |
| 2749 | ✗ | fprintf(fp, "\n"); | |
| 2750 | ✗ | same = true; | |
| 2751 | } else { | ||
| 2752 | 234 | same = false; | |
| 2753 | 234 | fprintf(fp, "\n\t}\n"); | |
| 2754 | } | ||
| 2755 | } else { | ||
| 2756 | 29 | same = false; | |
| 2757 | } | ||
| 2758 | 263 | prev = hook; | |
| 2759 | |||
| 2760 |
1/2✓ Branch 0 (21→22) taken 263 times.
✗ Branch 1 (21→27) not taken.
|
263 | if (!same) { |
| 2761 |
2/2✓ Branch 0 (22→23) taken 215 times.
✓ Branch 1 (22→25) taken 48 times.
|
263 | if (hook->devname) |
| 2762 | 215 | fprintf(fp, "\thook %s device %s {\n", | |
| 2763 | 215 | hooknum2str(family, hook->num), hook->devname); | |
| 2764 | else | ||
| 2765 | 48 | fprintf(fp, "\thook %s {\n", | |
| 2766 | 48 | hooknum2str(family, hook->num)); | |
| 2767 | } | ||
| 2768 | |||
| 2769 | 263 | prio = hook->prio; | |
| 2770 |
2/2✓ Branch 0 (27→28) taken 32 times.
✓ Branch 1 (27→29) taken 231 times.
|
263 | if (prio < 0) |
| 2771 | 32 | fprintf(fp, "\t\t%011d", prio); /* outputs a '-' sign */ | |
| 2772 |
2/2✓ Branch 0 (29→30) taken 215 times.
✓ Branch 1 (29→31) taken 16 times.
|
231 | else if (prio == 0) |
| 2773 | 215 | fprintf(fp, "\t\t %010u", prio); | |
| 2774 | else | ||
| 2775 | 16 | fprintf(fp, "\t\t+%010u", prio); | |
| 2776 | |||
| 2777 |
3/4✓ Branch 0 (32→33) taken 215 times.
✓ Branch 1 (32→36) taken 48 times.
✓ Branch 2 (33→34) taken 215 times.
✗ Branch 3 (33→36) not taken.
|
263 | if (hook->table && hook->chain) |
| 2778 | 215 | fprintf(fp, " %s %s %s %s", | |
| 2779 | 215 | hook->objtype, family2str(hook->chain_family), | |
| 2780 | hook->table, hook->chain); | ||
| 2781 |
2/4✓ Branch 0 (36→37) taken 48 times.
✗ Branch 1 (36→39) not taken.
✗ Branch 2 (37→38) not taken.
✓ Branch 3 (37→39) taken 48 times.
|
48 | else if (hook->hookfn && hook->chain) |
| 2782 | ✗ | fprintf(fp, " %s %s", hook->hookfn, hook->chain); | |
| 2783 |
1/2✓ Branch 0 (39→40) taken 48 times.
✗ Branch 1 (39→41) not taken.
|
48 | else if (hook->hookfn) { |
| 2784 | 48 | fprintf(fp, " %s", hook->hookfn); | |
| 2785 | } | ||
| 2786 |
2/2✓ Branch 0 (41→42) taken 215 times.
✓ Branch 1 (41→43) taken 48 times.
|
263 | if (hook->module_name) |
| 2787 | 215 | fprintf(fp, " [%s]", hook->module_name); | |
| 2788 | } | ||
| 2789 | |||
| 2790 | 29 | fprintf(fp, "\n\t}\n"); | |
| 2791 | 29 | fprintf(fp, "}\n"); | |
| 2792 | } | ||
| 2793 | |||
| 2794 | 24 | static int mnl_nft_dump_nf(struct netlink_ctx *ctx, int family, | |
| 2795 | const char *devname, struct list_head *hook_list) | ||
| 2796 | { | ||
| 2797 | int i, err; | ||
| 2798 | |||
| 2799 |
2/2✓ Branch 0 (7→3) taken 120 times.
✓ Branch 1 (7→8) taken 24 times.
|
144 | for (i = 0; i <= NF_INET_POST_ROUTING; i++) { |
| 2800 | int tmp; | ||
| 2801 | |||
| 2802 | 120 | tmp = __mnl_nft_dump_nf_hooks(ctx, family, family, i, devname, hook_list); | |
| 2803 |
1/2✓ Branch 0 (4→5) taken 120 times.
✗ Branch 1 (4→6) not taken.
|
120 | if (tmp == 0) |
| 2804 | 120 | err = 0; | |
| 2805 | } | ||
| 2806 | |||
| 2807 | 24 | return err; | |
| 2808 | } | ||
| 2809 | |||
| 2810 | 8 | static int mnl_nft_dump_nf_arp(struct netlink_ctx *ctx, int family, | |
| 2811 | const char *devname, struct list_head *hook_list) | ||
| 2812 | { | ||
| 2813 | int err1, err2; | ||
| 2814 | |||
| 2815 | 8 | err1 = __mnl_nft_dump_nf_hooks(ctx, family, family, NF_ARP_IN, devname, hook_list); | |
| 2816 | 8 | err2 = __mnl_nft_dump_nf_hooks(ctx, family, family, NF_ARP_OUT, devname, hook_list); | |
| 2817 | |||
| 2818 |
1/2✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→6) taken 8 times.
|
8 | return err1 ? err2 : err1; |
| 2819 | } | ||
| 2820 | |||
| 2821 | 217 | static int mnl_nft_dump_nf_netdev(struct netlink_ctx *ctx, int family, | |
| 2822 | const char *devname, struct list_head *hook_list) | ||
| 2823 | { | ||
| 2824 | int err1, err2; | ||
| 2825 | |||
| 2826 | 217 | err1 = __mnl_nft_dump_nf_hooks(ctx, family, NFPROTO_NETDEV, NF_NETDEV_INGRESS, devname, hook_list); | |
| 2827 | 217 | err2 = __mnl_nft_dump_nf_hooks(ctx, family, NFPROTO_NETDEV, NF_NETDEV_EGRESS, devname, hook_list); | |
| 2828 | |||
| 2829 |
2/2✓ Branch 0 (4→5) taken 2 times.
✓ Branch 1 (4→6) taken 215 times.
|
217 | return err1 ? err2 : err1; |
| 2830 | } | ||
| 2831 | |||
| 2832 | 55 | static void release_hook_list(struct list_head *hook_list) | |
| 2833 | { | ||
| 2834 | struct basehook *hook, *next; | ||
| 2835 | |||
| 2836 |
2/2✓ Branch 0 (5→3) taken 263 times.
✓ Branch 1 (5→6) taken 55 times.
|
318 | list_for_each_entry_safe(hook, next, hook_list, list) |
| 2837 | 263 | basehook_free(hook); | |
| 2838 | 55 | } | |
| 2839 | |||
| 2840 | 32 | static void warn_if_device(struct nft_ctx *nft, const char *devname) | |
| 2841 | { | ||
| 2842 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 32 times.
|
32 | if (devname) |
| 2843 | ✗ | nft_print(&nft->output, "# device keyword (%s) unexpected for this family\n", devname); | |
| 2844 | 32 | } | |
| 2845 | |||
| 2846 | 63 | int mnl_nft_dump_nf_hooks(struct netlink_ctx *ctx, int family, const char *devname) | |
| 2847 | { | ||
| 2848 | 63 | LIST_HEAD(hook_list); | |
| 2849 | 63 | int ret = -1, tmp; | |
| 2850 | |||
| 2851 | 63 | errno = 0; | |
| 2852 | |||
| 2853 |
5/6✓ Branch 0 (2→3) taken 8 times.
✓ Branch 1 (2→14) taken 8 times.
✓ Branch 2 (2→23) taken 24 times.
✓ Branch 3 (2→26) taken 8 times.
✓ Branch 4 (2→29) taken 15 times.
✗ Branch 5 (2→39) not taken.
|
63 | switch (family) { |
| 2854 | 8 | case NFPROTO_UNSPEC: | |
| 2855 | 8 | ret = mnl_nft_dump_nf_hooks(ctx, NFPROTO_ARP, NULL); | |
| 2856 | 8 | tmp = mnl_nft_dump_nf_hooks(ctx, NFPROTO_INET, NULL); | |
| 2857 |
1/2✓ Branch 0 (5→6) taken 8 times.
✗ Branch 1 (5→7) not taken.
|
8 | if (tmp == 0) |
| 2858 | 8 | ret = 0; | |
| 2859 | 8 | tmp = mnl_nft_dump_nf_hooks(ctx, NFPROTO_BRIDGE, NULL); | |
| 2860 |
1/2✓ Branch 0 (8→9) taken 8 times.
✗ Branch 1 (8→10) not taken.
|
8 | if (tmp == 0) |
| 2861 | 8 | ret = 0; | |
| 2862 | |||
| 2863 | 8 | tmp = mnl_nft_dump_nf_hooks(ctx, NFPROTO_NETDEV, devname); | |
| 2864 |
1/2✓ Branch 0 (11→12) taken 8 times.
✗ Branch 1 (11→13) not taken.
|
8 | if (tmp == 0) |
| 2865 | 8 | ret = 0; | |
| 2866 | |||
| 2867 | 8 | return ret; | |
| 2868 | 8 | case NFPROTO_INET: | |
| 2869 | 8 | ret = 0; | |
| 2870 |
1/2✗ Branch 0 (14→15) not taken.
✓ Branch 1 (14→16) taken 8 times.
|
8 | if (devname) |
| 2871 | ✗ | ret = __mnl_nft_dump_nf_hooks(ctx, family, NFPROTO_NETDEV, | |
| 2872 | NF_NETDEV_INGRESS, devname, &hook_list); | ||
| 2873 | 8 | tmp = mnl_nft_dump_nf_hooks(ctx, NFPROTO_IPV4, NULL); | |
| 2874 |
1/2✓ Branch 0 (17→18) taken 8 times.
✗ Branch 1 (17→19) not taken.
|
8 | if (tmp == 0) |
| 2875 | 8 | ret = 0; | |
| 2876 | 8 | tmp = mnl_nft_dump_nf_hooks(ctx, NFPROTO_IPV6, NULL); | |
| 2877 |
1/2✓ Branch 0 (20→21) taken 8 times.
✗ Branch 1 (20→22) not taken.
|
8 | if (tmp == 0) |
| 2878 | 8 | ret = 0; | |
| 2879 | |||
| 2880 | 8 | break; | |
| 2881 | 24 | case NFPROTO_IPV4: | |
| 2882 | case NFPROTO_IPV6: | ||
| 2883 | case NFPROTO_BRIDGE: | ||
| 2884 | 24 | warn_if_device(ctx->nft, devname); | |
| 2885 | 24 | ret = mnl_nft_dump_nf(ctx, family, devname, &hook_list); | |
| 2886 | 24 | break; | |
| 2887 | 8 | case NFPROTO_ARP: | |
| 2888 | 8 | warn_if_device(ctx->nft, devname); | |
| 2889 | 8 | ret = mnl_nft_dump_nf_arp(ctx, family, devname, &hook_list); | |
| 2890 | 8 | break; | |
| 2891 | 15 | case NFPROTO_NETDEV: | |
| 2892 |
2/2✓ Branch 0 (29→30) taken 7 times.
✓ Branch 1 (29→31) taken 8 times.
|
15 | if (devname) { |
| 2893 | 7 | ret = mnl_nft_dump_nf_netdev(ctx, family, devname, &hook_list); | |
| 2894 | } else { | ||
| 2895 | const struct iface *iface; | ||
| 2896 | |||
| 2897 | 8 | iface = iface_cache_get_next_entry(NULL); | |
| 2898 | 8 | ret = 0; | |
| 2899 | |||
| 2900 |
2/2✓ Branch 0 (37→33) taken 210 times.
✓ Branch 1 (37→38) taken 8 times.
|
218 | while (iface) { |
| 2901 | 210 | tmp = mnl_nft_dump_nf_netdev(ctx, family, iface->name, &hook_list); | |
| 2902 |
1/2✓ Branch 0 (34→35) taken 210 times.
✗ Branch 1 (34→36) not taken.
|
210 | if (tmp == 0) |
| 2903 | 210 | ret = 0; | |
| 2904 | |||
| 2905 | 210 | iface = iface_cache_get_next_entry(iface); | |
| 2906 | } | ||
| 2907 | } | ||
| 2908 | |||
| 2909 | 15 | break; | |
| 2910 | } | ||
| 2911 | |||
| 2912 | 55 | print_hooks(ctx, family, &hook_list); | |
| 2913 | 55 | release_hook_list(&hook_list); | |
| 2914 | |||
| 2915 | 55 | return ret; | |
| 2916 | } | ||
| 2917 |