libnl 1.1.4
Modules | Data Structures | Functions

Modules

 Controller
 

Data Structures

struct  genl_cmd
 Generic Netlink Command. More...
 
struct  genl_ops
 Generic Netlink Operations. More...
 

Functions

char * genl_op2name (int family, int op, char *buf, size_t len)
 

Register/Unregister

int genl_register (struct nl_cache_ops *ops)
 Register generic netlink operations.
 
void genl_unregister (struct nl_cache_ops *ops)
 Unregister generic netlink operations.
 

Resolving ID/Name

int genl_ops_resolve (struct nl_handle *handle, struct genl_ops *ops)
 
int genl_mngt_resolve (struct nl_handle *handle)
 

Detailed Description

1) Registering a generic netlink module
#include <netlink/genl/mngt.h>
// First step is to define all the commands being used in
// particular generic netlink family. The ID and name are
// mandatory to be filled out. A callback function and
// most the attribute policy that comes with it must be
// defined for commands expected to be issued towards
// userspace.
static struct genl_cmd foo_cmds[] = {
{
.c_id = FOO_CMD_NEW,
.c_name = "NEWFOO" ,
.c_maxattr = FOO_ATTR_MAX,
.c_attr_policy = foo_policy,
.c_msg_parser = foo_msg_parser,
},
{
.c_id = FOO_CMD_DEL,
.c_name = "DELFOO" ,
},
};
// The list of commands must then be integrated into a
// struct genl_ops serving as handle for this particular
// family.
static struct genl_ops my_genl_ops = {
.o_cmds = foo_cmds,
.o_ncmds = ARRAY_SIZE(foo_cmds),
};
// Using the above struct genl_ops an arbitary number of
// cache handles can be associated to it.
//
// The macro GENL_HDRSIZE() must be used to specify the
// length of the header to automatically take headers on
// generic layers into account.
//
// The macro GENL_FAMILY() is used to represent the generic
// netlink family id.
static struct nl_cache_ops genl_foo_ops = {
.co_name = "genl/foo",
.co_hdrsize = GENL_HDRSIZE(sizeof(struct my_hdr)),
.co_msgtypes = GENL_FAMILY(GENL_ID_GENERATE, "foo"),
.co_genl = &my_genl_ops,
.co_protocol = NETLINK_GENERIC,
.co_request_update = foo_request_update,
.co_obj_ops = &genl_foo_ops,
};
// Finally each cache handle for a generic netlink family
// must be registered using genl_register().
static void __init foo_init(void)
{
genl_register(&genl_foo_ops);
}
// ... respectively unregsted again.
static void __exit foo_exit(void)
{
genl_unregister(&genl_foo_ops);
}
int genl_register(struct nl_cache_ops *ops)
Register generic netlink operations.
Definition: mngt.c:172
void genl_unregister(struct nl_cache_ops *ops)
Unregister generic netlink operations.
Definition: mngt.c:214
Generic Netlink Command.
Definition: mngt.h:30
int c_id
Unique command identifier.
Definition: mngt.h:32
Generic Netlink Operations.
Definition: mngt.h:58
Cache Operations.
Definition: cache-api.h:164

Function Documentation

◆ genl_op2name()

char * genl_op2name ( int  family,
int  op,
char *  buf,
size_t  len 
)

Definition at line 139 of file mngt.c.

140{
141 struct genl_ops *ops;
142 int i;
143
144 nl_list_for_each_entry(ops, &genl_ops_list, o_list) {
145 if (ops->o_family == family) {
146 for (i = 0; i < ops->o_ncmds; i++) {
147 struct genl_cmd *cmd;
148 cmd = &ops->o_cmds[i];
149
150 if (cmd->c_id == op) {
151 strncpy(buf, cmd->c_name, len - 1);
152 return buf;
153 }
154 }
155 }
156 }
157
158 strncpy(buf, "unknown", len - 1);
159 return NULL;
160}
char * c_name
Name/description of command.
Definition: mngt.h:35

