]> git.proxmox.com Git - mirror_iproute2.git/blob - genl/ctrl.c
6133336ab435539de0df6228a47a8a3086ed40f6
[mirror_iproute2.git] / genl / ctrl.c
1 /*
2 * ctrl.c generic netlink controller
3 *
4 * This program is free software; you can distribute 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: J Hadi Salim (hadi@cyberus.ca)
10 * Johannes Berg (johannes@sipsolutions.net)
11 */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <fcntl.h>
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
20 #include <string.h>
21
22 #include "utils.h"
23 #include "genl_utils.h"
24
25 #define GENL_MAX_FAM_OPS 256
26 #define GENL_MAX_FAM_GRPS 256
27
28 static int usage(void)
29 {
30 fprintf(stderr,"Usage: ctrl <CMD>\n" \
31 "CMD := get <PARMS> | list | monitor\n" \
32 "PARMS := name <name> | id <id>\n" \
33 "Examples:\n" \
34 "\tctrl ls\n" \
35 "\tctrl monitor\n" \
36 "\tctrl get name foobar\n" \
37 "\tctrl get id 0xF\n");
38 return -1;
39 }
40
41 int genl_ctrl_resolve_family(const char *family)
42 {
43 struct rtnl_handle rth;
44 int ret = 0;
45 struct {
46 struct nlmsghdr n;
47 struct genlmsghdr g;
48 char buf[4096];
49 } req = {
50 .n.nlmsg_len = NLMSG_LENGTH(GENL_HDRLEN),
51 .n.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK,
52 .n.nlmsg_type = GENL_ID_CTRL,
53 .g.cmd = CTRL_CMD_GETFAMILY,
54 };
55 struct nlmsghdr *nlh = &req.n;
56 struct genlmsghdr *ghdr = &req.g;
57 struct nlmsghdr *answer = NULL;
58
59 if (rtnl_open_byproto(&rth, 0, NETLINK_GENERIC) < 0) {
60 fprintf(stderr, "Cannot open generic netlink socket\n");
61 exit(1);
62 }
63
64 addattr_l(nlh, 128, CTRL_ATTR_FAMILY_NAME, family, strlen(family) + 1);
65
66 if (rtnl_talk(&rth, nlh, &answer) < 0) {
67 fprintf(stderr, "Error talking to the kernel\n");
68 goto errout;
69 }
70
71 {
72 struct rtattr *tb[CTRL_ATTR_MAX + 1];
73 int len = answer->nlmsg_len;
74 struct rtattr *attrs;
75
76 if (answer->nlmsg_type != GENL_ID_CTRL) {
77 fprintf(stderr, "Not a controller message, nlmsg_len=%d "
78 "nlmsg_type=0x%x\n", answer->nlmsg_len, answer->nlmsg_type);
79 goto errout;
80 }
81
82 if (ghdr->cmd != CTRL_CMD_NEWFAMILY) {
83 fprintf(stderr, "Unknown controller command %d\n", ghdr->cmd);
84 goto errout;
85 }
86
87 len -= NLMSG_LENGTH(GENL_HDRLEN);
88
89 if (len < 0) {
90 fprintf(stderr, "wrong controller message len %d\n", len);
91 free(answer);
92 return -1;
93 }
94
95 attrs = (struct rtattr *) ((char *) answer + NLMSG_LENGTH(GENL_HDRLEN));
96 parse_rtattr(tb, CTRL_ATTR_MAX, attrs, len);
97
98 if (tb[CTRL_ATTR_FAMILY_ID] == NULL) {
99 fprintf(stderr, "Missing family id TLV\n");
100 goto errout;
101 }
102
103 ret = rta_getattr_u16(tb[CTRL_ATTR_FAMILY_ID]);
104 }
105
106 errout:
107 free(answer);
108 rtnl_close(&rth);
109 return ret;
110 }
111
112 static void print_ctrl_cmd_flags(FILE *fp, __u32 fl)
113 {
114 fprintf(fp, "\n\t\tCapabilities (0x%x):\n ", fl);
115 if (!fl) {
116 fprintf(fp, "\n");
117 return;
118 }
119 fprintf(fp, "\t\t ");
120
121 if (fl & GENL_ADMIN_PERM)
122 fprintf(fp, " requires admin permission;");
123 if (fl & GENL_CMD_CAP_DO)
124 fprintf(fp, " can doit;");
125 if (fl & GENL_CMD_CAP_DUMP)
126 fprintf(fp, " can dumpit;");
127 if (fl & GENL_CMD_CAP_HASPOL)
128 fprintf(fp, " has policy");
129
130 fprintf(fp, "\n");
131 }
132
133 static int print_ctrl_cmds(FILE *fp, struct rtattr *arg, __u32 ctrl_ver)
134 {
135 struct rtattr *tb[CTRL_ATTR_OP_MAX + 1];
136
137 if (arg == NULL)
138 return -1;
139
140 parse_rtattr_nested(tb, CTRL_ATTR_OP_MAX, arg);
141 if (tb[CTRL_ATTR_OP_ID]) {
142 __u32 *id = RTA_DATA(tb[CTRL_ATTR_OP_ID]);
143 fprintf(fp, " ID-0x%x ",*id);
144 }
145 /* we are only gonna do this for newer version of the controller */
146 if (tb[CTRL_ATTR_OP_FLAGS] && ctrl_ver >= 0x2) {
147 __u32 *fl = RTA_DATA(tb[CTRL_ATTR_OP_FLAGS]);
148 print_ctrl_cmd_flags(fp, *fl);
149 }
150 return 0;
151
152 }
153
154 static int print_ctrl_grp(FILE *fp, struct rtattr *arg, __u32 ctrl_ver)
155 {
156 struct rtattr *tb[CTRL_ATTR_MCAST_GRP_MAX + 1];
157
158 if (arg == NULL)
159 return -1;
160
161 parse_rtattr_nested(tb, CTRL_ATTR_MCAST_GRP_MAX, arg);
162 if (tb[2]) {
163 __u32 *id = RTA_DATA(tb[CTRL_ATTR_MCAST_GRP_ID]);
164 fprintf(fp, " ID-0x%x ",*id);
165 }
166 if (tb[1]) {
167 char *name = RTA_DATA(tb[CTRL_ATTR_MCAST_GRP_NAME]);
168 fprintf(fp, " name: %s ", name);
169 }
170 return 0;
171
172 }
173
174 /*
175 * The controller sends one nlmsg per family
176 */
177 static int print_ctrl(struct rtnl_ctrl_data *ctrl,
178 struct nlmsghdr *n, void *arg)
179 {
180 struct rtattr *tb[CTRL_ATTR_MAX + 1];
181 struct genlmsghdr *ghdr = NLMSG_DATA(n);
182 int len = n->nlmsg_len;
183 struct rtattr *attrs;
184 FILE *fp = (FILE *) arg;
185 __u32 ctrl_v = 0x1;
186
187 if (n->nlmsg_type != GENL_ID_CTRL) {
188 fprintf(stderr, "Not a controller message, nlmsg_len=%d "
189 "nlmsg_type=0x%x\n", n->nlmsg_len, n->nlmsg_type);
190 return 0;
191 }
192
193 if (ghdr->cmd != CTRL_CMD_GETFAMILY &&
194 ghdr->cmd != CTRL_CMD_DELFAMILY &&
195 ghdr->cmd != CTRL_CMD_NEWFAMILY &&
196 ghdr->cmd != CTRL_CMD_NEWMCAST_GRP &&
197 ghdr->cmd != CTRL_CMD_DELMCAST_GRP) {
198 fprintf(stderr, "Unknown controller command %d\n", ghdr->cmd);
199 return 0;
200 }
201
202 len -= NLMSG_LENGTH(GENL_HDRLEN);
203
204 if (len < 0) {
205 fprintf(stderr, "wrong controller message len %d\n", len);
206 return -1;
207 }
208
209 attrs = (struct rtattr *) ((char *) ghdr + GENL_HDRLEN);
210 parse_rtattr(tb, CTRL_ATTR_MAX, attrs, len);
211
212 if (tb[CTRL_ATTR_FAMILY_NAME]) {
213 char *name = RTA_DATA(tb[CTRL_ATTR_FAMILY_NAME]);
214 fprintf(fp, "\nName: %s\n",name);
215 }
216 if (tb[CTRL_ATTR_FAMILY_ID]) {
217 __u16 *id = RTA_DATA(tb[CTRL_ATTR_FAMILY_ID]);
218 fprintf(fp, "\tID: 0x%x ",*id);
219 }
220 if (tb[CTRL_ATTR_VERSION]) {
221 __u32 *v = RTA_DATA(tb[CTRL_ATTR_VERSION]);
222 fprintf(fp, " Version: 0x%x ",*v);
223 ctrl_v = *v;
224 }
225 if (tb[CTRL_ATTR_HDRSIZE]) {
226 __u32 *h = RTA_DATA(tb[CTRL_ATTR_HDRSIZE]);
227 fprintf(fp, " header size: %d ",*h);
228 }
229 if (tb[CTRL_ATTR_MAXATTR]) {
230 __u32 *ma = RTA_DATA(tb[CTRL_ATTR_MAXATTR]);
231 fprintf(fp, " max attribs: %d ",*ma);
232 }
233 /* end of family definitions .. */
234 fprintf(fp,"\n");
235 if (tb[CTRL_ATTR_OPS]) {
236 struct rtattr *tb2[GENL_MAX_FAM_OPS];
237 int i=0;
238 parse_rtattr_nested(tb2, GENL_MAX_FAM_OPS, tb[CTRL_ATTR_OPS]);
239 fprintf(fp, "\tcommands supported: \n");
240 for (i = 0; i < GENL_MAX_FAM_OPS; i++) {
241 if (tb2[i]) {
242 fprintf(fp, "\t\t#%d: ", i);
243 if (0 > print_ctrl_cmds(fp, tb2[i], ctrl_v)) {
244 fprintf(fp, "Error printing command\n");
245 }
246 /* for next command */
247 fprintf(fp,"\n");
248 }
249 }
250
251 /* end of family::cmds definitions .. */
252 fprintf(fp,"\n");
253 }
254
255 if (tb[CTRL_ATTR_MCAST_GROUPS]) {
256 struct rtattr *tb2[GENL_MAX_FAM_GRPS + 1];
257 int i;
258
259 parse_rtattr_nested(tb2, GENL_MAX_FAM_GRPS,
260 tb[CTRL_ATTR_MCAST_GROUPS]);
261 fprintf(fp, "\tmulticast groups:\n");
262
263 for (i = 0; i < GENL_MAX_FAM_GRPS; i++) {
264 if (tb2[i]) {
265 fprintf(fp, "\t\t#%d: ", i);
266 if (0 > print_ctrl_grp(fp, tb2[i], ctrl_v))
267 fprintf(fp, "Error printing group\n");
268 /* for next group */
269 fprintf(fp,"\n");
270 }
271 }
272
273 /* end of family::groups definitions .. */
274 fprintf(fp,"\n");
275 }
276
277 fflush(fp);
278 return 0;
279 }
280
281 static int print_ctrl2(struct nlmsghdr *n, void *arg)
282 {
283 return print_ctrl(NULL, n, arg);
284 }
285
286 static int ctrl_list(int cmd, int argc, char **argv)
287 {
288 struct rtnl_handle rth;
289 int ret = -1;
290 char d[GENL_NAMSIZ];
291 struct {
292 struct nlmsghdr n;
293 struct genlmsghdr g;
294 char buf[4096];
295 } req = {
296 .n.nlmsg_len = NLMSG_LENGTH(GENL_HDRLEN),
297 .n.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK,
298 .n.nlmsg_type = GENL_ID_CTRL,
299 .g.cmd = CTRL_CMD_GETFAMILY,
300 };
301 struct nlmsghdr *nlh = &req.n;
302 struct nlmsghdr *answer = NULL;
303
304 if (rtnl_open_byproto(&rth, 0, NETLINK_GENERIC) < 0) {
305 fprintf(stderr, "Cannot open generic netlink socket\n");
306 exit(1);
307 }
308
309 if (cmd == CTRL_CMD_GETFAMILY) {
310 if (argc != 2) {
311 fprintf(stderr, "Wrong number of params\n");
312 return -1;
313 }
314
315 if (matches(*argv, "name") == 0) {
316 NEXT_ARG();
317 strlcpy(d, *argv, sizeof(d));
318 addattr_l(nlh, 128, CTRL_ATTR_FAMILY_NAME,
319 d, strlen(d) + 1);
320 } else if (matches(*argv, "id") == 0) {
321 __u16 id;
322 NEXT_ARG();
323 if (get_u16(&id, *argv, 0)) {
324 fprintf(stderr, "Illegal \"id\"\n");
325 goto ctrl_done;
326 }
327
328 addattr_l(nlh, 128, CTRL_ATTR_FAMILY_ID, &id, 2);
329
330 } else {
331 fprintf(stderr, "Wrong params\n");
332 goto ctrl_done;
333 }
334
335 if (rtnl_talk(&rth, nlh, &answer) < 0) {
336 fprintf(stderr, "Error talking to the kernel\n");
337 goto ctrl_done;
338 }
339
340 if (print_ctrl2(answer, (void *) stdout) < 0) {
341 fprintf(stderr, "Dump terminated\n");
342 goto ctrl_done;
343 }
344
345 }
346
347 if (cmd == CTRL_CMD_UNSPEC) {
348 nlh->nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
349 nlh->nlmsg_seq = rth.dump = ++rth.seq;
350
351 if (rtnl_send(&rth, nlh, nlh->nlmsg_len) < 0) {
352 perror("Failed to send dump request\n");
353 goto ctrl_done;
354 }
355
356 rtnl_dump_filter(&rth, print_ctrl2, stdout);
357
358 }
359
360 ret = 0;
361 ctrl_done:
362 free(answer);
363 rtnl_close(&rth);
364 return ret;
365 }
366
367 static int ctrl_listen(int argc, char **argv)
368 {
369 struct rtnl_handle rth;
370
371 if (rtnl_open_byproto(&rth, nl_mgrp(GENL_ID_CTRL), NETLINK_GENERIC) < 0) {
372 fprintf(stderr, "Canot open generic netlink socket\n");
373 return -1;
374 }
375
376 if (rtnl_listen(&rth, print_ctrl, (void *) stdout) < 0)
377 return -1;
378
379 return 0;
380 }
381
382 static int parse_ctrl(struct genl_util *a, int argc, char **argv)
383 {
384 argv++;
385 if (--argc <= 0) {
386 fprintf(stderr, "wrong controller params\n");
387 return -1;
388 }
389
390 if (matches(*argv, "monitor") == 0)
391 return ctrl_listen(argc-1, argv+1);
392 if (matches(*argv, "get") == 0)
393 return ctrl_list(CTRL_CMD_GETFAMILY, argc-1, argv+1);
394 if (matches(*argv, "list") == 0 ||
395 matches(*argv, "show") == 0 ||
396 matches(*argv, "lst") == 0)
397 return ctrl_list(CTRL_CMD_UNSPEC, argc-1, argv+1);
398 if (matches(*argv, "help") == 0)
399 return usage();
400
401 fprintf(stderr, "ctrl command \"%s\" is unknown, try \"ctrl help\".\n",
402 *argv);
403
404 return -1;
405 }
406
407 struct genl_util ctrl_genl_util = {
408 .name = "ctrl",
409 .parse_genlopt = parse_ctrl,
410 .print_genlopt = print_ctrl2,
411 };