GNU Linux-libre 4.14.290-gnu1
[releases.git] / include / linux / netlink.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __LINUX_NETLINK_H
3 #define __LINUX_NETLINK_H
4
5
6 #include <linux/capability.h>
7 #include <linux/skbuff.h>
8 #include <linux/export.h>
9 #include <net/scm.h>
10 #include <uapi/linux/netlink.h>
11
12 struct net;
13
14 static inline struct nlmsghdr *nlmsg_hdr(const struct sk_buff *skb)
15 {
16         return (struct nlmsghdr *)skb->data;
17 }
18
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 */
24 };
25
26 struct netlink_skb_parms {
27         struct scm_creds        creds;          /* Skb credentials      */
28         __u32                   portid;
29         __u32                   dst_group;
30         __u32                   flags;
31         struct sock             *sk;
32         bool                    nsid_is_set;
33         int                     nsid;
34 };
35
36 #define NETLINK_CB(skb)         (*(struct netlink_skb_parms*)&((skb)->cb))
37 #define NETLINK_CREDS(skb)      (&NETLINK_CB((skb)).creds)
38
39
40 extern void netlink_table_grab(void);
41 extern void netlink_table_ungrab(void);
42
43 #define NL_CFG_F_NONROOT_RECV   (1 << 0)
44 #define NL_CFG_F_NONROOT_SEND   (1 << 1)
45
46 /* optional Netlink kernel configuration parameters */
47 struct netlink_kernel_cfg {
48         unsigned int    groups;
49         unsigned int    flags;
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);
55 };
56
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)
62 {
63         return __netlink_kernel_create(net, unit, THIS_MODULE, cfg);
64 }
65
66 /* this can be increased when necessary - don't expose to userland */
67 #define NETLINK_MAX_COOKIE_LEN  20
68
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
74  * @cookie: cookie data to return to userspace (for success)
75  * @cookie_len: actual cookie data length
76  */
77 struct netlink_ext_ack {
78         const char *_msg;
79         const struct nlattr *bad_attr;
80         u8 cookie[NETLINK_MAX_COOKIE_LEN];
81         u8 cookie_len;
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  */
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;                 \
96 } while (0)
97
98 #define NL_SET_ERR_MSG_MOD(extack, msg)                 \
99         NL_SET_ERR_MSG((extack), KBUILD_MODNAME ": " msg)
100
101 #define NL_SET_BAD_ATTR(extack, attr) do {              \
102         if ((extack))                                   \
103                 (extack)->bad_attr = (attr);            \
104 } while (0)
105
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
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);
123
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),
130         void *filter_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);
134
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);
141
142 static inline struct sk_buff *
143 netlink_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
158 /*
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.
163  */
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
170 #define NLMSG_DEFAULT_SIZE (NLMSG_GOODSIZE - NLMSG_HDRLEN)
171
172
173 struct netlink_callback {
174         struct sk_buff          *skb;
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);
180         void                    *data;
181         /* the module that dump function belong to */
182         struct module           *module;
183         u16                     family;
184         u16                     min_dump_alloc;
185         unsigned int            prev_seq, seq;
186         long                    args[6];
187 };
188
189 struct netlink_notify {
190         struct net *net;
191         u32 portid;
192         int protocol;
193 };
194
195 struct nlmsghdr *
196 __nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, int type, int len, int flags);
197
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 *);
202         void *data;
203         struct module *module;
204         u16 min_dump_alloc;
205 };
206
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)
213 {
214         if (!control->module)
215                 control->module = THIS_MODULE;
216
217         return __netlink_dump_start(ssk, skb, nlh, control);
218 }
219
220 struct netlink_tap {
221         struct net_device *dev;
222         struct module *module;
223         struct list_head list;
224 };
225
226 extern int netlink_add_tap(struct netlink_tap *nt);
227 extern int netlink_remove_tap(struct netlink_tap *nt);
228
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);
235
236 #endif  /* __LINUX_NETLINK_H */