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