2488 lines · 74.7 KB · C
RawDownload
/* SPDX-License-Identifier: GPL-2.0 */#ifndef __NET_NETLINK_H#define __NET_NETLINK_H #include <linux/types.h>#include <linux/netlink.h>#include <linux/jiffies.h>#include <linux/in6.h> /* ======================================================================== *         Netlink Messages and Attributes Interface (As Seen On TV) * ------------------------------------------------------------------------ *                          Messages Interface * ------------------------------------------------------------------------ * * Message Format: *    <--- nlmsg_total_size(payload)  ---> *    <-- nlmsg_msg_size(payload) -> *   +----------+- - -+-------------+- - -+-------- - - *   | nlmsghdr | Pad |   Payload   | Pad | nlmsghdr *   +----------+- - -+-------------+- - -+-------- - - *   nlmsg_data(nlh)---^                   ^ *   nlmsg_next(nlh)-----------------------+ * * Payload Format: *    <---------------------- nlmsg_len(nlh) ---------------------> *    <------ hdrlen ------>       <- nlmsg_attrlen(nlh, hdrlen) -> *   +----------------------+- - -+--------------------------------+ *   |     Family Header    | Pad |           Attributes           | *   +----------------------+- - -+--------------------------------+ *   nlmsg_attrdata(nlh, hdrlen)---^ * * Data Structures: *   struct nlmsghdr			netlink message header * * Message Construction: *   nlmsg_new()			create a new netlink message *   nlmsg_put()			add a netlink message to an skb *   nlmsg_put_answer()			callback based nlmsg_put() *   nlmsg_end()			finalize netlink message *   nlmsg_get_pos()			return current position in message *   nlmsg_trim()			trim part of message *   nlmsg_cancel()			cancel message construction *   nlmsg_consume()			free a netlink message (expected) *   nlmsg_free()			free a netlink message (drop) * * Message Sending: *   nlmsg_multicast()			multicast message to several groups *   nlmsg_unicast()			unicast a message to a single socket *   nlmsg_notify()			send notification message * * Message Length Calculations: *   nlmsg_msg_size(payload)		length of message w/o padding *   nlmsg_total_size(payload)		length of message w/ padding *   nlmsg_padlen(payload)		length of padding at tail * * Message Payload Access: *   nlmsg_data(nlh)			head of message payload *   nlmsg_len(nlh)			length of message payload *   nlmsg_attrdata(nlh, hdrlen)	head of attributes data *   nlmsg_attrlen(nlh, hdrlen)		length of attributes data * * Message Parsing: *   nlmsg_ok(nlh, remaining)		does nlh fit into remaining bytes? *   nlmsg_next(nlh, remaining)		get next netlink message *   nlmsg_parse()			parse attributes of a message *   nlmsg_find_attr()			find an attribute in a message *   nlmsg_for_each_msg()		loop over all messages *   nlmsg_validate()			validate netlink message incl. attrs *   nlmsg_for_each_attr()		loop over all attributes *   nlmsg_for_each_attr_type()		loop over all attributes with the *					given type * * Misc: *   nlmsg_report()			report back to application? * * ------------------------------------------------------------------------ *                          Attributes Interface * ------------------------------------------------------------------------ * * Attribute Format: *    <------- nla_total_size(payload) -------> *    <---- nla_attr_size(payload) -----> *   +----------+- - -+- - - - - - - - - +- - -+-------- - - *   |  Header  | Pad |     Payload      | Pad |  Header *   +----------+- - -+- - - - - - - - - +- - -+-------- - - *                     <- nla_len(nla) ->      ^ *   nla_data(nla)----^                        | *   nla_next(nla)-----------------------------' * * Data Structures: *   struct nlattr			netlink attribute header * * Attribute Construction: *   nla_reserve(skb, type, len)	reserve room for an attribute *   nla_reserve_nohdr(skb, len)	reserve room for an attribute w/o hdr *   nla_put(skb, type, len, data)	add attribute to skb *   nla_put_nohdr(skb, len, data)	add attribute w/o hdr *   nla_append(skb, len, data)		append data to skb * * Attribute Construction for Basic Types: *   nla_put_u8(skb, type, value)	add u8 attribute to skb *   nla_put_u16(skb, type, value)	add u16 attribute to skb *   nla_put_u32(skb, type, value)	add u32 attribute to skb *   nla_put_u64_64bit(skb, type, *                     value, padattr)	add u64 attribute to skb *   nla_put_s8(skb, type, value)	add s8 attribute to skb *   nla_put_s16(skb, type, value)	add s16 attribute to skb *   nla_put_s32(skb, type, value)	add s32 attribute to skb *   nla_put_s64(skb, type, value, *               padattr)		add s64 attribute to skb *   nla_put_string(skb, type, str)	add string attribute to skb *   nla_put_flag(skb, type)		add flag attribute to skb *   nla_put_msecs(skb, type, jiffies, *                 padattr)		add msecs attribute to skb *   nla_put_in_addr(skb, type, addr)	add IPv4 address attribute to skb *   nla_put_in6_addr(skb, type, addr)	add IPv6 address attribute to skb * * Nested Attributes Construction: *   nla_nest_start(skb, type)		start a nested attribute *   nla_nest_end(skb, nla)		finalize a nested attribute *   nla_nest_cancel(skb, nla)		cancel nested attribute construction *   nla_put_empty_nest(skb, type)	create an empty nest * * Attribute Length Calculations: *   nla_attr_size(payload)		length of attribute w/o padding *   nla_total_size(payload)		length of attribute w/ padding *   nla_padlen(payload)		length of padding * * Attribute Payload Access: *   nla_data(nla)			head of attribute payload *   nla_len(nla)			length of attribute payload * * Attribute Payload Access for Basic Types: *   nla_get_uint(nla)			get payload for a uint attribute *   nla_get_sint(nla)			get payload for a sint attribute *   nla_get_u8(nla)			get payload for a u8 attribute *   nla_get_u16(nla)			get payload for a u16 attribute *   nla_get_u32(nla)			get payload for a u32 attribute *   nla_get_u64(nla)			get payload for a u64 attribute *   nla_get_s8(nla)			get payload for a s8 attribute *   nla_get_s16(nla)			get payload for a s16 attribute *   nla_get_s32(nla)			get payload for a s32 attribute *   nla_get_s64(nla)			get payload for a s64 attribute *   nla_get_flag(nla)			return 1 if flag is true *   nla_get_msecs(nla)			get payload for a msecs attribute * *   The same functions also exist with _default(). * * Attribute Misc: *   nla_memcpy(dest, nla, count)	copy attribute into memory *   nla_memcmp(nla, data, size)	compare attribute with memory area *   nla_strscpy(dst, nla, size)	copy attribute to a sized string *   nla_strcmp(nla, str)		compare attribute with string * * Attribute Parsing: *   nla_ok(nla, remaining)		does nla fit into remaining bytes? *   nla_next(nla, remaining)		get next netlink attribute *   nla_validate()			validate a stream of attributes *   nla_validate_nested()		validate a stream of nested attributes *   nla_find()				find attribute in stream of attributes *   nla_find_nested()			find attribute in nested attributes *   nla_parse()			parse and validate stream of attrs *   nla_parse_nested()			parse nested attributes *   nla_for_each_attr()		loop over all attributes *   nla_for_each_attr_type()		loop over all attributes with the *					given type *   nla_for_each_nested()		loop over the nested attributes *   nla_for_each_nested_type()		loop over the nested attributes with *					the given type *========================================================================= */  /**  * Standard attribute types to specify validation policy  */enum {	NLA_UNSPEC,	NLA_U8,	NLA_U16,	NLA_U32,	NLA_U64,	NLA_STRING,	NLA_FLAG,	NLA_MSECS,	NLA_NESTED,	NLA_NESTED_ARRAY,	NLA_NUL_STRING,	NLA_BINARY,	NLA_S8,	NLA_S16,	NLA_S32,	NLA_S64,	NLA_BITFIELD32,	NLA_REJECT,	NLA_BE16,	NLA_BE32,	NLA_SINT,	NLA_UINT,	__NLA_TYPE_MAX,}; #define NLA_TYPE_MAX (__NLA_TYPE_MAX - 1) struct netlink_range_validation {	u64 min, max;}; struct netlink_range_validation_signed {	s64 min, max;}; enum nla_policy_validation {	NLA_VALIDATE_NONE,	NLA_VALIDATE_RANGE,	NLA_VALIDATE_RANGE_WARN_TOO_LONG,	NLA_VALIDATE_MIN,	NLA_VALIDATE_MAX,	NLA_VALIDATE_MASK,	NLA_VALIDATE_RANGE_PTR,	NLA_VALIDATE_FUNCTION,}; /** * struct nla_policy - attribute validation policy * @type: Type of attribute or NLA_UNSPEC * @validation_type: type of attribute validation done in addition to *	type-specific validation (e.g. range, function call), see *	&enum nla_policy_validation * @len: Type specific length of payload * * Policies are defined as arrays of this struct, the array must be * accessible by attribute type up to the highest identifier to be expected. * * Meaning of `len' field: *    NLA_STRING           Maximum length of string *    NLA_NUL_STRING       Maximum length of string (excluding NUL) *    NLA_FLAG             Unused *    NLA_BINARY           Maximum length of attribute payload *                         (but see also below with the validation type) *    NLA_NESTED, *    NLA_NESTED_ARRAY     Length verification is done by checking len of *                         nested header (or empty); len field is used if *                         nested_policy is also used, for the max attr *                         number in the nested policy. *    NLA_SINT, NLA_UINT, *    NLA_U8, NLA_U16, *    NLA_U32, NLA_U64, *    NLA_S8, NLA_S16, *    NLA_S32, NLA_S64, *    NLA_BE16, NLA_BE32, *    NLA_MSECS            Leaving the length field zero will verify the *                         given type fits, using it verifies minimum length *                         just like "All other" *    NLA_BITFIELD32       Unused *    NLA_REJECT           Unused *    All other            Minimum length of attribute payload * * Meaning of validation union: *    NLA_BITFIELD32       This is a 32-bit bitmap/bitselector attribute and *                         `bitfield32_valid' is the u32 value of valid flags *    NLA_REJECT           This attribute is always rejected and `reject_message' *                         may point to a string to report as the error instead *                         of the generic one in extended ACK. *    NLA_NESTED           `nested_policy' to a nested policy to validate, must *                         also set `len' to the max attribute number. Use the *                         provided NLA_POLICY_NESTED() macro. *                         Note that nla_parse() will validate, but of course not *                         parse, the nested sub-policies. *    NLA_NESTED_ARRAY     `nested_policy' points to a nested policy to validate, *                         must also set `len' to the max attribute number. Use *                         the provided NLA_POLICY_NESTED_ARRAY() macro. *                         The difference to NLA_NESTED is the structure: *                         NLA_NESTED has the nested attributes directly inside *                         while an array has the nested attributes at another *                         level down and the attribute types directly in the *                         nesting don't matter. *    NLA_UINT, *    NLA_U8, *    NLA_U16, *    NLA_U32, *    NLA_U64, *    NLA_BE16, *    NLA_BE32, *    NLA_SINT, *    NLA_S8, *    NLA_S16, *    NLA_S32, *    NLA_S64              The `min' and `max' fields are used depending on the *                         validation_type field, if that is min/max/range then *                         the min, max or both are used (respectively) to check *                         the value of the integer attribute. *                         Note that in the interest of code simplicity and *                         struct size both limits are s16, so you cannot *                         enforce a range that doesn't fall within the range *                         of s16 - do that using the NLA_POLICY_FULL_RANGE() *                         or NLA_POLICY_FULL_RANGE_SIGNED() macros instead. *                         Use the NLA_POLICY_MIN(), NLA_POLICY_MAX() and *                         NLA_POLICY_RANGE() macros. *    NLA_UINT, *    NLA_U8, *    NLA_U16, *    NLA_U32, *    NLA_U64              If the validation_type field instead is set to *                         NLA_VALIDATE_RANGE_PTR, `range' must be a pointer *                         to a struct netlink_range_validation that indicates *                         the min/max values. *                         Use NLA_POLICY_FULL_RANGE(). *    NLA_SINT, *    NLA_S8, *    NLA_S16, *    NLA_S32, *    NLA_S64              If the validation_type field instead is set to *                         NLA_VALIDATE_RANGE_PTR, `range_signed' must be a *                         pointer to a struct netlink_range_validation_signed *                         that indicates the min/max values. *                         Use NLA_POLICY_FULL_RANGE_SIGNED(). * *    NLA_BINARY           If the validation type is like the ones for integers *                         above, then the min/max length (not value like for *                         integers) of the attribute is enforced. * *    All other            Unused - but note that it's a union * * Meaning of `validate' field, use via NLA_POLICY_VALIDATE_FN: *    NLA_U8, NLA_U16, *    NLA_U32, NLA_U64, *    NLA_S8, NLA_S16, *    NLA_S32, NLA_S64, *    NLA_MSECS, *    NLA_BINARY           Validation function called for the attribute. * *    All other            Unused - but note that it's a union * * Example: * * static const u32 myvalidflags = 0xff231023; * * static const struct nla_policy my_policy[ATTR_MAX+1] = { * 	[ATTR_FOO] = { .type = NLA_U16 }, *	[ATTR_BAR] = { .type = NLA_STRING, .len = BARSIZ }, *	[ATTR_BAZ] = NLA_POLICY_EXACT_LEN(sizeof(struct mystruct)), *	[ATTR_GOO] = NLA_POLICY_BITFIELD32(myvalidflags), * }; */struct nla_policy {	u8		type;	u8		validation_type;	u16		len;	union {		/**		 * @strict_start_type: first attribute to validate strictly		 *		 * This entry is special, and used for the attribute at index 0		 * only, and specifies special data about the policy, namely it		 * specifies the "boundary type" where strict length validation		 * starts for any attribute types >= this value, also, strict		 * nesting validation starts here.		 *		 * Additionally, it means that NLA_UNSPEC is actually NLA_REJECT		 * for any types >= this, so need to use NLA_POLICY_MIN_LEN() to		 * get the previous pure { .len = xyz } behaviour. The advantage		 * of this is that types not specified in the policy will be		 * rejected.		 *		 * For completely new families it should be set to 1 so that the		 * validation is enforced for all attributes. For existing ones		 * it should be set at least when new attributes are added to		 * the enum used by the policy, and be set to the new value that		 * was added to enforce strict validation from thereon.		 */		u16 strict_start_type; 		/* private: use NLA_POLICY_*() to set */		const u32 bitfield32_valid;		const u32 mask;		const char *reject_message;		const struct nla_policy *nested_policy;		const struct netlink_range_validation *range;		const struct netlink_range_validation_signed *range_signed;		struct {			s16 min, max;		};		int (*validate)(const struct nlattr *attr,				struct netlink_ext_ack *extack);	};}; #define NLA_POLICY_ETH_ADDR		NLA_POLICY_EXACT_LEN(ETH_ALEN)#define NLA_POLICY_ETH_ADDR_COMPAT	NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN) #define _NLA_POLICY_NESTED(maxattr, policy) \	{ .type = NLA_NESTED, .nested_policy = policy, .len = maxattr }#define _NLA_POLICY_NESTED_ARRAY(maxattr, policy) \	{ .type = NLA_NESTED_ARRAY, .nested_policy = policy, .len = maxattr }#define NLA_POLICY_NESTED(policy) \	_NLA_POLICY_NESTED(ARRAY_SIZE(policy) - 1, policy)#define NLA_POLICY_NESTED_ARRAY(policy) \	_NLA_POLICY_NESTED_ARRAY(ARRAY_SIZE(policy) - 1, policy)#define NLA_POLICY_BITFIELD32(valid) \	{ .type = NLA_BITFIELD32, .bitfield32_valid = valid } #define __NLA_IS_UINT_TYPE(tp)					\	(tp == NLA_U8 || tp == NLA_U16 || tp == NLA_U32 ||	\	 tp == NLA_U64 || tp == NLA_UINT ||			\	 tp == NLA_BE16 || tp == NLA_BE32)#define __NLA_IS_SINT_TYPE(tp)						\	(tp == NLA_S8 || tp == NLA_S16 || tp == NLA_S32 || tp == NLA_S64 || \	 tp == NLA_SINT) #define __NLA_ENSURE(condition) BUILD_BUG_ON_ZERO(!(condition))#define NLA_ENSURE_UINT_TYPE(tp)			\	(__NLA_ENSURE(__NLA_IS_UINT_TYPE(tp)) + tp)#define NLA_ENSURE_UINT_OR_BINARY_TYPE(tp)		\	(__NLA_ENSURE(__NLA_IS_UINT_TYPE(tp) ||	\		      tp == NLA_MSECS ||		\		      tp == NLA_BINARY) + tp)#define NLA_ENSURE_SINT_TYPE(tp)			\	(__NLA_ENSURE(__NLA_IS_SINT_TYPE(tp)) + tp)#define NLA_ENSURE_INT_OR_BINARY_TYPE(tp)		\	(__NLA_ENSURE(__NLA_IS_UINT_TYPE(tp) ||		\		      __NLA_IS_SINT_TYPE(tp) ||		\		      tp == NLA_MSECS ||		\		      tp == NLA_BINARY) + tp)#define NLA_ENSURE_NO_VALIDATION_PTR(tp)		\	(__NLA_ENSURE(tp != NLA_BITFIELD32 &&		\		      tp != NLA_REJECT &&		\		      tp != NLA_NESTED &&		\		      tp != NLA_NESTED_ARRAY) + tp) #define NLA_POLICY_RANGE(tp, _min, _max) {		\	.type = NLA_ENSURE_INT_OR_BINARY_TYPE(tp),	\	.validation_type = NLA_VALIDATE_RANGE,		\	.min = _min,					\	.max = _max					\} #define NLA_POLICY_FULL_RANGE(tp, _range) {		\	.type = NLA_ENSURE_UINT_OR_BINARY_TYPE(tp),	\	.validation_type = NLA_VALIDATE_RANGE_PTR,	\	.range = _range,				\} #define NLA_POLICY_FULL_RANGE_SIGNED(tp, _range) {	\	.type = NLA_ENSURE_SINT_TYPE(tp),		\	.validation_type = NLA_VALIDATE_RANGE_PTR,	\	.range_signed = _range,				\} #define NLA_POLICY_MIN(tp, _min) {			\	.type = NLA_ENSURE_INT_OR_BINARY_TYPE(tp),	\	.validation_type = NLA_VALIDATE_MIN,		\	.min = _min,					\} #define NLA_POLICY_MAX(tp, _max) {			\	.type = NLA_ENSURE_INT_OR_BINARY_TYPE(tp),	\	.validation_type = NLA_VALIDATE_MAX,		\	.max = _max,					\} #define NLA_POLICY_MASK(tp, _mask) {			\	.type = NLA_ENSURE_UINT_TYPE(tp),		\	.validation_type = NLA_VALIDATE_MASK,		\	.mask = _mask,					\} #define NLA_POLICY_VALIDATE_FN(tp, fn, ...) {		\	.type = NLA_ENSURE_NO_VALIDATION_PTR(tp),	\	.validation_type = NLA_VALIDATE_FUNCTION,	\	.validate = fn,					\	.len = __VA_ARGS__ + 0,				\} #define NLA_POLICY_EXACT_LEN(_len)	NLA_POLICY_RANGE(NLA_BINARY, _len, _len)#define NLA_POLICY_EXACT_LEN_WARN(_len) {			\	.type = NLA_BINARY,					\	.validation_type = NLA_VALIDATE_RANGE_WARN_TOO_LONG,	\	.min = _len,						\	.max = _len						\}#define NLA_POLICY_MIN_LEN(_len)	NLA_POLICY_MIN(NLA_BINARY, _len)#define NLA_POLICY_MAX_LEN(_len)	NLA_POLICY_MAX(NLA_BINARY, _len) /** * struct nl_info - netlink source information * @nlh: Netlink message header of original request * @nl_net: Network namespace * @portid: Netlink PORTID of requesting application * @skip_notify: Skip netlink notifications to user space * @skip_notify_kernel: Skip selected in-kernel notifications */struct nl_info {	struct nlmsghdr		*nlh;	struct net		*nl_net;	u32			portid;	u8			skip_notify:1,				skip_notify_kernel:1;}; 

Lines 1–500 of 2488.