| 1 |
// SPDX-License-Identifier: GPL-2.0+ |
| 2 |
/* |
| 3 |
* mnl_utils.c Helpers for working with libmnl. |
| 4 |
*/ |
| 5 |
|
| 6 |
#include <errno.h> |
| 7 |
#include <string.h> |
| 8 |
#include <time.h> |
| 9 |
#include <libmnl/libmnl.h> |
| 10 |
#include <linux/genetlink.h> |
| 11 |
|
| 12 |
#include "libnetlink.h" |
| 13 |
#include "mnl_utils.h" |
| 14 |
#include "utils.h" |
| 15 |
|
| 16 |
struct mnl_socket *mnlu_socket_open(int bus) |
| 17 |
{ |
| 18 |
struct mnl_socket *nl; |
| 19 |
int one = 1; |
| 20 |
|
| 21 |
nl = mnl_socket_open(bus); |
| 22 |
if (nl == NULL) |
| 23 |
return NULL; |
| 24 |
|
| 25 |
mnl_socket_setsockopt(nl, NETLINK_CAP_ACK, &one, sizeof(one)); |
| 26 |
mnl_socket_setsockopt(nl, NETLINK_EXT_ACK, &one, sizeof(one)); |
| 27 |
|
| 28 |
if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) |
| 29 |
goto err_bind; |
| 30 |
|
| 31 |
return nl; |
| 32 |
|
| 33 |
err_bind: |
| 34 |
mnl_socket_close(nl); |
| 35 |
return NULL; |
| 36 |
} |
| 37 |
|
| 38 |
int mnl_add_nl_group(struct mnl_socket *nl, unsigned int group) |
| 39 |
{ |
| 40 |
return mnl_socket_bind(nl, group, MNL_SOCKET_AUTOPID); |
| 41 |
} |
| 42 |
|
| 43 |
struct nlmsghdr *mnlu_msg_prepare(void *buf, uint32_t nlmsg_type, uint16_t flags, |
| 44 |
void *extra_header, size_t extra_header_size) |
| 45 |
{ |
| 46 |
struct nlmsghdr *nlh; |
| 47 |
void *eh; |
| 48 |
|
| 49 |
nlh = mnl_nlmsg_put_header(buf); |
| 50 |
nlh->nlmsg_type = nlmsg_type; |
| 51 |
nlh->nlmsg_flags = flags; |
| 52 |
nlh->nlmsg_seq = time(NULL); |
| 53 |
|
| 54 |
eh = mnl_nlmsg_put_extra_header(nlh, extra_header_size); |
| 55 |
memcpy(eh, extra_header, extra_header_size); |
| 56 |
|
| 57 |
return nlh; |
| 58 |
} |
| 59 |
|
| 60 |
static int mnlu_cb_noop(const struct nlmsghdr *nlh, void *data) |
| 61 |
{ |
| 62 |
return MNL_CB_OK; |
| 63 |
} |
| 64 |
|
| 65 |
static int mnlu_cb_error(const struct nlmsghdr *nlh, void *data) |
| 66 |
{ |
| 67 |
const struct nlmsgerr *err = mnl_nlmsg_get_payload(nlh); |
| 68 |
|
| 69 |
if (mnl_nlmsg_get_payload_len(nlh) < sizeof(*err)) |
| 70 |
return MNL_CB_STOP; |
| 71 |
/* Netlink subsystems returns the errno value with different signess */ |
| 72 |
if (err->error < 0) |
| 73 |
errno = -err->error; |
| 74 |
else |
| 75 |
errno = err->error; |
| 76 |
|
| 77 |
if (nl_dump_ext_ack(nlh, NULL)) |
| 78 |
return MNL_CB_ERROR; |
| 79 |
|
| 80 |
return err->error == 0 ? MNL_CB_STOP : MNL_CB_ERROR; |
| 81 |
} |
| 82 |
|
| 83 |
static int mnlu_cb_stop(const struct nlmsghdr *nlh, void *data) |
| 84 |
{ |
| 85 |
int len; |
| 86 |
|
| 87 |
if (mnl_nlmsg_get_payload_len(nlh) < sizeof(len)) |
| 88 |
return MNL_CB_STOP; |
| 89 |
len = *(int *)mnl_nlmsg_get_payload(nlh); |
| 90 |
if (len < 0) { |
| 91 |
errno = -len; |
| 92 |
nl_dump_ext_ack_done(nlh, sizeof(int), len); |
| 93 |
return MNL_CB_ERROR; |
| 94 |
} |
| 95 |
return MNL_CB_STOP; |
| 96 |
} |
| 97 |
|
| 98 |
static mnl_cb_t mnlu_cb_array[NLMSG_MIN_TYPE] = { |
| 99 |
[NLMSG_NOOP] = mnlu_cb_noop, |
| 100 |
[NLMSG_ERROR] = mnlu_cb_error, |
| 101 |
[NLMSG_DONE] = mnlu_cb_stop, |
| 102 |
[NLMSG_OVERRUN] = mnlu_cb_noop, |
| 103 |
}; |
| 104 |
|
| 105 |
int mnlu_socket_recv_run(struct mnl_socket *nl, unsigned int seq, void *buf, size_t buf_size, |
| 106 |
mnl_cb_t cb, void *data) |
| 107 |
{ |
| 108 |
unsigned int portid = mnl_socket_get_portid(nl); |
| 109 |
int err; |
| 110 |
|
| 111 |
do { |
| 112 |
err = mnl_socket_recvfrom(nl, buf, buf_size); |
| 113 |
if (err <= 0) |
| 114 |
break; |
| 115 |
err = mnl_cb_run2(buf, err, seq, portid, |
| 116 |
cb, data, mnlu_cb_array, |
| 117 |
ARRAY_SIZE(mnlu_cb_array)); |
| 118 |
} while (err > 0); |
| 119 |
|
| 120 |
return err; |
| 121 |
} |
| 122 |
|
| 123 |
static int ctrl_attrs_cb(const struct nlattr *attr, void *data) |
| 124 |
{ |
| 125 |
int type = mnl_attr_get_type(attr); |
| 126 |
const struct nlattr **tb = data; |
| 127 |
|
| 128 |
if (mnl_attr_type_valid(attr, CTRL_ATTR_MAX) < 0) |
| 129 |
return MNL_CB_ERROR; |
| 130 |
|
| 131 |
if (type == CTRL_ATTR_FAMILY_ID && |
| 132 |
mnl_attr_validate(attr, MNL_TYPE_U16) < 0) |
| 133 |
return MNL_CB_ERROR; |
| 134 |
if (type == CTRL_ATTR_MAXATTR && |
| 135 |
mnl_attr_validate(attr, MNL_TYPE_U32) < 0) |
| 136 |
return MNL_CB_ERROR; |
| 137 |
if (type == CTRL_ATTR_POLICY && |
| 138 |
mnl_attr_validate(attr, MNL_TYPE_NESTED) < 0) |
| 139 |
return MNL_CB_ERROR; |
| 140 |
if (type == CTRL_ATTR_OP_POLICY && |
| 141 |
mnl_attr_validate(attr, MNL_TYPE_NESTED) < 0) |
| 142 |
return MNL_CB_ERROR; |
| 143 |
tb[type] = attr; |
| 144 |
return MNL_CB_OK; |
| 145 |
} |
| 146 |
|
| 147 |
static int get_family_cb(const struct nlmsghdr *nlh, void *data) |
| 148 |
{ |
| 149 |
struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh); |
| 150 |
struct nlattr *tb[CTRL_ATTR_MAX + 1] = {}; |
| 151 |
struct mnlu_gen_socket *nlg = data; |
| 152 |
|
| 153 |
mnl_attr_parse(nlh, sizeof(*genl), ctrl_attrs_cb, tb); |
| 154 |
if (!tb[CTRL_ATTR_FAMILY_ID]) |
| 155 |
return MNL_CB_ERROR; |
| 156 |
if (!tb[CTRL_ATTR_MAXATTR]) |
| 157 |
return MNL_CB_ERROR; |
| 158 |
nlg->family = mnl_attr_get_u16(tb[CTRL_ATTR_FAMILY_ID]); |
| 159 |
nlg->maxattr = mnl_attr_get_u32(tb[CTRL_ATTR_MAXATTR]); |
| 160 |
return MNL_CB_OK; |
| 161 |
} |
| 162 |
|
| 163 |
static int family_get(struct mnlu_gen_socket *nlg, const char *family_name) |
| 164 |
{ |
| 165 |
struct genlmsghdr hdr = {}; |
| 166 |
struct nlmsghdr *nlh; |
| 167 |
int err; |
| 168 |
|
| 169 |
hdr.cmd = CTRL_CMD_GETFAMILY; |
| 170 |
hdr.version = 0x1; |
| 171 |
|
| 172 |
nlh = mnlu_msg_prepare(nlg->buf, GENL_ID_CTRL, |
| 173 |
NLM_F_REQUEST | NLM_F_ACK, |
| 174 |
&hdr, sizeof(hdr)); |
| 175 |
|
| 176 |
mnl_attr_put_strz(nlh, CTRL_ATTR_FAMILY_NAME, family_name); |
| 177 |
|
| 178 |
err = mnl_socket_sendto(nlg->nl, nlh, nlh->nlmsg_len); |
| 179 |
if (err < 0) |
| 180 |
return err; |
| 181 |
|
| 182 |
err = mnlu_socket_recv_run(nlg->nl, nlh->nlmsg_seq, nlg->buf, |
| 183 |
MNL_SOCKET_BUFFER_SIZE, |
| 184 |
get_family_cb, nlg); |
| 185 |
return err; |
| 186 |
} |
| 187 |
|
| 188 |
int mnlu_gen_socket_open(struct mnlu_gen_socket *nlg, const char *family_name, |
| 189 |
uint8_t version) |
| 190 |
{ |
| 191 |
int err; |
| 192 |
|
| 193 |
nlg->buf = malloc(MNL_SOCKET_BUFFER_SIZE); |
| 194 |
if (!nlg->buf) |
| 195 |
goto err_buf_alloc; |
| 196 |
|
| 197 |
nlg->nl = mnlu_socket_open(NETLINK_GENERIC); |
| 198 |
if (!nlg->nl) |
| 199 |
goto err_socket_open; |
| 200 |
|
| 201 |
nlg->version = version; |
| 202 |
|
| 203 |
err = family_get(nlg, family_name); |
| 204 |
if (err) |
| 205 |
goto err_socket; |
| 206 |
|
| 207 |
return 0; |
| 208 |
|
| 209 |
err_socket: |
| 210 |
mnl_socket_close(nlg->nl); |
| 211 |
err_socket_open: |
| 212 |
free(nlg->buf); |
| 213 |
err_buf_alloc: |
| 214 |
return -1; |
| 215 |
} |
| 216 |
|
| 217 |
void mnlu_gen_socket_close(struct mnlu_gen_socket *nlg) |
| 218 |
{ |
| 219 |
mnl_socket_close(nlg->nl); |
| 220 |
free(nlg->buf); |
| 221 |
} |
| 222 |
|
| 223 |
struct nlmsghdr * |
| 224 |
_mnlu_gen_socket_cmd_prepare(struct mnlu_gen_socket *nlg, |
| 225 |
uint8_t cmd, uint16_t flags, |
| 226 |
uint32_t id, uint8_t version) |
| 227 |
{ |
| 228 |
struct genlmsghdr hdr = {}; |
| 229 |
struct nlmsghdr *nlh; |
| 230 |
|
| 231 |
hdr.cmd = cmd; |
| 232 |
hdr.version = version; |
| 233 |
nlh = mnlu_msg_prepare(nlg->buf, id, flags, &hdr, sizeof(hdr)); |
| 234 |
nlg->seq = nlh->nlmsg_seq; |
| 235 |
return nlh; |
| 236 |
} |
| 237 |
|
| 238 |
struct nlmsghdr *mnlu_gen_socket_cmd_prepare(struct mnlu_gen_socket *nlg, |
| 239 |
uint8_t cmd, uint16_t flags) |
| 240 |
{ |
| 241 |
return _mnlu_gen_socket_cmd_prepare(nlg, cmd, flags, nlg->family, |
| 242 |
nlg->version); |
| 243 |
} |
| 244 |
|
| 245 |
int mnlu_gen_socket_sndrcv(struct mnlu_gen_socket *nlg, const struct nlmsghdr *nlh, |
| 246 |
mnl_cb_t data_cb, void *data) |
| 247 |
{ |
| 248 |
int err; |
| 249 |
|
| 250 |
err = mnl_socket_sendto(nlg->nl, nlh, nlh->nlmsg_len); |
| 251 |
if (err < 0) { |
| 252 |
perror("Failed to send data"); |
| 253 |
return -errno; |
| 254 |
} |
| 255 |
|
| 256 |
err = mnlu_socket_recv_run(nlg->nl, nlh->nlmsg_seq, nlg->buf, |
| 257 |
MNL_SOCKET_BUFFER_SIZE, |
| 258 |
data_cb, data); |
| 259 |
if (err < 0) { |
| 260 |
fprintf(stderr, "kernel answers: %s\n", strerror(errno)); |
| 261 |
return -errno; |
| 262 |
} |
| 263 |
return 0; |
| 264 |
} |
| 265 |
|
| 266 |
int mnlu_gen_socket_recv_run(struct mnlu_gen_socket *nlg, mnl_cb_t cb, |
| 267 |
void *data) |
| 268 |
{ |
| 269 |
return mnlu_socket_recv_run(nlg->nl, nlg->seq, nlg->buf, |
| 270 |
MNL_SOCKET_BUFFER_SIZE, |
| 271 |
cb, data); |
| 272 |
} |
| 273 |
|
| 274 |
static int ctrl_policy_attrs_cb(const struct nlattr *attr, void *data) |
| 275 |
{ |
| 276 |
int type = mnl_attr_get_type(attr); |
| 277 |
const struct nlattr **tb = data; |
| 278 |
|
| 279 |
if (mnl_attr_type_valid(attr, CTRL_ATTR_POLICY_DUMP_MAX) < 0) |
| 280 |
return MNL_CB_ERROR; |
| 281 |
|
| 282 |
if (type == CTRL_ATTR_POLICY_DO && |
| 283 |
mnl_attr_validate(attr, MNL_TYPE_U32) < 0) |
| 284 |
return MNL_CB_ERROR; |
| 285 |
if (type == CTRL_ATTR_POLICY_DUMP && |
| 286 |
mnl_attr_validate(attr, MNL_TYPE_U32) < 0) |
| 287 |
return MNL_CB_ERROR; |
| 288 |
|
| 289 |
tb[type] = attr; |
| 290 |
return MNL_CB_OK; |
| 291 |
} |
| 292 |
|
| 293 |
struct cmd_dump_policy_ctx { |
| 294 |
uint8_t cmd; |
| 295 |
uint8_t do_policy_idx_found:1, |
| 296 |
dump_policy_idx_found:1; |
| 297 |
uint32_t do_policy_idx; |
| 298 |
uint32_t dump_policy_idx; |
| 299 |
uint32_t dump_policy_attr_count; |
| 300 |
}; |
| 301 |
|
| 302 |
static void process_dump_op_policy_nest(const struct nlattr *op_policy_nest, |
| 303 |
struct cmd_dump_policy_ctx *ctx) |
| 304 |
{ |
| 305 |
struct nlattr *tb[CTRL_ATTR_POLICY_DUMP_MAX + 1] = {}; |
| 306 |
const struct nlattr *attr; |
| 307 |
int err; |
| 308 |
|
| 309 |
mnl_attr_for_each_nested(attr, op_policy_nest) { |
| 310 |
if (ctx->cmd != (attr->nla_type & ~NLA_F_NESTED)) |
| 311 |
continue; |
| 312 |
err = mnl_attr_parse_nested(attr, ctrl_policy_attrs_cb, tb); |
| 313 |
if (err != MNL_CB_OK) |
| 314 |
continue; |
| 315 |
if (tb[CTRL_ATTR_POLICY_DO]) { |
| 316 |
ctx->do_policy_idx = mnl_attr_get_u32(tb[CTRL_ATTR_POLICY_DO]); |
| 317 |
ctx->do_policy_idx_found = true; |
| 318 |
} |
| 319 |
if (tb[CTRL_ATTR_POLICY_DUMP]) { |
| 320 |
ctx->dump_policy_idx = mnl_attr_get_u32(tb[CTRL_ATTR_POLICY_DUMP]); |
| 321 |
ctx->dump_policy_idx_found = true; |
| 322 |
} |
| 323 |
break; |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
static void process_dump_policy_nest(const struct nlattr *policy_nest, |
| 328 |
struct cmd_dump_policy_ctx *ctx) |
| 329 |
{ |
| 330 |
const struct nlattr *attr; |
| 331 |
|
| 332 |
if (!ctx->dump_policy_idx_found) |
| 333 |
return; |
| 334 |
|
| 335 |
mnl_attr_for_each_nested(attr, policy_nest) |
| 336 |
if (ctx->dump_policy_idx == (attr->nla_type & ~NLA_F_NESTED)) |
| 337 |
ctx->dump_policy_attr_count++; |
| 338 |
} |
| 339 |
|
| 340 |
static int cmd_dump_policy_cb(const struct nlmsghdr *nlh, void *data) |
| 341 |
{ |
| 342 |
struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh); |
| 343 |
struct nlattr *tb[CTRL_ATTR_MAX + 1] = {}; |
| 344 |
struct cmd_dump_policy_ctx *ctx = data; |
| 345 |
|
| 346 |
mnl_attr_parse(nlh, sizeof(*genl), ctrl_attrs_cb, tb); |
| 347 |
if (!tb[CTRL_ATTR_FAMILY_ID]) |
| 348 |
return MNL_CB_OK; |
| 349 |
|
| 350 |
if (tb[CTRL_ATTR_OP_POLICY]) |
| 351 |
process_dump_op_policy_nest(tb[CTRL_ATTR_OP_POLICY], ctx); |
| 352 |
|
| 353 |
if (tb[CTRL_ATTR_POLICY]) |
| 354 |
process_dump_policy_nest(tb[CTRL_ATTR_POLICY], ctx); |
| 355 |
|
| 356 |
return MNL_CB_OK; |
| 357 |
} |
| 358 |
|
| 359 |
int mnlu_gen_cmd_dump_policy(struct mnlu_gen_socket *nlg, uint8_t cmd) |
| 360 |
{ |
| 361 |
struct cmd_dump_policy_ctx ctx = { |
| 362 |
.cmd = cmd, |
| 363 |
}; |
| 364 |
struct nlmsghdr *nlh; |
| 365 |
int err; |
| 366 |
|
| 367 |
nlh = _mnlu_gen_socket_cmd_prepare(nlg, CTRL_CMD_GETPOLICY, |
| 368 |
NLM_F_REQUEST | NLM_F_ACK | NLM_F_DUMP, |
| 369 |
GENL_ID_CTRL, 1); |
| 370 |
|
| 371 |
mnl_attr_put_u16(nlh, CTRL_ATTR_FAMILY_ID, nlg->family); |
| 372 |
|
| 373 |
err = mnlu_gen_socket_sndrcv(nlg, nlh, cmd_dump_policy_cb, &ctx); |
| 374 |
if (err) |
| 375 |
return err; |
| 376 |
|
| 377 |
if (!ctx.dump_policy_idx_found || !ctx.do_policy_idx_found || |
| 378 |
ctx.do_policy_idx == ctx.dump_policy_idx || |
| 379 |
!ctx.dump_policy_attr_count) |
| 380 |
return -ENOTSUP; |
| 381 |
|
| 382 |
return 0; |
| 383 |
} |