rtnetlink.h 7.0 KB

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