◆ genl_register()

int genl_register ( struct nl_cache_ops ops)
Parameters
opscache operations

Definition at line 172 of file mngt.c.

173{
174 int err;
175
176 if (ops->co_protocol != NETLINK_GENERIC) {
177 err = nl_error(EINVAL, "cache operations not for protocol " \
178 "NETLINK_GENERIC (protocol=%s)",
179 ops->co_protocol);
180 goto errout;
181 }
182
183 if (ops->co_hdrsize < GENL_HDRSIZE(0)) {
184 err = nl_error(EINVAL, "co_hdrsize too short, probably " \
185 "not including genlmsghdr, minsize=%d",
186 GENL_HDRSIZE(0));
187 goto errout;
188 }
189
190 if (ops->co_genl == NULL) {
191 err = nl_error(EINVAL, "co_genl is NULL, must provide " \
192 "valid genl operations");
193 goto errout;
194 }
195
196 ops->co_genl->o_cache_ops = ops;
197 ops->co_genl->o_name = ops->co_msgtypes[0].mt_name;
198 ops->co_genl->o_family = ops->co_msgtypes[0].mt_id;
199 ops->co_msg_parser = genl_msg_parser;
200
201 /* FIXME: check for dup */
202
203 nl_list_add_tail(&ops->co_genl->o_list, &genl_ops_list);
204
205 err = nl_cache_mngt_register(ops);
206errout:
207 return err;
208}
int nl_cache_mngt_register(struct nl_cache_ops *ops)
Register a set of cache operations.
Definition: cache_mngt.c:226
int(* co_msg_parser)(struct nl_cache_ops *, struct sockaddr_nl *, struct nlmsghdr *, struct nl_parser_param *)
Called whenever a message was received that needs to be parsed.
Definition: cache-api.h:183
int mt_id
Netlink message type.
Definition: cache-api.h:131
char * mt_name
Name of operation for human-readable printing.
Definition: cache-api.h:137

References nl_cache_ops::co_msg_parser, nl_msgtype::mt_id, nl_msgtype::mt_name, and nl_cache_mngt_register().

◆ genl_unregister()

void genl_unregister ( struct nl_cache_ops ops)
Parameters
opscache operations

Definition at line 214 of file mngt.c.

215{
217 nl_list_del(&ops->co_genl->o_list);
218}
int nl_cache_mngt_unregister(struct nl_cache_ops *ops)
Unregister a set of cache operations.
Definition: cache_mngt.c:261

References nl_cache_mngt_unregister().

◆ genl_ops_resolve()

int genl_ops_resolve ( struct nl_handle *  handle,
struct genl_ops ops 
)

Definition at line 243 of file mngt.c.

244{
245 struct nl_cache *ctrl;
246 int err;
247
248 ctrl = genl_ctrl_alloc_cache(handle);
249 if (ctrl == NULL) {
250 err = nl_get_errno();
251 goto errout;
252 }
253
254 err = __genl_ops_resolve(ctrl, ops);
255
256 nl_cache_free(ctrl);
257errout:
258 return err;
259}
void nl_cache_free(struct nl_cache *cache)
Free a cache.
Definition: cache.c:277

◆ genl_mngt_resolve()

int genl_mngt_resolve ( struct nl_handle *  handle)

Definition at line 261 of file mngt.c.

262{
263 struct nl_cache *ctrl;
264 struct genl_ops *ops;
265 int err = 0;
266
267 ctrl = genl_ctrl_alloc_cache(handle);
268 if (ctrl == NULL) {
269 err = nl_get_errno();
270 goto errout;
271 }
272
273 nl_list_for_each_entry(ops, &genl_ops_list, o_list) {
274 err = __genl_ops_resolve(ctrl, ops);
275 }
276
277 nl_cache_free(ctrl);
278errout:
279 return err;
280}