]>
Commit | Line | Data |
---|---|---|
a3c4b484 JP |
1 | /* |
2 | * mnlg.c Generic Netlink helpers for libmnl | |
3 | * | |
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. | |
8 | * | |
9 | * Authors: Jiri Pirko <jiri@mellanox.com> | |
10 | */ | |
11 | ||
12 | #include <stdlib.h> | |
13 | #include <stdbool.h> | |
14 | #include <string.h> | |
15 | #include <errno.h> | |
16 | #include <unistd.h> | |
17 | #include <time.h> | |
18 | #include <libmnl/libmnl.h> | |
19 | #include <linux/genetlink.h> | |
20 | ||
049c5853 AS |
21 | #include "libnetlink.h" |
22 | #include "utils.h" | |
a3c4b484 JP |
23 | #include "mnlg.h" |
24 | ||
25 | struct mnlg_socket { | |
26 | struct mnl_socket *nl; | |
27 | char *buf; | |
28 | uint32_t id; | |
29 | uint8_t version; | |
30 | unsigned int seq; | |
31 | unsigned int portid; | |
32 | }; | |
33 | ||
34 | static struct nlmsghdr *__mnlg_msg_prepare(struct mnlg_socket *nlg, uint8_t cmd, | |
35 | uint16_t flags, uint32_t id, | |
36 | uint8_t version) | |
37 | { | |
38 | struct nlmsghdr *nlh; | |
39 | struct genlmsghdr *genl; | |
40 | ||
41 | nlh = mnl_nlmsg_put_header(nlg->buf); | |
42 | nlh->nlmsg_type = id; | |
43 | nlh->nlmsg_flags = flags; | |
44 | nlg->seq = time(NULL); | |
45 | nlh->nlmsg_seq = nlg->seq; | |
46 | ||
47 | genl = mnl_nlmsg_put_extra_header(nlh, sizeof(struct genlmsghdr)); | |
48 | genl->cmd = cmd; | |
49 | genl->version = version; | |
50 | ||
51 | return nlh; | |
52 | } | |
53 | ||
54 | struct nlmsghdr *mnlg_msg_prepare(struct mnlg_socket *nlg, uint8_t cmd, | |
55 | uint16_t flags) | |
56 | { | |
57 | return __mnlg_msg_prepare(nlg, cmd, flags, nlg->id, nlg->version); | |
58 | } | |
59 | ||
60 | int mnlg_socket_send(struct mnlg_socket *nlg, const struct nlmsghdr *nlh) | |
61 | { | |
62 | return mnl_socket_sendto(nlg->nl, nlh, nlh->nlmsg_len); | |
63 | } | |
64 | ||
049c5853 AS |
65 | static int mnlg_cb_noop(const struct nlmsghdr *nlh, void *data) |
66 | { | |
67 | return MNL_CB_OK; | |
68 | } | |
69 | ||
70 | static int mnlg_cb_error(const struct nlmsghdr *nlh, void *data) | |
71 | { | |
72 | const struct nlmsgerr *err = mnl_nlmsg_get_payload(nlh); | |
73 | ||
049c5853 AS |
74 | /* Netlink subsystems returns the errno value with different signess */ |
75 | if (err->error < 0) | |
76 | errno = -err->error; | |
77 | else | |
78 | errno = err->error; | |
79 | ||
f85adc61 AS |
80 | if (nl_dump_ext_ack(nlh, NULL)) |
81 | return MNL_CB_ERROR; | |
82 | ||
049c5853 AS |
83 | return err->error == 0 ? MNL_CB_STOP : MNL_CB_ERROR; |
84 | } | |
85 | ||
86 | static int mnlg_cb_stop(const struct nlmsghdr *nlh, void *data) | |
87 | { | |
88 | return MNL_CB_STOP; | |
89 | } | |
90 | ||
91 | static mnl_cb_t mnlg_cb_array[NLMSG_MIN_TYPE] = { | |
92 | [NLMSG_NOOP] = mnlg_cb_noop, | |
93 | [NLMSG_ERROR] = mnlg_cb_error, | |
94 | [NLMSG_DONE] = mnlg_cb_stop, | |
95 | [NLMSG_OVERRUN] = mnlg_cb_noop, | |
96 | }; | |
97 | ||
a3c4b484 JP |
98 | int mnlg_socket_recv_run(struct mnlg_socket *nlg, mnl_cb_t data_cb, void *data) |
99 | { | |
100 | int err; | |
101 | ||
102 | do { | |
103 | err = mnl_socket_recvfrom(nlg->nl, nlg->buf, | |
104 | MNL_SOCKET_BUFFER_SIZE); | |
105 | if (err <= 0) | |
106 | break; | |
049c5853 AS |
107 | err = mnl_cb_run2(nlg->buf, err, nlg->seq, nlg->portid, |
108 | data_cb, data, mnlg_cb_array, | |
109 | ARRAY_SIZE(mnlg_cb_array)); | |
a3c4b484 JP |
110 | } while (err > 0); |
111 | ||
112 | return err; | |
113 | } | |
114 | ||
115 | struct group_info { | |
116 | bool found; | |
117 | uint32_t id; | |
118 | const char *name; | |
119 | }; | |
120 | ||
121 | static int parse_mc_grps_cb(const struct nlattr *attr, void *data) | |
122 | { | |
123 | const struct nlattr **tb = data; | |
124 | int type = mnl_attr_get_type(attr); | |
125 | ||
126 | if (mnl_attr_type_valid(attr, CTRL_ATTR_MCAST_GRP_MAX) < 0) | |
127 | return MNL_CB_OK; | |
128 | ||
129 | switch (type) { | |
130 | case CTRL_ATTR_MCAST_GRP_ID: | |
131 | if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) | |
132 | return MNL_CB_ERROR; | |
133 | break; | |
134 | case CTRL_ATTR_MCAST_GRP_NAME: | |
135 | if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0) | |
136 | return MNL_CB_ERROR; | |
137 | break; | |
138 | } | |
139 | tb[type] = attr; | |
140 | return MNL_CB_OK; | |
141 | } | |
142 | ||
143 | static void parse_genl_mc_grps(struct nlattr *nested, | |
144 | struct group_info *group_info) | |
145 | { | |
146 | struct nlattr *pos; | |
147 | const char *name; | |
148 | ||
149 | mnl_attr_for_each_nested(pos, nested) { | |
150 | struct nlattr *tb[CTRL_ATTR_MCAST_GRP_MAX + 1] = {}; | |
151 | ||
152 | mnl_attr_parse_nested(pos, parse_mc_grps_cb, tb); | |
153 | if (!tb[CTRL_ATTR_MCAST_GRP_NAME] || | |
154 | !tb[CTRL_ATTR_MCAST_GRP_ID]) | |
155 | continue; | |
156 | ||
157 | name = mnl_attr_get_str(tb[CTRL_ATTR_MCAST_GRP_NAME]); | |
158 | if (strcmp(name, group_info->name) != 0) | |
159 | continue; | |
160 | ||
161 | group_info->id = mnl_attr_get_u32(tb[CTRL_ATTR_MCAST_GRP_ID]); | |
162 | group_info->found = true; | |
163 | } | |
164 | } | |
165 | ||
166 | static int get_group_id_attr_cb(const struct nlattr *attr, void *data) | |
167 | { | |
168 | const struct nlattr **tb = data; | |
169 | int type = mnl_attr_get_type(attr); | |
170 | ||
171 | if (mnl_attr_type_valid(attr, CTRL_ATTR_MAX) < 0) | |
172 | return MNL_CB_ERROR; | |
173 | ||
174 | if (type == CTRL_ATTR_MCAST_GROUPS && | |
175 | mnl_attr_validate(attr, MNL_TYPE_NESTED) < 0) | |
176 | return MNL_CB_ERROR; | |
177 | tb[type] = attr; | |
178 | return MNL_CB_OK; | |
179 | } | |
180 | ||
181 | static int get_group_id_cb(const struct nlmsghdr *nlh, void *data) | |
182 | { | |
183 | struct group_info *group_info = data; | |
184 | struct nlattr *tb[CTRL_ATTR_MAX + 1] = {}; | |
185 | struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh); | |
186 | ||
187 | mnl_attr_parse(nlh, sizeof(*genl), get_group_id_attr_cb, tb); | |
188 | if (!tb[CTRL_ATTR_MCAST_GROUPS]) | |
189 | return MNL_CB_ERROR; | |
190 | parse_genl_mc_grps(tb[CTRL_ATTR_MCAST_GROUPS], group_info); | |
191 | return MNL_CB_OK; | |
192 | } | |
193 | ||
194 | int mnlg_socket_group_add(struct mnlg_socket *nlg, const char *group_name) | |
195 | { | |
196 | struct nlmsghdr *nlh; | |
197 | struct group_info group_info; | |
198 | int err; | |
199 | ||
200 | nlh = __mnlg_msg_prepare(nlg, CTRL_CMD_GETFAMILY, | |
201 | NLM_F_REQUEST | NLM_F_ACK, GENL_ID_CTRL, 1); | |
7f57c8b7 | 202 | mnl_attr_put_u16(nlh, CTRL_ATTR_FAMILY_ID, nlg->id); |
a3c4b484 JP |
203 | |
204 | err = mnlg_socket_send(nlg, nlh); | |
205 | if (err < 0) | |
206 | return err; | |
207 | ||
208 | group_info.found = false; | |
209 | group_info.name = group_name; | |
210 | err = mnlg_socket_recv_run(nlg, get_group_id_cb, &group_info); | |
211 | if (err < 0) | |
212 | return err; | |
213 | ||
214 | if (!group_info.found) { | |
215 | errno = ENOENT; | |
216 | return -1; | |
217 | } | |
218 | ||
219 | err = mnl_socket_setsockopt(nlg->nl, NETLINK_ADD_MEMBERSHIP, | |
220 | &group_info.id, sizeof(group_info.id)); | |
221 | if (err < 0) | |
222 | return err; | |
223 | ||
224 | return 0; | |
225 | } | |
226 | ||
227 | static int get_family_id_attr_cb(const struct nlattr *attr, void *data) | |
228 | { | |
229 | const struct nlattr **tb = data; | |
230 | int type = mnl_attr_get_type(attr); | |
231 | ||
232 | if (mnl_attr_type_valid(attr, CTRL_ATTR_MAX) < 0) | |
233 | return MNL_CB_ERROR; | |
234 | ||
235 | if (type == CTRL_ATTR_FAMILY_ID && | |
236 | mnl_attr_validate(attr, MNL_TYPE_U16) < 0) | |
237 | return MNL_CB_ERROR; | |
238 | tb[type] = attr; | |
239 | return MNL_CB_OK; | |
240 | } | |
241 | ||
242 | static int get_family_id_cb(const struct nlmsghdr *nlh, void *data) | |
243 | { | |
244 | uint32_t *p_id = data; | |
245 | struct nlattr *tb[CTRL_ATTR_MAX + 1] = {}; | |
246 | struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh); | |
247 | ||
248 | mnl_attr_parse(nlh, sizeof(*genl), get_family_id_attr_cb, tb); | |
249 | if (!tb[CTRL_ATTR_FAMILY_ID]) | |
250 | return MNL_CB_ERROR; | |
251 | *p_id = mnl_attr_get_u16(tb[CTRL_ATTR_FAMILY_ID]); | |
252 | return MNL_CB_OK; | |
253 | } | |
254 | ||
255 | struct mnlg_socket *mnlg_socket_open(const char *family_name, uint8_t version) | |
256 | { | |
257 | struct mnlg_socket *nlg; | |
258 | struct nlmsghdr *nlh; | |
049c5853 | 259 | int one = 1; |
a3c4b484 JP |
260 | int err; |
261 | ||
262 | nlg = malloc(sizeof(*nlg)); | |
263 | if (!nlg) | |
264 | return NULL; | |
265 | ||
266 | nlg->buf = malloc(MNL_SOCKET_BUFFER_SIZE); | |
267 | if (!nlg->buf) | |
268 | goto err_buf_alloc; | |
269 | ||
270 | nlg->nl = mnl_socket_open(NETLINK_GENERIC); | |
271 | if (!nlg->nl) | |
272 | goto err_mnl_socket_open; | |
273 | ||
9e4a92e5 IV |
274 | /* Older kernels may no support capped/extended ACK reporting */ |
275 | mnl_socket_setsockopt(nlg->nl, NETLINK_CAP_ACK, &one, sizeof(one)); | |
276 | mnl_socket_setsockopt(nlg->nl, NETLINK_EXT_ACK, &one, sizeof(one)); | |
049c5853 | 277 | |
a3c4b484 JP |
278 | err = mnl_socket_bind(nlg->nl, 0, MNL_SOCKET_AUTOPID); |
279 | if (err < 0) | |
280 | goto err_mnl_socket_bind; | |
281 | ||
282 | nlg->portid = mnl_socket_get_portid(nlg->nl); | |
283 | ||
284 | nlh = __mnlg_msg_prepare(nlg, CTRL_CMD_GETFAMILY, | |
285 | NLM_F_REQUEST | NLM_F_ACK, GENL_ID_CTRL, 1); | |
286 | mnl_attr_put_strz(nlh, CTRL_ATTR_FAMILY_NAME, family_name); | |
287 | ||
288 | err = mnlg_socket_send(nlg, nlh); | |
289 | if (err < 0) | |
290 | goto err_mnlg_socket_send; | |
291 | ||
292 | err = mnlg_socket_recv_run(nlg, get_family_id_cb, &nlg->id); | |
293 | if (err < 0) | |
294 | goto err_mnlg_socket_recv_run; | |
295 | ||
296 | nlg->version = version; | |
297 | return nlg; | |
298 | ||
299 | err_mnlg_socket_recv_run: | |
300 | err_mnlg_socket_send: | |
301 | err_mnl_socket_bind: | |
302 | mnl_socket_close(nlg->nl); | |
303 | err_mnl_socket_open: | |
304 | free(nlg->buf); | |
305 | err_buf_alloc: | |
306 | free(nlg); | |
307 | return NULL; | |
308 | } | |
309 | ||
310 | void mnlg_socket_close(struct mnlg_socket *nlg) | |
311 | { | |
312 | mnl_socket_close(nlg->nl); | |
313 | free(nlg->buf); | |
314 | free(nlg); | |
315 | } |