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