| 1 |
/* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 |
/* |
| 3 |
* mnlg.c Generic Netlink helpers for libmnl |
| 4 |
* |
| 5 |
* Authors: Jiri Pirko <[email protected]> |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <stdlib.h> |
| 9 |
#include <stdbool.h> |
| 10 |
#include <string.h> |
| 11 |
#include <errno.h> |
| 12 |
#include <unistd.h> |
| 13 |
#include <libmnl/libmnl.h> |
| 14 |
#include <linux/genetlink.h> |
| 15 |
|
| 16 |
#include "libnetlink.h" |
| 17 |
#include "mnl_utils.h" |
| 18 |
#include "utils.h" |
| 19 |
#include "mnlg.h" |
| 20 |
|
| 21 |
int mnlg_socket_send(struct mnlu_gen_socket *nlg, const struct nlmsghdr *nlh) |
| 22 |
{ |
| 23 |
return mnl_socket_sendto(nlg->nl, nlh, nlh->nlmsg_len); |
| 24 |
} |
| 25 |
|
| 26 |
struct group_info { |
| 27 |
bool found; |
| 28 |
uint32_t id; |
| 29 |
const char *name; |
| 30 |
}; |
| 31 |
|
| 32 |
static int parse_mc_grps_cb(const struct nlattr *attr, void *data) |
| 33 |
{ |
| 34 |
const struct nlattr **tb = data; |
| 35 |
int type = mnl_attr_get_type(attr); |
| 36 |
|
| 37 |
if (mnl_attr_type_valid(attr, CTRL_ATTR_MCAST_GRP_MAX) < 0) |
| 38 |
return MNL_CB_OK; |
| 39 |
|
| 40 |
switch (type) { |
| 41 |
case CTRL_ATTR_MCAST_GRP_ID: |
| 42 |
if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) |
| 43 |
return MNL_CB_ERROR; |
| 44 |
break; |
| 45 |
case CTRL_ATTR_MCAST_GRP_NAME: |
| 46 |
if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0) |
| 47 |
return MNL_CB_ERROR; |
| 48 |
break; |
| 49 |
} |
| 50 |
tb[type] = attr; |
| 51 |
return MNL_CB_OK; |
| 52 |
} |
| 53 |
|
| 54 |
static void parse_genl_mc_grps(struct nlattr *nested, |
| 55 |
struct group_info *group_info) |
| 56 |
{ |
| 57 |
struct nlattr *pos; |
| 58 |
const char *name; |
| 59 |
|
| 60 |
mnl_attr_for_each_nested(pos, nested) { |
| 61 |
struct nlattr *tb[CTRL_ATTR_MCAST_GRP_MAX + 1] = {}; |
| 62 |
|
| 63 |
mnl_attr_parse_nested(pos, parse_mc_grps_cb, tb); |
| 64 |
if (!tb[CTRL_ATTR_MCAST_GRP_NAME] || |
| 65 |
!tb[CTRL_ATTR_MCAST_GRP_ID]) |
| 66 |
continue; |
| 67 |
|
| 68 |
name = mnl_attr_get_str(tb[CTRL_ATTR_MCAST_GRP_NAME]); |
| 69 |
if (strcmp(name, group_info->name) != 0) |
| 70 |
continue; |
| 71 |
|
| 72 |
group_info->id = mnl_attr_get_u32(tb[CTRL_ATTR_MCAST_GRP_ID]); |
| 73 |
group_info->found = true; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
static int get_group_id_attr_cb(const struct nlattr *attr, void *data) |
| 78 |
{ |
| 79 |
const struct nlattr **tb = data; |
| 80 |
int type = mnl_attr_get_type(attr); |
| 81 |
|
| 82 |
if (mnl_attr_type_valid(attr, CTRL_ATTR_MAX) < 0) |
| 83 |
return MNL_CB_ERROR; |
| 84 |
|
| 85 |
if (type == CTRL_ATTR_MCAST_GROUPS && |
| 86 |
mnl_attr_validate(attr, MNL_TYPE_NESTED) < 0) |
| 87 |
return MNL_CB_ERROR; |
| 88 |
tb[type] = attr; |
| 89 |
return MNL_CB_OK; |
| 90 |
} |
| 91 |
|
| 92 |
static int get_group_id_cb(const struct nlmsghdr *nlh, void *data) |
| 93 |
{ |
| 94 |
struct group_info *group_info = data; |
| 95 |
struct nlattr *tb[CTRL_ATTR_MAX + 1] = {}; |
| 96 |
struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh); |
| 97 |
|
| 98 |
mnl_attr_parse(nlh, sizeof(*genl), get_group_id_attr_cb, tb); |
| 99 |
if (!tb[CTRL_ATTR_MCAST_GROUPS]) |
| 100 |
return MNL_CB_ERROR; |
| 101 |
parse_genl_mc_grps(tb[CTRL_ATTR_MCAST_GROUPS], group_info); |
| 102 |
return MNL_CB_OK; |
| 103 |
} |
| 104 |
|
| 105 |
int mnlg_socket_group_add(struct mnlu_gen_socket *nlg, const char *group_name) |
| 106 |
{ |
| 107 |
struct nlmsghdr *nlh; |
| 108 |
struct group_info group_info; |
| 109 |
int err; |
| 110 |
|
| 111 |
nlh = _mnlu_gen_socket_cmd_prepare(nlg, CTRL_CMD_GETFAMILY, |
| 112 |
NLM_F_REQUEST | NLM_F_ACK, |
| 113 |
GENL_ID_CTRL, 1); |
| 114 |
|
| 115 |
mnl_attr_put_u16(nlh, CTRL_ATTR_FAMILY_ID, nlg->family); |
| 116 |
|
| 117 |
err = mnlg_socket_send(nlg, nlh); |
| 118 |
if (err < 0) |
| 119 |
return err; |
| 120 |
|
| 121 |
group_info.found = false; |
| 122 |
group_info.name = group_name; |
| 123 |
err = mnlu_gen_socket_recv_run(nlg, get_group_id_cb, &group_info); |
| 124 |
if (err < 0) |
| 125 |
return err; |
| 126 |
|
| 127 |
if (!group_info.found) { |
| 128 |
errno = ENOENT; |
| 129 |
return -1; |
| 130 |
} |
| 131 |
|
| 132 |
err = mnl_socket_setsockopt(nlg->nl, NETLINK_ADD_MEMBERSHIP, |
| 133 |
&group_info.id, sizeof(group_info.id)); |
| 134 |
if (err < 0) |
| 135 |
return err; |
| 136 |
|
| 137 |
return 0; |
| 138 |
} |
| 139 |
|
| 140 |
int mnlg_socket_get_fd(struct mnlu_gen_socket *nlg) |
| 141 |
{ |
| 142 |
return mnl_socket_get_fd(nlg->nl); |
| 143 |
} |