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);
55 struct sock *__netlink_kernel_create(struct net *net, int unit,
56 struct module *module,
57 struct netlink_kernel_cfg *cfg);
58 static inline struct sock *
59 netlink_kernel_create(struct net *net, int unit, struct netlink_kernel_cfg *cfg)
61 return __netlink_kernel_create(net, unit, THIS_MODULE, cfg);
64 /* this can be increased when necessary - don't expose to userland */
65 #define NETLINK_MAX_COOKIE_LEN 20
66 #define NETLINK_MAX_FMTMSG_LEN 80
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
78 * @_msg_buf: output buffer for formatted message strings - don't access
79 * directly, use %NL_SET_ERR_MSG_FMT
81 struct netlink_ext_ack {
83 const struct nlattr *bad_attr;
84 const struct nla_policy *policy;
85 const struct nlattr *miss_nest;
87 u8 cookie[NETLINK_MAX_COOKIE_LEN];
89 char _msg_buf[NETLINK_MAX_FMTMSG_LEN];
92 /* Always use this macro, this allows later putting the
93 * message into a separate section or such for things
94 * like translation or listing all possible messages.
95 * If string formatting is needed use NL_SET_ERR_MSG_FMT.
97 #define NL_SET_ERR_MSG(extack, msg) do { \
98 static const char __msg[] = msg; \
99 struct netlink_ext_ack *__extack = (extack); \
101 do_trace_netlink_extack(__msg); \
104 __extack->_msg = __msg; \
107 /* We splice fmt with %s at each end even in the snprintf so that both calls
108 * can use the same string constant, avoiding its duplication in .ro
110 #define NL_SET_ERR_MSG_FMT(extack, fmt, args...) do { \
111 struct netlink_ext_ack *__extack = (extack); \
115 if (snprintf(__extack->_msg_buf, NETLINK_MAX_FMTMSG_LEN, \
116 "%s" fmt "%s", "", ##args, "") >= \
117 NETLINK_MAX_FMTMSG_LEN) \
118 net_warn_ratelimited("%s" fmt "%s", "truncated extack: ", \
121 do_trace_netlink_extack(__extack->_msg_buf); \
123 __extack->_msg = __extack->_msg_buf; \
126 #define NL_SET_ERR_MSG_MOD(extack, msg) \
127 NL_SET_ERR_MSG((extack), KBUILD_MODNAME ": " msg)
129 #define NL_SET_ERR_MSG_FMT_MOD(extack, fmt, args...) \
130 NL_SET_ERR_MSG_FMT((extack), KBUILD_MODNAME ": " fmt, ##args)
132 #define NL_SET_ERR_MSG_WEAK(extack, msg) do { \
133 if ((extack) && !(extack)->_msg) \
134 NL_SET_ERR_MSG((extack), msg); \
137 #define NL_SET_ERR_MSG_WEAK_MOD(extack, msg) do { \
138 if ((extack) && !(extack)->_msg) \
139 NL_SET_ERR_MSG_MOD((extack), msg); \
142 #define NL_SET_BAD_ATTR_POLICY(extack, attr, pol) do { \
144 (extack)->bad_attr = (attr); \
145 (extack)->policy = (pol); \
149 #define NL_SET_BAD_ATTR(extack, attr) NL_SET_BAD_ATTR_POLICY(extack, attr, NULL)
151 #define NL_SET_ERR_MSG_ATTR_POL(extack, attr, pol, msg) do { \
152 static const char __msg[] = msg; \
153 struct netlink_ext_ack *__extack = (extack); \
155 do_trace_netlink_extack(__msg); \
158 __extack->_msg = __msg; \
159 __extack->bad_attr = (attr); \
160 __extack->policy = (pol); \
164 #define NL_SET_ERR_MSG_ATTR_POL_FMT(extack, attr, pol, fmt, args...) do { \
165 struct netlink_ext_ack *__extack = (extack); \
170 if (snprintf(__extack->_msg_buf, NETLINK_MAX_FMTMSG_LEN, \
171 "%s" fmt "%s", "", ##args, "") >= \
172 NETLINK_MAX_FMTMSG_LEN) \
173 net_warn_ratelimited("%s" fmt "%s", "truncated extack: ", \
176 do_trace_netlink_extack(__extack->_msg_buf); \
178 __extack->_msg = __extack->_msg_buf; \
179 __extack->bad_attr = (attr); \
180 __extack->policy = (pol); \
183 #define NL_SET_ERR_MSG_ATTR(extack, attr, msg) \
184 NL_SET_ERR_MSG_ATTR_POL(extack, attr, NULL, msg)
186 #define NL_SET_ERR_MSG_ATTR_FMT(extack, attr, msg, args...) \
187 NL_SET_ERR_MSG_ATTR_POL_FMT(extack, attr, NULL, msg, ##args)
189 #define NL_SET_ERR_ATTR_MISS(extack, nest, type) do { \
190 struct netlink_ext_ack *__extack = (extack); \
193 __extack->miss_nest = (nest); \
194 __extack->miss_type = (type); \
198 #define NL_REQ_ATTR_CHECK(extack, nest, tb, type) ({ \
199 struct nlattr **__tb = (tb); \
200 u32 __attr = (type); \
203 __retval = !__tb[__attr]; \
205 NL_SET_ERR_ATTR_MISS((extack), (nest), __attr); \
209 static inline void nl_set_extack_cookie_u64(struct netlink_ext_ack *extack,
214 memcpy(extack->cookie, &cookie, sizeof(cookie));
215 extack->cookie_len = sizeof(cookie);
218 void netlink_kernel_release(struct sock *sk);
219 int __netlink_change_ngroups(struct sock *sk, unsigned int groups);
220 int netlink_change_ngroups(struct sock *sk, unsigned int groups);
221 void __netlink_clear_multicast_users(struct sock *sk, unsigned int group);
222 void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err,
223 const struct netlink_ext_ack *extack);
224 int netlink_has_listeners(struct sock *sk, unsigned int group);
225 bool netlink_strict_get_check(struct sk_buff *skb);
227 int netlink_unicast(struct sock *ssk, struct sk_buff *skb, __u32 portid, int nonblock);
228 int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, __u32 portid,
229 __u32 group, gfp_t allocation);
230 int netlink_set_err(struct sock *ssk, __u32 portid, __u32 group, int code);
231 int netlink_register_notifier(struct notifier_block *nb);
232 int netlink_unregister_notifier(struct notifier_block *nb);
234 /* finegrained unicast helpers: */
235 struct sock *netlink_getsockbyfilp(struct file *filp);
236 int netlink_attachskb(struct sock *sk, struct sk_buff *skb,
237 long *timeo, struct sock *ssk);
238 void netlink_detachskb(struct sock *sk, struct sk_buff *skb);
239 int netlink_sendskb(struct sock *sk, struct sk_buff *skb);
241 static inline struct sk_buff *
242 netlink_skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
244 struct sk_buff *nskb;
246 nskb = skb_clone(skb, gfp_mask);
250 /* This is a large skb, set destructor callback to release head */
251 if (is_vmalloc_addr(skb->head))
252 nskb->destructor = skb->destructor;
258 * skb should fit one page. This choice is good for headerless malloc.
259 * But we should limit to 8K so that userspace does not have to
260 * use enormous buffer sizes on recvmsg() calls just to avoid
261 * MSG_TRUNC when PAGE_SIZE is very large.
263 #if PAGE_SIZE < 8192UL
264 #define NLMSG_GOODSIZE SKB_WITH_OVERHEAD(PAGE_SIZE)
266 #define NLMSG_GOODSIZE SKB_WITH_OVERHEAD(8192UL)
269 #define NLMSG_DEFAULT_SIZE (NLMSG_GOODSIZE - NLMSG_HDRLEN)
272 struct netlink_callback {
274 const struct nlmsghdr *nlh;
275 int (*dump)(struct sk_buff * skb,
276 struct netlink_callback *cb);
277 int (*done)(struct netlink_callback *cb);
279 /* the module that dump function belong to */
280 struct module *module;
281 struct netlink_ext_ack *extack;
285 unsigned int prev_seq, seq;
290 /* args is deprecated. Cast a struct over ctx instead
291 * for proper type safety.
297 #define NL_ASSERT_DUMP_CTX_FITS(type_name) \
298 BUILD_BUG_ON(sizeof(type_name) > \
299 sizeof_field(struct netlink_callback, ctx))
301 struct netlink_notify {
308 __nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, int type, int len, int flags);
310 struct netlink_dump_control {
311 int (*start)(struct netlink_callback *);
312 int (*dump)(struct sk_buff *skb, struct netlink_callback *);
313 int (*done)(struct netlink_callback *);
315 struct module *module;
319 int __netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
320 const struct nlmsghdr *nlh,
321 struct netlink_dump_control *control);
322 static inline int netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
323 const struct nlmsghdr *nlh,
324 struct netlink_dump_control *control)
326 if (!control->module)
327 control->module = THIS_MODULE;
329 return __netlink_dump_start(ssk, skb, nlh, control);
333 struct net_device *dev;
334 struct module *module;
335 struct list_head list;
338 int netlink_add_tap(struct netlink_tap *nt);
339 int netlink_remove_tap(struct netlink_tap *nt);
341 bool __netlink_ns_capable(const struct netlink_skb_parms *nsp,
342 struct user_namespace *ns, int cap);
343 bool netlink_ns_capable(const struct sk_buff *skb,
344 struct user_namespace *ns, int cap);
345 bool netlink_capable(const struct sk_buff *skb, int cap);
346 bool netlink_net_capable(const struct sk_buff *skb, int cap);
348 #endif /* __LINUX_NETLINK_H */