1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 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, int genl_family, uint8_t genl_cmd) 37 { 38 assert(nw->hdr == NULL); 39 40 struct nlmsghdr *hdr = snl_reserve_msg_object(nw, struct nlmsghdr); 41 hdr->nlmsg_type = genl_family; 42 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK; 43 nw->hdr = hdr; 44 struct genlmsghdr *ghdr = snl_reserve_msg_object(nw, struct genlmsghdr); 45 ghdr->cmd = genl_cmd; 46 47 return (hdr); 48 } 49 50 static struct snl_field_parser snl_fp_genl[] = {}; 51 52 #define SNL_DECLARE_GENL_PARSER(_name, _np) SNL_DECLARE_PARSER(_name,\ 53 struct genlmsghdr, snl_fp_genl, _np) 54 55 struct _getfamily_attrs { 56 uint16_t family_id; 57 char *family_name; 58 }; 59 60 #define _IN(_field) offsetof(struct genlmsghdr, _field) 61 #define _OUT(_field) offsetof(struct _getfamily_attrs, _field) 62 static struct snl_attr_parser _nla_p_getfam[] = { 63 { .type = CTRL_ATTR_FAMILY_ID , .off = _OUT(family_id), .cb = snl_attr_get_uint16 }, 64 { .type = CTRL_ATTR_FAMILY_NAME, .off = _OUT(family_name), .cb = snl_attr_get_string }, 65 }; 66 #undef _IN 67 #undef _OUT 68 SNL_DECLARE_GENL_PARSER(_genl_ctrl_getfam_parser, _nla_p_getfam); 69 70 static inline uint16_t 71 snl_get_genl_family(struct snl_state *ss, const char *family_name) 72 { 73 struct snl_writer nw; 74 struct nlmsghdr *hdr; 75 76 snl_init_writer(ss, &nw); 77 hdr = snl_create_genl_msg_request(&nw, GENL_ID_CTRL, CTRL_CMD_GETFAMILY); 78 snl_add_msg_attr_string(&nw, CTRL_ATTR_FAMILY_NAME, family_name); 79 if (snl_finalize_msg(&nw) == NULL || !snl_send_message(ss, hdr)) 80 return (0); 81 82 hdr = snl_read_reply(ss, hdr->nlmsg_seq); 83 if (hdr != NULL && hdr->nlmsg_type != NLMSG_ERROR) { 84 struct _getfamily_attrs attrs = {}; 85 86 if (snl_parse_nlmsg(ss, hdr, &_genl_ctrl_getfam_parser, &attrs)) 87 return (attrs.family_id); 88 } 89 90 return (0); 91 } 92 93 static const struct snl_hdr_parser *snl_all_genl_parsers[] = { 94 &_genl_ctrl_getfam_parser, 95 }; 96 97 #endif 98