netlink.h 17 KB

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