rtnetlink.h 9.0 KB

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