Linux Audio

Check our new training course

Loading...
Note: File does not exist in v4.6.
  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 * @attrype: 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_binary() - update binary data from NLA_BINARY atribute
142 * @dst:  value to update
143 * @len:  destination buffer length
144 * @attr: netlink attribute with new value or null
145 * @mod:  pointer to bool for modification tracking
146 *
147 * Use the u8 value from NLA_U8 netlink attribute @attr to rewrite data block
148 * of length @len at @dst by attribute payload; do nothing if @attr is null.
149 * Bool pointed to by @mod is set to true if this function changed the logical
150 * value of *dst, otherwise it is left as is.
151 */
152static inline void ethnl_update_binary(void *dst, unsigned int len,
153				       const struct nlattr *attr, bool *mod)
154{
155	if (!attr)
156		return;
157	if (nla_len(attr) < len)
158		len = nla_len(attr);
159	if (!memcmp(dst, nla_data(attr), len))
160		return;
161
162	memcpy(dst, nla_data(attr), len);
163	*mod = true;
164}
165
166/**
167 * ethnl_update_bitfield32() - update u32 value from NLA_BITFIELD32 attribute
168 * @dst:  value to update
169 * @attr: netlink attribute with new value or null
170 * @mod:  pointer to bool for modification tracking
171 *
172 * Update bits in u32 value which are set in attribute's mask to values from
173 * attribute's value. Do nothing if @attr is null or the value wouldn't change;
174 * otherwise, set bool pointed to by @mod to true.
175 */
176static inline void ethnl_update_bitfield32(u32 *dst, const struct nlattr *attr,
177					   bool *mod)
178{
179	struct nla_bitfield32 change;
180	u32 newval;
181
182	if (!attr)
183		return;
184	change = nla_get_bitfield32(attr);
185	newval = (*dst & ~change.selector) | (change.value & change.selector);
186	if (*dst == newval)
187		return;
188
189	*dst = newval;
190	*mod = true;
191}
192
193/**
194 * ethnl_reply_header_size() - total size of reply header
195 *
196 * This is an upper estimate so that we do not need to hold RTNL lock longer
197 * than necessary (to prevent rename between size estimate and composing the
198 * message). Accounts only for device ifindex and name as those are the only
199 * attributes ethnl_fill_reply_header() puts into the reply header.
200 */
201static inline unsigned int ethnl_reply_header_size(void)
202{
203	return nla_total_size(nla_total_size(sizeof(u32)) +
204			      nla_total_size(IFNAMSIZ));
205}
206
207/* GET request handling */
208
209/* Unified processing of GET requests uses two data structures: request info
210 * and reply data. Request info holds information parsed from client request
211 * and its stays constant through all request processing. Reply data holds data
212 * retrieved from ethtool_ops callbacks or other internal sources which is used
213 * to compose the reply. When processing a dump request, request info is filled
214 * only once (when the request message is parsed) but reply data is filled for
215 * each reply message.
216 *
217 * Both structures consist of part common for all request types (struct
218 * ethnl_req_info and struct ethnl_reply_data defined below) and optional
219 * parts specific for each request type. Common part always starts at offset 0.
220 */
221
222/**
223 * struct ethnl_req_info - base type of request information for GET requests
224 * @dev:   network device the request is for (may be null)
225 * @flags: request flags common for all request types
226 *
227 * This is a common base for request specific structures holding data from
228 * parsed userspace request. These always embed struct ethnl_req_info at
229 * zero offset.
230 */
231struct ethnl_req_info {
232	struct net_device	*dev;
233	u32			flags;
234};
235
236/**
237 * struct ethnl_reply_data - base type of reply data for GET requests
238 * @dev:       device for current reply message; in single shot requests it is
239 *             equal to &ethnl_req_info.dev; in dumps it's different for each
240 *             reply message
241 *
242 * This is a common base for request specific structures holding data for
243 * kernel reply message. These always embed struct ethnl_reply_data at zero
244 * offset.
245 */
246struct ethnl_reply_data {
247	struct net_device		*dev;
248};
249
250static inline int ethnl_ops_begin(struct net_device *dev)
251{
252	if (dev && dev->ethtool_ops->begin)
253		return dev->ethtool_ops->begin(dev);
254	else
255		return 0;
256}
257
258static inline void ethnl_ops_complete(struct net_device *dev)
259{
260	if (dev && dev->ethtool_ops->complete)
261		dev->ethtool_ops->complete(dev);
262}
263
264/**
265 * struct ethnl_request_ops - unified handling of GET requests
266 * @request_cmd:      command id for request (GET)
267 * @reply_cmd:        command id for reply (GET_REPLY)
268 * @hdr_attr:         attribute type for request header
269 * @max_attr:         maximum (top level) attribute type
270 * @req_info_size:    size of request info
271 * @reply_data_size:  size of reply data
272 * @request_policy:   netlink policy for message contents
273 * @allow_nodev_do:   allow non-dump request with no device identification
274 * @parse_request:
275 *	Parse request except common header (struct ethnl_req_info). Common
276 *	header is already filled on entry, the rest up to @repdata_offset
277 *	is zero initialized. This callback should only modify type specific
278 *	request info by parsed attributes from request message.
279 * @prepare_data:
280 *	Retrieve and prepare data needed to compose a reply message. Calls to
281 *	ethtool_ops handlers are limited to this callback. Common reply data
282 *	(struct ethnl_reply_data) is filled on entry, type specific part after
283 *	it is zero initialized. This callback should only modify the type
284 *	specific part of reply data. Device identification from struct
285 *	ethnl_reply_data is to be used as for dump requests, it iterates
286 *	through network devices while dev member of struct ethnl_req_info
287 *	points to the device from client request.
288 * @reply_size:
289 *	Estimate reply message size. Returned value must be sufficient for
290 *	message payload without common reply header. The callback may returned
291 *	estimate higher than actual message size if exact calculation would
292 *	not be worth the saved memory space.
293 * @fill_reply:
294 *	Fill reply message payload (except for common header) from reply data.
295 *	The callback must not generate more payload than previously called
296 *	->reply_size() estimated.
297 * @cleanup_data:
298 *	Optional cleanup called when reply data is no longer needed. Can be
299 *	used e.g. to free any additional data structures outside the main
300 *	structure which were allocated by ->prepare_data(). When processing
301 *	dump requests, ->cleanup() is called for each message.
302 *
303 * Description of variable parts of GET request handling when using the
304 * unified infrastructure. When used, a pointer to an instance of this
305 * structure is to be added to &ethnl_default_requests array and generic
306 * handlers ethnl_default_doit(), ethnl_default_dumpit(),
307 * ethnl_default_start() and ethnl_default_done() used in @ethtool_genl_ops;
308 * ethnl_default_notify() can be used in @ethnl_notify_handlers to send
309 * notifications of the corresponding type.
310 */
311struct ethnl_request_ops {
312	u8			request_cmd;
313	u8			reply_cmd;
314	u16			hdr_attr;
315	unsigned int		max_attr;
316	unsigned int		req_info_size;
317	unsigned int		reply_data_size;
318	const struct nla_policy *request_policy;
319	bool			allow_nodev_do;
320
321	int (*parse_request)(struct ethnl_req_info *req_info,
322			     struct nlattr **tb,
323			     struct netlink_ext_ack *extack);
324	int (*prepare_data)(const struct ethnl_req_info *req_info,
325			    struct ethnl_reply_data *reply_data,
326			    struct genl_info *info);
327	int (*reply_size)(const struct ethnl_req_info *req_info,
328			  const struct ethnl_reply_data *reply_data);
329	int (*fill_reply)(struct sk_buff *skb,
330			  const struct ethnl_req_info *req_info,
331			  const struct ethnl_reply_data *reply_data);
332	void (*cleanup_data)(struct ethnl_reply_data *reply_data);
333};
334
335/* request handlers */
336
337extern const struct ethnl_request_ops ethnl_strset_request_ops;
338extern const struct ethnl_request_ops ethnl_linkinfo_request_ops;
339extern const struct ethnl_request_ops ethnl_linkmodes_request_ops;
340extern const struct ethnl_request_ops ethnl_linkstate_request_ops;
341extern const struct ethnl_request_ops ethnl_debug_request_ops;
342extern const struct ethnl_request_ops ethnl_wol_request_ops;
343extern const struct ethnl_request_ops ethnl_features_request_ops;
344extern const struct ethnl_request_ops ethnl_privflags_request_ops;
345extern const struct ethnl_request_ops ethnl_rings_request_ops;
346extern const struct ethnl_request_ops ethnl_channels_request_ops;
347extern const struct ethnl_request_ops ethnl_coalesce_request_ops;
348extern const struct ethnl_request_ops ethnl_pause_request_ops;
349extern const struct ethnl_request_ops ethnl_eee_request_ops;
350extern const struct ethnl_request_ops ethnl_tsinfo_request_ops;
351
352int ethnl_set_linkinfo(struct sk_buff *skb, struct genl_info *info);
353int ethnl_set_linkmodes(struct sk_buff *skb, struct genl_info *info);
354int ethnl_set_debug(struct sk_buff *skb, struct genl_info *info);
355int ethnl_set_wol(struct sk_buff *skb, struct genl_info *info);
356int ethnl_set_features(struct sk_buff *skb, struct genl_info *info);
357int ethnl_set_privflags(struct sk_buff *skb, struct genl_info *info);
358int ethnl_set_rings(struct sk_buff *skb, struct genl_info *info);
359int ethnl_set_channels(struct sk_buff *skb, struct genl_info *info);
360int ethnl_set_coalesce(struct sk_buff *skb, struct genl_info *info);
361int ethnl_set_pause(struct sk_buff *skb, struct genl_info *info);
362int ethnl_set_eee(struct sk_buff *skb, struct genl_info *info);
363int ethnl_act_cable_test(struct sk_buff *skb, struct genl_info *info);
364int ethnl_act_cable_test_tdr(struct sk_buff *skb, struct genl_info *info);
365int ethnl_tunnel_info_doit(struct sk_buff *skb, struct genl_info *info);
366int ethnl_tunnel_info_start(struct netlink_callback *cb);
367int ethnl_tunnel_info_dumpit(struct sk_buff *skb, struct netlink_callback *cb);
368
369#endif /* _NET_ETHTOOL_NETLINK_H */