4 * 2004+ Copyright (c) Evgeniy Polyakov <zbr@ioremap.net>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <linux/compiler.h>
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/list.h>
26 #include <linux/skbuff.h>
27 #include <net/netlink.h>
28 #include <linux/moduleparam.h>
29 #include <linux/connector.h>
30 #include <linux/slab.h>
31 #include <linux/mutex.h>
32 #include <linux/proc_fs.h>
33 #include <linux/spinlock.h>
37 MODULE_LICENSE("GPL");
38 MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
39 MODULE_DESCRIPTION("Generic userspace <-> kernelspace connector.");
40 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK
, NETLINK_CONNECTOR
);
42 static struct cn_dev cdev
;
44 static int cn_already_initialized
;
47 * Sends mult (multiple) cn_msg at a time.
49 * msg->seq and msg->ack are used to determine message genealogy.
50 * When someone sends message it puts there locally unique sequence
51 * and random acknowledge numbers. Sequence number may be copied into
52 * nlmsghdr->nlmsg_seq too.
54 * Sequence number is incremented with each message to be sent.
56 * If we expect a reply to our message then the sequence number in
57 * received message MUST be the same as in original message, and
58 * acknowledge number MUST be the same + 1.
60 * If we receive a message and its sequence number is not equal to the
61 * one we are expecting then it is a new message.
63 * If we receive a message and its sequence number is the same as one
64 * we are expecting but it's acknowledgement number is not equal to
65 * the acknowledgement number in the original message + 1, then it is
68 * If msg->len != len, then additional cn_msg messages are expected following
71 * The message is sent to, the portid if given, the group if given, both if
72 * both, or if both are zero then the group is looked up and sent there.
74 int cn_netlink_send_mult(struct cn_msg
*msg
, u16 len
, u32 portid
, u32 __group
,
77 struct cn_callback_entry
*__cbq
;
82 struct cn_dev
*dev
= &cdev
;
86 if (portid
|| __group
) {
89 spin_lock_bh(&dev
->cbdev
->queue_lock
);
90 list_for_each_entry(__cbq
, &dev
->cbdev
->queue_list
,
92 if (cn_cb_equal(&__cbq
->id
.id
, &msg
->id
)) {
98 spin_unlock_bh(&dev
->cbdev
->queue_lock
);
104 if (!portid
&& !netlink_has_listeners(dev
->nls
, group
))
107 size
= sizeof(*msg
) + len
;
109 skb
= nlmsg_new(size
, gfp_mask
);
113 nlh
= nlmsg_put(skb
, 0, msg
->seq
, NLMSG_DONE
, size
, 0);
119 data
= nlmsg_data(nlh
);
121 memcpy(data
, msg
, size
);
123 NETLINK_CB(skb
).dst_group
= group
;
126 return netlink_broadcast(dev
->nls
, skb
, portid
, group
,
128 return netlink_unicast(dev
->nls
, skb
, portid
,
129 !gfpflags_allow_blocking(gfp_mask
));
131 EXPORT_SYMBOL_GPL(cn_netlink_send_mult
);
133 /* same as cn_netlink_send_mult except msg->len is used for len */
134 int cn_netlink_send(struct cn_msg
*msg
, u32 portid
, u32 __group
,
137 return cn_netlink_send_mult(msg
, msg
->len
, portid
, __group
, gfp_mask
);
139 EXPORT_SYMBOL_GPL(cn_netlink_send
);
142 * Callback helper - queues work and setup destructor for given data.
144 static int cn_call_callback(struct sk_buff
*skb
)
146 struct nlmsghdr
*nlh
;
147 struct cn_callback_entry
*i
, *cbq
= NULL
;
148 struct cn_dev
*dev
= &cdev
;
149 struct cn_msg
*msg
= nlmsg_data(nlmsg_hdr(skb
));
150 struct netlink_skb_parms
*nsp
= &NETLINK_CB(skb
);
153 /* verify msg->len is within skb */
154 nlh
= nlmsg_hdr(skb
);
155 if (nlh
->nlmsg_len
< NLMSG_HDRLEN
+ sizeof(struct cn_msg
) + msg
->len
)
158 spin_lock_bh(&dev
->cbdev
->queue_lock
);
159 list_for_each_entry(i
, &dev
->cbdev
->queue_list
, callback_entry
) {
160 if (cn_cb_equal(&i
->id
.id
, &msg
->id
)) {
161 refcount_inc(&i
->refcnt
);
166 spin_unlock_bh(&dev
->cbdev
->queue_lock
);
169 cbq
->callback(msg
, nsp
);
171 cn_queue_release_callback(cbq
);
179 * Main netlink receiving function.
181 * It checks skb, netlink header and msg sizes, and calls callback helper.
183 static void cn_rx_skb(struct sk_buff
*skb
)
185 struct nlmsghdr
*nlh
;
188 if (skb
->len
>= NLMSG_HDRLEN
) {
189 nlh
= nlmsg_hdr(skb
);
190 len
= nlmsg_len(nlh
);
192 if (len
< (int)sizeof(struct cn_msg
) ||
193 skb
->len
< nlh
->nlmsg_len
||
194 len
> CONNECTOR_MAX_MSG_SIZE
)
197 err
= cn_call_callback(skb_get(skb
));
204 * Callback add routing - adds callback with given ID and name.
205 * If there is registered callback with the same ID it will not be added.
209 int cn_add_callback(struct cb_id
*id
, const char *name
,
210 void (*callback
)(struct cn_msg
*,
211 struct netlink_skb_parms
*))
214 struct cn_dev
*dev
= &cdev
;
216 if (!cn_already_initialized
)
219 err
= cn_queue_add_callback(dev
->cbdev
, name
, id
, callback
);
225 EXPORT_SYMBOL_GPL(cn_add_callback
);
228 * Callback remove routing - removes callback
230 * If there is no registered callback with given
231 * ID nothing happens.
233 * May sleep while waiting for reference counter to become zero.
235 void cn_del_callback(struct cb_id
*id
)
237 struct cn_dev
*dev
= &cdev
;
239 cn_queue_del_callback(dev
->cbdev
, id
);
241 EXPORT_SYMBOL_GPL(cn_del_callback
);
243 static int __maybe_unused
cn_proc_show(struct seq_file
*m
, void *v
)
245 struct cn_queue_dev
*dev
= cdev
.cbdev
;
246 struct cn_callback_entry
*cbq
;
248 seq_printf(m
, "Name ID\n");
250 spin_lock_bh(&dev
->queue_lock
);
252 list_for_each_entry(cbq
, &dev
->queue_list
, callback_entry
) {
253 seq_printf(m
, "%-15s %u:%u\n",
259 spin_unlock_bh(&dev
->queue_lock
);
264 static struct cn_dev cdev
= {
268 static int cn_init(void)
270 struct cn_dev
*dev
= &cdev
;
271 struct netlink_kernel_cfg cfg
= {
272 .groups
= CN_NETLINK_USERS
+ 0xf,
276 dev
->nls
= netlink_kernel_create(&init_net
, NETLINK_CONNECTOR
, &cfg
);
280 dev
->cbdev
= cn_queue_alloc_dev("cqueue", dev
->nls
);
282 netlink_kernel_release(dev
->nls
);
286 cn_already_initialized
= 1;
288 proc_create_single("connector", S_IRUGO
, init_net
.proc_net
, cn_proc_show
);
293 static void cn_fini(void)
295 struct cn_dev
*dev
= &cdev
;
297 cn_already_initialized
= 0;
299 remove_proc_entry("connector", init_net
.proc_net
);
301 cn_queue_free_dev(dev
->cbdev
);
302 netlink_kernel_release(dev
->nls
);
305 subsys_initcall(cn_init
);
306 module_exit(cn_fini
);