blob: f3075d6c7e8229c999ab650537f1e3b11e1f457b [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002#ifndef __LINUX_NETLINK_H
3#define __LINUX_NETLINK_H
4
Linus Torvalds1da177e2005-04-16 15:20:36 -07005
6#include <linux/capability.h>
7#include <linux/skbuff.h>
Pablo Neira Ayusoabb17e62012-09-21 09:35:38 +00008#include <linux/export.h>
Eric W. Biedermandbe9a412012-09-06 18:20:01 +00009#include <net/scm.h>
David Howells607ca462012-10-13 10:46:48 +010010#include <uapi/linux/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
Ollie Wild56b49f42010-09-22 05:54:54 +000012struct net;
13
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -070014static inline struct nlmsghdr *nlmsg_hdr(const struct sk_buff *skb)
15{
16 return (struct nlmsghdr *)skb->data;
17}
18
Patrick McHardy9652e932013-04-17 06:47:02 +000019enum netlink_skb_flags {
Eric W. Biederman2d7a85f2014-05-30 11:04:00 -070020 NETLINK_SKB_DST = 0x8, /* Dst set in sendto or sendmsg */
Patrick McHardy9652e932013-04-17 06:47:02 +000021};
22
Eric Dumazetd94d9fe2009-11-04 09:50:58 -080023struct netlink_skb_parms {
Eric W. Biedermandbe9a412012-09-06 18:20:01 +000024 struct scm_creds creds; /* Skb credentials */
Eric W. Biederman15e47302012-09-07 20:12:54 +000025 __u32 portid;
Patrick McHardyd629b832005-08-14 19:27:50 -070026 __u32 dst_group;
Patrick McHardy9652e932013-04-17 06:47:02 +000027 __u32 flags;
Patrick McHardye32123e2013-04-17 06:46:57 +000028 struct sock *sk;
Nicolas Dichtel59324cf2015-05-07 11:02:53 +020029 bool nsid_is_set;
30 int nsid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031};
32
33#define NETLINK_CB(skb) (*(struct netlink_skb_parms*)&((skb)->cb))
34#define NETLINK_CREDS(skb) (&NETLINK_CB((skb)).creds)
35
36
Johannes Bergd136f1b2009-09-12 03:03:15 +000037extern void netlink_table_grab(void);
38extern void netlink_table_ungrab(void);
39
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +000040#define NL_CFG_F_NONROOT_RECV (1 << 0)
41#define NL_CFG_F_NONROOT_SEND (1 << 1)
42
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +000043/* optional Netlink kernel configuration parameters */
44struct netlink_kernel_cfg {
45 unsigned int groups;
David S. Millerc9d2ea92012-09-23 02:09:23 -040046 unsigned int flags;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +000047 void (*input)(struct sk_buff *skb);
48 struct mutex *cb_mutex;
Johannes Berg023e2cf2014-12-23 21:00:06 +010049 int (*bind)(struct net *net, int group);
50 void (*unbind)(struct net *net, int group);
Gao fengda12c902013-06-06 14:49:11 +080051 bool (*compare)(struct net *net, struct sock *sk);
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +000052};
53
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +000054extern struct sock *__netlink_kernel_create(struct net *net, int unit,
55 struct module *module,
56 struct netlink_kernel_cfg *cfg);
57static inline struct sock *
58netlink_kernel_create(struct net *net, int unit, struct netlink_kernel_cfg *cfg)
59{
60 return __netlink_kernel_create(net, unit, THIS_MODULE, cfg);
61}
62
Johannes Bergba0dc5f2017-04-12 14:34:06 +020063/* this can be increased when necessary - don't expose to userland */
64#define NETLINK_MAX_COOKIE_LEN 20
65
Johannes Berg2d4bc932017-04-12 14:34:04 +020066/**
67 * struct netlink_ext_ack - netlink extended ACK report struct
68 * @_msg: message string to report - don't access directly, use
69 * %NL_SET_ERR_MSG
70 * @bad_attr: attribute with error
Johannes Bergba0dc5f2017-04-12 14:34:06 +020071 * @cookie: cookie data to return to userspace (for success)
72 * @cookie_len: actual cookie data length
Johannes Berg2d4bc932017-04-12 14:34:04 +020073 */
74struct netlink_ext_ack {
75 const char *_msg;
76 const struct nlattr *bad_attr;
Johannes Bergba0dc5f2017-04-12 14:34:06 +020077 u8 cookie[NETLINK_MAX_COOKIE_LEN];
78 u8 cookie_len;
Johannes Berg2d4bc932017-04-12 14:34:04 +020079};
80
81/* Always use this macro, this allows later putting the
82 * message into a separate section or such for things
83 * like translation or listing all possible messages.
84 * Currently string formatting is not supported (due
85 * to the lack of an output buffer.)
86 */
Daniel Borkmann4d463c42017-05-03 00:39:17 +020087#define NL_SET_ERR_MSG(extack, msg) do { \
Johannes Berg6311b7c2018-01-15 12:42:25 +010088 static const char __msg[] = msg; \
Daniel Borkmann4d463c42017-05-03 00:39:17 +020089 struct netlink_ext_ack *__extack = (extack); \
90 \
91 if (__extack) \
92 __extack->_msg = __msg; \
Johannes Berg2d4bc932017-04-12 14:34:04 +020093} while (0)
94
Daniel Borkmann4d463c42017-05-03 00:39:17 +020095#define NL_SET_ERR_MSG_MOD(extack, msg) \
96 NL_SET_ERR_MSG((extack), KBUILD_MODNAME ": " msg)
Jakub Kicinski45d9b372017-04-30 21:46:45 -070097
David Ahernc3ab2b42017-05-21 10:12:03 -060098#define NL_SET_BAD_ATTR(extack, attr) do { \
99 if ((extack)) \
100 (extack)->bad_attr = (attr); \
101} while (0)
102
David Ahern9ae28722017-05-27 16:19:28 -0600103#define NL_SET_ERR_MSG_ATTR(extack, attr, msg) do { \
Johannes Berg6311b7c2018-01-15 12:42:25 +0100104 static const char __msg[] = msg; \
David Ahern9ae28722017-05-27 16:19:28 -0600105 struct netlink_ext_ack *__extack = (extack); \
106 \
107 if (__extack) { \
108 __extack->_msg = __msg; \
109 __extack->bad_attr = (attr); \
110 } \
111} while (0)
112
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -0800113extern void netlink_kernel_release(struct sock *sk);
Johannes Bergd136f1b2009-09-12 03:03:15 +0000114extern int __netlink_change_ngroups(struct sock *sk, unsigned int groups);
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700115extern int netlink_change_ngroups(struct sock *sk, unsigned int groups);
Johannes Bergb8273572009-09-24 15:44:05 -0700116extern void __netlink_clear_multicast_users(struct sock *sk, unsigned int group);
Johannes Berg2d4bc932017-04-12 14:34:04 +0200117extern void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err,
118 const struct netlink_ext_ack *extack);
Patrick McHardy4277a082006-03-20 18:52:01 -0800119extern int netlink_has_listeners(struct sock *sk, unsigned int group);
Daniel Borkmann6bb0fef2015-09-10 02:10:57 +0200120
Eric W. Biederman15e47302012-09-07 20:12:54 +0000121extern int netlink_unicast(struct sock *ssk, struct sk_buff *skb, __u32 portid, int nonblock);
122extern int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, __u32 portid,
Al Virodd0fc662005-10-07 07:46:04 +0100123 __u32 group, gfp_t allocation);
Eric W. Biederman910a7e92010-05-04 17:36:46 -0700124extern int netlink_broadcast_filtered(struct sock *ssk, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000125 __u32 portid, __u32 group, gfp_t allocation,
Eric W. Biederman910a7e92010-05-04 17:36:46 -0700126 int (*filter)(struct sock *dsk, struct sk_buff *skb, void *data),
127 void *filter_data);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000128extern int netlink_set_err(struct sock *ssk, __u32 portid, __u32 group, int code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129extern int netlink_register_notifier(struct notifier_block *nb);
130extern int netlink_unregister_notifier(struct notifier_block *nb);
131
132/* finegrained unicast helpers: */
133struct sock *netlink_getsockbyfilp(struct file *filp);
Denis V. Lunev9457afe2008-06-05 11:23:39 -0700134int netlink_attachskb(struct sock *sk, struct sk_buff *skb,
Patrick McHardyc3d8d1e2007-11-07 02:42:09 -0800135 long *timeo, struct sock *ssk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136void netlink_detachskb(struct sock *sk, struct sk_buff *skb);
Denis V. Lunev7ee015e2007-10-10 21:14:03 -0700137int netlink_sendskb(struct sock *sk, struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Pablo Neira3a365152013-06-28 03:04:23 +0200139static inline struct sk_buff *
140netlink_skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
141{
142 struct sk_buff *nskb;
143
144 nskb = skb_clone(skb, gfp_mask);
145 if (!nskb)
146 return NULL;
147
148 /* This is a large skb, set destructor callback to release head */
149 if (is_vmalloc_addr(skb->head))
150 nskb->destructor = skb->destructor;
151
152 return nskb;
153}
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155/*
156 * skb should fit one page. This choice is good for headerless malloc.
David S. Millerfc910a22007-03-25 20:27:59 -0700157 * But we should limit to 8K so that userspace does not have to
158 * use enormous buffer sizes on recvmsg() calls just to avoid
159 * MSG_TRUNC when PAGE_SIZE is very large.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 */
David S. Millerfc910a22007-03-25 20:27:59 -0700161#if PAGE_SIZE < 8192UL
162#define NLMSG_GOODSIZE SKB_WITH_OVERHEAD(PAGE_SIZE)
163#else
164#define NLMSG_GOODSIZE SKB_WITH_OVERHEAD(8192UL)
165#endif
166
Thomas Graf339bf982006-11-10 14:10:15 -0800167#define NLMSG_DEFAULT_SIZE (NLMSG_GOODSIZE - NLMSG_HDRLEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169
Eric Dumazetd94d9fe2009-11-04 09:50:58 -0800170struct netlink_callback {
Patrick McHardy3a6c2b42009-08-25 16:07:40 +0200171 struct sk_buff *skb;
172 const struct nlmsghdr *nlh;
Tom Herbertfc9e50f2015-12-15 15:41:37 -0800173 int (*start)(struct netlink_callback *);
Patrick McHardy3a6c2b42009-08-25 16:07:40 +0200174 int (*dump)(struct sk_buff * skb,
175 struct netlink_callback *cb);
176 int (*done)(struct netlink_callback *cb);
Pablo Neira Ayuso7175c882012-02-24 14:30:16 +0000177 void *data;
Gao feng6dc878a2012-10-04 20:15:48 +0000178 /* the module that dump function belong to */
179 struct module *module;
Greg Rosec7ac8672011-06-10 01:27:09 +0000180 u16 family;
181 u16 min_dump_alloc;
Johannes Berg670dc282011-06-20 13:40:46 +0200182 unsigned int prev_seq, seq;
Patrick McHardy3a6c2b42009-08-25 16:07:40 +0200183 long args[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184};
185
Eric Dumazetd94d9fe2009-11-04 09:50:58 -0800186struct netlink_notify {
Eric W. Biedermanb4b51022007-09-12 13:05:38 +0200187 struct net *net;
Richard Weinberger0392d092015-04-13 00:52:35 +0200188 u32 portid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 int protocol;
190};
191
Denys Vlasenkoa46621a2012-01-30 15:22:06 -0500192struct nlmsghdr *
Eric W. Biederman15e47302012-09-07 20:12:54 +0000193__nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, int type, int len, int flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +0000195struct netlink_dump_control {
Tom Herbertfc9e50f2015-12-15 15:41:37 -0800196 int (*start)(struct netlink_callback *);
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +0000197 int (*dump)(struct sk_buff *skb, struct netlink_callback *);
Gao feng6dc878a2012-10-04 20:15:48 +0000198 int (*done)(struct netlink_callback *);
Pablo Neira Ayuso7175c882012-02-24 14:30:16 +0000199 void *data;
Gao feng6dc878a2012-10-04 20:15:48 +0000200 struct module *module;
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +0000201 u16 min_dump_alloc;
202};
203
Gao feng6dc878a2012-10-04 20:15:48 +0000204extern int __netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
205 const struct nlmsghdr *nlh,
206 struct netlink_dump_control *control);
207static inline int netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
208 const struct nlmsghdr *nlh,
209 struct netlink_dump_control *control)
210{
211 if (!control->module)
212 control->module = THIS_MODULE;
213
214 return __netlink_dump_start(ssk, skb, nlh, control);
215}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Daniel Borkmannbcbde0d2013-06-21 19:38:07 +0200217struct netlink_tap {
218 struct net_device *dev;
219 struct module *module;
220 struct list_head list;
221};
222
223extern int netlink_add_tap(struct netlink_tap *nt);
Daniel Borkmannbcbde0d2013-06-21 19:38:07 +0200224extern int netlink_remove_tap(struct netlink_tap *nt);
225
Eric W. Biedermanaa4cf942014-04-23 14:28:03 -0700226bool __netlink_ns_capable(const struct netlink_skb_parms *nsp,
227 struct user_namespace *ns, int cap);
228bool netlink_ns_capable(const struct sk_buff *skb,
229 struct user_namespace *ns, int cap);
230bool netlink_capable(const struct sk_buff *skb, int cap);
231bool netlink_net_capable(const struct sk_buff *skb, int cap);
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233#endif /* __LINUX_NETLINK_H */