xref: /aosp_15_r20/external/libnl/include/netlink/genl/mngt.h (revision 4dc78e53d49367fa8e61b07018507c90983a077d)
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3  * Copyright (c) 2003-2012 Thomas Graf <[email protected]>
4  */
5 
6 #ifndef NETLINK_GENL_MNGT_H_
7 #define NETLINK_GENL_MNGT_H_
8 
9 #include <netlink/netlink.h>
10 #include <netlink/attr.h>
11 #include <netlink/list.h>
12 
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
16 
17 struct nl_cache_ops;
18 
19 /**
20  * @ingroup genl_mngt
21  * @struct genl_info netlink/genl/mngt.h
22  *
23  * Informative structure passed on to message parser callbacks
24  *
25  * This structure is passed on to all message parser callbacks and contains
26  * information about the sender of the message as well as pointers to all
27  * relevant sections of the parsed message.
28  *
29  * @see genl_cmd::c_msg_parser
30  */
31 struct genl_info
32 {
33 	/** Socket address of sender */
34 	struct sockaddr_nl *    who;
35 
36 	/** Pointer to Netlink message header */
37 	struct nlmsghdr *       nlh;
38 
39 	/** Pointer to Generic Netlink message header */
40 	struct genlmsghdr *     genlhdr;
41 
42 	/** Pointer to user header */
43 	void *                  userhdr;
44 
45 	/** Pointer to array of parsed attributes */
46 	struct nlattr **        attrs;
47 };
48 
49 /**
50  * @ingroup genl_mngt
51  * @struct genl_cmd netlink/genl/mngt.h
52  *
53  * Definition of a Generic Netlink command.
54  *
55  * This structure is used to define the list of available commands on the
56  * receiving side.
57  *
58  * @par Example:
59  * @code
60  * static struct genl_cmd foo_cmds[] = {
61  * 	{
62  * 		.c_id		= FOO_CMD_NEW,
63  * 		.c_name		= "NEWFOO" ,
64  * 		.c_maxattr	= FOO_ATTR_MAX,
65  * 		.c_attr_policy	= foo_policy,
66  * 		.c_msg_parser	= foo_msg_parser,
67  * 	},
68  * 	{
69  * 		.c_id		= FOO_CMD_DEL,
70  * 		.c_name		= "DELFOO" ,
71  * 	},
72  * };
73  *
74  * static struct genl_ops my_genl_ops = {
75  * 	[...]
76  * 	.o_cmds			= foo_cmds,
77  * 	.o_ncmds		= ARRAY_SIZE(foo_cmds),
78  * };
79  * @endcode
80  */
81 struct genl_cmd
82 {
83 	/** Numeric command identifier (required) */
84 	int			c_id;
85 
86 	/** Human readable name  (required) */
87 	char *			c_name;
88 
89 	/** Maximum attribute identifier that the command is prepared to handle. */
90 	int			c_maxattr;
91 
92 	/** Called whenever a message for this command is received */
93 	int		      (*c_msg_parser)(struct nl_cache_ops *,
94 					      struct genl_cmd *,
95 					      struct genl_info *, void *);
96 
97 	/** Attribute validation policy, enforced before the callback is called */
98 	struct nla_policy *	c_attr_policy;
99 };
100 
101 /**
102  * @ingroup genl_mngt
103  * @struct genl_ops netlink/genl/mngt.h
104  *
105  * Definition of a Generic Netlink family
106  *
107  * @par Example:
108  * @code
109  * static struct genl_cmd foo_cmds[] = {
110  * 	[...]
111  * };
112  *
113  * static struct genl_ops my_genl_ops = {
114  * 	.o_name			= "foo",
115  * 	.o_hdrsize		= sizeof(struct my_hdr),
116  * 	.o_cmds			= foo_cmds,
117  * 	.o_ncmds		= ARRAY_SIZE(foo_cmds),
118  * };
119  *
120  * if ((err = genl_register_family(&my_genl_ops)) < 0)
121  * 	// ERROR
122  * @endcode
123  *
124  * @see genl_cmd
125  */
126 struct genl_ops
127 {
128 	/** Length of user header */
129 	unsigned int		o_hdrsize;
130 
131 	/** Numeric identifier, automatically filled in by genl_ops_resolve() */
132 	int			o_id;
133 
134 	/** Human readable name, used by genl_ops_resolve() to resolve numeric id */
135 	char *			o_name;
136 
137 	/**
138 	 * If registered via genl_register(), will point to the related
139 	 * cache operations.
140 	 */
141 	struct nl_cache_ops *	o_cache_ops;
142 
143 	/** Optional array defining the available Generic Netlink commands */
144 	struct genl_cmd	*	o_cmds;
145 
146 	/** Number of elements in \c o_cmds array */
147 	int			o_ncmds;
148 
149 	/**
150 	 * @private
151 	 * Used internally to link together all registered operations.
152 	 */
153 	struct nl_list_head	o_list;
154 };
155 
156 extern int		genl_register_family(struct genl_ops *);
157 extern int		genl_unregister_family(struct genl_ops *);
158 extern int		genl_handle_msg(struct nl_msg *, void *);
159 
160 extern int		genl_register(struct nl_cache_ops *);
161 extern void		genl_unregister(struct nl_cache_ops *);
162 
163 extern int		genl_ops_resolve(struct nl_sock *, struct genl_ops *);
164 extern int		genl_mngt_resolve(struct nl_sock *);
165 
166 #ifdef __cplusplus
167 }
168 #endif
169 
170 #endif
171