]>
git.proxmox.com Git - mirror_iproute2.git/blob - genl/ctrl.c
2 * ctrl.c generic netlink controller
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.
9 * Authors: J Hadi Salim (hadi@cyberus.ca)
10 * Johannes Berg (johannes@sipsolutions.net)
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
23 #include "genl_utils.h"
25 #define GENL_MAX_FAM_OPS 256
26 #define GENL_MAX_FAM_GRPS 256
28 static int usage(void)
30 fprintf(stderr
,"Usage: ctrl <CMD>\n" \
31 "CMD := get <PARMS> | list | monitor\n" \
32 "PARMS := name <name> | id <id>\n" \
36 "\tctrl get name foobar\n" \
37 "\tctrl get id 0xF\n");
41 static void print_ctrl_cmd_flags(FILE *fp
, __u32 fl
)
43 fprintf(fp
, "\n\t\tCapabilities (0x%x):\n ", fl
);
50 if (fl
& GENL_ADMIN_PERM
)
51 fprintf(fp
, " requires admin permission;");
52 if (fl
& GENL_CMD_CAP_DO
)
53 fprintf(fp
, " can doit;");
54 if (fl
& GENL_CMD_CAP_DUMP
)
55 fprintf(fp
, " can dumpit;");
56 if (fl
& GENL_CMD_CAP_HASPOL
)
57 fprintf(fp
, " has policy");
62 static int print_ctrl_cmds(FILE *fp
, struct rtattr
*arg
, __u32 ctrl_ver
)
64 struct rtattr
*tb
[CTRL_ATTR_OP_MAX
+ 1];
69 parse_rtattr_nested(tb
, CTRL_ATTR_OP_MAX
, arg
);
70 if (tb
[CTRL_ATTR_OP_ID
]) {
71 __u32
*id
= RTA_DATA(tb
[CTRL_ATTR_OP_ID
]);
72 fprintf(fp
, " ID-0x%x ",*id
);
74 /* we are only gonna do this for newer version of the controller */
75 if (tb
[CTRL_ATTR_OP_FLAGS
] && ctrl_ver
>= 0x2) {
76 __u32
*fl
= RTA_DATA(tb
[CTRL_ATTR_OP_FLAGS
]);
77 print_ctrl_cmd_flags(fp
, *fl
);
83 static int print_ctrl_grp(FILE *fp
, struct rtattr
*arg
, __u32 ctrl_ver
)
85 struct rtattr
*tb
[CTRL_ATTR_MCAST_GRP_MAX
+ 1];
90 parse_rtattr_nested(tb
, CTRL_ATTR_MCAST_GRP_MAX
, arg
);
92 __u32
*id
= RTA_DATA(tb
[CTRL_ATTR_MCAST_GRP_ID
]);
93 fprintf(fp
, " ID-0x%x ",*id
);
96 char *name
= RTA_DATA(tb
[CTRL_ATTR_MCAST_GRP_NAME
]);
97 fprintf(fp
, " name: %s ", name
);
104 * The controller sends one nlmsg per family
106 static int print_ctrl(struct rtnl_ctrl_data
*ctrl
,
107 struct nlmsghdr
*n
, void *arg
)
109 struct rtattr
*tb
[CTRL_ATTR_MAX
+ 1];
110 struct genlmsghdr
*ghdr
= NLMSG_DATA(n
);
111 int len
= n
->nlmsg_len
;
112 struct rtattr
*attrs
;
113 FILE *fp
= (FILE *) arg
;
116 if (n
->nlmsg_type
!= GENL_ID_CTRL
) {
117 fprintf(stderr
, "Not a controller message, nlmsg_len=%d "
118 "nlmsg_type=0x%x\n", n
->nlmsg_len
, n
->nlmsg_type
);
122 if (ghdr
->cmd
!= CTRL_CMD_GETFAMILY
&&
123 ghdr
->cmd
!= CTRL_CMD_DELFAMILY
&&
124 ghdr
->cmd
!= CTRL_CMD_NEWFAMILY
&&
125 ghdr
->cmd
!= CTRL_CMD_NEWMCAST_GRP
&&
126 ghdr
->cmd
!= CTRL_CMD_DELMCAST_GRP
) {
127 fprintf(stderr
, "Unknown controller command %d\n", ghdr
->cmd
);
131 len
-= NLMSG_LENGTH(GENL_HDRLEN
);
134 fprintf(stderr
, "wrong controller message len %d\n", len
);
138 attrs
= (struct rtattr
*) ((char *) ghdr
+ GENL_HDRLEN
);
139 parse_rtattr(tb
, CTRL_ATTR_MAX
, attrs
, len
);
141 if (tb
[CTRL_ATTR_FAMILY_NAME
]) {
142 char *name
= RTA_DATA(tb
[CTRL_ATTR_FAMILY_NAME
]);
143 fprintf(fp
, "\nName: %s\n",name
);
145 if (tb
[CTRL_ATTR_FAMILY_ID
]) {
146 __u16
*id
= RTA_DATA(tb
[CTRL_ATTR_FAMILY_ID
]);
147 fprintf(fp
, "\tID: 0x%x ",*id
);
149 if (tb
[CTRL_ATTR_VERSION
]) {
150 __u32
*v
= RTA_DATA(tb
[CTRL_ATTR_VERSION
]);
151 fprintf(fp
, " Version: 0x%x ",*v
);
154 if (tb
[CTRL_ATTR_HDRSIZE
]) {
155 __u32
*h
= RTA_DATA(tb
[CTRL_ATTR_HDRSIZE
]);
156 fprintf(fp
, " header size: %d ",*h
);
158 if (tb
[CTRL_ATTR_MAXATTR
]) {
159 __u32
*ma
= RTA_DATA(tb
[CTRL_ATTR_MAXATTR
]);
160 fprintf(fp
, " max attribs: %d ",*ma
);
162 /* end of family definitions .. */
164 if (tb
[CTRL_ATTR_OPS
]) {
165 struct rtattr
*tb2
[GENL_MAX_FAM_OPS
];
167 parse_rtattr_nested(tb2
, GENL_MAX_FAM_OPS
, tb
[CTRL_ATTR_OPS
]);
168 fprintf(fp
, "\tcommands supported: \n");
169 for (i
= 0; i
< GENL_MAX_FAM_OPS
; i
++) {
171 fprintf(fp
, "\t\t#%d: ", i
);
172 if (0 > print_ctrl_cmds(fp
, tb2
[i
], ctrl_v
)) {
173 fprintf(fp
, "Error printing command\n");
175 /* for next command */
180 /* end of family::cmds definitions .. */
184 if (tb
[CTRL_ATTR_MCAST_GROUPS
]) {
185 struct rtattr
*tb2
[GENL_MAX_FAM_GRPS
+ 1];
188 parse_rtattr_nested(tb2
, GENL_MAX_FAM_GRPS
,
189 tb
[CTRL_ATTR_MCAST_GROUPS
]);
190 fprintf(fp
, "\tmulticast groups:\n");
192 for (i
= 0; i
< GENL_MAX_FAM_GRPS
; i
++) {
194 fprintf(fp
, "\t\t#%d: ", i
);
195 if (0 > print_ctrl_grp(fp
, tb2
[i
], ctrl_v
))
196 fprintf(fp
, "Error printing group\n");
202 /* end of family::groups definitions .. */
210 static int print_ctrl2(struct nlmsghdr
*n
, void *arg
)
212 return print_ctrl(NULL
, n
, arg
);
215 static int ctrl_list(int cmd
, int argc
, char **argv
)
217 struct rtnl_handle rth
;
225 .n
.nlmsg_len
= NLMSG_LENGTH(GENL_HDRLEN
),
226 .n
.nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
,
227 .n
.nlmsg_type
= GENL_ID_CTRL
,
228 .g
.cmd
= CTRL_CMD_GETFAMILY
,
230 struct nlmsghdr
*nlh
= &req
.n
;
231 struct nlmsghdr
*answer
= NULL
;
233 if (rtnl_open_byproto(&rth
, 0, NETLINK_GENERIC
) < 0) {
234 fprintf(stderr
, "Cannot open generic netlink socket\n");
238 if (cmd
== CTRL_CMD_GETFAMILY
) {
240 fprintf(stderr
, "Wrong number of params\n");
244 if (matches(*argv
, "name") == 0) {
246 strlcpy(d
, *argv
, sizeof(d
));
247 addattr_l(nlh
, 128, CTRL_ATTR_FAMILY_NAME
,
249 } else if (matches(*argv
, "id") == 0) {
252 if (get_u16(&id
, *argv
, 0)) {
253 fprintf(stderr
, "Illegal \"id\"\n");
257 addattr_l(nlh
, 128, CTRL_ATTR_FAMILY_ID
, &id
, 2);
260 fprintf(stderr
, "Wrong params\n");
264 if (rtnl_talk(&rth
, nlh
, &answer
) < 0) {
265 fprintf(stderr
, "Error talking to the kernel\n");
269 if (print_ctrl2(answer
, (void *) stdout
) < 0) {
270 fprintf(stderr
, "Dump terminated\n");
276 if (cmd
== CTRL_CMD_UNSPEC
) {
277 nlh
->nlmsg_flags
= NLM_F_ROOT
|NLM_F_MATCH
|NLM_F_REQUEST
;
278 nlh
->nlmsg_seq
= rth
.dump
= ++rth
.seq
;
280 if (rtnl_send(&rth
, nlh
, nlh
->nlmsg_len
) < 0) {
281 perror("Failed to send dump request\n");
285 rtnl_dump_filter(&rth
, print_ctrl2
, stdout
);
296 static int ctrl_listen(int argc
, char **argv
)
298 struct rtnl_handle rth
;
300 if (rtnl_open_byproto(&rth
, nl_mgrp(GENL_ID_CTRL
), NETLINK_GENERIC
) < 0) {
301 fprintf(stderr
, "Canot open generic netlink socket\n");
305 if (rtnl_listen(&rth
, print_ctrl
, (void *) stdout
) < 0)
311 static int parse_ctrl(struct genl_util
*a
, int argc
, char **argv
)
315 fprintf(stderr
, "wrong controller params\n");
319 if (matches(*argv
, "monitor") == 0)
320 return ctrl_listen(argc
-1, argv
+1);
321 if (matches(*argv
, "get") == 0)
322 return ctrl_list(CTRL_CMD_GETFAMILY
, argc
-1, argv
+1);
323 if (matches(*argv
, "list") == 0 ||
324 matches(*argv
, "show") == 0 ||
325 matches(*argv
, "lst") == 0)
326 return ctrl_list(CTRL_CMD_UNSPEC
, argc
-1, argv
+1);
327 if (matches(*argv
, "help") == 0)
330 fprintf(stderr
, "ctrl command \"%s\" is unknown, try \"ctrl help\".\n",
336 struct genl_util ctrl_genl_util
= {
338 .parse_genlopt
= parse_ctrl
,
339 .print_genlopt
= print_ctrl2
,