1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2022 Alexander V. Chernikov <melifaro@FreeBSD.org> 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 #ifndef _NETLINK_NETLINK_SNL_GENERIC_H_ 28 #define _NETLINK_NETLINK_SNL_GENERIC_H_ 29 30 #include <netlink/netlink.h> 31 #include <netlink/netlink_generic.h> 32 #include <netlink/netlink_snl.h> 33 34 /* Genetlink helpers */ 35 static inline struct nlmsghdr * 36 snl_create_genl_msg_request(struct snl_writer *nw, uint16_t genl_family, 37 uint8_t genl_cmd) 38 { 39 struct nlmsghdr *hdr; 40 struct genlmsghdr *ghdr; 41 42 assert(nw->hdr == NULL); 43 44 hdr = snl_reserve_msg_object(nw, struct nlmsghdr); 45 if (__predict_false(hdr == NULL)) 46 return (NULL); 47 hdr->nlmsg_type = genl_family; 48 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK; 49 ghdr = snl_reserve_msg_object(nw, struct genlmsghdr); 50 if (__predict_false(ghdr == NULL)) 51 return (NULL); 52 ghdr->cmd = genl_cmd; 53 nw->hdr = hdr; 54 55 return (hdr); 56 } 57 58 static struct snl_field_parser snl_fp_genl[] = {}; 59 60 #define SNL_DECLARE_GENL_PARSER(_name, _np) SNL_DECLARE_PARSER(_name,\ 61 struct genlmsghdr, snl_fp_genl, _np) 62 63 struct snl_genl_ctrl_mcast_group { 64 uint32_t mcast_grp_id; 65 const char *mcast_grp_name; 66 }; 67 68 struct snl_genl_ctrl_mcast_groups { 69 uint32_t num_groups; 70 struct snl_genl_ctrl_mcast_group **groups; 71 }; 72 73 #define _OUT(_field) offsetof(struct snl_genl_ctrl_mcast_group, _field) 74 static struct snl_attr_parser _nla_p_getmc[] = { 75 { 76 .type = CTRL_ATTR_MCAST_GRP_NAME, 77 .off = _OUT(mcast_grp_name), 78 .cb = snl_attr_get_string, 79 }, 80 { 81 .type = CTRL_ATTR_MCAST_GRP_ID, 82 .off = _OUT(mcast_grp_id), 83 .cb = snl_attr_get_uint32, 84 }, 85 }; 86 #undef _OUT 87 SNL_DECLARE_ATTR_PARSER_EXT(_genl_ctrl_mc_parser, 88 sizeof(struct snl_genl_ctrl_mcast_group), _nla_p_getmc, NULL); 89 90 struct _getfamily_attrs { 91 uint16_t family_id; 92 const char *family_name; 93 struct snl_genl_ctrl_mcast_groups mcast_groups; 94 }; 95 96 #define _IN(_field) offsetof(struct genlmsghdr, _field) 97 #define _OUT(_field) offsetof(struct _getfamily_attrs, _field) 98 static struct snl_attr_parser _nla_p_getfam[] = { 99 { 100 .type = CTRL_ATTR_FAMILY_ID, 101 .off = _OUT(family_id), 102 .cb = snl_attr_get_uint16, 103 }, 104 { 105 .type = CTRL_ATTR_FAMILY_NAME, 106 .off = _OUT(family_name), 107 .cb = snl_attr_get_string, 108 }, 109 { 110 .type = CTRL_ATTR_MCAST_GROUPS, 111 .off = _OUT(mcast_groups), 112 .cb = snl_attr_get_parray, 113 .arg = &_genl_ctrl_mc_parser, 114 }, 115 }; 116 #undef _IN 117 #undef _OUT 118 SNL_DECLARE_GENL_PARSER(_genl_ctrl_getfam_parser, _nla_p_getfam); 119 120 static bool 121 snl_get_genl_family_info(struct snl_state *ss, const char *family_name, 122 struct _getfamily_attrs *attrs) 123 { 124 struct snl_writer nw; 125 struct nlmsghdr *hdr; 126 127 memset(attrs, 0, sizeof(*attrs)); 128 129 snl_init_writer(ss, &nw); 130 hdr = snl_create_genl_msg_request(&nw, GENL_ID_CTRL, CTRL_CMD_GETFAMILY); 131 snl_add_msg_attr_string(&nw, CTRL_ATTR_FAMILY_NAME, family_name); 132 if ((hdr = snl_finalize_msg(&nw)) == NULL || !snl_send_message(ss, hdr)) 133 return (false); 134 135 hdr = snl_read_reply(ss, hdr->nlmsg_seq); 136 if (hdr != NULL && hdr->nlmsg_type != NLMSG_ERROR) { 137 if (snl_parse_nlmsg(ss, hdr, &_genl_ctrl_getfam_parser, attrs)) 138 return (true); 139 } 140 141 return (false); 142 } 143 144 static inline uint16_t 145 snl_get_genl_family(struct snl_state *ss, const char *family_name) 146 { 147 struct _getfamily_attrs attrs = {}; 148 149 snl_get_genl_family_info(ss, family_name, &attrs); 150 return (attrs.family_id); 151 } 152 153 static inline uint16_t 154 snl_get_genl_mcast_group(struct snl_state *ss, const char *family_name, 155 const char *group_name, uint16_t *family_id) 156 { 157 struct _getfamily_attrs attrs = {}; 158 159 snl_get_genl_family_info(ss, family_name, &attrs); 160 if (attrs.family_id == 0) 161 return (0); 162 if (family_id != NULL) 163 *family_id = attrs.family_id; 164 for (u_int i = 0; i < attrs.mcast_groups.num_groups; i++) 165 if (strcmp(attrs.mcast_groups.groups[i]->mcast_grp_name, 166 group_name) == 0) 167 return (attrs.mcast_groups.groups[i]->mcast_grp_id); 168 return (0); 169 } 170 171 static const struct snl_hdr_parser *snl_all_genl_parsers[] = { 172 &_genl_ctrl_getfam_parser, &_genl_ctrl_mc_parser, 173 }; 174 175 #endif 176