FIx for ubuntu
[vde.git] / ipn / af_ipn.c
blob5e17fcab86bfb83fd2ed935b1fbd3021047658bb
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/poll.h>
25 #include <linux/un.h>
26 #include <linux/list.h>
27 #include <linux/mount.h>
28 #include <linux/version.h>
29 #include <net/sock.h>
31 #include <net/af_ipn.h>
33 #include "af_ipn.h"
34 #include "ipn_netdev.h"
36 MODULE_LICENSE("GPL");
37 MODULE_AUTHOR("VIEW-OS TEAM");
38 MODULE_DESCRIPTION("IPN Kernel Module");
40 #define IPN_MAX_PROTO 4
42 /*extension of RCV_SHUTDOWN defined in include/net/sock.h
43 * when the bit is set recv fails */
44 /* NO_OOB: do not send OOB */
45 #define RCV_SHUTDOWN_NO_OOB 4
46 /* EXTENDED MASK including OOB */
47 #define SHUTDOWN_XMASK (SHUTDOWN_MASK | RCV_SHUTDOWN_NO_OOB)
48 /* if XRCV_SHUTDOWN is all set recv fails */
49 #define XRCV_SHUTDOWN (RCV_SHUTDOWN | RCV_SHUTDOWN_NO_OOB)
51 /* Network table and hash */
52 struct hlist_head ipn_network_table[IPN_HASH_SIZE + 1];
53 DEFINE_SPINLOCK(ipn_table_lock);
54 static struct kmem_cache *ipn_network_cache;
55 static struct kmem_cache *ipn_node_cache;
56 static struct kmem_cache *ipn_msgitem_cache;
57 static DECLARE_MUTEX(ipn_glob_mutex);
59 /* Protocol 1: HUB/Broadcast default protocol. Function Prototypes */
60 static int ipn_bcast_newport(struct ipn_node *newport);
61 static int ipn_bcast_handlemsg(struct ipn_node *from,
62 struct msgpool_item *msgitem);
64 /* default protocol IPN_BROADCAST (0) */
65 static struct ipn_protocol ipn_bcast = {
66 .refcnt=0,
67 .ipn_p_newport=ipn_bcast_newport,
68 .ipn_p_handlemsg=ipn_bcast_handlemsg};
69 /* Protocol table */
70 static struct ipn_protocol *ipn_protocol_table[IPN_MAX_PROTO]={&ipn_bcast};
72 /* Socket call function prototypes */
73 static int ipn_release(struct socket *);
74 static int ipn_bind(struct socket *, struct sockaddr *, int);
75 static int ipn_connect(struct socket *, struct sockaddr *,
76 int addr_len, int flags);
77 static int ipn_getname(struct socket *, struct sockaddr *, int *, int);
78 static unsigned int ipn_poll(struct file *, struct socket *, poll_table *);
79 static int ipn_ioctl(struct socket *, unsigned int, unsigned long);
80 static int ipn_shutdown(struct socket *, int);
81 static int ipn_sendmsg(struct kiocb *, struct socket *,
82 struct msghdr *, size_t);
83 static int ipn_recvmsg(struct kiocb *, struct socket *,
84 struct msghdr *, size_t, int);
85 static int ipn_setsockopt(struct socket *sock, int level, int optname,
86 char __user *optval, int optlen);
87 static int ipn_getsockopt(struct socket *sock, int level, int optname,
88 char __user *optval, int __user *optlen);
90 /* Network table Management
91 * inode->ipn_network hash table */
92 static inline void ipn_insert_network(struct hlist_head *list, struct ipn_network *ipnn)
94 spin_lock(&ipn_table_lock);
95 hlist_add_head(&ipnn->hnode, list);
96 spin_unlock(&ipn_table_lock);
99 static inline void ipn_remove_network(struct ipn_network *ipnn)
101 spin_lock(&ipn_table_lock);
102 hlist_del(&ipnn->hnode);
103 spin_unlock(&ipn_table_lock);
106 static struct ipn_network *ipn_find_network_byinode(struct inode *i)
108 struct ipn_network *ipnn;
109 struct hlist_node *node;
111 spin_lock(&ipn_table_lock);
112 hlist_for_each_entry(ipnn, node,
113 &ipn_network_table[i->i_ino & (IPN_HASH_SIZE - 1)], hnode) {
114 struct dentry *dentry = ipnn->dentry;
116 if(atomic_read(&ipnn->refcnt) > 0 && dentry && dentry->d_inode == i)
117 goto found;
119 ipnn = NULL;
120 found:
121 spin_unlock(&ipn_table_lock);
122 return ipnn;
125 /* msgpool management
126 * msgpool_item are ipn_network dependent (each net has its own MTU)
127 * for each message sent there is one msgpool_item and many struct msgitem
128 * one for each receipient.
129 * msgitem are connected to the node's msgqueue or oobmsgqueue.
130 * when a message is delivered to a process the msgitem is deleted and
131 * the count of the msgpool_item is decreased.
132 * msgpool_item elements gets deleted automatically when count is 0*/
134 struct msgitem {
135 struct list_head list;
136 struct msgpool_item *msg;
139 /* alloc a fresh msgpool item. count is set to 1.
140 * the typical use is
141 * ipn_msgpool_alloc
142 * for each receipient
143 * enqueue messages to the process (using msgitem), ipn_msgpool_hold
144 * ipn_msgpool_put
145 * The message can be delivered concurrently. init count to 1 guarantees
146 * that it survives at least until is has been enqueued to all
147 * receivers */
148 static struct msgpool_item *_ipn_msgpool_alloc(struct ipn_network *ipnn)
150 struct msgpool_item *new;
151 if ((new=kmem_cache_alloc(ipnn->msgpool_cache,GFP_KERNEL)) != NULL) {
152 atomic_set(&new->count,1);
153 atomic_inc(&ipnn->msgpool_nelem);
155 return new;
158 struct msgpool_item *ipn_msgpool_alloc(struct ipn_network *ipnn,int leaky)
160 if (leaky && (ipnn->flags & IPN_FLAG_LOSSLESS) &&
161 atomic_read(&ipnn->msgpool_nelem) < ipnn->msgpool_size)
162 return NULL;
163 else
164 return _ipn_msgpool_alloc(ipnn);
167 /* If the service il LOSSLESS, this msgpool call waits for an
168 * available msgpool item */
169 static struct msgpool_item *ipn_msgpool_alloc_locking(struct ipn_network *ipnn)
171 if (ipnn->flags & IPN_FLAG_LOSSLESS) {
172 while (atomic_read(&ipnn->msgpool_nelem) >= ipnn->msgpool_size) {
173 if (wait_event_interruptible_exclusive(ipnn->send_wait,
174 atomic_read(&ipnn->msgpool_nelem) < ipnn->msgpool_size))
175 return NULL;
178 return _ipn_msgpool_alloc(ipnn);
181 static inline void ipn_msgpool_hold(struct msgpool_item *msg)
183 atomic_inc(&msg->count);
186 /* decrease count and delete msgpool_item if count == 0 */
187 void ipn_msgpool_put(struct msgpool_item *old,
188 struct ipn_network *ipnn)
190 if (atomic_dec_and_test(&old->count)) {
191 kmem_cache_free(ipnn->msgpool_cache,old);
192 atomic_dec(&ipnn->msgpool_nelem);
193 if (ipnn->flags & IPN_FLAG_LOSSLESS) /* could be done anyway */
194 wake_up_interruptible(&ipnn->send_wait);
198 /* socket calls */
199 static const struct proto_ops ipn_ops = {
200 .family = PF_IPN,
201 .owner = THIS_MODULE,
202 .release = ipn_release,
203 .bind = ipn_bind,
204 .connect = ipn_connect,
205 .socketpair = sock_no_socketpair,
206 .accept = sock_no_accept,
207 .getname = ipn_getname,
208 .poll = ipn_poll,
209 .ioctl = ipn_ioctl,
210 .listen = sock_no_listen,
211 .shutdown = ipn_shutdown,
212 .setsockopt = ipn_setsockopt,
213 .getsockopt = ipn_getsockopt,
214 .sendmsg = ipn_sendmsg,
215 .recvmsg = ipn_recvmsg,
216 .mmap = sock_no_mmap,
217 .sendpage = sock_no_sendpage,
220 static struct proto ipn_proto = {
221 .name = "IPN",
222 .owner = THIS_MODULE,
223 .obj_size = sizeof(struct ipn_sock),
226 /* create a socket
227 * ipn_node is a separate structure, pointed by ipn_sock -> node
228 * when a node is "persistent", ipn_node survives while ipn_sock gets released*/
229 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
230 static int ipn_create(struct socket *sock, int protocol)
231 #else
232 static int ipn_create(struct net *net,struct socket *sock, int protocol)
233 #endif
235 struct ipn_sock *ipn_sk;
236 struct ipn_node *ipn_node;
238 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
239 void *net=NULL;
240 #else
241 if (net != &init_net)
242 return -EAFNOSUPPORT;
243 #endif
245 if (sock->type != SOCK_RAW)
246 return -EPROTOTYPE;
247 if (protocol > 0)
248 protocol=protocol-1;
249 else
250 protocol=IPN_BROADCAST-1;
251 if (protocol < 0 || protocol >= IPN_MAX_PROTO ||
252 ipn_protocol_table[protocol] == NULL)
253 return -EPROTONOSUPPORT;
254 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
255 ipn_sk = (struct ipn_sock *) sk_alloc(PF_IPN, GFP_KERNEL, &ipn_proto, 1);
256 #else
257 ipn_sk = (struct ipn_sock *) sk_alloc(net, PF_IPN, GFP_KERNEL, &ipn_proto);
258 #endif
260 if (!ipn_sk)
261 return -ENOMEM;
262 ipn_sk->node=ipn_node=kmem_cache_alloc(ipn_node_cache,GFP_KERNEL);
263 if (!ipn_node) {
264 sock_put((struct sock *) ipn_sk);
265 return -ENOMEM;
267 sock_init_data(sock,(struct sock *) ipn_sk);
268 sock->state = SS_UNCONNECTED;
269 sock->ops = &ipn_ops;
270 sock->sk=(struct sock *)ipn_sk;
271 INIT_LIST_HEAD(&ipn_node->nodelist);
272 ipn_node->protocol=protocol;
273 ipn_node->flags=IPN_NODEFLAG_INUSE;
274 ipn_node->shutdown=RCV_SHUTDOWN_NO_OOB;
275 ipn_node->descr[0]=0;
276 ipn_node->portno=IPN_PORTNO_ANY;
277 ipn_node->net=net;
278 ipn_node->dev=NULL;
279 ipn_node->proto_private=NULL;
280 ipn_node->totmsgcount=0;
281 ipn_node->oobmsgcount=0;
282 spin_lock_init(&ipn_node->msglock);
283 INIT_LIST_HEAD(&ipn_node->msgqueue);
284 INIT_LIST_HEAD(&ipn_node->oobmsgqueue);
285 ipn_node->ipn=NULL;
286 init_waitqueue_head(&ipn_node->read_wait);
287 ipn_node->pbp=NULL;
288 return 0;
291 /* update # of readers and # of writers counters for an ipn network.
292 * This function sends oob messages to nodes requesting the service */
293 static void ipn_net_update_counters(struct ipn_network *ipnn,
294 int chg_readers, int chg_writers) {
295 ipnn->numreaders += chg_readers;
296 ipnn->numwriters += chg_writers;
297 if (ipnn->mtu >= sizeof(struct numnode_oob))
299 struct msgpool_item *ipn_msg=_ipn_msgpool_alloc(ipnn);
300 if (ipn_msg) {
301 struct numnode_oob *oob_msg=(struct numnode_oob *)(ipn_msg->data);
302 struct ipn_node *ipn_node;
303 ipn_msg->len=sizeof(struct numnode_oob);
304 oob_msg->level=IPN_ANY;
305 oob_msg->tag=IPN_OOB_NUMNODE_TAG;
306 oob_msg->numreaders=ipnn->numreaders;
307 oob_msg->numwriters=ipnn->numwriters;
308 list_for_each_entry(ipn_node, &ipnn->connectqueue, nodelist) {
309 if (ipn_node->flags & IPN_NODEFLAG_OOB_NUMNODES)
310 ipn_proto_oobsendmsg(ipn_node,ipn_msg);
312 ipn_msgpool_put(ipn_msg,ipnn);
317 /* flush pending messages (for close and shutdown RCV) */
318 static void ipn_flush_recvqueue(struct ipn_node *ipn_node)
320 struct ipn_network *ipnn=ipn_node->ipn;
321 spin_lock(&ipn_node->msglock);
322 while (!list_empty(&ipn_node->msgqueue)) {
323 struct msgitem *msgitem=
324 list_first_entry(&ipn_node->msgqueue, struct msgitem, list);
325 list_del(&msgitem->list);
326 ipn_node->totmsgcount--;
327 ipn_msgpool_put(msgitem->msg,ipnn);
328 kmem_cache_free(ipn_msgitem_cache,msgitem);
330 spin_unlock(&ipn_node->msglock);
333 /* flush pending oob messages (for socket close) */
334 static void ipn_flush_oobrecvqueue(struct ipn_node *ipn_node)
336 struct ipn_network *ipnn=ipn_node->ipn;
337 spin_lock(&ipn_node->msglock);
338 while (!list_empty(&ipn_node->oobmsgqueue)) {
339 struct msgitem *msgitem=
340 list_first_entry(&ipn_node->oobmsgqueue, struct msgitem, list);
341 list_del(&msgitem->list);
342 ipn_node->totmsgcount--;
343 ipn_node->oobmsgcount--;
344 ipn_msgpool_put(msgitem->msg,ipnn);
345 kmem_cache_free(ipn_msgitem_cache,msgitem);
347 spin_unlock(&ipn_node->msglock);
350 /* Terminate node. The node is "logically" terminated. */
351 static int ipn_terminate_node(struct ipn_node *ipn_node)
353 struct ipn_network *ipnn=ipn_node->ipn;
354 if (ipnn) {
355 if (down_interruptible(&ipnn->ipnn_mutex))
356 return -ERESTARTSYS;
357 if (ipn_node->portno >= 0) {
358 ipn_protocol_table[ipnn->protocol]->ipn_p_predelport(ipn_node);
359 ipnn->connport[ipn_node->portno]=NULL;
361 list_del(&ipn_node->nodelist);
362 ipn_flush_recvqueue(ipn_node);
363 ipn_flush_oobrecvqueue(ipn_node);
364 if (ipn_node->portno >= 0)
365 ipn_protocol_table[ipnn->protocol]->ipn_p_delport(ipn_node);
366 ipn_node->ipn=NULL;
367 ipn_net_update_counters(ipnn,
368 (ipn_node->shutdown & RCV_SHUTDOWN)?0:-1,
369 (ipn_node->shutdown & SEND_SHUTDOWN)?0:-1);
370 up(&ipnn->ipnn_mutex);
371 if (ipn_node->dev)
372 ipn_netdev_close(ipn_node);
373 /* No more network elements */
374 if (atomic_dec_and_test(&ipnn->refcnt))
376 ipn_protocol_table[ipnn->protocol]->ipn_p_delnet(ipnn);
377 ipn_remove_network(ipnn);
378 ipn_protocol_table[ipnn->protocol]->refcnt--;
379 if (ipnn->dentry) {
380 dput(ipnn->dentry);
381 mntput(ipnn->mnt);
383 module_put(THIS_MODULE);
384 if (ipnn->msgpool_cache)
385 kmem_cache_destroy(ipnn->msgpool_cache);
386 if (ipnn->connport)
387 kfree(ipnn->connport);
388 kmem_cache_free(ipn_network_cache, ipnn);
391 if (ipn_node->pbp) {
392 kfree(ipn_node->pbp);
393 ipn_node->pbp=NULL;
395 ipn_node->shutdown = SHUTDOWN_XMASK;
396 return 0;
399 /* release of a socket */
400 static int ipn_release (struct socket *sock)
402 struct ipn_sock *ipn_sk=(struct ipn_sock *)sock->sk;
403 struct ipn_node *ipn_node=ipn_sk->node;
404 int rv;
405 if (down_interruptible(&ipn_glob_mutex))
406 return -ERESTARTSYS;
407 if (ipn_node->flags & IPN_NODEFLAG_PERSIST) {
408 ipn_node->flags &= ~IPN_NODEFLAG_INUSE;
409 rv=0;
410 up(&ipn_glob_mutex);
411 } else {
412 rv=ipn_terminate_node(ipn_node);
413 up(&ipn_glob_mutex);
414 if (rv==0) {
415 ipn_netdevsync();
416 kmem_cache_free(ipn_node_cache,ipn_node);
419 if (rv==0)
420 sock_put((struct sock *) ipn_sk);
421 return rv;
424 /* _set persist, change the persistence of a node,
425 * when persistence gets cleared and the node is no longer used
426 * the node is terminated and freed.
427 * ipn_glob_mutex must be locked */
428 static int _ipn_setpersist(struct ipn_node *ipn_node, int persist)
430 int rv=0;
431 if (persist)
432 ipn_node->flags |= IPN_NODEFLAG_PERSIST;
433 else {
434 ipn_node->flags &= ~IPN_NODEFLAG_PERSIST;
435 if (!(ipn_node->flags & IPN_NODEFLAG_INUSE)) {
436 rv=ipn_terminate_node(ipn_node);
437 if (rv==0)
438 kmem_cache_free(ipn_node_cache,ipn_node);
441 return rv;
444 /* ipn_setpersist
445 * lock ipn_glob_mutex and call __ipn_setpersist above */
446 static int ipn_setpersist(struct ipn_node *ipn_node, int persist)
448 int rv=0;
449 if (ipn_node->dev == NULL)
450 return -ENODEV;
451 if (down_interruptible(&ipn_glob_mutex))
452 return -ERESTARTSYS;
453 rv=_ipn_setpersist(ipn_node,persist);
454 up(&ipn_glob_mutex);
455 return rv;
458 /* several network parameters can be set by setsockopt prior to bind */
459 /* struct pre_bind_parms is a temporary stucture connected to ipn_node->pbp
460 * to keep the parameter values. */
461 struct pre_bind_parms {
462 unsigned short maxports;
463 unsigned short flags;
464 unsigned short msgpoolsize;
465 unsigned short mtu;
466 unsigned short mode;
469 /* STD_PARMS: BITS_PER_LONG nodes, no flags, BITS_PER_BYTE pending msgs,
470 * Ethernet + VLAN MTU*/
471 #define STD_BIND_PARMS {BITS_PER_LONG, 0, BITS_PER_BYTE, 1514, 0x777};
473 static int ipn_mkname(struct sockaddr_un * sunaddr, int len)
475 if (len <= sizeof(short) || len > sizeof(*sunaddr))
476 return -EINVAL;
477 if (!sunaddr || sunaddr->sun_family != AF_IPN)
478 return -EINVAL;
480 * This may look like an off by one error but it is a bit more
481 * subtle. 108 is the longest valid AF_IPN path for a binding.
482 * sun_path[108] doesnt as such exist. However in kernel space
483 * we are guaranteed that it is a valid memory location in our
484 * kernel address buffer.
486 ((char *)sunaddr)[len]=0;
487 len = strlen(sunaddr->sun_path)+1+sizeof(short);
488 return len;
492 /* IPN BIND */
493 static int ipn_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
495 struct sockaddr_un *sunaddr=(struct sockaddr_un *)uaddr;
496 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
497 struct nameidata nd;
498 struct ipn_network *ipnn;
499 struct dentry * dentry = NULL;
500 int err;
501 struct pre_bind_parms parms=STD_BIND_PARMS;
503 //printk("IPN bind\n");
505 if (down_interruptible(&ipn_glob_mutex))
506 return -ERESTARTSYS;
507 if (sock->state != SS_UNCONNECTED ||
508 ipn_node->ipn != NULL) {
509 err= -EISCONN;
510 goto out;
513 if (ipn_node->protocol >= 0 &&
514 (ipn_node->protocol >= IPN_MAX_PROTO ||
515 ipn_protocol_table[ipn_node->protocol] == NULL)) {
516 err= -EPROTONOSUPPORT;
517 goto out;
520 addr_len = ipn_mkname(sunaddr, addr_len);
521 if (addr_len < 0) {
522 err=addr_len;
523 goto out;
526 /* check if there is already a socket with that name */
527 err = path_lookup(sunaddr->sun_path, LOOKUP_FOLLOW, &nd);
528 if (err) { /* it does not exist, NEW IPN socket! */
529 unsigned int mode;
530 /* Is it everything okay with the parent? */
531 err = path_lookup(sunaddr->sun_path, LOOKUP_PARENT, &nd);
532 if (err)
533 goto out_mknod_parent;
534 /* Do I have the permission to create a file? */
535 dentry = lookup_create(&nd, 0);
536 err = PTR_ERR(dentry);
537 if (IS_ERR(dentry))
538 goto out_mknod_unlock;
540 * All right, let's create it.
542 if (ipn_node->pbp)
543 mode = ipn_node->pbp->mode;
544 else
545 mode = SOCK_INODE(sock)->i_mode;
546 mode = S_IFSOCK | (mode & ~current->fs->umask);
547 #ifdef APPARMOR
548 err = vfs_mknod(nd.dentry->d_inode, dentry, nd.mnt, mode, 0);
549 #else
550 err = vfs_mknod(nd.dentry->d_inode, dentry, mode, 0);
551 #endif
552 if (err)
553 goto out_mknod_dput;
554 mutex_unlock(&nd.dentry->d_inode->i_mutex);
555 dput(nd.dentry);
556 nd.dentry = dentry;
557 /* create a new ipn_network item */
558 if (ipn_node->pbp)
559 parms=*ipn_node->pbp;
560 ipnn=kmem_cache_zalloc(ipn_network_cache,GFP_KERNEL);
561 if (!ipnn) {
562 err=-ENOMEM;
563 goto out_mknod_dput_ipnn;
565 ipnn->connport=kzalloc(parms.maxports * sizeof(struct ipn_node *),GFP_KERNEL);
566 if (!ipnn->connport) {
567 err=-ENOMEM;
568 goto out_mknod_dput_ipnn2;
571 /* module refcnt is incremented for each network, thus
572 * rmmod is forbidden if there are persistent node */
573 if (!try_module_get(THIS_MODULE)) {
574 err = -EINVAL;
575 goto out_mknod_dput_ipnn2;
577 memcpy(&ipnn->sunaddr,sunaddr,addr_len);
578 ipnn->mtu=parms.mtu;
579 ipnn->msgpool_cache=kmem_cache_create(ipnn->sunaddr.sun_path,sizeof(struct msgpool_item)+ipnn->mtu,0,0,NULL);
580 if (!ipnn->msgpool_cache) {
581 err=-ENOMEM;
582 goto out_mknod_dput_putmodule;
584 INIT_LIST_HEAD(&ipnn->unconnectqueue);
585 INIT_LIST_HEAD(&ipnn->connectqueue);
586 atomic_set(&ipnn->refcnt,1);
587 ipnn->dentry=nd.dentry;
588 ipnn->mnt=nd.mnt;
589 init_MUTEX(&ipnn->ipnn_mutex);
590 ipnn->sunaddr_len=addr_len;
591 ipnn->protocol=ipn_node->protocol;
592 if (ipnn->protocol < 0) ipnn->protocol = 0;
593 ipn_protocol_table[ipnn->protocol]->refcnt++;
594 ipnn->flags=parms.flags;
595 ipnn->numreaders=0;
596 ipnn->numwriters=0;
597 ipnn->maxports=parms.maxports;
598 atomic_set(&ipnn->msgpool_nelem,0);
599 ipnn->msgpool_size=parms.msgpoolsize;
600 ipnn->proto_private=NULL;
601 init_waitqueue_head(&ipnn->send_wait);
602 err=ipn_protocol_table[ipnn->protocol]->ipn_p_newnet(ipnn);
603 if (err)
604 goto out_mknod_dput_putmodule;
605 ipn_insert_network(&ipn_network_table[nd.dentry->d_inode->i_ino & (IPN_HASH_SIZE-1)],ipnn);
606 } else {
607 /* join an existing network */
608 if (parms.flags & IPN_FLAG_EXCL) {
609 err=-EEXIST;
610 goto put_fail;
612 err = vfs_permission(&nd, MAY_EXEC);
613 if (err)
614 goto put_fail;
615 err = -ECONNREFUSED;
616 if (!S_ISSOCK(nd.dentry->d_inode->i_mode))
617 goto put_fail;
618 ipnn=ipn_find_network_byinode(nd.dentry->d_inode);
619 if (!ipnn || (ipnn->flags & IPN_FLAG_TERMINATED) ||
620 (ipnn->flags & IPN_FLAG_EXCL))
621 goto put_fail;
622 list_add_tail(&ipn_node->nodelist,&ipnn->unconnectqueue);
623 atomic_inc(&ipnn->refcnt);
625 if (ipn_node->pbp) {
626 kfree(ipn_node->pbp);
627 ipn_node->pbp=NULL;
629 ipn_node->ipn=ipnn;
630 ipn_node->flags |= IPN_NODEFLAG_BOUND;
631 up(&ipn_glob_mutex);
632 return 0;
634 put_fail:
635 path_release(&nd);
636 out:
637 up(&ipn_glob_mutex);
638 return err;
640 out_mknod_dput_putmodule:
641 module_put(THIS_MODULE);
642 out_mknod_dput_ipnn2:
643 kfree(ipnn->connport);
644 out_mknod_dput_ipnn:
645 kmem_cache_free(ipn_network_cache,ipnn);
646 out_mknod_dput:
647 dput(dentry);
648 out_mknod_unlock:
649 mutex_unlock(&nd.dentry->d_inode->i_mutex);
650 path_release(&nd);
651 out_mknod_parent:
652 if (err==-EEXIST)
653 err=-EADDRINUSE;
654 up(&ipn_glob_mutex);
655 return err;
658 /* IPN CONNECT */
659 static int ipn_connect(struct socket *sock, struct sockaddr *addr,
660 int addr_len, int flags){
661 struct sockaddr_un *sunaddr=(struct sockaddr_un*)addr;
662 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
663 struct nameidata nd;
664 struct ipn_network *ipnn,*previousipnn;
665 int err=0;
666 int portno;
668 /* the socket cannot be connected twice */
669 if (sock->state != SS_UNCONNECTED)
670 return EISCONN;
672 if (down_interruptible(&ipn_glob_mutex))
673 return -ERESTARTSYS;
675 if ((previousipnn=ipn_node->ipn) == NULL) { /* unbound */
676 unsigned char mustshutdown=0;
677 err = ipn_mkname(sunaddr, addr_len);
678 if (err < 0)
679 goto out;
680 addr_len=err;
681 err = path_lookup(sunaddr->sun_path, LOOKUP_FOLLOW, &nd);
682 if (err)
683 goto out;
684 err = vfs_permission(&nd, MAY_READ);
685 if (err) {
686 if (err == -EACCES || err == -EROFS)
687 mustshutdown|=RCV_SHUTDOWN;
688 else
689 goto put_fail;
691 err = vfs_permission(&nd, MAY_WRITE);
692 if (err) {
693 if (err == -EACCES)
694 mustshutdown|=SEND_SHUTDOWN;
695 else
696 goto put_fail;
698 mustshutdown |= ipn_node->shutdown;
699 /* if the combination of shutdown and permissions leaves
700 * no abilities, connect returns EACCES */
701 if (mustshutdown == SHUTDOWN_XMASK) {
702 err=-EACCES;
703 goto put_fail;
704 } else {
705 err=0;
706 ipn_node->shutdown=mustshutdown;
708 if (!S_ISSOCK(nd.dentry->d_inode->i_mode)) {
709 err = -ECONNREFUSED;
710 goto put_fail;
712 ipnn=ipn_find_network_byinode(nd.dentry->d_inode);
713 if (!ipnn || (ipnn->flags & IPN_FLAG_TERMINATED)) {
714 err = -ECONNREFUSED;
715 goto put_fail;
717 if (ipn_node->protocol == IPN_ANY)
718 ipn_node->protocol=ipnn->protocol;
719 else if (ipnn->protocol != ipn_node->protocol) {
720 err = -EPROTO;
721 goto put_fail;
723 path_release(&nd);
724 ipn_node->ipn=ipnn;
725 } else
726 ipnn=ipn_node->ipn;
728 if (down_interruptible(&ipnn->ipnn_mutex)) {
729 err=-ERESTARTSYS;
730 goto out;
732 portno = ipn_protocol_table[ipnn->protocol]->ipn_p_newport(ipn_node);
733 if (portno >= 0 && portno<ipnn->maxports) {
734 sock->state = SS_CONNECTED;
735 ipn_node->portno=portno;
736 ipnn->connport[portno]=ipn_node;
737 if (!(ipn_node->flags & IPN_NODEFLAG_BOUND)) {
738 atomic_inc(&ipnn->refcnt);
739 list_del(&ipn_node->nodelist);
741 list_add_tail(&ipn_node->nodelist,&ipnn->connectqueue);
742 ipn_net_update_counters(ipnn,
743 (ipn_node->shutdown & RCV_SHUTDOWN)?0:1,
744 (ipn_node->shutdown & SEND_SHUTDOWN)?0:1);
745 } else {
746 ipn_node->ipn=previousipnn; /* undo changes on ipn_node->ipn */
747 err=-EADDRNOTAVAIL;
749 up(&ipnn->ipnn_mutex);
750 up(&ipn_glob_mutex);
751 return err;
753 put_fail:
754 path_release(&nd);
755 out:
756 up(&ipn_glob_mutex);
757 return err;
760 static int ipn_getname(struct socket *sock, struct sockaddr *uaddr,
761 int *uaddr_len, int peer) {
762 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
763 struct ipn_network *ipnn=ipn_node->ipn;
764 struct sockaddr_un *sunaddr=(struct sockaddr_un *)uaddr;
765 int err=0;
767 if (down_interruptible(&ipn_glob_mutex))
768 return -ERESTARTSYS;
769 if (ipnn) {
770 *uaddr_len = ipnn->sunaddr_len;
771 memcpy(sunaddr,&ipnn->sunaddr,*uaddr_len);
772 } else
773 err = -ENOTCONN;
774 up(&ipn_glob_mutex);
775 return err;
778 /* IPN POLL */
779 static unsigned int ipn_poll(struct file *file, struct socket *sock,
780 poll_table *wait) {
781 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
782 struct ipn_network *ipnn=ipn_node->ipn;
783 unsigned int mask=0;
785 if (ipnn) {
786 poll_wait(file,&ipn_node->read_wait,wait);
787 if (ipnn->flags & IPN_FLAG_LOSSLESS)
788 poll_wait(file,&ipnn->send_wait,wait);
789 /* POLLIN if recv succeeds,
790 * POLL{PRI,RDNORM} if there are {oob,non-oob} messages */
791 if (ipn_node->totmsgcount > 0) mask |= POLLIN;
792 if (!(list_empty(&ipn_node->msgqueue))) mask |= POLLRDNORM;
793 if (!(list_empty(&ipn_node->oobmsgqueue))) mask |= POLLPRI;
794 if ((!(ipnn->flags & IPN_FLAG_LOSSLESS)) |
795 (atomic_read(&ipnn->msgpool_nelem) < ipnn->msgpool_size))
796 mask |= POLLOUT | POLLWRNORM;
798 return mask;
801 /* connect netdev (from ioctl). connect a bound socket to a
802 * network device TAP or GRAB */
803 static int ipn_connect_netdev(struct socket *sock,struct ifreq *ifr)
805 int err=0;
806 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
807 struct ipn_network *ipnn=ipn_node->ipn;
808 if (!capable(CAP_NET_ADMIN))
809 return -EPERM;
810 if (sock->state != SS_UNCONNECTED)
811 return -EISCONN;
812 if (!ipnn)
813 return -ENOTCONN; /* Maybe we need a different error for "NOT BOUND" */
814 if (down_interruptible(&ipn_glob_mutex))
815 return -ERESTARTSYS;
816 if (down_interruptible(&ipnn->ipnn_mutex)) {
817 up(&ipn_glob_mutex);
818 return -ERESTARTSYS;
820 ipn_node->dev=ipn_netdev_alloc(ipn_node->net,ifr->ifr_flags,ifr->ifr_name,&err);
821 if (ipn_node->dev) {
822 int portno;
823 portno = ipn_protocol_table[ipnn->protocol]->ipn_p_newport(ipn_node);
824 if (portno >= 0 && portno<ipnn->maxports) {
825 sock->state = SS_CONNECTED;
826 ipn_node->portno=portno;
827 ipn_node->flags |= ifr->ifr_flags & IPN_NODEFLAG_DEVMASK;
828 ipnn->connport[portno]=ipn_node;
829 err=ipn_netdev_activate(ipn_node);
830 if (err) {
831 sock->state = SS_UNCONNECTED;
832 ipn_protocol_table[ipnn->protocol]->ipn_p_delport(ipn_node);
833 ipn_node->dev=NULL;
834 ipn_node->portno= -1;
835 ipn_node->flags &= ~IPN_NODEFLAG_DEVMASK;
836 ipnn->connport[portno]=NULL;
837 } else {
838 ipn_protocol_table[ipnn->protocol]->ipn_p_postnewport(ipn_node);
839 list_del(&ipn_node->nodelist);
840 list_add_tail(&ipn_node->nodelist,&ipnn->connectqueue);
842 } else {
843 ipn_netdev_close(ipn_node);
844 err=-EADDRNOTAVAIL;
845 ipn_node->dev=NULL;
847 } else
848 err=-EINVAL;
849 up(&ipnn->ipnn_mutex);
850 up(&ipn_glob_mutex);
851 return err;
854 /* join a netdev, a socket gets connected to a persistent node
855 * not connected to another socket */
856 static int ipn_join_netdev(struct socket *sock,struct ifreq *ifr)
858 int err=0;
859 struct net_device *dev;
860 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
861 struct ipn_node *ipn_joined;
862 struct ipn_network *ipnn=ipn_node->ipn;
863 if (sock->state != SS_UNCONNECTED)
864 return -EISCONN;
865 if (down_interruptible(&ipn_glob_mutex))
866 return -ERESTARTSYS;
867 if (down_interruptible(&ipnn->ipnn_mutex)) {
868 up(&ipn_glob_mutex);
869 return -ERESTARTSYS;
871 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
872 dev=__dev_get_by_name(ifr->ifr_name);
873 #else
874 dev=__dev_get_by_name(ipn_node->net,ifr->ifr_name);
875 #endif
876 if (!dev)
877 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
878 dev=__dev_get_by_index(ifr->ifr_ifindex);
879 #else
880 dev=__dev_get_by_index(ipn_node->net,ifr->ifr_ifindex);
881 #endif
882 if (dev && (ipn_joined=ipn_netdev2node(dev)) != NULL) { /* the interface does exist */
883 int i;
884 for (i=0;i<ipnn->maxports && ipn_joined != ipnn->connport[i] ;i++)
886 if (i < ipnn->maxports) { /* found */
887 /* ipn_joined is substituted to ipn_node */
888 ((struct ipn_sock *)sock->sk)->node=ipn_joined;
889 ipn_joined->flags |= IPN_NODEFLAG_INUSE;
890 atomic_dec(&ipnn->refcnt);
891 kmem_cache_free(ipn_node_cache,ipn_node);
892 } else
893 err=-EPERM;
894 } else
895 err=-EADDRNOTAVAIL;
896 up(&ipnn->ipnn_mutex);
897 up(&ipn_glob_mutex);
898 return err;
901 /* set persistence of a node looking for it by interface name
902 * (it is for sysadm, to close network interfaces)*/
903 static int ipn_setpersist_netdev(struct ifreq *ifr, int value)
905 struct net_device *dev;
906 struct ipn_node *ipn_node;
907 int err=0;
908 if (!capable(CAP_NET_ADMIN))
909 return -EPERM;
910 if (down_interruptible(&ipn_glob_mutex))
911 return -ERESTARTSYS;
912 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
913 dev=__dev_get_by_name(ifr->ifr_name);
914 #else
915 dev=__dev_get_by_name(&init_net,ifr->ifr_name);
916 #endif
917 if (!dev)
918 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
919 dev=__dev_get_by_index(ifr->ifr_ifindex);
920 #else
921 dev=__dev_get_by_index(&init_net,ifr->ifr_ifindex);
922 #endif
923 if (dev && (ipn_node=ipn_netdev2node(dev)) != NULL)
924 _ipn_setpersist(ipn_node,value);
925 else
926 err=-EADDRNOTAVAIL;
927 up(&ipn_glob_mutex);
928 return err;
931 /* IPN IOCTL */
932 static int ipn_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) {
933 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
934 struct ipn_network *ipnn=ipn_node->ipn;
935 void __user* argp = (void __user*)arg;
936 struct ifreq ifr;
938 if (ipn_node->shutdown == SHUTDOWN_XMASK)
939 return -ECONNRESET;
941 /* get arguments */
942 switch (cmd) {
943 case IPN_CHECK:
944 return IPN_CHECK;
945 case IPN_SETPERSIST_NETDEV:
946 case IPN_CLRPERSIST_NETDEV:
947 case IPN_CONN_NETDEV:
948 case IPN_JOIN_NETDEV:
949 case SIOCSIFHWADDR:
950 if (copy_from_user(&ifr, argp, sizeof ifr))
951 return -EFAULT;
952 ifr.ifr_name[IFNAMSIZ-1] = '\0';
955 /* actions for unconnected and unbound sockets */
956 switch (cmd) {
957 case IPN_SETPERSIST_NETDEV:
958 return ipn_setpersist_netdev(&ifr,1);
959 case IPN_CLRPERSIST_NETDEV:
960 return ipn_setpersist_netdev(&ifr,0);
961 case SIOCSIFHWADDR:
962 if (capable(CAP_NET_ADMIN))
963 return -EPERM;
964 if (ipn_node->dev && (ipn_node->flags &IPN_NODEFLAG_TAP))
965 return dev_set_mac_address(ipn_node->dev, &ifr.ifr_hwaddr);
966 else
967 return -EADDRNOTAVAIL;
969 if (ipnn == NULL || (ipnn->flags & IPN_FLAG_TERMINATED))
970 return -ENOTCONN;
971 /* actions for connected or bound sockets */
972 switch (cmd) {
973 case IPN_CONN_NETDEV:
974 return ipn_connect_netdev(sock,&ifr);
975 case IPN_JOIN_NETDEV:
976 return ipn_join_netdev(sock,&ifr);
977 case IPN_SETPERSIST:
978 return ipn_setpersist(ipn_node,arg);
979 default:
980 if (ipnn) {
981 int rv;
982 if (down_interruptible(&ipnn->ipnn_mutex))
983 return -ERESTARTSYS;
984 rv=ipn_protocol_table[ipn_node->protocol]->ipn_p_ioctl(ipn_node,cmd,arg);
985 up(&ipnn->ipnn_mutex);
986 return rv;
987 } else
988 return -EOPNOTSUPP;
992 /* shutdown: close socket for input or for output.
993 * shutdown can be called prior to connect and it is not reversible */
994 static int ipn_shutdown(struct socket *sock, int mode) {
995 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
996 struct ipn_network *ipnn=ipn_node->ipn;
997 int oldshutdown=ipn_node->shutdown;
998 mode = (mode+1)&(RCV_SHUTDOWN|SEND_SHUTDOWN);
1000 ipn_node->shutdown |= mode;
1002 if(ipnn) {
1003 if (down_interruptible(&ipnn->ipnn_mutex)) {
1004 ipn_node->shutdown = oldshutdown;
1005 return -ERESTARTSYS;
1007 oldshutdown=ipn_node->shutdown-oldshutdown;
1008 if (sock->state == SS_CONNECTED && oldshutdown) {
1009 ipn_net_update_counters(ipnn,
1010 (ipn_node->shutdown & RCV_SHUTDOWN)?0:-1,
1011 (ipn_node->shutdown & SEND_SHUTDOWN)?0:-1);
1014 /* if recv channel has been shut down, flush the recv queue */
1015 if ((ipn_node->shutdown & RCV_SHUTDOWN))
1016 ipn_flush_recvqueue(ipn_node);
1017 up(&ipnn->ipnn_mutex);
1019 return 0;
1022 /* injectmsg: a new message is entering the ipn network.
1023 * injectmsg gets called by send and by the grab/tap node */
1024 int ipn_proto_injectmsg(struct ipn_node *from, struct msgpool_item *msg)
1026 struct ipn_network *ipnn=from->ipn;
1027 int err=0;
1028 if (down_interruptible(&ipnn->ipnn_mutex))
1029 err=-ERESTARTSYS;
1030 else {
1031 ipn_protocol_table[ipnn->protocol]->ipn_p_handlemsg(from, msg);
1032 up(&ipnn->ipnn_mutex);
1034 return err;
1037 /* SEND MSG */
1038 static int ipn_sendmsg(struct kiocb *kiocb, struct socket *sock,
1039 struct msghdr *msg, size_t len) {
1040 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
1041 struct ipn_network *ipnn=ipn_node->ipn;
1042 struct msgpool_item *newmsg;
1043 int err=0;
1045 if (unlikely(sock->state != SS_CONNECTED))
1046 return -ENOTCONN;
1047 if (unlikely(ipn_node->shutdown & SEND_SHUTDOWN)) {
1048 if (ipn_node->shutdown == SHUTDOWN_XMASK)
1049 return -ECONNRESET;
1050 else
1051 return -EPIPE;
1053 if (len > ipnn->mtu)
1054 return -EOVERFLOW;
1055 newmsg=ipn_msgpool_alloc_locking(ipnn);
1056 if (!newmsg)
1057 return -ENOMEM;
1058 newmsg->len=len;
1059 err=memcpy_fromiovec(newmsg->data, msg->msg_iov, len);
1060 if (!err)
1061 ipn_proto_injectmsg(ipn_node, newmsg);
1062 ipn_msgpool_put(newmsg,ipnn);
1063 return err;
1066 /* enqueue an oob message. "to" is the destination */
1067 void ipn_proto_oobsendmsg(struct ipn_node *to, struct msgpool_item *msg)
1069 if (to) {
1070 if (!to->dev) { /* no oob to netdev */
1071 struct msgitem *msgitem;
1072 struct ipn_network *ipnn=to->ipn;
1073 spin_lock(&to->msglock);
1074 if ((to->shutdown & RCV_SHUTDOWN_NO_OOB) == 0 &&
1075 (ipnn->flags & IPN_FLAG_LOSSLESS ||
1076 to->oobmsgcount < ipnn->msgpool_size)) {
1077 if ((msgitem=kmem_cache_alloc(ipn_msgitem_cache,GFP_KERNEL))!=NULL) {
1078 msgitem->msg=msg;
1079 to->totmsgcount++;
1080 to->oobmsgcount++;
1081 list_add_tail(&msgitem->list, &to->oobmsgqueue);
1082 ipn_msgpool_hold(msg);
1085 spin_unlock(&to->msglock);
1086 wake_up_interruptible(&to->read_wait);
1091 /* ipn_proto_sendmsg is called by protocol implementation to enqueue a
1092 * for a destination (to).*/
1093 void ipn_proto_sendmsg(struct ipn_node *to, struct msgpool_item *msg)
1095 if (to) {
1096 if (to->dev) {
1097 ipn_netdev_sendmsg(to,msg);
1098 } else {
1099 /* socket send */
1100 struct msgitem *msgitem;
1101 struct ipn_network *ipnn=to->ipn;
1102 spin_lock(&to->msglock);
1103 if ((ipnn->flags & IPN_FLAG_LOSSLESS ||
1104 to->totmsgcount < ipnn->msgpool_size) &&
1105 (to->shutdown & RCV_SHUTDOWN)==0) {
1106 if ((msgitem=kmem_cache_alloc(ipn_msgitem_cache,GFP_KERNEL))!=NULL) {
1107 msgitem->msg=msg;
1108 to->totmsgcount++;
1109 list_add_tail(&msgitem->list, &to->msgqueue);
1110 ipn_msgpool_hold(msg);
1113 spin_unlock(&to->msglock);
1114 wake_up_interruptible(&to->read_wait);
1119 /* IPN RECV */
1120 static int ipn_recvmsg(struct kiocb *kiocb, struct socket *sock,
1121 struct msghdr *msg, size_t len, int flags) {
1122 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
1123 struct ipn_network *ipnn=ipn_node->ipn;
1124 struct msgitem *msgitem;
1125 struct msgpool_item *currmsg;
1127 if (unlikely(sock->state != SS_CONNECTED))
1128 return -ENOTCONN;
1130 if (unlikely((ipn_node->shutdown & XRCV_SHUTDOWN) == XRCV_SHUTDOWN)) {
1131 if (ipn_node->shutdown == SHUTDOWN_XMASK) /*EOF, nothing can be read*/
1132 return 0;
1133 else
1134 return -EPIPE; /*trying to read on a write only node */
1137 /* wait for a message */
1138 spin_lock(&ipn_node->msglock);
1139 while (ipn_node->totmsgcount == 0) {
1140 spin_unlock(&ipn_node->msglock);
1141 if (wait_event_interruptible(ipn_node->read_wait,
1142 !(ipn_node->totmsgcount == 0)))
1143 return -ERESTARTSYS;
1144 spin_lock(&ipn_node->msglock);
1146 /* oob gets delivered first. oob are rare */
1147 if (likely(list_empty(&ipn_node->oobmsgqueue)))
1148 msgitem=list_first_entry(&ipn_node->msgqueue, struct msgitem, list);
1149 else {
1150 msgitem=list_first_entry(&ipn_node->oobmsgqueue, struct msgitem, list);
1151 msg->msg_flags |= MSG_OOB;
1152 ipn_node->oobmsgcount--;
1154 list_del(&msgitem->list);
1155 ipn_node->totmsgcount--;
1156 spin_unlock(&ipn_node->msglock);
1157 currmsg=msgitem->msg;
1158 if (currmsg->len < len)
1159 len=currmsg->len;
1160 memcpy_toiovec(msg->msg_iov, currmsg->data, len);
1161 ipn_msgpool_put(currmsg,ipnn);
1162 kmem_cache_free(ipn_msgitem_cache,msgitem);
1164 return len;
1167 /* resize a network: change the # of communication ports (connport) */
1168 static int ipn_netresize(struct ipn_network *ipnn,int newsize)
1170 int oldsize,min;
1171 struct ipn_node **newconnport;
1172 struct ipn_node **oldconnport;
1173 int err;
1174 if (down_interruptible(&ipnn->ipnn_mutex))
1175 return -ERESTARTSYS;
1176 oldsize=ipnn->maxports;
1177 if (newsize == oldsize) {
1178 up(&ipnn->ipnn_mutex);
1179 return 0;
1181 min=oldsize;
1182 /* shrink a network. all the ports we are going to eliminate
1183 * must be unused! */
1184 if (newsize < oldsize) {
1185 int i;
1186 for (i=newsize; i<oldsize; i++)
1187 if (ipnn->connport[i]) {
1188 up(&ipnn->ipnn_mutex);
1189 return -EADDRINUSE;
1191 min=newsize;
1193 oldconnport=ipnn->connport;
1194 /* allocate the new connport array and copy the old one */
1195 newconnport=kzalloc(newsize * sizeof(struct ipn_node *),GFP_KERNEL);
1196 if (!newconnport) {
1197 up(&ipnn->ipnn_mutex);
1198 return -ENOMEM;
1200 memcpy(newconnport,oldconnport,min * sizeof(struct ipn_node *));
1201 ipnn->connport=newconnport;
1202 ipnn->maxports=newsize;
1203 /* notify the protocol that the netowrk has been resized */
1204 err=ipn_protocol_table[ipnn->protocol]->ipn_p_resizenet(ipnn,oldsize,newsize);
1205 if (err) {
1206 /* roll back if the resize operation failed for the protocol */
1207 ipnn->connport=oldconnport;
1208 ipnn->maxports=oldsize;
1209 kfree(newconnport);
1210 } else
1211 /* successful mission, network resized */
1212 kfree(oldconnport);
1213 up(&ipnn->ipnn_mutex);
1214 return err;
1217 /* IPN SETSOCKOPT */
1218 static int ipn_setsockopt(struct socket *sock, int level, int optname,
1219 char __user *optval, int optlen) {
1220 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
1221 struct ipn_network *ipnn=ipn_node->ipn;
1223 if (ipn_node->shutdown == SHUTDOWN_XMASK)
1224 return -ECONNRESET;
1225 if (level != 0 && level != ipn_node->protocol+1)
1226 return -EPROTONOSUPPORT;
1227 if (level > 0) {
1228 /* protocol specific sockopt */
1229 if (ipnn) {
1230 int rv;
1231 if (down_interruptible(&ipnn->ipnn_mutex))
1232 return -ERESTARTSYS;
1233 rv=ipn_protocol_table[ipn_node->protocol]->ipn_p_setsockopt(ipn_node,optname,optval,optlen);
1234 up(&ipnn->ipnn_mutex);
1235 return rv;
1236 } else
1237 return -EOPNOTSUPP;
1238 } else {
1239 if (optname == IPN_SO_DESCR) {
1240 if (optlen > IPN_DESCRLEN)
1241 return -EINVAL;
1242 else {
1243 memset(ipn_node->descr,0,IPN_DESCRLEN);
1244 if (copy_from_user(ipn_node->descr,optval,optlen))
1245 ipn_node->descr[0]=0;
1246 else
1247 ipn_node->descr[optlen-1]=0;
1248 return 0;
1250 } else {
1251 if (optlen < sizeof(int))
1252 return -EINVAL;
1253 else if ((optname & IPN_SO_PREBIND) && (ipnn != NULL))
1254 return -EISCONN;
1255 else {
1256 int val;
1257 get_user(val, (int __user *) optval);
1258 if ((optname & IPN_SO_PREBIND) && !ipn_node->pbp) {
1259 struct pre_bind_parms std=STD_BIND_PARMS;
1260 ipn_node->pbp=kzalloc(sizeof(struct pre_bind_parms),GFP_KERNEL);
1261 if (!ipn_node->pbp)
1262 return -ENOMEM;
1263 *(ipn_node->pbp)=std;
1265 switch (optname) {
1266 case IPN_SO_PORT:
1267 if (sock->state == SS_UNCONNECTED)
1268 ipn_node->portno=val;
1269 else
1270 return -EISCONN;
1271 break;
1272 case IPN_SO_CHANGE_NUMNODES:
1273 if ((ipn_node->flags & IPN_NODEFLAG_BOUND)!=0) {
1274 if (val <= 0)
1275 return -EINVAL;
1276 else
1277 return ipn_netresize(ipnn,val);
1278 } else
1279 val=-ENOTCONN;
1280 break;
1281 case IPN_SO_WANT_OOB_NUMNODES:
1282 if (val)
1283 ipn_node->flags |= IPN_NODEFLAG_OOB_NUMNODES;
1284 else
1285 ipn_node->flags &= ~IPN_NODEFLAG_OOB_NUMNODES;
1286 break;
1287 case IPN_SO_HANDLE_OOB:
1288 if (val)
1289 ipn_node->shutdown &= ~RCV_SHUTDOWN_NO_OOB;
1290 else
1291 ipn_node->shutdown |= RCV_SHUTDOWN_NO_OOB;
1292 break;
1293 case IPN_SO_MTU:
1294 if (val <= 0)
1295 return -EINVAL;
1296 else
1297 ipn_node->pbp->mtu=val;
1298 break;
1299 case IPN_SO_NUMNODES:
1300 if (val <= 0)
1301 return -EINVAL;
1302 else
1303 ipn_node->pbp->maxports=val;
1304 break;
1305 case IPN_SO_MSGPOOLSIZE:
1306 if (val <= 0)
1307 return -EINVAL;
1308 else
1309 ipn_node->pbp->msgpoolsize=val;
1310 break;
1311 case IPN_SO_FLAGS:
1312 ipn_node->pbp->flags=val;
1313 break;
1314 case IPN_SO_MODE:
1315 ipn_node->pbp->mode=val;
1316 break;
1318 return 0;
1324 /* IPN GETSOCKOPT */
1325 static int ipn_getsockopt(struct socket *sock, int level, int optname,
1326 char __user *optval, int __user *optlen) {
1327 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
1328 struct ipn_network *ipnn=ipn_node->ipn;
1329 int len;
1331 if (ipn_node->shutdown == SHUTDOWN_XMASK)
1332 return -ECONNRESET;
1333 if (level != 0 && level != ipn_node->protocol+1)
1334 return -EPROTONOSUPPORT;
1335 if (level > 0) {
1336 if (ipnn) {
1337 int rv;
1338 /* protocol specific sockopt */
1339 if (down_interruptible(&ipnn->ipnn_mutex))
1340 return -ERESTARTSYS;
1341 rv=ipn_protocol_table[ipn_node->protocol]->ipn_p_getsockopt(ipn_node,optname,optval,optlen);
1342 up(&ipnn->ipnn_mutex);
1343 return rv;
1344 } else
1345 return -EOPNOTSUPP;
1346 } else {
1347 if (get_user(len, optlen))
1348 return -EFAULT;
1349 if (optname == IPN_SO_DESCR) {
1350 if (len < IPN_DESCRLEN)
1351 return -EINVAL;
1352 else {
1353 if (len > IPN_DESCRLEN)
1354 len=IPN_DESCRLEN;
1355 if(put_user(len, optlen))
1356 return -EFAULT;
1357 if(copy_to_user(optval,ipn_node->descr,len))
1358 return -EFAULT;
1359 return 0;
1361 } else {
1362 int val=-2;
1363 switch (optname) {
1364 case IPN_SO_PORT:
1365 val=ipn_node->portno;
1366 break;
1367 case IPN_SO_MTU:
1368 if (ipnn)
1369 val=ipnn->mtu;
1370 else if (ipn_node->pbp)
1371 val=ipn_node->pbp->mtu;
1372 break;
1373 case IPN_SO_NUMNODES:
1374 if (ipnn)
1375 val=ipnn->maxports;
1376 else if (ipn_node->pbp)
1377 val=ipn_node->pbp->maxports;
1378 break;
1379 case IPN_SO_MSGPOOLSIZE:
1380 if (ipnn)
1381 val=ipnn->msgpool_size;
1382 else if (ipn_node->pbp)
1383 val=ipn_node->pbp->msgpoolsize;
1384 break;
1385 case IPN_SO_FLAGS:
1386 if (ipnn)
1387 val=ipnn->flags;
1388 else if (ipn_node->pbp)
1389 val=ipn_node->pbp->flags;
1390 break;
1391 case IPN_SO_MODE:
1392 if (ipnn)
1393 val=-1;
1394 else if (ipn_node->pbp)
1395 val=ipn_node->pbp->mode;
1396 break;
1398 if (val < -1)
1399 return -EINVAL;
1400 else {
1401 if (len < sizeof(int))
1402 return -EOVERFLOW;
1403 else {
1404 len = sizeof(int);
1405 if(put_user(len, optlen))
1406 return -EFAULT;
1407 if(copy_to_user(optval,&val,len))
1408 return -EFAULT;
1409 return 0;
1416 /* BROADCAST/HUB implementation */
1418 static int ipn_bcast_newport(struct ipn_node *newport) {
1419 struct ipn_network *ipnn=newport->ipn;
1420 int i;
1421 for (i=0;i<ipnn->maxports;i++) {
1422 if (ipnn->connport[i] == NULL)
1423 return i;
1425 return -1;
1428 static int ipn_bcast_handlemsg(struct ipn_node *from,
1429 struct msgpool_item *msgitem){
1430 struct ipn_network *ipnn=from->ipn;
1432 struct ipn_node *ipn_node;
1433 list_for_each_entry(ipn_node, &ipnn->connectqueue, nodelist) {
1434 if (ipn_node != from)
1435 ipn_proto_sendmsg(ipn_node,msgitem);
1437 return 0;
1440 static void ipn_null_delport(struct ipn_node *oldport) {}
1441 static void ipn_null_postnewport(struct ipn_node *newport) {}
1442 static void ipn_null_predelport(struct ipn_node *oldport) {}
1443 static int ipn_null_newnet(struct ipn_network *newnet) {return 0;}
1444 static int ipn_null_resizenet(struct ipn_network *net,int oldsize,int newsize) {
1445 return 0;}
1446 static void ipn_null_delnet(struct ipn_network *oldnet) {}
1447 static int ipn_null_setsockopt(struct ipn_node *port,int optname,
1448 char __user *optval, int optlen) {return -EOPNOTSUPP;}
1449 static int ipn_null_getsockopt(struct ipn_node *port,int optname,
1450 char __user *optval, int *optlen) {return -EOPNOTSUPP;}
1451 static int ipn_null_ioctl(struct ipn_node *port,unsigned int request,
1452 unsigned long arg) {return -EOPNOTSUPP;}
1454 /* Protocol Registration/deregisteration */
1456 void ipn_init_protocol(struct ipn_protocol *p)
1458 if (p->ipn_p_delport == NULL) p->ipn_p_delport=ipn_null_delport;
1459 if (p->ipn_p_postnewport == NULL) p->ipn_p_postnewport=ipn_null_postnewport;
1460 if (p->ipn_p_predelport == NULL) p->ipn_p_predelport=ipn_null_predelport;
1461 if (p->ipn_p_newnet == NULL) p->ipn_p_newnet=ipn_null_newnet;
1462 if (p->ipn_p_resizenet == NULL) p->ipn_p_resizenet=ipn_null_resizenet;
1463 if (p->ipn_p_delnet == NULL) p->ipn_p_delnet=ipn_null_delnet;
1464 if (p->ipn_p_setsockopt == NULL) p->ipn_p_setsockopt=ipn_null_setsockopt;
1465 if (p->ipn_p_getsockopt == NULL) p->ipn_p_getsockopt=ipn_null_getsockopt;
1466 if (p->ipn_p_ioctl == NULL) p->ipn_p_ioctl=ipn_null_ioctl;
1469 int ipn_proto_register(int protocol,struct ipn_protocol *ipn_service)
1471 int rv=0;
1472 if (ipn_service->ipn_p_newport == NULL ||
1473 ipn_service->ipn_p_handlemsg == NULL)
1474 return -EINVAL;
1475 ipn_init_protocol(ipn_service);
1476 if (down_interruptible(&ipn_glob_mutex))
1477 return -ERESTARTSYS;
1478 if (protocol > 1 && protocol <= IPN_MAX_PROTO) {
1479 protocol--;
1480 if (ipn_protocol_table[protocol])
1481 rv= -EEXIST;
1482 else {
1483 ipn_service->refcnt=0;
1484 ipn_protocol_table[protocol]=ipn_service;
1485 printk(KERN_INFO "IPN: Registered protocol %d\n",protocol+1);
1487 } else
1488 rv= -EINVAL;
1489 up(&ipn_glob_mutex);
1490 return rv;
1493 int ipn_proto_deregister(int protocol)
1495 int rv=0;
1496 if (down_interruptible(&ipn_glob_mutex))
1497 return -ERESTARTSYS;
1498 if (protocol > 1 && protocol <= IPN_MAX_PROTO) {
1499 protocol--;
1500 if (ipn_protocol_table[protocol]) {
1501 if (ipn_protocol_table[protocol]->refcnt == 0) {
1502 ipn_protocol_table[protocol]=NULL;
1503 printk(KERN_INFO "IPN: Unregistered protocol %d\n",protocol+1);
1504 } else
1505 rv=-EADDRINUSE;
1506 } else
1507 rv= -ENOENT;
1508 } else
1509 rv= -EINVAL;
1510 up(&ipn_glob_mutex);
1511 return rv;
1514 /* MAIN SECTION */
1515 /* Module constructor/destructor */
1516 static struct net_proto_family ipn_family_ops = {
1517 .family = PF_IPN,
1518 .create = ipn_create,
1519 .owner = THIS_MODULE,
1522 /* IPN constructor */
1523 static int ipn_init(void)
1525 int rc;
1527 ipn_init_protocol(&ipn_bcast);
1528 ipn_network_cache=kmem_cache_create("ipn_network",sizeof(struct ipn_network),0,0,NULL);
1529 if (!ipn_network_cache) {
1530 printk(KERN_CRIT "%s: Cannot create ipn_network SLAB cache!\n",
1531 __FUNCTION__);
1532 rc=-ENOMEM;
1533 goto out;
1536 ipn_node_cache=kmem_cache_create("ipn_node",sizeof(struct ipn_node),0,0,NULL);
1537 if (!ipn_node_cache) {
1538 printk(KERN_CRIT "%s: Cannot create ipn_node SLAB cache!\n",
1539 __FUNCTION__);
1540 rc=-ENOMEM;
1541 goto out_net;
1544 ipn_msgitem_cache=kmem_cache_create("ipn_msgitem",sizeof(struct msgitem),0,0,NULL);
1545 if (!ipn_msgitem_cache) {
1546 printk(KERN_CRIT "%s: Cannot create ipn_msgitem SLAB cache!\n",
1547 __FUNCTION__);
1548 rc=-ENOMEM;
1549 goto out_net_node;
1552 rc=proto_register(&ipn_proto,1);
1553 if (rc != 0) {
1554 printk(KERN_CRIT "%s: Cannot register the protocol!\n",
1555 __FUNCTION__);
1556 goto out_net_node_msg;
1559 sock_register(&ipn_family_ops);
1560 ipn_netdev_init();
1561 printk(KERN_INFO "IPN: Virtual Square Project, University of Bologna 2007\n");
1562 return 0;
1564 out_net_node_msg:
1565 kmem_cache_destroy(ipn_msgitem_cache);
1566 out_net_node:
1567 kmem_cache_destroy(ipn_node_cache);
1568 out_net:
1569 kmem_cache_destroy(ipn_network_cache);
1570 out:
1571 return rc;
1574 /* IPN destructor */
1575 static void ipn_exit(void)
1577 ipn_netdev_fini();
1578 if (ipn_msgitem_cache)
1579 kmem_cache_destroy(ipn_msgitem_cache);
1580 if (ipn_node_cache)
1581 kmem_cache_destroy(ipn_node_cache);
1582 if (ipn_network_cache)
1583 kmem_cache_destroy(ipn_network_cache);
1584 sock_unregister(PF_IPN);
1585 proto_unregister(&ipn_proto);
1586 printk(KERN_INFO "IPN removed\n");
1589 module_init(ipn_init);
1590 module_exit(ipn_exit);
1592 EXPORT_SYMBOL_GPL(ipn_proto_register);
1593 EXPORT_SYMBOL_GPL(ipn_proto_deregister);
1594 EXPORT_SYMBOL_GPL(ipn_proto_sendmsg);
1595 EXPORT_SYMBOL_GPL(ipn_proto_oobsendmsg);
1596 EXPORT_SYMBOL_GPL(ipn_msgpool_alloc);
1597 EXPORT_SYMBOL_GPL(ipn_msgpool_put);