1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __LINUX_NETLINK_H
3 #define __LINUX_NETLINK_H
6 #include <linux/capability.h>
7 #include <linux/skbuff.h>
8 #include <linux/export.h>
10 #include <uapi/linux/netlink.h>
14 void do_trace_netlink_extack(const char *msg);
16 static inline struct nlmsghdr *nlmsg_hdr(const struct sk_buff *skb)
18 return (struct nlmsghdr *)skb->data;
21 enum netlink_skb_flags {
22 NETLINK_SKB_DST = 0x8, /* Dst set in sendto or sendmsg */
25 struct netlink_skb_parms {
26 struct scm_creds creds; /* Skb credentials */
35 #define NETLINK_CB(skb) (*(struct netlink_skb_parms*)&((skb)->cb))
36 #define NETLINK_CREDS(skb) (&NETLINK_CB((skb)).creds)
39 void netlink_table_grab(void);
40 void netlink_table_ungrab(void);
42 #define NL_CFG_F_NONROOT_RECV (1 << 0)
43 #define NL_CFG_F_NONROOT_SEND (1 << 1)
45 /* optional Netlink kernel configuration parameters */
46 struct netlink_kernel_cfg {
49 void (*input)(struct sk_buff *skb);
50 struct mutex *cb_mutex;
51 int (*bind)(struct net *net, int group);
52 void (*unbind)(struct net *net, int group);
53 bool (*compare)(struct net *net, struct sock *sk);
56 struct sock *__netlink_kernel_create(struct net *net, int unit,
57 struct module *module,
58 struct netlink_kernel_cfg *cfg);
59 static inline struct sock *
60 netlink_kernel_create(struct net *net, int unit, struct netlink_kernel_cfg *cfg)
62 return __netlink_kernel_create(net, unit, THIS_MODULE, cfg);
65 /* this can be increased when necessary - don't expose to userland */
66 #define NETLINK_MAX_COOKIE_LEN 20
69 * struct netlink_ext_ack - netlink extended ACK report struct
70 * @_msg: message string to report - don't access directly, use
72 * @bad_attr: attribute with error
73 * @policy: policy for a bad attribute
74 * @miss_type: attribute type which was missing
75 * @miss_nest: nest missing an attribute (%NULL if missing top level attr)
76 * @cookie: cookie data to return to userspace (for success)
77 * @cookie_len: actual cookie data length
79 struct netlink_ext_ack {
81 const struct nlattr *bad_attr;
82 const struct nla_policy *policy;
83 const struct nlattr *miss_nest;
85 u8 cookie[NETLINK_MAX_COOKIE_LEN];
89 /* Always use this macro, this allows later putting the
90 * message into a separate section or such for things
91 * like translation or listing all possible messages.
92 * Currently string formatting is not supported (due
93 * to the lack of an output buffer.)
95 #define NL_SET_ERR_MSG(extack, msg) do { \
96 static const char __msg[] = msg; \
97 struct netlink_ext_ack *__extack = (extack); \
99 do_trace_netlink_extack(__msg); \
102 __extack->_msg = __msg; \
105 #define NL_SET_ERR_MSG_MOD(extack, msg) \
106 NL_SET_ERR_MSG((extack), KBUILD_MODNAME ": " msg)
108 #define NL_SET_BAD_ATTR_POLICY(extack, attr, pol) do { \
110 (extack)->bad_attr = (attr); \
111 (extack)->policy = (pol); \
115 #define NL_SET_BAD_ATTR(extack, attr) NL_SET_BAD_ATTR_POLICY(extack, attr, NULL)
117 #define NL_SET_ERR_MSG_ATTR_POL(extack, attr, pol, msg) do { \
118 static const char __msg[] = msg; \
119 struct netlink_ext_ack *__extack = (extack); \
121 do_trace_netlink_extack(__msg); \
124 __extack->_msg = __msg; \
125 __extack->bad_attr = (attr); \
126 __extack->policy = (pol); \
130 #define NL_SET_ERR_MSG_ATTR(extack, attr, msg) \
131 NL_SET_ERR_MSG_ATTR_POL(extack, attr, NULL, msg)
133 #define NL_SET_ERR_ATTR_MISS(extack, nest, type) do { \
134 struct netlink_ext_ack *__extack = (extack); \
137 __extack->miss_nest = (nest); \
138 __extack->miss_type = (type); \
142 #define NL_REQ_ATTR_CHECK(extack, nest, tb, type) ({ \
143 struct nlattr **__tb = (tb); \
144 u32 __attr = (type); \
147 __retval = !__tb[__attr]; \
149 NL_SET_ERR_ATTR_MISS((extack), (nest), __attr); \
153 static inline void nl_set_extack_cookie_u64(struct netlink_ext_ack *extack,
158 memcpy(extack->cookie, &cookie, sizeof(cookie));
159 extack->cookie_len = sizeof(cookie);
162 void netlink_kernel_release(struct sock *sk);
163 int __netlink_change_ngroups(struct sock *sk, unsigned int groups);
164 int netlink_change_ngroups(struct sock *sk, unsigned int groups);
165 void __netlink_clear_multicast_users(struct sock *sk, unsigned int group);
166 void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err,
167 const struct netlink_ext_ack *extack);
168 int netlink_has_listeners(struct sock *sk, unsigned int group);
169 bool netlink_strict_get_check(struct sk_buff *skb);
171 int netlink_unicast(struct sock *ssk, struct sk_buff *skb, __u32 portid, int nonblock);
172 int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, __u32 portid,
173 __u32 group, gfp_t allocation);
174 int netlink_set_err(struct sock *ssk, __u32 portid, __u32 group, int code);
175 int netlink_register_notifier(struct notifier_block *nb);
176 int netlink_unregister_notifier(struct notifier_block *nb);
178 /* finegrained unicast helpers: */
179 struct sock *netlink_getsockbyfilp(struct file *filp);
180 int netlink_attachskb(struct sock *sk, struct sk_buff *skb,
181 long *timeo, struct sock *ssk);
182 void netlink_detachskb(struct sock *sk, struct sk_buff *skb);
183 int netlink_sendskb(struct sock *sk, struct sk_buff *skb);
185 static inline struct sk_buff *
186 netlink_skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
188 struct sk_buff *nskb;
190 nskb = skb_clone(skb, gfp_mask);
194 /* This is a large skb, set destructor callback to release head */
195 if (is_vmalloc_addr(skb->head))
196 nskb->destructor = skb->destructor;
202 * skb should fit one page. This choice is good for headerless malloc.
203 * But we should limit to 8K so that userspace does not have to
204 * use enormous buffer sizes on recvmsg() calls just to avoid
205 * MSG_TRUNC when PAGE_SIZE is very large.
207 #if PAGE_SIZE < 8192UL
208 #define NLMSG_GOODSIZE SKB_WITH_OVERHEAD(PAGE_SIZE)
210 #define NLMSG_GOODSIZE SKB_WITH_OVERHEAD(8192UL)
213 #define NLMSG_DEFAULT_SIZE (NLMSG_GOODSIZE - NLMSG_HDRLEN)
216 struct netlink_callback {
218 const struct nlmsghdr *nlh;
219 int (*dump)(struct sk_buff * skb,
220 struct netlink_callback *cb);
221 int (*done)(struct netlink_callback *cb);
223 /* the module that dump function belong to */
224 struct module *module;
225 struct netlink_ext_ack *extack;
229 unsigned int prev_seq, seq;
234 /* args is deprecated. Cast a struct over ctx instead
235 * for proper type safety.
241 struct netlink_notify {
248 __nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, int type, int len, int flags);
250 struct netlink_dump_control {
251 int (*start)(struct netlink_callback *);
252 int (*dump)(struct sk_buff *skb, struct netlink_callback *);
253 int (*done)(struct netlink_callback *);
255 struct module *module;
259 int __netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
260 const struct nlmsghdr *nlh,
261 struct netlink_dump_control *control);
262 static inline int netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
263 const struct nlmsghdr *nlh,
264 struct netlink_dump_control *control)
266 if (!control->module)
267 control->module = THIS_MODULE;
269 return __netlink_dump_start(ssk, skb, nlh, control);
273 struct net_device *dev;
274 struct module *module;
275 struct list_head list;
278 int netlink_add_tap(struct netlink_tap *nt);
279 int netlink_remove_tap(struct netlink_tap *nt);
281 bool __netlink_ns_capable(const struct netlink_skb_parms *nsp,
282 struct user_namespace *ns, int cap);
283 bool netlink_ns_capable(const struct sk_buff *skb,
284 struct user_namespace *ns, int cap);
285 bool netlink_capable(const struct sk_buff *skb, int cap);
286 bool netlink_net_capable(const struct sk_buff *skb, int cap);
288 #endif /* __LINUX_NETLINK_H */