Changeset View
Changeset View
Standalone View
Standalone View
usr.bin/genl/genl.c
| Show First 20 Lines • Show All 92 Lines • ▼ Show 20 Lines | |||||
| * ambiguous memory leaks. Second reason is that genl(1) usage of the data is | * ambiguous memory leaks. Second reason is that genl(1) usage of the data is | ||||
| * more extended than typical application that cares only of its own family and | * more extended than typical application that cares only of its own family and | ||||
| * usually one group. We are going to cycle around all available. | * usually one group. We are going to cycle around all available. | ||||
| */ | */ | ||||
| struct genl_ctrl_op { | struct genl_ctrl_op { | ||||
| uint32_t id; | uint32_t id; | ||||
| uint32_t flags; | uint32_t flags; | ||||
| }; | }; | ||||
| struct genl_ctrl_ops { | |||||
| uint32_t num_ops; | |||||
| struct genl_ctrl_op **ops; | |||||
| }; | |||||
| static struct snl_attr_parser nla_p_getops[] = { | static struct snl_attr_parser nla_p_getops[] = { | ||||
| #define _OUT(_field) offsetof(struct genl_ctrl_op, _field) | #define _OUT(_field) offsetof(struct genl_ctrl_op, _field) | ||||
| { | { | ||||
| .type = CTRL_ATTR_OP_ID, | .type = CTRL_ATTR_OP_ID, | ||||
| .off = _OUT(id), | .off = _OUT(id), | ||||
| .cb = snl_attr_get_uint32 | .cb = snl_attr_get_uint32 | ||||
| }, | }, | ||||
| { | { | ||||
| .type = CTRL_ATTR_OP_FLAGS, | .type = CTRL_ATTR_OP_FLAGS, | ||||
| .off = _OUT(flags), | .off = _OUT(flags), | ||||
| .cb = snl_attr_get_uint32 | .cb = snl_attr_get_uint32 | ||||
| }, | }, | ||||
| #undef _OUT | #undef _OUT | ||||
| }; | }; | ||||
| SNL_DECLARE_ATTR_PARSER_EXT(genl_ctrl_op_parser, sizeof(struct genl_ctrl_op), | SNL_DECLARE_ATTR_PARSER_EXT(genl_ctrl_op_parser, sizeof(struct genl_ctrl_op), | ||||
| nla_p_getops, NULL); | nla_p_getops, NULL); | ||||
| struct genl_mcast_group { | struct genl_mcast_group { | ||||
| uint32_t id; | uint32_t id; | ||||
| const char *name; | const char *name; | ||||
| }; | }; | ||||
| struct genl_mcast_groups { | |||||
| uint32_t num_groups; | |||||
| struct genl_mcast_group **groups; | |||||
| }; | |||||
| static struct snl_attr_parser nla_p_getmc[] = { | static struct snl_attr_parser nla_p_getmc[] = { | ||||
| #define _OUT(_field) offsetof(struct genl_mcast_group, _field) | #define _OUT(_field) offsetof(struct genl_mcast_group, _field) | ||||
| { | { | ||||
| .type = CTRL_ATTR_MCAST_GRP_NAME, | .type = CTRL_ATTR_MCAST_GRP_NAME, | ||||
| .off = _OUT(name), | .off = _OUT(name), | ||||
| .cb = snl_attr_get_stringn, | .cb = snl_attr_get_stringn, | ||||
| }, | }, | ||||
| { | { | ||||
| .type = CTRL_ATTR_MCAST_GRP_ID, | .type = CTRL_ATTR_MCAST_GRP_ID, | ||||
| .off = _OUT(id), | .off = _OUT(id), | ||||
| .cb = snl_attr_get_uint32, | .cb = snl_attr_get_uint32, | ||||
| }, | }, | ||||
| #undef _OUT | #undef _OUT | ||||
| }; | }; | ||||
| SNL_DECLARE_ATTR_PARSER_EXT(genl_mc_parser, sizeof(struct genl_mcast_group), | SNL_DECLARE_ATTR_PARSER_EXT(genl_mc_parser, sizeof(struct genl_mcast_group), | ||||
| nla_p_getmc, NULL); | nla_p_getmc, NULL); | ||||
| struct genl_family { | struct genl_family { | ||||
| uint16_t id; | uint16_t id; | ||||
| const char *name; | const char *name; | ||||
| uint32_t version; | uint32_t version; | ||||
| uint32_t hdrsize; | uint32_t hdrsize; | ||||
| uint32_t max_attr; | uint32_t max_attr; | ||||
| struct genl_mcast_groups mcast_groups; | struct snl_parray mcast_groups; | ||||
| struct genl_ctrl_ops ops; | struct snl_parray ops; | ||||
| }; | }; | ||||
| static struct snl_attr_parser nla_p_getfamily[] = { | static struct snl_attr_parser nla_p_getfamily[] = { | ||||
| #define _OUT(_field) offsetof(struct genl_family, _field) | #define _OUT(_field) offsetof(struct genl_family, _field) | ||||
| { | { | ||||
| .type = CTRL_ATTR_FAMILY_ID, | .type = CTRL_ATTR_FAMILY_ID, | ||||
| .off = _OUT(id), | .off = _OUT(id), | ||||
| .cb = snl_attr_get_uint16, | .cb = snl_attr_get_uint16, | ||||
| Show All 40 Lines | |||||
| } op_caps[] = { | } op_caps[] = { | ||||
| { GENL_ADMIN_PERM, "requires admin permission" }, | { GENL_ADMIN_PERM, "requires admin permission" }, | ||||
| { GENL_CMD_CAP_DO, "can modify" }, | { GENL_CMD_CAP_DO, "can modify" }, | ||||
| { GENL_CMD_CAP_DUMP, "can get/dump" }, | { GENL_CMD_CAP_DUMP, "can get/dump" }, | ||||
| { GENL_CMD_CAP_HASPOL, "has policy" }, | { GENL_CMD_CAP_HASPOL, "has policy" }, | ||||
| }; | }; | ||||
| static void | static void | ||||
| dump_operations(struct genl_ctrl_ops *ops) | dump_operations(struct snl_parray *ops) | ||||
| { | { | ||||
| if (ops->num_ops == 0) | struct genl_ctrl_op *op; | ||||
| if (ops->count == 0) | |||||
| return; | return; | ||||
| printf("\tsupported operations: \n"); | printf("\tsupported operations: \n"); | ||||
| for (uint32_t i = 0; i < ops->num_ops; i++) { | for (uint32_t i = 0; i < ops->count; i++) { | ||||
| bool p = true; | bool p = true; | ||||
| op = ops->items[i]; | |||||
| printf("\t - ID: %#02x, Capabilities: %#02x", | printf("\t - ID: %#02x, Capabilities: %#02x", | ||||
| ops->ops[i]->id, | op->id, op->flags); | ||||
| ops->ops[i]->flags); | |||||
| for (size_t j = 0; j < nitems(op_caps); j++) | for (size_t j = 0; j < nitems(op_caps); j++) | ||||
| if ((ops->ops[i]->flags & op_caps[j].flag) == | if ((op->flags & op_caps[j].flag) == | ||||
| op_caps[j].flag) { | op_caps[j].flag) { | ||||
| printf("%s%s", p ? " (" : "; ", | printf("%s%s", p ? " (" : "; ", | ||||
| op_caps[j].str); | op_caps[j].str); | ||||
| p = false; | p = false; | ||||
| } | } | ||||
| printf("%s\n", p ? "" : ")"); | printf("%s\n", p ? "" : ")"); | ||||
| } | } | ||||
| } | } | ||||
| static void | static void | ||||
| dump_mcast_groups(struct genl_mcast_groups *mcast_groups) | dump_mcast_groups(struct snl_parray *mcast_groups) | ||||
| { | { | ||||
| if (mcast_groups->num_groups == 0) | struct genl_mcast_group *group; | ||||
| if (mcast_groups->count == 0) | |||||
| return; | return; | ||||
| printf("\tmulticast groups: \n"); | printf("\tmulticast groups: \n"); | ||||
| for (uint32_t i = 0; i < mcast_groups->num_groups; i++) | for (uint32_t i = 0; i < mcast_groups->count; i++) { | ||||
| group = mcast_groups->items[i]; | |||||
| printf("\t - ID: %#02x, Name: %s\n", | printf("\t - ID: %#02x, Name: %s\n", | ||||
| mcast_groups->groups[i]->id, | group->id, group->name); | ||||
| mcast_groups->groups[i]->name); | |||||
| } | } | ||||
| } | |||||
| static void | static void | ||||
| usage(void) | usage(void) | ||||
| { | { | ||||
| fprintf(stderr, "Usage: %s\n", getprogname()); | fprintf(stderr, "Usage: %s\n", getprogname()); | ||||
| for (size_t i = 0; i < nitems(cmds); i++) | for (size_t i = 0; i < nitems(cmds); i++) | ||||
| fprintf(stderr, " %s %s\n", getprogname(), cmds[i].usage); | fprintf(stderr, " %s %s\n", getprogname(), cmds[i].usage); | ||||
| } | } | ||||
| ▲ Show 20 Lines • Show All 41 Lines • ▼ Show 20 Lines | |||||
| } | } | ||||
| /* Populated by monitor_mcast() and may be used by protocol parser callbacks. */ | /* Populated by monitor_mcast() and may be used by protocol parser callbacks. */ | ||||
| static struct genl_family attrs; | static struct genl_family attrs; | ||||
| const char * | const char * | ||||
| group_name(uint32_t id) | group_name(uint32_t id) | ||||
| { | { | ||||
| for (u_int i = 0; i < attrs.mcast_groups.num_groups; i++) | struct genl_mcast_group *group; | ||||
| if (attrs.mcast_groups.groups[i]->id == id) | |||||
| return (attrs.mcast_groups.groups[i]->name); | for (u_int i = 0; i < attrs.mcast_groups.count; i++) { | ||||
| group = attrs.mcast_groups.items[i]; | |||||
| if (group->id == id) | |||||
| return (group->name); | |||||
| } | |||||
| return ("???"); | return ("???"); | ||||
| } | } | ||||
| static int | static int | ||||
| monitor_mcast(int argc, char **argv) | monitor_mcast(int argc, char **argv) | ||||
| { | { | ||||
| struct snl_state ss; | struct snl_state ss; | ||||
| struct snl_writer nw; | struct snl_writer nw; | ||||
| Show All 23 Lines | monitor_mcast(int argc, char **argv) | ||||
| if (hdr->nlmsg_type == NLMSG_ERROR) | if (hdr->nlmsg_type == NLMSG_ERROR) | ||||
| err(EXIT_FAILURE, "netlink(4) returned error"); | err(EXIT_FAILURE, "netlink(4) returned error"); | ||||
| memset(&attrs, 0, sizeof(attrs)); | memset(&attrs, 0, sizeof(attrs)); | ||||
| if (!snl_parse_nlmsg(&ss, hdr, &genl_family_parser, &attrs)) | if (!snl_parse_nlmsg(&ss, hdr, &genl_family_parser, &attrs)) | ||||
| err(EXIT_FAILURE, "snl_parse_nlmsg CTRL_CMD_GETFAMILY"); | err(EXIT_FAILURE, "snl_parse_nlmsg CTRL_CMD_GETFAMILY"); | ||||
| if (argc == 1) | if (argc == 1) | ||||
| all = true; | all = true; | ||||
| for (u_int i = 0; i < attrs.mcast_groups.num_groups; i++) { | for (u_int i = 0; i < attrs.mcast_groups.count; i++) { | ||||
| struct genl_mcast_group *group; | |||||
| group = attrs.mcast_groups.items[i]; | |||||
| if (all || | if (all || | ||||
| strcmp(attrs.mcast_groups.groups[i]->name, argv[1]) == 0) { | strcmp(group->name, argv[1]) == 0) { | ||||
| found = true; | found = true; | ||||
| if (setsockopt(ss.fd, SOL_NETLINK, | if (setsockopt(ss.fd, SOL_NETLINK, | ||||
| NETLINK_ADD_MEMBERSHIP, | NETLINK_ADD_MEMBERSHIP, | ||||
| &attrs.mcast_groups.groups[i]->id, | &group->id, sizeof(group->id)) | ||||
| sizeof(attrs.mcast_groups.groups[i]->id)) | |||||
| == -1) | == -1) | ||||
| err(EXIT_FAILURE, "Cannot subscribe to command " | err(EXIT_FAILURE, "Cannot subscribe to command " | ||||
| "notify"); | "notify"); | ||||
| if (!all) | if (!all) | ||||
| break; | break; | ||||
| } | } | ||||
| } | } | ||||
| if (!found) | if (!found) | ||||
| ▲ Show 20 Lines • Show All 82 Lines • Show Last 20 Lines | |||||