2.6.30 bugfix: current->fs->umask is no longer supported current_umask() must be...
[vde.git] / ipn / af_ipn.c
blob8fd27d599a512a18b78f02e5334f5f1175ed1722
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, 0777};
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 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 30)
560 mode = S_IFSOCK | (mode & ~current->fs->umask);
561 #else
562 mode = S_IFSOCK | (mode & ~current_umask());
563 #endif
564 #ifndef IPN_PRE2625
565 #ifdef APPARMOR
566 err = vfs_mknod(nd.path.dentry->d_inode, dentry, nd.path.mnt, mode, 0);
567 #else
568 err = vfs_mknod(nd.path.dentry->d_inode, dentry, mode, 0);
569 #endif
570 #else
571 #ifdef APPARMOR
572 err = vfs_mknod(nd.dentry->d_inode, dentry, nd.path.mnt, mode, 0);
573 #else
574 err = vfs_mknod(nd.dentry->d_inode, dentry, mode, 0);
575 #endif
576 #endif
577 if (err)
578 goto out_mknod_dput;
579 #ifndef IPN_PRE2625
580 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
581 dput(nd.path.dentry);
582 nd.path.dentry = dentry;
583 #else
584 mutex_unlock(&nd.dentry->d_inode->i_mutex);
585 dput(nd.dentry);
586 nd.dentry = dentry;
587 #endif
588 /* create a new ipn_network item */
589 if (ipn_node->pbp)
590 parms=*ipn_node->pbp;
591 ipnn=kmem_cache_zalloc(ipn_network_cache,GFP_KERNEL);
592 if (!ipnn) {
593 err=-ENOMEM;
594 goto out_mknod_dput_ipnn;
596 ipnn->connport=kzalloc(parms.maxports * sizeof(struct ipn_node *),GFP_KERNEL);
597 if (!ipnn->connport) {
598 err=-ENOMEM;
599 goto out_mknod_dput_ipnn2;
602 /* module refcnt is incremented for each network, thus
603 * rmmod is forbidden if there are persistent node */
604 if (!try_module_get(THIS_MODULE)) {
605 err = -EINVAL;
606 goto out_mknod_dput_ipnn2;
608 memcpy(&ipnn->sunaddr,sunaddr,addr_len);
609 ipnn->mtu=parms.mtu;
610 ipnn->msgpool_cache=ipn_msgbuf_get(ipnn->mtu);
611 if (!ipnn->msgpool_cache) {
612 err=-ENOMEM;
613 goto out_mknod_dput_putmodule;
615 INIT_LIST_HEAD(&ipnn->unconnectqueue);
616 INIT_LIST_HEAD(&ipnn->connectqueue);
617 ipnn->refcnt=1;
618 #ifndef IPN_PRE2625
619 ipnn->dentry=nd.path.dentry;
620 ipnn->mnt=nd.path.mnt;
621 #else
622 ipnn->dentry=nd.dentry;
623 ipnn->mnt=nd.mnt;
624 #endif
625 init_MUTEX(&ipnn->ipnn_mutex);
626 ipnn->sunaddr_len=addr_len;
627 ipnn->protocol=ipn_node->protocol;
628 if (ipnn->protocol < 0) ipnn->protocol = 0;
629 ipn_protocol_table[ipnn->protocol]->refcnt++;
630 ipnn->flags=parms.flags;
631 ipnn->numreaders=0;
632 ipnn->numwriters=0;
633 ipnn->maxports=parms.maxports;
634 atomic_set(&ipnn->msgpool_nelem,0);
635 ipnn->msgpool_size=parms.msgpoolsize;
636 ipnn->proto_private=NULL;
637 init_waitqueue_head(&ipnn->send_wait);
638 err=ipn_protocol_table[ipnn->protocol]->ipn_p_newnet(ipnn);
639 if (err)
640 goto out_mknod_dput_putmodule;
641 #ifndef IPN_PRE2625
642 ipn_insert_network(&ipn_network_table[nd.path.dentry->d_inode->i_ino & (IPN_HASH_SIZE-1)],ipnn);
643 #else
644 ipn_insert_network(&ipn_network_table[nd.dentry->d_inode->i_ino & (IPN_HASH_SIZE-1)],ipnn);
645 #endif
646 } else {
647 /* join an existing network */
648 if (parms.flags & IPN_FLAG_EXCL) {
649 err=-EEXIST;
650 goto put_fail;
652 err = inode_permission(nd.path.dentry->d_inode, MAY_EXEC);
653 if (err)
654 goto put_fail;
655 err = -ECONNREFUSED;
656 #ifndef IPN_PRE2625
657 if (!S_ISSOCK(nd.path.dentry->d_inode->i_mode))
658 goto put_fail;
659 ipnn=ipn_find_network_byinode(nd.path.dentry->d_inode);
660 #else
661 if (!S_ISSOCK(nd.dentry->d_inode->i_mode))
662 goto put_fail;
663 ipnn=ipn_find_network_byinode(nd.dentry->d_inode);
664 #endif
665 if (!ipnn || (ipnn->flags & IPN_FLAG_TERMINATED) ||
666 (ipnn->flags & IPN_FLAG_EXCL))
667 goto put_fail;
668 list_add_tail(&ipn_node->nodelist,&ipnn->unconnectqueue);
669 ipnn->refcnt++;
671 if (ipn_node->pbp) {
672 kfree(ipn_node->pbp);
673 ipn_node->pbp=NULL;
675 ipn_node->ipn=ipnn;
676 ipn_node->flags |= IPN_NODEFLAG_BOUND;
677 up(&ipn_glob_mutex);
678 return 0;
680 put_fail:
681 #ifndef IPN_PRE2625
682 path_put(&nd.path);
683 #else
684 path_release(&nd);
685 #endif
686 out:
687 up(&ipn_glob_mutex);
688 return err;
690 out_mknod_dput_putmodule:
691 module_put(THIS_MODULE);
692 out_mknod_dput_ipnn2:
693 kfree(ipnn->connport);
694 out_mknod_dput_ipnn:
695 kmem_cache_free(ipn_network_cache,ipnn);
696 out_mknod_dput:
697 dput(dentry);
698 out_mknod_unlock:
699 #ifndef IPN_PRE2625
700 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
701 path_put(&nd.path);
702 #else
703 mutex_unlock(&nd.dentry->d_inode->i_mutex);
704 path_release(&nd);
705 #endif
706 out_mknod_parent:
707 if (err==-EEXIST)
708 err=-EADDRINUSE;
709 up(&ipn_glob_mutex);
710 return err;
713 /* IPN CONNECT */
714 static int ipn_connect(struct socket *sock, struct sockaddr *addr,
715 int addr_len, int flags){
716 struct sockaddr_un *sunaddr=(struct sockaddr_un*)addr;
717 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
718 struct nameidata nd;
719 struct ipn_network *ipnn,*previousipnn;
720 int err=0;
721 int portno;
723 /* the socket cannot be connected twice */
724 if (sock->state != SS_UNCONNECTED)
725 return EISCONN;
727 if (down_interruptible(&ipn_glob_mutex))
728 return -ERESTARTSYS;
730 if ((previousipnn=ipn_node->ipn) == NULL) { /* unbound */
731 unsigned char mustshutdown=0;
732 err = ipn_mkname(sunaddr, addr_len);
733 if (err < 0)
734 goto out;
735 addr_len=err;
736 err = path_lookup(sunaddr->sun_path, LOOKUP_FOLLOW, &nd);
737 if (err)
738 goto out;
739 err = inode_permission(nd.path.dentry->d_inode, MAY_READ);
740 if (err) {
741 if (err == -EACCES || err == -EROFS)
742 mustshutdown|=RCV_SHUTDOWN;
743 else
744 goto put_fail;
746 err = inode_permission(nd.path.dentry->d_inode, MAY_WRITE);
747 if (err) {
748 if (err == -EACCES)
749 mustshutdown|=SEND_SHUTDOWN;
750 else
751 goto put_fail;
753 mustshutdown |= ipn_node->shutdown;
754 /* if the combination of shutdown and permissions leaves
755 * no abilities, connect returns EACCES */
756 if (mustshutdown == SHUTDOWN_XMASK) {
757 err=-EACCES;
758 goto put_fail;
759 } else {
760 err=0;
761 ipn_node->shutdown=mustshutdown;
763 #ifndef IPN_PRE2625
764 if (!S_ISSOCK(nd.path.dentry->d_inode->i_mode)) {
765 err = -ECONNREFUSED;
766 goto put_fail;
768 ipnn=ipn_find_network_byinode(nd.path.dentry->d_inode);
769 #else
770 if (!S_ISSOCK(nd.dentry->d_inode->i_mode)) {
771 err = -ECONNREFUSED;
772 goto put_fail;
774 ipnn=ipn_find_network_byinode(nd.dentry->d_inode);
775 #endif
776 if (!ipnn || (ipnn->flags & IPN_FLAG_TERMINATED)) {
777 err = -ECONNREFUSED;
778 goto put_fail;
780 if (ipn_node->protocol == IPN_ANY)
781 ipn_node->protocol=ipnn->protocol;
782 else if (ipnn->protocol != ipn_node->protocol) {
783 err = -EPROTO;
784 goto put_fail;
786 #ifndef IPN_PRE2625
787 path_put(&nd.path);
788 #else
789 path_release(&nd);
790 #endif
791 ipn_node->ipn=ipnn;
792 } else
793 ipnn=ipn_node->ipn;
795 if (down_interruptible(&ipnn->ipnn_mutex)) {
796 err=-ERESTARTSYS;
797 goto out;
799 portno = ipn_protocol_table[ipnn->protocol]->ipn_p_newport(ipn_node);
800 if (portno >= 0 && portno<ipnn->maxports) {
801 sock->state = SS_CONNECTED;
802 ipn_node->portno=portno;
803 ipnn->connport[portno]=ipn_node;
804 if (!(ipn_node->flags & IPN_NODEFLAG_BOUND)) {
805 ipnn->refcnt++;
806 list_del(&ipn_node->nodelist);
808 list_add_tail(&ipn_node->nodelist,&ipnn->connectqueue);
809 ipn_net_update_counters(ipnn,
810 (ipn_node->shutdown & RCV_SHUTDOWN)?0:1,
811 (ipn_node->shutdown & SEND_SHUTDOWN)?0:1);
812 } else {
813 ipn_node->ipn=previousipnn; /* undo changes on ipn_node->ipn */
814 err=-EADDRNOTAVAIL;
816 up(&ipnn->ipnn_mutex);
817 up(&ipn_glob_mutex);
818 return err;
820 put_fail:
821 #ifndef IPN_PRE2625
822 path_put(&nd.path);
823 #else
824 path_release(&nd);
825 #endif
826 out:
827 up(&ipn_glob_mutex);
828 return err;
831 static int ipn_getname(struct socket *sock, struct sockaddr *uaddr,
832 int *uaddr_len, int peer) {
833 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
834 struct ipn_network *ipnn=ipn_node->ipn;
835 struct sockaddr_un *sunaddr=(struct sockaddr_un *)uaddr;
836 int err=0;
838 if (down_interruptible(&ipn_glob_mutex))
839 return -ERESTARTSYS;
840 if (ipnn) {
841 *uaddr_len = ipnn->sunaddr_len;
842 memcpy(sunaddr,&ipnn->sunaddr,*uaddr_len);
843 } else
844 err = -ENOTCONN;
845 up(&ipn_glob_mutex);
846 return err;
849 /* IPN POLL */
850 static unsigned int ipn_poll(struct file *file, struct socket *sock,
851 poll_table *wait) {
852 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
853 struct ipn_network *ipnn=ipn_node->ipn;
854 unsigned int mask=0;
856 if (ipnn) {
857 poll_wait(file,&ipn_node->read_wait,wait);
858 if (ipnn->flags & IPN_FLAG_LOSSLESS)
859 poll_wait(file,&ipnn->send_wait,wait);
860 /* POLLIN if recv succeeds,
861 * POLL{PRI,RDNORM} if there are {oob,non-oob} messages */
862 if (ipn_node->totmsgcount > 0) mask |= POLLIN;
863 if (!(list_empty(&ipn_node->msgqueue))) mask |= POLLRDNORM;
864 if (!(list_empty(&ipn_node->oobmsgqueue))) mask |= POLLPRI;
865 if ((!(ipnn->flags & IPN_FLAG_LOSSLESS)) |
866 (atomic_read(&ipnn->msgpool_nelem) < ipnn->msgpool_size))
867 mask |= POLLOUT | POLLWRNORM;
869 return mask;
872 /* connect netdev (from ioctl). connect a bound socket to a
873 * network device TAP or GRAB */
874 static int ipn_connect_netdev(struct socket *sock,struct ifreq *ifr)
876 int err=0;
877 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
878 struct ipn_network *ipnn=ipn_node->ipn;
879 if (!capable(CAP_NET_ADMIN))
880 return -EPERM;
881 if (sock->state != SS_UNCONNECTED)
882 return -EISCONN;
883 if (!ipnn)
884 return -ENOTCONN; /* Maybe we need a different error for "NOT BOUND" */
885 if (down_interruptible(&ipn_glob_mutex))
886 return -ERESTARTSYS;
887 if (down_interruptible(&ipnn->ipnn_mutex)) {
888 up(&ipn_glob_mutex);
889 return -ERESTARTSYS;
891 ipn_node->dev=ipn_netdev_alloc(ipn_node->net,ifr->ifr_flags,ifr->ifr_name,&err);
892 if (ipn_node->dev) {
893 int portno;
894 portno = ipn_protocol_table[ipnn->protocol]->ipn_p_newport(ipn_node);
895 if (portno >= 0 && portno<ipnn->maxports) {
896 sock->state = SS_CONNECTED;
897 ipn_node->portno=portno;
898 ipn_node->flags |= ifr->ifr_flags & IPN_NODEFLAG_DEVMASK;
899 ipnn->connport[portno]=ipn_node;
900 err=ipn_netdev_activate(ipn_node);
901 if (err) {
902 sock->state = SS_UNCONNECTED;
903 ipn_protocol_table[ipnn->protocol]->ipn_p_delport(ipn_node);
904 ipn_node->dev=NULL;
905 ipn_node->portno= -1;
906 ipn_node->flags &= ~IPN_NODEFLAG_DEVMASK;
907 ipnn->connport[portno]=NULL;
908 } else {
909 ipn_protocol_table[ipnn->protocol]->ipn_p_postnewport(ipn_node);
910 list_del(&ipn_node->nodelist);
911 list_add_tail(&ipn_node->nodelist,&ipnn->connectqueue);
913 } else {
914 ipn_netdev_close(ipn_node);
915 err=-EADDRNOTAVAIL;
916 ipn_node->dev=NULL;
918 } else
919 err=-EINVAL;
920 up(&ipnn->ipnn_mutex);
921 up(&ipn_glob_mutex);
922 return err;
925 /* join a netdev, a socket gets connected to a persistent node
926 * not connected to another socket */
927 static int ipn_join_netdev(struct socket *sock,struct ifreq *ifr)
929 int err=0;
930 struct net_device *dev;
931 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
932 struct ipn_node *ipn_joined;
933 struct ipn_network *ipnn=ipn_node->ipn;
934 if (sock->state != SS_UNCONNECTED)
935 return -EISCONN;
936 if (down_interruptible(&ipn_glob_mutex))
937 return -ERESTARTSYS;
938 if (down_interruptible(&ipnn->ipnn_mutex)) {
939 up(&ipn_glob_mutex);
940 return -ERESTARTSYS;
942 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
943 dev=__dev_get_by_name(ifr->ifr_name);
944 #else
945 dev=__dev_get_by_name(ipn_node->net,ifr->ifr_name);
946 #endif
947 if (!dev)
948 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
949 dev=__dev_get_by_index(ifr->ifr_ifindex);
950 #else
951 dev=__dev_get_by_index(ipn_node->net,ifr->ifr_ifindex);
952 #endif
953 if (dev && (ipn_joined=ipn_netdev2node(dev)) != NULL) { /* the interface does exist */
954 int i;
955 for (i=0;i<ipnn->maxports && ipn_joined != ipnn->connport[i] ;i++)
957 if (i < ipnn->maxports) { /* found */
958 /* ipn_joined is substituted to ipn_node */
959 ((struct ipn_sock *)sock->sk)->node=ipn_joined;
960 ipn_joined->flags |= IPN_NODEFLAG_INUSE;
961 ipnn->refcnt--;
962 kmem_cache_free(ipn_node_cache,ipn_node);
963 } else
964 err=-EPERM;
965 } else
966 err=-EADDRNOTAVAIL;
967 up(&ipnn->ipnn_mutex);
968 up(&ipn_glob_mutex);
969 return err;
972 /* set persistence of a node looking for it by interface name
973 * (it is for sysadm, to close network interfaces)*/
974 static int ipn_setpersist_netdev(struct ifreq *ifr, int value)
976 struct net_device *dev;
977 struct ipn_node *ipn_node;
978 int err=0;
979 if (!capable(CAP_NET_ADMIN))
980 return -EPERM;
981 if (down_interruptible(&ipn_glob_mutex))
982 return -ERESTARTSYS;
983 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
984 dev=__dev_get_by_name(ifr->ifr_name);
985 #else
986 dev=__dev_get_by_name(&init_net,ifr->ifr_name);
987 #endif
988 if (!dev)
989 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
990 dev=__dev_get_by_index(ifr->ifr_ifindex);
991 #else
992 dev=__dev_get_by_index(&init_net,ifr->ifr_ifindex);
993 #endif
994 if (dev && (ipn_node=ipn_netdev2node(dev)) != NULL)
995 _ipn_setpersist(ipn_node,value);
996 else
997 err=-EADDRNOTAVAIL;
998 up(&ipn_glob_mutex);
999 return err;
1002 /* IPN IOCTL */
1003 static int ipn_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) {
1004 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
1005 struct ipn_network *ipnn=ipn_node->ipn;
1006 void __user* argp = (void __user*)arg;
1007 struct ifreq ifr;
1009 if (ipn_node->shutdown == SHUTDOWN_XMASK)
1010 return -ECONNRESET;
1012 /* get arguments */
1013 switch (cmd) {
1014 case IPN_CHECK:
1015 return IPN_CHECK;
1016 case IPN_SETPERSIST_NETDEV:
1017 case IPN_CLRPERSIST_NETDEV:
1018 case IPN_CONN_NETDEV:
1019 case IPN_JOIN_NETDEV:
1020 case SIOCSIFHWADDR:
1021 if (copy_from_user(&ifr, argp, sizeof ifr))
1022 return -EFAULT;
1023 ifr.ifr_name[IFNAMSIZ-1] = '\0';
1026 /* actions for unconnected and unbound sockets */
1027 switch (cmd) {
1028 case IPN_SETPERSIST_NETDEV:
1029 return ipn_setpersist_netdev(&ifr,1);
1030 case IPN_CLRPERSIST_NETDEV:
1031 return ipn_setpersist_netdev(&ifr,0);
1032 case SIOCSIFHWADDR:
1033 if (capable(CAP_NET_ADMIN))
1034 return -EPERM;
1035 if (ipn_node->dev && (ipn_node->flags &IPN_NODEFLAG_TAP))
1036 return dev_set_mac_address(ipn_node->dev, &ifr.ifr_hwaddr);
1037 else
1038 return -EADDRNOTAVAIL;
1040 if (ipnn == NULL || (ipnn->flags & IPN_FLAG_TERMINATED))
1041 return -ENOTCONN;
1042 /* actions for connected or bound sockets */
1043 switch (cmd) {
1044 case IPN_CONN_NETDEV:
1045 return ipn_connect_netdev(sock,&ifr);
1046 case IPN_JOIN_NETDEV:
1047 return ipn_join_netdev(sock,&ifr);
1048 case IPN_SETPERSIST:
1049 return ipn_setpersist(ipn_node,arg);
1050 default:
1051 if (ipnn) {
1052 int rv;
1053 if (down_interruptible(&ipnn->ipnn_mutex))
1054 return -ERESTARTSYS;
1055 rv=ipn_protocol_table[ipn_node->protocol]->ipn_p_ioctl(ipn_node,cmd,arg);
1056 up(&ipnn->ipnn_mutex);
1057 return rv;
1058 } else
1059 return -EOPNOTSUPP;
1063 /* shutdown: close socket for input or for output.
1064 * shutdown can be called prior to connect and it is not reversible */
1065 static int ipn_shutdown(struct socket *sock, int mode) {
1066 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
1067 struct ipn_network *ipnn=ipn_node->ipn;
1068 int oldshutdown=ipn_node->shutdown;
1069 mode = (mode+1)&(RCV_SHUTDOWN|SEND_SHUTDOWN);
1071 ipn_node->shutdown |= mode;
1073 if(ipnn) {
1074 if (down_interruptible(&ipnn->ipnn_mutex)) {
1075 ipn_node->shutdown = oldshutdown;
1076 return -ERESTARTSYS;
1078 oldshutdown=ipn_node->shutdown-oldshutdown;
1079 if (sock->state == SS_CONNECTED && oldshutdown) {
1080 ipn_net_update_counters(ipnn,
1081 (ipn_node->shutdown & RCV_SHUTDOWN)?0:-1,
1082 (ipn_node->shutdown & SEND_SHUTDOWN)?0:-1);
1085 /* if recv channel has been shut down, flush the recv queue */
1086 if ((ipn_node->shutdown & RCV_SHUTDOWN))
1087 ipn_flush_recvqueue(ipn_node);
1088 up(&ipnn->ipnn_mutex);
1090 return 0;
1093 /* injectmsg: a new message is entering the ipn network.
1094 * injectmsg gets called by send and by the grab/tap node */
1095 int ipn_proto_injectmsg(struct ipn_node *from, struct msgpool_item *msg)
1097 struct ipn_network *ipnn=from->ipn;
1098 int err=0;
1099 if (down_interruptible(&ipnn->ipnn_mutex))
1100 err=-ERESTARTSYS;
1101 else {
1102 ipn_protocol_table[ipnn->protocol]->ipn_p_handlemsg(from, msg);
1103 up(&ipnn->ipnn_mutex);
1105 return err;
1108 /* SEND MSG */
1109 static int ipn_sendmsg(struct kiocb *kiocb, struct socket *sock,
1110 struct msghdr *msg, size_t len) {
1111 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
1112 struct ipn_network *ipnn=ipn_node->ipn;
1113 struct msgpool_item *newmsg;
1114 int err=0;
1116 if (unlikely(sock->state != SS_CONNECTED))
1117 return -ENOTCONN;
1118 if (unlikely(ipn_node->shutdown & SEND_SHUTDOWN)) {
1119 if (ipn_node->shutdown == SHUTDOWN_XMASK)
1120 return -ECONNRESET;
1121 else
1122 return -EPIPE;
1124 if (len > ipnn->mtu)
1125 return -EOVERFLOW;
1126 newmsg=ipn_msgpool_alloc_locking(ipnn);
1127 if (!newmsg)
1128 return -ENOMEM;
1129 newmsg->len=len;
1130 err=memcpy_fromiovec(newmsg->data, msg->msg_iov, len);
1131 if (!err)
1132 ipn_proto_injectmsg(ipn_node, newmsg);
1133 ipn_msgpool_put(newmsg,ipnn);
1134 return err;
1137 /* enqueue an oob message. "to" is the destination */
1138 void ipn_proto_oobsendmsg(struct ipn_node *to, struct msgpool_item *msg)
1140 if (to) {
1141 if (!to->dev) { /* no oob to netdev */
1142 struct msgitem *msgitem;
1143 struct ipn_network *ipnn=to->ipn;
1144 spin_lock(&to->msglock);
1145 if ((to->shutdown & RCV_SHUTDOWN_NO_OOB) == 0 &&
1146 (ipnn->flags & IPN_FLAG_LOSSLESS ||
1147 to->oobmsgcount < ipnn->msgpool_size)) {
1148 if ((msgitem=kmem_cache_alloc(ipn_msgitem_cache,GFP_KERNEL))!=NULL) {
1149 msgitem->msg=msg;
1150 to->totmsgcount++;
1151 to->oobmsgcount++;
1152 list_add_tail(&msgitem->list, &to->oobmsgqueue);
1153 ipn_msgpool_hold(msg);
1156 spin_unlock(&to->msglock);
1157 wake_up_interruptible(&to->read_wait);
1162 /* ipn_proto_sendmsg is called by protocol implementation to enqueue a
1163 * for a destination (to).*/
1164 void ipn_proto_sendmsg(struct ipn_node *to, struct msgpool_item *msg)
1166 if (to) {
1167 if (to->dev) {
1168 ipn_netdev_sendmsg(to,msg);
1169 } else {
1170 /* socket send */
1171 struct msgitem *msgitem;
1172 struct ipn_network *ipnn=to->ipn;
1173 spin_lock(&to->msglock);
1174 if (likely((to->shutdown & RCV_SHUTDOWN)==0)) {
1175 /*if (unlikely((ipnn->flags & IPN_FLAG_LOSSLESS) == 0 ||
1176 to->totmsgcount >= ipnn->msgpool_size))
1177 yield();*/
1178 if (likely(ipnn->flags & IPN_FLAG_LOSSLESS ||
1179 to->totmsgcount < ipnn->msgpool_size)) {
1180 if ((msgitem=kmem_cache_alloc(ipn_msgitem_cache,GFP_KERNEL))!=NULL) {
1181 msgitem->msg=msg;
1182 to->totmsgcount++;
1183 list_add_tail(&msgitem->list, &to->msgqueue);
1184 ipn_msgpool_hold(msg);
1188 spin_unlock(&to->msglock);
1189 wake_up_interruptible(&to->read_wait);
1194 /* IPN RECV */
1195 static int ipn_recvmsg(struct kiocb *kiocb, struct socket *sock,
1196 struct msghdr *msg, size_t len, int flags) {
1197 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
1198 struct ipn_network *ipnn=ipn_node->ipn;
1199 struct msgitem *msgitem;
1200 struct msgpool_item *currmsg;
1202 if (unlikely(sock->state != SS_CONNECTED))
1203 return -ENOTCONN;
1205 if (unlikely((ipn_node->shutdown & XRCV_SHUTDOWN) == XRCV_SHUTDOWN)) {
1206 if (ipn_node->shutdown == SHUTDOWN_XMASK) /*EOF, nothing can be read*/
1207 return 0;
1208 else
1209 return -EPIPE; /*trying to read on a write only node */
1212 /* wait for a message */
1213 spin_lock(&ipn_node->msglock);
1214 while (ipn_node->totmsgcount == 0) {
1215 spin_unlock(&ipn_node->msglock);
1216 if (wait_event_interruptible(ipn_node->read_wait,
1217 !(ipn_node->totmsgcount == 0)))
1218 return -ERESTARTSYS;
1219 spin_lock(&ipn_node->msglock);
1221 /* oob gets delivered first. oob are rare */
1222 if (likely(list_empty(&ipn_node->oobmsgqueue)))
1223 msgitem=list_first_entry(&ipn_node->msgqueue, struct msgitem, list);
1224 else {
1225 msgitem=list_first_entry(&ipn_node->oobmsgqueue, struct msgitem, list);
1226 msg->msg_flags |= MSG_OOB;
1227 ipn_node->oobmsgcount--;
1229 list_del(&msgitem->list);
1230 ipn_node->totmsgcount--;
1231 spin_unlock(&ipn_node->msglock);
1232 currmsg=msgitem->msg;
1233 if (currmsg->len < len)
1234 len=currmsg->len;
1235 memcpy_toiovec(msg->msg_iov, currmsg->data, len);
1236 ipn_msgpool_put(currmsg,ipnn);
1237 kmem_cache_free(ipn_msgitem_cache,msgitem);
1239 return len;
1242 /* resize a network: change the # of communication ports (connport) */
1243 static int ipn_netresize(struct ipn_network *ipnn,int newsize)
1245 int oldsize,min;
1246 struct ipn_node **newconnport;
1247 struct ipn_node **oldconnport;
1248 int err;
1249 if (down_interruptible(&ipnn->ipnn_mutex))
1250 return -ERESTARTSYS;
1251 oldsize=ipnn->maxports;
1252 if (newsize == oldsize) {
1253 up(&ipnn->ipnn_mutex);
1254 return 0;
1256 min=oldsize;
1257 /* shrink a network. all the ports we are going to eliminate
1258 * must be unused! */
1259 if (newsize < oldsize) {
1260 int i;
1261 for (i=newsize; i<oldsize; i++)
1262 if (ipnn->connport[i]) {
1263 up(&ipnn->ipnn_mutex);
1264 return -EADDRINUSE;
1266 min=newsize;
1268 oldconnport=ipnn->connport;
1269 /* allocate the new connport array and copy the old one */
1270 newconnport=kzalloc(newsize * sizeof(struct ipn_node *),GFP_KERNEL);
1271 if (!newconnport) {
1272 up(&ipnn->ipnn_mutex);
1273 return -ENOMEM;
1275 memcpy(newconnport,oldconnport,min * sizeof(struct ipn_node *));
1276 ipnn->connport=newconnport;
1277 ipnn->maxports=newsize;
1278 /* notify the protocol that the netowrk has been resized */
1279 err=ipn_protocol_table[ipnn->protocol]->ipn_p_resizenet(ipnn,oldsize,newsize);
1280 if (err) {
1281 /* roll back if the resize operation failed for the protocol */
1282 ipnn->connport=oldconnport;
1283 ipnn->maxports=oldsize;
1284 kfree(newconnport);
1285 } else
1286 /* successful mission, network resized */
1287 kfree(oldconnport);
1288 up(&ipnn->ipnn_mutex);
1289 return err;
1292 /* IPN SETSOCKOPT */
1293 static int ipn_setsockopt(struct socket *sock, int level, int optname,
1294 char __user *optval, int optlen) {
1295 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
1296 struct ipn_network *ipnn=ipn_node->ipn;
1298 if (ipn_node->shutdown == SHUTDOWN_XMASK)
1299 return -ECONNRESET;
1300 if (level != 0 && level != ipn_node->protocol+1)
1301 return -EPROTONOSUPPORT;
1302 if (level > 0) {
1303 /* protocol specific sockopt */
1304 if (ipnn) {
1305 int rv;
1306 if (down_interruptible(&ipnn->ipnn_mutex))
1307 return -ERESTARTSYS;
1308 rv=ipn_protocol_table[ipn_node->protocol]->ipn_p_setsockopt(ipn_node,optname,optval,optlen);
1309 up(&ipnn->ipnn_mutex);
1310 return rv;
1311 } else
1312 return -EOPNOTSUPP;
1313 } else {
1314 if (optname == IPN_SO_DESCR) {
1315 if (optlen > IPN_DESCRLEN)
1316 return -EINVAL;
1317 else {
1318 memset(ipn_node->descr,0,IPN_DESCRLEN);
1319 if (copy_from_user(ipn_node->descr,optval,optlen))
1320 ipn_node->descr[0]=0;
1321 else
1322 ipn_node->descr[optlen-1]=0;
1323 return 0;
1325 } else {
1326 if (optlen < sizeof(int))
1327 return -EINVAL;
1328 else if ((optname & IPN_SO_PREBIND) && (ipnn != NULL))
1329 return -EISCONN;
1330 else {
1331 int val;
1332 get_user(val, (int __user *) optval);
1333 if ((optname & IPN_SO_PREBIND) && !ipn_node->pbp) {
1334 struct pre_bind_parms std=STD_BIND_PARMS;
1335 ipn_node->pbp=kzalloc(sizeof(struct pre_bind_parms),GFP_KERNEL);
1336 if (!ipn_node->pbp)
1337 return -ENOMEM;
1338 *(ipn_node->pbp)=std;
1340 switch (optname) {
1341 case IPN_SO_PORT:
1342 if (sock->state == SS_UNCONNECTED)
1343 ipn_node->portno=val;
1344 else
1345 return -EISCONN;
1346 break;
1347 case IPN_SO_CHANGE_NUMNODES:
1348 if ((ipn_node->flags & IPN_NODEFLAG_BOUND)!=0) {
1349 if (val <= 0)
1350 return -EINVAL;
1351 else
1352 return ipn_netresize(ipnn,val);
1353 } else
1354 val=-ENOTCONN;
1355 break;
1356 case IPN_SO_WANT_OOB_NUMNODES:
1357 if (val)
1358 ipn_node->flags |= IPN_NODEFLAG_OOB_NUMNODES;
1359 else
1360 ipn_node->flags &= ~IPN_NODEFLAG_OOB_NUMNODES;
1361 break;
1362 case IPN_SO_HANDLE_OOB:
1363 if (val)
1364 ipn_node->shutdown &= ~RCV_SHUTDOWN_NO_OOB;
1365 else
1366 ipn_node->shutdown |= RCV_SHUTDOWN_NO_OOB;
1367 break;
1368 case IPN_SO_MTU:
1369 if (val <= 0)
1370 return -EINVAL;
1371 else
1372 ipn_node->pbp->mtu=val;
1373 break;
1374 case IPN_SO_NUMNODES:
1375 if (val <= 0)
1376 return -EINVAL;
1377 else
1378 ipn_node->pbp->maxports=val;
1379 break;
1380 case IPN_SO_MSGPOOLSIZE:
1381 if (val <= 0)
1382 return -EINVAL;
1383 else
1384 ipn_node->pbp->msgpoolsize=val;
1385 break;
1386 case IPN_SO_FLAGS:
1387 ipn_node->pbp->flags=val;
1388 break;
1389 case IPN_SO_MODE:
1390 ipn_node->pbp->mode=val;
1391 break;
1393 return 0;
1399 /* IPN GETSOCKOPT */
1400 static int ipn_getsockopt(struct socket *sock, int level, int optname,
1401 char __user *optval, int __user *optlen) {
1402 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
1403 struct ipn_network *ipnn=ipn_node->ipn;
1404 int len;
1406 if (ipn_node->shutdown == SHUTDOWN_XMASK)
1407 return -ECONNRESET;
1408 if (level != 0 && level != ipn_node->protocol+1)
1409 return -EPROTONOSUPPORT;
1410 if (level > 0) {
1411 if (ipnn) {
1412 int rv;
1413 /* protocol specific sockopt */
1414 if (down_interruptible(&ipnn->ipnn_mutex))
1415 return -ERESTARTSYS;
1416 rv=ipn_protocol_table[ipn_node->protocol]->ipn_p_getsockopt(ipn_node,optname,optval,optlen);
1417 up(&ipnn->ipnn_mutex);
1418 return rv;
1419 } else
1420 return -EOPNOTSUPP;
1421 } else {
1422 if (get_user(len, optlen))
1423 return -EFAULT;
1424 if (optname == IPN_SO_DESCR) {
1425 if (len < IPN_DESCRLEN)
1426 return -EINVAL;
1427 else {
1428 if (len > IPN_DESCRLEN)
1429 len=IPN_DESCRLEN;
1430 if(put_user(len, optlen))
1431 return -EFAULT;
1432 if(copy_to_user(optval,ipn_node->descr,len))
1433 return -EFAULT;
1434 return 0;
1436 } else {
1437 int val=-2;
1438 switch (optname) {
1439 case IPN_SO_PORT:
1440 val=ipn_node->portno;
1441 break;
1442 case IPN_SO_MTU:
1443 if (ipnn)
1444 val=ipnn->mtu;
1445 else if (ipn_node->pbp)
1446 val=ipn_node->pbp->mtu;
1447 break;
1448 case IPN_SO_NUMNODES:
1449 if (ipnn)
1450 val=ipnn->maxports;
1451 else if (ipn_node->pbp)
1452 val=ipn_node->pbp->maxports;
1453 break;
1454 case IPN_SO_MSGPOOLSIZE:
1455 if (ipnn)
1456 val=ipnn->msgpool_size;
1457 else if (ipn_node->pbp)
1458 val=ipn_node->pbp->msgpoolsize;
1459 break;
1460 case IPN_SO_FLAGS:
1461 if (ipnn)
1462 val=ipnn->flags;
1463 else if (ipn_node->pbp)
1464 val=ipn_node->pbp->flags;
1465 break;
1466 case IPN_SO_MODE:
1467 if (ipnn)
1468 val=-1;
1469 else if (ipn_node->pbp)
1470 val=ipn_node->pbp->mode;
1471 break;
1473 if (val < -1)
1474 return -EINVAL;
1475 else {
1476 if (len < sizeof(int))
1477 return -EOVERFLOW;
1478 else {
1479 len = sizeof(int);
1480 if(put_user(len, optlen))
1481 return -EFAULT;
1482 if(copy_to_user(optval,&val,len))
1483 return -EFAULT;
1484 return 0;
1491 /* BROADCAST/HUB implementation */
1493 static int ipn_bcast_newport(struct ipn_node *newport) {
1494 struct ipn_network *ipnn=newport->ipn;
1495 int i;
1496 for (i=0;i<ipnn->maxports;i++) {
1497 if (ipnn->connport[i] == NULL)
1498 return i;
1500 return -1;
1503 static int ipn_bcast_handlemsg(struct ipn_node *from,
1504 struct msgpool_item *msgitem){
1505 struct ipn_network *ipnn=from->ipn;
1507 struct ipn_node *ipn_node;
1508 list_for_each_entry(ipn_node, &ipnn->connectqueue, nodelist) {
1509 if (ipn_node != from)
1510 ipn_proto_sendmsg(ipn_node,msgitem);
1512 return 0;
1515 static void ipn_null_delport(struct ipn_node *oldport) {}
1516 static void ipn_null_postnewport(struct ipn_node *newport) {}
1517 static void ipn_null_predelport(struct ipn_node *oldport) {}
1518 static int ipn_null_newnet(struct ipn_network *newnet) {return 0;}
1519 static int ipn_null_resizenet(struct ipn_network *net,int oldsize,int newsize) {
1520 return 0;}
1521 static void ipn_null_delnet(struct ipn_network *oldnet) {}
1522 static int ipn_null_setsockopt(struct ipn_node *port,int optname,
1523 char __user *optval, int optlen) {return -EOPNOTSUPP;}
1524 static int ipn_null_getsockopt(struct ipn_node *port,int optname,
1525 char __user *optval, int *optlen) {return -EOPNOTSUPP;}
1526 static int ipn_null_ioctl(struct ipn_node *port,unsigned int request,
1527 unsigned long arg) {return -EOPNOTSUPP;}
1529 /* Protocol Registration/deregisteration */
1531 void ipn_init_protocol(struct ipn_protocol *p)
1533 if (p->ipn_p_delport == NULL) p->ipn_p_delport=ipn_null_delport;
1534 if (p->ipn_p_postnewport == NULL) p->ipn_p_postnewport=ipn_null_postnewport;
1535 if (p->ipn_p_predelport == NULL) p->ipn_p_predelport=ipn_null_predelport;
1536 if (p->ipn_p_newnet == NULL) p->ipn_p_newnet=ipn_null_newnet;
1537 if (p->ipn_p_resizenet == NULL) p->ipn_p_resizenet=ipn_null_resizenet;
1538 if (p->ipn_p_delnet == NULL) p->ipn_p_delnet=ipn_null_delnet;
1539 if (p->ipn_p_setsockopt == NULL) p->ipn_p_setsockopt=ipn_null_setsockopt;
1540 if (p->ipn_p_getsockopt == NULL) p->ipn_p_getsockopt=ipn_null_getsockopt;
1541 if (p->ipn_p_ioctl == NULL) p->ipn_p_ioctl=ipn_null_ioctl;
1544 int ipn_proto_register(int protocol,struct ipn_protocol *ipn_service)
1546 int rv=0;
1547 if (ipn_service->ipn_p_newport == NULL ||
1548 ipn_service->ipn_p_handlemsg == NULL)
1549 return -EINVAL;
1550 ipn_init_protocol(ipn_service);
1551 if (down_interruptible(&ipn_glob_mutex))
1552 return -ERESTARTSYS;
1553 if (protocol > 1 && protocol <= IPN_MAX_PROTO) {
1554 protocol--;
1555 if (ipn_protocol_table[protocol])
1556 rv= -EEXIST;
1557 else {
1558 ipn_service->refcnt=0;
1559 ipn_protocol_table[protocol]=ipn_service;
1560 printk(KERN_INFO "IPN: Registered protocol %d\n",protocol+1);
1562 } else
1563 rv= -EINVAL;
1564 up(&ipn_glob_mutex);
1565 return rv;
1568 int ipn_proto_deregister(int protocol)
1570 int rv=0;
1571 if (down_interruptible(&ipn_glob_mutex))
1572 return -ERESTARTSYS;
1573 if (protocol > 1 && protocol <= IPN_MAX_PROTO) {
1574 protocol--;
1575 if (ipn_protocol_table[protocol]) {
1576 if (ipn_protocol_table[protocol]->refcnt == 0) {
1577 ipn_protocol_table[protocol]=NULL;
1578 printk(KERN_INFO "IPN: Unregistered protocol %d\n",protocol+1);
1579 } else
1580 rv=-EADDRINUSE;
1581 } else
1582 rv= -ENOENT;
1583 } else
1584 rv= -EINVAL;
1585 up(&ipn_glob_mutex);
1586 return rv;
1589 /* MAIN SECTION */
1590 /* Module constructor/destructor */
1591 static struct net_proto_family ipn_family_ops = {
1592 .family = PF_IPN,
1593 .create = ipn_create,
1594 .owner = THIS_MODULE,
1597 /* IPN constructor */
1598 static int ipn_init(void)
1600 int rc;
1602 ipn_init_protocol(&ipn_bcast);
1603 ipn_network_cache=kmem_cache_create("ipn_network",sizeof(struct ipn_network),0,0,NULL);
1604 if (!ipn_network_cache) {
1605 printk(KERN_CRIT "%s: Cannot create ipn_network SLAB cache!\n",
1606 __FUNCTION__);
1607 rc=-ENOMEM;
1608 goto out;
1611 ipn_node_cache=kmem_cache_create("ipn_node",sizeof(struct ipn_node),0,0,NULL);
1612 if (!ipn_node_cache) {
1613 printk(KERN_CRIT "%s: Cannot create ipn_node SLAB cache!\n",
1614 __FUNCTION__);
1615 rc=-ENOMEM;
1616 goto out_net;
1619 ipn_msgitem_cache=kmem_cache_create("ipn_msgitem",sizeof(struct msgitem),0,0,NULL);
1620 if (!ipn_msgitem_cache) {
1621 printk(KERN_CRIT "%s: Cannot create ipn_msgitem SLAB cache!\n",
1622 __FUNCTION__);
1623 rc=-ENOMEM;
1624 goto out_net_node;
1627 rc=ipn_msgbuf_init();
1628 if (rc != 0) {
1629 printk(KERN_CRIT "%s: Cannot create ipn_msgbuf SLAB cache\n",
1630 __FUNCTION__);
1631 goto out_net_node_msg;
1634 rc=proto_register(&ipn_proto,1);
1635 if (rc != 0) {
1636 printk(KERN_CRIT "%s: Cannot register the protocol!\n",
1637 __FUNCTION__);
1638 goto out_net_node_msg_msgbuf;
1641 sock_register(&ipn_family_ops);
1642 ipn_netdev_init();
1643 printk(KERN_INFO "IPN: Virtual Square Project, University of Bologna 2007\n");
1644 return 0;
1646 out_net_node_msg_msgbuf:
1647 ipn_msgbuf_fini();
1648 out_net_node_msg:
1649 kmem_cache_destroy(ipn_msgitem_cache);
1650 out_net_node:
1651 kmem_cache_destroy(ipn_node_cache);
1652 out_net:
1653 kmem_cache_destroy(ipn_network_cache);
1654 out:
1655 return rc;
1658 /* IPN destructor */
1659 static void ipn_exit(void)
1661 ipn_netdev_fini();
1662 if (ipn_msgitem_cache)
1663 kmem_cache_destroy(ipn_msgitem_cache);
1664 if (ipn_node_cache)
1665 kmem_cache_destroy(ipn_node_cache);
1666 if (ipn_network_cache)
1667 kmem_cache_destroy(ipn_network_cache);
1668 ipn_msgbuf_fini();
1669 sock_unregister(PF_IPN);
1670 proto_unregister(&ipn_proto);
1671 printk(KERN_INFO "IPN removed\n");
1674 module_init(ipn_init);
1675 module_exit(ipn_exit);
1677 EXPORT_SYMBOL_GPL(ipn_proto_register);
1678 EXPORT_SYMBOL_GPL(ipn_proto_deregister);
1679 EXPORT_SYMBOL_GPL(ipn_proto_sendmsg);
1680 EXPORT_SYMBOL_GPL(ipn_proto_oobsendmsg);
1681 EXPORT_SYMBOL_GPL(ipn_msgpool_alloc);
1682 EXPORT_SYMBOL_GPL(ipn_msgpool_put);