bugfix for kvde_switch
[vde.git] / ipn / af_ipn.c
blob0431634d968c45646929840e79102c5f423d6d4b
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"
35 #include "ipn_msgbuf.h"
37 MODULE_LICENSE("GPL");
38 MODULE_AUTHOR("VIEW-OS TEAM");
39 MODULE_DESCRIPTION("IPN Kernel Module");
41 #define IPN_MAX_PROTO 4
43 /*extension of RCV_SHUTDOWN defined in include/net/sock.h
44 * when the bit is set recv fails */
45 /* NO_OOB: do not send OOB */
46 #define RCV_SHUTDOWN_NO_OOB 4
47 /* EXTENDED MASK including OOB */
48 #define SHUTDOWN_XMASK (SHUTDOWN_MASK | RCV_SHUTDOWN_NO_OOB)
49 /* if XRCV_SHUTDOWN is all set recv fails */
50 #define XRCV_SHUTDOWN (RCV_SHUTDOWN | RCV_SHUTDOWN_NO_OOB)
52 /* Network table and hash */
53 struct hlist_head ipn_network_table[IPN_HASH_SIZE + 1];
54 /* not needed. Now protected by ipn_glob_mutex
55 * comment *IPNTL*
56 * DEFINE_SPINLOCK(ipn_table_lock);
58 static struct kmem_cache *ipn_network_cache;
59 static struct kmem_cache *ipn_node_cache;
60 static struct kmem_cache *ipn_msgitem_cache;
61 static DECLARE_MUTEX(ipn_glob_mutex);
63 /* Protocol 1: HUB/Broadcast default protocol. Function Prototypes */
64 static int ipn_bcast_newport(struct ipn_node *newport);
65 static int ipn_bcast_handlemsg(struct ipn_node *from,
66 struct msgpool_item *msgitem);
68 /* default protocol IPN_BROADCAST (0) */
69 static struct ipn_protocol ipn_bcast = {
70 .refcnt=0,
71 .ipn_p_newport=ipn_bcast_newport,
72 .ipn_p_handlemsg=ipn_bcast_handlemsg};
73 /* Protocol table */
74 static struct ipn_protocol *ipn_protocol_table[IPN_MAX_PROTO]={&ipn_bcast};
76 /* Socket call function prototypes */
77 static int ipn_release(struct socket *);
78 static int ipn_bind(struct socket *, struct sockaddr *, int);
79 static int ipn_connect(struct socket *, struct sockaddr *,
80 int addr_len, int flags);
81 static int ipn_getname(struct socket *, struct sockaddr *, int *, int);
82 static unsigned int ipn_poll(struct file *, struct socket *, poll_table *);
83 static int ipn_ioctl(struct socket *, unsigned int, unsigned long);
84 static int ipn_shutdown(struct socket *, int);
85 static int ipn_sendmsg(struct kiocb *, struct socket *,
86 struct msghdr *, size_t);
87 static int ipn_recvmsg(struct kiocb *, struct socket *,
88 struct msghdr *, size_t, int);
89 static int ipn_setsockopt(struct socket *sock, int level, int optname,
90 char __user *optval, int optlen);
91 static int ipn_getsockopt(struct socket *sock, int level, int optname,
92 char __user *optval, int __user *optlen);
94 /* Network table Management
95 * inode->ipn_network hash table
96 * LOCKING: MUTEX ipn_glob_mutex must be LOCKED*/
97 static inline void ipn_insert_network(struct hlist_head *list, struct ipn_network *ipnn)
99 /* *IPNTL* spin_lock(&ipn_table_lock); */
100 hlist_add_head(&ipnn->hnode, list);
101 /* *IPNTL* spin_unlock(&ipn_table_lock); */
104 static inline void ipn_remove_network(struct ipn_network *ipnn)
106 /* *IPNTL* spin_lock(&ipn_table_lock); */
107 hlist_del(&ipnn->hnode);
108 /* *IPNTL* spin_unlock(&ipn_table_lock); */
111 static struct ipn_network *ipn_find_network_byinode(struct inode *i)
113 struct ipn_network *ipnn;
114 struct hlist_node *node;
116 /* *IPNTL* spin_lock(&ipn_table_lock);*/
117 hlist_for_each_entry(ipnn, node,
118 &ipn_network_table[i->i_ino & (IPN_HASH_SIZE - 1)], hnode) {
119 struct dentry *dentry = ipnn->dentry;
121 if(ipnn->refcnt > 0 && dentry && dentry->d_inode == i)
122 goto found;
124 ipnn = NULL;
125 found:
126 /* *IPNTL* spin_unlock(&ipn_table_lock); */
127 return ipnn;
130 /* msgpool management
131 * msgpool_item are ipn_network dependent (each net has its own MTU)
132 * for each message sent there is one msgpool_item and many struct msgitem
133 * one for each receipient.
134 * msgitem are connected to the node's msgqueue or oobmsgqueue.
135 * when a message is delivered to a process the msgitem is deleted and
136 * the count of the msgpool_item is decreased.
137 * msgpool_item elements gets deleted automatically when count is 0*/
139 struct msgitem {
140 struct list_head list;
141 struct msgpool_item *msg;
144 /* alloc a fresh msgpool item. count is set to 1.
145 * the typical use is
146 * ipn_msgpool_alloc
147 * for each receipient
148 * enqueue messages to the process (using msgitem), ipn_msgpool_hold
149 * ipn_msgpool_put
150 * The message can be delivered concurrently. init count to 1 guarantees
151 * that it survives at least until is has been enqueued to all
152 * receivers */
153 static struct msgpool_item *_ipn_msgpool_alloc(struct ipn_network *ipnn)
155 struct msgpool_item *new;
156 if ((new=kmem_cache_alloc(ipnn->msgpool_cache,GFP_KERNEL)) != NULL) {
157 atomic_set(&new->count,1);
158 atomic_inc(&ipnn->msgpool_nelem);
160 return new;
163 struct msgpool_item *ipn_msgpool_alloc(struct ipn_network *ipnn,int leaky)
165 if (leaky && (ipnn->flags & IPN_FLAG_LOSSLESS) &&
166 atomic_read(&ipnn->msgpool_nelem) < ipnn->msgpool_size)
167 return NULL;
168 else
169 return _ipn_msgpool_alloc(ipnn);
172 /* If the service il LOSSLESS, this msgpool call waits for an
173 * available msgpool item */
174 static struct msgpool_item *ipn_msgpool_alloc_locking(struct ipn_network *ipnn)
176 if (ipnn->flags & IPN_FLAG_LOSSLESS) {
177 while (atomic_read(&ipnn->msgpool_nelem) >= ipnn->msgpool_size) {
178 if (wait_event_interruptible_exclusive(ipnn->send_wait,
179 atomic_read(&ipnn->msgpool_nelem) < ipnn->msgpool_size))
180 return NULL;
183 return _ipn_msgpool_alloc(ipnn);
186 static inline void ipn_msgpool_hold(struct msgpool_item *msg)
188 atomic_inc(&msg->count);
191 /* decrease count and delete msgpool_item if count == 0 */
192 void ipn_msgpool_put(struct msgpool_item *old,
193 struct ipn_network *ipnn)
195 if (atomic_dec_and_test(&old->count)) {
196 kmem_cache_free(ipnn->msgpool_cache,old);
197 atomic_dec(&ipnn->msgpool_nelem);
198 if (ipnn->flags & IPN_FLAG_LOSSLESS) /* this could be done anyway */
199 wake_up_interruptible(&ipnn->send_wait);
203 /* socket calls */
204 static const struct proto_ops ipn_ops = {
205 .family = PF_IPN,
206 .owner = THIS_MODULE,
207 .release = ipn_release,
208 .bind = ipn_bind,
209 .connect = ipn_connect,
210 .socketpair = sock_no_socketpair,
211 .accept = sock_no_accept,
212 .getname = ipn_getname,
213 .poll = ipn_poll,
214 .ioctl = ipn_ioctl,
215 .listen = sock_no_listen,
216 .shutdown = ipn_shutdown,
217 .setsockopt = ipn_setsockopt,
218 .getsockopt = ipn_getsockopt,
219 .sendmsg = ipn_sendmsg,
220 .recvmsg = ipn_recvmsg,
221 .mmap = sock_no_mmap,
222 .sendpage = sock_no_sendpage,
225 static struct proto ipn_proto = {
226 .name = "IPN",
227 .owner = THIS_MODULE,
228 .obj_size = sizeof(struct ipn_sock),
231 /* create a socket
232 * ipn_node is a separate structure, pointed by ipn_sock -> node
233 * when a node is "persistent", ipn_node survives while ipn_sock gets released*/
234 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
235 static int ipn_create(struct socket *sock, int protocol)
236 #else
237 static int ipn_create(struct net *net,struct socket *sock, int protocol)
238 #endif
240 struct ipn_sock *ipn_sk;
241 struct ipn_node *ipn_node;
243 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
244 void *net=NULL;
245 #else
246 if (net != &init_net)
247 return -EAFNOSUPPORT;
248 #endif
250 if (sock->type != SOCK_RAW)
251 return -EPROTOTYPE;
252 if (protocol > 0)
253 protocol=protocol-1;
254 else
255 protocol=IPN_BROADCAST-1;
256 if (protocol < 0 || protocol >= IPN_MAX_PROTO ||
257 ipn_protocol_table[protocol] == NULL)
258 return -EPROTONOSUPPORT;
259 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
260 ipn_sk = (struct ipn_sock *) sk_alloc(PF_IPN, GFP_KERNEL, &ipn_proto, 1);
261 #else
262 ipn_sk = (struct ipn_sock *) sk_alloc(net, PF_IPN, GFP_KERNEL, &ipn_proto);
263 #endif
265 if (!ipn_sk)
266 return -ENOMEM;
267 ipn_sk->node=ipn_node=kmem_cache_alloc(ipn_node_cache,GFP_KERNEL);
268 if (!ipn_node) {
269 sock_put((struct sock *) ipn_sk);
270 return -ENOMEM;
272 sock_init_data(sock,(struct sock *) ipn_sk);
273 sock->state = SS_UNCONNECTED;
274 sock->ops = &ipn_ops;
275 sock->sk=(struct sock *)ipn_sk;
276 INIT_LIST_HEAD(&ipn_node->nodelist);
277 ipn_node->protocol=protocol;
278 ipn_node->flags=IPN_NODEFLAG_INUSE;
279 ipn_node->shutdown=RCV_SHUTDOWN_NO_OOB;
280 ipn_node->descr[0]=0;
281 ipn_node->portno=IPN_PORTNO_ANY;
282 ipn_node->net=net;
283 ipn_node->dev=NULL;
284 ipn_node->proto_private=NULL;
285 ipn_node->totmsgcount=0;
286 ipn_node->oobmsgcount=0;
287 spin_lock_init(&ipn_node->msglock);
288 INIT_LIST_HEAD(&ipn_node->msgqueue);
289 INIT_LIST_HEAD(&ipn_node->oobmsgqueue);
290 ipn_node->ipn=NULL;
291 init_waitqueue_head(&ipn_node->read_wait);
292 ipn_node->pbp=NULL;
293 return 0;
296 /* update # of readers and # of writers counters for an ipn network.
297 * This function sends oob messages to nodes requesting the service */
298 /* LOCKING ipnn_mutex is locked */
299 static void ipn_net_update_counters(struct ipn_network *ipnn,
300 int chg_readers, int chg_writers) {
301 ipnn->numreaders += chg_readers;
302 ipnn->numwriters += chg_writers;
303 if (ipnn->mtu >= sizeof(struct numnode_oob))
305 struct msgpool_item *ipn_msg=_ipn_msgpool_alloc(ipnn);
306 if (ipn_msg) {
307 struct numnode_oob *oob_msg=(struct numnode_oob *)(ipn_msg->data);
308 struct ipn_node *ipn_node;
309 ipn_msg->len=sizeof(struct numnode_oob);
310 oob_msg->level=IPN_ANY;
311 oob_msg->tag=IPN_OOB_NUMNODE_TAG;
312 oob_msg->numreaders=ipnn->numreaders;
313 oob_msg->numwriters=ipnn->numwriters;
314 list_for_each_entry(ipn_node, &ipnn->connectqueue, nodelist) {
315 if (ipn_node->flags & IPN_NODEFLAG_OOB_NUMNODES)
316 ipn_proto_oobsendmsg(ipn_node,ipn_msg);
318 ipn_msgpool_put(ipn_msg,ipnn);
323 /* flush pending messages (for close and shutdown RCV) */
324 /* LOCKING: ipnn_mutex is locked */
325 static void ipn_flush_recvqueue(struct ipn_node *ipn_node)
327 struct ipn_network *ipnn=ipn_node->ipn;
328 spin_lock(&ipn_node->msglock);
329 while (!list_empty(&ipn_node->msgqueue)) {
330 struct msgitem *msgitem=
331 list_first_entry(&ipn_node->msgqueue, struct msgitem, list);
332 list_del(&msgitem->list);
333 ipn_node->totmsgcount--;
334 ipn_msgpool_put(msgitem->msg,ipnn);
335 kmem_cache_free(ipn_msgitem_cache,msgitem);
337 spin_unlock(&ipn_node->msglock);
340 /* flush pending oob messages (for socket close) */
341 /* LOCKING: ipnn_mutex is locked */
342 static void ipn_flush_oobrecvqueue(struct ipn_node *ipn_node)
344 struct ipn_network *ipnn=ipn_node->ipn;
345 spin_lock(&ipn_node->msglock);
346 while (!list_empty(&ipn_node->oobmsgqueue)) {
347 struct msgitem *msgitem=
348 list_first_entry(&ipn_node->oobmsgqueue, struct msgitem, list);
349 list_del(&msgitem->list);
350 ipn_node->totmsgcount--;
351 ipn_node->oobmsgcount--;
352 ipn_msgpool_put(msgitem->msg,ipnn);
353 kmem_cache_free(ipn_msgitem_cache,msgitem);
355 spin_unlock(&ipn_node->msglock);
358 /* Terminate node. The node is "logically" terminated. */
359 /* LOCKING: ipn_glob_lock must be locked here */
360 static int ipn_terminate_node(struct ipn_node *ipn_node)
362 struct ipn_network *ipnn=ipn_node->ipn;
363 if (ipnn) {
364 if (down_interruptible(&ipnn->ipnn_mutex))
365 return -ERESTARTSYS;
366 if (ipn_node->portno >= 0) {
367 ipn_protocol_table[ipnn->protocol]->ipn_p_predelport(ipn_node);
368 ipnn->connport[ipn_node->portno]=NULL;
370 list_del(&ipn_node->nodelist);
371 ipn_flush_recvqueue(ipn_node);
372 ipn_flush_oobrecvqueue(ipn_node);
373 if (ipn_node->portno >= 0)
374 ipn_protocol_table[ipnn->protocol]->ipn_p_delport(ipn_node);
375 ipn_node->ipn=NULL;
376 ipn_net_update_counters(ipnn,
377 (ipn_node->shutdown & RCV_SHUTDOWN)?0:-1,
378 (ipn_node->shutdown & SEND_SHUTDOWN)?0:-1);
379 ipn_node->shutdown = SHUTDOWN_XMASK;
380 up(&ipnn->ipnn_mutex);
381 if (ipn_node->dev)
382 ipn_netdev_close(ipn_node);
383 /* No more network elements */
384 ipnn->refcnt--;
385 if (ipnn->refcnt == 0)
387 ipn_protocol_table[ipnn->protocol]->ipn_p_delnet(ipnn);
388 ipn_remove_network(ipnn);
389 ipn_protocol_table[ipnn->protocol]->refcnt--;
390 if (ipnn->dentry) {
391 dput(ipnn->dentry);
392 mntput(ipnn->mnt);
394 if (ipnn->msgpool_cache)
395 ipn_msgbuf_put(ipnn->msgpool_cache);
396 if (ipnn->connport)
397 kfree(ipnn->connport);
398 kmem_cache_free(ipn_network_cache, ipnn);
399 module_put(THIS_MODULE);
402 if (ipn_node->pbp) {
403 kfree(ipn_node->pbp);
404 ipn_node->pbp=NULL;
406 return 0;
409 /* release of a socket */
410 static int ipn_release (struct socket *sock)
412 struct ipn_sock *ipn_sk=(struct ipn_sock *)sock->sk;
413 struct ipn_node *ipn_node=ipn_sk->node;
414 int rv;
415 if (down_interruptible(&ipn_glob_mutex))
416 return -ERESTARTSYS;
417 if (ipn_node->flags & IPN_NODEFLAG_PERSIST) {
418 ipn_node->flags &= ~IPN_NODEFLAG_INUSE;
419 rv=0;
420 up(&ipn_glob_mutex);
421 } else {
422 rv=ipn_terminate_node(ipn_node);
423 up(&ipn_glob_mutex);
424 if (rv==0) {
425 ipn_netdevsync();
426 kmem_cache_free(ipn_node_cache,ipn_node);
429 if (rv==0)
430 sock_put((struct sock *) ipn_sk);
431 return rv;
434 /* _set persist, change the persistence of a node,
435 * when persistence gets cleared and the node is no longer used
436 * the node is terminated and freed.
437 * ipn_glob_mutex must be locked */
438 static int _ipn_setpersist(struct ipn_node *ipn_node, int persist)
440 int rv=0;
441 if (persist)
442 ipn_node->flags |= IPN_NODEFLAG_PERSIST;
443 else {
444 ipn_node->flags &= ~IPN_NODEFLAG_PERSIST;
445 if (!(ipn_node->flags & IPN_NODEFLAG_INUSE)) {
446 rv=ipn_terminate_node(ipn_node);
447 if (rv==0)
448 kmem_cache_free(ipn_node_cache,ipn_node);
451 return rv;
454 /* ipn_setpersist
455 * lock ipn_glob_mutex and call __ipn_setpersist above */
456 static int ipn_setpersist(struct ipn_node *ipn_node, int persist)
458 int rv=0;
459 if (ipn_node->dev == NULL)
460 return -ENODEV;
461 if (down_interruptible(&ipn_glob_mutex))
462 return -ERESTARTSYS;
463 rv=_ipn_setpersist(ipn_node,persist);
464 up(&ipn_glob_mutex);
465 return rv;
468 /* several network parameters can be set by setsockopt prior to bind */
469 /* struct pre_bind_parms is a temporary stucture connected to ipn_node->pbp
470 * to keep the parameter values. */
471 struct pre_bind_parms {
472 unsigned short maxports;
473 unsigned short flags;
474 unsigned short msgpoolsize;
475 unsigned short mtu;
476 unsigned short mode;
479 /* STD_PARMS: BITS_PER_LONG nodes, no flags, BITS_PER_BYTE pending msgs,
480 * Ethernet + VLAN MTU*/
481 #define STD_BIND_PARMS {BITS_PER_LONG, 0, BITS_PER_BYTE, 1514, 0x777};
483 static int ipn_mkname(struct sockaddr_un * sunaddr, int len)
485 if (len <= sizeof(short) || len > sizeof(*sunaddr))
486 return -EINVAL;
487 if (!sunaddr || sunaddr->sun_family != AF_IPN)
488 return -EINVAL;
490 * This may look like an off by one error but it is a bit more
491 * subtle. 108 is the longest valid AF_IPN path for a binding.
492 * sun_path[108] doesnt as such exist. However in kernel space
493 * we are guaranteed that it is a valid memory location in our
494 * kernel address buffer.
496 ((char *)sunaddr)[len]=0;
497 len = strlen(sunaddr->sun_path)+1+sizeof(short);
498 return len;
501 /* IPN BIND */
502 static int ipn_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
504 struct sockaddr_un *sunaddr=(struct sockaddr_un *)uaddr;
505 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
506 struct nameidata nd;
507 struct ipn_network *ipnn;
508 struct dentry * dentry = NULL;
509 int err;
510 struct pre_bind_parms parms=STD_BIND_PARMS;
512 //printk("IPN bind\n");
514 if (down_interruptible(&ipn_glob_mutex))
515 return -ERESTARTSYS;
516 if (sock->state != SS_UNCONNECTED ||
517 ipn_node->ipn != NULL) {
518 err= -EISCONN;
519 goto out;
522 if (ipn_node->protocol >= 0 &&
523 (ipn_node->protocol >= IPN_MAX_PROTO ||
524 ipn_protocol_table[ipn_node->protocol] == NULL)) {
525 err= -EPROTONOSUPPORT;
526 goto out;
529 addr_len = ipn_mkname(sunaddr, addr_len);
530 if (addr_len < 0) {
531 err=addr_len;
532 goto out;
535 /* check if there is already an ipn-network socket with that name */
536 err = path_lookup(sunaddr->sun_path, LOOKUP_FOLLOW, &nd);
537 if (err) { /* it does not exist, NEW IPN socket! */
538 unsigned int mode;
539 /* Is it everything okay with the parent? */
540 err = path_lookup(sunaddr->sun_path, LOOKUP_PARENT, &nd);
541 if (err)
542 goto out_mknod_parent;
543 /* Do I have the permission to create a file? */
544 dentry = lookup_create(&nd, 0);
545 err = PTR_ERR(dentry);
546 if (IS_ERR(dentry))
547 goto out_mknod_unlock;
549 * All right, let's create it.
551 if (ipn_node->pbp)
552 mode = ipn_node->pbp->mode;
553 else
554 mode = SOCK_INODE(sock)->i_mode;
555 mode = S_IFSOCK | (mode & ~current->fs->umask);
556 #ifdef APPARMOR
557 err = vfs_mknod(nd.dentry->d_inode, dentry, nd.mnt, mode, 0);
558 #else
559 err = vfs_mknod(nd.dentry->d_inode, dentry, mode, 0);
560 #endif
561 if (err)
562 goto out_mknod_dput;
563 mutex_unlock(&nd.dentry->d_inode->i_mutex);
564 dput(nd.dentry);
565 nd.dentry = dentry;
566 /* create a new ipn_network item */
567 if (ipn_node->pbp)
568 parms=*ipn_node->pbp;
569 ipnn=kmem_cache_zalloc(ipn_network_cache,GFP_KERNEL);
570 if (!ipnn) {
571 err=-ENOMEM;
572 goto out_mknod_dput_ipnn;
574 ipnn->connport=kzalloc(parms.maxports * sizeof(struct ipn_node *),GFP_KERNEL);
575 if (!ipnn->connport) {
576 err=-ENOMEM;
577 goto out_mknod_dput_ipnn2;
580 /* module refcnt is incremented for each network, thus
581 * rmmod is forbidden if there are persistent node */
582 if (!try_module_get(THIS_MODULE)) {
583 err = -EINVAL;
584 goto out_mknod_dput_ipnn2;
586 memcpy(&ipnn->sunaddr,sunaddr,addr_len);
587 ipnn->mtu=parms.mtu;
588 ipnn->msgpool_cache=ipn_msgbuf_get(ipnn->mtu);
589 if (!ipnn->msgpool_cache) {
590 err=-ENOMEM;
591 goto out_mknod_dput_putmodule;
593 INIT_LIST_HEAD(&ipnn->unconnectqueue);
594 INIT_LIST_HEAD(&ipnn->connectqueue);
595 ipnn->refcnt=1;
596 ipnn->dentry=nd.dentry;
597 ipnn->mnt=nd.mnt;
598 init_MUTEX(&ipnn->ipnn_mutex);
599 ipnn->sunaddr_len=addr_len;
600 ipnn->protocol=ipn_node->protocol;
601 if (ipnn->protocol < 0) ipnn->protocol = 0;
602 ipn_protocol_table[ipnn->protocol]->refcnt++;
603 ipnn->flags=parms.flags;
604 ipnn->numreaders=0;
605 ipnn->numwriters=0;
606 ipnn->maxports=parms.maxports;
607 atomic_set(&ipnn->msgpool_nelem,0);
608 ipnn->msgpool_size=parms.msgpoolsize;
609 ipnn->proto_private=NULL;
610 init_waitqueue_head(&ipnn->send_wait);
611 err=ipn_protocol_table[ipnn->protocol]->ipn_p_newnet(ipnn);
612 if (err)
613 goto out_mknod_dput_putmodule;
614 ipn_insert_network(&ipn_network_table[nd.dentry->d_inode->i_ino & (IPN_HASH_SIZE-1)],ipnn);
615 } else {
616 /* join an existing network */
617 if (parms.flags & IPN_FLAG_EXCL) {
618 err=-EEXIST;
619 goto put_fail;
621 err = vfs_permission(&nd, MAY_EXEC);
622 if (err)
623 goto put_fail;
624 err = -ECONNREFUSED;
625 if (!S_ISSOCK(nd.dentry->d_inode->i_mode))
626 goto put_fail;
627 ipnn=ipn_find_network_byinode(nd.dentry->d_inode);
628 if (!ipnn || (ipnn->flags & IPN_FLAG_TERMINATED) ||
629 (ipnn->flags & IPN_FLAG_EXCL))
630 goto put_fail;
631 list_add_tail(&ipn_node->nodelist,&ipnn->unconnectqueue);
632 ipnn->refcnt++;
634 if (ipn_node->pbp) {
635 kfree(ipn_node->pbp);
636 ipn_node->pbp=NULL;
638 ipn_node->ipn=ipnn;
639 ipn_node->flags |= IPN_NODEFLAG_BOUND;
640 up(&ipn_glob_mutex);
641 return 0;
643 put_fail:
644 path_release(&nd);
645 out:
646 up(&ipn_glob_mutex);
647 return err;
649 out_mknod_dput_putmodule:
650 module_put(THIS_MODULE);
651 out_mknod_dput_ipnn2:
652 kfree(ipnn->connport);
653 out_mknod_dput_ipnn:
654 kmem_cache_free(ipn_network_cache,ipnn);
655 out_mknod_dput:
656 dput(dentry);
657 out_mknod_unlock:
658 mutex_unlock(&nd.dentry->d_inode->i_mutex);
659 path_release(&nd);
660 out_mknod_parent:
661 if (err==-EEXIST)
662 err=-EADDRINUSE;
663 up(&ipn_glob_mutex);
664 return err;
667 /* IPN CONNECT */
668 static int ipn_connect(struct socket *sock, struct sockaddr *addr,
669 int addr_len, int flags){
670 struct sockaddr_un *sunaddr=(struct sockaddr_un*)addr;
671 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
672 struct nameidata nd;
673 struct ipn_network *ipnn,*previousipnn;
674 int err=0;
675 int portno;
677 /* the socket cannot be connected twice */
678 if (sock->state != SS_UNCONNECTED)
679 return EISCONN;
681 if (down_interruptible(&ipn_glob_mutex))
682 return -ERESTARTSYS;
684 if ((previousipnn=ipn_node->ipn) == NULL) { /* unbound */
685 unsigned char mustshutdown=0;
686 err = ipn_mkname(sunaddr, addr_len);
687 if (err < 0)
688 goto out;
689 addr_len=err;
690 err = path_lookup(sunaddr->sun_path, LOOKUP_FOLLOW, &nd);
691 if (err)
692 goto out;
693 err = vfs_permission(&nd, MAY_READ);
694 if (err) {
695 if (err == -EACCES || err == -EROFS)
696 mustshutdown|=RCV_SHUTDOWN;
697 else
698 goto put_fail;
700 err = vfs_permission(&nd, MAY_WRITE);
701 if (err) {
702 if (err == -EACCES)
703 mustshutdown|=SEND_SHUTDOWN;
704 else
705 goto put_fail;
707 mustshutdown |= ipn_node->shutdown;
708 /* if the combination of shutdown and permissions leaves
709 * no abilities, connect returns EACCES */
710 if (mustshutdown == SHUTDOWN_XMASK) {
711 err=-EACCES;
712 goto put_fail;
713 } else {
714 err=0;
715 ipn_node->shutdown=mustshutdown;
717 if (!S_ISSOCK(nd.dentry->d_inode->i_mode)) {
718 err = -ECONNREFUSED;
719 goto put_fail;
721 ipnn=ipn_find_network_byinode(nd.dentry->d_inode);
722 if (!ipnn || (ipnn->flags & IPN_FLAG_TERMINATED)) {
723 err = -ECONNREFUSED;
724 goto put_fail;
726 if (ipn_node->protocol == IPN_ANY)
727 ipn_node->protocol=ipnn->protocol;
728 else if (ipnn->protocol != ipn_node->protocol) {
729 err = -EPROTO;
730 goto put_fail;
732 path_release(&nd);
733 ipn_node->ipn=ipnn;
734 } else
735 ipnn=ipn_node->ipn;
737 if (down_interruptible(&ipnn->ipnn_mutex)) {
738 err=-ERESTARTSYS;
739 goto out;
741 portno = ipn_protocol_table[ipnn->protocol]->ipn_p_newport(ipn_node);
742 if (portno >= 0 && portno<ipnn->maxports) {
743 sock->state = SS_CONNECTED;
744 ipn_node->portno=portno;
745 ipnn->connport[portno]=ipn_node;
746 if (!(ipn_node->flags & IPN_NODEFLAG_BOUND)) {
747 ipnn->refcnt++;
748 list_del(&ipn_node->nodelist);
750 list_add_tail(&ipn_node->nodelist,&ipnn->connectqueue);
751 ipn_net_update_counters(ipnn,
752 (ipn_node->shutdown & RCV_SHUTDOWN)?0:1,
753 (ipn_node->shutdown & SEND_SHUTDOWN)?0:1);
754 } else {
755 ipn_node->ipn=previousipnn; /* undo changes on ipn_node->ipn */
756 err=-EADDRNOTAVAIL;
758 up(&ipnn->ipnn_mutex);
759 up(&ipn_glob_mutex);
760 return err;
762 put_fail:
763 path_release(&nd);
764 out:
765 up(&ipn_glob_mutex);
766 return err;
769 static int ipn_getname(struct socket *sock, struct sockaddr *uaddr,
770 int *uaddr_len, int peer) {
771 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
772 struct ipn_network *ipnn=ipn_node->ipn;
773 struct sockaddr_un *sunaddr=(struct sockaddr_un *)uaddr;
774 int err=0;
776 if (down_interruptible(&ipn_glob_mutex))
777 return -ERESTARTSYS;
778 if (ipnn) {
779 *uaddr_len = ipnn->sunaddr_len;
780 memcpy(sunaddr,&ipnn->sunaddr,*uaddr_len);
781 } else
782 err = -ENOTCONN;
783 up(&ipn_glob_mutex);
784 return err;
787 /* IPN POLL */
788 static unsigned int ipn_poll(struct file *file, struct socket *sock,
789 poll_table *wait) {
790 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
791 struct ipn_network *ipnn=ipn_node->ipn;
792 unsigned int mask=0;
794 if (ipnn) {
795 poll_wait(file,&ipn_node->read_wait,wait);
796 if (ipnn->flags & IPN_FLAG_LOSSLESS)
797 poll_wait(file,&ipnn->send_wait,wait);
798 /* POLLIN if recv succeeds,
799 * POLL{PRI,RDNORM} if there are {oob,non-oob} messages */
800 if (ipn_node->totmsgcount > 0) mask |= POLLIN;
801 if (!(list_empty(&ipn_node->msgqueue))) mask |= POLLRDNORM;
802 if (!(list_empty(&ipn_node->oobmsgqueue))) mask |= POLLPRI;
803 if ((!(ipnn->flags & IPN_FLAG_LOSSLESS)) |
804 (atomic_read(&ipnn->msgpool_nelem) < ipnn->msgpool_size))
805 mask |= POLLOUT | POLLWRNORM;
807 return mask;
810 /* connect netdev (from ioctl). connect a bound socket to a
811 * network device TAP or GRAB */
812 static int ipn_connect_netdev(struct socket *sock,struct ifreq *ifr)
814 int err=0;
815 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
816 struct ipn_network *ipnn=ipn_node->ipn;
817 if (!capable(CAP_NET_ADMIN))
818 return -EPERM;
819 if (sock->state != SS_UNCONNECTED)
820 return -EISCONN;
821 if (!ipnn)
822 return -ENOTCONN; /* Maybe we need a different error for "NOT BOUND" */
823 if (down_interruptible(&ipn_glob_mutex))
824 return -ERESTARTSYS;
825 if (down_interruptible(&ipnn->ipnn_mutex)) {
826 up(&ipn_glob_mutex);
827 return -ERESTARTSYS;
829 ipn_node->dev=ipn_netdev_alloc(ipn_node->net,ifr->ifr_flags,ifr->ifr_name,&err);
830 if (ipn_node->dev) {
831 int portno;
832 portno = ipn_protocol_table[ipnn->protocol]->ipn_p_newport(ipn_node);
833 if (portno >= 0 && portno<ipnn->maxports) {
834 sock->state = SS_CONNECTED;
835 ipn_node->portno=portno;
836 ipn_node->flags |= ifr->ifr_flags & IPN_NODEFLAG_DEVMASK;
837 ipnn->connport[portno]=ipn_node;
838 err=ipn_netdev_activate(ipn_node);
839 if (err) {
840 sock->state = SS_UNCONNECTED;
841 ipn_protocol_table[ipnn->protocol]->ipn_p_delport(ipn_node);
842 ipn_node->dev=NULL;
843 ipn_node->portno= -1;
844 ipn_node->flags &= ~IPN_NODEFLAG_DEVMASK;
845 ipnn->connport[portno]=NULL;
846 } else {
847 ipn_protocol_table[ipnn->protocol]->ipn_p_postnewport(ipn_node);
848 list_del(&ipn_node->nodelist);
849 list_add_tail(&ipn_node->nodelist,&ipnn->connectqueue);
851 } else {
852 ipn_netdev_close(ipn_node);
853 err=-EADDRNOTAVAIL;
854 ipn_node->dev=NULL;
856 } else
857 err=-EINVAL;
858 up(&ipnn->ipnn_mutex);
859 up(&ipn_glob_mutex);
860 return err;
863 /* join a netdev, a socket gets connected to a persistent node
864 * not connected to another socket */
865 static int ipn_join_netdev(struct socket *sock,struct ifreq *ifr)
867 int err=0;
868 struct net_device *dev;
869 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
870 struct ipn_node *ipn_joined;
871 struct ipn_network *ipnn=ipn_node->ipn;
872 if (sock->state != SS_UNCONNECTED)
873 return -EISCONN;
874 if (down_interruptible(&ipn_glob_mutex))
875 return -ERESTARTSYS;
876 if (down_interruptible(&ipnn->ipnn_mutex)) {
877 up(&ipn_glob_mutex);
878 return -ERESTARTSYS;
880 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
881 dev=__dev_get_by_name(ifr->ifr_name);
882 #else
883 dev=__dev_get_by_name(ipn_node->net,ifr->ifr_name);
884 #endif
885 if (!dev)
886 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
887 dev=__dev_get_by_index(ifr->ifr_ifindex);
888 #else
889 dev=__dev_get_by_index(ipn_node->net,ifr->ifr_ifindex);
890 #endif
891 if (dev && (ipn_joined=ipn_netdev2node(dev)) != NULL) { /* the interface does exist */
892 int i;
893 for (i=0;i<ipnn->maxports && ipn_joined != ipnn->connport[i] ;i++)
895 if (i < ipnn->maxports) { /* found */
896 /* ipn_joined is substituted to ipn_node */
897 ((struct ipn_sock *)sock->sk)->node=ipn_joined;
898 ipn_joined->flags |= IPN_NODEFLAG_INUSE;
899 ipnn->refcnt--;
900 kmem_cache_free(ipn_node_cache,ipn_node);
901 } else
902 err=-EPERM;
903 } else
904 err=-EADDRNOTAVAIL;
905 up(&ipnn->ipnn_mutex);
906 up(&ipn_glob_mutex);
907 return err;
910 /* set persistence of a node looking for it by interface name
911 * (it is for sysadm, to close network interfaces)*/
912 static int ipn_setpersist_netdev(struct ifreq *ifr, int value)
914 struct net_device *dev;
915 struct ipn_node *ipn_node;
916 int err=0;
917 if (!capable(CAP_NET_ADMIN))
918 return -EPERM;
919 if (down_interruptible(&ipn_glob_mutex))
920 return -ERESTARTSYS;
921 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
922 dev=__dev_get_by_name(ifr->ifr_name);
923 #else
924 dev=__dev_get_by_name(&init_net,ifr->ifr_name);
925 #endif
926 if (!dev)
927 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
928 dev=__dev_get_by_index(ifr->ifr_ifindex);
929 #else
930 dev=__dev_get_by_index(&init_net,ifr->ifr_ifindex);
931 #endif
932 if (dev && (ipn_node=ipn_netdev2node(dev)) != NULL)
933 _ipn_setpersist(ipn_node,value);
934 else
935 err=-EADDRNOTAVAIL;
936 up(&ipn_glob_mutex);
937 return err;
940 /* IPN IOCTL */
941 static int ipn_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) {
942 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
943 struct ipn_network *ipnn=ipn_node->ipn;
944 void __user* argp = (void __user*)arg;
945 struct ifreq ifr;
947 if (ipn_node->shutdown == SHUTDOWN_XMASK)
948 return -ECONNRESET;
950 /* get arguments */
951 switch (cmd) {
952 case IPN_CHECK:
953 return IPN_CHECK;
954 case IPN_SETPERSIST_NETDEV:
955 case IPN_CLRPERSIST_NETDEV:
956 case IPN_CONN_NETDEV:
957 case IPN_JOIN_NETDEV:
958 case SIOCSIFHWADDR:
959 if (copy_from_user(&ifr, argp, sizeof ifr))
960 return -EFAULT;
961 ifr.ifr_name[IFNAMSIZ-1] = '\0';
964 /* actions for unconnected and unbound sockets */
965 switch (cmd) {
966 case IPN_SETPERSIST_NETDEV:
967 return ipn_setpersist_netdev(&ifr,1);
968 case IPN_CLRPERSIST_NETDEV:
969 return ipn_setpersist_netdev(&ifr,0);
970 case SIOCSIFHWADDR:
971 if (capable(CAP_NET_ADMIN))
972 return -EPERM;
973 if (ipn_node->dev && (ipn_node->flags &IPN_NODEFLAG_TAP))
974 return dev_set_mac_address(ipn_node->dev, &ifr.ifr_hwaddr);
975 else
976 return -EADDRNOTAVAIL;
978 if (ipnn == NULL || (ipnn->flags & IPN_FLAG_TERMINATED))
979 return -ENOTCONN;
980 /* actions for connected or bound sockets */
981 switch (cmd) {
982 case IPN_CONN_NETDEV:
983 return ipn_connect_netdev(sock,&ifr);
984 case IPN_JOIN_NETDEV:
985 return ipn_join_netdev(sock,&ifr);
986 case IPN_SETPERSIST:
987 return ipn_setpersist(ipn_node,arg);
988 default:
989 if (ipnn) {
990 int rv;
991 if (down_interruptible(&ipnn->ipnn_mutex))
992 return -ERESTARTSYS;
993 rv=ipn_protocol_table[ipn_node->protocol]->ipn_p_ioctl(ipn_node,cmd,arg);
994 up(&ipnn->ipnn_mutex);
995 return rv;
996 } else
997 return -EOPNOTSUPP;
1001 /* shutdown: close socket for input or for output.
1002 * shutdown can be called prior to connect and it is not reversible */
1003 static int ipn_shutdown(struct socket *sock, int mode) {
1004 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
1005 struct ipn_network *ipnn=ipn_node->ipn;
1006 int oldshutdown=ipn_node->shutdown;
1007 mode = (mode+1)&(RCV_SHUTDOWN|SEND_SHUTDOWN);
1009 ipn_node->shutdown |= mode;
1011 if(ipnn) {
1012 if (down_interruptible(&ipnn->ipnn_mutex)) {
1013 ipn_node->shutdown = oldshutdown;
1014 return -ERESTARTSYS;
1016 oldshutdown=ipn_node->shutdown-oldshutdown;
1017 if (sock->state == SS_CONNECTED && oldshutdown) {
1018 ipn_net_update_counters(ipnn,
1019 (ipn_node->shutdown & RCV_SHUTDOWN)?0:-1,
1020 (ipn_node->shutdown & SEND_SHUTDOWN)?0:-1);
1023 /* if recv channel has been shut down, flush the recv queue */
1024 if ((ipn_node->shutdown & RCV_SHUTDOWN))
1025 ipn_flush_recvqueue(ipn_node);
1026 up(&ipnn->ipnn_mutex);
1028 return 0;
1031 /* injectmsg: a new message is entering the ipn network.
1032 * injectmsg gets called by send and by the grab/tap node */
1033 int ipn_proto_injectmsg(struct ipn_node *from, struct msgpool_item *msg)
1035 struct ipn_network *ipnn=from->ipn;
1036 int err=0;
1037 if (down_interruptible(&ipnn->ipnn_mutex))
1038 err=-ERESTARTSYS;
1039 else {
1040 ipn_protocol_table[ipnn->protocol]->ipn_p_handlemsg(from, msg);
1041 up(&ipnn->ipnn_mutex);
1043 return err;
1046 /* SEND MSG */
1047 static int ipn_sendmsg(struct kiocb *kiocb, struct socket *sock,
1048 struct msghdr *msg, size_t len) {
1049 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
1050 struct ipn_network *ipnn=ipn_node->ipn;
1051 struct msgpool_item *newmsg;
1052 int err=0;
1054 if (unlikely(sock->state != SS_CONNECTED))
1055 return -ENOTCONN;
1056 if (unlikely(ipn_node->shutdown & SEND_SHUTDOWN)) {
1057 if (ipn_node->shutdown == SHUTDOWN_XMASK)
1058 return -ECONNRESET;
1059 else
1060 return -EPIPE;
1062 if (len > ipnn->mtu)
1063 return -EOVERFLOW;
1064 newmsg=ipn_msgpool_alloc_locking(ipnn);
1065 if (!newmsg)
1066 return -ENOMEM;
1067 newmsg->len=len;
1068 err=memcpy_fromiovec(newmsg->data, msg->msg_iov, len);
1069 if (!err)
1070 ipn_proto_injectmsg(ipn_node, newmsg);
1071 ipn_msgpool_put(newmsg,ipnn);
1072 return err;
1075 /* enqueue an oob message. "to" is the destination */
1076 void ipn_proto_oobsendmsg(struct ipn_node *to, struct msgpool_item *msg)
1078 if (to) {
1079 if (!to->dev) { /* no oob to netdev */
1080 struct msgitem *msgitem;
1081 struct ipn_network *ipnn=to->ipn;
1082 spin_lock(&to->msglock);
1083 if ((to->shutdown & RCV_SHUTDOWN_NO_OOB) == 0 &&
1084 (ipnn->flags & IPN_FLAG_LOSSLESS ||
1085 to->oobmsgcount < ipnn->msgpool_size)) {
1086 if ((msgitem=kmem_cache_alloc(ipn_msgitem_cache,GFP_KERNEL))!=NULL) {
1087 msgitem->msg=msg;
1088 to->totmsgcount++;
1089 to->oobmsgcount++;
1090 list_add_tail(&msgitem->list, &to->oobmsgqueue);
1091 ipn_msgpool_hold(msg);
1094 spin_unlock(&to->msglock);
1095 wake_up_interruptible(&to->read_wait);
1100 /* ipn_proto_sendmsg is called by protocol implementation to enqueue a
1101 * for a destination (to).*/
1102 void ipn_proto_sendmsg(struct ipn_node *to, struct msgpool_item *msg)
1104 if (to) {
1105 if (to->dev) {
1106 ipn_netdev_sendmsg(to,msg);
1107 } else {
1108 /* socket send */
1109 struct msgitem *msgitem;
1110 struct ipn_network *ipnn=to->ipn;
1111 spin_lock(&to->msglock);
1112 if (likely((to->shutdown & RCV_SHUTDOWN)==0)) {
1113 /*if (unlikely((ipnn->flags & IPN_FLAG_LOSSLESS) == 0 ||
1114 to->totmsgcount >= ipnn->msgpool_size))
1115 yield();*/
1116 if (likely(ipnn->flags & IPN_FLAG_LOSSLESS ||
1117 to->totmsgcount < ipnn->msgpool_size)) {
1118 if ((msgitem=kmem_cache_alloc(ipn_msgitem_cache,GFP_KERNEL))!=NULL) {
1119 msgitem->msg=msg;
1120 to->totmsgcount++;
1121 list_add_tail(&msgitem->list, &to->msgqueue);
1122 ipn_msgpool_hold(msg);
1126 spin_unlock(&to->msglock);
1127 wake_up_interruptible(&to->read_wait);
1132 /* IPN RECV */
1133 static int ipn_recvmsg(struct kiocb *kiocb, struct socket *sock,
1134 struct msghdr *msg, size_t len, int flags) {
1135 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
1136 struct ipn_network *ipnn=ipn_node->ipn;
1137 struct msgitem *msgitem;
1138 struct msgpool_item *currmsg;
1140 if (unlikely(sock->state != SS_CONNECTED))
1141 return -ENOTCONN;
1143 if (unlikely((ipn_node->shutdown & XRCV_SHUTDOWN) == XRCV_SHUTDOWN)) {
1144 if (ipn_node->shutdown == SHUTDOWN_XMASK) /*EOF, nothing can be read*/
1145 return 0;
1146 else
1147 return -EPIPE; /*trying to read on a write only node */
1150 /* wait for a message */
1151 spin_lock(&ipn_node->msglock);
1152 while (ipn_node->totmsgcount == 0) {
1153 spin_unlock(&ipn_node->msglock);
1154 if (wait_event_interruptible(ipn_node->read_wait,
1155 !(ipn_node->totmsgcount == 0)))
1156 return -ERESTARTSYS;
1157 spin_lock(&ipn_node->msglock);
1159 /* oob gets delivered first. oob are rare */
1160 if (likely(list_empty(&ipn_node->oobmsgqueue)))
1161 msgitem=list_first_entry(&ipn_node->msgqueue, struct msgitem, list);
1162 else {
1163 msgitem=list_first_entry(&ipn_node->oobmsgqueue, struct msgitem, list);
1164 msg->msg_flags |= MSG_OOB;
1165 ipn_node->oobmsgcount--;
1167 list_del(&msgitem->list);
1168 ipn_node->totmsgcount--;
1169 spin_unlock(&ipn_node->msglock);
1170 currmsg=msgitem->msg;
1171 if (currmsg->len < len)
1172 len=currmsg->len;
1173 memcpy_toiovec(msg->msg_iov, currmsg->data, len);
1174 ipn_msgpool_put(currmsg,ipnn);
1175 kmem_cache_free(ipn_msgitem_cache,msgitem);
1177 return len;
1180 /* resize a network: change the # of communication ports (connport) */
1181 static int ipn_netresize(struct ipn_network *ipnn,int newsize)
1183 int oldsize,min;
1184 struct ipn_node **newconnport;
1185 struct ipn_node **oldconnport;
1186 int err;
1187 if (down_interruptible(&ipnn->ipnn_mutex))
1188 return -ERESTARTSYS;
1189 oldsize=ipnn->maxports;
1190 if (newsize == oldsize) {
1191 up(&ipnn->ipnn_mutex);
1192 return 0;
1194 min=oldsize;
1195 /* shrink a network. all the ports we are going to eliminate
1196 * must be unused! */
1197 if (newsize < oldsize) {
1198 int i;
1199 for (i=newsize; i<oldsize; i++)
1200 if (ipnn->connport[i]) {
1201 up(&ipnn->ipnn_mutex);
1202 return -EADDRINUSE;
1204 min=newsize;
1206 oldconnport=ipnn->connport;
1207 /* allocate the new connport array and copy the old one */
1208 newconnport=kzalloc(newsize * sizeof(struct ipn_node *),GFP_KERNEL);
1209 if (!newconnport) {
1210 up(&ipnn->ipnn_mutex);
1211 return -ENOMEM;
1213 memcpy(newconnport,oldconnport,min * sizeof(struct ipn_node *));
1214 ipnn->connport=newconnport;
1215 ipnn->maxports=newsize;
1216 /* notify the protocol that the netowrk has been resized */
1217 err=ipn_protocol_table[ipnn->protocol]->ipn_p_resizenet(ipnn,oldsize,newsize);
1218 if (err) {
1219 /* roll back if the resize operation failed for the protocol */
1220 ipnn->connport=oldconnport;
1221 ipnn->maxports=oldsize;
1222 kfree(newconnport);
1223 } else
1224 /* successful mission, network resized */
1225 kfree(oldconnport);
1226 up(&ipnn->ipnn_mutex);
1227 return err;
1230 /* IPN SETSOCKOPT */
1231 static int ipn_setsockopt(struct socket *sock, int level, int optname,
1232 char __user *optval, int optlen) {
1233 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
1234 struct ipn_network *ipnn=ipn_node->ipn;
1236 if (ipn_node->shutdown == SHUTDOWN_XMASK)
1237 return -ECONNRESET;
1238 if (level != 0 && level != ipn_node->protocol+1)
1239 return -EPROTONOSUPPORT;
1240 if (level > 0) {
1241 /* protocol specific sockopt */
1242 if (ipnn) {
1243 int rv;
1244 if (down_interruptible(&ipnn->ipnn_mutex))
1245 return -ERESTARTSYS;
1246 rv=ipn_protocol_table[ipn_node->protocol]->ipn_p_setsockopt(ipn_node,optname,optval,optlen);
1247 up(&ipnn->ipnn_mutex);
1248 return rv;
1249 } else
1250 return -EOPNOTSUPP;
1251 } else {
1252 if (optname == IPN_SO_DESCR) {
1253 if (optlen > IPN_DESCRLEN)
1254 return -EINVAL;
1255 else {
1256 memset(ipn_node->descr,0,IPN_DESCRLEN);
1257 if (copy_from_user(ipn_node->descr,optval,optlen))
1258 ipn_node->descr[0]=0;
1259 else
1260 ipn_node->descr[optlen-1]=0;
1261 return 0;
1263 } else {
1264 if (optlen < sizeof(int))
1265 return -EINVAL;
1266 else if ((optname & IPN_SO_PREBIND) && (ipnn != NULL))
1267 return -EISCONN;
1268 else {
1269 int val;
1270 get_user(val, (int __user *) optval);
1271 if ((optname & IPN_SO_PREBIND) && !ipn_node->pbp) {
1272 struct pre_bind_parms std=STD_BIND_PARMS;
1273 ipn_node->pbp=kzalloc(sizeof(struct pre_bind_parms),GFP_KERNEL);
1274 if (!ipn_node->pbp)
1275 return -ENOMEM;
1276 *(ipn_node->pbp)=std;
1278 switch (optname) {
1279 case IPN_SO_PORT:
1280 if (sock->state == SS_UNCONNECTED)
1281 ipn_node->portno=val;
1282 else
1283 return -EISCONN;
1284 break;
1285 case IPN_SO_CHANGE_NUMNODES:
1286 if ((ipn_node->flags & IPN_NODEFLAG_BOUND)!=0) {
1287 if (val <= 0)
1288 return -EINVAL;
1289 else
1290 return ipn_netresize(ipnn,val);
1291 } else
1292 val=-ENOTCONN;
1293 break;
1294 case IPN_SO_WANT_OOB_NUMNODES:
1295 if (val)
1296 ipn_node->flags |= IPN_NODEFLAG_OOB_NUMNODES;
1297 else
1298 ipn_node->flags &= ~IPN_NODEFLAG_OOB_NUMNODES;
1299 break;
1300 case IPN_SO_HANDLE_OOB:
1301 if (val)
1302 ipn_node->shutdown &= ~RCV_SHUTDOWN_NO_OOB;
1303 else
1304 ipn_node->shutdown |= RCV_SHUTDOWN_NO_OOB;
1305 break;
1306 case IPN_SO_MTU:
1307 if (val <= 0)
1308 return -EINVAL;
1309 else
1310 ipn_node->pbp->mtu=val;
1311 break;
1312 case IPN_SO_NUMNODES:
1313 if (val <= 0)
1314 return -EINVAL;
1315 else
1316 ipn_node->pbp->maxports=val;
1317 break;
1318 case IPN_SO_MSGPOOLSIZE:
1319 if (val <= 0)
1320 return -EINVAL;
1321 else
1322 ipn_node->pbp->msgpoolsize=val;
1323 break;
1324 case IPN_SO_FLAGS:
1325 ipn_node->pbp->flags=val;
1326 break;
1327 case IPN_SO_MODE:
1328 ipn_node->pbp->mode=val;
1329 break;
1331 return 0;
1337 /* IPN GETSOCKOPT */
1338 static int ipn_getsockopt(struct socket *sock, int level, int optname,
1339 char __user *optval, int __user *optlen) {
1340 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
1341 struct ipn_network *ipnn=ipn_node->ipn;
1342 int len;
1344 if (ipn_node->shutdown == SHUTDOWN_XMASK)
1345 return -ECONNRESET;
1346 if (level != 0 && level != ipn_node->protocol+1)
1347 return -EPROTONOSUPPORT;
1348 if (level > 0) {
1349 if (ipnn) {
1350 int rv;
1351 /* protocol specific sockopt */
1352 if (down_interruptible(&ipnn->ipnn_mutex))
1353 return -ERESTARTSYS;
1354 rv=ipn_protocol_table[ipn_node->protocol]->ipn_p_getsockopt(ipn_node,optname,optval,optlen);
1355 up(&ipnn->ipnn_mutex);
1356 return rv;
1357 } else
1358 return -EOPNOTSUPP;
1359 } else {
1360 if (get_user(len, optlen))
1361 return -EFAULT;
1362 if (optname == IPN_SO_DESCR) {
1363 if (len < IPN_DESCRLEN)
1364 return -EINVAL;
1365 else {
1366 if (len > IPN_DESCRLEN)
1367 len=IPN_DESCRLEN;
1368 if(put_user(len, optlen))
1369 return -EFAULT;
1370 if(copy_to_user(optval,ipn_node->descr,len))
1371 return -EFAULT;
1372 return 0;
1374 } else {
1375 int val=-2;
1376 switch (optname) {
1377 case IPN_SO_PORT:
1378 val=ipn_node->portno;
1379 break;
1380 case IPN_SO_MTU:
1381 if (ipnn)
1382 val=ipnn->mtu;
1383 else if (ipn_node->pbp)
1384 val=ipn_node->pbp->mtu;
1385 break;
1386 case IPN_SO_NUMNODES:
1387 if (ipnn)
1388 val=ipnn->maxports;
1389 else if (ipn_node->pbp)
1390 val=ipn_node->pbp->maxports;
1391 break;
1392 case IPN_SO_MSGPOOLSIZE:
1393 if (ipnn)
1394 val=ipnn->msgpool_size;
1395 else if (ipn_node->pbp)
1396 val=ipn_node->pbp->msgpoolsize;
1397 break;
1398 case IPN_SO_FLAGS:
1399 if (ipnn)
1400 val=ipnn->flags;
1401 else if (ipn_node->pbp)
1402 val=ipn_node->pbp->flags;
1403 break;
1404 case IPN_SO_MODE:
1405 if (ipnn)
1406 val=-1;
1407 else if (ipn_node->pbp)
1408 val=ipn_node->pbp->mode;
1409 break;
1411 if (val < -1)
1412 return -EINVAL;
1413 else {
1414 if (len < sizeof(int))
1415 return -EOVERFLOW;
1416 else {
1417 len = sizeof(int);
1418 if(put_user(len, optlen))
1419 return -EFAULT;
1420 if(copy_to_user(optval,&val,len))
1421 return -EFAULT;
1422 return 0;
1429 /* BROADCAST/HUB implementation */
1431 static int ipn_bcast_newport(struct ipn_node *newport) {
1432 struct ipn_network *ipnn=newport->ipn;
1433 int i;
1434 for (i=0;i<ipnn->maxports;i++) {
1435 if (ipnn->connport[i] == NULL)
1436 return i;
1438 return -1;
1441 static int ipn_bcast_handlemsg(struct ipn_node *from,
1442 struct msgpool_item *msgitem){
1443 struct ipn_network *ipnn=from->ipn;
1445 struct ipn_node *ipn_node;
1446 list_for_each_entry(ipn_node, &ipnn->connectqueue, nodelist) {
1447 if (ipn_node != from)
1448 ipn_proto_sendmsg(ipn_node,msgitem);
1450 return 0;
1453 static void ipn_null_delport(struct ipn_node *oldport) {}
1454 static void ipn_null_postnewport(struct ipn_node *newport) {}
1455 static void ipn_null_predelport(struct ipn_node *oldport) {}
1456 static int ipn_null_newnet(struct ipn_network *newnet) {return 0;}
1457 static int ipn_null_resizenet(struct ipn_network *net,int oldsize,int newsize) {
1458 return 0;}
1459 static void ipn_null_delnet(struct ipn_network *oldnet) {}
1460 static int ipn_null_setsockopt(struct ipn_node *port,int optname,
1461 char __user *optval, int optlen) {return -EOPNOTSUPP;}
1462 static int ipn_null_getsockopt(struct ipn_node *port,int optname,
1463 char __user *optval, int *optlen) {return -EOPNOTSUPP;}
1464 static int ipn_null_ioctl(struct ipn_node *port,unsigned int request,
1465 unsigned long arg) {return -EOPNOTSUPP;}
1467 /* Protocol Registration/deregisteration */
1469 void ipn_init_protocol(struct ipn_protocol *p)
1471 if (p->ipn_p_delport == NULL) p->ipn_p_delport=ipn_null_delport;
1472 if (p->ipn_p_postnewport == NULL) p->ipn_p_postnewport=ipn_null_postnewport;
1473 if (p->ipn_p_predelport == NULL) p->ipn_p_predelport=ipn_null_predelport;
1474 if (p->ipn_p_newnet == NULL) p->ipn_p_newnet=ipn_null_newnet;
1475 if (p->ipn_p_resizenet == NULL) p->ipn_p_resizenet=ipn_null_resizenet;
1476 if (p->ipn_p_delnet == NULL) p->ipn_p_delnet=ipn_null_delnet;
1477 if (p->ipn_p_setsockopt == NULL) p->ipn_p_setsockopt=ipn_null_setsockopt;
1478 if (p->ipn_p_getsockopt == NULL) p->ipn_p_getsockopt=ipn_null_getsockopt;
1479 if (p->ipn_p_ioctl == NULL) p->ipn_p_ioctl=ipn_null_ioctl;
1482 int ipn_proto_register(int protocol,struct ipn_protocol *ipn_service)
1484 int rv=0;
1485 if (ipn_service->ipn_p_newport == NULL ||
1486 ipn_service->ipn_p_handlemsg == NULL)
1487 return -EINVAL;
1488 ipn_init_protocol(ipn_service);
1489 if (down_interruptible(&ipn_glob_mutex))
1490 return -ERESTARTSYS;
1491 if (protocol > 1 && protocol <= IPN_MAX_PROTO) {
1492 protocol--;
1493 if (ipn_protocol_table[protocol])
1494 rv= -EEXIST;
1495 else {
1496 ipn_service->refcnt=0;
1497 ipn_protocol_table[protocol]=ipn_service;
1498 printk(KERN_INFO "IPN: Registered protocol %d\n",protocol+1);
1500 } else
1501 rv= -EINVAL;
1502 up(&ipn_glob_mutex);
1503 return rv;
1506 int ipn_proto_deregister(int protocol)
1508 int rv=0;
1509 if (down_interruptible(&ipn_glob_mutex))
1510 return -ERESTARTSYS;
1511 if (protocol > 1 && protocol <= IPN_MAX_PROTO) {
1512 protocol--;
1513 if (ipn_protocol_table[protocol]) {
1514 if (ipn_protocol_table[protocol]->refcnt == 0) {
1515 ipn_protocol_table[protocol]=NULL;
1516 printk(KERN_INFO "IPN: Unregistered protocol %d\n",protocol+1);
1517 } else
1518 rv=-EADDRINUSE;
1519 } else
1520 rv= -ENOENT;
1521 } else
1522 rv= -EINVAL;
1523 up(&ipn_glob_mutex);
1524 return rv;
1527 /* MAIN SECTION */
1528 /* Module constructor/destructor */
1529 static struct net_proto_family ipn_family_ops = {
1530 .family = PF_IPN,
1531 .create = ipn_create,
1532 .owner = THIS_MODULE,
1535 /* IPN constructor */
1536 static int ipn_init(void)
1538 int rc;
1540 ipn_init_protocol(&ipn_bcast);
1541 ipn_network_cache=kmem_cache_create("ipn_network",sizeof(struct ipn_network),0,0,NULL);
1542 if (!ipn_network_cache) {
1543 printk(KERN_CRIT "%s: Cannot create ipn_network SLAB cache!\n",
1544 __FUNCTION__);
1545 rc=-ENOMEM;
1546 goto out;
1549 ipn_node_cache=kmem_cache_create("ipn_node",sizeof(struct ipn_node),0,0,NULL);
1550 if (!ipn_node_cache) {
1551 printk(KERN_CRIT "%s: Cannot create ipn_node SLAB cache!\n",
1552 __FUNCTION__);
1553 rc=-ENOMEM;
1554 goto out_net;
1557 ipn_msgitem_cache=kmem_cache_create("ipn_msgitem",sizeof(struct msgitem),0,0,NULL);
1558 if (!ipn_msgitem_cache) {
1559 printk(KERN_CRIT "%s: Cannot create ipn_msgitem SLAB cache!\n",
1560 __FUNCTION__);
1561 rc=-ENOMEM;
1562 goto out_net_node;
1565 rc=ipn_msgbuf_init();
1566 if (rc != 0) {
1567 printk(KERN_CRIT "%s: Cannot create ipn_msgbuf SLAB cache\n",
1568 __FUNCTION__);
1569 goto out_net_node_msg;
1572 rc=proto_register(&ipn_proto,1);
1573 if (rc != 0) {
1574 printk(KERN_CRIT "%s: Cannot register the protocol!\n",
1575 __FUNCTION__);
1576 goto out_net_node_msg_msgbuf;
1579 sock_register(&ipn_family_ops);
1580 ipn_netdev_init();
1581 printk(KERN_INFO "IPN: Virtual Square Project, University of Bologna 2007\n");
1582 return 0;
1584 out_net_node_msg_msgbuf:
1585 ipn_msgbuf_fini();
1586 out_net_node_msg:
1587 kmem_cache_destroy(ipn_msgitem_cache);
1588 out_net_node:
1589 kmem_cache_destroy(ipn_node_cache);
1590 out_net:
1591 kmem_cache_destroy(ipn_network_cache);
1592 out:
1593 return rc;
1596 /* IPN destructor */
1597 static void ipn_exit(void)
1599 ipn_netdev_fini();
1600 if (ipn_msgitem_cache)
1601 kmem_cache_destroy(ipn_msgitem_cache);
1602 if (ipn_node_cache)
1603 kmem_cache_destroy(ipn_node_cache);
1604 if (ipn_network_cache)
1605 kmem_cache_destroy(ipn_network_cache);
1606 ipn_msgbuf_fini();
1607 sock_unregister(PF_IPN);
1608 proto_unregister(&ipn_proto);
1609 printk(KERN_INFO "IPN removed\n");
1612 module_init(ipn_init);
1613 module_exit(ipn_exit);
1615 EXPORT_SYMBOL_GPL(ipn_proto_register);
1616 EXPORT_SYMBOL_GPL(ipn_proto_deregister);
1617 EXPORT_SYMBOL_GPL(ipn_proto_sendmsg);
1618 EXPORT_SYMBOL_GPL(ipn_proto_oobsendmsg);
1619 EXPORT_SYMBOL_GPL(ipn_msgpool_alloc);
1620 EXPORT_SYMBOL_GPL(ipn_msgpool_put);