xfrm: allow to accept packets with ipv6 NEXTHDR_HOP in xfrm_input
[linux/fpc-iii.git] / drivers / connector / connector.c
blobeeb7d31cbda5b2794afad4c9fd0ab65c8849240d
1 /*
2 * connector.c
4 * 2004+ Copyright (c) Evgeniy Polyakov <zbr@ioremap.net>
5 * All rights reserved.
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>
35 #include <net/sock.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
66 * a new message.
68 * If msg->len != len, then additional cn_msg messages are expected following
69 * the first msg.
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,
75 gfp_t gfp_mask)
77 struct cn_callback_entry *__cbq;
78 unsigned int size;
79 struct sk_buff *skb;
80 struct nlmsghdr *nlh;
81 struct cn_msg *data;
82 struct cn_dev *dev = &cdev;
83 u32 group = 0;
84 int found = 0;
86 if (portid || __group) {
87 group = __group;
88 } else {
89 spin_lock_bh(&dev->cbdev->queue_lock);
90 list_for_each_entry(__cbq, &dev->cbdev->queue_list,
91 callback_entry) {
92 if (cn_cb_equal(&__cbq->id.id, &msg->id)) {
93 found = 1;
94 group = __cbq->group;
95 break;
98 spin_unlock_bh(&dev->cbdev->queue_lock);
100 if (!found)
101 return -ENODEV;
104 if (!portid && !netlink_has_listeners(dev->nls, group))
105 return -ESRCH;
107 size = sizeof(*msg) + len;
109 skb = nlmsg_new(size, gfp_mask);
110 if (!skb)
111 return -ENOMEM;
113 nlh = nlmsg_put(skb, 0, msg->seq, NLMSG_DONE, size, 0);
114 if (!nlh) {
115 kfree_skb(skb);
116 return -EMSGSIZE;
119 data = nlmsg_data(nlh);
121 memcpy(data, msg, size);
123 NETLINK_CB(skb).dst_group = group;
125 if (group)
126 return netlink_broadcast(dev->nls, skb, portid, group,
127 gfp_mask);
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,
135 gfp_t gfp_mask)
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);
151 int err = -ENODEV;
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)
156 return -EINVAL;
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);
162 cbq = i;
163 break;
166 spin_unlock_bh(&dev->cbdev->queue_lock);
168 if (cbq != NULL) {
169 cbq->callback(msg, nsp);
170 kfree_skb(skb);
171 cn_queue_release_callback(cbq);
172 err = 0;
175 return err;
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;
186 int len, err;
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)
195 return;
197 err = cn_call_callback(skb_get(skb));
198 if (err < 0)
199 kfree_skb(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.
207 * May sleep.
209 int cn_add_callback(struct cb_id *id, const char *name,
210 void (*callback)(struct cn_msg *,
211 struct netlink_skb_parms *))
213 int err;
214 struct cn_dev *dev = &cdev;
216 if (!cn_already_initialized)
217 return -EAGAIN;
219 err = cn_queue_add_callback(dev->cbdev, name, id, callback);
220 if (err)
221 return err;
223 return 0;
225 EXPORT_SYMBOL_GPL(cn_add_callback);
228 * Callback remove routing - removes callback
229 * with given ID.
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",
254 cbq->id.name,
255 cbq->id.id.idx,
256 cbq->id.id.val);
259 spin_unlock_bh(&dev->queue_lock);
261 return 0;
264 static struct cn_dev cdev = {
265 .input = cn_rx_skb,
268 static int cn_init(void)
270 struct cn_dev *dev = &cdev;
271 struct netlink_kernel_cfg cfg = {
272 .groups = CN_NETLINK_USERS + 0xf,
273 .input = dev->input,
276 dev->nls = netlink_kernel_create(&init_net, NETLINK_CONNECTOR, &cfg);
277 if (!dev->nls)
278 return -EIO;
280 dev->cbdev = cn_queue_alloc_dev("cqueue", dev->nls);
281 if (!dev->cbdev) {
282 netlink_kernel_release(dev->nls);
283 return -EINVAL;
286 cn_already_initialized = 1;
288 proc_create_single("connector", S_IRUGO, init_net.proc_net, cn_proc_show);
290 return 0;
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);