netlink: remove unused 'compare' function
[linux-block.git] / include / linux / netlink.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
1da177e4
LT
2#ifndef __LINUX_NETLINK_H
3#define __LINUX_NETLINK_H
4
1da177e4
LT
5
6#include <linux/capability.h>
7#include <linux/skbuff.h>
abb17e6c 8#include <linux/export.h>
dbe9a417 9#include <net/scm.h>
607ca46e 10#include <uapi/linux/netlink.h>
1da177e4 11
56b49f4b
OW
12struct net;
13
7e3ce05e
MRL
14void do_trace_netlink_extack(const char *msg);
15
b529ccf2
ACM
16static inline struct nlmsghdr *nlmsg_hdr(const struct sk_buff *skb)
17{
18 return (struct nlmsghdr *)skb->data;
19}
20
9652e931 21enum netlink_skb_flags {
2d7a85f4 22 NETLINK_SKB_DST = 0x8, /* Dst set in sendto or sendmsg */
9652e931
PM
23};
24
d94d9fee 25struct netlink_skb_parms {
dbe9a417 26 struct scm_creds creds; /* Skb credentials */
15e47304 27 __u32 portid;
d629b836 28 __u32 dst_group;
9652e931 29 __u32 flags;
e32123e5 30 struct sock *sk;
59324cf3
ND
31 bool nsid_is_set;
32 int nsid;
1da177e4
LT
33};
34
35#define NETLINK_CB(skb) (*(struct netlink_skb_parms*)&((skb)->cb))
36#define NETLINK_CREDS(skb) (&NETLINK_CB((skb)).creds)
37
38
aa9d6e0f
SH
39void netlink_table_grab(void);
40void netlink_table_ungrab(void);
d136f1bd 41
9785e10a
PNA
42#define NL_CFG_F_NONROOT_RECV (1 << 0)
43#define NL_CFG_F_NONROOT_SEND (1 << 1)
44
a31f2d17
PNA
45/* optional Netlink kernel configuration parameters */
46struct netlink_kernel_cfg {
47 unsigned int groups;
c9d2ea96 48 unsigned int flags;
a31f2d17
PNA
49 void (*input)(struct sk_buff *skb);
50 struct mutex *cb_mutex;
023e2cfa
JB
51 int (*bind)(struct net *net, int group);
52 void (*unbind)(struct net *net, int group);
a31f2d17
PNA
53};
54
aa9d6e0f 55struct sock *__netlink_kernel_create(struct net *net, int unit,
9f00d977
PNA
56 struct module *module,
57 struct netlink_kernel_cfg *cfg);
58static inline struct sock *
59netlink_kernel_create(struct net *net, int unit, struct netlink_kernel_cfg *cfg)
60{
61 return __netlink_kernel_create(net, unit, THIS_MODULE, cfg);
62}
63
ba0dc5f6
JB
64/* this can be increased when necessary - don't expose to userland */
65#define NETLINK_MAX_COOKIE_LEN 20
51c352bd 66#define NETLINK_MAX_FMTMSG_LEN 80
ba0dc5f6 67
2d4bc933
JB
68/**
69 * struct netlink_ext_ack - netlink extended ACK report struct
70 * @_msg: message string to report - don't access directly, use
71 * %NL_SET_ERR_MSG
72 * @bad_attr: attribute with error
44f3625b 73 * @policy: policy for a bad attribute
690252f1
JK
74 * @miss_type: attribute type which was missing
75 * @miss_nest: nest missing an attribute (%NULL if missing top level attr)
ba0dc5f6
JB
76 * @cookie: cookie data to return to userspace (for success)
77 * @cookie_len: actual cookie data length
51c352bd
EC
78 * @_msg_buf: output buffer for formatted message strings - don't access
79 * directly, use %NL_SET_ERR_MSG_FMT
2d4bc933
JB
80 */
81struct netlink_ext_ack {
82 const char *_msg;
83 const struct nlattr *bad_attr;
44f3625b 84 const struct nla_policy *policy;
690252f1
JK
85 const struct nlattr *miss_nest;
86 u16 miss_type;
ba0dc5f6
JB
87 u8 cookie[NETLINK_MAX_COOKIE_LEN];
88 u8 cookie_len;
51c352bd 89 char _msg_buf[NETLINK_MAX_FMTMSG_LEN];
2d4bc933
JB
90};
91
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.
51c352bd 95 * If string formatting is needed use NL_SET_ERR_MSG_FMT.
2d4bc933 96 */
4d463c4d 97#define NL_SET_ERR_MSG(extack, msg) do { \
6311b7ce 98 static const char __msg[] = msg; \
4d463c4d
DB
99 struct netlink_ext_ack *__extack = (extack); \
100 \
7e3ce05e
MRL
101 do_trace_netlink_extack(__msg); \
102 \
4d463c4d
DB
103 if (__extack) \
104 __extack->_msg = __msg; \
2d4bc933
JB
105} while (0)
106
51c352bd
EC
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
109 */
110#define NL_SET_ERR_MSG_FMT(extack, fmt, args...) do { \
111 struct netlink_ext_ack *__extack = (extack); \
112 \
113 if (!__extack) \
114 break; \
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: ", \
119 ##args, "\n"); \
120 \
121 do_trace_netlink_extack(__extack->_msg_buf); \
122 \
123 __extack->_msg = __extack->_msg_buf; \
124} while (0)
125
4d463c4d
DB
126#define NL_SET_ERR_MSG_MOD(extack, msg) \
127 NL_SET_ERR_MSG((extack), KBUILD_MODNAME ": " msg)
45d9b378 128
51c352bd
EC
129#define NL_SET_ERR_MSG_FMT_MOD(extack, fmt, args...) \
130 NL_SET_ERR_MSG_FMT((extack), KBUILD_MODNAME ": " fmt, ##args)
131
028fb19c
LR
132#define NL_SET_ERR_MSG_WEAK(extack, msg) do { \
133 if ((extack) && !(extack)->_msg) \
134 NL_SET_ERR_MSG((extack), msg); \
135} while (0)
136
137#define NL_SET_ERR_MSG_WEAK_MOD(extack, msg) do { \
138 if ((extack) && !(extack)->_msg) \
139 NL_SET_ERR_MSG_MOD((extack), msg); \
140} while (0)
141
44f3625b
JB
142#define NL_SET_BAD_ATTR_POLICY(extack, attr, pol) do { \
143 if ((extack)) { \
c3ab2b4e 144 (extack)->bad_attr = (attr); \
44f3625b
JB
145 (extack)->policy = (pol); \
146 } \
c3ab2b4e
DA
147} while (0)
148
44f3625b
JB
149#define NL_SET_BAD_ATTR(extack, attr) NL_SET_BAD_ATTR_POLICY(extack, attr, NULL)
150
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); \
154 \
7e3ce05e
MRL
155 do_trace_netlink_extack(__msg); \
156 \
44f3625b
JB
157 if (__extack) { \
158 __extack->_msg = __msg; \
159 __extack->bad_attr = (attr); \
160 __extack->policy = (pol); \
161 } \
9ae28727
DA
162} while (0)
163
44f3625b
JB
164#define NL_SET_ERR_MSG_ATTR(extack, attr, msg) \
165 NL_SET_ERR_MSG_ATTR_POL(extack, attr, NULL, msg)
166
690252f1
JK
167#define NL_SET_ERR_ATTR_MISS(extack, nest, type) do { \
168 struct netlink_ext_ack *__extack = (extack); \
169 \
170 if (__extack) { \
171 __extack->miss_nest = (nest); \
172 __extack->miss_type = (type); \
173 } \
174} while (0)
175
45dca157
JK
176#define NL_REQ_ATTR_CHECK(extack, nest, tb, type) ({ \
177 struct nlattr **__tb = (tb); \
178 u32 __attr = (type); \
179 int __retval; \
180 \
181 __retval = !__tb[__attr]; \
182 if (__retval) \
183 NL_SET_ERR_ATTR_MISS((extack), (nest), __attr); \
184 __retval; \
185})
186
801f8746
JB
187static inline void nl_set_extack_cookie_u64(struct netlink_ext_ack *extack,
188 u64 cookie)
189{
55b474c4
MK
190 if (!extack)
191 return;
c6400e3f
AD
192 memcpy(extack->cookie, &cookie, sizeof(cookie));
193 extack->cookie_len = sizeof(cookie);
801f8746
JB
194}
195
aa9d6e0f
SH
196void netlink_kernel_release(struct sock *sk);
197int __netlink_change_ngroups(struct sock *sk, unsigned int groups);
198int netlink_change_ngroups(struct sock *sk, unsigned int groups);
199void __netlink_clear_multicast_users(struct sock *sk, unsigned int group);
200void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err,
201 const struct netlink_ext_ack *extack);
202int netlink_has_listeners(struct sock *sk, unsigned int group);
59c28058 203bool netlink_strict_get_check(struct sk_buff *skb);
aa9d6e0f
SH
204
205int netlink_unicast(struct sock *ssk, struct sk_buff *skb, __u32 portid, int nonblock);
206int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, __u32 portid,
207 __u32 group, gfp_t allocation);
aa9d6e0f
SH
208int netlink_set_err(struct sock *ssk, __u32 portid, __u32 group, int code);
209int netlink_register_notifier(struct notifier_block *nb);
210int netlink_unregister_notifier(struct notifier_block *nb);
1da177e4
LT
211
212/* finegrained unicast helpers: */
213struct sock *netlink_getsockbyfilp(struct file *filp);
9457afee 214int netlink_attachskb(struct sock *sk, struct sk_buff *skb,
c3d8d1e3 215 long *timeo, struct sock *ssk);
1da177e4 216void netlink_detachskb(struct sock *sk, struct sk_buff *skb);
7ee015e0 217int netlink_sendskb(struct sock *sk, struct sk_buff *skb);
1da177e4 218
3a36515f
PN
219static inline struct sk_buff *
220netlink_skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
221{
222 struct sk_buff *nskb;
223
224 nskb = skb_clone(skb, gfp_mask);
225 if (!nskb)
226 return NULL;
227
228 /* This is a large skb, set destructor callback to release head */
229 if (is_vmalloc_addr(skb->head))
230 nskb->destructor = skb->destructor;
231
232 return nskb;
233}
234
1da177e4
LT
235/*
236 * skb should fit one page. This choice is good for headerless malloc.
fc910a27
DM
237 * But we should limit to 8K so that userspace does not have to
238 * use enormous buffer sizes on recvmsg() calls just to avoid
239 * MSG_TRUNC when PAGE_SIZE is very large.
1da177e4 240 */
fc910a27
DM
241#if PAGE_SIZE < 8192UL
242#define NLMSG_GOODSIZE SKB_WITH_OVERHEAD(PAGE_SIZE)
243#else
244#define NLMSG_GOODSIZE SKB_WITH_OVERHEAD(8192UL)
245#endif
246
339bf98f 247#define NLMSG_DEFAULT_SIZE (NLMSG_GOODSIZE - NLMSG_HDRLEN)
1da177e4
LT
248
249
d94d9fee 250struct netlink_callback {
3a6c2b41
PM
251 struct sk_buff *skb;
252 const struct nlmsghdr *nlh;
253 int (*dump)(struct sk_buff * skb,
254 struct netlink_callback *cb);
255 int (*done)(struct netlink_callback *cb);
7175c883 256 void *data;
6dc878a8
G
257 /* the module that dump function belong to */
258 struct module *module;
4a19edb6 259 struct netlink_ext_ack *extack;
c7ac8679 260 u16 family;
22e6c58b 261 u16 answer_flags;
085c20ca 262 u32 min_dump_alloc;
670dc283 263 unsigned int prev_seq, seq;
085c20ca 264 bool strict_check;
362b87f5
JD
265 union {
266 u8 ctx[48];
267
268 /* args is deprecated. Cast a struct over ctx instead
269 * for proper type safety.
270 */
271 long args[6];
272 };
1da177e4
LT
273};
274
ec8f7d49 275#define NL_ASSERT_DUMP_CTX_FITS(type_name) \
2c7bc10d
JK
276 BUILD_BUG_ON(sizeof(type_name) > \
277 sizeof_field(struct netlink_callback, ctx))
278
d94d9fee 279struct netlink_notify {
b4b51029 280 struct net *net;
0392d099 281 u32 portid;
1da177e4
LT
282 int protocol;
283};
284
a46621a3 285struct nlmsghdr *
15e47304 286__nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, int type, int len, int flags);
1da177e4 287
80d326fa 288struct netlink_dump_control {
fc9e50f5 289 int (*start)(struct netlink_callback *);
80d326fa 290 int (*dump)(struct sk_buff *skb, struct netlink_callback *);
6dc878a8 291 int (*done)(struct netlink_callback *);
7175c883 292 void *data;
6dc878a8 293 struct module *module;
ebfe3c51 294 u32 min_dump_alloc;
80d326fa
PNA
295};
296
aa9d6e0f 297int __netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
6dc878a8
G
298 const struct nlmsghdr *nlh,
299 struct netlink_dump_control *control);
300static inline int netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
301 const struct nlmsghdr *nlh,
302 struct netlink_dump_control *control)
303{
304 if (!control->module)
305 control->module = THIS_MODULE;
306
307 return __netlink_dump_start(ssk, skb, nlh, control);
308}
1da177e4 309
bcbde0d4
DB
310struct netlink_tap {
311 struct net_device *dev;
312 struct module *module;
313 struct list_head list;
314};
315
aa9d6e0f
SH
316int netlink_add_tap(struct netlink_tap *nt);
317int netlink_remove_tap(struct netlink_tap *nt);
bcbde0d4 318
aa4cf945
EB
319bool __netlink_ns_capable(const struct netlink_skb_parms *nsp,
320 struct user_namespace *ns, int cap);
321bool netlink_ns_capable(const struct sk_buff *skb,
322 struct user_namespace *ns, int cap);
323bool netlink_capable(const struct sk_buff *skb, int cap);
324bool netlink_net_capable(const struct sk_buff *skb, int cap);
325
1da177e4 326#endif /* __LINUX_NETLINK_H */