rtnetlink.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __NET_RTNETLINK_H
  3. #define __NET_RTNETLINK_H
  4. #include <linux/rtnetlink.h>
  5. #include <net/netlink.h>
  6. typedef int (*rtnl_doit_func)(struct sk_buff *, struct nlmsghdr *,
  7. struct netlink_ext_ack *);
  8. typedef int (*rtnl_dumpit_func)(struct sk_buff *, struct netlink_callback *);
  9. enum rtnl_link_flags {
  10. RTNL_FLAG_DOIT_UNLOCKED = BIT(0),
  11. RTNL_FLAG_BULK_DEL_SUPPORTED = BIT(1),
  12. RTNL_FLAG_DUMP_UNLOCKED = BIT(2),
  13. RTNL_FLAG_DUMP_SPLIT_NLM_DONE = BIT(3), /* legacy behavior */
  14. };
  15. enum rtnl_kinds {
  16. RTNL_KIND_NEW,
  17. RTNL_KIND_DEL,
  18. RTNL_KIND_GET,
  19. RTNL_KIND_SET
  20. };
  21. #define RTNL_KIND_MASK 0x3
  22. static inline enum rtnl_kinds rtnl_msgtype_kind(int msgtype)
  23. {
  24. return msgtype & RTNL_KIND_MASK;
  25. }
  26. struct rtnl_msg_handler {
  27. struct module *owner;
  28. int protocol;
  29. int msgtype;
  30. rtnl_doit_func doit;
  31. rtnl_dumpit_func dumpit;
  32. int flags;
  33. };
  34. void rtnl_register(int protocol, int msgtype,
  35. rtnl_doit_func, rtnl_dumpit_func, unsigned int flags);
  36. int rtnl_register_module(struct module *owner, int protocol, int msgtype,
  37. rtnl_doit_func, rtnl_dumpit_func, unsigned int flags);
  38. int rtnl_unregister(int protocol, int msgtype);
  39. void rtnl_unregister_all(int protocol);
  40. int __rtnl_register_many(const struct rtnl_msg_handler *handlers, int n);
  41. void __rtnl_unregister_many(const struct rtnl_msg_handler *handlers, int n);
  42. #define rtnl_register_many(handlers) \
  43. __rtnl_register_many(handlers, ARRAY_SIZE(handlers))
  44. #define rtnl_unregister_many(handlers) \
  45. __rtnl_unregister_many(handlers, ARRAY_SIZE(handlers))
  46. static inline int rtnl_msg_family(const struct nlmsghdr *nlh)
  47. {
  48. if (nlmsg_len(nlh) >= sizeof(struct rtgenmsg))
  49. return ((struct rtgenmsg *) nlmsg_data(nlh))->rtgen_family;
  50. else
  51. return AF_UNSPEC;
  52. }
  53. /**
  54. * struct rtnl_link_ops - rtnetlink link operations
  55. *
  56. * @list: Used internally
  57. * @kind: Identifier
  58. * @netns_refund: Physical device, move to init_net on netns exit
  59. * @maxtype: Highest device specific netlink attribute number
  60. * @policy: Netlink policy for device specific attribute validation
  61. * @validate: Optional validation function for netlink/changelink parameters
  62. * @alloc: netdev allocation function, can be %NULL and is then used
  63. * in place of alloc_netdev_mqs(), in this case @priv_size
  64. * and @setup are unused. Returns a netdev or ERR_PTR().
  65. * @priv_size: sizeof net_device private space
  66. * @setup: net_device setup function
  67. * @newlink: Function for configuring and registering a new device
  68. * @changelink: Function for changing parameters of an existing device
  69. * @dellink: Function to remove a device
  70. * @get_size: Function to calculate required room for dumping device
  71. * specific netlink attributes
  72. * @fill_info: Function to dump device specific netlink attributes
  73. * @get_xstats_size: Function to calculate required room for dumping device
  74. * specific statistics
  75. * @fill_xstats: Function to dump device specific statistics
  76. * @get_num_tx_queues: Function to determine number of transmit queues
  77. * to create when creating a new device.
  78. * @get_num_rx_queues: Function to determine number of receive queues
  79. * to create when creating a new device.
  80. * @get_link_net: Function to get the i/o netns of the device
  81. * @get_linkxstats_size: Function to calculate the required room for
  82. * dumping device-specific extended link stats
  83. * @fill_linkxstats: Function to dump device-specific extended link stats
  84. */
  85. struct rtnl_link_ops {
  86. struct list_head list;
  87. const char *kind;
  88. size_t priv_size;
  89. struct net_device *(*alloc)(struct nlattr *tb[],
  90. const char *ifname,
  91. unsigned char name_assign_type,
  92. unsigned int num_tx_queues,
  93. unsigned int num_rx_queues);
  94. void (*setup)(struct net_device *dev);
  95. bool netns_refund;
  96. unsigned int maxtype;
  97. const struct nla_policy *policy;
  98. int (*validate)(struct nlattr *tb[],
  99. struct nlattr *data[],
  100. struct netlink_ext_ack *extack);
  101. int (*newlink)(struct net *src_net,
  102. struct net_device *dev,
  103. struct nlattr *tb[],
  104. struct nlattr *data[],
  105. struct netlink_ext_ack *extack);
  106. int (*changelink)(struct net_device *dev,
  107. struct nlattr *tb[],
  108. struct nlattr *data[],
  109. struct netlink_ext_ack *extack);
  110. void (*dellink)(struct net_device *dev,
  111. struct list_head *head);
  112. size_t (*get_size)(const struct net_device *dev);
  113. int (*fill_info)(struct sk_buff *skb,
  114. const struct net_device *dev);
  115. size_t (*get_xstats_size)(const struct net_device *dev);
  116. int (*fill_xstats)(struct sk_buff *skb,
  117. const struct net_device *dev);
  118. unsigned int (*get_num_tx_queues)(void);
  119. unsigned int (*get_num_rx_queues)(void);
  120. unsigned int slave_maxtype;
  121. const struct nla_policy *slave_policy;
  122. int (*slave_changelink)(struct net_device *dev,
  123. struct net_device *slave_dev,
  124. struct nlattr *tb[],
  125. struct nlattr *data[],
  126. struct netlink_ext_ack *extack);
  127. size_t (*get_slave_size)(const struct net_device *dev,
  128. const struct net_device *slave_dev);
  129. int (*fill_slave_info)(struct sk_buff *skb,
  130. const struct net_device *dev,
  131. const struct net_device *slave_dev);
  132. struct net *(*get_link_net)(const struct net_device *dev);
  133. size_t (*get_linkxstats_size)(const struct net_device *dev,
  134. int attr);
  135. int (*fill_linkxstats)(struct sk_buff *skb,
  136. const struct net_device *dev,
  137. int *prividx, int attr);
  138. };
  139. int __rtnl_link_register(struct rtnl_link_ops *ops);
  140. void __rtnl_link_unregister(struct rtnl_link_ops *ops);
  141. int rtnl_link_register(struct rtnl_link_ops *ops);
  142. void rtnl_link_unregister(struct rtnl_link_ops *ops);
  143. /**
  144. * struct rtnl_af_ops - rtnetlink address family operations
  145. *
  146. * @list: Used internally
  147. * @family: Address family
  148. * @fill_link_af: Function to fill IFLA_AF_SPEC with address family
  149. * specific netlink attributes.
  150. * @get_link_af_size: Function to calculate size of address family specific
  151. * netlink attributes.
  152. * @validate_link_af: Validate a IFLA_AF_SPEC attribute, must check attr
  153. * for invalid configuration settings.
  154. * @set_link_af: Function to parse a IFLA_AF_SPEC attribute and modify
  155. * net_device accordingly.
  156. */
  157. struct rtnl_af_ops {
  158. struct list_head list;
  159. int family;
  160. int (*fill_link_af)(struct sk_buff *skb,
  161. const struct net_device *dev,
  162. u32 ext_filter_mask);
  163. size_t (*get_link_af_size)(const struct net_device *dev,
  164. u32 ext_filter_mask);
  165. int (*validate_link_af)(const struct net_device *dev,
  166. const struct nlattr *attr,
  167. struct netlink_ext_ack *extack);
  168. int (*set_link_af)(struct net_device *dev,
  169. const struct nlattr *attr,
  170. struct netlink_ext_ack *extack);
  171. int (*fill_stats_af)(struct sk_buff *skb,
  172. const struct net_device *dev);
  173. size_t (*get_stats_af_size)(const struct net_device *dev);
  174. };
  175. void rtnl_af_register(struct rtnl_af_ops *ops);
  176. void rtnl_af_unregister(struct rtnl_af_ops *ops);
  177. struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[]);
  178. struct net_device *rtnl_create_link(struct net *net, const char *ifname,
  179. unsigned char name_assign_type,
  180. const struct rtnl_link_ops *ops,
  181. struct nlattr *tb[],
  182. struct netlink_ext_ack *extack);
  183. int rtnl_delete_link(struct net_device *dev, u32 portid, const struct nlmsghdr *nlh);
  184. int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm,
  185. u32 portid, const struct nlmsghdr *nlh);
  186. int rtnl_nla_parse_ifinfomsg(struct nlattr **tb, const struct nlattr *nla_peer,
  187. struct netlink_ext_ack *exterr);
  188. struct net *rtnl_get_net_ns_capable(struct sock *sk, int netnsid);
  189. #define MODULE_ALIAS_RTNL_LINK(kind) MODULE_ALIAS("rtnl-link-" kind)
  190. #endif