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_dev_get(struct ethnl_req_info *req_info,
14 const struct nlattr *nest, struct net *net,
15 struct netlink_ext_ack *extack,
17 int ethnl_fill_reply_header(struct sk_buff *skb, struct net_device *dev,
19 struct sk_buff *ethnl_reply_init(size_t payload, struct net_device *dev, u8 cmd,
20 u16 hdr_attrtype, struct genl_info *info,
22 void *ethnl_dump_put(struct sk_buff *skb, struct netlink_callback *cb, u8 cmd);
23 void *ethnl_bcastmsg_put(struct sk_buff *skb, u8 cmd);
24 int ethnl_multicast(struct sk_buff *skb, struct net_device *dev);
27 * ethnl_strz_size() - calculate attribute length for fixed size string
28 * @s: ETH_GSTRING_LEN sized string (may not be null terminated)
30 * Return: total length of an attribute with null terminated string from @s
32 static inline int ethnl_strz_size(const char *s)
34 return nla_total_size(strnlen(s, ETH_GSTRING_LEN) + 1);
38 * ethnl_put_strz() - put string attribute with fixed size string
39 * @skb: skb with the message
40 * @attrtype: attribute type
41 * @s: ETH_GSTRING_LEN sized string (may not be null terminated)
43 * Puts an attribute with null terminated string from @s into the message.
45 * Return: 0 on success, negative error code on failure
47 static inline int ethnl_put_strz(struct sk_buff *skb, u16 attrtype,
50 unsigned int len = strnlen(s, ETH_GSTRING_LEN);
53 attr = nla_reserve(skb, attrtype, len + 1);
57 memcpy(nla_data(attr), s, len);
58 ((char *)nla_data(attr))[len] = '\0';
63 * ethnl_update_u32() - update u32 value from NLA_U32 attribute
64 * @dst: value to update
65 * @attr: netlink attribute with new value or null
66 * @mod: pointer to bool for modification tracking
68 * Copy the u32 value from NLA_U32 netlink attribute @attr into variable
69 * pointed to by @dst; do nothing if @attr is null. Bool pointed to by @mod
70 * is set to true if this function changed the value of *dst, otherwise it
73 static inline void ethnl_update_u32(u32 *dst, const struct nlattr *attr,
80 val = nla_get_u32(attr);
89 * ethnl_update_u8() - update u8 value from NLA_U8 attribute
90 * @dst: value to update
91 * @attr: netlink attribute with new value or null
92 * @mod: pointer to bool for modification tracking
94 * Copy the u8 value from NLA_U8 netlink attribute @attr into variable
95 * pointed to by @dst; do nothing if @attr is null. Bool pointed to by @mod
96 * is set to true if this function changed the value of *dst, otherwise it
99 static inline void ethnl_update_u8(u8 *dst, const struct nlattr *attr,
106 val = nla_get_u8(attr);
115 * ethnl_update_bool32() - update u32 used as bool from NLA_U8 attribute
116 * @dst: value to update
117 * @attr: netlink attribute with new value or null
118 * @mod: pointer to bool for modification tracking
120 * Use the u8 value from NLA_U8 netlink attribute @attr to set u32 variable
121 * pointed to by @dst to 0 (if zero) or 1 (if not); do nothing if @attr is
122 * null. Bool pointed to by @mod is set to true if this function changed the
123 * logical value of *dst, otherwise it is left as is.
125 static inline void ethnl_update_bool32(u32 *dst, const struct nlattr *attr,
132 val = !!nla_get_u8(attr);
141 * ethnl_update_bool() - updateb bool used as bool from NLA_U8 attribute
142 * @dst: value to update
143 * @attr: netlink attribute with new value or null
144 * @mod: pointer to bool for modification tracking
146 * Use the bool value from NLA_U8 netlink attribute @attr to set bool variable
147 * pointed to by @dst to 0 (if zero) or 1 (if not); do nothing if @attr is
148 * null. Bool pointed to by @mod is set to true if this function changed the
149 * logical value of *dst, otherwise it is left as is.
151 static inline void ethnl_update_bool(bool *dst, const struct nlattr *attr,
158 val = !!nla_get_u8(attr);
167 * ethnl_update_binary() - update binary data from NLA_BINARY attribute
168 * @dst: value to update
169 * @len: destination buffer length
170 * @attr: netlink attribute with new value or null
171 * @mod: pointer to bool for modification tracking
173 * Use the u8 value from NLA_U8 netlink attribute @attr to rewrite data block
174 * of length @len at @dst by attribute payload; do nothing if @attr is null.
175 * Bool pointed to by @mod is set to true if this function changed the logical
176 * value of *dst, otherwise it is left as is.
178 static inline void ethnl_update_binary(void *dst, unsigned int len,
179 const struct nlattr *attr, bool *mod)
183 if (nla_len(attr) < len)
185 if (!memcmp(dst, nla_data(attr), len))
188 memcpy(dst, nla_data(attr), len);
193 * ethnl_update_bitfield32() - update u32 value from NLA_BITFIELD32 attribute
194 * @dst: value to update
195 * @attr: netlink attribute with new value or null
196 * @mod: pointer to bool for modification tracking
198 * Update bits in u32 value which are set in attribute's mask to values from
199 * attribute's value. Do nothing if @attr is null or the value wouldn't change;
200 * otherwise, set bool pointed to by @mod to true.
202 static inline void ethnl_update_bitfield32(u32 *dst, const struct nlattr *attr,
205 struct nla_bitfield32 change;
210 change = nla_get_bitfield32(attr);
211 newval = (*dst & ~change.selector) | (change.value & change.selector);
220 * ethnl_reply_header_size() - total size of reply header
222 * This is an upper estimate so that we do not need to hold RTNL lock longer
223 * than necessary (to prevent rename between size estimate and composing the
224 * message). Accounts only for device ifindex and name as those are the only
225 * attributes ethnl_fill_reply_header() puts into the reply header.
227 static inline unsigned int ethnl_reply_header_size(void)
229 return nla_total_size(nla_total_size(sizeof(u32)) +
230 nla_total_size(IFNAMSIZ));
233 /* GET request handling */
235 /* Unified processing of GET requests uses two data structures: request info
236 * and reply data. Request info holds information parsed from client request
237 * and its stays constant through all request processing. Reply data holds data
238 * retrieved from ethtool_ops callbacks or other internal sources which is used
239 * to compose the reply. When processing a dump request, request info is filled
240 * only once (when the request message is parsed) but reply data is filled for
241 * each reply message.
243 * Both structures consist of part common for all request types (struct
244 * ethnl_req_info and struct ethnl_reply_data defined below) and optional
245 * parts specific for each request type. Common part always starts at offset 0.
249 * struct ethnl_req_info - base type of request information for GET requests
250 * @dev: network device the request is for (may be null)
251 * @dev_tracker: refcount tracker for @dev reference
252 * @flags: request flags common for all request types
254 * This is a common base for request specific structures holding data from
255 * parsed userspace request. These always embed struct ethnl_req_info at
258 struct ethnl_req_info {
259 struct net_device *dev;
260 netdevice_tracker dev_tracker;
264 static inline void ethnl_parse_header_dev_put(struct ethnl_req_info *req_info)
266 netdev_put(req_info->dev, &req_info->dev_tracker);
270 * struct ethnl_reply_data - base type of reply data for GET requests
271 * @dev: device for current reply message; in single shot requests it is
272 * equal to ðnl_req_info.dev; in dumps it's different for each
275 * This is a common base for request specific structures holding data for
276 * kernel reply message. These always embed struct ethnl_reply_data at zero
279 struct ethnl_reply_data {
280 struct net_device *dev;
283 int ethnl_ops_begin(struct net_device *dev);
284 void ethnl_ops_complete(struct net_device *dev);
287 * struct ethnl_request_ops - unified handling of GET requests
288 * @request_cmd: command id for request (GET)
289 * @reply_cmd: command id for reply (GET_REPLY)
290 * @hdr_attr: attribute type for request header
291 * @req_info_size: size of request info
292 * @reply_data_size: size of reply data
293 * @allow_nodev_do: allow non-dump request with no device identification
295 * Parse request except common header (struct ethnl_req_info). Common
296 * header is already filled on entry, the rest up to @repdata_offset
297 * is zero initialized. This callback should only modify type specific
298 * request info by parsed attributes from request message.
300 * Retrieve and prepare data needed to compose a reply message. Calls to
301 * ethtool_ops handlers are limited to this callback. Common reply data
302 * (struct ethnl_reply_data) is filled on entry, type specific part after
303 * it is zero initialized. This callback should only modify the type
304 * specific part of reply data. Device identification from struct
305 * ethnl_reply_data is to be used as for dump requests, it iterates
306 * through network devices while dev member of struct ethnl_req_info
307 * points to the device from client request.
309 * Estimate reply message size. Returned value must be sufficient for
310 * message payload without common reply header. The callback may returned
311 * estimate higher than actual message size if exact calculation would
312 * not be worth the saved memory space.
314 * Fill reply message payload (except for common header) from reply data.
315 * The callback must not generate more payload than previously called
316 * ->reply_size() estimated.
318 * Optional cleanup called when reply data is no longer needed. Can be
319 * used e.g. to free any additional data structures outside the main
320 * structure which were allocated by ->prepare_data(). When processing
321 * dump requests, ->cleanup() is called for each message.
323 * Description of variable parts of GET request handling when using the
324 * unified infrastructure. When used, a pointer to an instance of this
325 * structure is to be added to ðnl_default_requests array and generic
326 * handlers ethnl_default_doit(), ethnl_default_dumpit(),
327 * ethnl_default_start() and ethnl_default_done() used in @ethtool_genl_ops;
328 * ethnl_default_notify() can be used in @ethnl_notify_handlers to send
329 * notifications of the corresponding type.
331 struct ethnl_request_ops {
335 unsigned int req_info_size;
336 unsigned int reply_data_size;
339 int (*parse_request)(struct ethnl_req_info *req_info,
341 struct netlink_ext_ack *extack);
342 int (*prepare_data)(const struct ethnl_req_info *req_info,
343 struct ethnl_reply_data *reply_data,
344 struct genl_info *info);
345 int (*reply_size)(const struct ethnl_req_info *req_info,
346 const struct ethnl_reply_data *reply_data);
347 int (*fill_reply)(struct sk_buff *skb,
348 const struct ethnl_req_info *req_info,
349 const struct ethnl_reply_data *reply_data);
350 void (*cleanup_data)(struct ethnl_reply_data *reply_data);
353 /* request handlers */
355 extern const struct ethnl_request_ops ethnl_strset_request_ops;
356 extern const struct ethnl_request_ops ethnl_linkinfo_request_ops;
357 extern const struct ethnl_request_ops ethnl_linkmodes_request_ops;
358 extern const struct ethnl_request_ops ethnl_linkstate_request_ops;
359 extern const struct ethnl_request_ops ethnl_debug_request_ops;
360 extern const struct ethnl_request_ops ethnl_wol_request_ops;
361 extern const struct ethnl_request_ops ethnl_features_request_ops;
362 extern const struct ethnl_request_ops ethnl_privflags_request_ops;
363 extern const struct ethnl_request_ops ethnl_rings_request_ops;
364 extern const struct ethnl_request_ops ethnl_channels_request_ops;
365 extern const struct ethnl_request_ops ethnl_coalesce_request_ops;
366 extern const struct ethnl_request_ops ethnl_pause_request_ops;
367 extern const struct ethnl_request_ops ethnl_eee_request_ops;
368 extern const struct ethnl_request_ops ethnl_tsinfo_request_ops;
369 extern const struct ethnl_request_ops ethnl_fec_request_ops;
370 extern const struct ethnl_request_ops ethnl_module_eeprom_request_ops;
371 extern const struct ethnl_request_ops ethnl_stats_request_ops;
372 extern const struct ethnl_request_ops ethnl_phc_vclocks_request_ops;
373 extern const struct ethnl_request_ops ethnl_module_request_ops;
374 extern const struct ethnl_request_ops ethnl_pse_request_ops;
375 extern const struct ethnl_request_ops ethnl_rss_request_ops;
376 extern const struct ethnl_request_ops ethnl_plca_cfg_request_ops;
377 extern const struct ethnl_request_ops ethnl_plca_status_request_ops;
378 extern const struct ethnl_request_ops ethnl_mm_request_ops;
380 extern const struct nla_policy ethnl_header_policy[ETHTOOL_A_HEADER_FLAGS + 1];
381 extern const struct nla_policy ethnl_header_policy_stats[ETHTOOL_A_HEADER_FLAGS + 1];
382 extern const struct nla_policy ethnl_strset_get_policy[ETHTOOL_A_STRSET_COUNTS_ONLY + 1];
383 extern const struct nla_policy ethnl_linkinfo_get_policy[ETHTOOL_A_LINKINFO_HEADER + 1];
384 extern const struct nla_policy ethnl_linkinfo_set_policy[ETHTOOL_A_LINKINFO_TP_MDIX_CTRL + 1];
385 extern const struct nla_policy ethnl_linkmodes_get_policy[ETHTOOL_A_LINKMODES_HEADER + 1];
386 extern const struct nla_policy ethnl_linkmodes_set_policy[ETHTOOL_A_LINKMODES_LANES + 1];
387 extern const struct nla_policy ethnl_linkstate_get_policy[ETHTOOL_A_LINKSTATE_HEADER + 1];
388 extern const struct nla_policy ethnl_debug_get_policy[ETHTOOL_A_DEBUG_HEADER + 1];
389 extern const struct nla_policy ethnl_debug_set_policy[ETHTOOL_A_DEBUG_MSGMASK + 1];
390 extern const struct nla_policy ethnl_wol_get_policy[ETHTOOL_A_WOL_HEADER + 1];
391 extern const struct nla_policy ethnl_wol_set_policy[ETHTOOL_A_WOL_SOPASS + 1];
392 extern const struct nla_policy ethnl_features_get_policy[ETHTOOL_A_FEATURES_HEADER + 1];
393 extern const struct nla_policy ethnl_features_set_policy[ETHTOOL_A_FEATURES_WANTED + 1];
394 extern const struct nla_policy ethnl_privflags_get_policy[ETHTOOL_A_PRIVFLAGS_HEADER + 1];
395 extern const struct nla_policy ethnl_privflags_set_policy[ETHTOOL_A_PRIVFLAGS_FLAGS + 1];
396 extern const struct nla_policy ethnl_rings_get_policy[ETHTOOL_A_RINGS_HEADER + 1];
397 extern const struct nla_policy ethnl_rings_set_policy[ETHTOOL_A_RINGS_TX_PUSH + 1];
398 extern const struct nla_policy ethnl_channels_get_policy[ETHTOOL_A_CHANNELS_HEADER + 1];
399 extern const struct nla_policy ethnl_channels_set_policy[ETHTOOL_A_CHANNELS_COMBINED_COUNT + 1];
400 extern const struct nla_policy ethnl_coalesce_get_policy[ETHTOOL_A_COALESCE_HEADER + 1];
401 extern const struct nla_policy ethnl_coalesce_set_policy[ETHTOOL_A_COALESCE_MAX + 1];
402 extern const struct nla_policy ethnl_pause_get_policy[ETHTOOL_A_PAUSE_STATS_SRC + 1];
403 extern const struct nla_policy ethnl_pause_set_policy[ETHTOOL_A_PAUSE_TX + 1];
404 extern const struct nla_policy ethnl_eee_get_policy[ETHTOOL_A_EEE_HEADER + 1];
405 extern const struct nla_policy ethnl_eee_set_policy[ETHTOOL_A_EEE_TX_LPI_TIMER + 1];
406 extern const struct nla_policy ethnl_tsinfo_get_policy[ETHTOOL_A_TSINFO_HEADER + 1];
407 extern const struct nla_policy ethnl_cable_test_act_policy[ETHTOOL_A_CABLE_TEST_HEADER + 1];
408 extern const struct nla_policy ethnl_cable_test_tdr_act_policy[ETHTOOL_A_CABLE_TEST_TDR_CFG + 1];
409 extern const struct nla_policy ethnl_tunnel_info_get_policy[ETHTOOL_A_TUNNEL_INFO_HEADER + 1];
410 extern const struct nla_policy ethnl_fec_get_policy[ETHTOOL_A_FEC_HEADER + 1];
411 extern const struct nla_policy ethnl_fec_set_policy[ETHTOOL_A_FEC_AUTO + 1];
412 extern const struct nla_policy ethnl_module_eeprom_get_policy[ETHTOOL_A_MODULE_EEPROM_I2C_ADDRESS + 1];
413 extern const struct nla_policy ethnl_stats_get_policy[ETHTOOL_A_STATS_SRC + 1];
414 extern const struct nla_policy ethnl_phc_vclocks_get_policy[ETHTOOL_A_PHC_VCLOCKS_HEADER + 1];
415 extern const struct nla_policy ethnl_module_get_policy[ETHTOOL_A_MODULE_HEADER + 1];
416 extern const struct nla_policy ethnl_module_set_policy[ETHTOOL_A_MODULE_POWER_MODE_POLICY + 1];
417 extern const struct nla_policy ethnl_pse_get_policy[ETHTOOL_A_PSE_HEADER + 1];
418 extern const struct nla_policy ethnl_pse_set_policy[ETHTOOL_A_PSE_MAX + 1];
419 extern const struct nla_policy ethnl_rss_get_policy[ETHTOOL_A_RSS_CONTEXT + 1];
420 extern const struct nla_policy ethnl_plca_get_cfg_policy[ETHTOOL_A_PLCA_HEADER + 1];
421 extern const struct nla_policy ethnl_plca_set_cfg_policy[ETHTOOL_A_PLCA_MAX + 1];
422 extern const struct nla_policy ethnl_plca_get_status_policy[ETHTOOL_A_PLCA_HEADER + 1];
423 extern const struct nla_policy ethnl_mm_get_policy[ETHTOOL_A_MM_HEADER + 1];
424 extern const struct nla_policy ethnl_mm_set_policy[ETHTOOL_A_MM_MAX + 1];
426 int ethnl_set_linkinfo(struct sk_buff *skb, struct genl_info *info);
427 int ethnl_set_linkmodes(struct sk_buff *skb, struct genl_info *info);
428 int ethnl_set_debug(struct sk_buff *skb, struct genl_info *info);
429 int ethnl_set_wol(struct sk_buff *skb, struct genl_info *info);
430 int ethnl_set_features(struct sk_buff *skb, struct genl_info *info);
431 int ethnl_set_privflags(struct sk_buff *skb, struct genl_info *info);
432 int ethnl_set_rings(struct sk_buff *skb, struct genl_info *info);
433 int ethnl_set_channels(struct sk_buff *skb, struct genl_info *info);
434 int ethnl_set_coalesce(struct sk_buff *skb, struct genl_info *info);
435 int ethnl_set_pause(struct sk_buff *skb, struct genl_info *info);
436 int ethnl_set_eee(struct sk_buff *skb, struct genl_info *info);
437 int ethnl_act_cable_test(struct sk_buff *skb, struct genl_info *info);
438 int ethnl_act_cable_test_tdr(struct sk_buff *skb, struct genl_info *info);
439 int ethnl_tunnel_info_doit(struct sk_buff *skb, struct genl_info *info);
440 int ethnl_tunnel_info_start(struct netlink_callback *cb);
441 int ethnl_tunnel_info_dumpit(struct sk_buff *skb, struct netlink_callback *cb);
442 int ethnl_set_fec(struct sk_buff *skb, struct genl_info *info);
443 int ethnl_set_module(struct sk_buff *skb, struct genl_info *info);
444 int ethnl_set_pse(struct sk_buff *skb, struct genl_info *info);
445 int ethnl_set_plca_cfg(struct sk_buff *skb, struct genl_info *info);
446 int ethnl_set_mm(struct sk_buff *skb, struct genl_info *info);
448 extern const char stats_std_names[__ETHTOOL_STATS_CNT][ETH_GSTRING_LEN];
449 extern const char stats_eth_phy_names[__ETHTOOL_A_STATS_ETH_PHY_CNT][ETH_GSTRING_LEN];
450 extern const char stats_eth_mac_names[__ETHTOOL_A_STATS_ETH_MAC_CNT][ETH_GSTRING_LEN];
451 extern const char stats_eth_ctrl_names[__ETHTOOL_A_STATS_ETH_CTRL_CNT][ETH_GSTRING_LEN];
452 extern const char stats_rmon_names[__ETHTOOL_A_STATS_RMON_CNT][ETH_GSTRING_LEN];
454 #endif /* _NET_ETHTOOL_NETLINK_H */