xref: /freebsd/sys/netlink/netlink_snl_generic.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
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, 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 snl_genl_ctrl_mcast_group {
56 	uint32_t mcast_grp_id;
57 	char *mcast_grp_name;
58 };
59 
60 struct snl_genl_ctrl_mcast_groups {
61 	uint32_t num_groups;
62 	struct snl_genl_ctrl_mcast_group **groups;
63 };
64 
65 #define	_OUT(_field)	offsetof(struct snl_genl_ctrl_mcast_group, _field)
66 static struct snl_attr_parser _nla_p_getmc[] = {
67 	{ .type = CTRL_ATTR_MCAST_GRP_NAME, .off = _OUT(mcast_grp_name), .cb = snl_attr_get_string },
68 	{ .type = CTRL_ATTR_MCAST_GRP_ID, .off = _OUT(mcast_grp_id), .cb = snl_attr_get_uint32 },
69 };
70 #undef _OUT
71 SNL_DECLARE_ATTR_PARSER_EXT(_genl_ctrl_mc_parser,
72 		sizeof(struct snl_genl_ctrl_mcast_group),
73 		_nla_p_getmc, NULL);
74 
75 struct _getfamily_attrs {
76 	uint16_t family_id;
77 	char	*family_name;
78 	struct snl_genl_ctrl_mcast_groups mcast_groups;
79 };
80 
81 #define	_IN(_field)	offsetof(struct genlmsghdr, _field)
82 #define	_OUT(_field)	offsetof(struct _getfamily_attrs, _field)
83 static struct snl_attr_parser _nla_p_getfam[] = {
84 	{ .type = CTRL_ATTR_FAMILY_ID , .off = _OUT(family_id), .cb = snl_attr_get_uint16 },
85 	{ .type = CTRL_ATTR_FAMILY_NAME, .off = _OUT(family_name), .cb = snl_attr_get_string },
86 	{
87 		.type = CTRL_ATTR_MCAST_GROUPS,
88 		.off = _OUT(mcast_groups),
89 		.cb = snl_attr_get_parray,
90 		.arg = &_genl_ctrl_mc_parser,
91 	},
92 };
93 #undef _IN
94 #undef _OUT
95 SNL_DECLARE_GENL_PARSER(_genl_ctrl_getfam_parser, _nla_p_getfam);
96 
97 static bool
98 snl_get_genl_family_info(struct snl_state *ss, const char *family_name,
99     struct _getfamily_attrs *attrs)
100 {
101 	struct snl_writer nw;
102 	struct nlmsghdr *hdr;
103 
104 	memset(attrs, 0, sizeof(*attrs));
105 
106 	snl_init_writer(ss, &nw);
107 	hdr = snl_create_genl_msg_request(&nw, GENL_ID_CTRL, CTRL_CMD_GETFAMILY);
108 	snl_add_msg_attr_string(&nw, CTRL_ATTR_FAMILY_NAME, family_name);
109 	if ((hdr = snl_finalize_msg(&nw)) == NULL || !snl_send_message(ss, hdr))
110 		return (false);
111 
112 	hdr = snl_read_reply(ss, hdr->nlmsg_seq);
113 	if (hdr != NULL && hdr->nlmsg_type != NLMSG_ERROR) {
114 		if (snl_parse_nlmsg(ss, hdr, &_genl_ctrl_getfam_parser, attrs))
115 			return (true);
116 	}
117 
118 	return (false);
119 }
120 
121 static inline uint16_t
122 snl_get_genl_family(struct snl_state *ss, const char *family_name)
123 {
124 	struct _getfamily_attrs attrs = {};
125 
126 	snl_get_genl_family_info(ss, family_name, &attrs);
127 	return (attrs.family_id);
128 }
129 
130 static const struct snl_hdr_parser *snl_all_genl_parsers[] = {
131 	&_genl_ctrl_getfam_parser, &_genl_ctrl_mc_parser,
132 };
133 
134 #endif
135