blob: 4028e8eeba821e99fde750d9119067854d240294 [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -07002/*
Valentin Ilief3c48ec2012-07-14 13:08:29 +00003 * connector.c
Frederic Weisbecker1a5645b2009-02-02 23:22:04 -08004 *
Evgeniy Polyakovacb9c1b2009-07-21 12:43:51 -07005 * 2004+ Copyright (c) Evgeniy Polyakov <zbr@ioremap.net>
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -07006 * All rights reserved.
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -07007 */
8
Randy Dunlapd2af6862018-07-07 08:27:53 -07009#include <linux/compiler.h>
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -070010#include <linux/kernel.h>
11#include <linux/module.h>
12#include <linux/list.h>
13#include <linux/skbuff.h>
Hong zhi guo9631d792013-03-27 06:54:56 +000014#include <net/netlink.h>
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -070015#include <linux/moduleparam.h>
16#include <linux/connector.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Arjan van de Ven8ed965d2006-03-23 03:00:21 -080018#include <linux/mutex.h>
Li Zefana0a61a62008-06-27 20:03:24 -070019#include <linux/proc_fs.h>
20#include <linux/spinlock.h>
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -070021
22#include <net/sock.h>
23
24MODULE_LICENSE("GPL");
Evgeniy Polyakovacb9c1b2009-07-21 12:43:51 -070025MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -070026MODULE_DESCRIPTION("Generic userspace <-> kernelspace connector.");
Stephen Hemminger3700c3c2010-12-10 12:27:49 -080027MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_CONNECTOR);
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -070028
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -070029static struct cn_dev cdev;
30
Li Zefan78374672008-02-26 18:25:53 -080031static int cn_already_initialized;
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -070032
33/*
David Fries34470e02014-04-08 22:37:08 -050034 * Sends mult (multiple) cn_msg at a time.
35 *
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -070036 * msg->seq and msg->ack are used to determine message genealogy.
37 * When someone sends message it puts there locally unique sequence
38 * and random acknowledge numbers. Sequence number may be copied into
39 * nlmsghdr->nlmsg_seq too.
40 *
41 * Sequence number is incremented with each message to be sent.
42 *
David Friesac8f7332014-01-15 22:29:19 -060043 * If we expect a reply to our message then the sequence number in
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -070044 * received message MUST be the same as in original message, and
45 * acknowledge number MUST be the same + 1.
46 *
47 * If we receive a message and its sequence number is not equal to the
48 * one we are expecting then it is a new message.
49 *
50 * If we receive a message and its sequence number is the same as one
51 * we are expecting but it's acknowledgement number is not equal to
52 * the acknowledgement number in the original message + 1, then it is
53 * a new message.
54 *
David Fries34470e02014-04-08 22:37:08 -050055 * If msg->len != len, then additional cn_msg messages are expected following
56 * the first msg.
57 *
David Friesac8f7332014-01-15 22:29:19 -060058 * The message is sent to, the portid if given, the group if given, both if
59 * both, or if both are zero then the group is looked up and sent there.
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -070060 */
David Fries34470e02014-04-08 22:37:08 -050061int cn_netlink_send_mult(struct cn_msg *msg, u16 len, u32 portid, u32 __group,
Jiri Pirko403863e2023-12-16 13:29:58 +010062 gfp_t gfp_mask, netlink_filter_fn filter,
63 void *filter_data)
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -070064{
65 struct cn_callback_entry *__cbq;
66 unsigned int size;
67 struct sk_buff *skb;
68 struct nlmsghdr *nlh;
69 struct cn_msg *data;
70 struct cn_dev *dev = &cdev;
71 u32 group = 0;
72 int found = 0;
73
David Friesac8f7332014-01-15 22:29:19 -060074 if (portid || __group) {
75 group = __group;
76 } else {
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -070077 spin_lock_bh(&dev->cbdev->queue_lock);
78 list_for_each_entry(__cbq, &dev->cbdev->queue_list,
79 callback_entry) {
Evgeniy Polyakovacd042b2005-09-26 15:06:50 -070080 if (cn_cb_equal(&__cbq->id.id, &msg->id)) {
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -070081 found = 1;
82 group = __cbq->group;
Li Zefanfd00eec2008-01-04 01:54:38 -080083 break;
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -070084 }
85 }
86 spin_unlock_bh(&dev->cbdev->queue_lock);
87
88 if (!found)
89 return -ENODEV;
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -070090 }
91
David Friesac8f7332014-01-15 22:29:19 -060092 if (!portid && !netlink_has_listeners(dev->nls, group))
Evgeniy Polyakovb191ba02006-03-20 22:21:40 -080093 return -ESRCH;
94
David Fries34470e02014-04-08 22:37:08 -050095 size = sizeof(*msg) + len;
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -070096
Hong zhi guo9631d792013-03-27 06:54:56 +000097 skb = nlmsg_new(size, gfp_mask);
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -070098 if (!skb)
99 return -ENOMEM;
100
Hong zhi guo9631d792013-03-27 06:54:56 +0000101 nlh = nlmsg_put(skb, 0, msg->seq, NLMSG_DONE, size, 0);
Javier Martinez Canillas85c93162012-06-26 05:41:24 +0000102 if (!nlh) {
103 kfree_skb(skb);
104 return -EMSGSIZE;
105 }
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700106
Javier Martinez Canillas85c93162012-06-26 05:41:24 +0000107 data = nlmsg_data(nlh);
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700108
Mathias Krauseac73bf52013-09-30 22:03:08 +0200109 memcpy(data, msg, size);
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700110
111 NETLINK_CB(skb).dst_group = group;
112
David Friesac8f7332014-01-15 22:29:19 -0600113 if (group)
Anjali Kulkarni2aa1f7a2023-07-19 13:18:18 -0700114 return netlink_broadcast_filtered(dev->nls, skb, portid, group,
115 gfp_mask, filter,
116 (void *)filter_data);
Mel Gormand0164ad2015-11-06 16:28:21 -0800117 return netlink_unicast(dev->nls, skb, portid,
118 !gfpflags_allow_blocking(gfp_mask));
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700119}
David Fries34470e02014-04-08 22:37:08 -0500120EXPORT_SYMBOL_GPL(cn_netlink_send_mult);
121
122/* same as cn_netlink_send_mult except msg->len is used for len */
123int cn_netlink_send(struct cn_msg *msg, u32 portid, u32 __group,
124 gfp_t gfp_mask)
125{
Anjali Kulkarni2aa1f7a2023-07-19 13:18:18 -0700126 return cn_netlink_send_mult(msg, msg->len, portid, __group, gfp_mask,
127 NULL, NULL);
David Fries34470e02014-04-08 22:37:08 -0500128}
Andrew Mortondeb0e9b2006-06-23 02:05:46 -0700129EXPORT_SYMBOL_GPL(cn_netlink_send);
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700130
131/*
132 * Callback helper - queues work and setup destructor for given data.
133 */
Philipp Reisnerf1489cf2009-10-02 02:40:07 +0000134static int cn_call_callback(struct sk_buff *skb)
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700135{
David Friesa30cfa42014-11-10 20:19:36 -0600136 struct nlmsghdr *nlh;
Patrick McHardy04f482f2011-03-28 08:39:36 +0000137 struct cn_callback_entry *i, *cbq = NULL;
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700138 struct cn_dev *dev = &cdev;
Hong zhi guo9631d792013-03-27 06:54:56 +0000139 struct cn_msg *msg = nlmsg_data(nlmsg_hdr(skb));
Patrick McHardy04f482f2011-03-28 08:39:36 +0000140 struct netlink_skb_parms *nsp = &NETLINK_CB(skb);
Evgeniy Polyakovacd042b2005-09-26 15:06:50 -0700141 int err = -ENODEV;
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700142
David Friesa30cfa42014-11-10 20:19:36 -0600143 /* verify msg->len is within skb */
144 nlh = nlmsg_hdr(skb);
145 if (nlh->nlmsg_len < NLMSG_HDRLEN + sizeof(struct cn_msg) + msg->len)
146 return -EINVAL;
147
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700148 spin_lock_bh(&dev->cbdev->queue_lock);
Patrick McHardy04f482f2011-03-28 08:39:36 +0000149 list_for_each_entry(i, &dev->cbdev->queue_list, callback_entry) {
150 if (cn_cb_equal(&i->id.id, &msg->id)) {
Elena Reshetovae65f7ee2017-10-20 10:23:49 +0300151 refcount_inc(&i->refcnt);
Patrick McHardy04f482f2011-03-28 08:39:36 +0000152 cbq = i;
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700153 break;
154 }
155 }
156 spin_unlock_bh(&dev->cbdev->queue_lock);
157
Patrick McHardy04f482f2011-03-28 08:39:36 +0000158 if (cbq != NULL) {
159 cbq->callback(msg, nsp);
160 kfree_skb(skb);
161 cn_queue_release_callback(cbq);
Patrick McHardy0e087852011-04-12 05:39:51 +0000162 err = 0;
Patrick McHardy04f482f2011-03-28 08:39:36 +0000163 }
164
Evgeniy Polyakovacd042b2005-09-26 15:06:50 -0700165 return err;
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700166}
167
Anjali Kulkarnibfdfdc22023-07-19 13:18:20 -0700168/*
169 * Allow non-root access for NETLINK_CONNECTOR family having CN_IDX_PROC
170 * multicast group.
171 */
172static int cn_bind(struct net *net, int group)
173{
174 unsigned long groups = (unsigned long) group;
175
176 if (ns_capable(net->user_ns, CAP_NET_ADMIN))
177 return 0;
178
179 if (test_bit(CN_IDX_PROC - 1, &groups))
180 return 0;
181
182 return -EPERM;
183}
184
Anjali Kulkarni2aa1f7a2023-07-19 13:18:18 -0700185static void cn_release(struct sock *sk, unsigned long *groups)
186{
187 if (groups && test_bit(CN_IDX_PROC - 1, groups)) {
188 kfree(sk->sk_user_data);
189 sk->sk_user_data = NULL;
190 }
191}
192
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700193/*
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700194 * Main netlink receiving function.
195 *
Li Zefan00f5e062008-01-04 01:55:01 -0800196 * It checks skb, netlink header and msg sizes, and calls callback helper.
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700197 */
Florian Westphal55285bf2015-12-31 14:26:33 +0100198static void cn_rx_skb(struct sk_buff *skb)
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700199{
200 struct nlmsghdr *nlh;
Mathias Krause162b2be2013-09-30 22:03:07 +0200201 int len, err;
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700202
Hong zhi guo9631d792013-03-27 06:54:56 +0000203 if (skb->len >= NLMSG_HDRLEN) {
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -0700204 nlh = nlmsg_hdr(skb);
Mathias Krause162b2be2013-09-30 22:03:07 +0200205 len = nlmsg_len(nlh);
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700206
Mathias Krause162b2be2013-09-30 22:03:07 +0200207 if (len < (int)sizeof(struct cn_msg) ||
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700208 skb->len < nlh->nlmsg_len ||
Florian Westphal55285bf2015-12-31 14:26:33 +0100209 len > CONNECTOR_MAX_MSG_SIZE)
Michal Januszewski6cf92e92007-10-30 20:41:49 -0700210 return;
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700211
Florian Westphal55285bf2015-12-31 14:26:33 +0100212 err = cn_call_callback(skb_get(skb));
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700213 if (err < 0)
214 kfree_skb(skb);
215 }
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700216}
217
218/*
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700219 * Callback add routing - adds callback with given ID and name.
220 * If there is registered callback with the same ID it will not be added.
221 *
222 * May sleep.
223 */
Geoff Levandc18e6862020-12-14 21:15:47 -0800224int cn_add_callback(const struct cb_id *id, const char *name,
Valentin Ilief3c48ec2012-07-14 13:08:29 +0000225 void (*callback)(struct cn_msg *,
226 struct netlink_skb_parms *))
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700227{
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700228 struct cn_dev *dev = &cdev;
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700229
Evgeniy Polyakovd6cc7f12006-06-19 23:42:53 -0700230 if (!cn_already_initialized)
231 return -EAGAIN;
232
Qinglang Miaofe6bc892020-09-21 21:10:06 +0800233 return cn_queue_add_callback(dev->cbdev, name, id, callback);
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700234}
Andrew Mortondeb0e9b2006-06-23 02:05:46 -0700235EXPORT_SYMBOL_GPL(cn_add_callback);
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700236
237/*
238 * Callback remove routing - removes callback
239 * with given ID.
240 * If there is no registered callback with given
241 * ID nothing happens.
242 *
243 * May sleep while waiting for reference counter to become zero.
244 */
Geoff Levandc18e6862020-12-14 21:15:47 -0800245void cn_del_callback(const struct cb_id *id)
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700246{
247 struct cn_dev *dev = &cdev;
248
249 cn_queue_del_callback(dev->cbdev, id);
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700250}
Andrew Mortondeb0e9b2006-06-23 02:05:46 -0700251EXPORT_SYMBOL_GPL(cn_del_callback);
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700252
Randy Dunlapd2af6862018-07-07 08:27:53 -0700253static int __maybe_unused cn_proc_show(struct seq_file *m, void *v)
Li Zefana0a61a62008-06-27 20:03:24 -0700254{
255 struct cn_queue_dev *dev = cdev.cbdev;
256 struct cn_callback_entry *cbq;
257
258 seq_printf(m, "Name ID\n");
259
260 spin_lock_bh(&dev->queue_lock);
261
262 list_for_each_entry(cbq, &dev->queue_list, callback_entry) {
263 seq_printf(m, "%-15s %u:%u\n",
264 cbq->id.name,
265 cbq->id.id.idx,
266 cbq->id.id.val);
267 }
268
269 spin_unlock_bh(&dev->queue_lock);
270
271 return 0;
272}
273
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -0800274static int cn_init(void)
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700275{
276 struct cn_dev *dev = &cdev;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +0000277 struct netlink_kernel_cfg cfg = {
278 .groups = CN_NETLINK_USERS + 0xf,
Vasily Averin903e9d12019-07-18 07:26:46 +0300279 .input = cn_rx_skb,
Anjali Kulkarnibfdfdc22023-07-19 13:18:20 -0700280 .flags = NL_CFG_F_NONROOT_RECV,
281 .bind = cn_bind,
Anjali Kulkarni2aa1f7a2023-07-19 13:18:18 -0700282 .release = cn_release,
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +0000283 };
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700284
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +0000285 dev->nls = netlink_kernel_create(&init_net, NETLINK_CONNECTOR, &cfg);
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700286 if (!dev->nls)
287 return -EIO;
288
289 dev->cbdev = cn_queue_alloc_dev("cqueue", dev->nls);
290 if (!dev->cbdev) {
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -0800291 netlink_kernel_release(dev->nls);
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700292 return -EINVAL;
293 }
Frederic Weisbecker1a5645b2009-02-02 23:22:04 -0800294
Evgeniy Polyakovd6cc7f12006-06-19 23:42:53 -0700295 cn_already_initialized = 1;
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700296
Christoph Hellwig3f3942a2018-05-15 15:57:23 +0200297 proc_create_single("connector", S_IRUGO, init_net.proc_net, cn_proc_show);
Li Zefana0a61a62008-06-27 20:03:24 -0700298
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700299 return 0;
300}
301
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -0800302static void cn_fini(void)
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700303{
304 struct cn_dev *dev = &cdev;
305
306 cn_already_initialized = 0;
307
Gao fengece31ff2013-02-18 01:34:56 +0000308 remove_proc_entry("connector", init_net.proc_net);
Li Zefana0a61a62008-06-27 20:03:24 -0700309
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700310 cn_queue_free_dev(dev->cbdev);
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -0800311 netlink_kernel_release(dev->nls);
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700312}
313
Evgeniy Polyakovd6cc7f12006-06-19 23:42:53 -0700314subsys_initcall(cn_init);
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700315module_exit(cn_fini);