2 * mnlg.c Generic Netlink helpers for libmnl
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
9 * Authors: Jiri Pirko <jiri@mellanox.com>
18 #include <libmnl/libmnl.h>
19 #include <linux/genetlink.h>
23 #ifndef MNL_ARRAY_SIZE
24 #define MNL_ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
28 #define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
31 #ifndef NETLINK_CAP_ACK
32 #define NETLINK_CAP_ACK 10
34 #ifndef NETLINK_EXT_ACK
35 #define NETLINK_EXT_ACK 11
39 struct mnl_socket *nl;
47 static struct nlmsghdr *__mnlg_msg_prepare(struct mnlg_socket *nlg, uint8_t cmd,
48 uint16_t flags, uint32_t id,
52 struct genlmsghdr *genl;
54 nlh = mnl_nlmsg_put_header(nlg->buf);
56 nlh->nlmsg_flags = flags;
57 nlg->seq = time(NULL);
58 nlh->nlmsg_seq = nlg->seq;
60 genl = mnl_nlmsg_put_extra_header(nlh, sizeof(struct genlmsghdr));
62 genl->version = version;
67 struct nlmsghdr *mnlg_msg_prepare(struct mnlg_socket *nlg, uint8_t cmd,
70 return __mnlg_msg_prepare(nlg, cmd, flags, nlg->id, nlg->version);
73 int mnlg_socket_send(struct mnlg_socket *nlg, const struct nlmsghdr *nlh)
75 return mnl_socket_sendto(nlg->nl, nlh, nlh->nlmsg_len);
78 static int mnlg_cb_noop(const struct nlmsghdr *nlh, void *data)
83 static int mnlg_cb_error(const struct nlmsghdr *nlh, void *data)
85 const struct nlmsgerr *err = mnl_nlmsg_get_payload(nlh);
87 /* Netlink subsystems returns the errno value with different signess */
93 return err->error == 0 ? MNL_CB_STOP : MNL_CB_ERROR;
96 static int mnlg_cb_stop(const struct nlmsghdr *nlh, void *data)
98 int len = *(int *)NLMSG_DATA(nlh);
107 static mnl_cb_t mnlg_cb_array[NLMSG_MIN_TYPE] = {
108 [NLMSG_NOOP] = mnlg_cb_noop,
109 [NLMSG_ERROR] = mnlg_cb_error,
110 [NLMSG_DONE] = mnlg_cb_stop,
111 [NLMSG_OVERRUN] = mnlg_cb_noop,
114 int mnlg_socket_recv_run(struct mnlg_socket *nlg, mnl_cb_t data_cb, void *data)
119 err = mnl_socket_recvfrom(nlg->nl, nlg->buf,
120 MNL_SOCKET_BUFFER_SIZE);
123 err = mnl_cb_run2(nlg->buf, err, nlg->seq, nlg->portid,
124 data_cb, data, mnlg_cb_array,
125 ARRAY_SIZE(mnlg_cb_array));
137 static int parse_mc_grps_cb(const struct nlattr *attr, void *data)
139 const struct nlattr **tb = data;
140 int type = mnl_attr_get_type(attr);
142 if (mnl_attr_type_valid(attr, CTRL_ATTR_MCAST_GRP_MAX) < 0)
146 case CTRL_ATTR_MCAST_GRP_ID:
147 if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
150 case CTRL_ATTR_MCAST_GRP_NAME:
151 if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0)
159 static void parse_genl_mc_grps(struct nlattr *nested,
160 struct group_info *group_info)
165 mnl_attr_for_each_nested(pos, nested) {
166 struct nlattr *tb[CTRL_ATTR_MCAST_GRP_MAX + 1] = {};
168 mnl_attr_parse_nested(pos, parse_mc_grps_cb, tb);
169 if (!tb[CTRL_ATTR_MCAST_GRP_NAME] ||
170 !tb[CTRL_ATTR_MCAST_GRP_ID])
173 name = mnl_attr_get_str(tb[CTRL_ATTR_MCAST_GRP_NAME]);
174 if (strcmp(name, group_info->name) != 0)
177 group_info->id = mnl_attr_get_u32(tb[CTRL_ATTR_MCAST_GRP_ID]);
178 group_info->found = true;
182 static int get_group_id_attr_cb(const struct nlattr *attr, void *data)
184 const struct nlattr **tb = data;
185 int type = mnl_attr_get_type(attr);
187 if (mnl_attr_type_valid(attr, CTRL_ATTR_MAX) < 0)
190 if (type == CTRL_ATTR_MCAST_GROUPS &&
191 mnl_attr_validate(attr, MNL_TYPE_NESTED) < 0)
197 static int get_group_id_cb(const struct nlmsghdr *nlh, void *data)
199 struct group_info *group_info = data;
200 struct nlattr *tb[CTRL_ATTR_MAX + 1] = {};
201 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
203 mnl_attr_parse(nlh, sizeof(*genl), get_group_id_attr_cb, tb);
204 if (!tb[CTRL_ATTR_MCAST_GROUPS])
206 parse_genl_mc_grps(tb[CTRL_ATTR_MCAST_GROUPS], group_info);
210 int mnlg_socket_group_add(struct mnlg_socket *nlg, const char *group_name)
212 struct nlmsghdr *nlh;
213 struct group_info group_info;
216 nlh = __mnlg_msg_prepare(nlg, CTRL_CMD_GETFAMILY,
217 NLM_F_REQUEST | NLM_F_ACK, GENL_ID_CTRL, 1);
218 mnl_attr_put_u16(nlh, CTRL_ATTR_FAMILY_ID, nlg->id);
220 err = mnlg_socket_send(nlg, nlh);
224 group_info.found = false;
225 group_info.name = group_name;
226 err = mnlg_socket_recv_run(nlg, get_group_id_cb, &group_info);
230 if (!group_info.found) {
235 err = mnl_socket_setsockopt(nlg->nl, NETLINK_ADD_MEMBERSHIP,
236 &group_info.id, sizeof(group_info.id));
243 static int get_family_id_attr_cb(const struct nlattr *attr, void *data)
245 const struct nlattr **tb = data;
246 int type = mnl_attr_get_type(attr);
248 if (mnl_attr_type_valid(attr, CTRL_ATTR_MAX) < 0)
251 if (type == CTRL_ATTR_FAMILY_ID &&
252 mnl_attr_validate(attr, MNL_TYPE_U16) < 0)
258 static int get_family_id_cb(const struct nlmsghdr *nlh, void *data)
260 uint32_t *p_id = data;
261 struct nlattr *tb[CTRL_ATTR_MAX + 1] = {};
262 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
264 mnl_attr_parse(nlh, sizeof(*genl), get_family_id_attr_cb, tb);
265 if (!tb[CTRL_ATTR_FAMILY_ID])
267 *p_id = mnl_attr_get_u16(tb[CTRL_ATTR_FAMILY_ID]);
271 struct mnlg_socket *mnlg_socket_open(const char *family_name, uint8_t version)
273 struct mnlg_socket *nlg;
274 struct nlmsghdr *nlh;
278 nlg = malloc(sizeof(*nlg));
282 nlg->buf = malloc(MNL_SOCKET_BUFFER_SIZE);
286 nlg->nl = mnl_socket_open(NETLINK_GENERIC);
288 goto err_mnl_socket_open;
290 /* Older kernels may no support capped/extended ACK reporting */
291 mnl_socket_setsockopt(nlg->nl, NETLINK_CAP_ACK, &one, sizeof(one));
292 mnl_socket_setsockopt(nlg->nl, NETLINK_EXT_ACK, &one, sizeof(one));
294 err = mnl_socket_bind(nlg->nl, 0, MNL_SOCKET_AUTOPID);
296 goto err_mnl_socket_bind;
298 nlg->portid = mnl_socket_get_portid(nlg->nl);
300 nlh = __mnlg_msg_prepare(nlg, CTRL_CMD_GETFAMILY,
301 NLM_F_REQUEST | NLM_F_ACK, GENL_ID_CTRL, 1);
302 mnl_attr_put_strz(nlh, CTRL_ATTR_FAMILY_NAME, family_name);
304 err = mnlg_socket_send(nlg, nlh);
306 goto err_mnlg_socket_send;
308 err = mnlg_socket_recv_run(nlg, get_family_id_cb, &nlg->id);
310 goto err_mnlg_socket_recv_run;
312 nlg->version = version;
315 err_mnlg_socket_recv_run:
316 err_mnlg_socket_send:
318 mnl_socket_close(nlg->nl);
326 void mnlg_socket_close(struct mnlg_socket *nlg)
328 mnl_socket_close(nlg->nl);