]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - include/linux/netlink.h
Merge remote-tracking branches 'asoc/topic/ac97', 'asoc/topic/ac97-mfd', 'asoc/topic...
[mirror_ubuntu-focal-kernel.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
b529ccf2
ACM
14static inline struct nlmsghdr *nlmsg_hdr(const struct sk_buff *skb)
15{
16 return (struct nlmsghdr *)skb->data;
17}
18
9652e931 19enum netlink_skb_flags {
2d7a85f4
EB
20 NETLINK_SKB_MMAPED = 0x1, /* Packet data is mmaped */
21 NETLINK_SKB_TX = 0x2, /* Packet was sent by userspace */
22 NETLINK_SKB_DELIVERED = 0x4, /* Packet was delivered */
23 NETLINK_SKB_DST = 0x8, /* Dst set in sendto or sendmsg */
9652e931
PM
24};
25
d94d9fee 26struct netlink_skb_parms {
dbe9a417 27 struct scm_creds creds; /* Skb credentials */
15e47304 28 __u32 portid;
d629b836 29 __u32 dst_group;
9652e931 30 __u32 flags;
e32123e5 31 struct sock *sk;
59324cf3
ND
32 bool nsid_is_set;
33 int nsid;
1da177e4
LT
34};
35
36#define NETLINK_CB(skb) (*(struct netlink_skb_parms*)&((skb)->cb))
37#define NETLINK_CREDS(skb) (&NETLINK_CB((skb)).creds)
38
39
d136f1bd
JB
40extern void netlink_table_grab(void);
41extern void netlink_table_ungrab(void);
42
9785e10a
PNA
43#define NL_CFG_F_NONROOT_RECV (1 << 0)
44#define NL_CFG_F_NONROOT_SEND (1 << 1)
45
a31f2d17
PNA
46/* optional Netlink kernel configuration parameters */
47struct netlink_kernel_cfg {
48 unsigned int groups;
c9d2ea96 49 unsigned int flags;
a31f2d17
PNA
50 void (*input)(struct sk_buff *skb);
51 struct mutex *cb_mutex;
023e2cfa
JB
52 int (*bind)(struct net *net, int group);
53 void (*unbind)(struct net *net, int group);
da12c90e 54 bool (*compare)(struct net *net, struct sock *sk);
a31f2d17
PNA
55};
56
9f00d977
PNA
57extern struct sock *__netlink_kernel_create(struct net *net, int unit,
58 struct module *module,
59 struct netlink_kernel_cfg *cfg);
60static inline struct sock *
61netlink_kernel_create(struct net *net, int unit, struct netlink_kernel_cfg *cfg)
62{
63 return __netlink_kernel_create(net, unit, THIS_MODULE, cfg);
64}
65
ba0dc5f6
JB
66/* this can be increased when necessary - don't expose to userland */
67#define NETLINK_MAX_COOKIE_LEN 20
68
2d4bc933
JB
69/**
70 * struct netlink_ext_ack - netlink extended ACK report struct
71 * @_msg: message string to report - don't access directly, use
72 * %NL_SET_ERR_MSG
73 * @bad_attr: attribute with error
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;
ba0dc5f6
JB
80 u8 cookie[NETLINK_MAX_COOKIE_LEN];
81 u8 cookie_len;
2d4bc933
JB
82};
83
84/* Always use this macro, this allows later putting the
85 * message into a separate section or such for things
86 * like translation or listing all possible messages.
87 * Currently string formatting is not supported (due
88 * to the lack of an output buffer.)
89 */
4d463c4d
DB
90#define NL_SET_ERR_MSG(extack, msg) do { \
91 static const char __msg[] = (msg); \
92 struct netlink_ext_ack *__extack = (extack); \
93 \
94 if (__extack) \
95 __extack->_msg = __msg; \
2d4bc933
JB
96} while (0)
97
4d463c4d
DB
98#define NL_SET_ERR_MSG_MOD(extack, msg) \
99 NL_SET_ERR_MSG((extack), KBUILD_MODNAME ": " msg)
45d9b378 100
c3ab2b4e
DA
101#define NL_SET_BAD_ATTR(extack, attr) do { \
102 if ((extack)) \
103 (extack)->bad_attr = (attr); \
104} while (0)
105
9ae28727
DA
106#define NL_SET_ERR_MSG_ATTR(extack, attr, msg) do { \
107 static const char __msg[] = (msg); \
108 struct netlink_ext_ack *__extack = (extack); \
109 \
110 if (__extack) { \
111 __extack->_msg = __msg; \
112 __extack->bad_attr = (attr); \
113 } \
114} while (0)
115
b7c6ba6e 116extern void netlink_kernel_release(struct sock *sk);
d136f1bd 117extern int __netlink_change_ngroups(struct sock *sk, unsigned int groups);
b4ff4f04 118extern int netlink_change_ngroups(struct sock *sk, unsigned int groups);
b8273570 119extern void __netlink_clear_multicast_users(struct sock *sk, unsigned int group);
2d4bc933
JB
120extern void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err,
121 const struct netlink_ext_ack *extack);
4277a083 122extern int netlink_has_listeners(struct sock *sk, unsigned int group);
6bb0fef4 123
15e47304
EB
124extern int netlink_unicast(struct sock *ssk, struct sk_buff *skb, __u32 portid, int nonblock);
125extern int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, __u32 portid,
dd0fc66f 126 __u32 group, gfp_t allocation);
910a7e90 127extern int netlink_broadcast_filtered(struct sock *ssk, struct sk_buff *skb,
15e47304 128 __u32 portid, __u32 group, gfp_t allocation,
910a7e90
EB
129 int (*filter)(struct sock *dsk, struct sk_buff *skb, void *data),
130 void *filter_data);
15e47304 131extern int netlink_set_err(struct sock *ssk, __u32 portid, __u32 group, int code);
1da177e4
LT
132extern int netlink_register_notifier(struct notifier_block *nb);
133extern int netlink_unregister_notifier(struct notifier_block *nb);
134
135/* finegrained unicast helpers: */
136struct sock *netlink_getsockbyfilp(struct file *filp);
9457afee 137int netlink_attachskb(struct sock *sk, struct sk_buff *skb,
c3d8d1e3 138 long *timeo, struct sock *ssk);
1da177e4 139void netlink_detachskb(struct sock *sk, struct sk_buff *skb);
7ee015e0 140int netlink_sendskb(struct sock *sk, struct sk_buff *skb);
1da177e4 141
3a36515f
PN
142static inline struct sk_buff *
143netlink_skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
144{
145 struct sk_buff *nskb;
146
147 nskb = skb_clone(skb, gfp_mask);
148 if (!nskb)
149 return NULL;
150
151 /* This is a large skb, set destructor callback to release head */
152 if (is_vmalloc_addr(skb->head))
153 nskb->destructor = skb->destructor;
154
155 return nskb;
156}
157
1da177e4
LT
158/*
159 * skb should fit one page. This choice is good for headerless malloc.
fc910a27
DM
160 * But we should limit to 8K so that userspace does not have to
161 * use enormous buffer sizes on recvmsg() calls just to avoid
162 * MSG_TRUNC when PAGE_SIZE is very large.
1da177e4 163 */
fc910a27
DM
164#if PAGE_SIZE < 8192UL
165#define NLMSG_GOODSIZE SKB_WITH_OVERHEAD(PAGE_SIZE)
166#else
167#define NLMSG_GOODSIZE SKB_WITH_OVERHEAD(8192UL)
168#endif
169
339bf98f 170#define NLMSG_DEFAULT_SIZE (NLMSG_GOODSIZE - NLMSG_HDRLEN)
1da177e4
LT
171
172
d94d9fee 173struct netlink_callback {
3a6c2b41
PM
174 struct sk_buff *skb;
175 const struct nlmsghdr *nlh;
fc9e50f5 176 int (*start)(struct netlink_callback *);
3a6c2b41
PM
177 int (*dump)(struct sk_buff * skb,
178 struct netlink_callback *cb);
179 int (*done)(struct netlink_callback *cb);
7175c883 180 void *data;
6dc878a8
G
181 /* the module that dump function belong to */
182 struct module *module;
c7ac8679
GR
183 u16 family;
184 u16 min_dump_alloc;
670dc283 185 unsigned int prev_seq, seq;
3a6c2b41 186 long args[6];
1da177e4
LT
187};
188
d94d9fee 189struct netlink_notify {
b4b51029 190 struct net *net;
0392d099 191 u32 portid;
1da177e4
LT
192 int protocol;
193};
194
a46621a3 195struct nlmsghdr *
15e47304 196__nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, int type, int len, int flags);
1da177e4 197
80d326fa 198struct netlink_dump_control {
fc9e50f5 199 int (*start)(struct netlink_callback *);
80d326fa 200 int (*dump)(struct sk_buff *skb, struct netlink_callback *);
6dc878a8 201 int (*done)(struct netlink_callback *);
7175c883 202 void *data;
6dc878a8 203 struct module *module;
80d326fa
PNA
204 u16 min_dump_alloc;
205};
206
6dc878a8
G
207extern int __netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
208 const struct nlmsghdr *nlh,
209 struct netlink_dump_control *control);
210static inline int netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
211 const struct nlmsghdr *nlh,
212 struct netlink_dump_control *control)
213{
214 if (!control->module)
215 control->module = THIS_MODULE;
216
217 return __netlink_dump_start(ssk, skb, nlh, control);
218}
1da177e4 219
bcbde0d4
DB
220struct netlink_tap {
221 struct net_device *dev;
222 struct module *module;
223 struct list_head list;
224};
225
226extern int netlink_add_tap(struct netlink_tap *nt);
bcbde0d4
DB
227extern int netlink_remove_tap(struct netlink_tap *nt);
228
aa4cf945
EB
229bool __netlink_ns_capable(const struct netlink_skb_parms *nsp,
230 struct user_namespace *ns, int cap);
231bool netlink_ns_capable(const struct sk_buff *skb,
232 struct user_namespace *ns, int cap);
233bool netlink_capable(const struct sk_buff *skb, int cap);
234bool netlink_net_capable(const struct sk_buff *skb, int cap);
235
1da177e4 236#endif /* __LINUX_NETLINK_H */