2.6.29 compatibility update (vfs_permission does not exist any more,
[vde.git] / ipn / af_ipn.c
blob659f675466bbb1ed9cab8123edb4da3c2a5c55c0
1 /*
2 * Main inter process networking (virtual distributed ethernet) module
3 * (part of the View-OS project: wiki.virtualsquare.org)
5 * Copyright (C) 2007 Renzo Davoli (renzo@cs.unibo.it)
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 * Due to this file being licensed under the GPL there is controversy over
13 * whether this permits you to write a module that #includes this file
14 * without placing your module under the GPL. Please consult a lawyer for
15 * advice before doing this.
17 * WARNING: THIS CODE IS ALREADY EXPERIMENTAL
21 #include <linux/init.h>
22 #include <linux/module.h>
23 #include <linux/socket.h>
24 #include <linux/namei.h>
25 #include <linux/poll.h>
26 #include <linux/un.h>
27 #include <linux/list.h>
28 #include <linux/mount.h>
29 #include <linux/version.h>
30 #include <net/sock.h>
32 #include <net/af_ipn.h>
34 #include "af_ipn.h"
35 #include "ipn_netdev.h"
36 #include "ipn_msgbuf.h"
38 MODULE_LICENSE("GPL");
39 MODULE_AUTHOR("VIEW-OS TEAM");
40 MODULE_DESCRIPTION("IPN Kernel Module");
42 #define IPN_MAX_PROTO 4
43 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25)
44 #define IPN_PRE2625
45 #endif
47 /*extension of RCV_SHUTDOWN defined in include/net/sock.h
48 * when the bit is set recv fails */
49 /* NO_OOB: do not send OOB */
50 #define RCV_SHUTDOWN_NO_OOB 4
51 /* EXTENDED MASK including OOB */
52 #define SHUTDOWN_XMASK (SHUTDOWN_MASK | RCV_SHUTDOWN_NO_OOB)
53 /* if XRCV_SHUTDOWN is all set recv fails */
54 #define XRCV_SHUTDOWN (RCV_SHUTDOWN | RCV_SHUTDOWN_NO_OOB)
56 /* Network table and hash */
57 struct hlist_head ipn_network_table[IPN_HASH_SIZE + 1];
58 /* not needed. Now protected by ipn_glob_mutex
59 * comment *IPNTL*
60 * DEFINE_SPINLOCK(ipn_table_lock);
62 static struct kmem_cache *ipn_network_cache;
63 static struct kmem_cache *ipn_node_cache;
64 static struct kmem_cache *ipn_msgitem_cache;
65 static DECLARE_MUTEX(ipn_glob_mutex);
67 /* Protocol 1: HUB/Broadcast default protocol. Function Prototypes */
68 static int ipn_bcast_newport(struct ipn_node *newport);
69 static int ipn_bcast_handlemsg(struct ipn_node *from,
70 struct msgpool_item *msgitem);
72 /* default protocol IPN_BROADCAST (0) */
73 static struct ipn_protocol ipn_bcast = {
74 .refcnt=0,
75 .ipn_p_newport=ipn_bcast_newport,
76 .ipn_p_handlemsg=ipn_bcast_handlemsg};
77 /* Protocol table */
78 static struct ipn_protocol *ipn_protocol_table[IPN_MAX_PROTO]={&ipn_bcast};
80 /* Socket call function prototypes */
81 static int ipn_release(struct socket *);
82 static int ipn_bind(struct socket *, struct sockaddr *, int);
83 static int ipn_connect(struct socket *, struct sockaddr *,
84 int addr_len, int flags);
85 static int ipn_getname(struct socket *, struct sockaddr *, int *, int);
86 static unsigned int ipn_poll(struct file *, struct socket *, poll_table *);
87 static int ipn_ioctl(struct socket *, unsigned int, unsigned long);
88 static int ipn_shutdown(struct socket *, int);
89 static int ipn_sendmsg(struct kiocb *, struct socket *,
90 struct msghdr *, size_t);
91 static int ipn_recvmsg(struct kiocb *, struct socket *,
92 struct msghdr *, size_t, int);
93 static int ipn_setsockopt(struct socket *sock, int level, int optname,
94 char __user *optval, int optlen);
95 static int ipn_getsockopt(struct socket *sock, int level, int optname,
96 char __user *optval, int __user *optlen);
98 /* Network table Management
99 * inode->ipn_network hash table
100 * LOCKING: MUTEX ipn_glob_mutex must be LOCKED*/
101 static inline void ipn_insert_network(struct hlist_head *list, struct ipn_network *ipnn)
103 /* *IPNTL* spin_lock(&ipn_table_lock); */
104 hlist_add_head(&ipnn->hnode, list);
105 /* *IPNTL* spin_unlock(&ipn_table_lock); */
108 static inline void ipn_remove_network(struct ipn_network *ipnn)
110 /* *IPNTL* spin_lock(&ipn_table_lock); */
111 hlist_del(&ipnn->hnode);
112 /* *IPNTL* spin_unlock(&ipn_table_lock); */
115 static struct ipn_network *ipn_find_network_byinode(struct inode *i)
117 struct ipn_network *ipnn;
118 struct hlist_node *node;
120 /* *IPNTL* spin_lock(&ipn_table_lock);*/
121 hlist_for_each_entry(ipnn, node,
122 &ipn_network_table[i->i_ino & (IPN_HASH_SIZE - 1)], hnode) {
123 struct dentry *dentry = ipnn->dentry;
125 if(ipnn->refcnt > 0 && dentry && dentry->d_inode == i)
126 goto found;
128 ipnn = NULL;
129 found:
130 /* *IPNTL* spin_unlock(&ipn_table_lock); */
131 return ipnn;
134 /* msgpool management
135 * msgpool_item are ipn_network dependent (each net has its own MTU)
136 * for each message sent there is one msgpool_item and many struct msgitem
137 * one for each receipient.
138 * msgitem are connected to the node's msgqueue or oobmsgqueue.
139 * when a message is delivered to a process the msgitem is deleted and
140 * the count of the msgpool_item is decreased.
141 * msgpool_item elements gets deleted automatically when count is 0*/
143 struct msgitem {
144 struct list_head list;
145 struct msgpool_item *msg;
148 /* alloc a fresh msgpool item. count is set to 1.
149 * the typical use is
150 * ipn_msgpool_alloc
151 * for each receipient
152 * enqueue messages to the process (using msgitem), ipn_msgpool_hold
153 * ipn_msgpool_put
154 * The message can be delivered concurrently. init count to 1 guarantees
155 * that it survives at least until is has been enqueued to all
156 * receivers */
157 static struct msgpool_item *_ipn_msgpool_alloc(struct ipn_network *ipnn)
159 struct msgpool_item *new;
160 if ((new=kmem_cache_alloc(ipnn->msgpool_cache,GFP_KERNEL)) != NULL) {
161 atomic_set(&new->count,1);
162 atomic_inc(&ipnn->msgpool_nelem);
164 return new;
167 struct msgpool_item *ipn_msgpool_alloc(struct ipn_network *ipnn,int leaky)
169 if (leaky && (ipnn->flags & IPN_FLAG_LOSSLESS) &&
170 atomic_read(&ipnn->msgpool_nelem) < ipnn->msgpool_size)
171 return NULL;
172 else
173 return _ipn_msgpool_alloc(ipnn);
176 /* If the service il LOSSLESS, this msgpool call waits for an
177 * available msgpool item */
178 static struct msgpool_item *ipn_msgpool_alloc_locking(struct ipn_network *ipnn)
180 if (ipnn->flags & IPN_FLAG_LOSSLESS) {
181 while (atomic_read(&ipnn->msgpool_nelem) >= ipnn->msgpool_size) {
182 if (wait_event_interruptible_exclusive(ipnn->send_wait,
183 atomic_read(&ipnn->msgpool_nelem) < ipnn->msgpool_size))
184 return NULL;
187 return _ipn_msgpool_alloc(ipnn);
190 static inline void ipn_msgpool_hold(struct msgpool_item *msg)
192 atomic_inc(&msg->count);
195 /* decrease count and delete msgpool_item if count == 0 */
196 void ipn_msgpool_put(struct msgpool_item *old,
197 struct ipn_network *ipnn)
199 if (atomic_dec_and_test(&old->count)) {
200 kmem_cache_free(ipnn->msgpool_cache,old);
201 atomic_dec(&ipnn->msgpool_nelem);
202 if (ipnn->flags & IPN_FLAG_LOSSLESS) /* this could be done anyway */
203 wake_up_interruptible(&ipnn->send_wait);
207 /* socket calls */
208 static const struct proto_ops ipn_ops = {
209 .family = PF_IPN,
210 .owner = THIS_MODULE,
211 .release = ipn_release,
212 .bind = ipn_bind,
213 .connect = ipn_connect,
214 .socketpair = sock_no_socketpair,
215 .accept = sock_no_accept,
216 .getname = ipn_getname,
217 .poll = ipn_poll,
218 .ioctl = ipn_ioctl,
219 .listen = sock_no_listen,
220 .shutdown = ipn_shutdown,
221 .setsockopt = ipn_setsockopt,
222 .getsockopt = ipn_getsockopt,
223 .sendmsg = ipn_sendmsg,
224 .recvmsg = ipn_recvmsg,
225 .mmap = sock_no_mmap,
226 .sendpage = sock_no_sendpage,
229 static struct proto ipn_proto = {
230 .name = "IPN",
231 .owner = THIS_MODULE,
232 .obj_size = sizeof(struct ipn_sock),
235 /* create a socket
236 * ipn_node is a separate structure, pointed by ipn_sock -> node
237 * when a node is "persistent", ipn_node survives while ipn_sock gets released*/
238 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
239 static int ipn_create(struct socket *sock, int protocol)
240 #else
241 static int ipn_create(struct net *net,struct socket *sock, int protocol)
242 #endif
244 struct ipn_sock *ipn_sk;
245 struct ipn_node *ipn_node;
247 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
248 void *net=NULL;
249 #else
250 if (net != &init_net)
251 return -EAFNOSUPPORT;
252 #endif
254 if (sock->type != SOCK_RAW)
255 return -EPROTOTYPE;
256 if (protocol > 0)
257 protocol=protocol-1;
258 else
259 protocol=IPN_BROADCAST-1;
260 if (protocol < 0 || protocol >= IPN_MAX_PROTO ||
261 ipn_protocol_table[protocol] == NULL)
262 return -EPROTONOSUPPORT;
263 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
264 ipn_sk = (struct ipn_sock *) sk_alloc(PF_IPN, GFP_KERNEL, &ipn_proto, 1);
265 #else
266 ipn_sk = (struct ipn_sock *) sk_alloc(net, PF_IPN, GFP_KERNEL, &ipn_proto);
267 #endif
269 if (!ipn_sk)
270 return -ENOMEM;
271 ipn_sk->node=ipn_node=kmem_cache_alloc(ipn_node_cache,GFP_KERNEL);
272 if (!ipn_node) {
273 sock_put((struct sock *) ipn_sk);
274 return -ENOMEM;
276 sock_init_data(sock,(struct sock *) ipn_sk);
277 sock->state = SS_UNCONNECTED;
278 sock->ops = &ipn_ops;
279 sock->sk=(struct sock *)ipn_sk;
280 INIT_LIST_HEAD(&ipn_node->nodelist);
281 ipn_node->protocol=protocol;
282 ipn_node->flags=IPN_NODEFLAG_INUSE;
283 ipn_node->shutdown=RCV_SHUTDOWN_NO_OOB;
284 ipn_node->descr[0]=0;
285 ipn_node->portno=IPN_PORTNO_ANY;
286 ipn_node->net=net;
287 ipn_node->dev=NULL;
288 ipn_node->proto_private=NULL;
289 ipn_node->totmsgcount=0;
290 ipn_node->oobmsgcount=0;
291 spin_lock_init(&ipn_node->msglock);
292 INIT_LIST_HEAD(&ipn_node->msgqueue);
293 INIT_LIST_HEAD(&ipn_node->oobmsgqueue);
294 ipn_node->ipn=NULL;
295 init_waitqueue_head(&ipn_node->read_wait);
296 ipn_node->pbp=NULL;
297 return 0;
300 /* update # of readers and # of writers counters for an ipn network.
301 * This function sends oob messages to nodes requesting the service */
302 /* LOCKING ipnn_mutex is locked */
303 static void ipn_net_update_counters(struct ipn_network *ipnn,
304 int chg_readers, int chg_writers) {
305 ipnn->numreaders += chg_readers;
306 ipnn->numwriters += chg_writers;
307 if (ipnn->mtu >= sizeof(struct numnode_oob))
309 struct msgpool_item *ipn_msg=_ipn_msgpool_alloc(ipnn);
310 if (ipn_msg) {
311 struct numnode_oob *oob_msg=(struct numnode_oob *)(ipn_msg->data);
312 struct ipn_node *ipn_node;
313 ipn_msg->len=sizeof(struct numnode_oob);
314 oob_msg->level=IPN_ANY;
315 oob_msg->tag=IPN_OOB_NUMNODE_TAG;
316 oob_msg->numreaders=ipnn->numreaders;
317 oob_msg->numwriters=ipnn->numwriters;
318 list_for_each_entry(ipn_node, &ipnn->connectqueue, nodelist) {
319 if (ipn_node->flags & IPN_NODEFLAG_OOB_NUMNODES)
320 ipn_proto_oobsendmsg(ipn_node,ipn_msg);
322 ipn_msgpool_put(ipn_msg,ipnn);
327 /* flush pending messages (for close and shutdown RCV) */
328 /* LOCKING: ipnn_mutex is locked */
329 static void ipn_flush_recvqueue(struct ipn_node *ipn_node)
331 struct ipn_network *ipnn=ipn_node->ipn;
332 spin_lock(&ipn_node->msglock);
333 while (!list_empty(&ipn_node->msgqueue)) {
334 struct msgitem *msgitem=
335 list_first_entry(&ipn_node->msgqueue, struct msgitem, list);
336 list_del(&msgitem->list);
337 ipn_node->totmsgcount--;
338 ipn_msgpool_put(msgitem->msg,ipnn);
339 kmem_cache_free(ipn_msgitem_cache,msgitem);
341 spin_unlock(&ipn_node->msglock);
344 /* flush pending oob messages (for socket close) */
345 /* LOCKING: ipnn_mutex is locked */
346 static void ipn_flush_oobrecvqueue(struct ipn_node *ipn_node)
348 struct ipn_network *ipnn=ipn_node->ipn;
349 spin_lock(&ipn_node->msglock);
350 while (!list_empty(&ipn_node->oobmsgqueue)) {
351 struct msgitem *msgitem=
352 list_first_entry(&ipn_node->oobmsgqueue, struct msgitem, list);
353 list_del(&msgitem->list);
354 ipn_node->totmsgcount--;
355 ipn_node->oobmsgcount--;
356 ipn_msgpool_put(msgitem->msg,ipnn);
357 kmem_cache_free(ipn_msgitem_cache,msgitem);
359 spin_unlock(&ipn_node->msglock);
362 /* Terminate node. The node is "logically" terminated. */
363 /* LOCKING: ipn_glob_lock must be locked here */
364 static int ipn_terminate_node(struct ipn_node *ipn_node)
366 struct ipn_network *ipnn=ipn_node->ipn;
367 if (ipnn) {
368 if (down_interruptible(&ipnn->ipnn_mutex))
369 return -ERESTARTSYS;
370 if (ipn_node->portno >= 0) {
371 ipn_protocol_table[ipnn->protocol]->ipn_p_predelport(ipn_node);
372 ipnn->connport[ipn_node->portno]=NULL;
374 list_del(&ipn_node->nodelist);
375 ipn_flush_recvqueue(ipn_node);
376 ipn_flush_oobrecvqueue(ipn_node);
377 if (ipn_node->portno >= 0)
378 ipn_protocol_table[ipnn->protocol]->ipn_p_delport(ipn_node);
379 ipn_node->ipn=NULL;
380 ipn_net_update_counters(ipnn,
381 (ipn_node->shutdown & RCV_SHUTDOWN)?0:-1,
382 (ipn_node->shutdown & SEND_SHUTDOWN)?0:-1);
383 ipn_node->shutdown = SHUTDOWN_XMASK;
384 up(&ipnn->ipnn_mutex);
385 if (ipn_node->dev)
386 ipn_netdev_close(ipn_node);
387 /* No more network elements */
388 ipnn->refcnt--;
389 if (ipnn->refcnt == 0)
391 ipn_protocol_table[ipnn->protocol]->ipn_p_delnet(ipnn);
392 ipn_remove_network(ipnn);
393 ipn_protocol_table[ipnn->protocol]->refcnt--;
394 if (ipnn->dentry) {
395 dput(ipnn->dentry);
396 mntput(ipnn->mnt);
398 if (ipnn->msgpool_cache)
399 ipn_msgbuf_put(ipnn->msgpool_cache);
400 if (ipnn->connport)
401 kfree(ipnn->connport);
402 kmem_cache_free(ipn_network_cache, ipnn);
403 module_put(THIS_MODULE);
406 if (ipn_node->pbp) {
407 kfree(ipn_node->pbp);
408 ipn_node->pbp=NULL;
410 return 0;
413 /* release of a socket */
414 static int ipn_release (struct socket *sock)
416 struct ipn_sock *ipn_sk=(struct ipn_sock *)sock->sk;
417 struct ipn_node *ipn_node=ipn_sk->node;
418 int rv;
419 if (down_interruptible(&ipn_glob_mutex))
420 return -ERESTARTSYS;
421 if (ipn_node->flags & IPN_NODEFLAG_PERSIST) {
422 ipn_node->flags &= ~IPN_NODEFLAG_INUSE;
423 rv=0;
424 up(&ipn_glob_mutex);
425 } else {
426 rv=ipn_terminate_node(ipn_node);
427 up(&ipn_glob_mutex);
428 if (rv==0) {
429 ipn_netdevsync();
430 kmem_cache_free(ipn_node_cache,ipn_node);
433 if (rv==0)
434 sock_put((struct sock *) ipn_sk);
435 return rv;
438 /* _set persist, change the persistence of a node,
439 * when persistence gets cleared and the node is no longer used
440 * the node is terminated and freed.
441 * ipn_glob_mutex must be locked */
442 static int _ipn_setpersist(struct ipn_node *ipn_node, int persist)
444 int rv=0;
445 if (persist)
446 ipn_node->flags |= IPN_NODEFLAG_PERSIST;
447 else {
448 ipn_node->flags &= ~IPN_NODEFLAG_PERSIST;
449 if (!(ipn_node->flags & IPN_NODEFLAG_INUSE)) {
450 rv=ipn_terminate_node(ipn_node);
451 if (rv==0)
452 kmem_cache_free(ipn_node_cache,ipn_node);
455 return rv;
458 /* ipn_setpersist
459 * lock ipn_glob_mutex and call __ipn_setpersist above */
460 static int ipn_setpersist(struct ipn_node *ipn_node, int persist)
462 int rv=0;
463 if (ipn_node->dev == NULL)
464 return -ENODEV;
465 if (down_interruptible(&ipn_glob_mutex))
466 return -ERESTARTSYS;
467 rv=_ipn_setpersist(ipn_node,persist);
468 up(&ipn_glob_mutex);
469 return rv;
472 /* several network parameters can be set by setsockopt prior to bind */
473 /* struct pre_bind_parms is a temporary stucture connected to ipn_node->pbp
474 * to keep the parameter values. */
475 struct pre_bind_parms {
476 unsigned short maxports;
477 unsigned short flags;
478 unsigned short msgpoolsize;
479 unsigned short mtu;
480 unsigned short mode;
483 /* STD_PARMS: BITS_PER_LONG nodes, no flags, BITS_PER_BYTE pending msgs,
484 * Ethernet + VLAN MTU*/
485 #define STD_BIND_PARMS {BITS_PER_LONG, 0, BITS_PER_BYTE, 1514, 0x777};
487 static int ipn_mkname(struct sockaddr_un * sunaddr, int len)
489 if (len <= sizeof(short) || len > sizeof(*sunaddr))
490 return -EINVAL;
491 if (!sunaddr || sunaddr->sun_family != AF_IPN)
492 return -EINVAL;
494 * This may look like an off by one error but it is a bit more
495 * subtle. 108 is the longest valid AF_IPN path for a binding.
496 * sun_path[108] doesnt as such exist. However in kernel space
497 * we are guaranteed that it is a valid memory location in our
498 * kernel address buffer.
500 ((char *)sunaddr)[len]=0;
501 len = strlen(sunaddr->sun_path)+1+sizeof(short);
502 return len;
505 /* IPN BIND */
506 static int ipn_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
508 struct sockaddr_un *sunaddr=(struct sockaddr_un *)uaddr;
509 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
510 struct nameidata nd;
511 struct ipn_network *ipnn;
512 struct dentry * dentry = NULL;
513 int err;
514 struct pre_bind_parms parms=STD_BIND_PARMS;
516 //printk("IPN bind\n");
518 if (down_interruptible(&ipn_glob_mutex))
519 return -ERESTARTSYS;
520 if (sock->state != SS_UNCONNECTED ||
521 ipn_node->ipn != NULL) {
522 err= -EISCONN;
523 goto out;
526 if (ipn_node->protocol >= 0 &&
527 (ipn_node->protocol >= IPN_MAX_PROTO ||
528 ipn_protocol_table[ipn_node->protocol] == NULL)) {
529 err= -EPROTONOSUPPORT;
530 goto out;
533 addr_len = ipn_mkname(sunaddr, addr_len);
534 if (addr_len < 0) {
535 err=addr_len;
536 goto out;
539 /* check if there is already an ipn-network socket with that name */
540 err = path_lookup(sunaddr->sun_path, LOOKUP_FOLLOW, &nd);
541 if (err) { /* it does not exist, NEW IPN socket! */
542 unsigned int mode;
543 /* Is it everything okay with the parent? */
544 err = path_lookup(sunaddr->sun_path, LOOKUP_PARENT, &nd);
545 if (err)
546 goto out_mknod_parent;
547 /* Do I have the permission to create a file? */
548 dentry = lookup_create(&nd, 0);
549 err = PTR_ERR(dentry);
550 if (IS_ERR(dentry))
551 goto out_mknod_unlock;
553 * All right, let's create it.
555 if (ipn_node->pbp)
556 mode = ipn_node->pbp->mode;
557 else
558 mode = SOCK_INODE(sock)->i_mode;
559 mode = S_IFSOCK | (mode & ~current->fs->umask);
560 #ifndef IPN_PRE2625
561 #ifdef APPARMOR
562 err = vfs_mknod(nd.path.dentry->d_inode, dentry, nd.path.mnt, mode, 0);
563 #else
564 err = vfs_mknod(nd.path.dentry->d_inode, dentry, mode, 0);
565 #endif
566 #else
567 #ifdef APPARMOR
568 err = vfs_mknod(nd.dentry->d_inode, dentry, nd.path.mnt, mode, 0);
569 #else
570 err = vfs_mknod(nd.dentry->d_inode, dentry, mode, 0);
571 #endif
572 #endif
573 if (err)
574 goto out_mknod_dput;
575 #ifndef IPN_PRE2625
576 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
577 dput(nd.path.dentry);
578 nd.path.dentry = dentry;
579 #else
580 mutex_unlock(&nd.dentry->d_inode->i_mutex);
581 dput(nd.dentry);
582 nd.dentry = dentry;
583 #endif
584 /* create a new ipn_network item */
585 if (ipn_node->pbp)
586 parms=*ipn_node->pbp;
587 ipnn=kmem_cache_zalloc(ipn_network_cache,GFP_KERNEL);
588 if (!ipnn) {
589 err=-ENOMEM;
590 goto out_mknod_dput_ipnn;
592 ipnn->connport=kzalloc(parms.maxports * sizeof(struct ipn_node *),GFP_KERNEL);
593 if (!ipnn->connport) {
594 err=-ENOMEM;
595 goto out_mknod_dput_ipnn2;
598 /* module refcnt is incremented for each network, thus
599 * rmmod is forbidden if there are persistent node */
600 if (!try_module_get(THIS_MODULE)) {
601 err = -EINVAL;
602 goto out_mknod_dput_ipnn2;
604 memcpy(&ipnn->sunaddr,sunaddr,addr_len);
605 ipnn->mtu=parms.mtu;
606 ipnn->msgpool_cache=ipn_msgbuf_get(ipnn->mtu);
607 if (!ipnn->msgpool_cache) {
608 err=-ENOMEM;
609 goto out_mknod_dput_putmodule;
611 INIT_LIST_HEAD(&ipnn->unconnectqueue);
612 INIT_LIST_HEAD(&ipnn->connectqueue);
613 ipnn->refcnt=1;
614 #ifndef IPN_PRE2625
615 ipnn->dentry=nd.path.dentry;
616 ipnn->mnt=nd.path.mnt;
617 #else
618 ipnn->dentry=nd.dentry;
619 ipnn->mnt=nd.mnt;
620 #endif
621 init_MUTEX(&ipnn->ipnn_mutex);
622 ipnn->sunaddr_len=addr_len;
623 ipnn->protocol=ipn_node->protocol;
624 if (ipnn->protocol < 0) ipnn->protocol = 0;
625 ipn_protocol_table[ipnn->protocol]->refcnt++;
626 ipnn->flags=parms.flags;
627 ipnn->numreaders=0;
628 ipnn->numwriters=0;
629 ipnn->maxports=parms.maxports;
630 atomic_set(&ipnn->msgpool_nelem,0);
631 ipnn->msgpool_size=parms.msgpoolsize;
632 ipnn->proto_private=NULL;
633 init_waitqueue_head(&ipnn->send_wait);
634 err=ipn_protocol_table[ipnn->protocol]->ipn_p_newnet(ipnn);
635 if (err)
636 goto out_mknod_dput_putmodule;
637 #ifndef IPN_PRE2625
638 ipn_insert_network(&ipn_network_table[nd.path.dentry->d_inode->i_ino & (IPN_HASH_SIZE-1)],ipnn);
639 #else
640 ipn_insert_network(&ipn_network_table[nd.dentry->d_inode->i_ino & (IPN_HASH_SIZE-1)],ipnn);
641 #endif
642 } else {
643 /* join an existing network */
644 if (parms.flags & IPN_FLAG_EXCL) {
645 err=-EEXIST;
646 goto put_fail;
648 err = inode_permission(nd.path.dentry->d_inode, MAY_EXEC);
649 if (err)
650 goto put_fail;
651 err = -ECONNREFUSED;
652 #ifndef IPN_PRE2625
653 if (!S_ISSOCK(nd.path.dentry->d_inode->i_mode))
654 goto put_fail;
655 ipnn=ipn_find_network_byinode(nd.path.dentry->d_inode);
656 #else
657 if (!S_ISSOCK(nd.dentry->d_inode->i_mode))
658 goto put_fail;
659 ipnn=ipn_find_network_byinode(nd.dentry->d_inode);
660 #endif
661 if (!ipnn || (ipnn->flags & IPN_FLAG_TERMINATED) ||
662 (ipnn->flags & IPN_FLAG_EXCL))
663 goto put_fail;
664 list_add_tail(&ipn_node->nodelist,&ipnn->unconnectqueue);
665 ipnn->refcnt++;
667 if (ipn_node->pbp) {
668 kfree(ipn_node->pbp);
669 ipn_node->pbp=NULL;
671 ipn_node->ipn=ipnn;
672 ipn_node->flags |= IPN_NODEFLAG_BOUND;
673 up(&ipn_glob_mutex);
674 return 0;
676 put_fail:
677 #ifndef IPN_PRE2625
678 path_put(&nd.path);
679 #else
680 path_release(&nd);
681 #endif
682 out:
683 up(&ipn_glob_mutex);
684 return err;
686 out_mknod_dput_putmodule:
687 module_put(THIS_MODULE);
688 out_mknod_dput_ipnn2:
689 kfree(ipnn->connport);
690 out_mknod_dput_ipnn:
691 kmem_cache_free(ipn_network_cache,ipnn);
692 out_mknod_dput:
693 dput(dentry);
694 out_mknod_unlock:
695 #ifndef IPN_PRE2625
696 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
697 path_put(&nd.path);
698 #else
699 mutex_unlock(&nd.dentry->d_inode->i_mutex);
700 path_release(&nd);
701 #endif
702 out_mknod_parent:
703 if (err==-EEXIST)
704 err=-EADDRINUSE;
705 up(&ipn_glob_mutex);
706 return err;
709 /* IPN CONNECT */
710 static int ipn_connect(struct socket *sock, struct sockaddr *addr,
711 int addr_len, int flags){
712 struct sockaddr_un *sunaddr=(struct sockaddr_un*)addr;
713 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
714 struct nameidata nd;
715 struct ipn_network *ipnn,*previousipnn;
716 int err=0;
717 int portno;
719 /* the socket cannot be connected twice */
720 if (sock->state != SS_UNCONNECTED)
721 return EISCONN;
723 if (down_interruptible(&ipn_glob_mutex))
724 return -ERESTARTSYS;
726 if ((previousipnn=ipn_node->ipn) == NULL) { /* unbound */
727 unsigned char mustshutdown=0;
728 err = ipn_mkname(sunaddr, addr_len);
729 if (err < 0)
730 goto out;
731 addr_len=err;
732 err = path_lookup(sunaddr->sun_path, LOOKUP_FOLLOW, &nd);
733 if (err)
734 goto out;
735 err = inode_permission(nd.path.dentry->d_inode, MAY_READ);
736 if (err) {
737 if (err == -EACCES || err == -EROFS)
738 mustshutdown|=RCV_SHUTDOWN;
739 else
740 goto put_fail;
742 err = inode_permission(nd.path.dentry->d_inode, MAY_WRITE);
743 if (err) {
744 if (err == -EACCES)
745 mustshutdown|=SEND_SHUTDOWN;
746 else
747 goto put_fail;
749 mustshutdown |= ipn_node->shutdown;
750 /* if the combination of shutdown and permissions leaves
751 * no abilities, connect returns EACCES */
752 if (mustshutdown == SHUTDOWN_XMASK) {
753 err=-EACCES;
754 goto put_fail;
755 } else {
756 err=0;
757 ipn_node->shutdown=mustshutdown;
759 #ifndef IPN_PRE2625
760 if (!S_ISSOCK(nd.path.dentry->d_inode->i_mode)) {
761 err = -ECONNREFUSED;
762 goto put_fail;
764 ipnn=ipn_find_network_byinode(nd.path.dentry->d_inode);
765 #else
766 if (!S_ISSOCK(nd.dentry->d_inode->i_mode)) {
767 err = -ECONNREFUSED;
768 goto put_fail;
770 ipnn=ipn_find_network_byinode(nd.dentry->d_inode);
771 #endif
772 if (!ipnn || (ipnn->flags & IPN_FLAG_TERMINATED)) {
773 err = -ECONNREFUSED;
774 goto put_fail;
776 if (ipn_node->protocol == IPN_ANY)
777 ipn_node->protocol=ipnn->protocol;
778 else if (ipnn->protocol != ipn_node->protocol) {
779 err = -EPROTO;
780 goto put_fail;
782 #ifndef IPN_PRE2625
783 path_put(&nd.path);
784 #else
785 path_release(&nd);
786 #endif
787 ipn_node->ipn=ipnn;
788 } else
789 ipnn=ipn_node->ipn;
791 if (down_interruptible(&ipnn->ipnn_mutex)) {
792 err=-ERESTARTSYS;
793 goto out;
795 portno = ipn_protocol_table[ipnn->protocol]->ipn_p_newport(ipn_node);
796 if (portno >= 0 && portno<ipnn->maxports) {
797 sock->state = SS_CONNECTED;
798 ipn_node->portno=portno;
799 ipnn->connport[portno]=ipn_node;
800 if (!(ipn_node->flags & IPN_NODEFLAG_BOUND)) {
801 ipnn->refcnt++;
802 list_del(&ipn_node->nodelist);
804 list_add_tail(&ipn_node->nodelist,&ipnn->connectqueue);
805 ipn_net_update_counters(ipnn,
806 (ipn_node->shutdown & RCV_SHUTDOWN)?0:1,
807 (ipn_node->shutdown & SEND_SHUTDOWN)?0:1);
808 } else {
809 ipn_node->ipn=previousipnn; /* undo changes on ipn_node->ipn */
810 err=-EADDRNOTAVAIL;
812 up(&ipnn->ipnn_mutex);
813 up(&ipn_glob_mutex);
814 return err;
816 put_fail:
817 #ifndef IPN_PRE2625
818 path_put(&nd.path);
819 #else
820 path_release(&nd);
821 #endif
822 out:
823 up(&ipn_glob_mutex);
824 return err;
827 static int ipn_getname(struct socket *sock, struct sockaddr *uaddr,
828 int *uaddr_len, int peer) {
829 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
830 struct ipn_network *ipnn=ipn_node->ipn;
831 struct sockaddr_un *sunaddr=(struct sockaddr_un *)uaddr;
832 int err=0;
834 if (down_interruptible(&ipn_glob_mutex))
835 return -ERESTARTSYS;
836 if (ipnn) {
837 *uaddr_len = ipnn->sunaddr_len;
838 memcpy(sunaddr,&ipnn->sunaddr,*uaddr_len);
839 } else
840 err = -ENOTCONN;
841 up(&ipn_glob_mutex);
842 return err;
845 /* IPN POLL */
846 static unsigned int ipn_poll(struct file *file, struct socket *sock,
847 poll_table *wait) {
848 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
849 struct ipn_network *ipnn=ipn_node->ipn;
850 unsigned int mask=0;
852 if (ipnn) {
853 poll_wait(file,&ipn_node->read_wait,wait);
854 if (ipnn->flags & IPN_FLAG_LOSSLESS)
855 poll_wait(file,&ipnn->send_wait,wait);
856 /* POLLIN if recv succeeds,
857 * POLL{PRI,RDNORM} if there are {oob,non-oob} messages */
858 if (ipn_node->totmsgcount > 0) mask |= POLLIN;
859 if (!(list_empty(&ipn_node->msgqueue))) mask |= POLLRDNORM;
860 if (!(list_empty(&ipn_node->oobmsgqueue))) mask |= POLLPRI;
861 if ((!(ipnn->flags & IPN_FLAG_LOSSLESS)) |
862 (atomic_read(&ipnn->msgpool_nelem) < ipnn->msgpool_size))
863 mask |= POLLOUT | POLLWRNORM;
865 return mask;
868 /* connect netdev (from ioctl). connect a bound socket to a
869 * network device TAP or GRAB */
870 static int ipn_connect_netdev(struct socket *sock,struct ifreq *ifr)
872 int err=0;
873 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
874 struct ipn_network *ipnn=ipn_node->ipn;
875 if (!capable(CAP_NET_ADMIN))
876 return -EPERM;
877 if (sock->state != SS_UNCONNECTED)
878 return -EISCONN;
879 if (!ipnn)
880 return -ENOTCONN; /* Maybe we need a different error for "NOT BOUND" */
881 if (down_interruptible(&ipn_glob_mutex))
882 return -ERESTARTSYS;
883 if (down_interruptible(&ipnn->ipnn_mutex)) {
884 up(&ipn_glob_mutex);
885 return -ERESTARTSYS;
887 ipn_node->dev=ipn_netdev_alloc(ipn_node->net,ifr->ifr_flags,ifr->ifr_name,&err);
888 if (ipn_node->dev) {
889 int portno;
890 portno = ipn_protocol_table[ipnn->protocol]->ipn_p_newport(ipn_node);
891 if (portno >= 0 && portno<ipnn->maxports) {
892 sock->state = SS_CONNECTED;
893 ipn_node->portno=portno;
894 ipn_node->flags |= ifr->ifr_flags & IPN_NODEFLAG_DEVMASK;
895 ipnn->connport[portno]=ipn_node;
896 err=ipn_netdev_activate(ipn_node);
897 if (err) {
898 sock->state = SS_UNCONNECTED;
899 ipn_protocol_table[ipnn->protocol]->ipn_p_delport(ipn_node);
900 ipn_node->dev=NULL;
901 ipn_node->portno= -1;
902 ipn_node->flags &= ~IPN_NODEFLAG_DEVMASK;
903 ipnn->connport[portno]=NULL;
904 } else {
905 ipn_protocol_table[ipnn->protocol]->ipn_p_postnewport(ipn_node);
906 list_del(&ipn_node->nodelist);
907 list_add_tail(&ipn_node->nodelist,&ipnn->connectqueue);
909 } else {
910 ipn_netdev_close(ipn_node);
911 err=-EADDRNOTAVAIL;
912 ipn_node->dev=NULL;
914 } else
915 err=-EINVAL;
916 up(&ipnn->ipnn_mutex);
917 up(&ipn_glob_mutex);
918 return err;
921 /* join a netdev, a socket gets connected to a persistent node
922 * not connected to another socket */
923 static int ipn_join_netdev(struct socket *sock,struct ifreq *ifr)
925 int err=0;
926 struct net_device *dev;
927 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
928 struct ipn_node *ipn_joined;
929 struct ipn_network *ipnn=ipn_node->ipn;
930 if (sock->state != SS_UNCONNECTED)
931 return -EISCONN;
932 if (down_interruptible(&ipn_glob_mutex))
933 return -ERESTARTSYS;
934 if (down_interruptible(&ipnn->ipnn_mutex)) {
935 up(&ipn_glob_mutex);
936 return -ERESTARTSYS;
938 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
939 dev=__dev_get_by_name(ifr->ifr_name);
940 #else
941 dev=__dev_get_by_name(ipn_node->net,ifr->ifr_name);
942 #endif
943 if (!dev)
944 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
945 dev=__dev_get_by_index(ifr->ifr_ifindex);
946 #else
947 dev=__dev_get_by_index(ipn_node->net,ifr->ifr_ifindex);
948 #endif
949 if (dev && (ipn_joined=ipn_netdev2node(dev)) != NULL) { /* the interface does exist */
950 int i;
951 for (i=0;i<ipnn->maxports && ipn_joined != ipnn->connport[i] ;i++)
953 if (i < ipnn->maxports) { /* found */
954 /* ipn_joined is substituted to ipn_node */
955 ((struct ipn_sock *)sock->sk)->node=ipn_joined;
956 ipn_joined->flags |= IPN_NODEFLAG_INUSE;
957 ipnn->refcnt--;
958 kmem_cache_free(ipn_node_cache,ipn_node);
959 } else
960 err=-EPERM;
961 } else
962 err=-EADDRNOTAVAIL;
963 up(&ipnn->ipnn_mutex);
964 up(&ipn_glob_mutex);
965 return err;
968 /* set persistence of a node looking for it by interface name
969 * (it is for sysadm, to close network interfaces)*/
970 static int ipn_setpersist_netdev(struct ifreq *ifr, int value)
972 struct net_device *dev;
973 struct ipn_node *ipn_node;
974 int err=0;
975 if (!capable(CAP_NET_ADMIN))
976 return -EPERM;
977 if (down_interruptible(&ipn_glob_mutex))
978 return -ERESTARTSYS;
979 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
980 dev=__dev_get_by_name(ifr->ifr_name);
981 #else
982 dev=__dev_get_by_name(&init_net,ifr->ifr_name);
983 #endif
984 if (!dev)
985 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
986 dev=__dev_get_by_index(ifr->ifr_ifindex);
987 #else
988 dev=__dev_get_by_index(&init_net,ifr->ifr_ifindex);
989 #endif
990 if (dev && (ipn_node=ipn_netdev2node(dev)) != NULL)
991 _ipn_setpersist(ipn_node,value);
992 else
993 err=-EADDRNOTAVAIL;
994 up(&ipn_glob_mutex);
995 return err;
998 /* IPN IOCTL */
999 static int ipn_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) {
1000 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
1001 struct ipn_network *ipnn=ipn_node->ipn;
1002 void __user* argp = (void __user*)arg;
1003 struct ifreq ifr;
1005 if (ipn_node->shutdown == SHUTDOWN_XMASK)
1006 return -ECONNRESET;
1008 /* get arguments */
1009 switch (cmd) {
1010 case IPN_CHECK:
1011 return IPN_CHECK;
1012 case IPN_SETPERSIST_NETDEV:
1013 case IPN_CLRPERSIST_NETDEV:
1014 case IPN_CONN_NETDEV:
1015 case IPN_JOIN_NETDEV:
1016 case SIOCSIFHWADDR:
1017 if (copy_from_user(&ifr, argp, sizeof ifr))
1018 return -EFAULT;
1019 ifr.ifr_name[IFNAMSIZ-1] = '\0';
1022 /* actions for unconnected and unbound sockets */
1023 switch (cmd) {
1024 case IPN_SETPERSIST_NETDEV:
1025 return ipn_setpersist_netdev(&ifr,1);
1026 case IPN_CLRPERSIST_NETDEV:
1027 return ipn_setpersist_netdev(&ifr,0);
1028 case SIOCSIFHWADDR:
1029 if (capable(CAP_NET_ADMIN))
1030 return -EPERM;
1031 if (ipn_node->dev && (ipn_node->flags &IPN_NODEFLAG_TAP))
1032 return dev_set_mac_address(ipn_node->dev, &ifr.ifr_hwaddr);
1033 else
1034 return -EADDRNOTAVAIL;
1036 if (ipnn == NULL || (ipnn->flags & IPN_FLAG_TERMINATED))
1037 return -ENOTCONN;
1038 /* actions for connected or bound sockets */
1039 switch (cmd) {
1040 case IPN_CONN_NETDEV:
1041 return ipn_connect_netdev(sock,&ifr);
1042 case IPN_JOIN_NETDEV:
1043 return ipn_join_netdev(sock,&ifr);
1044 case IPN_SETPERSIST:
1045 return ipn_setpersist(ipn_node,arg);
1046 default:
1047 if (ipnn) {
1048 int rv;
1049 if (down_interruptible(&ipnn->ipnn_mutex))
1050 return -ERESTARTSYS;
1051 rv=ipn_protocol_table[ipn_node->protocol]->ipn_p_ioctl(ipn_node,cmd,arg);
1052 up(&ipnn->ipnn_mutex);
1053 return rv;
1054 } else
1055 return -EOPNOTSUPP;
1059 /* shutdown: close socket for input or for output.
1060 * shutdown can be called prior to connect and it is not reversible */
1061 static int ipn_shutdown(struct socket *sock, int mode) {
1062 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
1063 struct ipn_network *ipnn=ipn_node->ipn;
1064 int oldshutdown=ipn_node->shutdown;
1065 mode = (mode+1)&(RCV_SHUTDOWN|SEND_SHUTDOWN);
1067 ipn_node->shutdown |= mode;
1069 if(ipnn) {
1070 if (down_interruptible(&ipnn->ipnn_mutex)) {
1071 ipn_node->shutdown = oldshutdown;
1072 return -ERESTARTSYS;
1074 oldshutdown=ipn_node->shutdown-oldshutdown;
1075 if (sock->state == SS_CONNECTED && oldshutdown) {
1076 ipn_net_update_counters(ipnn,
1077 (ipn_node->shutdown & RCV_SHUTDOWN)?0:-1,
1078 (ipn_node->shutdown & SEND_SHUTDOWN)?0:-1);
1081 /* if recv channel has been shut down, flush the recv queue */
1082 if ((ipn_node->shutdown & RCV_SHUTDOWN))
1083 ipn_flush_recvqueue(ipn_node);
1084 up(&ipnn->ipnn_mutex);
1086 return 0;
1089 /* injectmsg: a new message is entering the ipn network.
1090 * injectmsg gets called by send and by the grab/tap node */
1091 int ipn_proto_injectmsg(struct ipn_node *from, struct msgpool_item *msg)
1093 struct ipn_network *ipnn=from->ipn;
1094 int err=0;
1095 if (down_interruptible(&ipnn->ipnn_mutex))
1096 err=-ERESTARTSYS;
1097 else {
1098 ipn_protocol_table[ipnn->protocol]->ipn_p_handlemsg(from, msg);
1099 up(&ipnn->ipnn_mutex);
1101 return err;
1104 /* SEND MSG */
1105 static int ipn_sendmsg(struct kiocb *kiocb, struct socket *sock,
1106 struct msghdr *msg, size_t len) {
1107 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
1108 struct ipn_network *ipnn=ipn_node->ipn;
1109 struct msgpool_item *newmsg;
1110 int err=0;
1112 if (unlikely(sock->state != SS_CONNECTED))
1113 return -ENOTCONN;
1114 if (unlikely(ipn_node->shutdown & SEND_SHUTDOWN)) {
1115 if (ipn_node->shutdown == SHUTDOWN_XMASK)
1116 return -ECONNRESET;
1117 else
1118 return -EPIPE;
1120 if (len > ipnn->mtu)
1121 return -EOVERFLOW;
1122 newmsg=ipn_msgpool_alloc_locking(ipnn);
1123 if (!newmsg)
1124 return -ENOMEM;
1125 newmsg->len=len;
1126 err=memcpy_fromiovec(newmsg->data, msg->msg_iov, len);
1127 if (!err)
1128 ipn_proto_injectmsg(ipn_node, newmsg);
1129 ipn_msgpool_put(newmsg,ipnn);
1130 return err;
1133 /* enqueue an oob message. "to" is the destination */
1134 void ipn_proto_oobsendmsg(struct ipn_node *to, struct msgpool_item *msg)
1136 if (to) {
1137 if (!to->dev) { /* no oob to netdev */
1138 struct msgitem *msgitem;
1139 struct ipn_network *ipnn=to->ipn;
1140 spin_lock(&to->msglock);
1141 if ((to->shutdown & RCV_SHUTDOWN_NO_OOB) == 0 &&
1142 (ipnn->flags & IPN_FLAG_LOSSLESS ||
1143 to->oobmsgcount < ipnn->msgpool_size)) {
1144 if ((msgitem=kmem_cache_alloc(ipn_msgitem_cache,GFP_KERNEL))!=NULL) {
1145 msgitem->msg=msg;
1146 to->totmsgcount++;
1147 to->oobmsgcount++;
1148 list_add_tail(&msgitem->list, &to->oobmsgqueue);
1149 ipn_msgpool_hold(msg);
1152 spin_unlock(&to->msglock);
1153 wake_up_interruptible(&to->read_wait);
1158 /* ipn_proto_sendmsg is called by protocol implementation to enqueue a
1159 * for a destination (to).*/
1160 void ipn_proto_sendmsg(struct ipn_node *to, struct msgpool_item *msg)
1162 if (to) {
1163 if (to->dev) {
1164 ipn_netdev_sendmsg(to,msg);
1165 } else {
1166 /* socket send */
1167 struct msgitem *msgitem;
1168 struct ipn_network *ipnn=to->ipn;
1169 spin_lock(&to->msglock);
1170 if (likely((to->shutdown & RCV_SHUTDOWN)==0)) {
1171 /*if (unlikely((ipnn->flags & IPN_FLAG_LOSSLESS) == 0 ||
1172 to->totmsgcount >= ipnn->msgpool_size))
1173 yield();*/
1174 if (likely(ipnn->flags & IPN_FLAG_LOSSLESS ||
1175 to->totmsgcount < ipnn->msgpool_size)) {
1176 if ((msgitem=kmem_cache_alloc(ipn_msgitem_cache,GFP_KERNEL))!=NULL) {
1177 msgitem->msg=msg;
1178 to->totmsgcount++;
1179 list_add_tail(&msgitem->list, &to->msgqueue);
1180 ipn_msgpool_hold(msg);
1184 spin_unlock(&to->msglock);
1185 wake_up_interruptible(&to->read_wait);
1190 /* IPN RECV */
1191 static int ipn_recvmsg(struct kiocb *kiocb, struct socket *sock,
1192 struct msghdr *msg, size_t len, int flags) {
1193 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
1194 struct ipn_network *ipnn=ipn_node->ipn;
1195 struct msgitem *msgitem;
1196 struct msgpool_item *currmsg;
1198 if (unlikely(sock->state != SS_CONNECTED))
1199 return -ENOTCONN;
1201 if (unlikely((ipn_node->shutdown & XRCV_SHUTDOWN) == XRCV_SHUTDOWN)) {
1202 if (ipn_node->shutdown == SHUTDOWN_XMASK) /*EOF, nothing can be read*/
1203 return 0;
1204 else
1205 return -EPIPE; /*trying to read on a write only node */
1208 /* wait for a message */
1209 spin_lock(&ipn_node->msglock);
1210 while (ipn_node->totmsgcount == 0) {
1211 spin_unlock(&ipn_node->msglock);
1212 if (wait_event_interruptible(ipn_node->read_wait,
1213 !(ipn_node->totmsgcount == 0)))
1214 return -ERESTARTSYS;
1215 spin_lock(&ipn_node->msglock);
1217 /* oob gets delivered first. oob are rare */
1218 if (likely(list_empty(&ipn_node->oobmsgqueue)))
1219 msgitem=list_first_entry(&ipn_node->msgqueue, struct msgitem, list);
1220 else {
1221 msgitem=list_first_entry(&ipn_node->oobmsgqueue, struct msgitem, list);
1222 msg->msg_flags |= MSG_OOB;
1223 ipn_node->oobmsgcount--;
1225 list_del(&msgitem->list);
1226 ipn_node->totmsgcount--;
1227 spin_unlock(&ipn_node->msglock);
1228 currmsg=msgitem->msg;
1229 if (currmsg->len < len)
1230 len=currmsg->len;
1231 memcpy_toiovec(msg->msg_iov, currmsg->data, len);
1232 ipn_msgpool_put(currmsg,ipnn);
1233 kmem_cache_free(ipn_msgitem_cache,msgitem);
1235 return len;
1238 /* resize a network: change the # of communication ports (connport) */
1239 static int ipn_netresize(struct ipn_network *ipnn,int newsize)
1241 int oldsize,min;
1242 struct ipn_node **newconnport;
1243 struct ipn_node **oldconnport;
1244 int err;
1245 if (down_interruptible(&ipnn->ipnn_mutex))
1246 return -ERESTARTSYS;
1247 oldsize=ipnn->maxports;
1248 if (newsize == oldsize) {
1249 up(&ipnn->ipnn_mutex);
1250 return 0;
1252 min=oldsize;
1253 /* shrink a network. all the ports we are going to eliminate
1254 * must be unused! */
1255 if (newsize < oldsize) {
1256 int i;
1257 for (i=newsize; i<oldsize; i++)
1258 if (ipnn->connport[i]) {
1259 up(&ipnn->ipnn_mutex);
1260 return -EADDRINUSE;
1262 min=newsize;
1264 oldconnport=ipnn->connport;
1265 /* allocate the new connport array and copy the old one */
1266 newconnport=kzalloc(newsize * sizeof(struct ipn_node *),GFP_KERNEL);
1267 if (!newconnport) {
1268 up(&ipnn->ipnn_mutex);
1269 return -ENOMEM;
1271 memcpy(newconnport,oldconnport,min * sizeof(struct ipn_node *));
1272 ipnn->connport=newconnport;
1273 ipnn->maxports=newsize;
1274 /* notify the protocol that the netowrk has been resized */
1275 err=ipn_protocol_table[ipnn->protocol]->ipn_p_resizenet(ipnn,oldsize,newsize);
1276 if (err) {
1277 /* roll back if the resize operation failed for the protocol */
1278 ipnn->connport=oldconnport;
1279 ipnn->maxports=oldsize;
1280 kfree(newconnport);
1281 } else
1282 /* successful mission, network resized */
1283 kfree(oldconnport);
1284 up(&ipnn->ipnn_mutex);
1285 return err;
1288 /* IPN SETSOCKOPT */
1289 static int ipn_setsockopt(struct socket *sock, int level, int optname,
1290 char __user *optval, int optlen) {
1291 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
1292 struct ipn_network *ipnn=ipn_node->ipn;
1294 if (ipn_node->shutdown == SHUTDOWN_XMASK)
1295 return -ECONNRESET;
1296 if (level != 0 && level != ipn_node->protocol+1)
1297 return -EPROTONOSUPPORT;
1298 if (level > 0) {
1299 /* protocol specific sockopt */
1300 if (ipnn) {
1301 int rv;
1302 if (down_interruptible(&ipnn->ipnn_mutex))
1303 return -ERESTARTSYS;
1304 rv=ipn_protocol_table[ipn_node->protocol]->ipn_p_setsockopt(ipn_node,optname,optval,optlen);
1305 up(&ipnn->ipnn_mutex);
1306 return rv;
1307 } else
1308 return -EOPNOTSUPP;
1309 } else {
1310 if (optname == IPN_SO_DESCR) {
1311 if (optlen > IPN_DESCRLEN)
1312 return -EINVAL;
1313 else {
1314 memset(ipn_node->descr,0,IPN_DESCRLEN);
1315 if (copy_from_user(ipn_node->descr,optval,optlen))
1316 ipn_node->descr[0]=0;
1317 else
1318 ipn_node->descr[optlen-1]=0;
1319 return 0;
1321 } else {
1322 if (optlen < sizeof(int))
1323 return -EINVAL;
1324 else if ((optname & IPN_SO_PREBIND) && (ipnn != NULL))
1325 return -EISCONN;
1326 else {
1327 int val;
1328 get_user(val, (int __user *) optval);
1329 if ((optname & IPN_SO_PREBIND) && !ipn_node->pbp) {
1330 struct pre_bind_parms std=STD_BIND_PARMS;
1331 ipn_node->pbp=kzalloc(sizeof(struct pre_bind_parms),GFP_KERNEL);
1332 if (!ipn_node->pbp)
1333 return -ENOMEM;
1334 *(ipn_node->pbp)=std;
1336 switch (optname) {
1337 case IPN_SO_PORT:
1338 if (sock->state == SS_UNCONNECTED)
1339 ipn_node->portno=val;
1340 else
1341 return -EISCONN;
1342 break;
1343 case IPN_SO_CHANGE_NUMNODES:
1344 if ((ipn_node->flags & IPN_NODEFLAG_BOUND)!=0) {
1345 if (val <= 0)
1346 return -EINVAL;
1347 else
1348 return ipn_netresize(ipnn,val);
1349 } else
1350 val=-ENOTCONN;
1351 break;
1352 case IPN_SO_WANT_OOB_NUMNODES:
1353 if (val)
1354 ipn_node->flags |= IPN_NODEFLAG_OOB_NUMNODES;
1355 else
1356 ipn_node->flags &= ~IPN_NODEFLAG_OOB_NUMNODES;
1357 break;
1358 case IPN_SO_HANDLE_OOB:
1359 if (val)
1360 ipn_node->shutdown &= ~RCV_SHUTDOWN_NO_OOB;
1361 else
1362 ipn_node->shutdown |= RCV_SHUTDOWN_NO_OOB;
1363 break;
1364 case IPN_SO_MTU:
1365 if (val <= 0)
1366 return -EINVAL;
1367 else
1368 ipn_node->pbp->mtu=val;
1369 break;
1370 case IPN_SO_NUMNODES:
1371 if (val <= 0)
1372 return -EINVAL;
1373 else
1374 ipn_node->pbp->maxports=val;
1375 break;
1376 case IPN_SO_MSGPOOLSIZE:
1377 if (val <= 0)
1378 return -EINVAL;
1379 else
1380 ipn_node->pbp->msgpoolsize=val;
1381 break;
1382 case IPN_SO_FLAGS:
1383 ipn_node->pbp->flags=val;
1384 break;
1385 case IPN_SO_MODE:
1386 ipn_node->pbp->mode=val;
1387 break;
1389 return 0;
1395 /* IPN GETSOCKOPT */
1396 static int ipn_getsockopt(struct socket *sock, int level, int optname,
1397 char __user *optval, int __user *optlen) {
1398 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
1399 struct ipn_network *ipnn=ipn_node->ipn;
1400 int len;
1402 if (ipn_node->shutdown == SHUTDOWN_XMASK)
1403 return -ECONNRESET;
1404 if (level != 0 && level != ipn_node->protocol+1)
1405 return -EPROTONOSUPPORT;
1406 if (level > 0) {
1407 if (ipnn) {
1408 int rv;
1409 /* protocol specific sockopt */
1410 if (down_interruptible(&ipnn->ipnn_mutex))
1411 return -ERESTARTSYS;
1412 rv=ipn_protocol_table[ipn_node->protocol]->ipn_p_getsockopt(ipn_node,optname,optval,optlen);
1413 up(&ipnn->ipnn_mutex);
1414 return rv;
1415 } else
1416 return -EOPNOTSUPP;
1417 } else {
1418 if (get_user(len, optlen))
1419 return -EFAULT;
1420 if (optname == IPN_SO_DESCR) {
1421 if (len < IPN_DESCRLEN)
1422 return -EINVAL;
1423 else {
1424 if (len > IPN_DESCRLEN)
1425 len=IPN_DESCRLEN;
1426 if(put_user(len, optlen))
1427 return -EFAULT;
1428 if(copy_to_user(optval,ipn_node->descr,len))
1429 return -EFAULT;
1430 return 0;
1432 } else {
1433 int val=-2;
1434 switch (optname) {
1435 case IPN_SO_PORT:
1436 val=ipn_node->portno;
1437 break;
1438 case IPN_SO_MTU:
1439 if (ipnn)
1440 val=ipnn->mtu;
1441 else if (ipn_node->pbp)
1442 val=ipn_node->pbp->mtu;
1443 break;
1444 case IPN_SO_NUMNODES:
1445 if (ipnn)
1446 val=ipnn->maxports;
1447 else if (ipn_node->pbp)
1448 val=ipn_node->pbp->maxports;
1449 break;
1450 case IPN_SO_MSGPOOLSIZE:
1451 if (ipnn)
1452 val=ipnn->msgpool_size;
1453 else if (ipn_node->pbp)
1454 val=ipn_node->pbp->msgpoolsize;
1455 break;
1456 case IPN_SO_FLAGS:
1457 if (ipnn)
1458 val=ipnn->flags;
1459 else if (ipn_node->pbp)
1460 val=ipn_node->pbp->flags;
1461 break;
1462 case IPN_SO_MODE:
1463 if (ipnn)
1464 val=-1;
1465 else if (ipn_node->pbp)
1466 val=ipn_node->pbp->mode;
1467 break;
1469 if (val < -1)
1470 return -EINVAL;
1471 else {
1472 if (len < sizeof(int))
1473 return -EOVERFLOW;
1474 else {
1475 len = sizeof(int);
1476 if(put_user(len, optlen))
1477 return -EFAULT;
1478 if(copy_to_user(optval,&val,len))
1479 return -EFAULT;
1480 return 0;
1487 /* BROADCAST/HUB implementation */
1489 static int ipn_bcast_newport(struct ipn_node *newport) {
1490 struct ipn_network *ipnn=newport->ipn;
1491 int i;
1492 for (i=0;i<ipnn->maxports;i++) {
1493 if (ipnn->connport[i] == NULL)
1494 return i;
1496 return -1;
1499 static int ipn_bcast_handlemsg(struct ipn_node *from,
1500 struct msgpool_item *msgitem){
1501 struct ipn_network *ipnn=from->ipn;
1503 struct ipn_node *ipn_node;
1504 list_for_each_entry(ipn_node, &ipnn->connectqueue, nodelist) {
1505 if (ipn_node != from)
1506 ipn_proto_sendmsg(ipn_node,msgitem);
1508 return 0;
1511 static void ipn_null_delport(struct ipn_node *oldport) {}
1512 static void ipn_null_postnewport(struct ipn_node *newport) {}
1513 static void ipn_null_predelport(struct ipn_node *oldport) {}
1514 static int ipn_null_newnet(struct ipn_network *newnet) {return 0;}
1515 static int ipn_null_resizenet(struct ipn_network *net,int oldsize,int newsize) {
1516 return 0;}
1517 static void ipn_null_delnet(struct ipn_network *oldnet) {}
1518 static int ipn_null_setsockopt(struct ipn_node *port,int optname,
1519 char __user *optval, int optlen) {return -EOPNOTSUPP;}
1520 static int ipn_null_getsockopt(struct ipn_node *port,int optname,
1521 char __user *optval, int *optlen) {return -EOPNOTSUPP;}
1522 static int ipn_null_ioctl(struct ipn_node *port,unsigned int request,
1523 unsigned long arg) {return -EOPNOTSUPP;}
1525 /* Protocol Registration/deregisteration */
1527 void ipn_init_protocol(struct ipn_protocol *p)
1529 if (p->ipn_p_delport == NULL) p->ipn_p_delport=ipn_null_delport;
1530 if (p->ipn_p_postnewport == NULL) p->ipn_p_postnewport=ipn_null_postnewport;
1531 if (p->ipn_p_predelport == NULL) p->ipn_p_predelport=ipn_null_predelport;
1532 if (p->ipn_p_newnet == NULL) p->ipn_p_newnet=ipn_null_newnet;
1533 if (p->ipn_p_resizenet == NULL) p->ipn_p_resizenet=ipn_null_resizenet;
1534 if (p->ipn_p_delnet == NULL) p->ipn_p_delnet=ipn_null_delnet;
1535 if (p->ipn_p_setsockopt == NULL) p->ipn_p_setsockopt=ipn_null_setsockopt;
1536 if (p->ipn_p_getsockopt == NULL) p->ipn_p_getsockopt=ipn_null_getsockopt;
1537 if (p->ipn_p_ioctl == NULL) p->ipn_p_ioctl=ipn_null_ioctl;
1540 int ipn_proto_register(int protocol,struct ipn_protocol *ipn_service)
1542 int rv=0;
1543 if (ipn_service->ipn_p_newport == NULL ||
1544 ipn_service->ipn_p_handlemsg == NULL)
1545 return -EINVAL;
1546 ipn_init_protocol(ipn_service);
1547 if (down_interruptible(&ipn_glob_mutex))
1548 return -ERESTARTSYS;
1549 if (protocol > 1 && protocol <= IPN_MAX_PROTO) {
1550 protocol--;
1551 if (ipn_protocol_table[protocol])
1552 rv= -EEXIST;
1553 else {
1554 ipn_service->refcnt=0;
1555 ipn_protocol_table[protocol]=ipn_service;
1556 printk(KERN_INFO "IPN: Registered protocol %d\n",protocol+1);
1558 } else
1559 rv= -EINVAL;
1560 up(&ipn_glob_mutex);
1561 return rv;
1564 int ipn_proto_deregister(int protocol)
1566 int rv=0;
1567 if (down_interruptible(&ipn_glob_mutex))
1568 return -ERESTARTSYS;
1569 if (protocol > 1 && protocol <= IPN_MAX_PROTO) {
1570 protocol--;
1571 if (ipn_protocol_table[protocol]) {
1572 if (ipn_protocol_table[protocol]->refcnt == 0) {
1573 ipn_protocol_table[protocol]=NULL;
1574 printk(KERN_INFO "IPN: Unregistered protocol %d\n",protocol+1);
1575 } else
1576 rv=-EADDRINUSE;
1577 } else
1578 rv= -ENOENT;
1579 } else
1580 rv= -EINVAL;
1581 up(&ipn_glob_mutex);
1582 return rv;
1585 /* MAIN SECTION */
1586 /* Module constructor/destructor */
1587 static struct net_proto_family ipn_family_ops = {
1588 .family = PF_IPN,
1589 .create = ipn_create,
1590 .owner = THIS_MODULE,
1593 /* IPN constructor */
1594 static int ipn_init(void)
1596 int rc;
1598 ipn_init_protocol(&ipn_bcast);
1599 ipn_network_cache=kmem_cache_create("ipn_network",sizeof(struct ipn_network),0,0,NULL);
1600 if (!ipn_network_cache) {
1601 printk(KERN_CRIT "%s: Cannot create ipn_network SLAB cache!\n",
1602 __FUNCTION__);
1603 rc=-ENOMEM;
1604 goto out;
1607 ipn_node_cache=kmem_cache_create("ipn_node",sizeof(struct ipn_node),0,0,NULL);
1608 if (!ipn_node_cache) {
1609 printk(KERN_CRIT "%s: Cannot create ipn_node SLAB cache!\n",
1610 __FUNCTION__);
1611 rc=-ENOMEM;
1612 goto out_net;
1615 ipn_msgitem_cache=kmem_cache_create("ipn_msgitem",sizeof(struct msgitem),0,0,NULL);
1616 if (!ipn_msgitem_cache) {
1617 printk(KERN_CRIT "%s: Cannot create ipn_msgitem SLAB cache!\n",
1618 __FUNCTION__);
1619 rc=-ENOMEM;
1620 goto out_net_node;
1623 rc=ipn_msgbuf_init();
1624 if (rc != 0) {
1625 printk(KERN_CRIT "%s: Cannot create ipn_msgbuf SLAB cache\n",
1626 __FUNCTION__);
1627 goto out_net_node_msg;
1630 rc=proto_register(&ipn_proto,1);
1631 if (rc != 0) {
1632 printk(KERN_CRIT "%s: Cannot register the protocol!\n",
1633 __FUNCTION__);
1634 goto out_net_node_msg_msgbuf;
1637 sock_register(&ipn_family_ops);
1638 ipn_netdev_init();
1639 printk(KERN_INFO "IPN: Virtual Square Project, University of Bologna 2007\n");
1640 return 0;
1642 out_net_node_msg_msgbuf:
1643 ipn_msgbuf_fini();
1644 out_net_node_msg:
1645 kmem_cache_destroy(ipn_msgitem_cache);
1646 out_net_node:
1647 kmem_cache_destroy(ipn_node_cache);
1648 out_net:
1649 kmem_cache_destroy(ipn_network_cache);
1650 out:
1651 return rc;
1654 /* IPN destructor */
1655 static void ipn_exit(void)
1657 ipn_netdev_fini();
1658 if (ipn_msgitem_cache)
1659 kmem_cache_destroy(ipn_msgitem_cache);
1660 if (ipn_node_cache)
1661 kmem_cache_destroy(ipn_node_cache);
1662 if (ipn_network_cache)
1663 kmem_cache_destroy(ipn_network_cache);
1664 ipn_msgbuf_fini();
1665 sock_unregister(PF_IPN);
1666 proto_unregister(&ipn_proto);
1667 printk(KERN_INFO "IPN removed\n");
1670 module_init(ipn_init);
1671 module_exit(ipn_exit);
1673 EXPORT_SYMBOL_GPL(ipn_proto_register);
1674 EXPORT_SYMBOL_GPL(ipn_proto_deregister);
1675 EXPORT_SYMBOL_GPL(ipn_proto_sendmsg);
1676 EXPORT_SYMBOL_GPL(ipn_proto_oobsendmsg);
1677 EXPORT_SYMBOL_GPL(ipn_msgpool_alloc);
1678 EXPORT_SYMBOL_GPL(ipn_msgpool_put);