1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #ifndef _NET_ETHTOOL_NETLINK_H
4 #define _NET_ETHTOOL_NETLINK_H
6 #include <linux/ethtool_netlink.h>
7 #include <linux/netdevice.h>
8 #include <net/genetlink.h>
11 struct ethnl_req_info
;
13 int ethnl_parse_header(struct ethnl_req_info
*req_info
,
14 const struct nlattr
*nest
, struct net
*net
,
15 struct netlink_ext_ack
*extack
, bool require_dev
);
16 int ethnl_fill_reply_header(struct sk_buff
*skb
, struct net_device
*dev
,
18 struct sk_buff
*ethnl_reply_init(size_t payload
, struct net_device
*dev
, u8 cmd
,
19 u16 hdr_attrtype
, struct genl_info
*info
,
23 * ethnl_strz_size() - calculate attribute length for fixed size string
24 * @s: ETH_GSTRING_LEN sized string (may not be null terminated)
26 * Return: total length of an attribute with null terminated string from @s
28 static inline int ethnl_strz_size(const char *s
)
30 return nla_total_size(strnlen(s
, ETH_GSTRING_LEN
) + 1);
34 * ethnl_put_strz() - put string attribute with fixed size string
35 * @skb: skb with the message
36 * @attrype: attribute type
37 * @s: ETH_GSTRING_LEN sized string (may not be null terminated)
39 * Puts an attribute with null terminated string from @s into the message.
41 * Return: 0 on success, negative error code on failure
43 static inline int ethnl_put_strz(struct sk_buff
*skb
, u16 attrtype
,
46 unsigned int len
= strnlen(s
, ETH_GSTRING_LEN
);
49 attr
= nla_reserve(skb
, attrtype
, len
+ 1);
53 memcpy(nla_data(attr
), s
, len
);
54 ((char *)nla_data(attr
))[len
] = '\0';
59 * ethnl_update_u32() - update u32 value from NLA_U32 attribute
60 * @dst: value to update
61 * @attr: netlink attribute with new value or null
62 * @mod: pointer to bool for modification tracking
64 * Copy the u32 value from NLA_U32 netlink attribute @attr into variable
65 * pointed to by @dst; do nothing if @attr is null. Bool pointed to by @mod
66 * is set to true if this function changed the value of *dst, otherwise it
69 static inline void ethnl_update_u32(u32
*dst
, const struct nlattr
*attr
,
76 val
= nla_get_u32(attr
);
85 * ethnl_update_u8() - update u8 value from NLA_U8 attribute
86 * @dst: value to update
87 * @attr: netlink attribute with new value or null
88 * @mod: pointer to bool for modification tracking
90 * Copy the u8 value from NLA_U8 netlink attribute @attr into variable
91 * pointed to by @dst; do nothing if @attr is null. Bool pointed to by @mod
92 * is set to true if this function changed the value of *dst, otherwise it
95 static inline void ethnl_update_u8(u8
*dst
, const struct nlattr
*attr
,
102 val
= nla_get_u8(attr
);
111 * ethnl_update_bool32() - update u32 used as bool from NLA_U8 attribute
112 * @dst: value to update
113 * @attr: netlink attribute with new value or null
114 * @mod: pointer to bool for modification tracking
116 * Use the u8 value from NLA_U8 netlink attribute @attr to set u32 variable
117 * pointed to by @dst to 0 (if zero) or 1 (if not); do nothing if @attr is
118 * null. Bool pointed to by @mod is set to true if this function changed the
119 * logical value of *dst, otherwise it is left as is.
121 static inline void ethnl_update_bool32(u32
*dst
, const struct nlattr
*attr
,
128 val
= !!nla_get_u8(attr
);
137 * ethnl_update_binary() - update binary data from NLA_BINARY atribute
138 * @dst: value to update
139 * @len: destination buffer length
140 * @attr: netlink attribute with new value or null
141 * @mod: pointer to bool for modification tracking
143 * Use the u8 value from NLA_U8 netlink attribute @attr to rewrite data block
144 * of length @len at @dst by attribute payload; do nothing if @attr is null.
145 * Bool pointed to by @mod is set to true if this function changed the logical
146 * value of *dst, otherwise it is left as is.
148 static inline void ethnl_update_binary(void *dst
, unsigned int len
,
149 const struct nlattr
*attr
, bool *mod
)
153 if (nla_len(attr
) < len
)
155 if (!memcmp(dst
, nla_data(attr
), len
))
158 memcpy(dst
, nla_data(attr
), len
);
163 * ethnl_update_bitfield32() - update u32 value from NLA_BITFIELD32 attribute
164 * @dst: value to update
165 * @attr: netlink attribute with new value or null
166 * @mod: pointer to bool for modification tracking
168 * Update bits in u32 value which are set in attribute's mask to values from
169 * attribute's value. Do nothing if @attr is null or the value wouldn't change;
170 * otherwise, set bool pointed to by @mod to true.
172 static inline void ethnl_update_bitfield32(u32
*dst
, const struct nlattr
*attr
,
175 struct nla_bitfield32 change
;
180 change
= nla_get_bitfield32(attr
);
181 newval
= (*dst
& ~change
.selector
) | (change
.value
& change
.selector
);
190 * ethnl_reply_header_size() - total size of reply header
192 * This is an upper estimate so that we do not need to hold RTNL lock longer
193 * than necessary (to prevent rename between size estimate and composing the
194 * message). Accounts only for device ifindex and name as those are the only
195 * attributes ethnl_fill_reply_header() puts into the reply header.
197 static inline unsigned int ethnl_reply_header_size(void)
199 return nla_total_size(nla_total_size(sizeof(u32
)) +
200 nla_total_size(IFNAMSIZ
));
203 /* GET request handling */
205 /* Unified processing of GET requests uses two data structures: request info
206 * and reply data. Request info holds information parsed from client request
207 * and its stays constant through all request processing. Reply data holds data
208 * retrieved from ethtool_ops callbacks or other internal sources which is used
209 * to compose the reply. When processing a dump request, request info is filled
210 * only once (when the request message is parsed) but reply data is filled for
211 * each reply message.
213 * Both structures consist of part common for all request types (struct
214 * ethnl_req_info and struct ethnl_reply_data defined below) and optional
215 * parts specific for each request type. Common part always starts at offset 0.
219 * struct ethnl_req_info - base type of request information for GET requests
220 * @dev: network device the request is for (may be null)
221 * @flags: request flags common for all request types
223 * This is a common base for request specific structures holding data from
224 * parsed userspace request. These always embed struct ethnl_req_info at
227 struct ethnl_req_info
{
228 struct net_device
*dev
;
233 * struct ethnl_reply_data - base type of reply data for GET requests
234 * @dev: device for current reply message; in single shot requests it is
235 * equal to ðnl_req_info.dev; in dumps it's different for each
238 * This is a common base for request specific structures holding data for
239 * kernel reply message. These always embed struct ethnl_reply_data at zero
242 struct ethnl_reply_data
{
243 struct net_device
*dev
;
246 static inline int ethnl_ops_begin(struct net_device
*dev
)
248 if (dev
&& dev
->ethtool_ops
->begin
)
249 return dev
->ethtool_ops
->begin(dev
);
254 static inline void ethnl_ops_complete(struct net_device
*dev
)
256 if (dev
&& dev
->ethtool_ops
->complete
)
257 dev
->ethtool_ops
->complete(dev
);
261 * struct ethnl_request_ops - unified handling of GET requests
262 * @request_cmd: command id for request (GET)
263 * @reply_cmd: command id for reply (GET_REPLY)
264 * @hdr_attr: attribute type for request header
265 * @max_attr: maximum (top level) attribute type
266 * @req_info_size: size of request info
267 * @reply_data_size: size of reply data
268 * @request_policy: netlink policy for message contents
269 * @allow_nodev_do: allow non-dump request with no device identification
271 * Parse request except common header (struct ethnl_req_info). Common
272 * header is already filled on entry, the rest up to @repdata_offset
273 * is zero initialized. This callback should only modify type specific
274 * request info by parsed attributes from request message.
276 * Retrieve and prepare data needed to compose a reply message. Calls to
277 * ethtool_ops handlers are limited to this callback. Common reply data
278 * (struct ethnl_reply_data) is filled on entry, type specific part after
279 * it is zero initialized. This callback should only modify the type
280 * specific part of reply data. Device identification from struct
281 * ethnl_reply_data is to be used as for dump requests, it iterates
282 * through network devices while dev member of struct ethnl_req_info
283 * points to the device from client request.
285 * Estimate reply message size. Returned value must be sufficient for
286 * message payload without common reply header. The callback may returned
287 * estimate higher than actual message size if exact calculation would
288 * not be worth the saved memory space.
290 * Fill reply message payload (except for common header) from reply data.
291 * The callback must not generate more payload than previously called
292 * ->reply_size() estimated.
294 * Optional cleanup called when reply data is no longer needed. Can be
295 * used e.g. to free any additional data structures outside the main
296 * structure which were allocated by ->prepare_data(). When processing
297 * dump requests, ->cleanup() is called for each message.
299 * Description of variable parts of GET request handling when using the
300 * unified infrastructure. When used, a pointer to an instance of this
301 * structure is to be added to ðnl_default_requests array and generic
302 * handlers ethnl_default_doit(), ethnl_default_dumpit(),
303 * ethnl_default_start() and ethnl_default_done() used in @ethtool_genl_ops.
305 struct ethnl_request_ops
{
309 unsigned int max_attr
;
310 unsigned int req_info_size
;
311 unsigned int reply_data_size
;
312 const struct nla_policy
*request_policy
;
315 int (*parse_request
)(struct ethnl_req_info
*req_info
,
317 struct netlink_ext_ack
*extack
);
318 int (*prepare_data
)(const struct ethnl_req_info
*req_info
,
319 struct ethnl_reply_data
*reply_data
,
320 struct genl_info
*info
);
321 int (*reply_size
)(const struct ethnl_req_info
*req_info
,
322 const struct ethnl_reply_data
*reply_data
);
323 int (*fill_reply
)(struct sk_buff
*skb
,
324 const struct ethnl_req_info
*req_info
,
325 const struct ethnl_reply_data
*reply_data
);
326 void (*cleanup_data
)(struct ethnl_reply_data
*reply_data
);
329 /* request handlers */
331 extern const struct ethnl_request_ops ethnl_strset_request_ops
;
332 extern const struct ethnl_request_ops ethnl_linkinfo_request_ops
;
334 int ethnl_set_linkinfo(struct sk_buff
*skb
, struct genl_info
*info
);
336 #endif /* _NET_ETHTOOL_NETLINK_H */