Merge tag 'platform-drivers-x86-v5.18-1' of git://git.kernel.org/pub/scm/linux/kernel...
[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);
da12c90e 53 bool (*compare)(struct net *net, struct sock *sk);
a31f2d17
PNA
54};
55
aa9d6e0f 56struct sock *__netlink_kernel_create(struct net *net, int unit,
9f00d977
PNA
57 struct module *module,
58 struct netlink_kernel_cfg *cfg);
59static inline struct sock *
60netlink_kernel_create(struct net *net, int unit, struct netlink_kernel_cfg *cfg)
61{
62 return __netlink_kernel_create(net, unit, THIS_MODULE, cfg);
63}
64
ba0dc5f6
JB
65/* this can be increased when necessary - don't expose to userland */
66#define NETLINK_MAX_COOKIE_LEN 20
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
ba0dc5f6
JB
74 * @cookie: cookie data to return to userspace (for success)
75 * @cookie_len: actual cookie data length
2d4bc933
JB
76 */
77struct netlink_ext_ack {
78 const char *_msg;
79 const struct nlattr *bad_attr;
44f3625b 80 const struct nla_policy *policy;
ba0dc5f6
JB
81 u8 cookie[NETLINK_MAX_COOKIE_LEN];
82 u8 cookie_len;
2d4bc933
JB
83};
84
85/* Always use this macro, this allows later putting the
86 * message into a separate section or such for things
87 * like translation or listing all possible messages.
88 * Currently string formatting is not supported (due
89 * to the lack of an output buffer.)
90 */
4d463c4d 91#define NL_SET_ERR_MSG(extack, msg) do { \
6311b7ce 92 static const char __msg[] = msg; \
4d463c4d
DB
93 struct netlink_ext_ack *__extack = (extack); \
94 \
7e3ce05e
MRL
95 do_trace_netlink_extack(__msg); \
96 \
4d463c4d
DB
97 if (__extack) \
98 __extack->_msg = __msg; \
2d4bc933
JB
99} while (0)
100
4d463c4d
DB
101#define NL_SET_ERR_MSG_MOD(extack, msg) \
102 NL_SET_ERR_MSG((extack), KBUILD_MODNAME ": " msg)
45d9b378 103
44f3625b
JB
104#define NL_SET_BAD_ATTR_POLICY(extack, attr, pol) do { \
105 if ((extack)) { \
c3ab2b4e 106 (extack)->bad_attr = (attr); \
44f3625b
JB
107 (extack)->policy = (pol); \
108 } \
c3ab2b4e
DA
109} while (0)
110
44f3625b
JB
111#define NL_SET_BAD_ATTR(extack, attr) NL_SET_BAD_ATTR_POLICY(extack, attr, NULL)
112
113#define NL_SET_ERR_MSG_ATTR_POL(extack, attr, pol, msg) do { \
114 static const char __msg[] = msg; \
115 struct netlink_ext_ack *__extack = (extack); \
116 \
7e3ce05e
MRL
117 do_trace_netlink_extack(__msg); \
118 \
44f3625b
JB
119 if (__extack) { \
120 __extack->_msg = __msg; \
121 __extack->bad_attr = (attr); \
122 __extack->policy = (pol); \
123 } \
9ae28727
DA
124} while (0)
125
44f3625b
JB
126#define NL_SET_ERR_MSG_ATTR(extack, attr, msg) \
127 NL_SET_ERR_MSG_ATTR_POL(extack, attr, NULL, msg)
128
801f8746
JB
129static inline void nl_set_extack_cookie_u64(struct netlink_ext_ack *extack,
130 u64 cookie)
131{
55b474c4
MK
132 if (!extack)
133 return;
c6400e3f
AD
134 memcpy(extack->cookie, &cookie, sizeof(cookie));
135 extack->cookie_len = sizeof(cookie);
801f8746
JB
136}
137
aa9d6e0f
SH
138void netlink_kernel_release(struct sock *sk);
139int __netlink_change_ngroups(struct sock *sk, unsigned int groups);
140int netlink_change_ngroups(struct sock *sk, unsigned int groups);
141void __netlink_clear_multicast_users(struct sock *sk, unsigned int group);
142void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err,
143 const struct netlink_ext_ack *extack);
144int netlink_has_listeners(struct sock *sk, unsigned int group);
59c28058 145bool netlink_strict_get_check(struct sk_buff *skb);
aa9d6e0f
SH
146
147int netlink_unicast(struct sock *ssk, struct sk_buff *skb, __u32 portid, int nonblock);
148int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, __u32 portid,
149 __u32 group, gfp_t allocation);
aa9d6e0f
SH
150int netlink_set_err(struct sock *ssk, __u32 portid, __u32 group, int code);
151int netlink_register_notifier(struct notifier_block *nb);
152int netlink_unregister_notifier(struct notifier_block *nb);
1da177e4
LT
153
154/* finegrained unicast helpers: */
155struct sock *netlink_getsockbyfilp(struct file *filp);
9457afee 156int netlink_attachskb(struct sock *sk, struct sk_buff *skb,
c3d8d1e3 157 long *timeo, struct sock *ssk);
1da177e4 158void netlink_detachskb(struct sock *sk, struct sk_buff *skb);
7ee015e0 159int netlink_sendskb(struct sock *sk, struct sk_buff *skb);
1da177e4 160
3a36515f
PN
161static inline struct sk_buff *
162netlink_skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
163{
164 struct sk_buff *nskb;
165
166 nskb = skb_clone(skb, gfp_mask);
167 if (!nskb)
168 return NULL;
169
170 /* This is a large skb, set destructor callback to release head */
171 if (is_vmalloc_addr(skb->head))
172 nskb->destructor = skb->destructor;
173
174 return nskb;
175}
176
1da177e4
LT
177/*
178 * skb should fit one page. This choice is good for headerless malloc.
fc910a27
DM
179 * But we should limit to 8K so that userspace does not have to
180 * use enormous buffer sizes on recvmsg() calls just to avoid
181 * MSG_TRUNC when PAGE_SIZE is very large.
1da177e4 182 */
fc910a27
DM
183#if PAGE_SIZE < 8192UL
184#define NLMSG_GOODSIZE SKB_WITH_OVERHEAD(PAGE_SIZE)
185#else
186#define NLMSG_GOODSIZE SKB_WITH_OVERHEAD(8192UL)
187#endif
188
339bf98f 189#define NLMSG_DEFAULT_SIZE (NLMSG_GOODSIZE - NLMSG_HDRLEN)
1da177e4
LT
190
191
d94d9fee 192struct netlink_callback {
3a6c2b41
PM
193 struct sk_buff *skb;
194 const struct nlmsghdr *nlh;
195 int (*dump)(struct sk_buff * skb,
196 struct netlink_callback *cb);
197 int (*done)(struct netlink_callback *cb);
7175c883 198 void *data;
6dc878a8
G
199 /* the module that dump function belong to */
200 struct module *module;
4a19edb6 201 struct netlink_ext_ack *extack;
c7ac8679 202 u16 family;
22e6c58b 203 u16 answer_flags;
085c20ca 204 u32 min_dump_alloc;
670dc283 205 unsigned int prev_seq, seq;
085c20ca 206 bool strict_check;
362b87f5
JD
207 union {
208 u8 ctx[48];
209
210 /* args is deprecated. Cast a struct over ctx instead
211 * for proper type safety.
212 */
213 long args[6];
214 };
1da177e4
LT
215};
216
d94d9fee 217struct netlink_notify {
b4b51029 218 struct net *net;
0392d099 219 u32 portid;
1da177e4
LT
220 int protocol;
221};
222
a46621a3 223struct nlmsghdr *
15e47304 224__nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, int type, int len, int flags);
1da177e4 225
80d326fa 226struct netlink_dump_control {
fc9e50f5 227 int (*start)(struct netlink_callback *);
80d326fa 228 int (*dump)(struct sk_buff *skb, struct netlink_callback *);
6dc878a8 229 int (*done)(struct netlink_callback *);
7175c883 230 void *data;
6dc878a8 231 struct module *module;
ebfe3c51 232 u32 min_dump_alloc;
80d326fa
PNA
233};
234
aa9d6e0f 235int __netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
6dc878a8
G
236 const struct nlmsghdr *nlh,
237 struct netlink_dump_control *control);
238static inline int netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
239 const struct nlmsghdr *nlh,
240 struct netlink_dump_control *control)
241{
242 if (!control->module)
243 control->module = THIS_MODULE;
244
245 return __netlink_dump_start(ssk, skb, nlh, control);
246}
1da177e4 247
bcbde0d4
DB
248struct netlink_tap {
249 struct net_device *dev;
250 struct module *module;
251 struct list_head list;
252};
253
aa9d6e0f
SH
254int netlink_add_tap(struct netlink_tap *nt);
255int netlink_remove_tap(struct netlink_tap *nt);
bcbde0d4 256
aa4cf945
EB
257bool __netlink_ns_capable(const struct netlink_skb_parms *nsp,
258 struct user_namespace *ns, int cap);
259bool netlink_ns_capable(const struct sk_buff *skb,
260 struct user_namespace *ns, int cap);
261bool netlink_capable(const struct sk_buff *skb, int cap);
262bool netlink_net_capable(const struct sk_buff *skb, int cap);
263
1da177e4 264#endif /* __LINUX_NETLINK_H */