blob: 23553ed6b548063df3a39139994b0eaf722cbd8d [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,
David Friesac8f7332014-01-15 22:29:19 -060062 gfp_t gfp_mask)
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -070063{
64 struct cn_callback_entry *__cbq;
65 unsigned int size;
66 struct sk_buff *skb;
67 struct nlmsghdr *nlh;
68 struct cn_msg *data;
69 struct cn_dev *dev = &cdev;
70 u32 group = 0;
71 int found = 0;
72
David Friesac8f7332014-01-15 22:29:19 -060073 if (portid || __group) {
74 group = __group;
75 } else {
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -070076 spin_lock_bh(&dev->cbdev->queue_lock);
77 list_for_each_entry(__cbq, &dev->cbdev->queue_list,
78 callback_entry) {
Evgeniy Polyakovacd042b2005-09-26 15:06:50 -070079 if (cn_cb_equal(&__cbq->id.id, &msg->id)) {
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -070080 found = 1;
81 group = __cbq->group;
Li Zefanfd00eec2008-01-04 01:54:38 -080082 break;
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -070083 }
84 }
85 spin_unlock_bh(&dev->cbdev->queue_lock);
86
87 if (!found)
88 return -ENODEV;
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -070089 }
90
David Friesac8f7332014-01-15 22:29:19 -060091 if (!portid && !netlink_has_listeners(dev->nls, group))
Evgeniy Polyakovb191ba02006-03-20 22:21:40 -080092 return -ESRCH;
93
David Fries34470e02014-04-08 22:37:08 -050094 size = sizeof(*msg) + len;
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -070095
Hong zhi guo9631d792013-03-27 06:54:56 +000096 skb = nlmsg_new(size, gfp_mask);
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -070097 if (!skb)
98 return -ENOMEM;
99
Hong zhi guo9631d792013-03-27 06:54:56 +0000100 nlh = nlmsg_put(skb, 0, msg->seq, NLMSG_DONE, size, 0);
Javier Martinez Canillas85c93162012-06-26 05:41:24 +0000101 if (!nlh) {
102 kfree_skb(skb);
103 return -EMSGSIZE;
104 }
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700105
Javier Martinez Canillas85c93162012-06-26 05:41:24 +0000106 data = nlmsg_data(nlh);
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700107
Mathias Krauseac73bf52013-09-30 22:03:08 +0200108 memcpy(data, msg, size);
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700109
110 NETLINK_CB(skb).dst_group = group;
111
David Friesac8f7332014-01-15 22:29:19 -0600112 if (group)
113 return netlink_broadcast(dev->nls, skb, portid, group,
114 gfp_mask);
Mel Gormand0164ad2015-11-06 16:28:21 -0800115 return netlink_unicast(dev->nls, skb, portid,
116 !gfpflags_allow_blocking(gfp_mask));
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700117}
David Fries34470e02014-04-08 22:37:08 -0500118EXPORT_SYMBOL_GPL(cn_netlink_send_mult);
119
120/* same as cn_netlink_send_mult except msg->len is used for len */
121int cn_netlink_send(struct cn_msg *msg, u32 portid, u32 __group,
122 gfp_t gfp_mask)
123{
124 return cn_netlink_send_mult(msg, msg->len, portid, __group, gfp_mask);
125}
Andrew Mortondeb0e9b2006-06-23 02:05:46 -0700126EXPORT_SYMBOL_GPL(cn_netlink_send);
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700127
128/*
129 * Callback helper - queues work and setup destructor for given data.
130 */
Philipp Reisnerf1489cf2009-10-02 02:40:07 +0000131static int cn_call_callback(struct sk_buff *skb)
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700132{
David Friesa30cfa42014-11-10 20:19:36 -0600133 struct nlmsghdr *nlh;
Patrick McHardy04f482f2011-03-28 08:39:36 +0000134 struct cn_callback_entry *i, *cbq = NULL;
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700135 struct cn_dev *dev = &cdev;
Hong zhi guo9631d792013-03-27 06:54:56 +0000136 struct cn_msg *msg = nlmsg_data(nlmsg_hdr(skb));
Patrick McHardy04f482f2011-03-28 08:39:36 +0000137 struct netlink_skb_parms *nsp = &NETLINK_CB(skb);
Evgeniy Polyakovacd042b2005-09-26 15:06:50 -0700138 int err = -ENODEV;
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700139
David Friesa30cfa42014-11-10 20:19:36 -0600140 /* verify msg->len is within skb */
141 nlh = nlmsg_hdr(skb);
142 if (nlh->nlmsg_len < NLMSG_HDRLEN + sizeof(struct cn_msg) + msg->len)
143 return -EINVAL;
144
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700145 spin_lock_bh(&dev->cbdev->queue_lock);
Patrick McHardy04f482f2011-03-28 08:39:36 +0000146 list_for_each_entry(i, &dev->cbdev->queue_list, callback_entry) {
147 if (cn_cb_equal(&i->id.id, &msg->id)) {
Elena Reshetovae65f7ee2017-10-20 10:23:49 +0300148 refcount_inc(&i->refcnt);
Patrick McHardy04f482f2011-03-28 08:39:36 +0000149 cbq = i;
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700150 break;
151 }
152 }
153 spin_unlock_bh(&dev->cbdev->queue_lock);
154
Patrick McHardy04f482f2011-03-28 08:39:36 +0000155 if (cbq != NULL) {
156 cbq->callback(msg, nsp);
157 kfree_skb(skb);
158 cn_queue_release_callback(cbq);
Patrick McHardy0e087852011-04-12 05:39:51 +0000159 err = 0;
Patrick McHardy04f482f2011-03-28 08:39:36 +0000160 }
161
Evgeniy Polyakovacd042b2005-09-26 15:06:50 -0700162 return err;
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700163}
164
165/*
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700166 * Main netlink receiving function.
167 *
Li Zefan00f5e062008-01-04 01:55:01 -0800168 * It checks skb, netlink header and msg sizes, and calls callback helper.
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700169 */
Florian Westphal55285bf2015-12-31 14:26:33 +0100170static void cn_rx_skb(struct sk_buff *skb)
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700171{
172 struct nlmsghdr *nlh;
Mathias Krause162b2be2013-09-30 22:03:07 +0200173 int len, err;
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700174
Hong zhi guo9631d792013-03-27 06:54:56 +0000175 if (skb->len >= NLMSG_HDRLEN) {
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -0700176 nlh = nlmsg_hdr(skb);
Mathias Krause162b2be2013-09-30 22:03:07 +0200177 len = nlmsg_len(nlh);
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700178
Mathias Krause162b2be2013-09-30 22:03:07 +0200179 if (len < (int)sizeof(struct cn_msg) ||
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700180 skb->len < nlh->nlmsg_len ||
Florian Westphal55285bf2015-12-31 14:26:33 +0100181 len > CONNECTOR_MAX_MSG_SIZE)
Michal Januszewski6cf92e92007-10-30 20:41:49 -0700182 return;
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700183
Florian Westphal55285bf2015-12-31 14:26:33 +0100184 err = cn_call_callback(skb_get(skb));
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700185 if (err < 0)
186 kfree_skb(skb);
187 }
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700188}
189
190/*
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700191 * Callback add routing - adds callback with given ID and name.
192 * If there is registered callback with the same ID it will not be added.
193 *
194 * May sleep.
195 */
Joe Perches008536e2011-02-19 15:45:29 -0800196int cn_add_callback(struct cb_id *id, const char *name,
Valentin Ilief3c48ec2012-07-14 13:08:29 +0000197 void (*callback)(struct cn_msg *,
198 struct netlink_skb_parms *))
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700199{
200 int err;
201 struct cn_dev *dev = &cdev;
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700202
Evgeniy Polyakovd6cc7f12006-06-19 23:42:53 -0700203 if (!cn_already_initialized)
204 return -EAGAIN;
205
Evgeniy Polyakovacd042b2005-09-26 15:06:50 -0700206 err = cn_queue_add_callback(dev->cbdev, name, id, callback);
207 if (err)
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700208 return err;
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700209
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700210 return 0;
211}
Andrew Mortondeb0e9b2006-06-23 02:05:46 -0700212EXPORT_SYMBOL_GPL(cn_add_callback);
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700213
214/*
215 * Callback remove routing - removes callback
216 * with given ID.
217 * If there is no registered callback with given
218 * ID nothing happens.
219 *
220 * May sleep while waiting for reference counter to become zero.
221 */
222void cn_del_callback(struct cb_id *id)
223{
224 struct cn_dev *dev = &cdev;
225
226 cn_queue_del_callback(dev->cbdev, id);
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700227}
Andrew Mortondeb0e9b2006-06-23 02:05:46 -0700228EXPORT_SYMBOL_GPL(cn_del_callback);
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700229
Randy Dunlapd2af6862018-07-07 08:27:53 -0700230static int __maybe_unused cn_proc_show(struct seq_file *m, void *v)
Li Zefana0a61a62008-06-27 20:03:24 -0700231{
232 struct cn_queue_dev *dev = cdev.cbdev;
233 struct cn_callback_entry *cbq;
234
235 seq_printf(m, "Name ID\n");
236
237 spin_lock_bh(&dev->queue_lock);
238
239 list_for_each_entry(cbq, &dev->queue_list, callback_entry) {
240 seq_printf(m, "%-15s %u:%u\n",
241 cbq->id.name,
242 cbq->id.id.idx,
243 cbq->id.id.val);
244 }
245
246 spin_unlock_bh(&dev->queue_lock);
247
248 return 0;
249}
250
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +0000251static struct cn_dev cdev = {
252 .input = cn_rx_skb,
253};
254
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -0800255static int cn_init(void)
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700256{
257 struct cn_dev *dev = &cdev;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +0000258 struct netlink_kernel_cfg cfg = {
259 .groups = CN_NETLINK_USERS + 0xf,
260 .input = dev->input,
261 };
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700262
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +0000263 dev->nls = netlink_kernel_create(&init_net, NETLINK_CONNECTOR, &cfg);
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700264 if (!dev->nls)
265 return -EIO;
266
267 dev->cbdev = cn_queue_alloc_dev("cqueue", dev->nls);
268 if (!dev->cbdev) {
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -0800269 netlink_kernel_release(dev->nls);
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700270 return -EINVAL;
271 }
Frederic Weisbecker1a5645b2009-02-02 23:22:04 -0800272
Evgeniy Polyakovd6cc7f12006-06-19 23:42:53 -0700273 cn_already_initialized = 1;
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700274
Christoph Hellwig3f3942a2018-05-15 15:57:23 +0200275 proc_create_single("connector", S_IRUGO, init_net.proc_net, cn_proc_show);
Li Zefana0a61a62008-06-27 20:03:24 -0700276
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700277 return 0;
278}
279
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -0800280static void cn_fini(void)
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700281{
282 struct cn_dev *dev = &cdev;
283
284 cn_already_initialized = 0;
285
Gao fengece31ff2013-02-18 01:34:56 +0000286 remove_proc_entry("connector", init_net.proc_net);
Li Zefana0a61a62008-06-27 20:03:24 -0700287
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700288 cn_queue_free_dev(dev->cbdev);
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -0800289 netlink_kernel_release(dev->nls);
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700290}
291
Evgeniy Polyakovd6cc7f12006-06-19 23:42:53 -0700292subsys_initcall(cn_init);
Evgeniy Polyakov7672d0b2005-09-11 19:15:07 -0700293module_exit(cn_fini);