netlink.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <net/sock.h>
  3. #include <linux/ethtool_netlink.h>
  4. #include <linux/phy_link_topology.h>
  5. #include <linux/pm_runtime.h>
  6. #include "netlink.h"
  7. #include "module_fw.h"
  8. static struct genl_family ethtool_genl_family;
  9. static bool ethnl_ok __read_mostly;
  10. static u32 ethnl_bcast_seq;
  11. #define ETHTOOL_FLAGS_BASIC (ETHTOOL_FLAG_COMPACT_BITSETS | \
  12. ETHTOOL_FLAG_OMIT_REPLY)
  13. #define ETHTOOL_FLAGS_STATS (ETHTOOL_FLAGS_BASIC | ETHTOOL_FLAG_STATS)
  14. const struct nla_policy ethnl_header_policy[] = {
  15. [ETHTOOL_A_HEADER_DEV_INDEX] = { .type = NLA_U32 },
  16. [ETHTOOL_A_HEADER_DEV_NAME] = { .type = NLA_NUL_STRING,
  17. .len = ALTIFNAMSIZ - 1 },
  18. [ETHTOOL_A_HEADER_FLAGS] = NLA_POLICY_MASK(NLA_U32,
  19. ETHTOOL_FLAGS_BASIC),
  20. };
  21. const struct nla_policy ethnl_header_policy_stats[] = {
  22. [ETHTOOL_A_HEADER_DEV_INDEX] = { .type = NLA_U32 },
  23. [ETHTOOL_A_HEADER_DEV_NAME] = { .type = NLA_NUL_STRING,
  24. .len = ALTIFNAMSIZ - 1 },
  25. [ETHTOOL_A_HEADER_FLAGS] = NLA_POLICY_MASK(NLA_U32,
  26. ETHTOOL_FLAGS_STATS),
  27. };
  28. const struct nla_policy ethnl_header_policy_phy[] = {
  29. [ETHTOOL_A_HEADER_DEV_INDEX] = { .type = NLA_U32 },
  30. [ETHTOOL_A_HEADER_DEV_NAME] = { .type = NLA_NUL_STRING,
  31. .len = ALTIFNAMSIZ - 1 },
  32. [ETHTOOL_A_HEADER_FLAGS] = NLA_POLICY_MASK(NLA_U32,
  33. ETHTOOL_FLAGS_BASIC),
  34. [ETHTOOL_A_HEADER_PHY_INDEX] = NLA_POLICY_MIN(NLA_U32, 1),
  35. };
  36. const struct nla_policy ethnl_header_policy_phy_stats[] = {
  37. [ETHTOOL_A_HEADER_DEV_INDEX] = { .type = NLA_U32 },
  38. [ETHTOOL_A_HEADER_DEV_NAME] = { .type = NLA_NUL_STRING,
  39. .len = ALTIFNAMSIZ - 1 },
  40. [ETHTOOL_A_HEADER_FLAGS] = NLA_POLICY_MASK(NLA_U32,
  41. ETHTOOL_FLAGS_STATS),
  42. [ETHTOOL_A_HEADER_PHY_INDEX] = NLA_POLICY_MIN(NLA_U32, 1),
  43. };
  44. int ethnl_sock_priv_set(struct sk_buff *skb, struct net_device *dev, u32 portid,
  45. enum ethnl_sock_type type)
  46. {
  47. struct ethnl_sock_priv *sk_priv;
  48. sk_priv = genl_sk_priv_get(&ethtool_genl_family, NETLINK_CB(skb).sk);
  49. if (IS_ERR(sk_priv))
  50. return PTR_ERR(sk_priv);
  51. sk_priv->dev = dev;
  52. sk_priv->portid = portid;
  53. sk_priv->type = type;
  54. return 0;
  55. }
  56. static void ethnl_sock_priv_destroy(void *priv)
  57. {
  58. struct ethnl_sock_priv *sk_priv = priv;
  59. switch (sk_priv->type) {
  60. case ETHTOOL_SOCK_TYPE_MODULE_FW_FLASH:
  61. ethnl_module_fw_flash_sock_destroy(sk_priv);
  62. break;
  63. default:
  64. break;
  65. }
  66. }
  67. int ethnl_ops_begin(struct net_device *dev)
  68. {
  69. int ret;
  70. if (!dev)
  71. return -ENODEV;
  72. if (dev->dev.parent)
  73. pm_runtime_get_sync(dev->dev.parent);
  74. if (!netif_device_present(dev) ||
  75. dev->reg_state >= NETREG_UNREGISTERING) {
  76. ret = -ENODEV;
  77. goto err;
  78. }
  79. if (dev->ethtool_ops->begin) {
  80. ret = dev->ethtool_ops->begin(dev);
  81. if (ret)
  82. goto err;
  83. }
  84. return 0;
  85. err:
  86. if (dev->dev.parent)
  87. pm_runtime_put(dev->dev.parent);
  88. return ret;
  89. }
  90. void ethnl_ops_complete(struct net_device *dev)
  91. {
  92. if (dev->ethtool_ops->complete)
  93. dev->ethtool_ops->complete(dev);
  94. if (dev->dev.parent)
  95. pm_runtime_put(dev->dev.parent);
  96. }
  97. /**
  98. * ethnl_parse_header_dev_get() - parse request header
  99. * @req_info: structure to put results into
  100. * @header: nest attribute with request header
  101. * @net: request netns
  102. * @extack: netlink extack for error reporting
  103. * @require_dev: fail if no device identified in header
  104. *
  105. * Parse request header in nested attribute @nest and puts results into
  106. * the structure pointed to by @req_info. Extack from @info is used for error
  107. * reporting. If req_info->dev is not null on return, reference to it has
  108. * been taken. If error is returned, *req_info is null initialized and no
  109. * reference is held.
  110. *
  111. * Return: 0 on success or negative error code
  112. */
  113. int ethnl_parse_header_dev_get(struct ethnl_req_info *req_info,
  114. const struct nlattr *header, struct net *net,
  115. struct netlink_ext_ack *extack, bool require_dev)
  116. {
  117. struct nlattr *tb[ARRAY_SIZE(ethnl_header_policy_phy)];
  118. const struct nlattr *devname_attr;
  119. struct net_device *dev = NULL;
  120. u32 flags = 0;
  121. int ret;
  122. if (!header) {
  123. if (!require_dev)
  124. return 0;
  125. NL_SET_ERR_MSG(extack, "request header missing");
  126. return -EINVAL;
  127. }
  128. /* No validation here, command policy should have a nested policy set
  129. * for the header, therefore validation should have already been done.
  130. */
  131. ret = nla_parse_nested(tb, ARRAY_SIZE(ethnl_header_policy_phy) - 1, header,
  132. NULL, extack);
  133. if (ret < 0)
  134. return ret;
  135. if (tb[ETHTOOL_A_HEADER_FLAGS])
  136. flags = nla_get_u32(tb[ETHTOOL_A_HEADER_FLAGS]);
  137. devname_attr = tb[ETHTOOL_A_HEADER_DEV_NAME];
  138. if (tb[ETHTOOL_A_HEADER_DEV_INDEX]) {
  139. u32 ifindex = nla_get_u32(tb[ETHTOOL_A_HEADER_DEV_INDEX]);
  140. dev = netdev_get_by_index(net, ifindex, &req_info->dev_tracker,
  141. GFP_KERNEL);
  142. if (!dev) {
  143. NL_SET_ERR_MSG_ATTR(extack,
  144. tb[ETHTOOL_A_HEADER_DEV_INDEX],
  145. "no device matches ifindex");
  146. return -ENODEV;
  147. }
  148. /* if both ifindex and ifname are passed, they must match */
  149. if (devname_attr &&
  150. strncmp(dev->name, nla_data(devname_attr), IFNAMSIZ)) {
  151. netdev_put(dev, &req_info->dev_tracker);
  152. NL_SET_ERR_MSG_ATTR(extack, header,
  153. "ifindex and name do not match");
  154. return -ENODEV;
  155. }
  156. } else if (devname_attr) {
  157. dev = netdev_get_by_name(net, nla_data(devname_attr),
  158. &req_info->dev_tracker, GFP_KERNEL);
  159. if (!dev) {
  160. NL_SET_ERR_MSG_ATTR(extack, devname_attr,
  161. "no device matches name");
  162. return -ENODEV;
  163. }
  164. } else if (require_dev) {
  165. NL_SET_ERR_MSG_ATTR(extack, header,
  166. "neither ifindex nor name specified");
  167. return -EINVAL;
  168. }
  169. if (tb[ETHTOOL_A_HEADER_PHY_INDEX]) {
  170. if (dev) {
  171. req_info->phy_index = nla_get_u32(tb[ETHTOOL_A_HEADER_PHY_INDEX]);
  172. } else {
  173. NL_SET_ERR_MSG_ATTR(extack, header,
  174. "phy_index set without a netdev");
  175. return -EINVAL;
  176. }
  177. }
  178. req_info->dev = dev;
  179. req_info->flags = flags;
  180. return 0;
  181. }
  182. struct phy_device *ethnl_req_get_phydev(const struct ethnl_req_info *req_info,
  183. struct nlattr **tb, unsigned int header,
  184. struct netlink_ext_ack *extack)
  185. {
  186. struct phy_device *phydev;
  187. ASSERT_RTNL();
  188. if (!req_info->dev)
  189. return NULL;
  190. if (!req_info->phy_index)
  191. return req_info->dev->phydev;
  192. phydev = phy_link_topo_get_phy(req_info->dev, req_info->phy_index);
  193. if (!phydev && tb) {
  194. NL_SET_ERR_MSG_ATTR(extack, tb[header],
  195. "no phy matching phyindex");
  196. return ERR_PTR(-ENODEV);
  197. }
  198. return phydev;
  199. }
  200. /**
  201. * ethnl_fill_reply_header() - Put common header into a reply message
  202. * @skb: skb with the message
  203. * @dev: network device to describe in header
  204. * @attrtype: attribute type to use for the nest
  205. *
  206. * Create a nested attribute with attributes describing given network device.
  207. *
  208. * Return: 0 on success, error value (-EMSGSIZE only) on error
  209. */
  210. int ethnl_fill_reply_header(struct sk_buff *skb, struct net_device *dev,
  211. u16 attrtype)
  212. {
  213. struct nlattr *nest;
  214. if (!dev)
  215. return 0;
  216. nest = nla_nest_start(skb, attrtype);
  217. if (!nest)
  218. return -EMSGSIZE;
  219. if (nla_put_u32(skb, ETHTOOL_A_HEADER_DEV_INDEX, (u32)dev->ifindex) ||
  220. nla_put_string(skb, ETHTOOL_A_HEADER_DEV_NAME, dev->name))
  221. goto nla_put_failure;
  222. /* If more attributes are put into reply header, ethnl_header_size()
  223. * must be updated to account for them.
  224. */
  225. nla_nest_end(skb, nest);
  226. return 0;
  227. nla_put_failure:
  228. nla_nest_cancel(skb, nest);
  229. return -EMSGSIZE;
  230. }
  231. /**
  232. * ethnl_reply_init() - Create skb for a reply and fill device identification
  233. * @payload: payload length (without netlink and genetlink header)
  234. * @dev: device the reply is about (may be null)
  235. * @cmd: ETHTOOL_MSG_* message type for reply
  236. * @hdr_attrtype: attribute type for common header
  237. * @info: genetlink info of the received packet we respond to
  238. * @ehdrp: place to store payload pointer returned by genlmsg_new()
  239. *
  240. * Return: pointer to allocated skb on success, NULL on error
  241. */
  242. struct sk_buff *ethnl_reply_init(size_t payload, struct net_device *dev, u8 cmd,
  243. u16 hdr_attrtype, struct genl_info *info,
  244. void **ehdrp)
  245. {
  246. struct sk_buff *skb;
  247. skb = genlmsg_new(payload, GFP_KERNEL);
  248. if (!skb)
  249. goto err;
  250. *ehdrp = genlmsg_put_reply(skb, info, &ethtool_genl_family, 0, cmd);
  251. if (!*ehdrp)
  252. goto err_free;
  253. if (dev) {
  254. int ret;
  255. ret = ethnl_fill_reply_header(skb, dev, hdr_attrtype);
  256. if (ret < 0)
  257. goto err_free;
  258. }
  259. return skb;
  260. err_free:
  261. nlmsg_free(skb);
  262. err:
  263. if (info)
  264. GENL_SET_ERR_MSG(info, "failed to setup reply message");
  265. return NULL;
  266. }
  267. void *ethnl_dump_put(struct sk_buff *skb, struct netlink_callback *cb, u8 cmd)
  268. {
  269. return genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
  270. &ethtool_genl_family, 0, cmd);
  271. }
  272. void *ethnl_bcastmsg_put(struct sk_buff *skb, u8 cmd)
  273. {
  274. return genlmsg_put(skb, 0, ++ethnl_bcast_seq, &ethtool_genl_family, 0,
  275. cmd);
  276. }
  277. void *ethnl_unicast_put(struct sk_buff *skb, u32 portid, u32 seq, u8 cmd)
  278. {
  279. return genlmsg_put(skb, portid, seq, &ethtool_genl_family, 0, cmd);
  280. }
  281. int ethnl_multicast(struct sk_buff *skb, struct net_device *dev)
  282. {
  283. return genlmsg_multicast_netns(&ethtool_genl_family, dev_net(dev), skb,
  284. 0, ETHNL_MCGRP_MONITOR, GFP_KERNEL);
  285. }
  286. /* GET request helpers */
  287. /**
  288. * struct ethnl_dump_ctx - context structure for generic dumpit() callback
  289. * @ops: request ops of currently processed message type
  290. * @req_info: parsed request header of processed request
  291. * @reply_data: data needed to compose the reply
  292. * @pos_ifindex: saved iteration position - ifindex
  293. *
  294. * These parameters are kept in struct netlink_callback as context preserved
  295. * between iterations. They are initialized by ethnl_default_start() and used
  296. * in ethnl_default_dumpit() and ethnl_default_done().
  297. */
  298. struct ethnl_dump_ctx {
  299. const struct ethnl_request_ops *ops;
  300. struct ethnl_req_info *req_info;
  301. struct ethnl_reply_data *reply_data;
  302. unsigned long pos_ifindex;
  303. };
  304. static const struct ethnl_request_ops *
  305. ethnl_default_requests[__ETHTOOL_MSG_USER_CNT] = {
  306. [ETHTOOL_MSG_STRSET_GET] = &ethnl_strset_request_ops,
  307. [ETHTOOL_MSG_LINKINFO_GET] = &ethnl_linkinfo_request_ops,
  308. [ETHTOOL_MSG_LINKINFO_SET] = &ethnl_linkinfo_request_ops,
  309. [ETHTOOL_MSG_LINKMODES_GET] = &ethnl_linkmodes_request_ops,
  310. [ETHTOOL_MSG_LINKMODES_SET] = &ethnl_linkmodes_request_ops,
  311. [ETHTOOL_MSG_LINKSTATE_GET] = &ethnl_linkstate_request_ops,
  312. [ETHTOOL_MSG_DEBUG_GET] = &ethnl_debug_request_ops,
  313. [ETHTOOL_MSG_DEBUG_SET] = &ethnl_debug_request_ops,
  314. [ETHTOOL_MSG_WOL_GET] = &ethnl_wol_request_ops,
  315. [ETHTOOL_MSG_WOL_SET] = &ethnl_wol_request_ops,
  316. [ETHTOOL_MSG_FEATURES_GET] = &ethnl_features_request_ops,
  317. [ETHTOOL_MSG_PRIVFLAGS_GET] = &ethnl_privflags_request_ops,
  318. [ETHTOOL_MSG_PRIVFLAGS_SET] = &ethnl_privflags_request_ops,
  319. [ETHTOOL_MSG_RINGS_GET] = &ethnl_rings_request_ops,
  320. [ETHTOOL_MSG_RINGS_SET] = &ethnl_rings_request_ops,
  321. [ETHTOOL_MSG_CHANNELS_GET] = &ethnl_channels_request_ops,
  322. [ETHTOOL_MSG_CHANNELS_SET] = &ethnl_channels_request_ops,
  323. [ETHTOOL_MSG_COALESCE_GET] = &ethnl_coalesce_request_ops,
  324. [ETHTOOL_MSG_COALESCE_SET] = &ethnl_coalesce_request_ops,
  325. [ETHTOOL_MSG_PAUSE_GET] = &ethnl_pause_request_ops,
  326. [ETHTOOL_MSG_PAUSE_SET] = &ethnl_pause_request_ops,
  327. [ETHTOOL_MSG_EEE_GET] = &ethnl_eee_request_ops,
  328. [ETHTOOL_MSG_EEE_SET] = &ethnl_eee_request_ops,
  329. [ETHTOOL_MSG_FEC_GET] = &ethnl_fec_request_ops,
  330. [ETHTOOL_MSG_FEC_SET] = &ethnl_fec_request_ops,
  331. [ETHTOOL_MSG_TSINFO_GET] = &ethnl_tsinfo_request_ops,
  332. [ETHTOOL_MSG_MODULE_EEPROM_GET] = &ethnl_module_eeprom_request_ops,
  333. [ETHTOOL_MSG_STATS_GET] = &ethnl_stats_request_ops,
  334. [ETHTOOL_MSG_PHC_VCLOCKS_GET] = &ethnl_phc_vclocks_request_ops,
  335. [ETHTOOL_MSG_MODULE_GET] = &ethnl_module_request_ops,
  336. [ETHTOOL_MSG_MODULE_SET] = &ethnl_module_request_ops,
  337. [ETHTOOL_MSG_PSE_GET] = &ethnl_pse_request_ops,
  338. [ETHTOOL_MSG_PSE_SET] = &ethnl_pse_request_ops,
  339. [ETHTOOL_MSG_RSS_GET] = &ethnl_rss_request_ops,
  340. [ETHTOOL_MSG_PLCA_GET_CFG] = &ethnl_plca_cfg_request_ops,
  341. [ETHTOOL_MSG_PLCA_SET_CFG] = &ethnl_plca_cfg_request_ops,
  342. [ETHTOOL_MSG_PLCA_GET_STATUS] = &ethnl_plca_status_request_ops,
  343. [ETHTOOL_MSG_MM_GET] = &ethnl_mm_request_ops,
  344. [ETHTOOL_MSG_MM_SET] = &ethnl_mm_request_ops,
  345. };
  346. static struct ethnl_dump_ctx *ethnl_dump_context(struct netlink_callback *cb)
  347. {
  348. return (struct ethnl_dump_ctx *)cb->ctx;
  349. }
  350. /**
  351. * ethnl_default_parse() - Parse request message
  352. * @req_info: pointer to structure to put data into
  353. * @info: genl_info from the request
  354. * @request_ops: struct request_ops for request type
  355. * @require_dev: fail if no device identified in header
  356. *
  357. * Parse universal request header and call request specific ->parse_request()
  358. * callback (if defined) to parse the rest of the message.
  359. *
  360. * Return: 0 on success or negative error code
  361. */
  362. static int ethnl_default_parse(struct ethnl_req_info *req_info,
  363. const struct genl_info *info,
  364. const struct ethnl_request_ops *request_ops,
  365. bool require_dev)
  366. {
  367. struct nlattr **tb = info->attrs;
  368. int ret;
  369. ret = ethnl_parse_header_dev_get(req_info, tb[request_ops->hdr_attr],
  370. genl_info_net(info), info->extack,
  371. require_dev);
  372. if (ret < 0)
  373. return ret;
  374. if (request_ops->parse_request) {
  375. ret = request_ops->parse_request(req_info, tb, info->extack);
  376. if (ret < 0)
  377. return ret;
  378. }
  379. return 0;
  380. }
  381. /**
  382. * ethnl_init_reply_data() - Initialize reply data for GET request
  383. * @reply_data: pointer to embedded struct ethnl_reply_data
  384. * @ops: instance of struct ethnl_request_ops describing the layout
  385. * @dev: network device to initialize the reply for
  386. *
  387. * Fills the reply data part with zeros and sets the dev member. Must be called
  388. * before calling the ->fill_reply() callback (for each iteration when handling
  389. * dump requests).
  390. */
  391. static void ethnl_init_reply_data(struct ethnl_reply_data *reply_data,
  392. const struct ethnl_request_ops *ops,
  393. struct net_device *dev)
  394. {
  395. memset(reply_data, 0, ops->reply_data_size);
  396. reply_data->dev = dev;
  397. }
  398. /* default ->doit() handler for GET type requests */
  399. static int ethnl_default_doit(struct sk_buff *skb, struct genl_info *info)
  400. {
  401. struct ethnl_reply_data *reply_data = NULL;
  402. struct ethnl_req_info *req_info = NULL;
  403. const u8 cmd = info->genlhdr->cmd;
  404. const struct ethnl_request_ops *ops;
  405. int hdr_len, reply_len;
  406. struct sk_buff *rskb;
  407. void *reply_payload;
  408. int ret;
  409. ops = ethnl_default_requests[cmd];
  410. if (WARN_ONCE(!ops, "cmd %u has no ethnl_request_ops\n", cmd))
  411. return -EOPNOTSUPP;
  412. if (GENL_REQ_ATTR_CHECK(info, ops->hdr_attr))
  413. return -EINVAL;
  414. req_info = kzalloc(ops->req_info_size, GFP_KERNEL);
  415. if (!req_info)
  416. return -ENOMEM;
  417. reply_data = kmalloc(ops->reply_data_size, GFP_KERNEL);
  418. if (!reply_data) {
  419. kfree(req_info);
  420. return -ENOMEM;
  421. }
  422. ret = ethnl_default_parse(req_info, info, ops, !ops->allow_nodev_do);
  423. if (ret < 0)
  424. goto err_dev;
  425. ethnl_init_reply_data(reply_data, ops, req_info->dev);
  426. rtnl_lock();
  427. ret = ops->prepare_data(req_info, reply_data, info);
  428. rtnl_unlock();
  429. if (ret < 0)
  430. goto err_dev;
  431. ret = ops->reply_size(req_info, reply_data);
  432. if (ret < 0)
  433. goto err_cleanup;
  434. reply_len = ret;
  435. ret = -ENOMEM;
  436. rskb = ethnl_reply_init(reply_len + ethnl_reply_header_size(),
  437. req_info->dev, ops->reply_cmd,
  438. ops->hdr_attr, info, &reply_payload);
  439. if (!rskb)
  440. goto err_cleanup;
  441. hdr_len = rskb->len;
  442. ret = ops->fill_reply(rskb, req_info, reply_data);
  443. if (ret < 0)
  444. goto err_msg;
  445. WARN_ONCE(rskb->len - hdr_len > reply_len,
  446. "ethnl cmd %d: calculated reply length %d, but consumed %d\n",
  447. cmd, reply_len, rskb->len - hdr_len);
  448. if (ops->cleanup_data)
  449. ops->cleanup_data(reply_data);
  450. genlmsg_end(rskb, reply_payload);
  451. netdev_put(req_info->dev, &req_info->dev_tracker);
  452. kfree(reply_data);
  453. kfree(req_info);
  454. return genlmsg_reply(rskb, info);
  455. err_msg:
  456. WARN_ONCE(ret == -EMSGSIZE, "calculated message payload length (%d) not sufficient\n", reply_len);
  457. nlmsg_free(rskb);
  458. err_cleanup:
  459. if (ops->cleanup_data)
  460. ops->cleanup_data(reply_data);
  461. err_dev:
  462. netdev_put(req_info->dev, &req_info->dev_tracker);
  463. kfree(reply_data);
  464. kfree(req_info);
  465. return ret;
  466. }
  467. static int ethnl_default_dump_one(struct sk_buff *skb, struct net_device *dev,
  468. const struct ethnl_dump_ctx *ctx,
  469. const struct genl_info *info)
  470. {
  471. void *ehdr;
  472. int ret;
  473. ehdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
  474. &ethtool_genl_family, NLM_F_MULTI,
  475. ctx->ops->reply_cmd);
  476. if (!ehdr)
  477. return -EMSGSIZE;
  478. ethnl_init_reply_data(ctx->reply_data, ctx->ops, dev);
  479. rtnl_lock();
  480. ret = ctx->ops->prepare_data(ctx->req_info, ctx->reply_data, info);
  481. rtnl_unlock();
  482. if (ret < 0)
  483. goto out_cancel;
  484. ret = ethnl_fill_reply_header(skb, dev, ctx->ops->hdr_attr);
  485. if (ret < 0)
  486. goto out;
  487. ret = ctx->ops->fill_reply(skb, ctx->req_info, ctx->reply_data);
  488. out:
  489. if (ctx->ops->cleanup_data)
  490. ctx->ops->cleanup_data(ctx->reply_data);
  491. out_cancel:
  492. ctx->reply_data->dev = NULL;
  493. if (ret < 0)
  494. genlmsg_cancel(skb, ehdr);
  495. else
  496. genlmsg_end(skb, ehdr);
  497. return ret;
  498. }
  499. /* Default ->dumpit() handler for GET requests. */
  500. static int ethnl_default_dumpit(struct sk_buff *skb,
  501. struct netlink_callback *cb)
  502. {
  503. struct ethnl_dump_ctx *ctx = ethnl_dump_context(cb);
  504. struct net *net = sock_net(skb->sk);
  505. struct net_device *dev;
  506. int ret = 0;
  507. rcu_read_lock();
  508. for_each_netdev_dump(net, dev, ctx->pos_ifindex) {
  509. dev_hold(dev);
  510. rcu_read_unlock();
  511. ret = ethnl_default_dump_one(skb, dev, ctx, genl_info_dump(cb));
  512. rcu_read_lock();
  513. dev_put(dev);
  514. if (ret < 0 && ret != -EOPNOTSUPP) {
  515. if (likely(skb->len))
  516. ret = skb->len;
  517. break;
  518. }
  519. ret = 0;
  520. }
  521. rcu_read_unlock();
  522. return ret;
  523. }
  524. /* generic ->start() handler for GET requests */
  525. static int ethnl_default_start(struct netlink_callback *cb)
  526. {
  527. const struct genl_dumpit_info *info = genl_dumpit_info(cb);
  528. struct ethnl_dump_ctx *ctx = ethnl_dump_context(cb);
  529. struct ethnl_reply_data *reply_data;
  530. const struct ethnl_request_ops *ops;
  531. struct ethnl_req_info *req_info;
  532. struct genlmsghdr *ghdr;
  533. int ret;
  534. BUILD_BUG_ON(sizeof(*ctx) > sizeof(cb->ctx));
  535. ghdr = nlmsg_data(cb->nlh);
  536. ops = ethnl_default_requests[ghdr->cmd];
  537. if (WARN_ONCE(!ops, "cmd %u has no ethnl_request_ops\n", ghdr->cmd))
  538. return -EOPNOTSUPP;
  539. req_info = kzalloc(ops->req_info_size, GFP_KERNEL);
  540. if (!req_info)
  541. return -ENOMEM;
  542. reply_data = kmalloc(ops->reply_data_size, GFP_KERNEL);
  543. if (!reply_data) {
  544. ret = -ENOMEM;
  545. goto free_req_info;
  546. }
  547. ret = ethnl_default_parse(req_info, &info->info, ops, false);
  548. if (req_info->dev) {
  549. /* We ignore device specification in dump requests but as the
  550. * same parser as for non-dump (doit) requests is used, it
  551. * would take reference to the device if it finds one
  552. */
  553. netdev_put(req_info->dev, &req_info->dev_tracker);
  554. req_info->dev = NULL;
  555. }
  556. if (ret < 0)
  557. goto free_reply_data;
  558. ctx->ops = ops;
  559. ctx->req_info = req_info;
  560. ctx->reply_data = reply_data;
  561. ctx->pos_ifindex = 0;
  562. return 0;
  563. free_reply_data:
  564. kfree(reply_data);
  565. free_req_info:
  566. kfree(req_info);
  567. return ret;
  568. }
  569. /* default ->done() handler for GET requests */
  570. static int ethnl_default_done(struct netlink_callback *cb)
  571. {
  572. struct ethnl_dump_ctx *ctx = ethnl_dump_context(cb);
  573. kfree(ctx->reply_data);
  574. kfree(ctx->req_info);
  575. return 0;
  576. }
  577. static int ethnl_default_set_doit(struct sk_buff *skb, struct genl_info *info)
  578. {
  579. const struct ethnl_request_ops *ops;
  580. struct ethnl_req_info req_info = {};
  581. const u8 cmd = info->genlhdr->cmd;
  582. int ret;
  583. ops = ethnl_default_requests[cmd];
  584. if (WARN_ONCE(!ops, "cmd %u has no ethnl_request_ops\n", cmd))
  585. return -EOPNOTSUPP;
  586. if (GENL_REQ_ATTR_CHECK(info, ops->hdr_attr))
  587. return -EINVAL;
  588. ret = ethnl_parse_header_dev_get(&req_info, info->attrs[ops->hdr_attr],
  589. genl_info_net(info), info->extack,
  590. true);
  591. if (ret < 0)
  592. return ret;
  593. if (ops->set_validate) {
  594. ret = ops->set_validate(&req_info, info);
  595. /* 0 means nothing to do */
  596. if (ret <= 0)
  597. goto out_dev;
  598. }
  599. rtnl_lock();
  600. ret = ethnl_ops_begin(req_info.dev);
  601. if (ret < 0)
  602. goto out_rtnl;
  603. ret = ops->set(&req_info, info);
  604. if (ret <= 0)
  605. goto out_ops;
  606. ethtool_notify(req_info.dev, ops->set_ntf_cmd, NULL);
  607. ret = 0;
  608. out_ops:
  609. ethnl_ops_complete(req_info.dev);
  610. out_rtnl:
  611. rtnl_unlock();
  612. out_dev:
  613. ethnl_parse_header_dev_put(&req_info);
  614. return ret;
  615. }
  616. static const struct ethnl_request_ops *
  617. ethnl_default_notify_ops[ETHTOOL_MSG_KERNEL_MAX + 1] = {
  618. [ETHTOOL_MSG_LINKINFO_NTF] = &ethnl_linkinfo_request_ops,
  619. [ETHTOOL_MSG_LINKMODES_NTF] = &ethnl_linkmodes_request_ops,
  620. [ETHTOOL_MSG_DEBUG_NTF] = &ethnl_debug_request_ops,
  621. [ETHTOOL_MSG_WOL_NTF] = &ethnl_wol_request_ops,
  622. [ETHTOOL_MSG_FEATURES_NTF] = &ethnl_features_request_ops,
  623. [ETHTOOL_MSG_PRIVFLAGS_NTF] = &ethnl_privflags_request_ops,
  624. [ETHTOOL_MSG_RINGS_NTF] = &ethnl_rings_request_ops,
  625. [ETHTOOL_MSG_CHANNELS_NTF] = &ethnl_channels_request_ops,
  626. [ETHTOOL_MSG_COALESCE_NTF] = &ethnl_coalesce_request_ops,
  627. [ETHTOOL_MSG_PAUSE_NTF] = &ethnl_pause_request_ops,
  628. [ETHTOOL_MSG_EEE_NTF] = &ethnl_eee_request_ops,
  629. [ETHTOOL_MSG_FEC_NTF] = &ethnl_fec_request_ops,
  630. [ETHTOOL_MSG_MODULE_NTF] = &ethnl_module_request_ops,
  631. [ETHTOOL_MSG_PLCA_NTF] = &ethnl_plca_cfg_request_ops,
  632. [ETHTOOL_MSG_MM_NTF] = &ethnl_mm_request_ops,
  633. };
  634. /* default notification handler */
  635. static void ethnl_default_notify(struct net_device *dev, unsigned int cmd,
  636. const void *data)
  637. {
  638. struct ethnl_reply_data *reply_data;
  639. const struct ethnl_request_ops *ops;
  640. struct ethnl_req_info *req_info;
  641. struct genl_info info;
  642. struct sk_buff *skb;
  643. void *reply_payload;
  644. int reply_len;
  645. int ret;
  646. genl_info_init_ntf(&info, &ethtool_genl_family, cmd);
  647. if (WARN_ONCE(cmd > ETHTOOL_MSG_KERNEL_MAX ||
  648. !ethnl_default_notify_ops[cmd],
  649. "unexpected notification type %u\n", cmd))
  650. return;
  651. ops = ethnl_default_notify_ops[cmd];
  652. req_info = kzalloc(ops->req_info_size, GFP_KERNEL);
  653. if (!req_info)
  654. return;
  655. reply_data = kmalloc(ops->reply_data_size, GFP_KERNEL);
  656. if (!reply_data) {
  657. kfree(req_info);
  658. return;
  659. }
  660. req_info->dev = dev;
  661. req_info->flags |= ETHTOOL_FLAG_COMPACT_BITSETS;
  662. ethnl_init_reply_data(reply_data, ops, dev);
  663. ret = ops->prepare_data(req_info, reply_data, &info);
  664. if (ret < 0)
  665. goto err_rep;
  666. ret = ops->reply_size(req_info, reply_data);
  667. if (ret < 0)
  668. goto err_cleanup;
  669. reply_len = ret + ethnl_reply_header_size();
  670. skb = genlmsg_new(reply_len, GFP_KERNEL);
  671. if (!skb)
  672. goto err_cleanup;
  673. reply_payload = ethnl_bcastmsg_put(skb, cmd);
  674. if (!reply_payload)
  675. goto err_skb;
  676. ret = ethnl_fill_reply_header(skb, dev, ops->hdr_attr);
  677. if (ret < 0)
  678. goto err_msg;
  679. ret = ops->fill_reply(skb, req_info, reply_data);
  680. if (ret < 0)
  681. goto err_msg;
  682. if (ops->cleanup_data)
  683. ops->cleanup_data(reply_data);
  684. genlmsg_end(skb, reply_payload);
  685. kfree(reply_data);
  686. kfree(req_info);
  687. ethnl_multicast(skb, dev);
  688. return;
  689. err_msg:
  690. WARN_ONCE(ret == -EMSGSIZE,
  691. "calculated message payload length (%d) not sufficient\n",
  692. reply_len);
  693. err_skb:
  694. nlmsg_free(skb);
  695. err_cleanup:
  696. if (ops->cleanup_data)
  697. ops->cleanup_data(reply_data);
  698. err_rep:
  699. kfree(reply_data);
  700. kfree(req_info);
  701. return;
  702. }
  703. /* notifications */
  704. typedef void (*ethnl_notify_handler_t)(struct net_device *dev, unsigned int cmd,
  705. const void *data);
  706. static const ethnl_notify_handler_t ethnl_notify_handlers[] = {
  707. [ETHTOOL_MSG_LINKINFO_NTF] = ethnl_default_notify,
  708. [ETHTOOL_MSG_LINKMODES_NTF] = ethnl_default_notify,
  709. [ETHTOOL_MSG_DEBUG_NTF] = ethnl_default_notify,
  710. [ETHTOOL_MSG_WOL_NTF] = ethnl_default_notify,
  711. [ETHTOOL_MSG_FEATURES_NTF] = ethnl_default_notify,
  712. [ETHTOOL_MSG_PRIVFLAGS_NTF] = ethnl_default_notify,
  713. [ETHTOOL_MSG_RINGS_NTF] = ethnl_default_notify,
  714. [ETHTOOL_MSG_CHANNELS_NTF] = ethnl_default_notify,
  715. [ETHTOOL_MSG_COALESCE_NTF] = ethnl_default_notify,
  716. [ETHTOOL_MSG_PAUSE_NTF] = ethnl_default_notify,
  717. [ETHTOOL_MSG_EEE_NTF] = ethnl_default_notify,
  718. [ETHTOOL_MSG_FEC_NTF] = ethnl_default_notify,
  719. [ETHTOOL_MSG_MODULE_NTF] = ethnl_default_notify,
  720. [ETHTOOL_MSG_PLCA_NTF] = ethnl_default_notify,
  721. [ETHTOOL_MSG_MM_NTF] = ethnl_default_notify,
  722. };
  723. void ethtool_notify(struct net_device *dev, unsigned int cmd, const void *data)
  724. {
  725. if (unlikely(!ethnl_ok))
  726. return;
  727. ASSERT_RTNL();
  728. if (likely(cmd < ARRAY_SIZE(ethnl_notify_handlers) &&
  729. ethnl_notify_handlers[cmd]))
  730. ethnl_notify_handlers[cmd](dev, cmd, data);
  731. else
  732. WARN_ONCE(1, "notification %u not implemented (dev=%s)\n",
  733. cmd, netdev_name(dev));
  734. }
  735. EXPORT_SYMBOL(ethtool_notify);
  736. static void ethnl_notify_features(struct netdev_notifier_info *info)
  737. {
  738. struct net_device *dev = netdev_notifier_info_to_dev(info);
  739. ethtool_notify(dev, ETHTOOL_MSG_FEATURES_NTF, NULL);
  740. }
  741. static int ethnl_netdev_event(struct notifier_block *this, unsigned long event,
  742. void *ptr)
  743. {
  744. struct netdev_notifier_info *info = ptr;
  745. struct netlink_ext_ack *extack;
  746. struct net_device *dev;
  747. dev = netdev_notifier_info_to_dev(info);
  748. extack = netdev_notifier_info_to_extack(info);
  749. switch (event) {
  750. case NETDEV_FEAT_CHANGE:
  751. ethnl_notify_features(ptr);
  752. break;
  753. case NETDEV_PRE_UP:
  754. if (dev->ethtool->module_fw_flash_in_progress) {
  755. NL_SET_ERR_MSG(extack, "Can't set port up while flashing module firmware");
  756. return NOTIFY_BAD;
  757. }
  758. }
  759. return NOTIFY_DONE;
  760. }
  761. static struct notifier_block ethnl_netdev_notifier = {
  762. .notifier_call = ethnl_netdev_event,
  763. };
  764. /* genetlink setup */
  765. static const struct genl_ops ethtool_genl_ops[] = {
  766. {
  767. .cmd = ETHTOOL_MSG_STRSET_GET,
  768. .doit = ethnl_default_doit,
  769. .start = ethnl_default_start,
  770. .dumpit = ethnl_default_dumpit,
  771. .done = ethnl_default_done,
  772. .policy = ethnl_strset_get_policy,
  773. .maxattr = ARRAY_SIZE(ethnl_strset_get_policy) - 1,
  774. },
  775. {
  776. .cmd = ETHTOOL_MSG_LINKINFO_GET,
  777. .doit = ethnl_default_doit,
  778. .start = ethnl_default_start,
  779. .dumpit = ethnl_default_dumpit,
  780. .done = ethnl_default_done,
  781. .policy = ethnl_linkinfo_get_policy,
  782. .maxattr = ARRAY_SIZE(ethnl_linkinfo_get_policy) - 1,
  783. },
  784. {
  785. .cmd = ETHTOOL_MSG_LINKINFO_SET,
  786. .flags = GENL_UNS_ADMIN_PERM,
  787. .doit = ethnl_default_set_doit,
  788. .policy = ethnl_linkinfo_set_policy,
  789. .maxattr = ARRAY_SIZE(ethnl_linkinfo_set_policy) - 1,
  790. },
  791. {
  792. .cmd = ETHTOOL_MSG_LINKMODES_GET,
  793. .doit = ethnl_default_doit,
  794. .start = ethnl_default_start,
  795. .dumpit = ethnl_default_dumpit,
  796. .done = ethnl_default_done,
  797. .policy = ethnl_linkmodes_get_policy,
  798. .maxattr = ARRAY_SIZE(ethnl_linkmodes_get_policy) - 1,
  799. },
  800. {
  801. .cmd = ETHTOOL_MSG_LINKMODES_SET,
  802. .flags = GENL_UNS_ADMIN_PERM,
  803. .doit = ethnl_default_set_doit,
  804. .policy = ethnl_linkmodes_set_policy,
  805. .maxattr = ARRAY_SIZE(ethnl_linkmodes_set_policy) - 1,
  806. },
  807. {
  808. .cmd = ETHTOOL_MSG_LINKSTATE_GET,
  809. .doit = ethnl_default_doit,
  810. .start = ethnl_default_start,
  811. .dumpit = ethnl_default_dumpit,
  812. .done = ethnl_default_done,
  813. .policy = ethnl_linkstate_get_policy,
  814. .maxattr = ARRAY_SIZE(ethnl_linkstate_get_policy) - 1,
  815. },
  816. {
  817. .cmd = ETHTOOL_MSG_DEBUG_GET,
  818. .doit = ethnl_default_doit,
  819. .start = ethnl_default_start,
  820. .dumpit = ethnl_default_dumpit,
  821. .done = ethnl_default_done,
  822. .policy = ethnl_debug_get_policy,
  823. .maxattr = ARRAY_SIZE(ethnl_debug_get_policy) - 1,
  824. },
  825. {
  826. .cmd = ETHTOOL_MSG_DEBUG_SET,
  827. .flags = GENL_UNS_ADMIN_PERM,
  828. .doit = ethnl_default_set_doit,
  829. .policy = ethnl_debug_set_policy,
  830. .maxattr = ARRAY_SIZE(ethnl_debug_set_policy) - 1,
  831. },
  832. {
  833. .cmd = ETHTOOL_MSG_WOL_GET,
  834. .flags = GENL_UNS_ADMIN_PERM,
  835. .doit = ethnl_default_doit,
  836. .start = ethnl_default_start,
  837. .dumpit = ethnl_default_dumpit,
  838. .done = ethnl_default_done,
  839. .policy = ethnl_wol_get_policy,
  840. .maxattr = ARRAY_SIZE(ethnl_wol_get_policy) - 1,
  841. },
  842. {
  843. .cmd = ETHTOOL_MSG_WOL_SET,
  844. .flags = GENL_UNS_ADMIN_PERM,
  845. .doit = ethnl_default_set_doit,
  846. .policy = ethnl_wol_set_policy,
  847. .maxattr = ARRAY_SIZE(ethnl_wol_set_policy) - 1,
  848. },
  849. {
  850. .cmd = ETHTOOL_MSG_FEATURES_GET,
  851. .doit = ethnl_default_doit,
  852. .start = ethnl_default_start,
  853. .dumpit = ethnl_default_dumpit,
  854. .done = ethnl_default_done,
  855. .policy = ethnl_features_get_policy,
  856. .maxattr = ARRAY_SIZE(ethnl_features_get_policy) - 1,
  857. },
  858. {
  859. .cmd = ETHTOOL_MSG_FEATURES_SET,
  860. .flags = GENL_UNS_ADMIN_PERM,
  861. .doit = ethnl_set_features,
  862. .policy = ethnl_features_set_policy,
  863. .maxattr = ARRAY_SIZE(ethnl_features_set_policy) - 1,
  864. },
  865. {
  866. .cmd = ETHTOOL_MSG_PRIVFLAGS_GET,
  867. .doit = ethnl_default_doit,
  868. .start = ethnl_default_start,
  869. .dumpit = ethnl_default_dumpit,
  870. .done = ethnl_default_done,
  871. .policy = ethnl_privflags_get_policy,
  872. .maxattr = ARRAY_SIZE(ethnl_privflags_get_policy) - 1,
  873. },
  874. {
  875. .cmd = ETHTOOL_MSG_PRIVFLAGS_SET,
  876. .flags = GENL_UNS_ADMIN_PERM,
  877. .doit = ethnl_default_set_doit,
  878. .policy = ethnl_privflags_set_policy,
  879. .maxattr = ARRAY_SIZE(ethnl_privflags_set_policy) - 1,
  880. },
  881. {
  882. .cmd = ETHTOOL_MSG_RINGS_GET,
  883. .doit = ethnl_default_doit,
  884. .start = ethnl_default_start,
  885. .dumpit = ethnl_default_dumpit,
  886. .done = ethnl_default_done,
  887. .policy = ethnl_rings_get_policy,
  888. .maxattr = ARRAY_SIZE(ethnl_rings_get_policy) - 1,
  889. },
  890. {
  891. .cmd = ETHTOOL_MSG_RINGS_SET,
  892. .flags = GENL_UNS_ADMIN_PERM,
  893. .doit = ethnl_default_set_doit,
  894. .policy = ethnl_rings_set_policy,
  895. .maxattr = ARRAY_SIZE(ethnl_rings_set_policy) - 1,
  896. },
  897. {
  898. .cmd = ETHTOOL_MSG_CHANNELS_GET,
  899. .doit = ethnl_default_doit,
  900. .start = ethnl_default_start,
  901. .dumpit = ethnl_default_dumpit,
  902. .done = ethnl_default_done,
  903. .policy = ethnl_channels_get_policy,
  904. .maxattr = ARRAY_SIZE(ethnl_channels_get_policy) - 1,
  905. },
  906. {
  907. .cmd = ETHTOOL_MSG_CHANNELS_SET,
  908. .flags = GENL_UNS_ADMIN_PERM,
  909. .doit = ethnl_default_set_doit,
  910. .policy = ethnl_channels_set_policy,
  911. .maxattr = ARRAY_SIZE(ethnl_channels_set_policy) - 1,
  912. },
  913. {
  914. .cmd = ETHTOOL_MSG_COALESCE_GET,
  915. .doit = ethnl_default_doit,
  916. .start = ethnl_default_start,
  917. .dumpit = ethnl_default_dumpit,
  918. .done = ethnl_default_done,
  919. .policy = ethnl_coalesce_get_policy,
  920. .maxattr = ARRAY_SIZE(ethnl_coalesce_get_policy) - 1,
  921. },
  922. {
  923. .cmd = ETHTOOL_MSG_COALESCE_SET,
  924. .flags = GENL_UNS_ADMIN_PERM,
  925. .doit = ethnl_default_set_doit,
  926. .policy = ethnl_coalesce_set_policy,
  927. .maxattr = ARRAY_SIZE(ethnl_coalesce_set_policy) - 1,
  928. },
  929. {
  930. .cmd = ETHTOOL_MSG_PAUSE_GET,
  931. .doit = ethnl_default_doit,
  932. .start = ethnl_default_start,
  933. .dumpit = ethnl_default_dumpit,
  934. .done = ethnl_default_done,
  935. .policy = ethnl_pause_get_policy,
  936. .maxattr = ARRAY_SIZE(ethnl_pause_get_policy) - 1,
  937. },
  938. {
  939. .cmd = ETHTOOL_MSG_PAUSE_SET,
  940. .flags = GENL_UNS_ADMIN_PERM,
  941. .doit = ethnl_default_set_doit,
  942. .policy = ethnl_pause_set_policy,
  943. .maxattr = ARRAY_SIZE(ethnl_pause_set_policy) - 1,
  944. },
  945. {
  946. .cmd = ETHTOOL_MSG_EEE_GET,
  947. .doit = ethnl_default_doit,
  948. .start = ethnl_default_start,
  949. .dumpit = ethnl_default_dumpit,
  950. .done = ethnl_default_done,
  951. .policy = ethnl_eee_get_policy,
  952. .maxattr = ARRAY_SIZE(ethnl_eee_get_policy) - 1,
  953. },
  954. {
  955. .cmd = ETHTOOL_MSG_EEE_SET,
  956. .flags = GENL_UNS_ADMIN_PERM,
  957. .doit = ethnl_default_set_doit,
  958. .policy = ethnl_eee_set_policy,
  959. .maxattr = ARRAY_SIZE(ethnl_eee_set_policy) - 1,
  960. },
  961. {
  962. .cmd = ETHTOOL_MSG_TSINFO_GET,
  963. .doit = ethnl_default_doit,
  964. .start = ethnl_default_start,
  965. .dumpit = ethnl_default_dumpit,
  966. .done = ethnl_default_done,
  967. .policy = ethnl_tsinfo_get_policy,
  968. .maxattr = ARRAY_SIZE(ethnl_tsinfo_get_policy) - 1,
  969. },
  970. {
  971. .cmd = ETHTOOL_MSG_CABLE_TEST_ACT,
  972. .flags = GENL_UNS_ADMIN_PERM,
  973. .doit = ethnl_act_cable_test,
  974. .policy = ethnl_cable_test_act_policy,
  975. .maxattr = ARRAY_SIZE(ethnl_cable_test_act_policy) - 1,
  976. },
  977. {
  978. .cmd = ETHTOOL_MSG_CABLE_TEST_TDR_ACT,
  979. .flags = GENL_UNS_ADMIN_PERM,
  980. .doit = ethnl_act_cable_test_tdr,
  981. .policy = ethnl_cable_test_tdr_act_policy,
  982. .maxattr = ARRAY_SIZE(ethnl_cable_test_tdr_act_policy) - 1,
  983. },
  984. {
  985. .cmd = ETHTOOL_MSG_TUNNEL_INFO_GET,
  986. .doit = ethnl_tunnel_info_doit,
  987. .start = ethnl_tunnel_info_start,
  988. .dumpit = ethnl_tunnel_info_dumpit,
  989. .policy = ethnl_tunnel_info_get_policy,
  990. .maxattr = ARRAY_SIZE(ethnl_tunnel_info_get_policy) - 1,
  991. },
  992. {
  993. .cmd = ETHTOOL_MSG_FEC_GET,
  994. .doit = ethnl_default_doit,
  995. .start = ethnl_default_start,
  996. .dumpit = ethnl_default_dumpit,
  997. .done = ethnl_default_done,
  998. .policy = ethnl_fec_get_policy,
  999. .maxattr = ARRAY_SIZE(ethnl_fec_get_policy) - 1,
  1000. },
  1001. {
  1002. .cmd = ETHTOOL_MSG_FEC_SET,
  1003. .flags = GENL_UNS_ADMIN_PERM,
  1004. .doit = ethnl_default_set_doit,
  1005. .policy = ethnl_fec_set_policy,
  1006. .maxattr = ARRAY_SIZE(ethnl_fec_set_policy) - 1,
  1007. },
  1008. {
  1009. .cmd = ETHTOOL_MSG_MODULE_EEPROM_GET,
  1010. .flags = GENL_UNS_ADMIN_PERM,
  1011. .doit = ethnl_default_doit,
  1012. .start = ethnl_default_start,
  1013. .dumpit = ethnl_default_dumpit,
  1014. .done = ethnl_default_done,
  1015. .policy = ethnl_module_eeprom_get_policy,
  1016. .maxattr = ARRAY_SIZE(ethnl_module_eeprom_get_policy) - 1,
  1017. },
  1018. {
  1019. .cmd = ETHTOOL_MSG_STATS_GET,
  1020. .doit = ethnl_default_doit,
  1021. .start = ethnl_default_start,
  1022. .dumpit = ethnl_default_dumpit,
  1023. .done = ethnl_default_done,
  1024. .policy = ethnl_stats_get_policy,
  1025. .maxattr = ARRAY_SIZE(ethnl_stats_get_policy) - 1,
  1026. },
  1027. {
  1028. .cmd = ETHTOOL_MSG_PHC_VCLOCKS_GET,
  1029. .doit = ethnl_default_doit,
  1030. .start = ethnl_default_start,
  1031. .dumpit = ethnl_default_dumpit,
  1032. .done = ethnl_default_done,
  1033. .policy = ethnl_phc_vclocks_get_policy,
  1034. .maxattr = ARRAY_SIZE(ethnl_phc_vclocks_get_policy) - 1,
  1035. },
  1036. {
  1037. .cmd = ETHTOOL_MSG_MODULE_GET,
  1038. .doit = ethnl_default_doit,
  1039. .start = ethnl_default_start,
  1040. .dumpit = ethnl_default_dumpit,
  1041. .done = ethnl_default_done,
  1042. .policy = ethnl_module_get_policy,
  1043. .maxattr = ARRAY_SIZE(ethnl_module_get_policy) - 1,
  1044. },
  1045. {
  1046. .cmd = ETHTOOL_MSG_MODULE_SET,
  1047. .flags = GENL_UNS_ADMIN_PERM,
  1048. .doit = ethnl_default_set_doit,
  1049. .policy = ethnl_module_set_policy,
  1050. .maxattr = ARRAY_SIZE(ethnl_module_set_policy) - 1,
  1051. },
  1052. {
  1053. .cmd = ETHTOOL_MSG_PSE_GET,
  1054. .doit = ethnl_default_doit,
  1055. .start = ethnl_default_start,
  1056. .dumpit = ethnl_default_dumpit,
  1057. .done = ethnl_default_done,
  1058. .policy = ethnl_pse_get_policy,
  1059. .maxattr = ARRAY_SIZE(ethnl_pse_get_policy) - 1,
  1060. },
  1061. {
  1062. .cmd = ETHTOOL_MSG_PSE_SET,
  1063. .flags = GENL_UNS_ADMIN_PERM,
  1064. .doit = ethnl_default_set_doit,
  1065. .policy = ethnl_pse_set_policy,
  1066. .maxattr = ARRAY_SIZE(ethnl_pse_set_policy) - 1,
  1067. },
  1068. {
  1069. .cmd = ETHTOOL_MSG_RSS_GET,
  1070. .doit = ethnl_default_doit,
  1071. .start = ethnl_rss_dump_start,
  1072. .dumpit = ethnl_rss_dumpit,
  1073. .policy = ethnl_rss_get_policy,
  1074. .maxattr = ARRAY_SIZE(ethnl_rss_get_policy) - 1,
  1075. },
  1076. {
  1077. .cmd = ETHTOOL_MSG_PLCA_GET_CFG,
  1078. .doit = ethnl_default_doit,
  1079. .start = ethnl_default_start,
  1080. .dumpit = ethnl_default_dumpit,
  1081. .done = ethnl_default_done,
  1082. .policy = ethnl_plca_get_cfg_policy,
  1083. .maxattr = ARRAY_SIZE(ethnl_plca_get_cfg_policy) - 1,
  1084. },
  1085. {
  1086. .cmd = ETHTOOL_MSG_PLCA_SET_CFG,
  1087. .flags = GENL_UNS_ADMIN_PERM,
  1088. .doit = ethnl_default_set_doit,
  1089. .policy = ethnl_plca_set_cfg_policy,
  1090. .maxattr = ARRAY_SIZE(ethnl_plca_set_cfg_policy) - 1,
  1091. },
  1092. {
  1093. .cmd = ETHTOOL_MSG_PLCA_GET_STATUS,
  1094. .doit = ethnl_default_doit,
  1095. .start = ethnl_default_start,
  1096. .dumpit = ethnl_default_dumpit,
  1097. .done = ethnl_default_done,
  1098. .policy = ethnl_plca_get_status_policy,
  1099. .maxattr = ARRAY_SIZE(ethnl_plca_get_status_policy) - 1,
  1100. },
  1101. {
  1102. .cmd = ETHTOOL_MSG_MM_GET,
  1103. .doit = ethnl_default_doit,
  1104. .start = ethnl_default_start,
  1105. .dumpit = ethnl_default_dumpit,
  1106. .done = ethnl_default_done,
  1107. .policy = ethnl_mm_get_policy,
  1108. .maxattr = ARRAY_SIZE(ethnl_mm_get_policy) - 1,
  1109. },
  1110. {
  1111. .cmd = ETHTOOL_MSG_MM_SET,
  1112. .flags = GENL_UNS_ADMIN_PERM,
  1113. .doit = ethnl_default_set_doit,
  1114. .policy = ethnl_mm_set_policy,
  1115. .maxattr = ARRAY_SIZE(ethnl_mm_set_policy) - 1,
  1116. },
  1117. {
  1118. .cmd = ETHTOOL_MSG_MODULE_FW_FLASH_ACT,
  1119. .flags = GENL_UNS_ADMIN_PERM,
  1120. .doit = ethnl_act_module_fw_flash,
  1121. .policy = ethnl_module_fw_flash_act_policy,
  1122. .maxattr = ARRAY_SIZE(ethnl_module_fw_flash_act_policy) - 1,
  1123. },
  1124. {
  1125. .cmd = ETHTOOL_MSG_PHY_GET,
  1126. .doit = ethnl_phy_doit,
  1127. .start = ethnl_phy_start,
  1128. .dumpit = ethnl_phy_dumpit,
  1129. .done = ethnl_phy_done,
  1130. .policy = ethnl_phy_get_policy,
  1131. .maxattr = ARRAY_SIZE(ethnl_phy_get_policy) - 1,
  1132. },
  1133. };
  1134. static const struct genl_multicast_group ethtool_nl_mcgrps[] = {
  1135. [ETHNL_MCGRP_MONITOR] = { .name = ETHTOOL_MCGRP_MONITOR_NAME },
  1136. };
  1137. static struct genl_family ethtool_genl_family __ro_after_init = {
  1138. .name = ETHTOOL_GENL_NAME,
  1139. .version = ETHTOOL_GENL_VERSION,
  1140. .netnsok = true,
  1141. .parallel_ops = true,
  1142. .ops = ethtool_genl_ops,
  1143. .n_ops = ARRAY_SIZE(ethtool_genl_ops),
  1144. .resv_start_op = ETHTOOL_MSG_MODULE_GET + 1,
  1145. .mcgrps = ethtool_nl_mcgrps,
  1146. .n_mcgrps = ARRAY_SIZE(ethtool_nl_mcgrps),
  1147. .sock_priv_size = sizeof(struct ethnl_sock_priv),
  1148. .sock_priv_destroy = ethnl_sock_priv_destroy,
  1149. };
  1150. /* module setup */
  1151. static int __init ethnl_init(void)
  1152. {
  1153. int ret;
  1154. ret = genl_register_family(&ethtool_genl_family);
  1155. if (WARN(ret < 0, "ethtool: genetlink family registration failed"))
  1156. return ret;
  1157. ethnl_ok = true;
  1158. ret = register_netdevice_notifier(&ethnl_netdev_notifier);
  1159. WARN(ret < 0, "ethtool: net device notifier registration failed");
  1160. return ret;
  1161. }
  1162. subsys_initcall(ethnl_init);