xref: /OK3568_Linux_fs/kernel/net/ethtool/netlink.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun 
3*4882a593Smuzhiyun #include <net/sock.h>
4*4882a593Smuzhiyun #include <linux/ethtool_netlink.h>
5*4882a593Smuzhiyun #include "netlink.h"
6*4882a593Smuzhiyun 
7*4882a593Smuzhiyun static struct genl_family ethtool_genl_family;
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun static bool ethnl_ok __read_mostly;
10*4882a593Smuzhiyun static u32 ethnl_bcast_seq;
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun #define ETHTOOL_FLAGS_BASIC (ETHTOOL_FLAG_COMPACT_BITSETS |	\
13*4882a593Smuzhiyun 			     ETHTOOL_FLAG_OMIT_REPLY)
14*4882a593Smuzhiyun #define ETHTOOL_FLAGS_STATS (ETHTOOL_FLAGS_BASIC | ETHTOOL_FLAG_STATS)
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun const struct nla_policy ethnl_header_policy[] = {
17*4882a593Smuzhiyun 	[ETHTOOL_A_HEADER_DEV_INDEX]	= { .type = NLA_U32 },
18*4882a593Smuzhiyun 	[ETHTOOL_A_HEADER_DEV_NAME]	= { .type = NLA_NUL_STRING,
19*4882a593Smuzhiyun 					    .len = ALTIFNAMSIZ - 1 },
20*4882a593Smuzhiyun 	[ETHTOOL_A_HEADER_FLAGS]	= NLA_POLICY_MASK(NLA_U32,
21*4882a593Smuzhiyun 							  ETHTOOL_FLAGS_BASIC),
22*4882a593Smuzhiyun };
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun const struct nla_policy ethnl_header_policy_stats[] = {
25*4882a593Smuzhiyun 	[ETHTOOL_A_HEADER_DEV_INDEX]	= { .type = NLA_U32 },
26*4882a593Smuzhiyun 	[ETHTOOL_A_HEADER_DEV_NAME]	= { .type = NLA_NUL_STRING,
27*4882a593Smuzhiyun 					    .len = ALTIFNAMSIZ - 1 },
28*4882a593Smuzhiyun 	[ETHTOOL_A_HEADER_FLAGS]	= NLA_POLICY_MASK(NLA_U32,
29*4882a593Smuzhiyun 							  ETHTOOL_FLAGS_STATS),
30*4882a593Smuzhiyun };
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun /**
33*4882a593Smuzhiyun  * ethnl_parse_header_dev_get() - parse request header
34*4882a593Smuzhiyun  * @req_info:    structure to put results into
35*4882a593Smuzhiyun  * @header:      nest attribute with request header
36*4882a593Smuzhiyun  * @net:         request netns
37*4882a593Smuzhiyun  * @extack:      netlink extack for error reporting
38*4882a593Smuzhiyun  * @require_dev: fail if no device identified in header
39*4882a593Smuzhiyun  *
40*4882a593Smuzhiyun  * Parse request header in nested attribute @nest and puts results into
41*4882a593Smuzhiyun  * the structure pointed to by @req_info. Extack from @info is used for error
42*4882a593Smuzhiyun  * reporting. If req_info->dev is not null on return, reference to it has
43*4882a593Smuzhiyun  * been taken. If error is returned, *req_info is null initialized and no
44*4882a593Smuzhiyun  * reference is held.
45*4882a593Smuzhiyun  *
46*4882a593Smuzhiyun  * Return: 0 on success or negative error code
47*4882a593Smuzhiyun  */
ethnl_parse_header_dev_get(struct ethnl_req_info * req_info,const struct nlattr * header,struct net * net,struct netlink_ext_ack * extack,bool require_dev)48*4882a593Smuzhiyun int ethnl_parse_header_dev_get(struct ethnl_req_info *req_info,
49*4882a593Smuzhiyun 			       const struct nlattr *header, struct net *net,
50*4882a593Smuzhiyun 			       struct netlink_ext_ack *extack, bool require_dev)
51*4882a593Smuzhiyun {
52*4882a593Smuzhiyun 	struct nlattr *tb[ARRAY_SIZE(ethnl_header_policy)];
53*4882a593Smuzhiyun 	const struct nlattr *devname_attr;
54*4882a593Smuzhiyun 	struct net_device *dev = NULL;
55*4882a593Smuzhiyun 	u32 flags = 0;
56*4882a593Smuzhiyun 	int ret;
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun 	if (!header) {
59*4882a593Smuzhiyun 		NL_SET_ERR_MSG(extack, "request header missing");
60*4882a593Smuzhiyun 		return -EINVAL;
61*4882a593Smuzhiyun 	}
62*4882a593Smuzhiyun 	/* No validation here, command policy should have a nested policy set
63*4882a593Smuzhiyun 	 * for the header, therefore validation should have already been done.
64*4882a593Smuzhiyun 	 */
65*4882a593Smuzhiyun 	ret = nla_parse_nested(tb, ARRAY_SIZE(ethnl_header_policy) - 1, header,
66*4882a593Smuzhiyun 			       NULL, extack);
67*4882a593Smuzhiyun 	if (ret < 0)
68*4882a593Smuzhiyun 		return ret;
69*4882a593Smuzhiyun 	if (tb[ETHTOOL_A_HEADER_FLAGS])
70*4882a593Smuzhiyun 		flags = nla_get_u32(tb[ETHTOOL_A_HEADER_FLAGS]);
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun 	devname_attr = tb[ETHTOOL_A_HEADER_DEV_NAME];
73*4882a593Smuzhiyun 	if (tb[ETHTOOL_A_HEADER_DEV_INDEX]) {
74*4882a593Smuzhiyun 		u32 ifindex = nla_get_u32(tb[ETHTOOL_A_HEADER_DEV_INDEX]);
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun 		dev = dev_get_by_index(net, ifindex);
77*4882a593Smuzhiyun 		if (!dev) {
78*4882a593Smuzhiyun 			NL_SET_ERR_MSG_ATTR(extack,
79*4882a593Smuzhiyun 					    tb[ETHTOOL_A_HEADER_DEV_INDEX],
80*4882a593Smuzhiyun 					    "no device matches ifindex");
81*4882a593Smuzhiyun 			return -ENODEV;
82*4882a593Smuzhiyun 		}
83*4882a593Smuzhiyun 		/* if both ifindex and ifname are passed, they must match */
84*4882a593Smuzhiyun 		if (devname_attr &&
85*4882a593Smuzhiyun 		    strncmp(dev->name, nla_data(devname_attr), IFNAMSIZ)) {
86*4882a593Smuzhiyun 			dev_put(dev);
87*4882a593Smuzhiyun 			NL_SET_ERR_MSG_ATTR(extack, header,
88*4882a593Smuzhiyun 					    "ifindex and name do not match");
89*4882a593Smuzhiyun 			return -ENODEV;
90*4882a593Smuzhiyun 		}
91*4882a593Smuzhiyun 	} else if (devname_attr) {
92*4882a593Smuzhiyun 		dev = dev_get_by_name(net, nla_data(devname_attr));
93*4882a593Smuzhiyun 		if (!dev) {
94*4882a593Smuzhiyun 			NL_SET_ERR_MSG_ATTR(extack, devname_attr,
95*4882a593Smuzhiyun 					    "no device matches name");
96*4882a593Smuzhiyun 			return -ENODEV;
97*4882a593Smuzhiyun 		}
98*4882a593Smuzhiyun 	} else if (require_dev) {
99*4882a593Smuzhiyun 		NL_SET_ERR_MSG_ATTR(extack, header,
100*4882a593Smuzhiyun 				    "neither ifindex nor name specified");
101*4882a593Smuzhiyun 		return -EINVAL;
102*4882a593Smuzhiyun 	}
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun 	if (dev && !netif_device_present(dev)) {
105*4882a593Smuzhiyun 		dev_put(dev);
106*4882a593Smuzhiyun 		NL_SET_ERR_MSG(extack, "device not present");
107*4882a593Smuzhiyun 		return -ENODEV;
108*4882a593Smuzhiyun 	}
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun 	req_info->dev = dev;
111*4882a593Smuzhiyun 	req_info->flags = flags;
112*4882a593Smuzhiyun 	return 0;
113*4882a593Smuzhiyun }
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun /**
116*4882a593Smuzhiyun  * ethnl_fill_reply_header() - Put common header into a reply message
117*4882a593Smuzhiyun  * @skb:      skb with the message
118*4882a593Smuzhiyun  * @dev:      network device to describe in header
119*4882a593Smuzhiyun  * @attrtype: attribute type to use for the nest
120*4882a593Smuzhiyun  *
121*4882a593Smuzhiyun  * Create a nested attribute with attributes describing given network device.
122*4882a593Smuzhiyun  *
123*4882a593Smuzhiyun  * Return: 0 on success, error value (-EMSGSIZE only) on error
124*4882a593Smuzhiyun  */
ethnl_fill_reply_header(struct sk_buff * skb,struct net_device * dev,u16 attrtype)125*4882a593Smuzhiyun int ethnl_fill_reply_header(struct sk_buff *skb, struct net_device *dev,
126*4882a593Smuzhiyun 			    u16 attrtype)
127*4882a593Smuzhiyun {
128*4882a593Smuzhiyun 	struct nlattr *nest;
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun 	if (!dev)
131*4882a593Smuzhiyun 		return 0;
132*4882a593Smuzhiyun 	nest = nla_nest_start(skb, attrtype);
133*4882a593Smuzhiyun 	if (!nest)
134*4882a593Smuzhiyun 		return -EMSGSIZE;
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun 	if (nla_put_u32(skb, ETHTOOL_A_HEADER_DEV_INDEX, (u32)dev->ifindex) ||
137*4882a593Smuzhiyun 	    nla_put_string(skb, ETHTOOL_A_HEADER_DEV_NAME, dev->name))
138*4882a593Smuzhiyun 		goto nla_put_failure;
139*4882a593Smuzhiyun 	/* If more attributes are put into reply header, ethnl_header_size()
140*4882a593Smuzhiyun 	 * must be updated to account for them.
141*4882a593Smuzhiyun 	 */
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun 	nla_nest_end(skb, nest);
144*4882a593Smuzhiyun 	return 0;
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun nla_put_failure:
147*4882a593Smuzhiyun 	nla_nest_cancel(skb, nest);
148*4882a593Smuzhiyun 	return -EMSGSIZE;
149*4882a593Smuzhiyun }
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun /**
152*4882a593Smuzhiyun  * ethnl_reply_init() - Create skb for a reply and fill device identification
153*4882a593Smuzhiyun  * @payload:      payload length (without netlink and genetlink header)
154*4882a593Smuzhiyun  * @dev:          device the reply is about (may be null)
155*4882a593Smuzhiyun  * @cmd:          ETHTOOL_MSG_* message type for reply
156*4882a593Smuzhiyun  * @hdr_attrtype: attribute type for common header
157*4882a593Smuzhiyun  * @info:         genetlink info of the received packet we respond to
158*4882a593Smuzhiyun  * @ehdrp:        place to store payload pointer returned by genlmsg_new()
159*4882a593Smuzhiyun  *
160*4882a593Smuzhiyun  * Return: pointer to allocated skb on success, NULL on error
161*4882a593Smuzhiyun  */
ethnl_reply_init(size_t payload,struct net_device * dev,u8 cmd,u16 hdr_attrtype,struct genl_info * info,void ** ehdrp)162*4882a593Smuzhiyun struct sk_buff *ethnl_reply_init(size_t payload, struct net_device *dev, u8 cmd,
163*4882a593Smuzhiyun 				 u16 hdr_attrtype, struct genl_info *info,
164*4882a593Smuzhiyun 				 void **ehdrp)
165*4882a593Smuzhiyun {
166*4882a593Smuzhiyun 	struct sk_buff *skb;
167*4882a593Smuzhiyun 
168*4882a593Smuzhiyun 	skb = genlmsg_new(payload, GFP_KERNEL);
169*4882a593Smuzhiyun 	if (!skb)
170*4882a593Smuzhiyun 		goto err;
171*4882a593Smuzhiyun 	*ehdrp = genlmsg_put_reply(skb, info, &ethtool_genl_family, 0, cmd);
172*4882a593Smuzhiyun 	if (!*ehdrp)
173*4882a593Smuzhiyun 		goto err_free;
174*4882a593Smuzhiyun 
175*4882a593Smuzhiyun 	if (dev) {
176*4882a593Smuzhiyun 		int ret;
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun 		ret = ethnl_fill_reply_header(skb, dev, hdr_attrtype);
179*4882a593Smuzhiyun 		if (ret < 0)
180*4882a593Smuzhiyun 			goto err_free;
181*4882a593Smuzhiyun 	}
182*4882a593Smuzhiyun 	return skb;
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun err_free:
185*4882a593Smuzhiyun 	nlmsg_free(skb);
186*4882a593Smuzhiyun err:
187*4882a593Smuzhiyun 	if (info)
188*4882a593Smuzhiyun 		GENL_SET_ERR_MSG(info, "failed to setup reply message");
189*4882a593Smuzhiyun 	return NULL;
190*4882a593Smuzhiyun }
191*4882a593Smuzhiyun 
ethnl_dump_put(struct sk_buff * skb,struct netlink_callback * cb,u8 cmd)192*4882a593Smuzhiyun void *ethnl_dump_put(struct sk_buff *skb, struct netlink_callback *cb, u8 cmd)
193*4882a593Smuzhiyun {
194*4882a593Smuzhiyun 	return genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
195*4882a593Smuzhiyun 			   &ethtool_genl_family, 0, cmd);
196*4882a593Smuzhiyun }
197*4882a593Smuzhiyun 
ethnl_bcastmsg_put(struct sk_buff * skb,u8 cmd)198*4882a593Smuzhiyun void *ethnl_bcastmsg_put(struct sk_buff *skb, u8 cmd)
199*4882a593Smuzhiyun {
200*4882a593Smuzhiyun 	return genlmsg_put(skb, 0, ++ethnl_bcast_seq, &ethtool_genl_family, 0,
201*4882a593Smuzhiyun 			   cmd);
202*4882a593Smuzhiyun }
203*4882a593Smuzhiyun 
ethnl_multicast(struct sk_buff * skb,struct net_device * dev)204*4882a593Smuzhiyun int ethnl_multicast(struct sk_buff *skb, struct net_device *dev)
205*4882a593Smuzhiyun {
206*4882a593Smuzhiyun 	return genlmsg_multicast_netns(&ethtool_genl_family, dev_net(dev), skb,
207*4882a593Smuzhiyun 				       0, ETHNL_MCGRP_MONITOR, GFP_KERNEL);
208*4882a593Smuzhiyun }
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun /* GET request helpers */
211*4882a593Smuzhiyun 
212*4882a593Smuzhiyun /**
213*4882a593Smuzhiyun  * struct ethnl_dump_ctx - context structure for generic dumpit() callback
214*4882a593Smuzhiyun  * @ops:        request ops of currently processed message type
215*4882a593Smuzhiyun  * @req_info:   parsed request header of processed request
216*4882a593Smuzhiyun  * @reply_data: data needed to compose the reply
217*4882a593Smuzhiyun  * @pos_hash:   saved iteration position - hashbucket
218*4882a593Smuzhiyun  * @pos_idx:    saved iteration position - index
219*4882a593Smuzhiyun  *
220*4882a593Smuzhiyun  * These parameters are kept in struct netlink_callback as context preserved
221*4882a593Smuzhiyun  * between iterations. They are initialized by ethnl_default_start() and used
222*4882a593Smuzhiyun  * in ethnl_default_dumpit() and ethnl_default_done().
223*4882a593Smuzhiyun  */
224*4882a593Smuzhiyun struct ethnl_dump_ctx {
225*4882a593Smuzhiyun 	const struct ethnl_request_ops	*ops;
226*4882a593Smuzhiyun 	struct ethnl_req_info		*req_info;
227*4882a593Smuzhiyun 	struct ethnl_reply_data		*reply_data;
228*4882a593Smuzhiyun 	int				pos_hash;
229*4882a593Smuzhiyun 	int				pos_idx;
230*4882a593Smuzhiyun };
231*4882a593Smuzhiyun 
232*4882a593Smuzhiyun static const struct ethnl_request_ops *
233*4882a593Smuzhiyun ethnl_default_requests[__ETHTOOL_MSG_USER_CNT] = {
234*4882a593Smuzhiyun 	[ETHTOOL_MSG_STRSET_GET]	= &ethnl_strset_request_ops,
235*4882a593Smuzhiyun 	[ETHTOOL_MSG_LINKINFO_GET]	= &ethnl_linkinfo_request_ops,
236*4882a593Smuzhiyun 	[ETHTOOL_MSG_LINKMODES_GET]	= &ethnl_linkmodes_request_ops,
237*4882a593Smuzhiyun 	[ETHTOOL_MSG_LINKSTATE_GET]	= &ethnl_linkstate_request_ops,
238*4882a593Smuzhiyun 	[ETHTOOL_MSG_DEBUG_GET]		= &ethnl_debug_request_ops,
239*4882a593Smuzhiyun 	[ETHTOOL_MSG_WOL_GET]		= &ethnl_wol_request_ops,
240*4882a593Smuzhiyun 	[ETHTOOL_MSG_FEATURES_GET]	= &ethnl_features_request_ops,
241*4882a593Smuzhiyun 	[ETHTOOL_MSG_PRIVFLAGS_GET]	= &ethnl_privflags_request_ops,
242*4882a593Smuzhiyun 	[ETHTOOL_MSG_RINGS_GET]		= &ethnl_rings_request_ops,
243*4882a593Smuzhiyun 	[ETHTOOL_MSG_CHANNELS_GET]	= &ethnl_channels_request_ops,
244*4882a593Smuzhiyun 	[ETHTOOL_MSG_COALESCE_GET]	= &ethnl_coalesce_request_ops,
245*4882a593Smuzhiyun 	[ETHTOOL_MSG_PAUSE_GET]		= &ethnl_pause_request_ops,
246*4882a593Smuzhiyun 	[ETHTOOL_MSG_EEE_GET]		= &ethnl_eee_request_ops,
247*4882a593Smuzhiyun 	[ETHTOOL_MSG_TSINFO_GET]	= &ethnl_tsinfo_request_ops,
248*4882a593Smuzhiyun };
249*4882a593Smuzhiyun 
ethnl_dump_context(struct netlink_callback * cb)250*4882a593Smuzhiyun static struct ethnl_dump_ctx *ethnl_dump_context(struct netlink_callback *cb)
251*4882a593Smuzhiyun {
252*4882a593Smuzhiyun 	return (struct ethnl_dump_ctx *)cb->ctx;
253*4882a593Smuzhiyun }
254*4882a593Smuzhiyun 
255*4882a593Smuzhiyun /**
256*4882a593Smuzhiyun  * ethnl_default_parse() - Parse request message
257*4882a593Smuzhiyun  * @req_info:    pointer to structure to put data into
258*4882a593Smuzhiyun  * @tb:		 parsed attributes
259*4882a593Smuzhiyun  * @net:         request netns
260*4882a593Smuzhiyun  * @request_ops: struct request_ops for request type
261*4882a593Smuzhiyun  * @extack:      netlink extack for error reporting
262*4882a593Smuzhiyun  * @require_dev: fail if no device identified in header
263*4882a593Smuzhiyun  *
264*4882a593Smuzhiyun  * Parse universal request header and call request specific ->parse_request()
265*4882a593Smuzhiyun  * callback (if defined) to parse the rest of the message.
266*4882a593Smuzhiyun  *
267*4882a593Smuzhiyun  * Return: 0 on success or negative error code
268*4882a593Smuzhiyun  */
ethnl_default_parse(struct ethnl_req_info * req_info,struct nlattr ** tb,struct net * net,const struct ethnl_request_ops * request_ops,struct netlink_ext_ack * extack,bool require_dev)269*4882a593Smuzhiyun static int ethnl_default_parse(struct ethnl_req_info *req_info,
270*4882a593Smuzhiyun 			       struct nlattr **tb, struct net *net,
271*4882a593Smuzhiyun 			       const struct ethnl_request_ops *request_ops,
272*4882a593Smuzhiyun 			       struct netlink_ext_ack *extack, bool require_dev)
273*4882a593Smuzhiyun {
274*4882a593Smuzhiyun 	int ret;
275*4882a593Smuzhiyun 
276*4882a593Smuzhiyun 	ret = ethnl_parse_header_dev_get(req_info, tb[request_ops->hdr_attr],
277*4882a593Smuzhiyun 					 net, extack, require_dev);
278*4882a593Smuzhiyun 	if (ret < 0)
279*4882a593Smuzhiyun 		return ret;
280*4882a593Smuzhiyun 
281*4882a593Smuzhiyun 	if (request_ops->parse_request) {
282*4882a593Smuzhiyun 		ret = request_ops->parse_request(req_info, tb, extack);
283*4882a593Smuzhiyun 		if (ret < 0)
284*4882a593Smuzhiyun 			return ret;
285*4882a593Smuzhiyun 	}
286*4882a593Smuzhiyun 
287*4882a593Smuzhiyun 	return 0;
288*4882a593Smuzhiyun }
289*4882a593Smuzhiyun 
290*4882a593Smuzhiyun /**
291*4882a593Smuzhiyun  * ethnl_init_reply_data() - Initialize reply data for GET request
292*4882a593Smuzhiyun  * @reply_data: pointer to embedded struct ethnl_reply_data
293*4882a593Smuzhiyun  * @ops:        instance of struct ethnl_request_ops describing the layout
294*4882a593Smuzhiyun  * @dev:        network device to initialize the reply for
295*4882a593Smuzhiyun  *
296*4882a593Smuzhiyun  * Fills the reply data part with zeros and sets the dev member. Must be called
297*4882a593Smuzhiyun  * before calling the ->fill_reply() callback (for each iteration when handling
298*4882a593Smuzhiyun  * dump requests).
299*4882a593Smuzhiyun  */
ethnl_init_reply_data(struct ethnl_reply_data * reply_data,const struct ethnl_request_ops * ops,struct net_device * dev)300*4882a593Smuzhiyun static void ethnl_init_reply_data(struct ethnl_reply_data *reply_data,
301*4882a593Smuzhiyun 				  const struct ethnl_request_ops *ops,
302*4882a593Smuzhiyun 				  struct net_device *dev)
303*4882a593Smuzhiyun {
304*4882a593Smuzhiyun 	memset(reply_data, 0, ops->reply_data_size);
305*4882a593Smuzhiyun 	reply_data->dev = dev;
306*4882a593Smuzhiyun }
307*4882a593Smuzhiyun 
308*4882a593Smuzhiyun /* default ->doit() handler for GET type requests */
ethnl_default_doit(struct sk_buff * skb,struct genl_info * info)309*4882a593Smuzhiyun static int ethnl_default_doit(struct sk_buff *skb, struct genl_info *info)
310*4882a593Smuzhiyun {
311*4882a593Smuzhiyun 	struct ethnl_reply_data *reply_data = NULL;
312*4882a593Smuzhiyun 	struct ethnl_req_info *req_info = NULL;
313*4882a593Smuzhiyun 	const u8 cmd = info->genlhdr->cmd;
314*4882a593Smuzhiyun 	const struct ethnl_request_ops *ops;
315*4882a593Smuzhiyun 	struct sk_buff *rskb;
316*4882a593Smuzhiyun 	void *reply_payload;
317*4882a593Smuzhiyun 	int reply_len;
318*4882a593Smuzhiyun 	int ret;
319*4882a593Smuzhiyun 
320*4882a593Smuzhiyun 	ops = ethnl_default_requests[cmd];
321*4882a593Smuzhiyun 	if (WARN_ONCE(!ops, "cmd %u has no ethnl_request_ops\n", cmd))
322*4882a593Smuzhiyun 		return -EOPNOTSUPP;
323*4882a593Smuzhiyun 	req_info = kzalloc(ops->req_info_size, GFP_KERNEL);
324*4882a593Smuzhiyun 	if (!req_info)
325*4882a593Smuzhiyun 		return -ENOMEM;
326*4882a593Smuzhiyun 	reply_data = kmalloc(ops->reply_data_size, GFP_KERNEL);
327*4882a593Smuzhiyun 	if (!reply_data) {
328*4882a593Smuzhiyun 		kfree(req_info);
329*4882a593Smuzhiyun 		return -ENOMEM;
330*4882a593Smuzhiyun 	}
331*4882a593Smuzhiyun 
332*4882a593Smuzhiyun 	ret = ethnl_default_parse(req_info, info->attrs, genl_info_net(info),
333*4882a593Smuzhiyun 				  ops, info->extack, !ops->allow_nodev_do);
334*4882a593Smuzhiyun 	if (ret < 0)
335*4882a593Smuzhiyun 		goto err_dev;
336*4882a593Smuzhiyun 	ethnl_init_reply_data(reply_data, ops, req_info->dev);
337*4882a593Smuzhiyun 
338*4882a593Smuzhiyun 	rtnl_lock();
339*4882a593Smuzhiyun 	ret = ops->prepare_data(req_info, reply_data, info);
340*4882a593Smuzhiyun 	rtnl_unlock();
341*4882a593Smuzhiyun 	if (ret < 0)
342*4882a593Smuzhiyun 		goto err_cleanup;
343*4882a593Smuzhiyun 	ret = ops->reply_size(req_info, reply_data);
344*4882a593Smuzhiyun 	if (ret < 0)
345*4882a593Smuzhiyun 		goto err_cleanup;
346*4882a593Smuzhiyun 	reply_len = ret + ethnl_reply_header_size();
347*4882a593Smuzhiyun 	ret = -ENOMEM;
348*4882a593Smuzhiyun 	rskb = ethnl_reply_init(reply_len, req_info->dev, ops->reply_cmd,
349*4882a593Smuzhiyun 				ops->hdr_attr, info, &reply_payload);
350*4882a593Smuzhiyun 	if (!rskb)
351*4882a593Smuzhiyun 		goto err_cleanup;
352*4882a593Smuzhiyun 	ret = ops->fill_reply(rskb, req_info, reply_data);
353*4882a593Smuzhiyun 	if (ret < 0)
354*4882a593Smuzhiyun 		goto err_msg;
355*4882a593Smuzhiyun 	if (ops->cleanup_data)
356*4882a593Smuzhiyun 		ops->cleanup_data(reply_data);
357*4882a593Smuzhiyun 
358*4882a593Smuzhiyun 	genlmsg_end(rskb, reply_payload);
359*4882a593Smuzhiyun 	if (req_info->dev)
360*4882a593Smuzhiyun 		dev_put(req_info->dev);
361*4882a593Smuzhiyun 	kfree(reply_data);
362*4882a593Smuzhiyun 	kfree(req_info);
363*4882a593Smuzhiyun 	return genlmsg_reply(rskb, info);
364*4882a593Smuzhiyun 
365*4882a593Smuzhiyun err_msg:
366*4882a593Smuzhiyun 	WARN_ONCE(ret == -EMSGSIZE, "calculated message payload length (%d) not sufficient\n", reply_len);
367*4882a593Smuzhiyun 	nlmsg_free(rskb);
368*4882a593Smuzhiyun err_cleanup:
369*4882a593Smuzhiyun 	if (ops->cleanup_data)
370*4882a593Smuzhiyun 		ops->cleanup_data(reply_data);
371*4882a593Smuzhiyun err_dev:
372*4882a593Smuzhiyun 	if (req_info->dev)
373*4882a593Smuzhiyun 		dev_put(req_info->dev);
374*4882a593Smuzhiyun 	kfree(reply_data);
375*4882a593Smuzhiyun 	kfree(req_info);
376*4882a593Smuzhiyun 	return ret;
377*4882a593Smuzhiyun }
378*4882a593Smuzhiyun 
ethnl_default_dump_one(struct sk_buff * skb,struct net_device * dev,const struct ethnl_dump_ctx * ctx,struct netlink_callback * cb)379*4882a593Smuzhiyun static int ethnl_default_dump_one(struct sk_buff *skb, struct net_device *dev,
380*4882a593Smuzhiyun 				  const struct ethnl_dump_ctx *ctx,
381*4882a593Smuzhiyun 				  struct netlink_callback *cb)
382*4882a593Smuzhiyun {
383*4882a593Smuzhiyun 	void *ehdr;
384*4882a593Smuzhiyun 	int ret;
385*4882a593Smuzhiyun 
386*4882a593Smuzhiyun 	ehdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
387*4882a593Smuzhiyun 			   &ethtool_genl_family, NLM_F_MULTI,
388*4882a593Smuzhiyun 			   ctx->ops->reply_cmd);
389*4882a593Smuzhiyun 	if (!ehdr)
390*4882a593Smuzhiyun 		return -EMSGSIZE;
391*4882a593Smuzhiyun 
392*4882a593Smuzhiyun 	ethnl_init_reply_data(ctx->reply_data, ctx->ops, dev);
393*4882a593Smuzhiyun 	rtnl_lock();
394*4882a593Smuzhiyun 	ret = ctx->ops->prepare_data(ctx->req_info, ctx->reply_data, NULL);
395*4882a593Smuzhiyun 	rtnl_unlock();
396*4882a593Smuzhiyun 	if (ret < 0)
397*4882a593Smuzhiyun 		goto out;
398*4882a593Smuzhiyun 	ret = ethnl_fill_reply_header(skb, dev, ctx->ops->hdr_attr);
399*4882a593Smuzhiyun 	if (ret < 0)
400*4882a593Smuzhiyun 		goto out;
401*4882a593Smuzhiyun 	ret = ctx->ops->fill_reply(skb, ctx->req_info, ctx->reply_data);
402*4882a593Smuzhiyun 
403*4882a593Smuzhiyun out:
404*4882a593Smuzhiyun 	if (ctx->ops->cleanup_data)
405*4882a593Smuzhiyun 		ctx->ops->cleanup_data(ctx->reply_data);
406*4882a593Smuzhiyun 	ctx->reply_data->dev = NULL;
407*4882a593Smuzhiyun 	if (ret < 0)
408*4882a593Smuzhiyun 		genlmsg_cancel(skb, ehdr);
409*4882a593Smuzhiyun 	else
410*4882a593Smuzhiyun 		genlmsg_end(skb, ehdr);
411*4882a593Smuzhiyun 	return ret;
412*4882a593Smuzhiyun }
413*4882a593Smuzhiyun 
414*4882a593Smuzhiyun /* Default ->dumpit() handler for GET requests. Device iteration copied from
415*4882a593Smuzhiyun  * rtnl_dump_ifinfo(); we have to be more careful about device hashtable
416*4882a593Smuzhiyun  * persistence as we cannot guarantee to hold RTNL lock through the whole
417*4882a593Smuzhiyun  * function as rtnetnlink does.
418*4882a593Smuzhiyun  */
ethnl_default_dumpit(struct sk_buff * skb,struct netlink_callback * cb)419*4882a593Smuzhiyun static int ethnl_default_dumpit(struct sk_buff *skb,
420*4882a593Smuzhiyun 				struct netlink_callback *cb)
421*4882a593Smuzhiyun {
422*4882a593Smuzhiyun 	struct ethnl_dump_ctx *ctx = ethnl_dump_context(cb);
423*4882a593Smuzhiyun 	struct net *net = sock_net(skb->sk);
424*4882a593Smuzhiyun 	int s_idx = ctx->pos_idx;
425*4882a593Smuzhiyun 	int h, idx = 0;
426*4882a593Smuzhiyun 	int ret = 0;
427*4882a593Smuzhiyun 
428*4882a593Smuzhiyun 	rtnl_lock();
429*4882a593Smuzhiyun 	for (h = ctx->pos_hash; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
430*4882a593Smuzhiyun 		struct hlist_head *head;
431*4882a593Smuzhiyun 		struct net_device *dev;
432*4882a593Smuzhiyun 		unsigned int seq;
433*4882a593Smuzhiyun 
434*4882a593Smuzhiyun 		head = &net->dev_index_head[h];
435*4882a593Smuzhiyun 
436*4882a593Smuzhiyun restart_chain:
437*4882a593Smuzhiyun 		seq = net->dev_base_seq;
438*4882a593Smuzhiyun 		cb->seq = seq;
439*4882a593Smuzhiyun 		idx = 0;
440*4882a593Smuzhiyun 		hlist_for_each_entry(dev, head, index_hlist) {
441*4882a593Smuzhiyun 			if (idx < s_idx)
442*4882a593Smuzhiyun 				goto cont;
443*4882a593Smuzhiyun 			dev_hold(dev);
444*4882a593Smuzhiyun 			rtnl_unlock();
445*4882a593Smuzhiyun 
446*4882a593Smuzhiyun 			ret = ethnl_default_dump_one(skb, dev, ctx, cb);
447*4882a593Smuzhiyun 			dev_put(dev);
448*4882a593Smuzhiyun 			if (ret < 0) {
449*4882a593Smuzhiyun 				if (ret == -EOPNOTSUPP)
450*4882a593Smuzhiyun 					goto lock_and_cont;
451*4882a593Smuzhiyun 				if (likely(skb->len))
452*4882a593Smuzhiyun 					ret = skb->len;
453*4882a593Smuzhiyun 				goto out;
454*4882a593Smuzhiyun 			}
455*4882a593Smuzhiyun lock_and_cont:
456*4882a593Smuzhiyun 			rtnl_lock();
457*4882a593Smuzhiyun 			if (net->dev_base_seq != seq) {
458*4882a593Smuzhiyun 				s_idx = idx + 1;
459*4882a593Smuzhiyun 				goto restart_chain;
460*4882a593Smuzhiyun 			}
461*4882a593Smuzhiyun cont:
462*4882a593Smuzhiyun 			idx++;
463*4882a593Smuzhiyun 		}
464*4882a593Smuzhiyun 
465*4882a593Smuzhiyun 	}
466*4882a593Smuzhiyun 	rtnl_unlock();
467*4882a593Smuzhiyun 
468*4882a593Smuzhiyun out:
469*4882a593Smuzhiyun 	ctx->pos_hash = h;
470*4882a593Smuzhiyun 	ctx->pos_idx = idx;
471*4882a593Smuzhiyun 	nl_dump_check_consistent(cb, nlmsg_hdr(skb));
472*4882a593Smuzhiyun 
473*4882a593Smuzhiyun 	return ret;
474*4882a593Smuzhiyun }
475*4882a593Smuzhiyun 
476*4882a593Smuzhiyun /* generic ->start() handler for GET requests */
ethnl_default_start(struct netlink_callback * cb)477*4882a593Smuzhiyun static int ethnl_default_start(struct netlink_callback *cb)
478*4882a593Smuzhiyun {
479*4882a593Smuzhiyun 	const struct genl_dumpit_info *info = genl_dumpit_info(cb);
480*4882a593Smuzhiyun 	struct ethnl_dump_ctx *ctx = ethnl_dump_context(cb);
481*4882a593Smuzhiyun 	struct ethnl_reply_data *reply_data;
482*4882a593Smuzhiyun 	const struct ethnl_request_ops *ops;
483*4882a593Smuzhiyun 	struct ethnl_req_info *req_info;
484*4882a593Smuzhiyun 	struct genlmsghdr *ghdr;
485*4882a593Smuzhiyun 	int ret;
486*4882a593Smuzhiyun 
487*4882a593Smuzhiyun 	BUILD_BUG_ON(sizeof(*ctx) > sizeof(cb->ctx));
488*4882a593Smuzhiyun 
489*4882a593Smuzhiyun 	ghdr = nlmsg_data(cb->nlh);
490*4882a593Smuzhiyun 	ops = ethnl_default_requests[ghdr->cmd];
491*4882a593Smuzhiyun 	if (WARN_ONCE(!ops, "cmd %u has no ethnl_request_ops\n", ghdr->cmd))
492*4882a593Smuzhiyun 		return -EOPNOTSUPP;
493*4882a593Smuzhiyun 	req_info = kzalloc(ops->req_info_size, GFP_KERNEL);
494*4882a593Smuzhiyun 	if (!req_info)
495*4882a593Smuzhiyun 		return -ENOMEM;
496*4882a593Smuzhiyun 	reply_data = kmalloc(ops->reply_data_size, GFP_KERNEL);
497*4882a593Smuzhiyun 	if (!reply_data) {
498*4882a593Smuzhiyun 		ret = -ENOMEM;
499*4882a593Smuzhiyun 		goto free_req_info;
500*4882a593Smuzhiyun 	}
501*4882a593Smuzhiyun 
502*4882a593Smuzhiyun 	ret = ethnl_default_parse(req_info, info->attrs, sock_net(cb->skb->sk),
503*4882a593Smuzhiyun 				  ops, cb->extack, false);
504*4882a593Smuzhiyun 	if (req_info->dev) {
505*4882a593Smuzhiyun 		/* We ignore device specification in dump requests but as the
506*4882a593Smuzhiyun 		 * same parser as for non-dump (doit) requests is used, it
507*4882a593Smuzhiyun 		 * would take reference to the device if it finds one
508*4882a593Smuzhiyun 		 */
509*4882a593Smuzhiyun 		dev_put(req_info->dev);
510*4882a593Smuzhiyun 		req_info->dev = NULL;
511*4882a593Smuzhiyun 	}
512*4882a593Smuzhiyun 	if (ret < 0)
513*4882a593Smuzhiyun 		goto free_reply_data;
514*4882a593Smuzhiyun 
515*4882a593Smuzhiyun 	ctx->ops = ops;
516*4882a593Smuzhiyun 	ctx->req_info = req_info;
517*4882a593Smuzhiyun 	ctx->reply_data = reply_data;
518*4882a593Smuzhiyun 	ctx->pos_hash = 0;
519*4882a593Smuzhiyun 	ctx->pos_idx = 0;
520*4882a593Smuzhiyun 
521*4882a593Smuzhiyun 	return 0;
522*4882a593Smuzhiyun 
523*4882a593Smuzhiyun free_reply_data:
524*4882a593Smuzhiyun 	kfree(reply_data);
525*4882a593Smuzhiyun free_req_info:
526*4882a593Smuzhiyun 	kfree(req_info);
527*4882a593Smuzhiyun 
528*4882a593Smuzhiyun 	return ret;
529*4882a593Smuzhiyun }
530*4882a593Smuzhiyun 
531*4882a593Smuzhiyun /* default ->done() handler for GET requests */
ethnl_default_done(struct netlink_callback * cb)532*4882a593Smuzhiyun static int ethnl_default_done(struct netlink_callback *cb)
533*4882a593Smuzhiyun {
534*4882a593Smuzhiyun 	struct ethnl_dump_ctx *ctx = ethnl_dump_context(cb);
535*4882a593Smuzhiyun 
536*4882a593Smuzhiyun 	kfree(ctx->reply_data);
537*4882a593Smuzhiyun 	kfree(ctx->req_info);
538*4882a593Smuzhiyun 
539*4882a593Smuzhiyun 	return 0;
540*4882a593Smuzhiyun }
541*4882a593Smuzhiyun 
542*4882a593Smuzhiyun static const struct ethnl_request_ops *
543*4882a593Smuzhiyun ethnl_default_notify_ops[ETHTOOL_MSG_KERNEL_MAX + 1] = {
544*4882a593Smuzhiyun 	[ETHTOOL_MSG_LINKINFO_NTF]	= &ethnl_linkinfo_request_ops,
545*4882a593Smuzhiyun 	[ETHTOOL_MSG_LINKMODES_NTF]	= &ethnl_linkmodes_request_ops,
546*4882a593Smuzhiyun 	[ETHTOOL_MSG_DEBUG_NTF]		= &ethnl_debug_request_ops,
547*4882a593Smuzhiyun 	[ETHTOOL_MSG_WOL_NTF]		= &ethnl_wol_request_ops,
548*4882a593Smuzhiyun 	[ETHTOOL_MSG_FEATURES_NTF]	= &ethnl_features_request_ops,
549*4882a593Smuzhiyun 	[ETHTOOL_MSG_PRIVFLAGS_NTF]	= &ethnl_privflags_request_ops,
550*4882a593Smuzhiyun 	[ETHTOOL_MSG_RINGS_NTF]		= &ethnl_rings_request_ops,
551*4882a593Smuzhiyun 	[ETHTOOL_MSG_CHANNELS_NTF]	= &ethnl_channels_request_ops,
552*4882a593Smuzhiyun 	[ETHTOOL_MSG_COALESCE_NTF]	= &ethnl_coalesce_request_ops,
553*4882a593Smuzhiyun 	[ETHTOOL_MSG_PAUSE_NTF]		= &ethnl_pause_request_ops,
554*4882a593Smuzhiyun 	[ETHTOOL_MSG_EEE_NTF]		= &ethnl_eee_request_ops,
555*4882a593Smuzhiyun };
556*4882a593Smuzhiyun 
557*4882a593Smuzhiyun /* default notification handler */
ethnl_default_notify(struct net_device * dev,unsigned int cmd,const void * data)558*4882a593Smuzhiyun static void ethnl_default_notify(struct net_device *dev, unsigned int cmd,
559*4882a593Smuzhiyun 				 const void *data)
560*4882a593Smuzhiyun {
561*4882a593Smuzhiyun 	struct ethnl_reply_data *reply_data;
562*4882a593Smuzhiyun 	const struct ethnl_request_ops *ops;
563*4882a593Smuzhiyun 	struct ethnl_req_info *req_info;
564*4882a593Smuzhiyun 	struct sk_buff *skb;
565*4882a593Smuzhiyun 	void *reply_payload;
566*4882a593Smuzhiyun 	int reply_len;
567*4882a593Smuzhiyun 	int ret;
568*4882a593Smuzhiyun 
569*4882a593Smuzhiyun 	if (WARN_ONCE(cmd > ETHTOOL_MSG_KERNEL_MAX ||
570*4882a593Smuzhiyun 		      !ethnl_default_notify_ops[cmd],
571*4882a593Smuzhiyun 		      "unexpected notification type %u\n", cmd))
572*4882a593Smuzhiyun 		return;
573*4882a593Smuzhiyun 	ops = ethnl_default_notify_ops[cmd];
574*4882a593Smuzhiyun 	req_info = kzalloc(ops->req_info_size, GFP_KERNEL);
575*4882a593Smuzhiyun 	if (!req_info)
576*4882a593Smuzhiyun 		return;
577*4882a593Smuzhiyun 	reply_data = kmalloc(ops->reply_data_size, GFP_KERNEL);
578*4882a593Smuzhiyun 	if (!reply_data) {
579*4882a593Smuzhiyun 		kfree(req_info);
580*4882a593Smuzhiyun 		return;
581*4882a593Smuzhiyun 	}
582*4882a593Smuzhiyun 
583*4882a593Smuzhiyun 	req_info->dev = dev;
584*4882a593Smuzhiyun 	req_info->flags |= ETHTOOL_FLAG_COMPACT_BITSETS;
585*4882a593Smuzhiyun 
586*4882a593Smuzhiyun 	ethnl_init_reply_data(reply_data, ops, dev);
587*4882a593Smuzhiyun 	ret = ops->prepare_data(req_info, reply_data, NULL);
588*4882a593Smuzhiyun 	if (ret < 0)
589*4882a593Smuzhiyun 		goto err_cleanup;
590*4882a593Smuzhiyun 	ret = ops->reply_size(req_info, reply_data);
591*4882a593Smuzhiyun 	if (ret < 0)
592*4882a593Smuzhiyun 		goto err_cleanup;
593*4882a593Smuzhiyun 	reply_len = ret + ethnl_reply_header_size();
594*4882a593Smuzhiyun 	ret = -ENOMEM;
595*4882a593Smuzhiyun 	skb = genlmsg_new(reply_len, GFP_KERNEL);
596*4882a593Smuzhiyun 	if (!skb)
597*4882a593Smuzhiyun 		goto err_cleanup;
598*4882a593Smuzhiyun 	reply_payload = ethnl_bcastmsg_put(skb, cmd);
599*4882a593Smuzhiyun 	if (!reply_payload)
600*4882a593Smuzhiyun 		goto err_skb;
601*4882a593Smuzhiyun 	ret = ethnl_fill_reply_header(skb, dev, ops->hdr_attr);
602*4882a593Smuzhiyun 	if (ret < 0)
603*4882a593Smuzhiyun 		goto err_msg;
604*4882a593Smuzhiyun 	ret = ops->fill_reply(skb, req_info, reply_data);
605*4882a593Smuzhiyun 	if (ret < 0)
606*4882a593Smuzhiyun 		goto err_msg;
607*4882a593Smuzhiyun 	if (ops->cleanup_data)
608*4882a593Smuzhiyun 		ops->cleanup_data(reply_data);
609*4882a593Smuzhiyun 
610*4882a593Smuzhiyun 	genlmsg_end(skb, reply_payload);
611*4882a593Smuzhiyun 	kfree(reply_data);
612*4882a593Smuzhiyun 	kfree(req_info);
613*4882a593Smuzhiyun 	ethnl_multicast(skb, dev);
614*4882a593Smuzhiyun 	return;
615*4882a593Smuzhiyun 
616*4882a593Smuzhiyun err_msg:
617*4882a593Smuzhiyun 	WARN_ONCE(ret == -EMSGSIZE,
618*4882a593Smuzhiyun 		  "calculated message payload length (%d) not sufficient\n",
619*4882a593Smuzhiyun 		  reply_len);
620*4882a593Smuzhiyun err_skb:
621*4882a593Smuzhiyun 	nlmsg_free(skb);
622*4882a593Smuzhiyun err_cleanup:
623*4882a593Smuzhiyun 	if (ops->cleanup_data)
624*4882a593Smuzhiyun 		ops->cleanup_data(reply_data);
625*4882a593Smuzhiyun 	kfree(reply_data);
626*4882a593Smuzhiyun 	kfree(req_info);
627*4882a593Smuzhiyun 	return;
628*4882a593Smuzhiyun }
629*4882a593Smuzhiyun 
630*4882a593Smuzhiyun /* notifications */
631*4882a593Smuzhiyun 
632*4882a593Smuzhiyun typedef void (*ethnl_notify_handler_t)(struct net_device *dev, unsigned int cmd,
633*4882a593Smuzhiyun 				       const void *data);
634*4882a593Smuzhiyun 
635*4882a593Smuzhiyun static const ethnl_notify_handler_t ethnl_notify_handlers[] = {
636*4882a593Smuzhiyun 	[ETHTOOL_MSG_LINKINFO_NTF]	= ethnl_default_notify,
637*4882a593Smuzhiyun 	[ETHTOOL_MSG_LINKMODES_NTF]	= ethnl_default_notify,
638*4882a593Smuzhiyun 	[ETHTOOL_MSG_DEBUG_NTF]		= ethnl_default_notify,
639*4882a593Smuzhiyun 	[ETHTOOL_MSG_WOL_NTF]		= ethnl_default_notify,
640*4882a593Smuzhiyun 	[ETHTOOL_MSG_FEATURES_NTF]	= ethnl_default_notify,
641*4882a593Smuzhiyun 	[ETHTOOL_MSG_PRIVFLAGS_NTF]	= ethnl_default_notify,
642*4882a593Smuzhiyun 	[ETHTOOL_MSG_RINGS_NTF]		= ethnl_default_notify,
643*4882a593Smuzhiyun 	[ETHTOOL_MSG_CHANNELS_NTF]	= ethnl_default_notify,
644*4882a593Smuzhiyun 	[ETHTOOL_MSG_COALESCE_NTF]	= ethnl_default_notify,
645*4882a593Smuzhiyun 	[ETHTOOL_MSG_PAUSE_NTF]		= ethnl_default_notify,
646*4882a593Smuzhiyun 	[ETHTOOL_MSG_EEE_NTF]		= ethnl_default_notify,
647*4882a593Smuzhiyun };
648*4882a593Smuzhiyun 
ethtool_notify(struct net_device * dev,unsigned int cmd,const void * data)649*4882a593Smuzhiyun void ethtool_notify(struct net_device *dev, unsigned int cmd, const void *data)
650*4882a593Smuzhiyun {
651*4882a593Smuzhiyun 	if (unlikely(!ethnl_ok))
652*4882a593Smuzhiyun 		return;
653*4882a593Smuzhiyun 	ASSERT_RTNL();
654*4882a593Smuzhiyun 
655*4882a593Smuzhiyun 	if (likely(cmd < ARRAY_SIZE(ethnl_notify_handlers) &&
656*4882a593Smuzhiyun 		   ethnl_notify_handlers[cmd]))
657*4882a593Smuzhiyun 		ethnl_notify_handlers[cmd](dev, cmd, data);
658*4882a593Smuzhiyun 	else
659*4882a593Smuzhiyun 		WARN_ONCE(1, "notification %u not implemented (dev=%s)\n",
660*4882a593Smuzhiyun 			  cmd, netdev_name(dev));
661*4882a593Smuzhiyun }
662*4882a593Smuzhiyun EXPORT_SYMBOL(ethtool_notify);
663*4882a593Smuzhiyun 
ethnl_notify_features(struct netdev_notifier_info * info)664*4882a593Smuzhiyun static void ethnl_notify_features(struct netdev_notifier_info *info)
665*4882a593Smuzhiyun {
666*4882a593Smuzhiyun 	struct net_device *dev = netdev_notifier_info_to_dev(info);
667*4882a593Smuzhiyun 
668*4882a593Smuzhiyun 	ethtool_notify(dev, ETHTOOL_MSG_FEATURES_NTF, NULL);
669*4882a593Smuzhiyun }
670*4882a593Smuzhiyun 
ethnl_netdev_event(struct notifier_block * this,unsigned long event,void * ptr)671*4882a593Smuzhiyun static int ethnl_netdev_event(struct notifier_block *this, unsigned long event,
672*4882a593Smuzhiyun 			      void *ptr)
673*4882a593Smuzhiyun {
674*4882a593Smuzhiyun 	switch (event) {
675*4882a593Smuzhiyun 	case NETDEV_FEAT_CHANGE:
676*4882a593Smuzhiyun 		ethnl_notify_features(ptr);
677*4882a593Smuzhiyun 		break;
678*4882a593Smuzhiyun 	}
679*4882a593Smuzhiyun 
680*4882a593Smuzhiyun 	return NOTIFY_DONE;
681*4882a593Smuzhiyun }
682*4882a593Smuzhiyun 
683*4882a593Smuzhiyun static struct notifier_block ethnl_netdev_notifier = {
684*4882a593Smuzhiyun 	.notifier_call = ethnl_netdev_event,
685*4882a593Smuzhiyun };
686*4882a593Smuzhiyun 
687*4882a593Smuzhiyun /* genetlink setup */
688*4882a593Smuzhiyun 
689*4882a593Smuzhiyun static const struct genl_ops ethtool_genl_ops[] = {
690*4882a593Smuzhiyun 	{
691*4882a593Smuzhiyun 		.cmd	= ETHTOOL_MSG_STRSET_GET,
692*4882a593Smuzhiyun 		.doit	= ethnl_default_doit,
693*4882a593Smuzhiyun 		.start	= ethnl_default_start,
694*4882a593Smuzhiyun 		.dumpit	= ethnl_default_dumpit,
695*4882a593Smuzhiyun 		.done	= ethnl_default_done,
696*4882a593Smuzhiyun 		.policy = ethnl_strset_get_policy,
697*4882a593Smuzhiyun 		.maxattr = ARRAY_SIZE(ethnl_strset_get_policy) - 1,
698*4882a593Smuzhiyun 	},
699*4882a593Smuzhiyun 	{
700*4882a593Smuzhiyun 		.cmd	= ETHTOOL_MSG_LINKINFO_GET,
701*4882a593Smuzhiyun 		.doit	= ethnl_default_doit,
702*4882a593Smuzhiyun 		.start	= ethnl_default_start,
703*4882a593Smuzhiyun 		.dumpit	= ethnl_default_dumpit,
704*4882a593Smuzhiyun 		.done	= ethnl_default_done,
705*4882a593Smuzhiyun 		.policy = ethnl_linkinfo_get_policy,
706*4882a593Smuzhiyun 		.maxattr = ARRAY_SIZE(ethnl_linkinfo_get_policy) - 1,
707*4882a593Smuzhiyun 	},
708*4882a593Smuzhiyun 	{
709*4882a593Smuzhiyun 		.cmd	= ETHTOOL_MSG_LINKINFO_SET,
710*4882a593Smuzhiyun 		.flags	= GENL_UNS_ADMIN_PERM,
711*4882a593Smuzhiyun 		.doit	= ethnl_set_linkinfo,
712*4882a593Smuzhiyun 		.policy = ethnl_linkinfo_set_policy,
713*4882a593Smuzhiyun 		.maxattr = ARRAY_SIZE(ethnl_linkinfo_set_policy) - 1,
714*4882a593Smuzhiyun 	},
715*4882a593Smuzhiyun 	{
716*4882a593Smuzhiyun 		.cmd	= ETHTOOL_MSG_LINKMODES_GET,
717*4882a593Smuzhiyun 		.doit	= ethnl_default_doit,
718*4882a593Smuzhiyun 		.start	= ethnl_default_start,
719*4882a593Smuzhiyun 		.dumpit	= ethnl_default_dumpit,
720*4882a593Smuzhiyun 		.done	= ethnl_default_done,
721*4882a593Smuzhiyun 		.policy = ethnl_linkmodes_get_policy,
722*4882a593Smuzhiyun 		.maxattr = ARRAY_SIZE(ethnl_linkmodes_get_policy) - 1,
723*4882a593Smuzhiyun 	},
724*4882a593Smuzhiyun 	{
725*4882a593Smuzhiyun 		.cmd	= ETHTOOL_MSG_LINKMODES_SET,
726*4882a593Smuzhiyun 		.flags	= GENL_UNS_ADMIN_PERM,
727*4882a593Smuzhiyun 		.doit	= ethnl_set_linkmodes,
728*4882a593Smuzhiyun 		.policy = ethnl_linkmodes_set_policy,
729*4882a593Smuzhiyun 		.maxattr = ARRAY_SIZE(ethnl_linkmodes_set_policy) - 1,
730*4882a593Smuzhiyun 	},
731*4882a593Smuzhiyun 	{
732*4882a593Smuzhiyun 		.cmd	= ETHTOOL_MSG_LINKSTATE_GET,
733*4882a593Smuzhiyun 		.doit	= ethnl_default_doit,
734*4882a593Smuzhiyun 		.start	= ethnl_default_start,
735*4882a593Smuzhiyun 		.dumpit	= ethnl_default_dumpit,
736*4882a593Smuzhiyun 		.done	= ethnl_default_done,
737*4882a593Smuzhiyun 		.policy = ethnl_linkstate_get_policy,
738*4882a593Smuzhiyun 		.maxattr = ARRAY_SIZE(ethnl_linkstate_get_policy) - 1,
739*4882a593Smuzhiyun 	},
740*4882a593Smuzhiyun 	{
741*4882a593Smuzhiyun 		.cmd	= ETHTOOL_MSG_DEBUG_GET,
742*4882a593Smuzhiyun 		.doit	= ethnl_default_doit,
743*4882a593Smuzhiyun 		.start	= ethnl_default_start,
744*4882a593Smuzhiyun 		.dumpit	= ethnl_default_dumpit,
745*4882a593Smuzhiyun 		.done	= ethnl_default_done,
746*4882a593Smuzhiyun 		.policy = ethnl_debug_get_policy,
747*4882a593Smuzhiyun 		.maxattr = ARRAY_SIZE(ethnl_debug_get_policy) - 1,
748*4882a593Smuzhiyun 	},
749*4882a593Smuzhiyun 	{
750*4882a593Smuzhiyun 		.cmd	= ETHTOOL_MSG_DEBUG_SET,
751*4882a593Smuzhiyun 		.flags	= GENL_UNS_ADMIN_PERM,
752*4882a593Smuzhiyun 		.doit	= ethnl_set_debug,
753*4882a593Smuzhiyun 		.policy = ethnl_debug_set_policy,
754*4882a593Smuzhiyun 		.maxattr = ARRAY_SIZE(ethnl_debug_set_policy) - 1,
755*4882a593Smuzhiyun 	},
756*4882a593Smuzhiyun 	{
757*4882a593Smuzhiyun 		.cmd	= ETHTOOL_MSG_WOL_GET,
758*4882a593Smuzhiyun 		.flags	= GENL_UNS_ADMIN_PERM,
759*4882a593Smuzhiyun 		.doit	= ethnl_default_doit,
760*4882a593Smuzhiyun 		.start	= ethnl_default_start,
761*4882a593Smuzhiyun 		.dumpit	= ethnl_default_dumpit,
762*4882a593Smuzhiyun 		.done	= ethnl_default_done,
763*4882a593Smuzhiyun 		.policy = ethnl_wol_get_policy,
764*4882a593Smuzhiyun 		.maxattr = ARRAY_SIZE(ethnl_wol_get_policy) - 1,
765*4882a593Smuzhiyun 	},
766*4882a593Smuzhiyun 	{
767*4882a593Smuzhiyun 		.cmd	= ETHTOOL_MSG_WOL_SET,
768*4882a593Smuzhiyun 		.flags	= GENL_UNS_ADMIN_PERM,
769*4882a593Smuzhiyun 		.doit	= ethnl_set_wol,
770*4882a593Smuzhiyun 		.policy = ethnl_wol_set_policy,
771*4882a593Smuzhiyun 		.maxattr = ARRAY_SIZE(ethnl_wol_set_policy) - 1,
772*4882a593Smuzhiyun 	},
773*4882a593Smuzhiyun 	{
774*4882a593Smuzhiyun 		.cmd	= ETHTOOL_MSG_FEATURES_GET,
775*4882a593Smuzhiyun 		.doit	= ethnl_default_doit,
776*4882a593Smuzhiyun 		.start	= ethnl_default_start,
777*4882a593Smuzhiyun 		.dumpit	= ethnl_default_dumpit,
778*4882a593Smuzhiyun 		.done	= ethnl_default_done,
779*4882a593Smuzhiyun 		.policy = ethnl_features_get_policy,
780*4882a593Smuzhiyun 		.maxattr = ARRAY_SIZE(ethnl_features_get_policy) - 1,
781*4882a593Smuzhiyun 	},
782*4882a593Smuzhiyun 	{
783*4882a593Smuzhiyun 		.cmd	= ETHTOOL_MSG_FEATURES_SET,
784*4882a593Smuzhiyun 		.flags	= GENL_UNS_ADMIN_PERM,
785*4882a593Smuzhiyun 		.doit	= ethnl_set_features,
786*4882a593Smuzhiyun 		.policy = ethnl_features_set_policy,
787*4882a593Smuzhiyun 		.maxattr = ARRAY_SIZE(ethnl_features_set_policy) - 1,
788*4882a593Smuzhiyun 	},
789*4882a593Smuzhiyun 	{
790*4882a593Smuzhiyun 		.cmd	= ETHTOOL_MSG_PRIVFLAGS_GET,
791*4882a593Smuzhiyun 		.doit	= ethnl_default_doit,
792*4882a593Smuzhiyun 		.start	= ethnl_default_start,
793*4882a593Smuzhiyun 		.dumpit	= ethnl_default_dumpit,
794*4882a593Smuzhiyun 		.done	= ethnl_default_done,
795*4882a593Smuzhiyun 		.policy = ethnl_privflags_get_policy,
796*4882a593Smuzhiyun 		.maxattr = ARRAY_SIZE(ethnl_privflags_get_policy) - 1,
797*4882a593Smuzhiyun 	},
798*4882a593Smuzhiyun 	{
799*4882a593Smuzhiyun 		.cmd	= ETHTOOL_MSG_PRIVFLAGS_SET,
800*4882a593Smuzhiyun 		.flags	= GENL_UNS_ADMIN_PERM,
801*4882a593Smuzhiyun 		.doit	= ethnl_set_privflags,
802*4882a593Smuzhiyun 		.policy = ethnl_privflags_set_policy,
803*4882a593Smuzhiyun 		.maxattr = ARRAY_SIZE(ethnl_privflags_set_policy) - 1,
804*4882a593Smuzhiyun 	},
805*4882a593Smuzhiyun 	{
806*4882a593Smuzhiyun 		.cmd	= ETHTOOL_MSG_RINGS_GET,
807*4882a593Smuzhiyun 		.doit	= ethnl_default_doit,
808*4882a593Smuzhiyun 		.start	= ethnl_default_start,
809*4882a593Smuzhiyun 		.dumpit	= ethnl_default_dumpit,
810*4882a593Smuzhiyun 		.done	= ethnl_default_done,
811*4882a593Smuzhiyun 		.policy = ethnl_rings_get_policy,
812*4882a593Smuzhiyun 		.maxattr = ARRAY_SIZE(ethnl_rings_get_policy) - 1,
813*4882a593Smuzhiyun 	},
814*4882a593Smuzhiyun 	{
815*4882a593Smuzhiyun 		.cmd	= ETHTOOL_MSG_RINGS_SET,
816*4882a593Smuzhiyun 		.flags	= GENL_UNS_ADMIN_PERM,
817*4882a593Smuzhiyun 		.doit	= ethnl_set_rings,
818*4882a593Smuzhiyun 		.policy = ethnl_rings_set_policy,
819*4882a593Smuzhiyun 		.maxattr = ARRAY_SIZE(ethnl_rings_set_policy) - 1,
820*4882a593Smuzhiyun 	},
821*4882a593Smuzhiyun 	{
822*4882a593Smuzhiyun 		.cmd	= ETHTOOL_MSG_CHANNELS_GET,
823*4882a593Smuzhiyun 		.doit	= ethnl_default_doit,
824*4882a593Smuzhiyun 		.start	= ethnl_default_start,
825*4882a593Smuzhiyun 		.dumpit	= ethnl_default_dumpit,
826*4882a593Smuzhiyun 		.done	= ethnl_default_done,
827*4882a593Smuzhiyun 		.policy = ethnl_channels_get_policy,
828*4882a593Smuzhiyun 		.maxattr = ARRAY_SIZE(ethnl_channels_get_policy) - 1,
829*4882a593Smuzhiyun 	},
830*4882a593Smuzhiyun 	{
831*4882a593Smuzhiyun 		.cmd	= ETHTOOL_MSG_CHANNELS_SET,
832*4882a593Smuzhiyun 		.flags	= GENL_UNS_ADMIN_PERM,
833*4882a593Smuzhiyun 		.doit	= ethnl_set_channels,
834*4882a593Smuzhiyun 		.policy = ethnl_channels_set_policy,
835*4882a593Smuzhiyun 		.maxattr = ARRAY_SIZE(ethnl_channels_set_policy) - 1,
836*4882a593Smuzhiyun 	},
837*4882a593Smuzhiyun 	{
838*4882a593Smuzhiyun 		.cmd	= ETHTOOL_MSG_COALESCE_GET,
839*4882a593Smuzhiyun 		.doit	= ethnl_default_doit,
840*4882a593Smuzhiyun 		.start	= ethnl_default_start,
841*4882a593Smuzhiyun 		.dumpit	= ethnl_default_dumpit,
842*4882a593Smuzhiyun 		.done	= ethnl_default_done,
843*4882a593Smuzhiyun 		.policy = ethnl_coalesce_get_policy,
844*4882a593Smuzhiyun 		.maxattr = ARRAY_SIZE(ethnl_coalesce_get_policy) - 1,
845*4882a593Smuzhiyun 	},
846*4882a593Smuzhiyun 	{
847*4882a593Smuzhiyun 		.cmd	= ETHTOOL_MSG_COALESCE_SET,
848*4882a593Smuzhiyun 		.flags	= GENL_UNS_ADMIN_PERM,
849*4882a593Smuzhiyun 		.doit	= ethnl_set_coalesce,
850*4882a593Smuzhiyun 		.policy = ethnl_coalesce_set_policy,
851*4882a593Smuzhiyun 		.maxattr = ARRAY_SIZE(ethnl_coalesce_set_policy) - 1,
852*4882a593Smuzhiyun 	},
853*4882a593Smuzhiyun 	{
854*4882a593Smuzhiyun 		.cmd	= ETHTOOL_MSG_PAUSE_GET,
855*4882a593Smuzhiyun 		.doit	= ethnl_default_doit,
856*4882a593Smuzhiyun 		.start	= ethnl_default_start,
857*4882a593Smuzhiyun 		.dumpit	= ethnl_default_dumpit,
858*4882a593Smuzhiyun 		.done	= ethnl_default_done,
859*4882a593Smuzhiyun 		.policy = ethnl_pause_get_policy,
860*4882a593Smuzhiyun 		.maxattr = ARRAY_SIZE(ethnl_pause_get_policy) - 1,
861*4882a593Smuzhiyun 	},
862*4882a593Smuzhiyun 	{
863*4882a593Smuzhiyun 		.cmd	= ETHTOOL_MSG_PAUSE_SET,
864*4882a593Smuzhiyun 		.flags	= GENL_UNS_ADMIN_PERM,
865*4882a593Smuzhiyun 		.doit	= ethnl_set_pause,
866*4882a593Smuzhiyun 		.policy = ethnl_pause_set_policy,
867*4882a593Smuzhiyun 		.maxattr = ARRAY_SIZE(ethnl_pause_set_policy) - 1,
868*4882a593Smuzhiyun 	},
869*4882a593Smuzhiyun 	{
870*4882a593Smuzhiyun 		.cmd	= ETHTOOL_MSG_EEE_GET,
871*4882a593Smuzhiyun 		.doit	= ethnl_default_doit,
872*4882a593Smuzhiyun 		.start	= ethnl_default_start,
873*4882a593Smuzhiyun 		.dumpit	= ethnl_default_dumpit,
874*4882a593Smuzhiyun 		.done	= ethnl_default_done,
875*4882a593Smuzhiyun 		.policy = ethnl_eee_get_policy,
876*4882a593Smuzhiyun 		.maxattr = ARRAY_SIZE(ethnl_eee_get_policy) - 1,
877*4882a593Smuzhiyun 	},
878*4882a593Smuzhiyun 	{
879*4882a593Smuzhiyun 		.cmd	= ETHTOOL_MSG_EEE_SET,
880*4882a593Smuzhiyun 		.flags	= GENL_UNS_ADMIN_PERM,
881*4882a593Smuzhiyun 		.doit	= ethnl_set_eee,
882*4882a593Smuzhiyun 		.policy = ethnl_eee_set_policy,
883*4882a593Smuzhiyun 		.maxattr = ARRAY_SIZE(ethnl_eee_set_policy) - 1,
884*4882a593Smuzhiyun 	},
885*4882a593Smuzhiyun 	{
886*4882a593Smuzhiyun 		.cmd	= ETHTOOL_MSG_TSINFO_GET,
887*4882a593Smuzhiyun 		.doit	= ethnl_default_doit,
888*4882a593Smuzhiyun 		.start	= ethnl_default_start,
889*4882a593Smuzhiyun 		.dumpit	= ethnl_default_dumpit,
890*4882a593Smuzhiyun 		.done	= ethnl_default_done,
891*4882a593Smuzhiyun 		.policy = ethnl_tsinfo_get_policy,
892*4882a593Smuzhiyun 		.maxattr = ARRAY_SIZE(ethnl_tsinfo_get_policy) - 1,
893*4882a593Smuzhiyun 	},
894*4882a593Smuzhiyun 	{
895*4882a593Smuzhiyun 		.cmd	= ETHTOOL_MSG_CABLE_TEST_ACT,
896*4882a593Smuzhiyun 		.flags	= GENL_UNS_ADMIN_PERM,
897*4882a593Smuzhiyun 		.doit	= ethnl_act_cable_test,
898*4882a593Smuzhiyun 		.policy = ethnl_cable_test_act_policy,
899*4882a593Smuzhiyun 		.maxattr = ARRAY_SIZE(ethnl_cable_test_act_policy) - 1,
900*4882a593Smuzhiyun 	},
901*4882a593Smuzhiyun 	{
902*4882a593Smuzhiyun 		.cmd	= ETHTOOL_MSG_CABLE_TEST_TDR_ACT,
903*4882a593Smuzhiyun 		.flags	= GENL_UNS_ADMIN_PERM,
904*4882a593Smuzhiyun 		.doit	= ethnl_act_cable_test_tdr,
905*4882a593Smuzhiyun 		.policy = ethnl_cable_test_tdr_act_policy,
906*4882a593Smuzhiyun 		.maxattr = ARRAY_SIZE(ethnl_cable_test_tdr_act_policy) - 1,
907*4882a593Smuzhiyun 	},
908*4882a593Smuzhiyun 	{
909*4882a593Smuzhiyun 		.cmd	= ETHTOOL_MSG_TUNNEL_INFO_GET,
910*4882a593Smuzhiyun 		.doit	= ethnl_tunnel_info_doit,
911*4882a593Smuzhiyun 		.start	= ethnl_tunnel_info_start,
912*4882a593Smuzhiyun 		.dumpit	= ethnl_tunnel_info_dumpit,
913*4882a593Smuzhiyun 		.policy = ethnl_tunnel_info_get_policy,
914*4882a593Smuzhiyun 		.maxattr = ARRAY_SIZE(ethnl_tunnel_info_get_policy) - 1,
915*4882a593Smuzhiyun 	},
916*4882a593Smuzhiyun };
917*4882a593Smuzhiyun 
918*4882a593Smuzhiyun static const struct genl_multicast_group ethtool_nl_mcgrps[] = {
919*4882a593Smuzhiyun 	[ETHNL_MCGRP_MONITOR] = { .name = ETHTOOL_MCGRP_MONITOR_NAME },
920*4882a593Smuzhiyun };
921*4882a593Smuzhiyun 
922*4882a593Smuzhiyun static struct genl_family ethtool_genl_family __ro_after_init = {
923*4882a593Smuzhiyun 	.name		= ETHTOOL_GENL_NAME,
924*4882a593Smuzhiyun 	.version	= ETHTOOL_GENL_VERSION,
925*4882a593Smuzhiyun 	.netnsok	= true,
926*4882a593Smuzhiyun 	.parallel_ops	= true,
927*4882a593Smuzhiyun 	.ops		= ethtool_genl_ops,
928*4882a593Smuzhiyun 	.n_ops		= ARRAY_SIZE(ethtool_genl_ops),
929*4882a593Smuzhiyun 	.mcgrps		= ethtool_nl_mcgrps,
930*4882a593Smuzhiyun 	.n_mcgrps	= ARRAY_SIZE(ethtool_nl_mcgrps),
931*4882a593Smuzhiyun };
932*4882a593Smuzhiyun 
933*4882a593Smuzhiyun /* module setup */
934*4882a593Smuzhiyun 
ethnl_init(void)935*4882a593Smuzhiyun static int __init ethnl_init(void)
936*4882a593Smuzhiyun {
937*4882a593Smuzhiyun 	int ret;
938*4882a593Smuzhiyun 
939*4882a593Smuzhiyun 	ret = genl_register_family(&ethtool_genl_family);
940*4882a593Smuzhiyun 	if (WARN(ret < 0, "ethtool: genetlink family registration failed"))
941*4882a593Smuzhiyun 		return ret;
942*4882a593Smuzhiyun 	ethnl_ok = true;
943*4882a593Smuzhiyun 
944*4882a593Smuzhiyun 	ret = register_netdevice_notifier(&ethnl_netdev_notifier);
945*4882a593Smuzhiyun 	WARN(ret < 0, "ethtool: net device notifier registration failed");
946*4882a593Smuzhiyun 	return ret;
947*4882a593Smuzhiyun }
948*4882a593Smuzhiyun 
949*4882a593Smuzhiyun subsys_initcall(ethnl_init);
950