port to 2.6.37: mutex changed into semaphore and netdev_rx_handler management
[vde.git] / ipn / af_ipn.c
blob197578d3e287078c130048d68c2701908e367358
1 /*
2 * Main inter process networking (virtual distributed ethernet) module
3 * (part of the View-OS project: wiki.virtualsquare.org)
5 * Copyright (C) 2007 Renzo Davoli (renzo@cs.unibo.it)
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * Due to this file being licensed under the GPL there is controversy over
13 * whether this permits you to write a module that #includes this file
14 * without placing your module under the GPL. Please consult a lawyer for
15 * advice before doing this.
17 * WARNING: THIS CODE IS ALREADY EXPERIMENTAL
21 #include <linux/init.h>
22 #include <linux/module.h>
23 #include <linux/socket.h>
24 #include <linux/namei.h>
25 #include <linux/poll.h>
26 #include <linux/un.h>
27 #include <linux/list.h>
28 #include <linux/mount.h>
29 #include <linux/version.h>
30 #include <net/sock.h>
32 #include <net/af_ipn.h>
34 #include "af_ipn.h"
35 #include "ipn_netdev.h"
36 #include "ipn_msgbuf.h"
37 #include "ipn_chrdev.h"
39 MODULE_LICENSE("GPL");
40 MODULE_AUTHOR("VIEW-OS TEAM");
41 MODULE_DESCRIPTION("IPN Kernel Module");
43 #define IPN_MAX_PROTO 4
44 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25)
45 #define IPN_PRE2625
46 #endif
47 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,32)
48 #define IPN_PRE2632
49 #endif
50 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,37)
51 #define IPN_PRE2637
52 #endif
54 /*extension of RCV_SHUTDOWN defined in include/net/sock.h
55 * when the bit is set recv fails */
56 /* NO_OOB: do not send OOB */
57 #define RCV_SHUTDOWN_NO_OOB 4
58 /* EXTENDED MASK including OOB */
59 #define SHUTDOWN_XMASK (SHUTDOWN_MASK | RCV_SHUTDOWN_NO_OOB)
60 /* if XRCV_SHUTDOWN is all set recv fails */
61 #define XRCV_SHUTDOWN (RCV_SHUTDOWN | RCV_SHUTDOWN_NO_OOB)
63 /* Global MUTEX: this is locked to add/delete/modify networks
64 * this is *not* locked just to send/receive msgs */
65 #ifdef IPN_PRE2637
66 static DECLARE_MUTEX(ipn_glob_mutex);
67 #else
68 static DEFINE_SEMAPHORE(ipn_glob_mutex);
69 #endif
70 /* Network table and hash */
71 struct hlist_head ipn_network_table[IPN_HASH_SIZE + 1];
72 /* slab(s) for fast data structure allocation */
73 static struct kmem_cache *ipn_network_cache;
74 static struct kmem_cache *ipn_node_cache;
75 static struct kmem_cache *ipn_msgitem_cache;
77 /* Protocol 1: HUB/Broadcast default protocol. Function Prototypes */
78 static int ipn_bcast_newport(struct ipn_node *newport);
79 static int ipn_bcast_handlemsg(struct ipn_node *from,
80 struct msgpool_item *msgitem);
82 /* default protocol IPN_BROADCAST (0) */
83 static struct ipn_protocol ipn_bcast = {
84 .refcnt=0,
85 .ipn_p_newport=ipn_bcast_newport,
86 .ipn_p_handlemsg=ipn_bcast_handlemsg};
87 /* Protocol table */
88 static struct ipn_protocol *ipn_protocol_table[IPN_MAX_PROTO]={&ipn_bcast};
90 /* Socket call function prototypes */
91 static int ipn_release(struct socket *);
92 static int ipn_bind(struct socket *, struct sockaddr *, int);
93 static int ipn_connect(struct socket *, struct sockaddr *,
94 int addr_len, int flags);
95 static int ipn_getname(struct socket *, struct sockaddr *, int *, int);
96 static unsigned int ipn_poll(struct file *, struct socket *, poll_table *);
97 static int ipn_ioctl(struct socket *, unsigned int, unsigned long);
98 static int ipn_shutdown(struct socket *, int);
99 static int ipn_sendmsg(struct kiocb *, struct socket *,
100 struct msghdr *, size_t);
101 static int ipn_recvmsg(struct kiocb *, struct socket *,
102 struct msghdr *, size_t, int);
103 #ifndef IPN_PRE2632
104 static int ipn_setsockopt(struct socket *sock, int level, int optname,
105 char __user *optval, unsigned int optlen);
106 #else
107 static int ipn_setsockopt(struct socket *sock, int level, int optname,
108 char __user *optval, int optlen);
109 #endif
110 static int ipn_getsockopt(struct socket *sock, int level, int optname,
111 char __user *optval, int __user *optlen);
113 /* Network table Management
114 * inode->ipn_network hash table
115 * protected by ipn_glob_mutex */
116 static inline void ipn_insert_network(struct hlist_head *list, struct ipn_network *ipnn)
118 hlist_add_head(&ipnn->hnode, list);
121 static inline void ipn_remove_network(struct ipn_network *ipnn)
123 hlist_del(&ipnn->hnode);
126 static struct ipn_network *ipn_find_network_byinode(struct inode *i)
128 struct ipn_network *ipnn;
129 struct hlist_node *node;
131 hlist_for_each_entry(ipnn, node,
132 &ipn_network_table[i->i_ino & (IPN_HASH_SIZE - 1)], hnode) {
133 struct dentry *dentry = ipnn->dentry;
135 if(dentry && dentry->d_inode == i)
136 return ipnn;
138 return NULL;
141 struct ipn_network *ipn_find_network_byfun(
142 int (*fun)(struct ipn_network *,void *),void *funarg)
144 struct ipn_network *ipnn;
145 struct hlist_node *node;
146 int ipn_table_scan;
148 for (ipn_table_scan=0;ipn_table_scan<IPN_HASH_SIZE;ipn_table_scan++) {
149 hlist_for_each_entry(ipnn, node, &ipn_network_table[ipn_table_scan], hnode) {
150 if(fun(ipnn,funarg))
151 return ipnn;
154 return NULL;
157 /* msgpool management
158 * msgpool_item are ipn_network dependent (each net has its own MTU)
159 * for each message sent there is one msgpool_item and many struct msgitem
160 * one for each receipient.
161 * msgitem are connected to the node's msgqueue or oobmsgqueue.
162 * when a message is delivered to a process the msgitem is deleted and
163 * the count of the msgpool_item is decreased.
164 * msgpool_item elements gets deleted automatically when count is 0*/
166 struct msgitem {
167 struct list_head list;
168 struct msgpool_item *msg;
171 /* alloc a fresh msgpool item. count is set to 1.
172 * the typical use is
173 * ipn_msgpool_alloc
174 * for each receipient
175 * enqueue messages to the process (using msgitem), ipn_msgpool_hold
176 * ipn_msgpool_put
177 * The message can be delivered concurrently. init count to 1 guarantees
178 * that it survives at least until is has been enqueued to all
179 * receivers */
180 /* msgpool_cache == NULL when the ipn network is using the flexible allocation,
181 * i.e. the msgpool item gets allocated by kmalloc instead of using slab */
182 static inline struct msgpool_item *_ipn_msgpool_alloc(struct ipn_network *ipnn, int len)
184 struct msgpool_item *new;
185 if ((new=ipnn->msgpool_cache?
186 kmem_cache_alloc(ipnn->msgpool_cache,GFP_KERNEL):
187 kmalloc(sizeof(struct msgpool_item)+len,GFP_KERNEL)) != NULL) {
188 atomic_set(&new->count,1);
189 atomic_inc(&ipnn->msgpool_nelem);
191 return new;
194 /* ipn_msgpool_alloc returns null when leaky, lossless and no space is left
195 * for new msgpool items. When !leaky, it can allocate more msgpooli items,
196 * this temporary overassignment blocks new messages to be delivered until
197 * there is space for a msgpool item to be allocated */
198 struct msgpool_item *ipn_msgpool_alloc(struct ipn_network *ipnn,int leaky,int len)
200 if (leaky && (ipnn->flags & IPN_FLAG_LOSSLESS) &&
201 atomic_read(&ipnn->msgpool_nelem) < ipnn->msgpool_size)
202 return NULL;
203 else
204 return _ipn_msgpool_alloc(ipnn,len);
207 /* If the service il LOSSLESS, this msgpool call waits for an
208 * available msgpool item */
209 static inline struct msgpool_item *ipn_msgpool_alloc_locking(struct ipn_network *ipnn,int len)
211 if (ipnn->flags & IPN_FLAG_LOSSLESS) {
212 while (atomic_read(&ipnn->msgpool_nelem) >= ipnn->msgpool_size) {
213 if (wait_event_interruptible_exclusive(ipnn->send_wait,
214 atomic_read(&ipnn->msgpool_nelem) < ipnn->msgpool_size))
215 return NULL;
218 return _ipn_msgpool_alloc(ipnn,len);
221 /* register one more user for this msgpool item (i.e. count++) */
222 static inline void ipn_msgpool_hold(struct msgpool_item *msg)
224 atomic_inc(&msg->count);
227 /* decrease count and delete msgpool_item if count == 0 */
228 void ipn_msgpool_put(struct msgpool_item *old,
229 struct ipn_network *ipnn)
231 if (atomic_dec_and_test(&old->count)) {
232 if (ipnn->msgpool_cache)
233 kmem_cache_free(ipnn->msgpool_cache,old);
234 else
235 kfree(old);
236 atomic_dec(&ipnn->msgpool_nelem);
237 if (ipnn->flags & IPN_FLAG_LOSSLESS) /* this could be done anyway */
238 wake_up_interruptible(&ipnn->send_wait);
242 /* socket calls */
243 static const struct proto_ops ipn_ops = {
244 .family = PF_IPN,
245 .owner = THIS_MODULE,
246 .release = ipn_release,
247 .bind = ipn_bind,
248 .connect = ipn_connect,
249 .socketpair = sock_no_socketpair,
250 .accept = sock_no_accept,
251 .getname = ipn_getname,
252 .poll = ipn_poll,
253 .ioctl = ipn_ioctl,
254 .listen = sock_no_listen,
255 .shutdown = ipn_shutdown,
256 .setsockopt = ipn_setsockopt,
257 .getsockopt = ipn_getsockopt,
258 .sendmsg = ipn_sendmsg,
259 .recvmsg = ipn_recvmsg,
260 .mmap = sock_no_mmap,
261 .sendpage = sock_no_sendpage,
264 static struct proto ipn_proto = {
265 .name = "IPN",
266 .owner = THIS_MODULE,
267 .obj_size = sizeof(struct ipn_sock),
270 /* create a new ipn_node */
271 struct ipn_node *ipn_node_create(struct net *net)
273 struct ipn_node *ipn_node;
274 ipn_node=kmem_cache_alloc(ipn_node_cache,GFP_KERNEL);
275 if (ipn_node!=NULL) {
276 INIT_LIST_HEAD(&ipn_node->nodelist);
277 ipn_node->protocol=0; /* BROADCAST, caller must set a proper value */
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->netdev=NULL;
284 ipn_node->chrdev=0;
285 ipn_node->proto_private=NULL;
286 ipn_node->totmsgcount=0;
287 ipn_node->oobmsgcount=0;
288 spin_lock_init(&ipn_node->msglock);
289 INIT_LIST_HEAD(&ipn_node->msgqueue);
290 INIT_LIST_HEAD(&ipn_node->oobmsgqueue);
291 ipn_node->ipn=NULL;
292 init_waitqueue_head(&ipn_node->read_wait);
293 ipn_node->pbp=NULL;
295 return ipn_node;
298 /* create a socket
299 * ipn_node is a separate structure, pointed by ipn_sock -> node
300 * when a node is "persistent", ipn_node survives while ipn_sock gets released*/
301 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
302 static int ipn_create(struct socket *sock, int protocol)
303 #elif LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 33)
304 static int ipn_create(struct net *net,struct socket *sock, int protocol)
305 #else
306 static int ipn_create(struct net *net,struct socket *sock,
307 int protocol, int kern)
308 #endif
310 struct ipn_sock *ipn_sk;
312 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
313 void *net=NULL;
314 #else
315 if (net != &init_net)
316 return -EAFNOSUPPORT;
317 #endif
319 if (sock->type != SOCK_RAW)
320 return -EPROTOTYPE;
321 if (protocol > 0)
322 protocol=protocol-1;
323 else
324 protocol=IPN_BROADCAST-1;
325 if (protocol < 0 || protocol >= IPN_MAX_PROTO ||
326 ipn_protocol_table[protocol] == NULL)
327 return -EPROTONOSUPPORT;
328 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
329 ipn_sk = (struct ipn_sock *) sk_alloc(PF_IPN, GFP_KERNEL, &ipn_proto, 1);
330 #else
331 ipn_sk = (struct ipn_sock *) sk_alloc(net, PF_IPN, GFP_KERNEL, &ipn_proto);
332 #endif
334 if (!ipn_sk)
335 return -ENOMEM;
336 if ((ipn_sk->node=ipn_node_create(net))==NULL) {
337 sock_put((struct sock *) ipn_sk);
338 return -ENOMEM;
340 ipn_sk->node->protocol=protocol;
341 sock_init_data(sock,(struct sock *) ipn_sk);
342 sock->state = SS_UNCONNECTED;
343 sock->ops = &ipn_ops;
344 sock->sk=(struct sock *)ipn_sk;
345 return 0;
348 /* update # of readers and # of writers counters for an ipn network.
349 * This function sends oob messages to nodes requesting the service */
350 /* LOCKING ipnn_mutex is locked */
351 static void ipn_net_update_counters(struct ipn_network *ipnn,
352 int chg_readers, int chg_writers) {
353 ipnn->numreaders += chg_readers;
354 ipnn->numwriters += chg_writers;
355 if (ipnn->mtu >= sizeof(struct numnode_oob))
357 struct msgpool_item *ipn_msg=_ipn_msgpool_alloc(ipnn,sizeof(struct numnode_oob));
358 if (ipn_msg) {
359 struct numnode_oob *oob_msg=(struct numnode_oob *)(ipn_msg->data);
360 struct ipn_node *ipn_node;
361 ipn_msg->len=sizeof(struct numnode_oob);
362 oob_msg->level=IPN_ANY;
363 oob_msg->tag=IPN_OOB_NUMNODE_TAG;
364 oob_msg->numreaders=ipnn->numreaders;
365 oob_msg->numwriters=ipnn->numwriters;
366 list_for_each_entry(ipn_node, &ipnn->connectqueue, nodelist) {
367 if (ipn_node->flags & IPN_NODEFLAG_OOB_NUMNODES)
368 ipn_proto_oobsendmsg(ipn_node,ipn_msg);
370 ipn_msgpool_put(ipn_msg,ipnn);
375 /* flush pending messages (for close and shutdown RCV) */
376 /* LOCKING: ipnn_mutex is locked */
377 static void ipn_flush_recvqueue(struct ipn_node *ipn_node)
379 struct ipn_network *ipnn=ipn_node->ipn;
380 spin_lock(&ipn_node->msglock);
381 while (!list_empty(&ipn_node->msgqueue)) {
382 struct msgitem *msgitem=
383 list_first_entry(&ipn_node->msgqueue, struct msgitem, list);
384 list_del(&msgitem->list);
385 ipn_node->totmsgcount--;
386 ipn_msgpool_put(msgitem->msg,ipnn);
387 kmem_cache_free(ipn_msgitem_cache,msgitem);
389 spin_unlock(&ipn_node->msglock);
392 /* flush pending oob messages (for socket close) */
393 /* LOCKING: ipnn_mutex is locked */
394 static void ipn_flush_oobrecvqueue(struct ipn_node *ipn_node)
396 struct ipn_network *ipnn=ipn_node->ipn;
397 spin_lock(&ipn_node->msglock);
398 while (!list_empty(&ipn_node->oobmsgqueue)) {
399 struct msgitem *msgitem=
400 list_first_entry(&ipn_node->oobmsgqueue, struct msgitem, list);
401 list_del(&msgitem->list);
402 ipn_node->totmsgcount--;
403 ipn_node->oobmsgcount--;
404 ipn_msgpool_put(msgitem->msg,ipnn);
405 kmem_cache_free(ipn_msgitem_cache,msgitem);
407 spin_unlock(&ipn_node->msglock);
410 /* Terminate node. The node is "logically" terminated. */
411 /* LOCKING: ipn_glob_lock must be locked here */
412 static int ipn_terminate_node(struct ipn_node *ipn_node)
414 struct ipn_network *ipnn=ipn_node->ipn;
415 if (ipnn) {
416 if (down_interruptible(&ipnn->ipnn_mutex))
417 return -ERESTARTSYS;
418 if (ipn_node->portno >= 0) {
419 ipn_protocol_table[ipnn->protocol]->ipn_p_predelport(ipn_node);
420 ipnn->connport[ipn_node->portno]=NULL;
422 list_del(&ipn_node->nodelist);
423 ipn_flush_recvqueue(ipn_node);
424 ipn_flush_oobrecvqueue(ipn_node);
425 if (ipn_node->portno >= 0)
426 ipn_protocol_table[ipnn->protocol]->ipn_p_delport(ipn_node);
427 ipn_node->ipn=NULL;
428 ipn_net_update_counters(ipnn,
429 (ipn_node->shutdown & RCV_SHUTDOWN)?0:-1,
430 (ipn_node->shutdown & SEND_SHUTDOWN)?0:-1);
431 ipn_node->shutdown = SHUTDOWN_XMASK;
432 up(&ipnn->ipnn_mutex);
433 if (ipn_node->netdev)
434 ipn_netdev_close(ipn_node);
435 /* No more network elements */
436 ipnn->refcnt--;
437 if (ipnn->refcnt == 0 && !ipn_is_persistent_chrdev(ipnn))
439 if (ipnn->chrdev)
440 ipn_deregister_chrdev(ipnn);
441 ipn_protocol_table[ipnn->protocol]->ipn_p_delnet(ipnn);
442 ipn_remove_network(ipnn);
443 ipn_protocol_table[ipnn->protocol]->refcnt--;
444 if (ipnn->dentry) {
445 dput(ipnn->dentry);
446 mntput(ipnn->mnt);
448 if (ipnn->msgpool_cache)
449 ipn_msgbuf_put(ipnn->msgpool_cache);
450 if (ipnn->connport)
451 kfree(ipnn->connport);
452 kmem_cache_free(ipn_network_cache, ipnn);
453 module_put(THIS_MODULE);
456 if (ipn_node->pbp) {
457 kfree(ipn_node->pbp);
458 ipn_node->pbp=NULL;
460 return 0;
463 /* release of an ipn_node */
464 int ipn_node_release(struct ipn_node *ipn_node)
466 int rv;
467 if (down_interruptible(&ipn_glob_mutex))
468 return -ERESTARTSYS;
469 if (ipn_node->flags & IPN_NODEFLAG_PERSIST) {
470 ipn_node->flags &= ~IPN_NODEFLAG_INUSE;
471 rv=0;
472 up(&ipn_glob_mutex);
473 } else {
474 rv=ipn_terminate_node(ipn_node);
475 up(&ipn_glob_mutex);
476 if (rv==0) {
477 ipn_netdevsync();
478 kmem_cache_free(ipn_node_cache,ipn_node);
481 return rv;
484 /* release of an ipn socket */
485 static int ipn_release (struct socket *sock)
487 struct ipn_sock *ipn_sk=(struct ipn_sock *)sock->sk;
488 struct ipn_node *ipn_node=ipn_sk->node;
489 int rv=ipn_node_release(ipn_node);
490 if (rv == 0)
491 sock_put((struct sock *) ipn_sk);
492 return rv;
495 /* _set persist, change the persistence of a node,
496 * when persistence gets cleared and the node is no longer used
497 * the node is terminated and freed.
498 * ipn_glob_mutex must be locked */
499 static int _ipn_setpersist(struct ipn_node *ipn_node, int persist)
501 int rv=0;
502 if (persist)
503 ipn_node->flags |= IPN_NODEFLAG_PERSIST;
504 else {
505 ipn_node->flags &= ~IPN_NODEFLAG_PERSIST;
506 if (!(ipn_node->flags & IPN_NODEFLAG_INUSE)) {
507 rv=ipn_terminate_node(ipn_node);
508 if (rv==0)
509 kmem_cache_free(ipn_node_cache,ipn_node);
512 return rv;
515 /* ipn_setpersist
516 * lock ipn_glob_mutex and call __ipn_setpersist above */
517 static int ipn_setpersist(struct ipn_node *ipn_node, int persist)
519 int rv=0;
520 if (ipn_node->netdev == NULL)
521 return -ENODEV;
522 if (down_interruptible(&ipn_glob_mutex))
523 return -ERESTARTSYS;
524 rv=_ipn_setpersist(ipn_node,persist);
525 up(&ipn_glob_mutex);
526 return rv;
529 /* several network parameters can be set by setsockopt prior to bind */
530 /* struct pre_bind_parms is a temporary stucture connected to ipn_node->pbp
531 * to keep the parameter values. */
532 struct pre_bind_parms {
533 unsigned short maxports;
534 unsigned short flags;
535 unsigned short msgpoolsize;
536 unsigned short mtu;
537 unsigned short mode;
540 /* STD_PARMS: BITS_PER_LONG nodes, no flags, BITS_PER_BYTE pending msgs,
541 * Ethernet + VLAN MTU*/
542 #define STD_BIND_PARMS {BITS_PER_LONG, 0, BITS_PER_BYTE, 1514, 0777};
544 static int ipn_mkname(struct sockaddr_un * sunaddr, int len)
546 if (len <= sizeof(short) || len > sizeof(*sunaddr))
547 return -EINVAL;
548 if (!sunaddr || sunaddr->sun_family != AF_IPN)
549 return -EINVAL;
551 * This may look like an off by one error but it is a bit more
552 * subtle. 108 is the longest valid AF_IPN path for a binding.
553 * sun_path[108] doesnt as such exist. However in kernel space
554 * we are guaranteed that it is a valid memory location in our
555 * kernel address buffer.
557 ((char *)sunaddr)[len]=0;
558 len = strlen(sunaddr->sun_path)+1+sizeof(short);
559 return len;
562 static int ipn_node_bind(struct ipn_node *ipn_node, struct ipn_network *ipnn)
564 if (ipnn == NULL)
565 return -ENOENT;
566 if (ipn_node->ipn != NULL)
567 return -EISCONN;
568 ipnn->refcnt++;
569 list_add_tail(&ipn_node->nodelist,&ipnn->unconnectqueue);
570 ipn_node->ipn=ipnn;
571 ipn_node->flags |= IPN_NODEFLAG_BOUND;
572 return 0;
575 /* IPN BIND */
576 static int ipn_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
578 struct sockaddr_un *sunaddr=(struct sockaddr_un *)uaddr;
579 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
580 struct nameidata nd;
581 struct ipn_network *ipnn;
582 struct dentry * dentry = NULL;
583 int err;
584 struct pre_bind_parms parms=STD_BIND_PARMS;
586 //printk("IPN bind\n");
588 if (down_interruptible(&ipn_glob_mutex))
589 return -ERESTARTSYS;
590 if (sock->state != SS_UNCONNECTED ||
591 ipn_node->ipn != NULL) {
592 err= -EISCONN;
593 goto out;
596 if (ipn_node->protocol >= 0 &&
597 (ipn_node->protocol >= IPN_MAX_PROTO ||
598 ipn_protocol_table[ipn_node->protocol] == NULL)) {
599 err= -EPROTONOSUPPORT;
600 goto out;
603 addr_len = ipn_mkname(sunaddr, addr_len);
604 if (addr_len < 0) {
605 err=addr_len;
606 goto out;
609 /* check if there is already an ipn-network socket with that name */
610 err = path_lookup(sunaddr->sun_path, LOOKUP_FOLLOW, &nd);
611 if (err) { /* it does not exist, NEW IPN socket! */
612 unsigned int mode;
613 /* Is it everything okay with the parent? */
614 err = path_lookup(sunaddr->sun_path, LOOKUP_PARENT, &nd);
615 if (err)
616 goto out_mknod_parent;
617 /* Do I have the permission to create a file? */
618 dentry = lookup_create(&nd, 0);
619 err = PTR_ERR(dentry);
620 if (IS_ERR(dentry))
621 goto out_mknod_unlock;
623 * All right, let's create it.
625 if (ipn_node->pbp)
626 mode = ipn_node->pbp->mode;
627 else
628 mode = SOCK_INODE(sock)->i_mode;
629 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 30)
630 mode = S_IFSOCK | (mode & ~current->fs->umask);
631 #else
632 mode = S_IFSOCK | (mode & ~current_umask());
633 #endif
634 #ifndef IPN_PRE2625
635 #ifdef APPARMOR
636 err = vfs_mknod(nd.path.dentry->d_inode, dentry, nd.path.mnt, mode, 0);
637 #else
638 err = vfs_mknod(nd.path.dentry->d_inode, dentry, mode, 0);
639 #endif
640 #else
641 #ifdef APPARMOR
642 err = vfs_mknod(nd.dentry->d_inode, dentry, nd.path.mnt, mode, 0);
643 #else
644 err = vfs_mknod(nd.dentry->d_inode, dentry, mode, 0);
645 #endif
646 #endif
647 if (err)
648 goto out_mknod_dput;
649 #ifndef IPN_PRE2625
650 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
651 dput(nd.path.dentry);
652 nd.path.dentry = dentry;
653 #else
654 mutex_unlock(&nd.dentry->d_inode->i_mutex);
655 dput(nd.dentry);
656 nd.dentry = dentry;
657 #endif
658 /* create a new ipn_network item */
659 if (ipn_node->pbp)
660 parms=*ipn_node->pbp;
661 ipnn=kmem_cache_zalloc(ipn_network_cache,GFP_KERNEL);
662 if (!ipnn) {
663 err=-ENOMEM;
664 goto out_mknod_dput_ipnn;
666 ipnn->connport=kzalloc(parms.maxports * sizeof(struct ipn_node *),GFP_KERNEL);
667 if (!ipnn->connport) {
668 err=-ENOMEM;
669 goto out_mknod_dput_ipnn2;
672 /* module refcnt is incremented for each network, thus
673 * rmmod is forbidden if there are persistent nodes */
674 if (!try_module_get(THIS_MODULE)) {
675 err = -EINVAL;
676 goto out_mknod_dput_ipnn2;
678 memcpy(&ipnn->sunaddr,sunaddr,addr_len);
679 ipnn->mtu=parms.mtu;
680 ipnn->flags=parms.flags;
681 if (ipnn->flags & IPN_FLAG_FLEXMTU)
682 ipnn->msgpool_cache= NULL;
683 else {
684 ipnn->msgpool_cache=ipn_msgbuf_get(ipnn->mtu);
685 if (!ipnn->msgpool_cache) {
686 err=-ENOMEM;
687 goto out_mknod_dput_putmodule;
690 INIT_LIST_HEAD(&ipnn->unconnectqueue);
691 INIT_LIST_HEAD(&ipnn->connectqueue);
692 ipnn->refcnt=0;
693 #ifndef IPN_PRE2625
694 ipnn->dentry=nd.path.dentry;
695 ipnn->mnt=nd.path.mnt;
696 #else
697 ipnn->dentry=nd.dentry;
698 ipnn->mnt=nd.mnt;
699 #endif
700 sema_init(&ipnn->ipnn_mutex,1);
701 ipnn->sunaddr_len=addr_len;
702 ipnn->protocol=ipn_node->protocol;
703 if (ipnn->protocol < 0) ipnn->protocol = 0;
704 ipn_protocol_table[ipnn->protocol]->refcnt++;
705 ipnn->numreaders=0;
706 ipnn->numwriters=0;
707 ipnn->maxports=parms.maxports;
708 atomic_set(&ipnn->msgpool_nelem,0);
709 ipnn->msgpool_size=parms.msgpoolsize;
710 ipnn->proto_private=NULL;
711 init_waitqueue_head(&ipnn->send_wait);
712 ipnn->chrdev=NULL;
713 err=ipn_protocol_table[ipnn->protocol]->ipn_p_newnet(ipnn);
714 if (err)
715 goto out_mknod_dput_putmodule;
716 #ifndef IPN_PRE2625
717 ipn_insert_network(&ipn_network_table[nd.path.dentry->d_inode->i_ino & (IPN_HASH_SIZE-1)],ipnn);
718 #else
719 ipn_insert_network(&ipn_network_table[nd.dentry->d_inode->i_ino & (IPN_HASH_SIZE-1)],ipnn);
720 #endif
721 } else {
722 /* join an existing network */
723 if (parms.flags & IPN_FLAG_EXCL) {
724 err=-EEXIST;
725 goto put_fail;
727 err = inode_permission(nd.path.dentry->d_inode, MAY_EXEC);
728 if (err)
729 goto put_fail;
730 err = -ECONNREFUSED;
731 #ifndef IPN_PRE2625
732 if (!S_ISSOCK(nd.path.dentry->d_inode->i_mode))
733 goto put_fail;
734 ipnn=ipn_find_network_byinode(nd.path.dentry->d_inode);
735 #else
736 if (!S_ISSOCK(nd.dentry->d_inode->i_mode))
737 goto put_fail;
738 ipnn=ipn_find_network_byinode(nd.dentry->d_inode);
739 #endif
740 if (!ipnn || (ipnn->flags & IPN_FLAG_TERMINATED) ||
741 (ipnn->flags & IPN_FLAG_EXCL))
742 goto put_fail;
744 if (ipn_node->pbp) {
745 kfree(ipn_node->pbp);
746 ipn_node->pbp=NULL;
748 ipn_node_bind(ipn_node,ipnn);
749 up(&ipn_glob_mutex);
750 return 0;
752 put_fail:
753 #ifndef IPN_PRE2625
754 path_put(&nd.path);
755 #else
756 path_release(&nd);
757 #endif
758 out:
759 up(&ipn_glob_mutex);
760 return err;
762 out_mknod_dput_putmodule:
763 module_put(THIS_MODULE);
764 out_mknod_dput_ipnn2:
765 kfree(ipnn->connport);
766 out_mknod_dput_ipnn:
767 kmem_cache_free(ipn_network_cache,ipnn);
768 out_mknod_dput:
769 dput(dentry);
770 out_mknod_unlock:
771 #ifndef IPN_PRE2625
772 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
773 path_put(&nd.path);
774 #else
775 mutex_unlock(&nd.dentry->d_inode->i_mutex);
776 path_release(&nd);
777 #endif
778 out_mknod_parent:
779 if (err==-EEXIST)
780 err=-EADDRINUSE;
781 up(&ipn_glob_mutex);
782 return err;
785 /* IPN CONNECT */
786 static int ipn_connect(struct socket *sock, struct sockaddr *addr,
787 int addr_len, int flags){
788 struct sockaddr_un *sunaddr=(struct sockaddr_un*)addr;
789 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
790 struct nameidata nd;
791 struct ipn_network *ipnn,*previousipnn;
792 int err=0;
793 int portno;
795 /* the socket cannot be connected twice */
796 if (sock->state != SS_UNCONNECTED)
797 return EISCONN;
799 if (down_interruptible(&ipn_glob_mutex))
800 return -ERESTARTSYS;
802 if ((previousipnn=ipn_node->ipn) == NULL) { /* unbound */
803 unsigned char mustshutdown=0;
804 err = ipn_mkname(sunaddr, addr_len);
805 if (err < 0)
806 goto out;
807 addr_len=err;
808 err = path_lookup(sunaddr->sun_path, LOOKUP_FOLLOW, &nd);
809 if (err)
810 goto out;
811 err = inode_permission(nd.path.dentry->d_inode, MAY_READ);
812 if (err) {
813 if (err == -EACCES || err == -EROFS)
814 mustshutdown|=RCV_SHUTDOWN;
815 else
816 goto put_fail;
818 err = inode_permission(nd.path.dentry->d_inode, MAY_WRITE);
819 if (err) {
820 if (err == -EACCES)
821 mustshutdown|=SEND_SHUTDOWN;
822 else
823 goto put_fail;
825 mustshutdown |= ipn_node->shutdown;
826 /* if the combination of shutdown and permissions leaves
827 * no abilities, connect returns EACCES */
828 if (mustshutdown == SHUTDOWN_XMASK) {
829 err=-EACCES;
830 goto put_fail;
831 } else {
832 err=0;
833 ipn_node->shutdown=mustshutdown;
835 #ifndef IPN_PRE2625
836 if (!S_ISSOCK(nd.path.dentry->d_inode->i_mode)) {
837 err = -ECONNREFUSED;
838 goto put_fail;
840 ipnn=ipn_find_network_byinode(nd.path.dentry->d_inode);
841 #else
842 if (!S_ISSOCK(nd.dentry->d_inode->i_mode)) {
843 err = -ECONNREFUSED;
844 goto put_fail;
846 ipnn=ipn_find_network_byinode(nd.dentry->d_inode);
847 #endif
848 if (!ipnn || (ipnn->flags & IPN_FLAG_TERMINATED)) {
849 err = -ECONNREFUSED;
850 goto put_fail;
852 if (ipn_node->protocol == IPN_ANY)
853 ipn_node->protocol=ipnn->protocol;
854 else if (ipnn->protocol != ipn_node->protocol) {
855 err = -EPROTO;
856 goto put_fail;
858 #ifndef IPN_PRE2625
859 path_put(&nd.path);
860 #else
861 path_release(&nd);
862 #endif
863 ipn_node->ipn=ipnn;
864 } else
865 ipnn=ipn_node->ipn;
867 if (down_interruptible(&ipnn->ipnn_mutex)) {
868 err=-ERESTARTSYS;
869 goto out;
871 portno = ipn_protocol_table[ipnn->protocol]->ipn_p_newport(ipn_node);
872 if (portno >= 0 && portno<ipnn->maxports) {
873 sock->state = SS_CONNECTED;
874 ipn_node->portno=portno;
875 ipnn->connport[portno]=ipn_node;
876 if (!(ipn_node->flags & IPN_NODEFLAG_BOUND))
877 ipnn->refcnt++;
878 else
879 list_del(&ipn_node->nodelist);
880 list_add_tail(&ipn_node->nodelist,&ipnn->connectqueue);
881 ipn_net_update_counters(ipnn,
882 (ipn_node->shutdown & RCV_SHUTDOWN)?0:1,
883 (ipn_node->shutdown & SEND_SHUTDOWN)?0:1);
884 } else {
885 ipn_node->ipn=previousipnn; /* undo changes on ipn_node->ipn */
886 err=-EADDRNOTAVAIL;
888 up(&ipnn->ipnn_mutex);
889 up(&ipn_glob_mutex);
890 return err;
892 put_fail:
893 #ifndef IPN_PRE2625
894 path_put(&nd.path);
895 #else
896 path_release(&nd);
897 #endif
898 out:
899 up(&ipn_glob_mutex);
900 return err;
903 int ipn_node_create_connect(struct ipn_node **ipn_node_out,
904 struct ipn_network *(* ipnn_map)(void *),void *ipnn_map_arg) {
905 struct ipn_node *ipn_node;
906 struct ipn_network *ipnn;
907 int err=0;
908 int portno;
909 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
910 ipn_node=ipn_node_create(NULL);
911 #else
912 ipn_node=ipn_node_create(&init_net);
913 #endif
914 if (down_interruptible(&ipn_glob_mutex)) {
915 err=-ERESTARTSYS;
916 goto err_ipn_node_release;
918 ipnn=ipnn_map(ipnn_map_arg);
919 ipn_node->ipn=ipnn;
920 ipn_node->protocol=ipnn->protocol;
921 if (down_interruptible(&ipnn->ipnn_mutex)) {
922 err=-ERESTARTSYS;
923 goto out_glob;
925 portno = ipn_protocol_table[ipnn->protocol]->ipn_p_newport(ipn_node);
926 if (portno >= 0 && portno<ipnn->maxports) {
927 ipn_node->portno=portno;
928 ipnn->connport[portno]=ipn_node;
929 if (!(ipn_node->flags & IPN_NODEFLAG_BOUND)) {
930 ipnn->refcnt++;
931 list_del(&ipn_node->nodelist);
933 *ipn_node_out=ipn_node;
934 list_add_tail(&ipn_node->nodelist,&ipnn->connectqueue);
935 ipn_net_update_counters(ipnn,
936 (ipn_node->shutdown & RCV_SHUTDOWN)?0:1,
937 (ipn_node->shutdown & SEND_SHUTDOWN)?0:1);
938 } else {
939 ipn_node->ipn=NULL;
940 err=-EADDRNOTAVAIL;
941 goto out_glob_ipnn;
944 up(&ipnn->ipnn_mutex);
945 up(&ipn_glob_mutex);
946 return err;
947 out_glob_ipnn:
948 up(&ipnn->ipnn_mutex);
949 out_glob:
950 up(&ipn_glob_mutex);
951 err_ipn_node_release:
952 ipn_node_release(ipn_node);
953 ipn_node_out=NULL;
954 return err;
957 int ipn_node_connect(struct ipn_node *ipn_node)
959 struct ipn_network *ipnn;
960 int err=0;
961 int portno;
962 if (down_interruptible(&ipn_glob_mutex))
963 return -ERESTARTSYS;
965 ipnn=ipn_node->ipn;
966 if (down_interruptible(&ipnn->ipnn_mutex)) {
967 err=-ERESTARTSYS;
968 goto out;
970 portno = ipn_protocol_table[ipnn->protocol]->ipn_p_newport(ipn_node);
971 if (portno >= 0 && portno<ipnn->maxports) {
972 ipn_node->portno=portno;
973 ipnn->connport[portno]=ipn_node;
974 if (!(ipn_node->flags & IPN_NODEFLAG_BOUND)) {
975 ipnn->refcnt++;
976 list_del(&ipn_node->nodelist);
978 list_add_tail(&ipn_node->nodelist,&ipnn->connectqueue);
979 ipn_net_update_counters(ipnn,
980 (ipn_node->shutdown & RCV_SHUTDOWN)?0:1,
981 (ipn_node->shutdown & SEND_SHUTDOWN)?0:1);
982 } else {
983 ipn_node->ipn=NULL;
984 err=-EADDRNOTAVAIL;
987 up(&ipnn->ipnn_mutex);
988 up(&ipn_glob_mutex);
989 return err;
990 out:
991 up(&ipn_glob_mutex);
992 return err;
995 static int ipn_getname(struct socket *sock, struct sockaddr *uaddr,
996 int *uaddr_len, int peer) {
997 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
998 struct ipn_network *ipnn=ipn_node->ipn;
999 struct sockaddr_un *sunaddr=(struct sockaddr_un *)uaddr;
1000 int err=0;
1002 if (down_interruptible(&ipn_glob_mutex))
1003 return -ERESTARTSYS;
1004 if (ipnn) {
1005 *uaddr_len = ipnn->sunaddr_len;
1006 memcpy(sunaddr,&ipnn->sunaddr,*uaddr_len);
1007 } else
1008 err = -ENOTCONN;
1009 up(&ipn_glob_mutex);
1010 return err;
1013 /* IPN POLL */
1014 unsigned int ipn_node_poll(struct ipn_node *ipn_node, struct file *file, poll_table *wait) {
1015 struct ipn_network *ipnn=ipn_node->ipn;
1016 unsigned int mask=0;
1018 if (ipnn) {
1019 poll_wait(file,&ipn_node->read_wait,wait);
1020 if (ipnn->flags & IPN_FLAG_LOSSLESS)
1021 poll_wait(file,&ipnn->send_wait,wait);
1022 /* POLLIN if recv succeeds,
1023 * POLL{PRI,RDNORM} if there are {oob,non-oob} messages */
1024 if (ipn_node->totmsgcount > 0) mask |= POLLIN;
1025 if (!(list_empty(&ipn_node->msgqueue))) mask |= POLLRDNORM;
1026 if (!(list_empty(&ipn_node->oobmsgqueue))) mask |= POLLPRI;
1027 if ((!(ipnn->flags & IPN_FLAG_LOSSLESS)) |
1028 (atomic_read(&ipnn->msgpool_nelem) < ipnn->msgpool_size))
1029 mask |= POLLOUT | POLLWRNORM;
1031 return mask;
1034 static unsigned int ipn_poll(struct file *file, struct socket *sock,
1035 poll_table *wait) {
1036 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
1037 return ipn_node_poll(ipn_node, file, wait);
1040 /* connect netdev (from ioctl). connect a bound socket to a
1041 * network device TAP or GRAB */
1042 static int ipn_connect_netdev(struct socket *sock,struct ifreq *ifr)
1044 int err=0;
1045 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
1046 struct ipn_network *ipnn=ipn_node->ipn;
1047 if (!capable(CAP_NET_ADMIN))
1048 return -EPERM;
1049 if (sock->state != SS_UNCONNECTED)
1050 return -EISCONN;
1051 if (!ipnn)
1052 return -ENOTCONN; /* Maybe we need a different error for "NOT BOUND" */
1053 if (down_interruptible(&ipn_glob_mutex))
1054 return -ERESTARTSYS;
1055 if (down_interruptible(&ipnn->ipnn_mutex)) {
1056 up(&ipn_glob_mutex);
1057 return -ERESTARTSYS;
1059 ipn_node->netdev=ipn_netdev_alloc(ipn_node->net,ifr->ifr_flags,ifr->ifr_name,&err);
1060 if (ipn_node->netdev) {
1061 int portno;
1062 portno = ipn_protocol_table[ipnn->protocol]->ipn_p_newport(ipn_node);
1063 if (portno >= 0 && portno<ipnn->maxports) {
1064 sock->state = SS_CONNECTED;
1065 ipn_node->portno=portno;
1066 ipn_node->flags |= ifr->ifr_flags & IPN_NODEFLAG_DEVMASK;
1067 ipnn->connport[portno]=ipn_node;
1068 err=ipn_netdev_activate(ipn_node);
1069 if (err) {
1070 sock->state = SS_UNCONNECTED;
1071 ipn_protocol_table[ipnn->protocol]->ipn_p_delport(ipn_node);
1072 ipn_node->netdev=NULL;
1073 ipn_node->portno= -1;
1074 ipn_node->flags &= ~IPN_NODEFLAG_DEVMASK;
1075 ipnn->connport[portno]=NULL;
1076 } else {
1077 ipn_protocol_table[ipnn->protocol]->ipn_p_postnewport(ipn_node);
1078 list_del(&ipn_node->nodelist);
1079 list_add_tail(&ipn_node->nodelist,&ipnn->connectqueue);
1081 } else {
1082 ipn_netdev_close(ipn_node);
1083 err=-EADDRNOTAVAIL;
1084 ipn_node->netdev=NULL;
1086 } else
1087 err=-EINVAL;
1088 up(&ipnn->ipnn_mutex);
1089 up(&ipn_glob_mutex);
1090 return err;
1093 /* join a netdev, a socket gets connected to a persistent node
1094 * not connected to another socket */
1095 static int ipn_join_netdev(struct socket *sock,struct ifreq *ifr)
1097 int err=0;
1098 struct net_device *dev;
1099 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
1100 struct ipn_node *ipn_joined;
1101 struct ipn_network *ipnn=ipn_node->ipn;
1102 if (sock->state != SS_UNCONNECTED)
1103 return -EISCONN;
1104 if (down_interruptible(&ipn_glob_mutex))
1105 return -ERESTARTSYS;
1106 if (down_interruptible(&ipnn->ipnn_mutex)) {
1107 up(&ipn_glob_mutex);
1108 return -ERESTARTSYS;
1110 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
1111 dev=__dev_get_by_name(ifr->ifr_name);
1112 #else
1113 dev=__dev_get_by_name(ipn_node->net,ifr->ifr_name);
1114 #endif
1115 if (!dev)
1116 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
1117 dev=__dev_get_by_index(ifr->ifr_ifindex);
1118 #else
1119 dev=__dev_get_by_index(ipn_node->net,ifr->ifr_ifindex);
1120 #endif
1121 if (dev && (ipn_joined=ipn_netdev2node(dev)) != NULL) { /* the interface does exist */
1122 int i;
1123 for (i=0;i<ipnn->maxports && ipn_joined != ipnn->connport[i] ;i++)
1125 if (i < ipnn->maxports) { /* found */
1126 /* ipn_joined is substituted to ipn_node */
1127 ((struct ipn_sock *)sock->sk)->node=ipn_joined;
1128 ipn_joined->flags |= IPN_NODEFLAG_INUSE;
1129 ipnn->refcnt--;
1130 kmem_cache_free(ipn_node_cache,ipn_node);
1131 } else
1132 err=-EPERM;
1133 } else
1134 err=-EADDRNOTAVAIL;
1135 up(&ipnn->ipnn_mutex);
1136 up(&ipn_glob_mutex);
1137 return err;
1140 /* set persistence of a node looking for it by interface name
1141 * (it is for sysadm, to close network interfaces)*/
1142 static int ipn_setpersist_netdev(struct ifreq *ifr, int value)
1144 struct net_device *dev;
1145 struct ipn_node *ipn_node;
1146 int err=0;
1147 if (!capable(CAP_NET_ADMIN))
1148 return -EPERM;
1149 if (down_interruptible(&ipn_glob_mutex))
1150 return -ERESTARTSYS;
1151 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
1152 dev=__dev_get_by_name(ifr->ifr_name);
1153 #else
1154 dev=__dev_get_by_name(&init_net,ifr->ifr_name);
1155 #endif
1156 if (!dev)
1157 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
1158 dev=__dev_get_by_index(ifr->ifr_ifindex);
1159 #else
1160 dev=__dev_get_by_index(&init_net,ifr->ifr_ifindex);
1161 #endif
1162 if (dev && (ipn_node=ipn_netdev2node(dev)) != NULL)
1163 _ipn_setpersist(ipn_node,value);
1164 else
1165 err=-EADDRNOTAVAIL;
1166 up(&ipn_glob_mutex);
1167 return err;
1170 /* IPN IOCTL */
1172 int ipn_node_ioctl(struct ipn_node *ipn_node, unsigned int cmd, unsigned long arg) {
1173 struct ipn_network *ipnn=ipn_node->ipn;
1174 if (ipnn &&
1175 (ipn_protocol_table[ipn_node->protocol]->ipn_p_ioctl != NULL)) {
1176 int rv;
1177 if (down_interruptible(&ipnn->ipnn_mutex))
1178 return -ERESTARTSYS;
1179 rv=ipn_protocol_table[ipn_node->protocol]->ipn_p_ioctl(ipn_node,cmd,arg);
1180 up(&ipnn->ipnn_mutex);
1181 return rv;
1182 } else
1183 return -EOPNOTSUPP;
1186 static int ipn_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) {
1187 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
1188 struct ipn_network *ipnn=ipn_node->ipn;
1189 void __user* argp = (void __user*)arg;
1190 struct ifreq ifr;
1191 struct chrdevreq devr;
1193 if (ipn_node->shutdown == SHUTDOWN_XMASK)
1194 return -ECONNRESET;
1196 /* get arguments */
1197 switch (cmd) {
1198 case IPN_CHECK:
1199 return IPN_CHECK;
1200 case IPN_SETPERSIST_NETDEV:
1201 case IPN_CLRPERSIST_NETDEV:
1202 case IPN_CONN_NETDEV:
1203 case IPN_JOIN_NETDEV:
1204 case SIOCSIFHWADDR:
1205 if (copy_from_user(&ifr, argp, sizeof(ifr)))
1206 return -EFAULT;
1207 ifr.ifr_name[IFNAMSIZ-1] = '\0';
1208 break;
1209 case IPN_REGISTER_CHRDEV:
1210 case IPN_JOIN_CHRDEV:
1211 if (copy_from_user(&devr, argp, sizeof(devr)))
1212 return -EFAULT;
1213 /*printk("IPN_REGISTER_CHRDEV %p %d %d %d\n",
1214 argp, sizeof(devr),devr.major, devr.minor);*/
1215 break;
1218 /* actions for unconnected and unbound sockets */
1219 switch (cmd) {
1220 case IPN_SETPERSIST_NETDEV:
1221 return ipn_setpersist_netdev(&ifr,1);
1222 case IPN_CLRPERSIST_NETDEV:
1223 return ipn_setpersist_netdev(&ifr,0);
1224 case IPN_JOIN_CHRDEV:
1226 int rv;
1227 if (!capable(CAP_MKNOD))
1228 return -EPERM;
1229 if (ipn_node->ipn != NULL)
1230 return -EISCONN;
1231 if (down_interruptible(&ipn_glob_mutex))
1232 return -ERESTARTSYS;
1233 rv=ipn_node_bind(ipn_node, ipn_find_chrdev(&devr));
1234 up(&ipn_glob_mutex);
1235 return rv;
1237 case SIOCSIFHWADDR:
1238 if (capable(CAP_NET_ADMIN))
1239 return -EPERM;
1240 if (ipn_node->netdev && (ipn_node->flags &IPN_NODEFLAG_TAP))
1241 return dev_set_mac_address(ipn_node->netdev, &ifr.ifr_hwaddr);
1242 else
1243 return -EADDRNOTAVAIL;
1245 if (ipnn == NULL || (ipnn->flags & IPN_FLAG_TERMINATED))
1246 return -ENOTCONN;
1247 /* actions for connected or bound sockets */
1248 switch (cmd) {
1249 case IPN_CONN_NETDEV:
1250 return ipn_connect_netdev(sock,&ifr);
1251 case IPN_JOIN_NETDEV:
1252 return ipn_join_netdev(sock,&ifr);
1253 case IPN_SETPERSIST:
1254 return ipn_setpersist(ipn_node,arg);
1255 case IPN_CHRDEV_PERSIST:
1256 return ipn_chrdev_persistence(ipnn,arg);
1257 case IPN_REGISTER_CHRDEV:
1259 int rv;
1260 unsigned int reqmajor=devr.major;
1261 if (down_interruptible(&ipnn->ipnn_mutex))
1262 return -ERESTARTSYS;
1263 rv=ipn_register_chrdev(ipnn,&devr);
1264 if (reqmajor==0 && rv==0) {
1265 if (copy_to_user(argp, &devr, sizeof devr))
1266 rv=EFAULT;
1268 up(&ipnn->ipnn_mutex);
1269 return rv;
1271 case IPN_UNREGISTER_CHRDEV:
1273 int rv;
1274 if (down_interruptible(&ipnn->ipnn_mutex))
1275 return -ERESTARTSYS;
1276 rv=ipn_deregister_chrdev(ipnn);
1277 up(&ipnn->ipnn_mutex);
1278 return rv;
1280 default:
1281 return ipn_node_ioctl(ipn_node, cmd, arg);
1285 /* shutdown: close socket for input or for output.
1286 * shutdown can be called prior to connect and it is not reversible */
1287 static int ipn_shutdown(struct socket *sock, int mode) {
1288 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
1289 struct ipn_network *ipnn=ipn_node->ipn;
1290 int oldshutdown=ipn_node->shutdown;
1291 mode = (mode+1)&(RCV_SHUTDOWN|SEND_SHUTDOWN);
1293 /* define the new mode first... */
1294 ipn_node->shutdown |= mode;
1296 if(ipnn) {
1297 /* ... and wait for all pending ops to be completed */
1298 if (down_interruptible(&ipnn->ipnn_mutex)) {
1299 ipn_node->shutdown = oldshutdown;
1300 return -ERESTARTSYS;
1302 oldshutdown=ipn_node->shutdown-oldshutdown;
1303 if (sock->state == SS_CONNECTED && oldshutdown) {
1304 ipn_net_update_counters(ipnn,
1305 (ipn_node->shutdown & RCV_SHUTDOWN)?0:-1,
1306 (ipn_node->shutdown & SEND_SHUTDOWN)?0:-1);
1309 /* if recv channel has been shut down, flush the recv queue */
1310 if ((ipn_node->shutdown & RCV_SHUTDOWN))
1311 ipn_flush_recvqueue(ipn_node);
1312 up(&ipnn->ipnn_mutex);
1314 return 0;
1317 /* injectmsg: a new message is entering the ipn network.
1318 * injectmsg gets called by send and by the grab/tap node */
1319 /* handlemsg is protected from ipn_network changes: ipnn->ipnn_mutex is locked */
1320 int ipn_proto_injectmsg(struct ipn_node *from, struct msgpool_item *msg)
1322 struct ipn_network *ipnn=from->ipn;
1323 int err=0;
1324 static int recinject=0;
1325 //printk("INJECTMSG IN\n");
1326 if (recinject)
1327 ipn_protocol_table[ipnn->protocol]->ipn_p_handlemsg(from, msg);
1328 else if (down_interruptible(&ipnn->ipnn_mutex))
1329 err=-ERESTARTSYS;
1330 else {
1331 recinject=1;
1332 ipn_protocol_table[ipnn->protocol]->ipn_p_handlemsg(from, msg);
1333 recinject=0;
1334 up(&ipnn->ipnn_mutex);
1336 //printk("INJECTMSG OUT %d\n",err);
1337 return err;
1340 /* SEND MSG */
1341 int ipn_node_write(struct ipn_node *ipn_node, struct iovec *msg_iov, int len) {
1342 struct ipn_network *ipnn=ipn_node->ipn;
1343 struct msgpool_item *newmsg;
1344 int err=0;
1346 if (unlikely(ipn_node->shutdown & SEND_SHUTDOWN)) {
1347 if (ipn_node->shutdown == SHUTDOWN_XMASK)
1348 return -ECONNRESET;
1349 else
1350 return -EPIPE;
1352 if (len > ipnn->mtu)
1353 return -EOVERFLOW;
1354 newmsg=ipn_msgpool_alloc_locking(ipnn,len);
1355 if (!newmsg)
1356 return -ENOMEM;
1357 newmsg->len=len;
1358 err=memcpy_fromiovec(newmsg->data, msg_iov, len);
1359 if (!err)
1360 ipn_proto_injectmsg(ipn_node, newmsg);
1361 ipn_msgpool_put(newmsg,ipnn);
1362 return len;
1365 static int ipn_sendmsg(struct kiocb *kiocb, struct socket *sock,
1366 struct msghdr *msg, size_t len) {
1367 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
1369 if (unlikely(sock->state != SS_CONNECTED))
1370 return -ENOTCONN;
1371 else
1372 return ipn_node_write(ipn_node, msg->msg_iov, len);
1375 /* ipn_proto_sendmsg is called by protocol implementation to enqueue a
1376 * for a destination (to).*/
1377 /* enqueue a msgitem on the receiver's queue, the queue is protected by a
1378 * spinlock (msglock) */
1379 void ipn_proto_sendmsg(struct ipn_node *to, struct msgpool_item *msg)
1381 if (to) {
1382 if (to->netdev) {
1383 ipn_netdev_sendmsg(to,msg);
1384 } else {
1385 /* socket send */
1386 struct msgitem *msgitem;
1387 struct ipn_network *ipnn=to->ipn;
1388 spin_lock(&to->msglock);
1389 if (likely((to->shutdown & RCV_SHUTDOWN)==0)) {
1390 /*if (unlikely((ipnn->flags & IPN_FLAG_LOSSLESS) == 0 ||
1391 to->totmsgcount >= ipnn->msgpool_size))
1392 yield();*/
1393 if (likely(ipnn->flags & IPN_FLAG_LOSSLESS ||
1394 to->totmsgcount < ipnn->msgpool_size)) {
1395 if ((msgitem=kmem_cache_alloc(ipn_msgitem_cache,GFP_KERNEL))!=NULL) {
1396 msgitem->msg=msg;
1397 to->totmsgcount++;
1398 list_add_tail(&msgitem->list, &to->msgqueue);
1399 ipn_msgpool_hold(msg);
1403 spin_unlock(&to->msglock);
1404 wake_up_interruptible(&to->read_wait);
1409 /* enqueue an oob message. "to" is the destination */
1410 void ipn_proto_oobsendmsg(struct ipn_node *to, struct msgpool_item *msg)
1412 if (to) {
1413 if (!to->netdev) { /* no oob to netdev */
1414 struct msgitem *msgitem;
1415 struct ipn_network *ipnn=to->ipn;
1416 spin_lock(&to->msglock);
1417 if ((to->shutdown & RCV_SHUTDOWN_NO_OOB) == 0 &&
1418 (ipnn->flags & IPN_FLAG_LOSSLESS ||
1419 to->oobmsgcount < ipnn->msgpool_size)) {
1420 if ((msgitem=kmem_cache_alloc(ipn_msgitem_cache,GFP_KERNEL))!=NULL) {
1421 msgitem->msg=msg;
1422 to->totmsgcount++;
1423 to->oobmsgcount++;
1424 list_add_tail(&msgitem->list, &to->oobmsgqueue);
1425 ipn_msgpool_hold(msg);
1428 spin_unlock(&to->msglock);
1429 wake_up_interruptible(&to->read_wait);
1434 /* IPN RECV */
1435 int ipn_node_read(struct ipn_node *ipn_node, struct iovec *msg_iov, size_t len, int *msg_flags, int flags) {
1436 struct ipn_network *ipnn=ipn_node->ipn;
1437 struct msgitem *msgitem;
1438 struct msgpool_item *currmsg;
1440 if (unlikely((ipn_node->shutdown & XRCV_SHUTDOWN) == XRCV_SHUTDOWN)) {
1441 if (ipn_node->shutdown == SHUTDOWN_XMASK) /*EOF, nothing can be read*/
1442 return 0;
1443 else
1444 return -EPIPE; /*trying to read on a write only node */
1447 /* wait for a message */
1448 spin_lock(&ipn_node->msglock);
1449 while (ipn_node->totmsgcount == 0) {
1450 spin_unlock(&ipn_node->msglock);
1451 if (wait_event_interruptible(ipn_node->read_wait,
1452 !(ipn_node->totmsgcount == 0)))
1453 return -ERESTARTSYS;
1454 spin_lock(&ipn_node->msglock);
1456 /* oob gets delivered first. oob are rare */
1457 if (likely(list_empty(&ipn_node->oobmsgqueue)))
1458 msgitem=list_first_entry(&ipn_node->msgqueue, struct msgitem, list);
1459 else {
1460 msgitem=list_first_entry(&ipn_node->oobmsgqueue, struct msgitem, list);
1461 *msg_flags |= MSG_OOB;
1462 ipn_node->oobmsgcount--;
1464 list_del(&msgitem->list);
1465 ipn_node->totmsgcount--;
1466 spin_unlock(&ipn_node->msglock);
1467 currmsg=msgitem->msg;
1468 if (currmsg->len < len)
1469 len=currmsg->len;
1470 memcpy_toiovec(msg_iov, currmsg->data, len);
1471 ipn_msgpool_put(currmsg,ipnn);
1472 kmem_cache_free(ipn_msgitem_cache,msgitem);
1473 return len;
1476 static int ipn_recvmsg(struct kiocb *kiocb, struct socket *sock,
1477 struct msghdr *msg, size_t len, int flags) {
1478 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
1480 if (unlikely(sock->state != SS_CONNECTED))
1481 return -ENOTCONN;
1482 else
1483 return ipn_node_read(ipn_node, msg->msg_iov, len, &msg->msg_flags, flags);
1486 /* resize a network: change the # of communication ports (connport) */
1487 static int ipn_netresize(struct ipn_network *ipnn,int newsize)
1489 int oldsize,min;
1490 struct ipn_node **newconnport;
1491 struct ipn_node **oldconnport;
1492 int err;
1493 if (down_interruptible(&ipnn->ipnn_mutex))
1494 return -ERESTARTSYS;
1495 oldsize=ipnn->maxports;
1496 if (newsize == oldsize) {
1497 up(&ipnn->ipnn_mutex);
1498 return 0;
1500 min=oldsize;
1501 /* shrink a network. all the ports we are going to eliminate
1502 * must be unused! */
1503 if (newsize < oldsize) {
1504 int i;
1505 for (i=newsize; i<oldsize; i++)
1506 if (ipnn->connport[i]) {
1507 up(&ipnn->ipnn_mutex);
1508 return -EADDRINUSE;
1510 min=newsize;
1512 oldconnport=ipnn->connport;
1513 /* allocate the new connport array and copy the old one */
1514 newconnport=kzalloc(newsize * sizeof(struct ipn_node *),GFP_KERNEL);
1515 if (!newconnport) {
1516 up(&ipnn->ipnn_mutex);
1517 return -ENOMEM;
1519 memcpy(newconnport,oldconnport,min * sizeof(struct ipn_node *));
1520 ipnn->connport=newconnport;
1521 ipnn->maxports=newsize;
1522 /* notify the protocol that the netowrk has been resized */
1523 err=ipn_protocol_table[ipnn->protocol]->ipn_p_resizenet(ipnn,oldsize,newsize);
1524 if (err) {
1525 /* roll back if the resize operation failed for the protocol */
1526 ipnn->connport=oldconnport;
1527 ipnn->maxports=oldsize;
1528 kfree(newconnport);
1529 } else
1530 /* successful mission, network resized */
1531 kfree(oldconnport);
1532 up(&ipnn->ipnn_mutex);
1533 return err;
1536 /* IPN SETSOCKOPT */
1537 #ifndef IPN_PRE2632
1538 static int ipn_setsockopt(struct socket *sock, int level, int optname,
1539 char __user *optval, unsigned int optlen)
1540 #else
1541 static int ipn_setsockopt(struct socket *sock, int level, int optname,
1542 char __user *optval, int optlen)
1543 #endif
1545 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
1546 struct ipn_network *ipnn=ipn_node->ipn;
1548 if (ipn_node->shutdown == SHUTDOWN_XMASK)
1549 return -ECONNRESET;
1550 if (level != 0 && level != ipn_node->protocol+1)
1551 return -EPROTONOSUPPORT;
1552 if (level > 0) {
1553 /* protocol specific sockopt */
1554 if (ipnn) {
1555 int rv;
1556 if (down_interruptible(&ipnn->ipnn_mutex))
1557 return -ERESTARTSYS;
1558 rv=ipn_protocol_table[ipn_node->protocol]->ipn_p_setsockopt(ipn_node,optname,optval,optlen);
1559 up(&ipnn->ipnn_mutex);
1560 return rv;
1561 } else
1562 return -EOPNOTSUPP;
1563 } else {
1564 if (optname == IPN_SO_DESCR) {
1565 if (optlen > IPN_DESCRLEN)
1566 return -EINVAL;
1567 else {
1568 memset(ipn_node->descr,0,IPN_DESCRLEN);
1569 if (copy_from_user(ipn_node->descr,optval,optlen))
1570 ipn_node->descr[0]=0;
1571 else
1572 ipn_node->descr[optlen-1]=0;
1573 return 0;
1575 } else {
1576 if (optlen < sizeof(int))
1577 return -EINVAL;
1578 else if ((optname & IPN_SO_PREBIND) && (ipnn != NULL))
1579 return -EISCONN;
1580 else {
1581 int val;
1582 get_user(val, (int __user *) optval);
1583 if ((optname & IPN_SO_PREBIND) && !ipn_node->pbp) {
1584 struct pre_bind_parms std=STD_BIND_PARMS;
1585 ipn_node->pbp=kzalloc(sizeof(struct pre_bind_parms),GFP_KERNEL);
1586 if (!ipn_node->pbp)
1587 return -ENOMEM;
1588 *(ipn_node->pbp)=std;
1590 switch (optname) {
1591 case IPN_SO_PORT:
1592 if (sock->state == SS_UNCONNECTED)
1593 ipn_node->portno=val;
1594 else
1595 return -EISCONN;
1596 break;
1597 case IPN_SO_CHANGE_NUMNODES:
1598 if ((ipn_node->flags & IPN_NODEFLAG_BOUND)!=0) {
1599 if (val <= 0)
1600 return -EINVAL;
1601 else
1602 return ipn_netresize(ipnn,val);
1603 } else
1604 val=-ENOTCONN;
1605 break;
1606 case IPN_SO_WANT_OOB_NUMNODES:
1607 if (val)
1608 ipn_node->flags |= IPN_NODEFLAG_OOB_NUMNODES;
1609 else
1610 ipn_node->flags &= ~IPN_NODEFLAG_OOB_NUMNODES;
1611 break;
1612 case IPN_SO_HANDLE_OOB:
1613 if (val)
1614 ipn_node->shutdown &= ~RCV_SHUTDOWN_NO_OOB;
1615 else
1616 ipn_node->shutdown |= RCV_SHUTDOWN_NO_OOB;
1617 break;
1618 case IPN_SO_MTU:
1619 if (val <= 0)
1620 return -EINVAL;
1621 else
1622 ipn_node->pbp->mtu=val;
1623 break;
1624 case IPN_SO_NUMNODES:
1625 if (val <= 0)
1626 return -EINVAL;
1627 else
1628 ipn_node->pbp->maxports=val;
1629 break;
1630 case IPN_SO_MSGPOOLSIZE:
1631 if (val <= 0)
1632 return -EINVAL;
1633 else
1634 ipn_node->pbp->msgpoolsize=val;
1635 break;
1636 case IPN_SO_FLAGS:
1637 ipn_node->pbp->flags=val;
1638 break;
1639 case IPN_SO_MODE:
1640 ipn_node->pbp->mode=val;
1641 break;
1643 return 0;
1649 /* IPN GETSOCKOPT */
1650 static int ipn_getsockopt(struct socket *sock, int level, int optname,
1651 char __user *optval, int __user *optlen) {
1652 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
1653 struct ipn_network *ipnn=ipn_node->ipn;
1654 int len;
1656 if (ipn_node->shutdown == SHUTDOWN_XMASK)
1657 return -ECONNRESET;
1658 if (level != 0 && level != ipn_node->protocol+1)
1659 return -EPROTONOSUPPORT;
1660 if (level > 0) {
1661 if (ipnn) {
1662 int rv;
1663 /* protocol specific sockopt */
1664 if (down_interruptible(&ipnn->ipnn_mutex))
1665 return -ERESTARTSYS;
1666 rv=ipn_protocol_table[ipn_node->protocol]->ipn_p_getsockopt(ipn_node,optname,optval,optlen);
1667 up(&ipnn->ipnn_mutex);
1668 return rv;
1669 } else
1670 return -EOPNOTSUPP;
1671 } else {
1672 if (get_user(len, optlen))
1673 return -EFAULT;
1674 if (optname == IPN_SO_DESCR) {
1675 if (len < IPN_DESCRLEN)
1676 return -EINVAL;
1677 else {
1678 if (len > IPN_DESCRLEN)
1679 len=IPN_DESCRLEN;
1680 if(put_user(len, optlen))
1681 return -EFAULT;
1682 if(copy_to_user(optval,ipn_node->descr,len))
1683 return -EFAULT;
1684 return 0;
1686 } else {
1687 int val=-2;
1688 switch (optname) {
1689 case IPN_SO_PORT:
1690 val=ipn_node->portno;
1691 break;
1692 case IPN_SO_MTU:
1693 if (ipnn)
1694 val=ipnn->mtu;
1695 else if (ipn_node->pbp)
1696 val=ipn_node->pbp->mtu;
1697 break;
1698 case IPN_SO_NUMNODES:
1699 if (ipnn)
1700 val=ipnn->maxports;
1701 else if (ipn_node->pbp)
1702 val=ipn_node->pbp->maxports;
1703 break;
1704 case IPN_SO_MSGPOOLSIZE:
1705 if (ipnn)
1706 val=ipnn->msgpool_size;
1707 else if (ipn_node->pbp)
1708 val=ipn_node->pbp->msgpoolsize;
1709 break;
1710 case IPN_SO_FLAGS:
1711 if (ipnn)
1712 val=ipnn->flags;
1713 else if (ipn_node->pbp)
1714 val=ipn_node->pbp->flags;
1715 break;
1716 case IPN_SO_MODE:
1717 if (ipnn)
1718 val=-1;
1719 else if (ipn_node->pbp)
1720 val=ipn_node->pbp->mode;
1721 break;
1723 if (val < -1)
1724 return -EINVAL;
1725 else {
1726 if (len < sizeof(int))
1727 return -EOVERFLOW;
1728 else {
1729 len = sizeof(int);
1730 if(put_user(len, optlen))
1731 return -EFAULT;
1732 if(copy_to_user(optval,&val,len))
1733 return -EFAULT;
1734 return 0;
1741 /* BROADCAST/HUB implementation */
1743 static int ipn_bcast_newport(struct ipn_node *newport) {
1744 struct ipn_network *ipnn=newport->ipn;
1745 int i;
1746 for (i=0;i<ipnn->maxports;i++) {
1747 if (ipnn->connport[i] == NULL)
1748 return i;
1750 return -1;
1753 static int ipn_bcast_handlemsg(struct ipn_node *from,
1754 struct msgpool_item *msgitem){
1755 struct ipn_network *ipnn=from->ipn;
1757 struct ipn_node *ipn_node;
1758 list_for_each_entry(ipn_node, &ipnn->connectqueue, nodelist) {
1759 if (ipn_node != from)
1760 ipn_proto_sendmsg(ipn_node,msgitem);
1762 return 0;
1765 static void ipn_null_delport(struct ipn_node *oldport) {}
1766 static void ipn_null_postnewport(struct ipn_node *newport) {}
1767 static void ipn_null_predelport(struct ipn_node *oldport) {}
1768 static int ipn_null_newnet(struct ipn_network *newnet) {return 0;}
1769 static int ipn_null_resizenet(struct ipn_network *net,int oldsize,int newsize) {
1770 return 0;}
1771 static void ipn_null_delnet(struct ipn_network *oldnet) {}
1772 static int ipn_null_setsockopt(struct ipn_node *port,int optname,
1773 char __user *optval, int optlen) {return -EOPNOTSUPP;}
1774 static int ipn_null_getsockopt(struct ipn_node *port,int optname,
1775 char __user *optval, int *optlen) {return -EOPNOTSUPP;}
1776 static int ipn_null_ioctl(struct ipn_node *port,unsigned int request,
1777 unsigned long arg) {return -EOPNOTSUPP;}
1779 /* Protocol Registration/deregisteration */
1781 void ipn_init_protocol(struct ipn_protocol *p)
1783 if (p->ipn_p_delport == NULL) p->ipn_p_delport=ipn_null_delport;
1784 if (p->ipn_p_postnewport == NULL) p->ipn_p_postnewport=ipn_null_postnewport;
1785 if (p->ipn_p_predelport == NULL) p->ipn_p_predelport=ipn_null_predelport;
1786 if (p->ipn_p_newnet == NULL) p->ipn_p_newnet=ipn_null_newnet;
1787 if (p->ipn_p_resizenet == NULL) p->ipn_p_resizenet=ipn_null_resizenet;
1788 if (p->ipn_p_delnet == NULL) p->ipn_p_delnet=ipn_null_delnet;
1789 if (p->ipn_p_setsockopt == NULL) p->ipn_p_setsockopt=ipn_null_setsockopt;
1790 if (p->ipn_p_getsockopt == NULL) p->ipn_p_getsockopt=ipn_null_getsockopt;
1791 if (p->ipn_p_ioctl == NULL) p->ipn_p_ioctl=ipn_null_ioctl;
1794 int ipn_proto_register(int protocol,struct ipn_protocol *ipn_service)
1796 int rv=0;
1797 if (ipn_service->ipn_p_newport == NULL ||
1798 ipn_service->ipn_p_handlemsg == NULL)
1799 return -EINVAL;
1800 ipn_init_protocol(ipn_service);
1801 if (protocol > 1 && protocol <= IPN_MAX_PROTO) {
1802 protocol--;
1803 if (down_interruptible(&ipn_glob_mutex))
1804 return -ERESTARTSYS;
1805 if (ipn_protocol_table[protocol])
1806 rv= -EEXIST;
1807 else {
1808 ipn_service->refcnt=0;
1809 ipn_protocol_table[protocol]=ipn_service;
1810 printk(KERN_INFO "IPN: Registered protocol %d\n",protocol+1);
1812 up(&ipn_glob_mutex);
1813 } else
1814 rv= -EINVAL;
1815 return rv;
1818 int ipn_proto_deregister(int protocol)
1820 int rv=0;
1821 if (protocol > 1 && protocol <= IPN_MAX_PROTO) {
1822 protocol--;
1823 if (down_interruptible(&ipn_glob_mutex))
1824 return -ERESTARTSYS;
1825 if (ipn_protocol_table[protocol]) {
1826 if (ipn_protocol_table[protocol]->refcnt == 0) {
1827 ipn_protocol_table[protocol]=NULL;
1828 printk(KERN_INFO "IPN: Unregistered protocol %d\n",protocol+1);
1829 } else
1830 rv=-EADDRINUSE;
1831 } else
1832 rv= -ENOENT;
1833 up(&ipn_glob_mutex);
1834 } else
1835 rv= -EINVAL;
1836 return rv;
1839 /* MAIN SECTION */
1840 /* Module constructor/destructor */
1841 static struct net_proto_family ipn_family_ops = {
1842 .family = PF_IPN,
1843 .create = ipn_create,
1844 .owner = THIS_MODULE,
1847 /* IPN constructor */
1848 static int ipn_init(void)
1850 int rc;
1852 ipn_init_protocol(&ipn_bcast);
1853 ipn_network_cache=kmem_cache_create("ipn_network",sizeof(struct ipn_network),0,0,NULL);
1854 if (!ipn_network_cache) {
1855 printk(KERN_CRIT "%s: Cannot create ipn_network SLAB cache!\n",
1856 __FUNCTION__);
1857 rc=-ENOMEM;
1858 goto out;
1861 ipn_node_cache=kmem_cache_create("ipn_node",sizeof(struct ipn_node),0,0,NULL);
1862 if (!ipn_node_cache) {
1863 printk(KERN_CRIT "%s: Cannot create ipn_node SLAB cache!\n",
1864 __FUNCTION__);
1865 rc=-ENOMEM;
1866 goto out_net;
1869 ipn_msgitem_cache=kmem_cache_create("ipn_msgitem",sizeof(struct msgitem),0,0,NULL);
1870 if (!ipn_msgitem_cache) {
1871 printk(KERN_CRIT "%s: Cannot create ipn_msgitem SLAB cache!\n",
1872 __FUNCTION__);
1873 rc=-ENOMEM;
1874 goto out_net_node;
1877 rc=ipn_msgbuf_init();
1878 if (rc != 0) {
1879 printk(KERN_CRIT "%s: Cannot create ipn_msgbuf SLAB cache\n",
1880 __FUNCTION__);
1881 goto out_net_node_msg;
1884 rc=proto_register(&ipn_proto,1);
1885 if (rc != 0) {
1886 printk(KERN_CRIT "%s: Cannot register the protocol!\n",
1887 __FUNCTION__);
1888 goto out_net_node_msg_msgbuf;
1891 sock_register(&ipn_family_ops);
1892 ipn_netdev_init();
1893 printk(KERN_INFO "IPN: Virtual Square Project, University of Bologna 2007-09\n");
1894 return 0;
1896 out_net_node_msg_msgbuf:
1897 ipn_msgbuf_fini();
1898 out_net_node_msg:
1899 kmem_cache_destroy(ipn_msgitem_cache);
1900 out_net_node:
1901 kmem_cache_destroy(ipn_node_cache);
1902 out_net:
1903 kmem_cache_destroy(ipn_network_cache);
1904 out:
1905 return rc;
1908 /* IPN destructor */
1909 static void ipn_exit(void)
1911 ipn_netdev_fini();
1912 if (ipn_msgitem_cache)
1913 kmem_cache_destroy(ipn_msgitem_cache);
1914 if (ipn_node_cache)
1915 kmem_cache_destroy(ipn_node_cache);
1916 if (ipn_network_cache)
1917 kmem_cache_destroy(ipn_network_cache);
1918 ipn_msgbuf_fini();
1919 sock_unregister(PF_IPN);
1920 proto_unregister(&ipn_proto);
1921 printk(KERN_INFO "IPN removed\n");
1924 module_init(ipn_init);
1925 module_exit(ipn_exit);
1927 EXPORT_SYMBOL_GPL(ipn_proto_register);
1928 EXPORT_SYMBOL_GPL(ipn_proto_deregister);
1929 EXPORT_SYMBOL_GPL(ipn_proto_sendmsg);
1930 EXPORT_SYMBOL_GPL(ipn_proto_oobsendmsg);
1931 EXPORT_SYMBOL_GPL(ipn_msgpool_alloc);
1932 EXPORT_SYMBOL_GPL(ipn_msgpool_put);