1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2e2849863SThomas Graf #ifndef __NET_RTNETLINK_H
3e2849863SThomas Graf #define __NET_RTNETLINK_H
4e2849863SThomas Graf
5e2849863SThomas Graf #include <linux/rtnetlink.h>
6e2849863SThomas Graf #include <net/netlink.h>
7e2849863SThomas Graf
8c21ef3e3SDavid Ahern typedef int (*rtnl_doit_func)(struct sk_buff *, struct nlmsghdr *,
9c21ef3e3SDavid Ahern struct netlink_ext_ack *);
10e2849863SThomas Graf typedef int (*rtnl_dumpit_func)(struct sk_buff *, struct netlink_callback *);
11e2849863SThomas Graf
1262256f98SFlorian Westphal enum rtnl_link_flags {
130569e31fSNikolay Aleksandrov RTNL_FLAG_DOIT_UNLOCKED = BIT(0),
14a6cec0bcSNikolay Aleksandrov RTNL_FLAG_BULK_DEL_SUPPORTED = BIT(1),
15*ba5366b8SEric Dumazet RTNL_FLAG_DUMP_UNLOCKED = BIT(2),
1662256f98SFlorian Westphal };
1762256f98SFlorian Westphal
1812dc5c2cSNikolay Aleksandrov enum rtnl_kinds {
1912dc5c2cSNikolay Aleksandrov RTNL_KIND_NEW,
2012dc5c2cSNikolay Aleksandrov RTNL_KIND_DEL,
2112dc5c2cSNikolay Aleksandrov RTNL_KIND_GET,
2212dc5c2cSNikolay Aleksandrov RTNL_KIND_SET
2312dc5c2cSNikolay Aleksandrov };
242e9ea3e3SNikolay Aleksandrov #define RTNL_KIND_MASK 0x3
252e9ea3e3SNikolay Aleksandrov
rtnl_msgtype_kind(int msgtype)262e9ea3e3SNikolay Aleksandrov static inline enum rtnl_kinds rtnl_msgtype_kind(int msgtype)
272e9ea3e3SNikolay Aleksandrov {
282e9ea3e3SNikolay Aleksandrov return msgtype & RTNL_KIND_MASK;
292e9ea3e3SNikolay Aleksandrov }
3012dc5c2cSNikolay Aleksandrov
3135b72beaSKuniyuki Iwashima struct rtnl_msg_handler {
3235b72beaSKuniyuki Iwashima struct module *owner;
3335b72beaSKuniyuki Iwashima int protocol;
3435b72beaSKuniyuki Iwashima int msgtype;
3535b72beaSKuniyuki Iwashima rtnl_doit_func doit;
3635b72beaSKuniyuki Iwashima rtnl_dumpit_func dumpit;
3735b72beaSKuniyuki Iwashima int flags;
3835b72beaSKuniyuki Iwashima };
3935b72beaSKuniyuki Iwashima
40efb48ccfSJoe Perches void rtnl_register(int protocol, int msgtype,
41b97bac64SFlorian Westphal rtnl_doit_func, rtnl_dumpit_func, unsigned int flags);
42e4202511SFlorian Westphal int rtnl_register_module(struct module *owner, int protocol, int msgtype,
43e4202511SFlorian Westphal rtnl_doit_func, rtnl_dumpit_func, unsigned int flags);
44efb48ccfSJoe Perches int rtnl_unregister(int protocol, int msgtype);
45efb48ccfSJoe Perches void rtnl_unregister_all(int protocol);
46e2849863SThomas Graf
4735b72beaSKuniyuki Iwashima int __rtnl_register_many(const struct rtnl_msg_handler *handlers, int n);
4835b72beaSKuniyuki Iwashima void __rtnl_unregister_many(const struct rtnl_msg_handler *handlers, int n);
4935b72beaSKuniyuki Iwashima
5035b72beaSKuniyuki Iwashima #define rtnl_register_many(handlers) \
5135b72beaSKuniyuki Iwashima __rtnl_register_many(handlers, ARRAY_SIZE(handlers))
5235b72beaSKuniyuki Iwashima #define rtnl_unregister_many(handlers) \
5335b72beaSKuniyuki Iwashima __rtnl_unregister_many(handlers, ARRAY_SIZE(handlers))
5435b72beaSKuniyuki Iwashima
rtnl_msg_family(const struct nlmsghdr * nlh)553a6c2b41SPatrick McHardy static inline int rtnl_msg_family(const struct nlmsghdr *nlh)
56c454673dSThomas Graf {
57c454673dSThomas Graf if (nlmsg_len(nlh) >= sizeof(struct rtgenmsg))
58c454673dSThomas Graf return ((struct rtgenmsg *) nlmsg_data(nlh))->rtgen_family;
59c454673dSThomas Graf else
60c454673dSThomas Graf return AF_UNSPEC;
61c454673dSThomas Graf }
62c454673dSThomas Graf
6338f7b870SPatrick McHardy /**
6438f7b870SPatrick McHardy * struct rtnl_link_ops - rtnetlink link operations
6538f7b870SPatrick McHardy *
6638f7b870SPatrick McHardy * @list: Used internally
6738f7b870SPatrick McHardy * @kind: Identifier
683a5ca857SMartin Willi * @netns_refund: Physical device, move to init_net on netns exit
6938f7b870SPatrick McHardy * @maxtype: Highest device specific netlink attribute number
7038f7b870SPatrick McHardy * @policy: Netlink policy for device specific attribute validation
7138f7b870SPatrick McHardy * @validate: Optional validation function for netlink/changelink parameters
728c713dc9SJohannes Berg * @alloc: netdev allocation function, can be %NULL and is then used
738c713dc9SJohannes Berg * in place of alloc_netdev_mqs(), in this case @priv_size
748c713dc9SJohannes Berg * and @setup are unused. Returns a netdev or ERR_PTR().
7538f7b870SPatrick McHardy * @priv_size: sizeof net_device private space
7638f7b870SPatrick McHardy * @setup: net_device setup function
7738f7b870SPatrick McHardy * @newlink: Function for configuring and registering a new device
7838f7b870SPatrick McHardy * @changelink: Function for changing parameters of an existing device
7938f7b870SPatrick McHardy * @dellink: Function to remove a device
8038f7b870SPatrick McHardy * @get_size: Function to calculate required room for dumping device
8138f7b870SPatrick McHardy * specific netlink attributes
8238f7b870SPatrick McHardy * @fill_info: Function to dump device specific netlink attributes
839b17876fSstephen hemminger * @get_xstats_size: Function to calculate required room for dumping device
8438f7b870SPatrick McHardy * specific statistics
8538f7b870SPatrick McHardy * @fill_xstats: Function to dump device specific statistics
86d40156aaSJiri Pirko * @get_num_tx_queues: Function to determine number of transmit queues
87d40156aaSJiri Pirko * to create when creating a new device.
88d40156aaSJiri Pirko * @get_num_rx_queues: Function to determine number of receive queues
89d40156aaSJiri Pirko * to create when creating a new device.
90d37512a2SNicolas Dichtel * @get_link_net: Function to get the i/o netns of the device
9197a47facSNikolay Aleksandrov * @get_linkxstats_size: Function to calculate the required room for
9297a47facSNikolay Aleksandrov * dumping device-specific extended link stats
9397a47facSNikolay Aleksandrov * @fill_linkxstats: Function to dump device-specific extended link stats
9438f7b870SPatrick McHardy */
9538f7b870SPatrick McHardy struct rtnl_link_ops {
9638f7b870SPatrick McHardy struct list_head list;
9738f7b870SPatrick McHardy
9838f7b870SPatrick McHardy const char *kind;
9938f7b870SPatrick McHardy
10038f7b870SPatrick McHardy size_t priv_size;
1018c713dc9SJohannes Berg struct net_device *(*alloc)(struct nlattr *tb[],
1028c713dc9SJohannes Berg const char *ifname,
1038c713dc9SJohannes Berg unsigned char name_assign_type,
1048c713dc9SJohannes Berg unsigned int num_tx_queues,
1058c713dc9SJohannes Berg unsigned int num_rx_queues);
10638f7b870SPatrick McHardy void (*setup)(struct net_device *dev);
10738f7b870SPatrick McHardy
1083a5ca857SMartin Willi bool netns_refund;
109ccf8dbcdSKees Cook unsigned int maxtype;
11038f7b870SPatrick McHardy const struct nla_policy *policy;
11138f7b870SPatrick McHardy int (*validate)(struct nlattr *tb[],
112a8b8a889SMatthias Schiffer struct nlattr *data[],
113a8b8a889SMatthias Schiffer struct netlink_ext_ack *extack);
11438f7b870SPatrick McHardy
11581adee47SEric W. Biederman int (*newlink)(struct net *src_net,
11681adee47SEric W. Biederman struct net_device *dev,
11738f7b870SPatrick McHardy struct nlattr *tb[],
1187a3f4a18SMatthias Schiffer struct nlattr *data[],
1197a3f4a18SMatthias Schiffer struct netlink_ext_ack *extack);
12038f7b870SPatrick McHardy int (*changelink)(struct net_device *dev,
12138f7b870SPatrick McHardy struct nlattr *tb[],
122ad744b22SMatthias Schiffer struct nlattr *data[],
123ad744b22SMatthias Schiffer struct netlink_ext_ack *extack);
12423289a37SEric Dumazet void (*dellink)(struct net_device *dev,
12523289a37SEric Dumazet struct list_head *head);
12638f7b870SPatrick McHardy
12738f7b870SPatrick McHardy size_t (*get_size)(const struct net_device *dev);
12838f7b870SPatrick McHardy int (*fill_info)(struct sk_buff *skb,
12938f7b870SPatrick McHardy const struct net_device *dev);
13038f7b870SPatrick McHardy
13138f7b870SPatrick McHardy size_t (*get_xstats_size)(const struct net_device *dev);
13238f7b870SPatrick McHardy int (*fill_xstats)(struct sk_buff *skb,
13338f7b870SPatrick McHardy const struct net_device *dev);
134d40156aaSJiri Pirko unsigned int (*get_num_tx_queues)(void);
135d40156aaSJiri Pirko unsigned int (*get_num_rx_queues)(void);
136ba7d49b1SJiri Pirko
137ccf8dbcdSKees Cook unsigned int slave_maxtype;
138ba7d49b1SJiri Pirko const struct nla_policy *slave_policy;
139ba7d49b1SJiri Pirko int (*slave_changelink)(struct net_device *dev,
140ba7d49b1SJiri Pirko struct net_device *slave_dev,
141ba7d49b1SJiri Pirko struct nlattr *tb[],
14217dd0ec4SMatthias Schiffer struct nlattr *data[],
14317dd0ec4SMatthias Schiffer struct netlink_ext_ack *extack);
144ba7d49b1SJiri Pirko size_t (*get_slave_size)(const struct net_device *dev,
145ba7d49b1SJiri Pirko const struct net_device *slave_dev);
146ba7d49b1SJiri Pirko int (*fill_slave_info)(struct sk_buff *skb,
147ba7d49b1SJiri Pirko const struct net_device *dev,
148ba7d49b1SJiri Pirko const struct net_device *slave_dev);
149d37512a2SNicolas Dichtel struct net *(*get_link_net)(const struct net_device *dev);
15080e73cc5SNikolay Aleksandrov size_t (*get_linkxstats_size)(const struct net_device *dev,
15180e73cc5SNikolay Aleksandrov int attr);
15297a47facSNikolay Aleksandrov int (*fill_linkxstats)(struct sk_buff *skb,
15397a47facSNikolay Aleksandrov const struct net_device *dev,
15480e73cc5SNikolay Aleksandrov int *prividx, int attr);
15538f7b870SPatrick McHardy };
15638f7b870SPatrick McHardy
157efb48ccfSJoe Perches int __rtnl_link_register(struct rtnl_link_ops *ops);
158efb48ccfSJoe Perches void __rtnl_link_unregister(struct rtnl_link_ops *ops);
15938f7b870SPatrick McHardy
160efb48ccfSJoe Perches int rtnl_link_register(struct rtnl_link_ops *ops);
161efb48ccfSJoe Perches void rtnl_link_unregister(struct rtnl_link_ops *ops);
16238f7b870SPatrick McHardy
163f8ff182cSThomas Graf /**
164f8ff182cSThomas Graf * struct rtnl_af_ops - rtnetlink address family operations
165f8ff182cSThomas Graf *
166f8ff182cSThomas Graf * @list: Used internally
167f8ff182cSThomas Graf * @family: Address family
168f8ff182cSThomas Graf * @fill_link_af: Function to fill IFLA_AF_SPEC with address family
169f8ff182cSThomas Graf * specific netlink attributes.
170f8ff182cSThomas Graf * @get_link_af_size: Function to calculate size of address family specific
1719b17876fSstephen hemminger * netlink attributes.
172cf7afbfeSThomas Graf * @validate_link_af: Validate a IFLA_AF_SPEC attribute, must check attr
173cf7afbfeSThomas Graf * for invalid configuration settings.
174cf7afbfeSThomas Graf * @set_link_af: Function to parse a IFLA_AF_SPEC attribute and modify
175f8ff182cSThomas Graf * net_device accordingly.
176f8ff182cSThomas Graf */
177f8ff182cSThomas Graf struct rtnl_af_ops {
178f8ff182cSThomas Graf struct list_head list;
179f8ff182cSThomas Graf int family;
180f8ff182cSThomas Graf
181f8ff182cSThomas Graf int (*fill_link_af)(struct sk_buff *skb,
182d5566fd7SSowmini Varadhan const struct net_device *dev,
183d5566fd7SSowmini Varadhan u32 ext_filter_mask);
184b1974ed0SArad, Ronen size_t (*get_link_af_size)(const struct net_device *dev,
185b1974ed0SArad, Ronen u32 ext_filter_mask);
186f8ff182cSThomas Graf
187cf7afbfeSThomas Graf int (*validate_link_af)(const struct net_device *dev,
1888679c31eSRocco Yue const struct nlattr *attr,
1898679c31eSRocco Yue struct netlink_ext_ack *extack);
190cf7afbfeSThomas Graf int (*set_link_af)(struct net_device *dev,
1913583a4e8SStephen Hemminger const struct nlattr *attr,
1923583a4e8SStephen Hemminger struct netlink_ext_ack *extack);
193aefb4d4aSRobert Shearman int (*fill_stats_af)(struct sk_buff *skb,
194aefb4d4aSRobert Shearman const struct net_device *dev);
195aefb4d4aSRobert Shearman size_t (*get_stats_af_size)(const struct net_device *dev);
196f8ff182cSThomas Graf };
197f8ff182cSThomas Graf
1983678a9d8Sstephen hemminger void rtnl_af_register(struct rtnl_af_ops *ops);
199efb48ccfSJoe Perches void rtnl_af_unregister(struct rtnl_af_ops *ops);
200f8ff182cSThomas Graf
201efb48ccfSJoe Perches struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[]);
20278ebb0d0SThomas Graf struct net_device *rtnl_create_link(struct net *net, const char *ifname,
2035517750fSTom Gundersen unsigned char name_assign_type,
204efb48ccfSJoe Perches const struct rtnl_link_ops *ops,
205d0522f1cSDavid Ahern struct nlattr *tb[],
206d0522f1cSDavid Ahern struct netlink_ext_ack *extack);
207f3a63cceSHangbin Liu int rtnl_delete_link(struct net_device *dev, u32 portid, const struct nlmsghdr *nlh);
2081d997f10SHangbin Liu int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm,
2091d997f10SHangbin Liu u32 portid, const struct nlmsghdr *nlh);
210f8ff182cSThomas Graf
211f534f658SJakub Kicinski int rtnl_nla_parse_ifinfomsg(struct nlattr **tb, const struct nlattr *nla_peer,
212fceb6435SJohannes Berg struct netlink_ext_ack *exterr);
213c383edc4SChristian Brauner struct net *rtnl_get_net_ns_capable(struct sock *sk, int netnsid);
214e7199288SPavel Emelianov
21538f7b870SPatrick McHardy #define MODULE_ALIAS_RTNL_LINK(kind) MODULE_ALIAS("rtnl-link-" kind)
21638f7b870SPatrick McHardy
217e2849863SThomas Graf #endif
218