~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/include/linux/netlink.h

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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 void do_trace_netlink_extack(const char *msg);
 15 
 16 static inline struct nlmsghdr *nlmsg_hdr(const struct sk_buff *skb)
 17 {
 18         return (struct nlmsghdr *)skb->data;
 19 }
 20 
 21 enum netlink_skb_flags {
 22         NETLINK_SKB_DST         = 0x8,  /* Dst set in sendto or sendmsg */
 23 };
 24 
 25 struct netlink_skb_parms {
 26         struct scm_creds        creds;          /* Skb credentials      */
 27         __u32                   portid;
 28         __u32                   dst_group;
 29         __u32                   flags;
 30         struct sock             *sk;
 31         bool                    nsid_is_set;
 32         int                     nsid;
 33 };
 34 
 35 #define NETLINK_CB(skb)         (*(struct netlink_skb_parms*)&((skb)->cb))
 36 #define NETLINK_CREDS(skb)      (&NETLINK_CB((skb)).creds)
 37 
 38 
 39 void netlink_table_grab(void);
 40 void netlink_table_ungrab(void);
 41 
 42 #define NL_CFG_F_NONROOT_RECV   (1 << 0)
 43 #define NL_CFG_F_NONROOT_SEND   (1 << 1)
 44 
 45 /* optional Netlink kernel configuration parameters */
 46 struct netlink_kernel_cfg {
 47         unsigned int    groups;
 48         unsigned int    flags;
 49         void            (*input)(struct sk_buff *skb);
 50         int             (*bind)(struct net *net, int group);
 51         void            (*unbind)(struct net *net, int group);
 52         void            (*release) (struct sock *sk, unsigned long *groups);
 53 };
 54 
 55 struct sock *__netlink_kernel_create(struct net *net, int unit,
 56                                             struct module *module,
 57                                             struct netlink_kernel_cfg *cfg);
 58 static inline struct sock *
 59 netlink_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 
 64 /* this can be increased when necessary - don't expose to userland */
 65 #define NETLINK_MAX_COOKIE_LEN  20
 66 #define NETLINK_MAX_FMTMSG_LEN  80
 67 
 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
 73  * @policy: policy for a bad attribute
 74  * @miss_type: attribute type which was missing
 75  * @miss_nest: nest missing an attribute (%NULL if missing top level attr)
 76  * @cookie: cookie data to return to userspace (for success)
 77  * @cookie_len: actual cookie data length
 78  * @_msg_buf: output buffer for formatted message strings - don't access
 79  *      directly, use %NL_SET_ERR_MSG_FMT
 80  */
 81 struct netlink_ext_ack {
 82         const char *_msg;
 83         const struct nlattr *bad_attr;
 84         const struct nla_policy *policy;
 85         const struct nlattr *miss_nest;
 86         u16 miss_type;
 87         u8 cookie[NETLINK_MAX_COOKIE_LEN];
 88         u8 cookie_len;
 89         char _msg_buf[NETLINK_MAX_FMTMSG_LEN];
 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.
 95  * If string formatting is needed use NL_SET_ERR_MSG_FMT.
 96  */
 97 #define NL_SET_ERR_MSG(extack, msg) do {                \
 98         static const char __msg[] = msg;                \
 99         struct netlink_ext_ack *__extack = (extack);    \
100                                                         \
101         do_trace_netlink_extack(__msg);                 \
102                                                         \
103         if (__extack)                                   \
104                 __extack->_msg = __msg;                 \
105 } while (0)
106 
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 
126 #define NL_SET_ERR_MSG_MOD(extack, msg)                 \
127         NL_SET_ERR_MSG((extack), KBUILD_MODNAME ": " msg)
128 
129 #define NL_SET_ERR_MSG_FMT_MOD(extack, fmt, args...)    \
130         NL_SET_ERR_MSG_FMT((extack), KBUILD_MODNAME ": " fmt, ##args)
131 
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 
142 #define NL_SET_BAD_ATTR_POLICY(extack, attr, pol) do {  \
143         if ((extack)) {                                 \
144                 (extack)->bad_attr = (attr);            \
145                 (extack)->policy = (pol);               \
146         }                                               \
147 } while (0)
148 
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                                                                 \
155         do_trace_netlink_extack(__msg);                         \
156                                                                 \
157         if (__extack) {                                         \
158                 __extack->_msg = __msg;                         \
159                 __extack->bad_attr = (attr);                    \
160                 __extack->policy = (pol);                       \
161         }                                                       \
162 } while (0)
163 
164 #define NL_SET_ERR_MSG_ATTR_POL_FMT(extack, attr, pol, fmt, args...) do {       \
165         struct netlink_ext_ack *__extack = (extack);                            \
166                                                                                 \
167         if (!__extack)                                                          \
168                 break;                                                          \
169                                                                                 \
170         if (snprintf(__extack->_msg_buf, NETLINK_MAX_FMTMSG_LEN,                \
171                      "%s" fmt "%s", "", ##args, "") >=                          \
172             NETLINK_MAX_FMTMSG_LEN)                                             \
173                 net_warn_ratelimited("%s" fmt "%s", "truncated extack: ",       \
174                                      ##args, "\n");                             \
175                                                                                 \
176         do_trace_netlink_extack(__extack->_msg_buf);                            \
177                                                                                 \
178         __extack->_msg = __extack->_msg_buf;                                    \
179         __extack->bad_attr = (attr);                                            \
180         __extack->policy = (pol);                                               \
181 } while (0)
182 
183 #define NL_SET_ERR_MSG_ATTR(extack, attr, msg)          \
184         NL_SET_ERR_MSG_ATTR_POL(extack, attr, NULL, msg)
185 
186 #define NL_SET_ERR_MSG_ATTR_FMT(extack, attr, msg, args...) \
187         NL_SET_ERR_MSG_ATTR_POL_FMT(extack, attr, NULL, msg, ##args)
188 
189 #define NL_SET_ERR_ATTR_MISS(extack, nest, type)  do {  \
190         struct netlink_ext_ack *__extack = (extack);    \
191                                                         \
192         if (__extack) {                                 \
193                 __extack->miss_nest = (nest);           \
194                 __extack->miss_type = (type);           \
195         }                                               \
196 } while (0)
197 
198 #define NL_REQ_ATTR_CHECK(extack, nest, tb, type) ({            \
199         struct nlattr **__tb = (tb);                            \
200         u32 __attr = (type);                                    \
201         int __retval;                                           \
202                                                                 \
203         __retval = !__tb[__attr];                               \
204         if (__retval)                                           \
205                 NL_SET_ERR_ATTR_MISS((extack), (nest), __attr); \
206         __retval;                                               \
207 })
208 
209 static inline void nl_set_extack_cookie_u64(struct netlink_ext_ack *extack,
210                                             u64 cookie)
211 {
212         if (!extack)
213                 return;
214         memcpy(extack->cookie, &cookie, sizeof(cookie));
215         extack->cookie_len = sizeof(cookie);
216 }
217 
218 void netlink_kernel_release(struct sock *sk);
219 int __netlink_change_ngroups(struct sock *sk, unsigned int groups);
220 int netlink_change_ngroups(struct sock *sk, unsigned int groups);
221 void __netlink_clear_multicast_users(struct sock *sk, unsigned int group);
222 void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err,
223                  const struct netlink_ext_ack *extack);
224 int netlink_has_listeners(struct sock *sk, unsigned int group);
225 bool netlink_strict_get_check(struct sk_buff *skb);
226 
227 int netlink_unicast(struct sock *ssk, struct sk_buff *skb, __u32 portid, int nonblock);
228 int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, __u32 portid,
229                       __u32 group, gfp_t allocation);
230 
231 typedef int (*netlink_filter_fn)(struct sock *dsk, struct sk_buff *skb, void *data);
232 
233 int netlink_broadcast_filtered(struct sock *ssk, struct sk_buff *skb,
234                                __u32 portid, __u32 group, gfp_t allocation,
235                                netlink_filter_fn filter,
236                                void *filter_data);
237 int netlink_set_err(struct sock *ssk, __u32 portid, __u32 group, int code);
238 int netlink_register_notifier(struct notifier_block *nb);
239 int netlink_unregister_notifier(struct notifier_block *nb);
240 
241 /* finegrained unicast helpers: */
242 struct sock *netlink_getsockbyfilp(struct file *filp);
243 int netlink_attachskb(struct sock *sk, struct sk_buff *skb,
244                       long *timeo, struct sock *ssk);
245 void netlink_detachskb(struct sock *sk, struct sk_buff *skb);
246 int netlink_sendskb(struct sock *sk, struct sk_buff *skb);
247 
248 static inline struct sk_buff *
249 netlink_skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
250 {
251         struct sk_buff *nskb;
252 
253         nskb = skb_clone(skb, gfp_mask);
254         if (!nskb)
255                 return NULL;
256 
257         /* This is a large skb, set destructor callback to release head */
258         if (is_vmalloc_addr(skb->head))
259                 nskb->destructor = skb->destructor;
260 
261         return nskb;
262 }
263 
264 /*
265  *      skb should fit one page. This choice is good for headerless malloc.
266  *      But we should limit to 8K so that userspace does not have to
267  *      use enormous buffer sizes on recvmsg() calls just to avoid
268  *      MSG_TRUNC when PAGE_SIZE is very large.
269  */
270 #if PAGE_SIZE < 8192UL
271 #define NLMSG_GOODSIZE  SKB_WITH_OVERHEAD(PAGE_SIZE)
272 #else
273 #define NLMSG_GOODSIZE  SKB_WITH_OVERHEAD(8192UL)
274 #endif
275 
276 #define NLMSG_DEFAULT_SIZE (NLMSG_GOODSIZE - NLMSG_HDRLEN)
277 
278 
279 struct netlink_callback {
280         struct sk_buff          *skb;
281         const struct nlmsghdr   *nlh;
282         int                     (*dump)(struct sk_buff * skb,
283                                         struct netlink_callback *cb);
284         int                     (*done)(struct netlink_callback *cb);
285         void                    *data;
286         /* the module that dump function belong to */
287         struct module           *module;
288         struct netlink_ext_ack  *extack;
289         u16                     family;
290         u16                     answer_flags;
291         u32                     min_dump_alloc;
292         unsigned int            prev_seq, seq;
293         int                     flags;
294         bool                    strict_check;
295         union {
296                 u8              ctx[48];
297 
298                 /* args is deprecated. Cast a struct over ctx instead
299                  * for proper type safety.
300                  */
301                 long            args[6];
302         };
303 };
304 
305 #define NL_ASSERT_DUMP_CTX_FITS(type_name)                              \
306         BUILD_BUG_ON(sizeof(type_name) >                                \
307                      sizeof_field(struct netlink_callback, ctx))
308 
309 struct netlink_notify {
310         struct net *net;
311         u32 portid;
312         int protocol;
313 };
314 
315 struct nlmsghdr *
316 __nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, int type, int len, int flags);
317 
318 struct netlink_dump_control {
319         int (*start)(struct netlink_callback *);
320         int (*dump)(struct sk_buff *skb, struct netlink_callback *);
321         int (*done)(struct netlink_callback *);
322         struct netlink_ext_ack *extack;
323         void *data;
324         struct module *module;
325         u32 min_dump_alloc;
326         int flags;
327 };
328 
329 int __netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
330                                 const struct nlmsghdr *nlh,
331                                 struct netlink_dump_control *control);
332 static inline int netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
333                                      const struct nlmsghdr *nlh,
334                                      struct netlink_dump_control *control)
335 {
336         if (!control->module)
337                 control->module = THIS_MODULE;
338 
339         return __netlink_dump_start(ssk, skb, nlh, control);
340 }
341 
342 struct netlink_tap {
343         struct net_device *dev;
344         struct module *module;
345         struct list_head list;
346 };
347 
348 int netlink_add_tap(struct netlink_tap *nt);
349 int netlink_remove_tap(struct netlink_tap *nt);
350 
351 bool __netlink_ns_capable(const struct netlink_skb_parms *nsp,
352                           struct user_namespace *ns, int cap);
353 bool netlink_ns_capable(const struct sk_buff *skb,
354                         struct user_namespace *ns, int cap);
355 bool netlink_capable(const struct sk_buff *skb, int cap);
356 bool netlink_net_capable(const struct sk_buff *skb, int cap);
357 struct sk_buff *netlink_alloc_large_skb(unsigned int size, int broadcast);
358 
359 #endif  /* __LINUX_NETLINK_H */
360 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php