Loading...
1/* SPDX-License-Identifier: GPL-2.0-only */
2
3#ifndef _NET_ETHTOOL_NETLINK_H
4#define _NET_ETHTOOL_NETLINK_H
5
6#include <linux/ethtool_netlink.h>
7#include <linux/netdevice.h>
8#include <net/genetlink.h>
9#include <net/sock.h>
10
11struct ethnl_req_info;
12
13int 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,
16 bool require_dev);
17int ethnl_fill_reply_header(struct sk_buff *skb, struct net_device *dev,
18 u16 attrtype);
19struct sk_buff *ethnl_reply_init(size_t payload, struct net_device *dev, u8 cmd,
20 u16 hdr_attrtype, struct genl_info *info,
21 void **ehdrp);
22void *ethnl_dump_put(struct sk_buff *skb, struct netlink_callback *cb, u8 cmd);
23void *ethnl_bcastmsg_put(struct sk_buff *skb, u8 cmd);
24void *ethnl_unicast_put(struct sk_buff *skb, u32 portid, u32 seq, u8 cmd);
25int ethnl_multicast(struct sk_buff *skb, struct net_device *dev);
26
27/**
28 * ethnl_strz_size() - calculate attribute length for fixed size string
29 * @s: ETH_GSTRING_LEN sized string (may not be null terminated)
30 *
31 * Return: total length of an attribute with null terminated string from @s
32 */
33static inline int ethnl_strz_size(const char *s)
34{
35 return nla_total_size(strnlen(s, ETH_GSTRING_LEN) + 1);
36}
37
38/**
39 * ethnl_put_strz() - put string attribute with fixed size string
40 * @skb: skb with the message
41 * @attrtype: attribute type
42 * @s: ETH_GSTRING_LEN sized string (may not be null terminated)
43 *
44 * Puts an attribute with null terminated string from @s into the message.
45 *
46 * Return: 0 on success, negative error code on failure
47 */
48static inline int ethnl_put_strz(struct sk_buff *skb, u16 attrtype,
49 const char *s)
50{
51 unsigned int len = strnlen(s, ETH_GSTRING_LEN);
52 struct nlattr *attr;
53
54 attr = nla_reserve(skb, attrtype, len + 1);
55 if (!attr)
56 return -EMSGSIZE;
57
58 memcpy(nla_data(attr), s, len);
59 ((char *)nla_data(attr))[len] = '\0';
60 return 0;
61}
62
63/**
64 * ethnl_update_u32() - update u32 value from NLA_U32 attribute
65 * @dst: value to update
66 * @attr: netlink attribute with new value or null
67 * @mod: pointer to bool for modification tracking
68 *
69 * Copy the u32 value from NLA_U32 netlink attribute @attr into variable
70 * pointed to by @dst; do nothing if @attr is null. Bool pointed to by @mod
71 * is set to true if this function changed the value of *dst, otherwise it
72 * is left as is.
73 */
74static inline void ethnl_update_u32(u32 *dst, const struct nlattr *attr,
75 bool *mod)
76{
77 u32 val;
78
79 if (!attr)
80 return;
81 val = nla_get_u32(attr);
82 if (*dst == val)
83 return;
84
85 *dst = val;
86 *mod = true;
87}
88
89/**
90 * ethnl_update_u8() - update u8 value from NLA_U8 attribute
91 * @dst: value to update
92 * @attr: netlink attribute with new value or null
93 * @mod: pointer to bool for modification tracking
94 *
95 * Copy the u8 value from NLA_U8 netlink attribute @attr into variable
96 * pointed to by @dst; do nothing if @attr is null. Bool pointed to by @mod
97 * is set to true if this function changed the value of *dst, otherwise it
98 * is left as is.
99 */
100static inline void ethnl_update_u8(u8 *dst, const struct nlattr *attr,
101 bool *mod)
102{
103 u8 val;
104
105 if (!attr)
106 return;
107 val = nla_get_u8(attr);
108 if (*dst == val)
109 return;
110
111 *dst = val;
112 *mod = true;
113}
114
115/**
116 * ethnl_update_bool32() - update u32 used as bool from NLA_U8 attribute
117 * @dst: value to update
118 * @attr: netlink attribute with new value or null
119 * @mod: pointer to bool for modification tracking
120 *
121 * Use the u8 value from NLA_U8 netlink attribute @attr to set u32 variable
122 * pointed to by @dst to 0 (if zero) or 1 (if not); do nothing if @attr is
123 * null. Bool pointed to by @mod is set to true if this function changed the
124 * logical value of *dst, otherwise it is left as is.
125 */
126static inline void ethnl_update_bool32(u32 *dst, const struct nlattr *attr,
127 bool *mod)
128{
129 u8 val;
130
131 if (!attr)
132 return;
133 val = !!nla_get_u8(attr);
134 if (!!*dst == val)
135 return;
136
137 *dst = val;
138 *mod = true;
139}
140
141/**
142 * ethnl_update_bool() - updateb bool used as bool from NLA_U8 attribute
143 * @dst: value to update
144 * @attr: netlink attribute with new value or null
145 * @mod: pointer to bool for modification tracking
146 *
147 * Use the bool value from NLA_U8 netlink attribute @attr to set bool variable
148 * pointed to by @dst to 0 (if zero) or 1 (if not); do nothing if @attr is
149 * null. Bool pointed to by @mod is set to true if this function changed the
150 * logical value of *dst, otherwise it is left as is.
151 */
152static inline void ethnl_update_bool(bool *dst, const struct nlattr *attr,
153 bool *mod)
154{
155 u8 val;
156
157 if (!attr)
158 return;
159 val = !!nla_get_u8(attr);
160 if (!!*dst == val)
161 return;
162
163 *dst = val;
164 *mod = true;
165}
166
167/**
168 * ethnl_update_binary() - update binary data from NLA_BINARY attribute
169 * @dst: value to update
170 * @len: destination buffer length
171 * @attr: netlink attribute with new value or null
172 * @mod: pointer to bool for modification tracking
173 *
174 * Use the u8 value from NLA_U8 netlink attribute @attr to rewrite data block
175 * of length @len at @dst by attribute payload; do nothing if @attr is null.
176 * Bool pointed to by @mod is set to true if this function changed the logical
177 * value of *dst, otherwise it is left as is.
178 */
179static inline void ethnl_update_binary(void *dst, unsigned int len,
180 const struct nlattr *attr, bool *mod)
181{
182 if (!attr)
183 return;
184 if (nla_len(attr) < len)
185 len = nla_len(attr);
186 if (!memcmp(dst, nla_data(attr), len))
187 return;
188
189 memcpy(dst, nla_data(attr), len);
190 *mod = true;
191}
192
193/**
194 * ethnl_update_bitfield32() - update u32 value from NLA_BITFIELD32 attribute
195 * @dst: value to update
196 * @attr: netlink attribute with new value or null
197 * @mod: pointer to bool for modification tracking
198 *
199 * Update bits in u32 value which are set in attribute's mask to values from
200 * attribute's value. Do nothing if @attr is null or the value wouldn't change;
201 * otherwise, set bool pointed to by @mod to true.
202 */
203static inline void ethnl_update_bitfield32(u32 *dst, const struct nlattr *attr,
204 bool *mod)
205{
206 struct nla_bitfield32 change;
207 u32 newval;
208
209 if (!attr)
210 return;
211 change = nla_get_bitfield32(attr);
212 newval = (*dst & ~change.selector) | (change.value & change.selector);
213 if (*dst == newval)
214 return;
215
216 *dst = newval;
217 *mod = true;
218}
219
220/**
221 * ethnl_reply_header_size() - total size of reply header
222 *
223 * This is an upper estimate so that we do not need to hold RTNL lock longer
224 * than necessary (to prevent rename between size estimate and composing the
225 * message). Accounts only for device ifindex and name as those are the only
226 * attributes ethnl_fill_reply_header() puts into the reply header.
227 */
228static inline unsigned int ethnl_reply_header_size(void)
229{
230 return nla_total_size(nla_total_size(sizeof(u32)) +
231 nla_total_size(IFNAMSIZ));
232}
233
234/* GET request handling */
235
236/* Unified processing of GET requests uses two data structures: request info
237 * and reply data. Request info holds information parsed from client request
238 * and its stays constant through all request processing. Reply data holds data
239 * retrieved from ethtool_ops callbacks or other internal sources which is used
240 * to compose the reply. When processing a dump request, request info is filled
241 * only once (when the request message is parsed) but reply data is filled for
242 * each reply message.
243 *
244 * Both structures consist of part common for all request types (struct
245 * ethnl_req_info and struct ethnl_reply_data defined below) and optional
246 * parts specific for each request type. Common part always starts at offset 0.
247 */
248
249/**
250 * struct ethnl_req_info - base type of request information for GET requests
251 * @dev: network device the request is for (may be null)
252 * @dev_tracker: refcount tracker for @dev reference
253 * @flags: request flags common for all request types
254 * @phy_index: phy_device index connected to @dev this request is for. Can be
255 * 0 if the request doesn't target a phy, or if the @dev's attached
256 * phy is targeted.
257 *
258 * This is a common base for request specific structures holding data from
259 * parsed userspace request. These always embed struct ethnl_req_info at
260 * zero offset.
261 */
262struct ethnl_req_info {
263 struct net_device *dev;
264 netdevice_tracker dev_tracker;
265 u32 flags;
266 u32 phy_index;
267};
268
269static inline void ethnl_parse_header_dev_put(struct ethnl_req_info *req_info)
270{
271 netdev_put(req_info->dev, &req_info->dev_tracker);
272}
273
274/**
275 * ethnl_req_get_phydev() - Gets the phy_device targeted by this request,
276 * if any. Must be called under rntl_lock().
277 * @req_info: The ethnl request to get the phy from.
278 * @tb: The netlink attributes array, for error reporting.
279 * @header: The netlink header index, used for error reporting.
280 * @extack: The netlink extended ACK, for error reporting.
281 *
282 * The caller must hold RTNL, until it's done interacting with the returned
283 * phy_device.
284 *
285 * Return: A phy_device pointer corresponding either to the passed phy_index
286 * if one is provided. If not, the phy_device attached to the
287 * net_device targeted by this request is returned. If there's no
288 * targeted net_device, or no phy_device is attached, NULL is
289 * returned. If the provided phy_index is invalid, an error pointer
290 * is returned.
291 */
292struct phy_device *ethnl_req_get_phydev(const struct ethnl_req_info *req_info,
293 struct nlattr **tb, unsigned int header,
294 struct netlink_ext_ack *extack);
295
296/**
297 * struct ethnl_reply_data - base type of reply data for GET requests
298 * @dev: device for current reply message; in single shot requests it is
299 * equal to ðnl_req_info.dev; in dumps it's different for each
300 * reply message
301 *
302 * This is a common base for request specific structures holding data for
303 * kernel reply message. These always embed struct ethnl_reply_data at zero
304 * offset.
305 */
306struct ethnl_reply_data {
307 struct net_device *dev;
308};
309
310int ethnl_ops_begin(struct net_device *dev);
311void ethnl_ops_complete(struct net_device *dev);
312
313enum ethnl_sock_type {
314 ETHTOOL_SOCK_TYPE_MODULE_FW_FLASH,
315};
316
317struct ethnl_sock_priv {
318 struct net_device *dev;
319 u32 portid;
320 enum ethnl_sock_type type;
321};
322
323int ethnl_sock_priv_set(struct sk_buff *skb, struct net_device *dev, u32 portid,
324 enum ethnl_sock_type type);
325
326/**
327 * struct ethnl_request_ops - unified handling of GET and SET requests
328 * @request_cmd: command id for request (GET)
329 * @reply_cmd: command id for reply (GET_REPLY)
330 * @hdr_attr: attribute type for request header
331 * @req_info_size: size of request info
332 * @reply_data_size: size of reply data
333 * @allow_nodev_do: allow non-dump request with no device identification
334 * @set_ntf_cmd: notification to generate on changes (SET)
335 * @parse_request:
336 * Parse request except common header (struct ethnl_req_info). Common
337 * header is already filled on entry, the rest up to @repdata_offset
338 * is zero initialized. This callback should only modify type specific
339 * request info by parsed attributes from request message.
340 * @prepare_data:
341 * Retrieve and prepare data needed to compose a reply message. Calls to
342 * ethtool_ops handlers are limited to this callback. Common reply data
343 * (struct ethnl_reply_data) is filled on entry, type specific part after
344 * it is zero initialized. This callback should only modify the type
345 * specific part of reply data. Device identification from struct
346 * ethnl_reply_data is to be used as for dump requests, it iterates
347 * through network devices while dev member of struct ethnl_req_info
348 * points to the device from client request.
349 * @reply_size:
350 * Estimate reply message size. Returned value must be sufficient for
351 * message payload without common reply header. The callback may returned
352 * estimate higher than actual message size if exact calculation would
353 * not be worth the saved memory space.
354 * @fill_reply:
355 * Fill reply message payload (except for common header) from reply data.
356 * The callback must not generate more payload than previously called
357 * ->reply_size() estimated.
358 * @cleanup_data:
359 * Optional cleanup called when reply data is no longer needed. Can be
360 * used e.g. to free any additional data structures outside the main
361 * structure which were allocated by ->prepare_data(). When processing
362 * dump requests, ->cleanup() is called for each message.
363 * @set_validate:
364 * Check if set operation is supported for a given device, and perform
365 * extra input checks. Expected return values:
366 * - 0 if the operation is a noop for the device (rare)
367 * - 1 if operation should proceed to calling @set
368 * - negative errno on errors
369 * Called without any locks, just a reference on the netdev.
370 * @set:
371 * Execute the set operation. The implementation should return
372 * - 0 if no configuration has changed
373 * - 1 if configuration changed and notification should be generated
374 * - negative errno on errors
375 *
376 * Description of variable parts of GET request handling when using the
377 * unified infrastructure. When used, a pointer to an instance of this
378 * structure is to be added to ðnl_default_requests array and generic
379 * handlers ethnl_default_doit(), ethnl_default_dumpit(),
380 * ethnl_default_start() and ethnl_default_done() used in @ethtool_genl_ops;
381 * ethnl_default_notify() can be used in @ethnl_notify_handlers to send
382 * notifications of the corresponding type.
383 */
384struct ethnl_request_ops {
385 u8 request_cmd;
386 u8 reply_cmd;
387 u16 hdr_attr;
388 unsigned int req_info_size;
389 unsigned int reply_data_size;
390 bool allow_nodev_do;
391 u8 set_ntf_cmd;
392
393 int (*parse_request)(struct ethnl_req_info *req_info,
394 struct nlattr **tb,
395 struct netlink_ext_ack *extack);
396 int (*prepare_data)(const struct ethnl_req_info *req_info,
397 struct ethnl_reply_data *reply_data,
398 const struct genl_info *info);
399 int (*reply_size)(const struct ethnl_req_info *req_info,
400 const struct ethnl_reply_data *reply_data);
401 int (*fill_reply)(struct sk_buff *skb,
402 const struct ethnl_req_info *req_info,
403 const struct ethnl_reply_data *reply_data);
404 void (*cleanup_data)(struct ethnl_reply_data *reply_data);
405
406 int (*set_validate)(struct ethnl_req_info *req_info,
407 struct genl_info *info);
408 int (*set)(struct ethnl_req_info *req_info,
409 struct genl_info *info);
410};
411
412/* request handlers */
413
414extern const struct ethnl_request_ops ethnl_strset_request_ops;
415extern const struct ethnl_request_ops ethnl_linkinfo_request_ops;
416extern const struct ethnl_request_ops ethnl_linkmodes_request_ops;
417extern const struct ethnl_request_ops ethnl_linkstate_request_ops;
418extern const struct ethnl_request_ops ethnl_debug_request_ops;
419extern const struct ethnl_request_ops ethnl_wol_request_ops;
420extern const struct ethnl_request_ops ethnl_features_request_ops;
421extern const struct ethnl_request_ops ethnl_privflags_request_ops;
422extern const struct ethnl_request_ops ethnl_rings_request_ops;
423extern const struct ethnl_request_ops ethnl_channels_request_ops;
424extern const struct ethnl_request_ops ethnl_coalesce_request_ops;
425extern const struct ethnl_request_ops ethnl_pause_request_ops;
426extern const struct ethnl_request_ops ethnl_eee_request_ops;
427extern const struct ethnl_request_ops ethnl_tsinfo_request_ops;
428extern const struct ethnl_request_ops ethnl_fec_request_ops;
429extern const struct ethnl_request_ops ethnl_module_eeprom_request_ops;
430extern const struct ethnl_request_ops ethnl_stats_request_ops;
431extern const struct ethnl_request_ops ethnl_phc_vclocks_request_ops;
432extern const struct ethnl_request_ops ethnl_module_request_ops;
433extern const struct ethnl_request_ops ethnl_pse_request_ops;
434extern const struct ethnl_request_ops ethnl_rss_request_ops;
435extern const struct ethnl_request_ops ethnl_plca_cfg_request_ops;
436extern const struct ethnl_request_ops ethnl_plca_status_request_ops;
437extern const struct ethnl_request_ops ethnl_mm_request_ops;
438extern const struct ethnl_request_ops ethnl_phy_request_ops;
439
440extern const struct nla_policy ethnl_header_policy[ETHTOOL_A_HEADER_FLAGS + 1];
441extern const struct nla_policy ethnl_header_policy_stats[ETHTOOL_A_HEADER_FLAGS + 1];
442extern const struct nla_policy ethnl_header_policy_phy[ETHTOOL_A_HEADER_PHY_INDEX + 1];
443extern const struct nla_policy ethnl_header_policy_phy_stats[ETHTOOL_A_HEADER_PHY_INDEX + 1];
444extern const struct nla_policy ethnl_strset_get_policy[ETHTOOL_A_STRSET_COUNTS_ONLY + 1];
445extern const struct nla_policy ethnl_linkinfo_get_policy[ETHTOOL_A_LINKINFO_HEADER + 1];
446extern const struct nla_policy ethnl_linkinfo_set_policy[ETHTOOL_A_LINKINFO_TP_MDIX_CTRL + 1];
447extern const struct nla_policy ethnl_linkmodes_get_policy[ETHTOOL_A_LINKMODES_HEADER + 1];
448extern const struct nla_policy ethnl_linkmodes_set_policy[ETHTOOL_A_LINKMODES_LANES + 1];
449extern const struct nla_policy ethnl_linkstate_get_policy[ETHTOOL_A_LINKSTATE_HEADER + 1];
450extern const struct nla_policy ethnl_debug_get_policy[ETHTOOL_A_DEBUG_HEADER + 1];
451extern const struct nla_policy ethnl_debug_set_policy[ETHTOOL_A_DEBUG_MSGMASK + 1];
452extern const struct nla_policy ethnl_wol_get_policy[ETHTOOL_A_WOL_HEADER + 1];
453extern const struct nla_policy ethnl_wol_set_policy[ETHTOOL_A_WOL_SOPASS + 1];
454extern const struct nla_policy ethnl_features_get_policy[ETHTOOL_A_FEATURES_HEADER + 1];
455extern const struct nla_policy ethnl_features_set_policy[ETHTOOL_A_FEATURES_WANTED + 1];
456extern const struct nla_policy ethnl_privflags_get_policy[ETHTOOL_A_PRIVFLAGS_HEADER + 1];
457extern const struct nla_policy ethnl_privflags_set_policy[ETHTOOL_A_PRIVFLAGS_FLAGS + 1];
458extern const struct nla_policy ethnl_rings_get_policy[ETHTOOL_A_RINGS_HEADER + 1];
459extern const struct nla_policy ethnl_rings_set_policy[ETHTOOL_A_RINGS_TX_PUSH_BUF_LEN_MAX + 1];
460extern const struct nla_policy ethnl_channels_get_policy[ETHTOOL_A_CHANNELS_HEADER + 1];
461extern const struct nla_policy ethnl_channels_set_policy[ETHTOOL_A_CHANNELS_COMBINED_COUNT + 1];
462extern const struct nla_policy ethnl_coalesce_get_policy[ETHTOOL_A_COALESCE_HEADER + 1];
463extern const struct nla_policy ethnl_coalesce_set_policy[ETHTOOL_A_COALESCE_MAX + 1];
464extern const struct nla_policy ethnl_pause_get_policy[ETHTOOL_A_PAUSE_STATS_SRC + 1];
465extern const struct nla_policy ethnl_pause_set_policy[ETHTOOL_A_PAUSE_TX + 1];
466extern const struct nla_policy ethnl_eee_get_policy[ETHTOOL_A_EEE_HEADER + 1];
467extern const struct nla_policy ethnl_eee_set_policy[ETHTOOL_A_EEE_TX_LPI_TIMER + 1];
468extern const struct nla_policy ethnl_tsinfo_get_policy[ETHTOOL_A_TSINFO_HEADER + 1];
469extern const struct nla_policy ethnl_cable_test_act_policy[ETHTOOL_A_CABLE_TEST_HEADER + 1];
470extern const struct nla_policy ethnl_cable_test_tdr_act_policy[ETHTOOL_A_CABLE_TEST_TDR_CFG + 1];
471extern const struct nla_policy ethnl_tunnel_info_get_policy[ETHTOOL_A_TUNNEL_INFO_HEADER + 1];
472extern const struct nla_policy ethnl_fec_get_policy[ETHTOOL_A_FEC_HEADER + 1];
473extern const struct nla_policy ethnl_fec_set_policy[ETHTOOL_A_FEC_AUTO + 1];
474extern const struct nla_policy ethnl_module_eeprom_get_policy[ETHTOOL_A_MODULE_EEPROM_I2C_ADDRESS + 1];
475extern const struct nla_policy ethnl_stats_get_policy[ETHTOOL_A_STATS_SRC + 1];
476extern const struct nla_policy ethnl_phc_vclocks_get_policy[ETHTOOL_A_PHC_VCLOCKS_HEADER + 1];
477extern const struct nla_policy ethnl_module_get_policy[ETHTOOL_A_MODULE_HEADER + 1];
478extern const struct nla_policy ethnl_module_set_policy[ETHTOOL_A_MODULE_POWER_MODE_POLICY + 1];
479extern const struct nla_policy ethnl_pse_get_policy[ETHTOOL_A_PSE_HEADER + 1];
480extern const struct nla_policy ethnl_pse_set_policy[ETHTOOL_A_PSE_MAX + 1];
481extern const struct nla_policy ethnl_rss_get_policy[ETHTOOL_A_RSS_START_CONTEXT + 1];
482extern const struct nla_policy ethnl_plca_get_cfg_policy[ETHTOOL_A_PLCA_HEADER + 1];
483extern const struct nla_policy ethnl_plca_set_cfg_policy[ETHTOOL_A_PLCA_MAX + 1];
484extern const struct nla_policy ethnl_plca_get_status_policy[ETHTOOL_A_PLCA_HEADER + 1];
485extern const struct nla_policy ethnl_mm_get_policy[ETHTOOL_A_MM_HEADER + 1];
486extern const struct nla_policy ethnl_mm_set_policy[ETHTOOL_A_MM_MAX + 1];
487extern const struct nla_policy ethnl_module_fw_flash_act_policy[ETHTOOL_A_MODULE_FW_FLASH_PASSWORD + 1];
488extern const struct nla_policy ethnl_phy_get_policy[ETHTOOL_A_PHY_HEADER + 1];
489
490int ethnl_set_features(struct sk_buff *skb, struct genl_info *info);
491int ethnl_act_cable_test(struct sk_buff *skb, struct genl_info *info);
492int ethnl_act_cable_test_tdr(struct sk_buff *skb, struct genl_info *info);
493int ethnl_tunnel_info_doit(struct sk_buff *skb, struct genl_info *info);
494int ethnl_tunnel_info_start(struct netlink_callback *cb);
495int ethnl_tunnel_info_dumpit(struct sk_buff *skb, struct netlink_callback *cb);
496int ethnl_act_module_fw_flash(struct sk_buff *skb, struct genl_info *info);
497int ethnl_rss_dump_start(struct netlink_callback *cb);
498int ethnl_rss_dumpit(struct sk_buff *skb, struct netlink_callback *cb);
499int ethnl_phy_start(struct netlink_callback *cb);
500int ethnl_phy_doit(struct sk_buff *skb, struct genl_info *info);
501int ethnl_phy_dumpit(struct sk_buff *skb, struct netlink_callback *cb);
502int ethnl_phy_done(struct netlink_callback *cb);
503
504extern const char stats_std_names[__ETHTOOL_STATS_CNT][ETH_GSTRING_LEN];
505extern const char stats_eth_phy_names[__ETHTOOL_A_STATS_ETH_PHY_CNT][ETH_GSTRING_LEN];
506extern const char stats_eth_mac_names[__ETHTOOL_A_STATS_ETH_MAC_CNT][ETH_GSTRING_LEN];
507extern const char stats_eth_ctrl_names[__ETHTOOL_A_STATS_ETH_CTRL_CNT][ETH_GSTRING_LEN];
508extern const char stats_rmon_names[__ETHTOOL_A_STATS_RMON_CNT][ETH_GSTRING_LEN];
509
510#endif /* _NET_ETHTOOL_NETLINK_H */
1/* SPDX-License-Identifier: GPL-2.0-only */
2
3#ifndef _NET_ETHTOOL_NETLINK_H
4#define _NET_ETHTOOL_NETLINK_H
5
6#include <linux/ethtool_netlink.h>
7#include <linux/netdevice.h>
8#include <net/genetlink.h>
9#include <net/sock.h>
10
11struct ethnl_req_info;
12
13int 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,
16 bool require_dev);
17int ethnl_fill_reply_header(struct sk_buff *skb, struct net_device *dev,
18 u16 attrtype);
19struct sk_buff *ethnl_reply_init(size_t payload, struct net_device *dev, u8 cmd,
20 u16 hdr_attrtype, struct genl_info *info,
21 void **ehdrp);
22void *ethnl_dump_put(struct sk_buff *skb, struct netlink_callback *cb, u8 cmd);
23void *ethnl_bcastmsg_put(struct sk_buff *skb, u8 cmd);
24int ethnl_multicast(struct sk_buff *skb, struct net_device *dev);
25
26/**
27 * ethnl_strz_size() - calculate attribute length for fixed size string
28 * @s: ETH_GSTRING_LEN sized string (may not be null terminated)
29 *
30 * Return: total length of an attribute with null terminated string from @s
31 */
32static inline int ethnl_strz_size(const char *s)
33{
34 return nla_total_size(strnlen(s, ETH_GSTRING_LEN) + 1);
35}
36
37/**
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)
42 *
43 * Puts an attribute with null terminated string from @s into the message.
44 *
45 * Return: 0 on success, negative error code on failure
46 */
47static inline int ethnl_put_strz(struct sk_buff *skb, u16 attrtype,
48 const char *s)
49{
50 unsigned int len = strnlen(s, ETH_GSTRING_LEN);
51 struct nlattr *attr;
52
53 attr = nla_reserve(skb, attrtype, len + 1);
54 if (!attr)
55 return -EMSGSIZE;
56
57 memcpy(nla_data(attr), s, len);
58 ((char *)nla_data(attr))[len] = '\0';
59 return 0;
60}
61
62/**
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
67 *
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
71 * is left as is.
72 */
73static inline void ethnl_update_u32(u32 *dst, const struct nlattr *attr,
74 bool *mod)
75{
76 u32 val;
77
78 if (!attr)
79 return;
80 val = nla_get_u32(attr);
81 if (*dst == val)
82 return;
83
84 *dst = val;
85 *mod = true;
86}
87
88/**
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
93 *
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
97 * is left as is.
98 */
99static inline void ethnl_update_u8(u8 *dst, const struct nlattr *attr,
100 bool *mod)
101{
102 u8 val;
103
104 if (!attr)
105 return;
106 val = nla_get_u8(attr);
107 if (*dst == val)
108 return;
109
110 *dst = val;
111 *mod = true;
112}
113
114/**
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
119 *
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.
124 */
125static inline void ethnl_update_bool32(u32 *dst, const struct nlattr *attr,
126 bool *mod)
127{
128 u8 val;
129
130 if (!attr)
131 return;
132 val = !!nla_get_u8(attr);
133 if (!!*dst == val)
134 return;
135
136 *dst = val;
137 *mod = true;
138}
139
140/**
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
145 *
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.
150 */
151static inline void ethnl_update_bool(bool *dst, const struct nlattr *attr,
152 bool *mod)
153{
154 u8 val;
155
156 if (!attr)
157 return;
158 val = !!nla_get_u8(attr);
159 if (!!*dst == val)
160 return;
161
162 *dst = val;
163 *mod = true;
164}
165
166/**
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
172 *
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.
177 */
178static inline void ethnl_update_binary(void *dst, unsigned int len,
179 const struct nlattr *attr, bool *mod)
180{
181 if (!attr)
182 return;
183 if (nla_len(attr) < len)
184 len = nla_len(attr);
185 if (!memcmp(dst, nla_data(attr), len))
186 return;
187
188 memcpy(dst, nla_data(attr), len);
189 *mod = true;
190}
191
192/**
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
197 *
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.
201 */
202static inline void ethnl_update_bitfield32(u32 *dst, const struct nlattr *attr,
203 bool *mod)
204{
205 struct nla_bitfield32 change;
206 u32 newval;
207
208 if (!attr)
209 return;
210 change = nla_get_bitfield32(attr);
211 newval = (*dst & ~change.selector) | (change.value & change.selector);
212 if (*dst == newval)
213 return;
214
215 *dst = newval;
216 *mod = true;
217}
218
219/**
220 * ethnl_reply_header_size() - total size of reply header
221 *
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.
226 */
227static inline unsigned int ethnl_reply_header_size(void)
228{
229 return nla_total_size(nla_total_size(sizeof(u32)) +
230 nla_total_size(IFNAMSIZ));
231}
232
233/* GET request handling */
234
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.
242 *
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.
246 */
247
248/**
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
253 *
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
256 * zero offset.
257 */
258struct ethnl_req_info {
259 struct net_device *dev;
260 netdevice_tracker dev_tracker;
261 u32 flags;
262};
263
264static inline void ethnl_parse_header_dev_put(struct ethnl_req_info *req_info)
265{
266 netdev_put(req_info->dev, &req_info->dev_tracker);
267}
268
269/**
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
273 * reply message
274 *
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
277 * offset.
278 */
279struct ethnl_reply_data {
280 struct net_device *dev;
281};
282
283int ethnl_ops_begin(struct net_device *dev);
284void ethnl_ops_complete(struct net_device *dev);
285
286/**
287 * struct ethnl_request_ops - unified handling of GET and SET 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
294 * @set_ntf_cmd: notification to generate on changes (SET)
295 * @parse_request:
296 * Parse request except common header (struct ethnl_req_info). Common
297 * header is already filled on entry, the rest up to @repdata_offset
298 * is zero initialized. This callback should only modify type specific
299 * request info by parsed attributes from request message.
300 * @prepare_data:
301 * Retrieve and prepare data needed to compose a reply message. Calls to
302 * ethtool_ops handlers are limited to this callback. Common reply data
303 * (struct ethnl_reply_data) is filled on entry, type specific part after
304 * it is zero initialized. This callback should only modify the type
305 * specific part of reply data. Device identification from struct
306 * ethnl_reply_data is to be used as for dump requests, it iterates
307 * through network devices while dev member of struct ethnl_req_info
308 * points to the device from client request.
309 * @reply_size:
310 * Estimate reply message size. Returned value must be sufficient for
311 * message payload without common reply header. The callback may returned
312 * estimate higher than actual message size if exact calculation would
313 * not be worth the saved memory space.
314 * @fill_reply:
315 * Fill reply message payload (except for common header) from reply data.
316 * The callback must not generate more payload than previously called
317 * ->reply_size() estimated.
318 * @cleanup_data:
319 * Optional cleanup called when reply data is no longer needed. Can be
320 * used e.g. to free any additional data structures outside the main
321 * structure which were allocated by ->prepare_data(). When processing
322 * dump requests, ->cleanup() is called for each message.
323 * @set_validate:
324 * Check if set operation is supported for a given device, and perform
325 * extra input checks. Expected return values:
326 * - 0 if the operation is a noop for the device (rare)
327 * - 1 if operation should proceed to calling @set
328 * - negative errno on errors
329 * Called without any locks, just a reference on the netdev.
330 * @set:
331 * Execute the set operation. The implementation should return
332 * - 0 if no configuration has changed
333 * - 1 if configuration changed and notification should be generated
334 * - negative errno on errors
335 *
336 * Description of variable parts of GET request handling when using the
337 * unified infrastructure. When used, a pointer to an instance of this
338 * structure is to be added to ðnl_default_requests array and generic
339 * handlers ethnl_default_doit(), ethnl_default_dumpit(),
340 * ethnl_default_start() and ethnl_default_done() used in @ethtool_genl_ops;
341 * ethnl_default_notify() can be used in @ethnl_notify_handlers to send
342 * notifications of the corresponding type.
343 */
344struct ethnl_request_ops {
345 u8 request_cmd;
346 u8 reply_cmd;
347 u16 hdr_attr;
348 unsigned int req_info_size;
349 unsigned int reply_data_size;
350 bool allow_nodev_do;
351 u8 set_ntf_cmd;
352
353 int (*parse_request)(struct ethnl_req_info *req_info,
354 struct nlattr **tb,
355 struct netlink_ext_ack *extack);
356 int (*prepare_data)(const struct ethnl_req_info *req_info,
357 struct ethnl_reply_data *reply_data,
358 const struct genl_info *info);
359 int (*reply_size)(const struct ethnl_req_info *req_info,
360 const struct ethnl_reply_data *reply_data);
361 int (*fill_reply)(struct sk_buff *skb,
362 const struct ethnl_req_info *req_info,
363 const struct ethnl_reply_data *reply_data);
364 void (*cleanup_data)(struct ethnl_reply_data *reply_data);
365
366 int (*set_validate)(struct ethnl_req_info *req_info,
367 struct genl_info *info);
368 int (*set)(struct ethnl_req_info *req_info,
369 struct genl_info *info);
370};
371
372/* request handlers */
373
374extern const struct ethnl_request_ops ethnl_strset_request_ops;
375extern const struct ethnl_request_ops ethnl_linkinfo_request_ops;
376extern const struct ethnl_request_ops ethnl_linkmodes_request_ops;
377extern const struct ethnl_request_ops ethnl_linkstate_request_ops;
378extern const struct ethnl_request_ops ethnl_debug_request_ops;
379extern const struct ethnl_request_ops ethnl_wol_request_ops;
380extern const struct ethnl_request_ops ethnl_features_request_ops;
381extern const struct ethnl_request_ops ethnl_privflags_request_ops;
382extern const struct ethnl_request_ops ethnl_rings_request_ops;
383extern const struct ethnl_request_ops ethnl_channels_request_ops;
384extern const struct ethnl_request_ops ethnl_coalesce_request_ops;
385extern const struct ethnl_request_ops ethnl_pause_request_ops;
386extern const struct ethnl_request_ops ethnl_eee_request_ops;
387extern const struct ethnl_request_ops ethnl_tsinfo_request_ops;
388extern const struct ethnl_request_ops ethnl_fec_request_ops;
389extern const struct ethnl_request_ops ethnl_module_eeprom_request_ops;
390extern const struct ethnl_request_ops ethnl_stats_request_ops;
391extern const struct ethnl_request_ops ethnl_phc_vclocks_request_ops;
392extern const struct ethnl_request_ops ethnl_module_request_ops;
393extern const struct ethnl_request_ops ethnl_pse_request_ops;
394extern const struct ethnl_request_ops ethnl_rss_request_ops;
395extern const struct ethnl_request_ops ethnl_plca_cfg_request_ops;
396extern const struct ethnl_request_ops ethnl_plca_status_request_ops;
397extern const struct ethnl_request_ops ethnl_mm_request_ops;
398
399extern const struct nla_policy ethnl_header_policy[ETHTOOL_A_HEADER_FLAGS + 1];
400extern const struct nla_policy ethnl_header_policy_stats[ETHTOOL_A_HEADER_FLAGS + 1];
401extern const struct nla_policy ethnl_strset_get_policy[ETHTOOL_A_STRSET_COUNTS_ONLY + 1];
402extern const struct nla_policy ethnl_linkinfo_get_policy[ETHTOOL_A_LINKINFO_HEADER + 1];
403extern const struct nla_policy ethnl_linkinfo_set_policy[ETHTOOL_A_LINKINFO_TP_MDIX_CTRL + 1];
404extern const struct nla_policy ethnl_linkmodes_get_policy[ETHTOOL_A_LINKMODES_HEADER + 1];
405extern const struct nla_policy ethnl_linkmodes_set_policy[ETHTOOL_A_LINKMODES_LANES + 1];
406extern const struct nla_policy ethnl_linkstate_get_policy[ETHTOOL_A_LINKSTATE_HEADER + 1];
407extern const struct nla_policy ethnl_debug_get_policy[ETHTOOL_A_DEBUG_HEADER + 1];
408extern const struct nla_policy ethnl_debug_set_policy[ETHTOOL_A_DEBUG_MSGMASK + 1];
409extern const struct nla_policy ethnl_wol_get_policy[ETHTOOL_A_WOL_HEADER + 1];
410extern const struct nla_policy ethnl_wol_set_policy[ETHTOOL_A_WOL_SOPASS + 1];
411extern const struct nla_policy ethnl_features_get_policy[ETHTOOL_A_FEATURES_HEADER + 1];
412extern const struct nla_policy ethnl_features_set_policy[ETHTOOL_A_FEATURES_WANTED + 1];
413extern const struct nla_policy ethnl_privflags_get_policy[ETHTOOL_A_PRIVFLAGS_HEADER + 1];
414extern const struct nla_policy ethnl_privflags_set_policy[ETHTOOL_A_PRIVFLAGS_FLAGS + 1];
415extern const struct nla_policy ethnl_rings_get_policy[ETHTOOL_A_RINGS_HEADER + 1];
416extern const struct nla_policy ethnl_rings_set_policy[ETHTOOL_A_RINGS_TX_PUSH_BUF_LEN_MAX + 1];
417extern const struct nla_policy ethnl_channels_get_policy[ETHTOOL_A_CHANNELS_HEADER + 1];
418extern const struct nla_policy ethnl_channels_set_policy[ETHTOOL_A_CHANNELS_COMBINED_COUNT + 1];
419extern const struct nla_policy ethnl_coalesce_get_policy[ETHTOOL_A_COALESCE_HEADER + 1];
420extern const struct nla_policy ethnl_coalesce_set_policy[ETHTOOL_A_COALESCE_MAX + 1];
421extern const struct nla_policy ethnl_pause_get_policy[ETHTOOL_A_PAUSE_STATS_SRC + 1];
422extern const struct nla_policy ethnl_pause_set_policy[ETHTOOL_A_PAUSE_TX + 1];
423extern const struct nla_policy ethnl_eee_get_policy[ETHTOOL_A_EEE_HEADER + 1];
424extern const struct nla_policy ethnl_eee_set_policy[ETHTOOL_A_EEE_TX_LPI_TIMER + 1];
425extern const struct nla_policy ethnl_tsinfo_get_policy[ETHTOOL_A_TSINFO_HEADER + 1];
426extern const struct nla_policy ethnl_cable_test_act_policy[ETHTOOL_A_CABLE_TEST_HEADER + 1];
427extern const struct nla_policy ethnl_cable_test_tdr_act_policy[ETHTOOL_A_CABLE_TEST_TDR_CFG + 1];
428extern const struct nla_policy ethnl_tunnel_info_get_policy[ETHTOOL_A_TUNNEL_INFO_HEADER + 1];
429extern const struct nla_policy ethnl_fec_get_policy[ETHTOOL_A_FEC_HEADER + 1];
430extern const struct nla_policy ethnl_fec_set_policy[ETHTOOL_A_FEC_AUTO + 1];
431extern const struct nla_policy ethnl_module_eeprom_get_policy[ETHTOOL_A_MODULE_EEPROM_I2C_ADDRESS + 1];
432extern const struct nla_policy ethnl_stats_get_policy[ETHTOOL_A_STATS_SRC + 1];
433extern const struct nla_policy ethnl_phc_vclocks_get_policy[ETHTOOL_A_PHC_VCLOCKS_HEADER + 1];
434extern const struct nla_policy ethnl_module_get_policy[ETHTOOL_A_MODULE_HEADER + 1];
435extern const struct nla_policy ethnl_module_set_policy[ETHTOOL_A_MODULE_POWER_MODE_POLICY + 1];
436extern const struct nla_policy ethnl_pse_get_policy[ETHTOOL_A_PSE_HEADER + 1];
437extern const struct nla_policy ethnl_pse_set_policy[ETHTOOL_A_PSE_MAX + 1];
438extern const struct nla_policy ethnl_rss_get_policy[ETHTOOL_A_RSS_CONTEXT + 1];
439extern const struct nla_policy ethnl_plca_get_cfg_policy[ETHTOOL_A_PLCA_HEADER + 1];
440extern const struct nla_policy ethnl_plca_set_cfg_policy[ETHTOOL_A_PLCA_MAX + 1];
441extern const struct nla_policy ethnl_plca_get_status_policy[ETHTOOL_A_PLCA_HEADER + 1];
442extern const struct nla_policy ethnl_mm_get_policy[ETHTOOL_A_MM_HEADER + 1];
443extern const struct nla_policy ethnl_mm_set_policy[ETHTOOL_A_MM_MAX + 1];
444
445int ethnl_set_features(struct sk_buff *skb, struct genl_info *info);
446int ethnl_act_cable_test(struct sk_buff *skb, struct genl_info *info);
447int ethnl_act_cable_test_tdr(struct sk_buff *skb, struct genl_info *info);
448int ethnl_tunnel_info_doit(struct sk_buff *skb, struct genl_info *info);
449int ethnl_tunnel_info_start(struct netlink_callback *cb);
450int ethnl_tunnel_info_dumpit(struct sk_buff *skb, struct netlink_callback *cb);
451
452extern const char stats_std_names[__ETHTOOL_STATS_CNT][ETH_GSTRING_LEN];
453extern const char stats_eth_phy_names[__ETHTOOL_A_STATS_ETH_PHY_CNT][ETH_GSTRING_LEN];
454extern const char stats_eth_mac_names[__ETHTOOL_A_STATS_ETH_MAC_CNT][ETH_GSTRING_LEN];
455extern const char stats_eth_ctrl_names[__ETHTOOL_A_STATS_ETH_CTRL_CNT][ETH_GSTRING_LEN];
456extern const char stats_rmon_names[__ETHTOOL_A_STATS_RMON_CNT][ETH_GSTRING_LEN];
457
458#endif /* _NET_ETHTOOL_NETLINK_H */