libnl  3.7.0
mngt.h
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3  * Copyright (c) 2003-2012 Thomas Graf <tgraf@suug.ch>
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 */
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
int genl_register(struct nl_cache_ops *)
Register Generic Netlink family backed cache.
Definition: mngt.c:241
int genl_mngt_resolve(struct nl_sock *)
Resolve all registered Generic Netlink families.
Definition: mngt.c:379
void genl_unregister(struct nl_cache_ops *)
Unregister cache based Generic Netlink family.
Definition: mngt.c:278
int genl_ops_resolve(struct nl_sock *, struct genl_ops *)
Resolve a single Generic Netlink family.
Definition: mngt.c:351
int genl_unregister_family(struct genl_ops *)
Unregister Generic Netlink family.
Definition: mngt.c:194
int genl_register_family(struct genl_ops *)
Register Generic Netlink family and associated commands.
Definition: mngt.c:164
int genl_handle_msg(struct nl_msg *, void *)
Run a received message through the demultiplexer.
Definition: mngt.c:208
Definition of a Generic Netlink command.
Definition: mngt.h:82
struct nla_policy * c_attr_policy
Attribute validation policy, enforced before the callback is called.
Definition: mngt.h:98
int c_id
Numeric command identifier (required)
Definition: mngt.h:84
char * c_name
Human readable name (required)
Definition: mngt.h:87
int(* c_msg_parser)(struct nl_cache_ops *, struct genl_cmd *, struct genl_info *, void *)
Called whenever a message for this command is received.
Definition: mngt.h:93
int c_maxattr
Maximum attribute identifier that the command is prepared to handle.
Definition: mngt.h:90
Informative structure passed on to message parser callbacks.
Definition: mngt.h:32
struct nlmsghdr * nlh
Pointer to Netlink message header.
Definition: mngt.h:37
void * userhdr
Pointer to user header.
Definition: mngt.h:43
struct sockaddr_nl * who
Socket address of sender.
Definition: mngt.h:34
struct nlattr ** attrs
Pointer to array of parsed attributes.
Definition: mngt.h:46
struct genlmsghdr * genlhdr
Pointer to Generic Netlink message header.
Definition: mngt.h:40
Definition of a Generic Netlink family.
Definition: mngt.h:127
int o_ncmds
Number of elements in o_cmds array.
Definition: mngt.h:147
unsigned int o_hdrsize
Length of user header.
Definition: mngt.h:129
struct nl_list_head o_list
Used internally to link together all registered operations.
Definition: mngt.h:153
char * o_name
Human readable name, used by genl_ops_resolve() to resolve numeric id.
Definition: mngt.h:135
int o_id
Numeric identifier, automatically filled in by genl_ops_resolve()
Definition: mngt.h:132
struct genl_cmd * o_cmds
Optional array defining the available Generic Netlink commands.
Definition: mngt.h:144
struct nl_cache_ops * o_cache_ops
If registered via genl_register(), will point to the related cache operations.
Definition: mngt.h:141
Attribute validation policy.
Definition: attr.h:63