xref: /openbmc/linux/net/ethtool/netlink.h (revision 55fd7e02)
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 
11 struct ethnl_req_info;
12 
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,
16 			       bool require_dev);
17 int ethnl_fill_reply_header(struct sk_buff *skb, struct net_device *dev,
18 			    u16 attrtype);
19 struct 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);
22 void *ethnl_bcastmsg_put(struct sk_buff *skb, u8 cmd);
23 int ethnl_multicast(struct sk_buff *skb, struct net_device *dev);
24 
25 /**
26  * ethnl_strz_size() - calculate attribute length for fixed size string
27  * @s: ETH_GSTRING_LEN sized string (may not be null terminated)
28  *
29  * Return: total length of an attribute with null terminated string from @s
30  */
31 static inline int ethnl_strz_size(const char *s)
32 {
33 	return nla_total_size(strnlen(s, ETH_GSTRING_LEN) + 1);
34 }
35 
36 /**
37  * ethnl_put_strz() - put string attribute with fixed size string
38  * @skb:     skb with the message
39  * @attrype: attribute type
40  * @s:       ETH_GSTRING_LEN sized string (may not be null terminated)
41  *
42  * Puts an attribute with null terminated string from @s into the message.
43  *
44  * Return: 0 on success, negative error code on failure
45  */
46 static inline int ethnl_put_strz(struct sk_buff *skb, u16 attrtype,
47 				 const char *s)
48 {
49 	unsigned int len = strnlen(s, ETH_GSTRING_LEN);
50 	struct nlattr *attr;
51 
52 	attr = nla_reserve(skb, attrtype, len + 1);
53 	if (!attr)
54 		return -EMSGSIZE;
55 
56 	memcpy(nla_data(attr), s, len);
57 	((char *)nla_data(attr))[len] = '\0';
58 	return 0;
59 }
60 
61 /**
62  * ethnl_update_u32() - update u32 value from NLA_U32 attribute
63  * @dst:  value to update
64  * @attr: netlink attribute with new value or null
65  * @mod:  pointer to bool for modification tracking
66  *
67  * Copy the u32 value from NLA_U32 netlink attribute @attr into variable
68  * pointed to by @dst; do nothing if @attr is null. Bool pointed to by @mod
69  * is set to true if this function changed the value of *dst, otherwise it
70  * is left as is.
71  */
72 static inline void ethnl_update_u32(u32 *dst, const struct nlattr *attr,
73 				    bool *mod)
74 {
75 	u32 val;
76 
77 	if (!attr)
78 		return;
79 	val = nla_get_u32(attr);
80 	if (*dst == val)
81 		return;
82 
83 	*dst = val;
84 	*mod = true;
85 }
86 
87 /**
88  * ethnl_update_u8() - update u8 value from NLA_U8 attribute
89  * @dst:  value to update
90  * @attr: netlink attribute with new value or null
91  * @mod:  pointer to bool for modification tracking
92  *
93  * Copy the u8 value from NLA_U8 netlink attribute @attr into variable
94  * pointed to by @dst; do nothing if @attr is null. Bool pointed to by @mod
95  * is set to true if this function changed the value of *dst, otherwise it
96  * is left as is.
97  */
98 static inline void ethnl_update_u8(u8 *dst, const struct nlattr *attr,
99 				   bool *mod)
100 {
101 	u8 val;
102 
103 	if (!attr)
104 		return;
105 	val = nla_get_u8(attr);
106 	if (*dst == val)
107 		return;
108 
109 	*dst = val;
110 	*mod = true;
111 }
112 
113 /**
114  * ethnl_update_bool32() - update u32 used as bool from NLA_U8 attribute
115  * @dst:  value to update
116  * @attr: netlink attribute with new value or null
117  * @mod:  pointer to bool for modification tracking
118  *
119  * Use the u8 value from NLA_U8 netlink attribute @attr to set u32 variable
120  * pointed to by @dst to 0 (if zero) or 1 (if not); do nothing if @attr is
121  * null. Bool pointed to by @mod is set to true if this function changed the
122  * logical value of *dst, otherwise it is left as is.
123  */
124 static inline void ethnl_update_bool32(u32 *dst, const struct nlattr *attr,
125 				       bool *mod)
126 {
127 	u8 val;
128 
129 	if (!attr)
130 		return;
131 	val = !!nla_get_u8(attr);
132 	if (!!*dst == val)
133 		return;
134 
135 	*dst = val;
136 	*mod = true;
137 }
138 
139 /**
140  * ethnl_update_binary() - update binary data from NLA_BINARY atribute
141  * @dst:  value to update
142  * @len:  destination buffer length
143  * @attr: netlink attribute with new value or null
144  * @mod:  pointer to bool for modification tracking
145  *
146  * Use the u8 value from NLA_U8 netlink attribute @attr to rewrite data block
147  * of length @len at @dst by attribute payload; do nothing if @attr is null.
148  * Bool pointed to by @mod is set to true if this function changed the logical
149  * value of *dst, otherwise it is left as is.
150  */
151 static inline void ethnl_update_binary(void *dst, unsigned int len,
152 				       const struct nlattr *attr, bool *mod)
153 {
154 	if (!attr)
155 		return;
156 	if (nla_len(attr) < len)
157 		len = nla_len(attr);
158 	if (!memcmp(dst, nla_data(attr), len))
159 		return;
160 
161 	memcpy(dst, nla_data(attr), len);
162 	*mod = true;
163 }
164 
165 /**
166  * ethnl_update_bitfield32() - update u32 value from NLA_BITFIELD32 attribute
167  * @dst:  value to update
168  * @attr: netlink attribute with new value or null
169  * @mod:  pointer to bool for modification tracking
170  *
171  * Update bits in u32 value which are set in attribute's mask to values from
172  * attribute's value. Do nothing if @attr is null or the value wouldn't change;
173  * otherwise, set bool pointed to by @mod to true.
174  */
175 static inline void ethnl_update_bitfield32(u32 *dst, const struct nlattr *attr,
176 					   bool *mod)
177 {
178 	struct nla_bitfield32 change;
179 	u32 newval;
180 
181 	if (!attr)
182 		return;
183 	change = nla_get_bitfield32(attr);
184 	newval = (*dst & ~change.selector) | (change.value & change.selector);
185 	if (*dst == newval)
186 		return;
187 
188 	*dst = newval;
189 	*mod = true;
190 }
191 
192 /**
193  * ethnl_reply_header_size() - total size of reply header
194  *
195  * This is an upper estimate so that we do not need to hold RTNL lock longer
196  * than necessary (to prevent rename between size estimate and composing the
197  * message). Accounts only for device ifindex and name as those are the only
198  * attributes ethnl_fill_reply_header() puts into the reply header.
199  */
200 static inline unsigned int ethnl_reply_header_size(void)
201 {
202 	return nla_total_size(nla_total_size(sizeof(u32)) +
203 			      nla_total_size(IFNAMSIZ));
204 }
205 
206 /* GET request handling */
207 
208 /* Unified processing of GET requests uses two data structures: request info
209  * and reply data. Request info holds information parsed from client request
210  * and its stays constant through all request processing. Reply data holds data
211  * retrieved from ethtool_ops callbacks or other internal sources which is used
212  * to compose the reply. When processing a dump request, request info is filled
213  * only once (when the request message is parsed) but reply data is filled for
214  * each reply message.
215  *
216  * Both structures consist of part common for all request types (struct
217  * ethnl_req_info and struct ethnl_reply_data defined below) and optional
218  * parts specific for each request type. Common part always starts at offset 0.
219  */
220 
221 /**
222  * struct ethnl_req_info - base type of request information for GET requests
223  * @dev:   network device the request is for (may be null)
224  * @flags: request flags common for all request types
225  *
226  * This is a common base for request specific structures holding data from
227  * parsed userspace request. These always embed struct ethnl_req_info at
228  * zero offset.
229  */
230 struct ethnl_req_info {
231 	struct net_device	*dev;
232 	u32			flags;
233 };
234 
235 /**
236  * struct ethnl_reply_data - base type of reply data for GET requests
237  * @dev:       device for current reply message; in single shot requests it is
238  *             equal to &ethnl_req_info.dev; in dumps it's different for each
239  *             reply message
240  *
241  * This is a common base for request specific structures holding data for
242  * kernel reply message. These always embed struct ethnl_reply_data at zero
243  * offset.
244  */
245 struct ethnl_reply_data {
246 	struct net_device		*dev;
247 };
248 
249 static inline int ethnl_ops_begin(struct net_device *dev)
250 {
251 	if (dev && dev->ethtool_ops->begin)
252 		return dev->ethtool_ops->begin(dev);
253 	else
254 		return 0;
255 }
256 
257 static inline void ethnl_ops_complete(struct net_device *dev)
258 {
259 	if (dev && dev->ethtool_ops->complete)
260 		dev->ethtool_ops->complete(dev);
261 }
262 
263 /**
264  * struct ethnl_request_ops - unified handling of GET requests
265  * @request_cmd:      command id for request (GET)
266  * @reply_cmd:        command id for reply (GET_REPLY)
267  * @hdr_attr:         attribute type for request header
268  * @max_attr:         maximum (top level) attribute type
269  * @req_info_size:    size of request info
270  * @reply_data_size:  size of reply data
271  * @request_policy:   netlink policy for message contents
272  * @allow_nodev_do:   allow non-dump request with no device identification
273  * @parse_request:
274  *	Parse request except common header (struct ethnl_req_info). Common
275  *	header is already filled on entry, the rest up to @repdata_offset
276  *	is zero initialized. This callback should only modify type specific
277  *	request info by parsed attributes from request message.
278  * @prepare_data:
279  *	Retrieve and prepare data needed to compose a reply message. Calls to
280  *	ethtool_ops handlers are limited to this callback. Common reply data
281  *	(struct ethnl_reply_data) is filled on entry, type specific part after
282  *	it is zero initialized. This callback should only modify the type
283  *	specific part of reply data. Device identification from struct
284  *	ethnl_reply_data is to be used as for dump requests, it iterates
285  *	through network devices while dev member of struct ethnl_req_info
286  *	points to the device from client request.
287  * @reply_size:
288  *	Estimate reply message size. Returned value must be sufficient for
289  *	message payload without common reply header. The callback may returned
290  *	estimate higher than actual message size if exact calculation would
291  *	not be worth the saved memory space.
292  * @fill_reply:
293  *	Fill reply message payload (except for common header) from reply data.
294  *	The callback must not generate more payload than previously called
295  *	->reply_size() estimated.
296  * @cleanup_data:
297  *	Optional cleanup called when reply data is no longer needed. Can be
298  *	used e.g. to free any additional data structures outside the main
299  *	structure which were allocated by ->prepare_data(). When processing
300  *	dump requests, ->cleanup() is called for each message.
301  *
302  * Description of variable parts of GET request handling when using the
303  * unified infrastructure. When used, a pointer to an instance of this
304  * structure is to be added to &ethnl_default_requests array and generic
305  * handlers ethnl_default_doit(), ethnl_default_dumpit(),
306  * ethnl_default_start() and ethnl_default_done() used in @ethtool_genl_ops;
307  * ethnl_default_notify() can be used in @ethnl_notify_handlers to send
308  * notifications of the corresponding type.
309  */
310 struct ethnl_request_ops {
311 	u8			request_cmd;
312 	u8			reply_cmd;
313 	u16			hdr_attr;
314 	unsigned int		max_attr;
315 	unsigned int		req_info_size;
316 	unsigned int		reply_data_size;
317 	const struct nla_policy *request_policy;
318 	bool			allow_nodev_do;
319 
320 	int (*parse_request)(struct ethnl_req_info *req_info,
321 			     struct nlattr **tb,
322 			     struct netlink_ext_ack *extack);
323 	int (*prepare_data)(const struct ethnl_req_info *req_info,
324 			    struct ethnl_reply_data *reply_data,
325 			    struct genl_info *info);
326 	int (*reply_size)(const struct ethnl_req_info *req_info,
327 			  const struct ethnl_reply_data *reply_data);
328 	int (*fill_reply)(struct sk_buff *skb,
329 			  const struct ethnl_req_info *req_info,
330 			  const struct ethnl_reply_data *reply_data);
331 	void (*cleanup_data)(struct ethnl_reply_data *reply_data);
332 };
333 
334 /* request handlers */
335 
336 extern const struct ethnl_request_ops ethnl_strset_request_ops;
337 extern const struct ethnl_request_ops ethnl_linkinfo_request_ops;
338 extern const struct ethnl_request_ops ethnl_linkmodes_request_ops;
339 extern const struct ethnl_request_ops ethnl_linkstate_request_ops;
340 extern const struct ethnl_request_ops ethnl_debug_request_ops;
341 extern const struct ethnl_request_ops ethnl_wol_request_ops;
342 extern const struct ethnl_request_ops ethnl_features_request_ops;
343 extern const struct ethnl_request_ops ethnl_privflags_request_ops;
344 extern const struct ethnl_request_ops ethnl_rings_request_ops;
345 extern const struct ethnl_request_ops ethnl_channels_request_ops;
346 extern const struct ethnl_request_ops ethnl_coalesce_request_ops;
347 extern const struct ethnl_request_ops ethnl_pause_request_ops;
348 extern const struct ethnl_request_ops ethnl_eee_request_ops;
349 extern const struct ethnl_request_ops ethnl_tsinfo_request_ops;
350 
351 int ethnl_set_linkinfo(struct sk_buff *skb, struct genl_info *info);
352 int ethnl_set_linkmodes(struct sk_buff *skb, struct genl_info *info);
353 int ethnl_set_debug(struct sk_buff *skb, struct genl_info *info);
354 int ethnl_set_wol(struct sk_buff *skb, struct genl_info *info);
355 int ethnl_set_features(struct sk_buff *skb, struct genl_info *info);
356 int ethnl_set_privflags(struct sk_buff *skb, struct genl_info *info);
357 int ethnl_set_rings(struct sk_buff *skb, struct genl_info *info);
358 int ethnl_set_channels(struct sk_buff *skb, struct genl_info *info);
359 int ethnl_set_coalesce(struct sk_buff *skb, struct genl_info *info);
360 int ethnl_set_pause(struct sk_buff *skb, struct genl_info *info);
361 int ethnl_set_eee(struct sk_buff *skb, struct genl_info *info);
362 int ethnl_act_cable_test(struct sk_buff *skb, struct genl_info *info);
363 int ethnl_act_cable_test_tdr(struct sk_buff *skb, struct genl_info *info);
364 
365 #endif /* _NET_ETHTOOL_NETLINK_H */
366