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 static inline struct nlmsghdr
*nlmsg_hdr(const struct sk_buff
*skb
)
16 return (struct nlmsghdr
*)skb
->data
;
19 enum netlink_skb_flags
{
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 */
26 struct netlink_skb_parms
{
27 struct scm_creds creds
; /* Skb credentials */
36 #define NETLINK_CB(skb) (*(struct netlink_skb_parms*)&((skb)->cb))
37 #define NETLINK_CREDS(skb) (&NETLINK_CB((skb)).creds)
40 extern void netlink_table_grab(void);
41 extern void netlink_table_ungrab(void);
43 #define NL_CFG_F_NONROOT_RECV (1 << 0)
44 #define NL_CFG_F_NONROOT_SEND (1 << 1)
46 /* optional Netlink kernel configuration parameters */
47 struct netlink_kernel_cfg
{
50 void (*input
)(struct sk_buff
*skb
);
51 struct mutex
*cb_mutex
;
52 int (*bind
)(struct net
*net
, int group
);
53 void (*unbind
)(struct net
*net
, int group
);
54 bool (*compare
)(struct net
*net
, struct sock
*sk
);
57 extern struct sock
*__netlink_kernel_create(struct net
*net
, int unit
,
58 struct module
*module
,
59 struct netlink_kernel_cfg
*cfg
);
60 static inline struct sock
*
61 netlink_kernel_create(struct net
*net
, int unit
, struct netlink_kernel_cfg
*cfg
)
63 return __netlink_kernel_create(net
, unit
, THIS_MODULE
, cfg
);
66 /* this can be increased when necessary - don't expose to userland */
67 #define NETLINK_MAX_COOKIE_LEN 20
70 * struct netlink_ext_ack - netlink extended ACK report struct
71 * @_msg: message string to report - don't access directly, use
73 * @bad_attr: attribute with error
74 * @cookie: cookie data to return to userspace (for success)
75 * @cookie_len: actual cookie data length
77 struct netlink_ext_ack
{
79 const struct nlattr
*bad_attr
;
80 u8 cookie
[NETLINK_MAX_COOKIE_LEN
];
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.)
90 #define NL_SET_ERR_MSG(extack, msg) do { \
91 static const char __msg[] = (msg); \
92 struct netlink_ext_ack *__extack = (extack); \
95 __extack->_msg = __msg; \
98 #define NL_SET_ERR_MSG_MOD(extack, msg) \
99 NL_SET_ERR_MSG((extack), KBUILD_MODNAME ": " msg)
101 #define NL_SET_BAD_ATTR(extack, attr) do { \
103 (extack)->bad_attr = (attr); \
106 #define NL_SET_ERR_MSG_ATTR(extack, attr, msg) do { \
107 static const char __msg[] = (msg); \
108 struct netlink_ext_ack *__extack = (extack); \
111 __extack->_msg = __msg; \
112 __extack->bad_attr = (attr); \
116 extern void netlink_kernel_release(struct sock
*sk
);
117 extern int __netlink_change_ngroups(struct sock
*sk
, unsigned int groups
);
118 extern int netlink_change_ngroups(struct sock
*sk
, unsigned int groups
);
119 extern void __netlink_clear_multicast_users(struct sock
*sk
, unsigned int group
);
120 extern void netlink_ack(struct sk_buff
*in_skb
, struct nlmsghdr
*nlh
, int err
,
121 const struct netlink_ext_ack
*extack
);
122 extern int netlink_has_listeners(struct sock
*sk
, unsigned int group
);
124 extern int netlink_unicast(struct sock
*ssk
, struct sk_buff
*skb
, __u32 portid
, int nonblock
);
125 extern int netlink_broadcast(struct sock
*ssk
, struct sk_buff
*skb
, __u32 portid
,
126 __u32 group
, gfp_t allocation
);
127 extern int netlink_broadcast_filtered(struct sock
*ssk
, struct sk_buff
*skb
,
128 __u32 portid
, __u32 group
, gfp_t allocation
,
129 int (*filter
)(struct sock
*dsk
, struct sk_buff
*skb
, void *data
),
131 extern int netlink_set_err(struct sock
*ssk
, __u32 portid
, __u32 group
, int code
);
132 extern int netlink_register_notifier(struct notifier_block
*nb
);
133 extern int netlink_unregister_notifier(struct notifier_block
*nb
);
135 /* finegrained unicast helpers: */
136 struct sock
*netlink_getsockbyfilp(struct file
*filp
);
137 int netlink_attachskb(struct sock
*sk
, struct sk_buff
*skb
,
138 long *timeo
, struct sock
*ssk
);
139 void netlink_detachskb(struct sock
*sk
, struct sk_buff
*skb
);
140 int netlink_sendskb(struct sock
*sk
, struct sk_buff
*skb
);
142 static inline struct sk_buff
*
143 netlink_skb_clone(struct sk_buff
*skb
, gfp_t gfp_mask
)
145 struct sk_buff
*nskb
;
147 nskb
= skb_clone(skb
, gfp_mask
);
151 /* This is a large skb, set destructor callback to release head */
152 if (is_vmalloc_addr(skb
->head
))
153 nskb
->destructor
= skb
->destructor
;
159 * skb should fit one page. This choice is good for headerless malloc.
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.
164 #if PAGE_SIZE < 8192UL
165 #define NLMSG_GOODSIZE SKB_WITH_OVERHEAD(PAGE_SIZE)
167 #define NLMSG_GOODSIZE SKB_WITH_OVERHEAD(8192UL)
170 #define NLMSG_DEFAULT_SIZE (NLMSG_GOODSIZE - NLMSG_HDRLEN)
173 struct netlink_callback
{
175 const struct nlmsghdr
*nlh
;
176 int (*start
)(struct netlink_callback
*);
177 int (*dump
)(struct sk_buff
* skb
,
178 struct netlink_callback
*cb
);
179 int (*done
)(struct netlink_callback
*cb
);
181 /* the module that dump function belong to */
182 struct module
*module
;
185 unsigned int prev_seq
, seq
;
189 struct netlink_notify
{
196 __nlmsg_put(struct sk_buff
*skb
, u32 portid
, u32 seq
, int type
, int len
, int flags
);
198 struct netlink_dump_control
{
199 int (*start
)(struct netlink_callback
*);
200 int (*dump
)(struct sk_buff
*skb
, struct netlink_callback
*);
201 int (*done
)(struct netlink_callback
*);
203 struct module
*module
;
207 extern int __netlink_dump_start(struct sock
*ssk
, struct sk_buff
*skb
,
208 const struct nlmsghdr
*nlh
,
209 struct netlink_dump_control
*control
);
210 static inline int netlink_dump_start(struct sock
*ssk
, struct sk_buff
*skb
,
211 const struct nlmsghdr
*nlh
,
212 struct netlink_dump_control
*control
)
214 if (!control
->module
)
215 control
->module
= THIS_MODULE
;
217 return __netlink_dump_start(ssk
, skb
, nlh
, control
);
221 struct net_device
*dev
;
222 struct module
*module
;
223 struct list_head list
;
226 extern int netlink_add_tap(struct netlink_tap
*nt
);
227 extern int netlink_remove_tap(struct netlink_tap
*nt
);
229 bool __netlink_ns_capable(const struct netlink_skb_parms
*nsp
,
230 struct user_namespace
*ns
, int cap
);
231 bool netlink_ns_capable(const struct sk_buff
*skb
,
232 struct user_namespace
*ns
, int cap
);
233 bool netlink_capable(const struct sk_buff
*skb
, int cap
);
234 bool netlink_net_capable(const struct sk_buff
*skb
, int cap
);
236 #endif /* __LINUX_NETLINK_H */