BUGFIX: one wrong TAG for 2.6.39 ifdef
[vde.git] / ipn / af_ipn.c
blobd0d1e005c1ba474fb420960501480aef4c297910
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,30)
45 #define IPN_PRE2630
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,33)
51 #define IPN_PRE2633
52 #endif
53 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,37)
54 #define IPN_PRE2637
55 #endif
56 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,39)
57 #define IPN_PRE2639
58 #endif
60 /*extension of RCV_SHUTDOWN defined in include/net/sock.h
61 * when the bit is set recv fails */
62 /* NO_OOB: do not send OOB */
63 #define RCV_SHUTDOWN_NO_OOB 4
64 /* EXTENDED MASK including OOB */
65 #define SHUTDOWN_XMASK (SHUTDOWN_MASK | RCV_SHUTDOWN_NO_OOB)
66 /* if XRCV_SHUTDOWN is all set recv fails */
67 #define XRCV_SHUTDOWN (RCV_SHUTDOWN | RCV_SHUTDOWN_NO_OOB)
69 /* Global MUTEX: this is locked to add/delete/modify networks
70 * this is *not* locked just to send/receive msgs */
71 #ifdef IPN_PRE2637
72 static DECLARE_MUTEX(ipn_glob_mutex);
73 #else
74 static DEFINE_SEMAPHORE(ipn_glob_mutex);
75 #endif
76 /* Network table and hash */
77 struct hlist_head ipn_network_table[IPN_HASH_SIZE + 1];
78 /* slab(s) for fast data structure allocation */
79 static struct kmem_cache *ipn_network_cache;
80 static struct kmem_cache *ipn_node_cache;
81 static struct kmem_cache *ipn_msgitem_cache;
83 /* Protocol 1: HUB/Broadcast default protocol. Function Prototypes */
84 static int ipn_bcast_newport(struct ipn_node *newport);
85 static int ipn_bcast_handlemsg(struct ipn_node *from,
86 struct msgpool_item *msgitem);
88 /* default protocol IPN_BROADCAST (0) */
89 static struct ipn_protocol ipn_bcast = {
90 .refcnt=0,
91 .ipn_p_newport=ipn_bcast_newport,
92 .ipn_p_handlemsg=ipn_bcast_handlemsg};
93 /* Protocol table */
94 static struct ipn_protocol *ipn_protocol_table[IPN_MAX_PROTO]={&ipn_bcast};
96 /* Socket call function prototypes */
97 static int ipn_release(struct socket *);
98 static int ipn_bind(struct socket *, struct sockaddr *, int);
99 static int ipn_connect(struct socket *, struct sockaddr *,
100 int addr_len, int flags);
101 static int ipn_getname(struct socket *, struct sockaddr *, int *, int);
102 static unsigned int ipn_poll(struct file *, struct socket *, poll_table *);
103 static int ipn_ioctl(struct socket *, unsigned int, unsigned long);
104 static int ipn_shutdown(struct socket *, int);
105 static int ipn_sendmsg(struct kiocb *, struct socket *,
106 struct msghdr *, size_t);
107 static int ipn_recvmsg(struct kiocb *, struct socket *,
108 struct msghdr *, size_t, int);
109 #ifndef IPN_PRE2632
110 static int ipn_setsockopt(struct socket *sock, int level, int optname,
111 char __user *optval, unsigned int optlen);
112 #else
113 static int ipn_setsockopt(struct socket *sock, int level, int optname,
114 char __user *optval, int optlen);
115 #endif
116 static int ipn_getsockopt(struct socket *sock, int level, int optname,
117 char __user *optval, int __user *optlen);
119 /* Network table Management
120 * inode->ipn_network hash table
121 * protected by ipn_glob_mutex */
122 static inline void ipn_insert_network(struct hlist_head *list, struct ipn_network *ipnn)
124 hlist_add_head(&ipnn->hnode, list);
127 static inline void ipn_remove_network(struct ipn_network *ipnn)
129 hlist_del(&ipnn->hnode);
132 static struct ipn_network *ipn_find_network_byinode(struct inode *i)
134 struct ipn_network *ipnn;
135 struct hlist_node *node;
137 hlist_for_each_entry(ipnn, node,
138 &ipn_network_table[i->i_ino & (IPN_HASH_SIZE - 1)], hnode) {
139 struct dentry *dentry = ipnn->dentry;
141 if(dentry && dentry->d_inode == i)
142 return ipnn;
144 return NULL;
147 struct ipn_network *ipn_find_network_byfun(
148 int (*fun)(struct ipn_network *,void *),void *funarg)
150 struct ipn_network *ipnn;
151 struct hlist_node *node;
152 int ipn_table_scan;
154 for (ipn_table_scan=0;ipn_table_scan<IPN_HASH_SIZE;ipn_table_scan++) {
155 hlist_for_each_entry(ipnn, node, &ipn_network_table[ipn_table_scan], hnode) {
156 if(fun(ipnn,funarg))
157 return ipnn;
160 return NULL;
163 /* msgpool management
164 * msgpool_item are ipn_network dependent (each net has its own MTU)
165 * for each message sent there is one msgpool_item and many struct msgitem
166 * one for each receipient.
167 * msgitem are connected to the node's msgqueue or oobmsgqueue.
168 * when a message is delivered to a process the msgitem is deleted and
169 * the count of the msgpool_item is decreased.
170 * msgpool_item elements gets deleted automatically when count is 0*/
172 struct msgitem {
173 struct list_head list;
174 struct msgpool_item *msg;
177 /* alloc a fresh msgpool item. count is set to 1.
178 * the typical use is
179 * ipn_msgpool_alloc
180 * for each receipient
181 * enqueue messages to the process (using msgitem), ipn_msgpool_hold
182 * ipn_msgpool_put
183 * The message can be delivered concurrently. init count to 1 guarantees
184 * that it survives at least until is has been enqueued to all
185 * receivers */
186 /* msgpool_cache == NULL when the ipn network is using the flexible allocation,
187 * i.e. the msgpool item gets allocated by kmalloc instead of using slab */
188 static inline struct msgpool_item *_ipn_msgpool_alloc(struct ipn_network *ipnn, int len)
190 struct msgpool_item *new;
191 if ((new=ipnn->msgpool_cache?
192 kmem_cache_alloc(ipnn->msgpool_cache,GFP_KERNEL):
193 kmalloc(sizeof(struct msgpool_item)+len,GFP_KERNEL)) != NULL) {
194 atomic_set(&new->count,1);
195 atomic_inc(&ipnn->msgpool_nelem);
197 return new;
200 /* ipn_msgpool_alloc returns null when leaky, lossless and no space is left
201 * for new msgpool items. When !leaky, it can allocate more msgpooli items,
202 * this temporary overassignment blocks new messages to be delivered until
203 * there is space for a msgpool item to be allocated */
204 struct msgpool_item *ipn_msgpool_alloc(struct ipn_network *ipnn,int leaky,int len)
206 if (leaky && (ipnn->flags & IPN_FLAG_LOSSLESS) &&
207 atomic_read(&ipnn->msgpool_nelem) < ipnn->msgpool_size)
208 return NULL;
209 else
210 return _ipn_msgpool_alloc(ipnn,len);
213 /* If the service il LOSSLESS, this msgpool call waits for an
214 * available msgpool item */
215 static inline struct msgpool_item *ipn_msgpool_alloc_locking(struct ipn_network *ipnn,int len)
217 if (ipnn->flags & IPN_FLAG_LOSSLESS) {
218 while (atomic_read(&ipnn->msgpool_nelem) >= ipnn->msgpool_size) {
219 if (wait_event_interruptible_exclusive(ipnn->send_wait,
220 atomic_read(&ipnn->msgpool_nelem) < ipnn->msgpool_size))
221 return NULL;
224 return _ipn_msgpool_alloc(ipnn,len);
227 /* register one more user for this msgpool item (i.e. count++) */
228 static inline void ipn_msgpool_hold(struct msgpool_item *msg)
230 atomic_inc(&msg->count);
233 /* decrease count and delete msgpool_item if count == 0 */
234 void ipn_msgpool_put(struct msgpool_item *old,
235 struct ipn_network *ipnn)
237 if (atomic_dec_and_test(&old->count)) {
238 if (ipnn->msgpool_cache)
239 kmem_cache_free(ipnn->msgpool_cache,old);
240 else
241 kfree(old);
242 atomic_dec(&ipnn->msgpool_nelem);
243 if (ipnn->flags & IPN_FLAG_LOSSLESS) /* this could be done anyway */
244 wake_up_interruptible(&ipnn->send_wait);
248 /* socket calls */
249 static const struct proto_ops ipn_ops = {
250 .family = PF_IPN,
251 .owner = THIS_MODULE,
252 .release = ipn_release,
253 .bind = ipn_bind,
254 .connect = ipn_connect,
255 .socketpair = sock_no_socketpair,
256 .accept = sock_no_accept,
257 .getname = ipn_getname,
258 .poll = ipn_poll,
259 .ioctl = ipn_ioctl,
260 .listen = sock_no_listen,
261 .shutdown = ipn_shutdown,
262 .setsockopt = ipn_setsockopt,
263 .getsockopt = ipn_getsockopt,
264 .sendmsg = ipn_sendmsg,
265 .recvmsg = ipn_recvmsg,
266 .mmap = sock_no_mmap,
267 .sendpage = sock_no_sendpage,
270 static struct proto ipn_proto = {
271 .name = "IPN",
272 .owner = THIS_MODULE,
273 .obj_size = sizeof(struct ipn_sock),
276 /* create a new ipn_node */
277 struct ipn_node *ipn_node_create(struct net *net)
279 struct ipn_node *ipn_node;
280 ipn_node=kmem_cache_alloc(ipn_node_cache,GFP_KERNEL);
281 if (ipn_node!=NULL) {
282 INIT_LIST_HEAD(&ipn_node->nodelist);
283 ipn_node->protocol=0; /* BROADCAST, caller must set a proper value */
284 ipn_node->flags=IPN_NODEFLAG_INUSE;
285 ipn_node->shutdown=RCV_SHUTDOWN_NO_OOB;
286 ipn_node->descr[0]=0;
287 ipn_node->portno=IPN_PORTNO_ANY;
288 ipn_node->net=net;
289 ipn_node->netdev=NULL;
290 ipn_node->chrdev=0;
291 ipn_node->proto_private=NULL;
292 ipn_node->totmsgcount=0;
293 ipn_node->oobmsgcount=0;
294 spin_lock_init(&ipn_node->msglock);
295 INIT_LIST_HEAD(&ipn_node->msgqueue);
296 INIT_LIST_HEAD(&ipn_node->oobmsgqueue);
297 ipn_node->ipn=NULL;
298 init_waitqueue_head(&ipn_node->read_wait);
299 ipn_node->pbp=NULL;
301 return ipn_node;
304 /* create a socket
305 * ipn_node is a separate structure, pointed by ipn_sock -> node
306 * when a node is "persistent", ipn_node survives while ipn_sock gets released*/
307 #ifdef IPN_PRE2633
308 static int ipn_create(struct net *net,struct socket *sock, int protocol)
309 #else
310 static int ipn_create(struct net *net,struct socket *sock,
311 int protocol, int kern)
312 #endif
314 struct ipn_sock *ipn_sk;
316 if (net != &init_net)
317 return -EAFNOSUPPORT;
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 ipn_sk = (struct ipn_sock *) sk_alloc(net, PF_IPN, GFP_KERNEL, &ipn_proto);
330 if (!ipn_sk)
331 return -ENOMEM;
332 if ((ipn_sk->node=ipn_node_create(net))==NULL) {
333 sock_put((struct sock *) ipn_sk);
334 return -ENOMEM;
336 ipn_sk->node->protocol=protocol;
337 sock_init_data(sock,(struct sock *) ipn_sk);
338 sock->state = SS_UNCONNECTED;
339 sock->ops = &ipn_ops;
340 sock->sk=(struct sock *)ipn_sk;
341 return 0;
344 /* update # of readers and # of writers counters for an ipn network.
345 * This function sends oob messages to nodes requesting the service */
346 /* LOCKING ipnn_mutex is locked */
347 static void ipn_net_update_counters(struct ipn_network *ipnn,
348 int chg_readers, int chg_writers) {
349 ipnn->numreaders += chg_readers;
350 ipnn->numwriters += chg_writers;
351 if (ipnn->mtu >= sizeof(struct numnode_oob))
353 struct msgpool_item *ipn_msg=_ipn_msgpool_alloc(ipnn,sizeof(struct numnode_oob));
354 if (ipn_msg) {
355 struct numnode_oob *oob_msg=(struct numnode_oob *)(ipn_msg->data);
356 struct ipn_node *ipn_node;
357 ipn_msg->len=sizeof(struct numnode_oob);
358 oob_msg->level=IPN_ANY;
359 oob_msg->tag=IPN_OOB_NUMNODE_TAG;
360 oob_msg->numreaders=ipnn->numreaders;
361 oob_msg->numwriters=ipnn->numwriters;
362 list_for_each_entry(ipn_node, &ipnn->connectqueue, nodelist) {
363 if (ipn_node->flags & IPN_NODEFLAG_OOB_NUMNODES)
364 ipn_proto_oobsendmsg(ipn_node,ipn_msg);
366 ipn_msgpool_put(ipn_msg,ipnn);
371 /* flush pending messages (for close and shutdown RCV) */
372 /* LOCKING: ipnn_mutex is locked */
373 static void ipn_flush_recvqueue(struct ipn_node *ipn_node)
375 struct ipn_network *ipnn=ipn_node->ipn;
376 spin_lock(&ipn_node->msglock);
377 while (!list_empty(&ipn_node->msgqueue)) {
378 struct msgitem *msgitem=
379 list_first_entry(&ipn_node->msgqueue, struct msgitem, list);
380 list_del(&msgitem->list);
381 ipn_node->totmsgcount--;
382 ipn_msgpool_put(msgitem->msg,ipnn);
383 kmem_cache_free(ipn_msgitem_cache,msgitem);
385 spin_unlock(&ipn_node->msglock);
388 /* flush pending oob messages (for socket close) */
389 /* LOCKING: ipnn_mutex is locked */
390 static void ipn_flush_oobrecvqueue(struct ipn_node *ipn_node)
392 struct ipn_network *ipnn=ipn_node->ipn;
393 spin_lock(&ipn_node->msglock);
394 while (!list_empty(&ipn_node->oobmsgqueue)) {
395 struct msgitem *msgitem=
396 list_first_entry(&ipn_node->oobmsgqueue, struct msgitem, list);
397 list_del(&msgitem->list);
398 ipn_node->totmsgcount--;
399 ipn_node->oobmsgcount--;
400 ipn_msgpool_put(msgitem->msg,ipnn);
401 kmem_cache_free(ipn_msgitem_cache,msgitem);
403 spin_unlock(&ipn_node->msglock);
406 /* Terminate node. The node is "logically" terminated. */
407 /* LOCKING: ipn_glob_lock must be locked here */
408 static int ipn_terminate_node(struct ipn_node *ipn_node)
410 struct ipn_network *ipnn=ipn_node->ipn;
411 if (ipnn) {
412 if (down_interruptible(&ipnn->ipnn_mutex))
413 return -ERESTARTSYS;
414 if (ipn_node->portno >= 0) {
415 ipn_protocol_table[ipnn->protocol]->ipn_p_predelport(ipn_node);
416 ipnn->connport[ipn_node->portno]=NULL;
418 list_del(&ipn_node->nodelist);
419 ipn_flush_recvqueue(ipn_node);
420 ipn_flush_oobrecvqueue(ipn_node);
421 if (ipn_node->portno >= 0)
422 ipn_protocol_table[ipnn->protocol]->ipn_p_delport(ipn_node);
423 ipn_node->ipn=NULL;
424 ipn_net_update_counters(ipnn,
425 (ipn_node->shutdown & RCV_SHUTDOWN)?0:-1,
426 (ipn_node->shutdown & SEND_SHUTDOWN)?0:-1);
427 ipn_node->shutdown = SHUTDOWN_XMASK;
428 up(&ipnn->ipnn_mutex);
429 if (ipn_node->netdev)
430 ipn_netdev_close(ipn_node);
431 /* No more network elements */
432 ipnn->refcnt--;
433 if (ipnn->refcnt == 0 && !ipn_is_persistent_chrdev(ipnn))
435 if (ipnn->chrdev)
436 ipn_deregister_chrdev(ipnn);
437 ipn_protocol_table[ipnn->protocol]->ipn_p_delnet(ipnn);
438 ipn_remove_network(ipnn);
439 ipn_protocol_table[ipnn->protocol]->refcnt--;
440 if (ipnn->dentry) {
441 dput(ipnn->dentry);
442 mntput(ipnn->mnt);
444 if (ipnn->msgpool_cache)
445 ipn_msgbuf_put(ipnn->msgpool_cache);
446 if (ipnn->connport)
447 kfree(ipnn->connport);
448 kmem_cache_free(ipn_network_cache, ipnn);
449 module_put(THIS_MODULE);
452 if (ipn_node->pbp) {
453 kfree(ipn_node->pbp);
454 ipn_node->pbp=NULL;
456 return 0;
459 /* release of an ipn_node */
460 int ipn_node_release(struct ipn_node *ipn_node)
462 int rv;
463 if (down_interruptible(&ipn_glob_mutex))
464 return -ERESTARTSYS;
465 if (ipn_node->flags & IPN_NODEFLAG_PERSIST) {
466 ipn_node->flags &= ~IPN_NODEFLAG_INUSE;
467 rv=0;
468 up(&ipn_glob_mutex);
469 } else {
470 rv=ipn_terminate_node(ipn_node);
471 up(&ipn_glob_mutex);
472 if (rv==0) {
473 ipn_netdevsync();
474 kmem_cache_free(ipn_node_cache,ipn_node);
477 return rv;
480 /* release of an ipn socket */
481 static int ipn_release (struct socket *sock)
483 struct ipn_sock *ipn_sk=(struct ipn_sock *)sock->sk;
484 struct ipn_node *ipn_node=ipn_sk->node;
485 int rv=ipn_node_release(ipn_node);
486 if (rv == 0)
487 sock_put((struct sock *) ipn_sk);
488 return rv;
491 /* _set persist, change the persistence of a node,
492 * when persistence gets cleared and the node is no longer used
493 * the node is terminated and freed.
494 * ipn_glob_mutex must be locked */
495 static int _ipn_setpersist(struct ipn_node *ipn_node, int persist)
497 int rv=0;
498 if (persist)
499 ipn_node->flags |= IPN_NODEFLAG_PERSIST;
500 else {
501 ipn_node->flags &= ~IPN_NODEFLAG_PERSIST;
502 if (!(ipn_node->flags & IPN_NODEFLAG_INUSE)) {
503 rv=ipn_terminate_node(ipn_node);
504 if (rv==0)
505 kmem_cache_free(ipn_node_cache,ipn_node);
508 return rv;
511 /* ipn_setpersist
512 * lock ipn_glob_mutex and call __ipn_setpersist above */
513 static int ipn_setpersist(struct ipn_node *ipn_node, int persist)
515 int rv=0;
516 if (ipn_node->netdev == NULL)
517 return -ENODEV;
518 if (down_interruptible(&ipn_glob_mutex))
519 return -ERESTARTSYS;
520 rv=_ipn_setpersist(ipn_node,persist);
521 up(&ipn_glob_mutex);
522 return rv;
525 /* several network parameters can be set by setsockopt prior to bind */
526 /* struct pre_bind_parms is a temporary stucture connected to ipn_node->pbp
527 * to keep the parameter values. */
528 struct pre_bind_parms {
529 unsigned short maxports;
530 unsigned short flags;
531 unsigned short msgpoolsize;
532 unsigned short mtu;
533 unsigned short mode;
536 /* STD_PARMS: BITS_PER_LONG nodes, no flags, BITS_PER_BYTE pending msgs,
537 * Ethernet + VLAN MTU*/
538 #define STD_BIND_PARMS {BITS_PER_LONG, 0, BITS_PER_BYTE, 1514, 0777};
540 static int ipn_mkname(struct sockaddr_un * sunaddr, int len)
542 if (len <= sizeof(short) || len > sizeof(*sunaddr))
543 return -EINVAL;
544 if (!sunaddr || sunaddr->sun_family != AF_IPN)
545 return -EINVAL;
547 * This may look like an off by one error but it is a bit more
548 * subtle. 108 is the longest valid AF_IPN path for a binding.
549 * sun_path[108] doesnt as such exist. However in kernel space
550 * we are guaranteed that it is a valid memory location in our
551 * kernel address buffer.
553 ((char *)sunaddr)[len]=0;
554 len = strlen(sunaddr->sun_path)+1+sizeof(short);
555 return len;
558 static int ipn_node_bind(struct ipn_node *ipn_node, struct ipn_network *ipnn)
560 if (ipnn == NULL)
561 return -ENOENT;
562 if (ipn_node->ipn != NULL)
563 return -EISCONN;
564 ipnn->refcnt++;
565 list_add_tail(&ipn_node->nodelist,&ipnn->unconnectqueue);
566 ipn_node->ipn=ipnn;
567 ipn_node->flags |= IPN_NODEFLAG_BOUND;
568 return 0;
571 /* IPN BIND */
572 static int ipn_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
574 struct sockaddr_un *sunaddr=(struct sockaddr_un *)uaddr;
575 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
576 struct nameidata nd;
577 struct ipn_network *ipnn;
578 struct dentry * dentry = NULL;
579 int err;
580 struct pre_bind_parms parms=STD_BIND_PARMS;
582 //printk("IPN bind\n");
584 if (down_interruptible(&ipn_glob_mutex))
585 return -ERESTARTSYS;
586 if (sock->state != SS_UNCONNECTED ||
587 ipn_node->ipn != NULL) {
588 err= -EISCONN;
589 goto out;
592 if (ipn_node->protocol >= 0 &&
593 (ipn_node->protocol >= IPN_MAX_PROTO ||
594 ipn_protocol_table[ipn_node->protocol] == NULL)) {
595 err= -EPROTONOSUPPORT;
596 goto out;
599 addr_len = ipn_mkname(sunaddr, addr_len);
600 if (addr_len < 0) {
601 err=addr_len;
602 goto out;
605 /* check if there is already an ipn-network socket with that name */
606 #ifndef IPN_PRE2639
607 err = kern_path(sunaddr->sun_path, LOOKUP_FOLLOW, &nd.path);
608 #else
609 err = path_lookup(sunaddr->sun_path, LOOKUP_FOLLOW, &nd);
610 #endif
611 if (err) { /* it does not exist, NEW IPN socket! */
612 unsigned int mode;
613 /* Is it everything okay with the parent? */
614 #ifndef IPN_PRE2639
615 err = kern_path_parent(sunaddr->sun_path, &nd);
616 #else
617 err = path_lookup(sunaddr->sun_path, LOOKUP_PARENT, &nd);
618 #endif
619 if (err)
620 goto out_mknod_parent;
621 /* Do I have the permission to create a file? */
622 dentry = lookup_create(&nd, 0);
623 err = PTR_ERR(dentry);
624 if (IS_ERR(dentry))
625 goto out_mknod_unlock;
627 * All right, let's create it.
629 if (ipn_node->pbp)
630 mode = ipn_node->pbp->mode;
631 else
632 mode = SOCK_INODE(sock)->i_mode;
633 #ifdef IPN_PRE2630
634 mode = S_IFSOCK | (mode & ~current->fs->umask);
635 #else
636 mode = S_IFSOCK | (mode & ~current_umask());
637 #endif
638 #ifdef APPARMOR
639 err = vfs_mknod(nd.path.dentry->d_inode, dentry, nd.path.mnt, mode, 0);
640 #else
641 err = vfs_mknod(nd.path.dentry->d_inode, dentry, mode, 0);
642 #endif
643 if (err)
644 goto out_mknod_dput;
645 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
646 dput(nd.path.dentry);
647 nd.path.dentry = dentry;
648 /* create a new ipn_network item */
649 if (ipn_node->pbp)
650 parms=*ipn_node->pbp;
651 ipnn=kmem_cache_zalloc(ipn_network_cache,GFP_KERNEL);
652 if (!ipnn) {
653 err=-ENOMEM;
654 goto out_mknod_dput_ipnn;
656 ipnn->connport=kzalloc(parms.maxports * sizeof(struct ipn_node *),GFP_KERNEL);
657 if (!ipnn->connport) {
658 err=-ENOMEM;
659 goto out_mknod_dput_ipnn2;
662 /* module refcnt is incremented for each network, thus
663 * rmmod is forbidden if there are persistent nodes */
664 if (!try_module_get(THIS_MODULE)) {
665 err = -EINVAL;
666 goto out_mknod_dput_ipnn2;
668 memcpy(&ipnn->sunaddr,sunaddr,addr_len);
669 ipnn->mtu=parms.mtu;
670 ipnn->flags=parms.flags;
671 if (ipnn->flags & IPN_FLAG_FLEXMTU)
672 ipnn->msgpool_cache= NULL;
673 else {
674 ipnn->msgpool_cache=ipn_msgbuf_get(ipnn->mtu);
675 if (!ipnn->msgpool_cache) {
676 err=-ENOMEM;
677 goto out_mknod_dput_putmodule;
680 INIT_LIST_HEAD(&ipnn->unconnectqueue);
681 INIT_LIST_HEAD(&ipnn->connectqueue);
682 ipnn->refcnt=0;
683 ipnn->dentry=nd.path.dentry;
684 ipnn->mnt=nd.path.mnt;
685 sema_init(&ipnn->ipnn_mutex,1);
686 ipnn->sunaddr_len=addr_len;
687 ipnn->protocol=ipn_node->protocol;
688 if (ipnn->protocol < 0) ipnn->protocol = 0;
689 ipn_protocol_table[ipnn->protocol]->refcnt++;
690 ipnn->numreaders=0;
691 ipnn->numwriters=0;
692 ipnn->maxports=parms.maxports;
693 atomic_set(&ipnn->msgpool_nelem,0);
694 ipnn->msgpool_size=parms.msgpoolsize;
695 ipnn->proto_private=NULL;
696 init_waitqueue_head(&ipnn->send_wait);
697 ipnn->chrdev=NULL;
698 err=ipn_protocol_table[ipnn->protocol]->ipn_p_newnet(ipnn);
699 if (err)
700 goto out_mknod_dput_putmodule;
701 ipn_insert_network(&ipn_network_table[nd.path.dentry->d_inode->i_ino & (IPN_HASH_SIZE-1)],ipnn);
702 } else {
703 /* join an existing network */
704 if (parms.flags & IPN_FLAG_EXCL) {
705 err=-EEXIST;
706 goto put_fail;
708 err = inode_permission(nd.path.dentry->d_inode, MAY_EXEC);
709 if (err)
710 goto put_fail;
711 err = -ECONNREFUSED;
712 if (!S_ISSOCK(nd.path.dentry->d_inode->i_mode))
713 goto put_fail;
714 ipnn=ipn_find_network_byinode(nd.path.dentry->d_inode);
715 if (!ipnn || (ipnn->flags & IPN_FLAG_TERMINATED) ||
716 (ipnn->flags & IPN_FLAG_EXCL))
717 goto put_fail;
719 if (ipn_node->pbp) {
720 kfree(ipn_node->pbp);
721 ipn_node->pbp=NULL;
723 ipn_node_bind(ipn_node,ipnn);
724 up(&ipn_glob_mutex);
725 return 0;
727 put_fail:
728 path_put(&nd.path);
729 out:
730 up(&ipn_glob_mutex);
731 return err;
733 out_mknod_dput_putmodule:
734 module_put(THIS_MODULE);
735 out_mknod_dput_ipnn2:
736 kfree(ipnn->connport);
737 out_mknod_dput_ipnn:
738 kmem_cache_free(ipn_network_cache,ipnn);
739 out_mknod_dput:
740 dput(dentry);
741 out_mknod_unlock:
742 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
743 path_put(&nd.path);
744 out_mknod_parent:
745 if (err==-EEXIST)
746 err=-EADDRINUSE;
747 up(&ipn_glob_mutex);
748 return err;
751 /* IPN CONNECT */
752 static int ipn_connect(struct socket *sock, struct sockaddr *addr,
753 int addr_len, int flags){
754 struct sockaddr_un *sunaddr=(struct sockaddr_un*)addr;
755 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
756 #ifndef IPN_PRE2639
757 struct path ndpath;
758 #else
759 struct nameidata nd;
760 #define ndpath nd.path
761 #endif
763 struct ipn_network *ipnn,*previousipnn;
764 int err=0;
765 int portno;
767 /* the socket cannot be connected twice */
768 if (sock->state != SS_UNCONNECTED)
769 return EISCONN;
771 if (down_interruptible(&ipn_glob_mutex))
772 return -ERESTARTSYS;
774 if ((previousipnn=ipn_node->ipn) == NULL) { /* unbound */
775 unsigned char mustshutdown=0;
776 err = ipn_mkname(sunaddr, addr_len);
777 if (err < 0)
778 goto out;
779 addr_len=err;
780 #ifndef IPN_PRE2639
781 err = kern_path(sunaddr->sun_path, LOOKUP_FOLLOW, &ndpath);
782 #else
783 err = path_lookup(sunaddr->sun_path, LOOKUP_FOLLOW, &nd);
784 #endif
785 if (err)
786 goto out;
787 err = inode_permission(ndpath.dentry->d_inode, MAY_READ);
788 if (err) {
789 if (err == -EACCES || err == -EROFS)
790 mustshutdown|=RCV_SHUTDOWN;
791 else
792 goto put_fail;
794 err = inode_permission(ndpath.dentry->d_inode, MAY_WRITE);
795 if (err) {
796 if (err == -EACCES)
797 mustshutdown|=SEND_SHUTDOWN;
798 else
799 goto put_fail;
801 mustshutdown |= ipn_node->shutdown;
802 /* if the combination of shutdown and permissions leaves
803 * no abilities, connect returns EACCES */
804 if (mustshutdown == SHUTDOWN_XMASK) {
805 err=-EACCES;
806 goto put_fail;
807 } else {
808 err=0;
809 ipn_node->shutdown=mustshutdown;
811 if (!S_ISSOCK(ndpath.dentry->d_inode->i_mode)) {
812 err = -ECONNREFUSED;
813 goto put_fail;
815 ipnn=ipn_find_network_byinode(ndpath.dentry->d_inode);
816 if (!ipnn || (ipnn->flags & IPN_FLAG_TERMINATED)) {
817 err = -ECONNREFUSED;
818 goto put_fail;
820 if (ipn_node->protocol == IPN_ANY)
821 ipn_node->protocol=ipnn->protocol;
822 else if (ipnn->protocol != ipn_node->protocol) {
823 err = -EPROTO;
824 goto put_fail;
826 path_put(&ndpath);
827 ipn_node->ipn=ipnn;
828 } else
829 ipnn=ipn_node->ipn;
831 if (down_interruptible(&ipnn->ipnn_mutex)) {
832 err=-ERESTARTSYS;
833 goto out;
835 portno = ipn_protocol_table[ipnn->protocol]->ipn_p_newport(ipn_node);
836 if (portno >= 0 && portno<ipnn->maxports) {
837 sock->state = SS_CONNECTED;
838 ipn_node->portno=portno;
839 ipnn->connport[portno]=ipn_node;
840 if (!(ipn_node->flags & IPN_NODEFLAG_BOUND))
841 ipnn->refcnt++;
842 else
843 list_del(&ipn_node->nodelist);
844 list_add_tail(&ipn_node->nodelist,&ipnn->connectqueue);
845 ipn_net_update_counters(ipnn,
846 (ipn_node->shutdown & RCV_SHUTDOWN)?0:1,
847 (ipn_node->shutdown & SEND_SHUTDOWN)?0:1);
848 } else {
849 ipn_node->ipn=previousipnn; /* undo changes on ipn_node->ipn */
850 err=-EADDRNOTAVAIL;
852 up(&ipnn->ipnn_mutex);
853 up(&ipn_glob_mutex);
854 return err;
856 put_fail:
857 path_put(&ndpath);
858 out:
859 up(&ipn_glob_mutex);
860 return err;
863 int ipn_node_create_connect(struct ipn_node **ipn_node_out,
864 struct ipn_network *(* ipnn_map)(void *),void *ipnn_map_arg) {
865 struct ipn_node *ipn_node;
866 struct ipn_network *ipnn;
867 int err=0;
868 int portno;
869 ipn_node=ipn_node_create(&init_net);
870 if (down_interruptible(&ipn_glob_mutex)) {
871 err=-ERESTARTSYS;
872 goto err_ipn_node_release;
874 ipnn=ipnn_map(ipnn_map_arg);
875 ipn_node->ipn=ipnn;
876 ipn_node->protocol=ipnn->protocol;
877 if (down_interruptible(&ipnn->ipnn_mutex)) {
878 err=-ERESTARTSYS;
879 goto out_glob;
881 portno = ipn_protocol_table[ipnn->protocol]->ipn_p_newport(ipn_node);
882 if (portno >= 0 && portno<ipnn->maxports) {
883 ipn_node->portno=portno;
884 ipnn->connport[portno]=ipn_node;
885 if (!(ipn_node->flags & IPN_NODEFLAG_BOUND)) {
886 ipnn->refcnt++;
887 list_del(&ipn_node->nodelist);
889 *ipn_node_out=ipn_node;
890 list_add_tail(&ipn_node->nodelist,&ipnn->connectqueue);
891 ipn_net_update_counters(ipnn,
892 (ipn_node->shutdown & RCV_SHUTDOWN)?0:1,
893 (ipn_node->shutdown & SEND_SHUTDOWN)?0:1);
894 } else {
895 ipn_node->ipn=NULL;
896 err=-EADDRNOTAVAIL;
897 goto out_glob_ipnn;
900 up(&ipnn->ipnn_mutex);
901 up(&ipn_glob_mutex);
902 return err;
903 out_glob_ipnn:
904 up(&ipnn->ipnn_mutex);
905 out_glob:
906 up(&ipn_glob_mutex);
907 err_ipn_node_release:
908 ipn_node_release(ipn_node);
909 ipn_node_out=NULL;
910 return err;
913 int ipn_node_connect(struct ipn_node *ipn_node)
915 struct ipn_network *ipnn;
916 int err=0;
917 int portno;
918 if (down_interruptible(&ipn_glob_mutex))
919 return -ERESTARTSYS;
921 ipnn=ipn_node->ipn;
922 if (down_interruptible(&ipnn->ipnn_mutex)) {
923 err=-ERESTARTSYS;
924 goto out;
926 portno = ipn_protocol_table[ipnn->protocol]->ipn_p_newport(ipn_node);
927 if (portno >= 0 && portno<ipnn->maxports) {
928 ipn_node->portno=portno;
929 ipnn->connport[portno]=ipn_node;
930 if (!(ipn_node->flags & IPN_NODEFLAG_BOUND)) {
931 ipnn->refcnt++;
932 list_del(&ipn_node->nodelist);
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;
943 up(&ipnn->ipnn_mutex);
944 up(&ipn_glob_mutex);
945 return err;
946 out:
947 up(&ipn_glob_mutex);
948 return err;
951 static int ipn_getname(struct socket *sock, struct sockaddr *uaddr,
952 int *uaddr_len, int peer) {
953 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
954 struct ipn_network *ipnn=ipn_node->ipn;
955 struct sockaddr_un *sunaddr=(struct sockaddr_un *)uaddr;
956 int err=0;
958 if (down_interruptible(&ipn_glob_mutex))
959 return -ERESTARTSYS;
960 if (ipnn) {
961 *uaddr_len = ipnn->sunaddr_len;
962 memcpy(sunaddr,&ipnn->sunaddr,*uaddr_len);
963 } else
964 err = -ENOTCONN;
965 up(&ipn_glob_mutex);
966 return err;
969 /* IPN POLL */
970 unsigned int ipn_node_poll(struct ipn_node *ipn_node, struct file *file, poll_table *wait) {
971 struct ipn_network *ipnn=ipn_node->ipn;
972 unsigned int mask=0;
974 if (ipnn) {
975 poll_wait(file,&ipn_node->read_wait,wait);
976 if (ipnn->flags & IPN_FLAG_LOSSLESS)
977 poll_wait(file,&ipnn->send_wait,wait);
978 /* POLLIN if recv succeeds,
979 * POLL{PRI,RDNORM} if there are {oob,non-oob} messages */
980 if (ipn_node->totmsgcount > 0) mask |= POLLIN;
981 if (!(list_empty(&ipn_node->msgqueue))) mask |= POLLRDNORM;
982 if (!(list_empty(&ipn_node->oobmsgqueue))) mask |= POLLPRI;
983 if ((!(ipnn->flags & IPN_FLAG_LOSSLESS)) |
984 (atomic_read(&ipnn->msgpool_nelem) < ipnn->msgpool_size))
985 mask |= POLLOUT | POLLWRNORM;
987 return mask;
990 static unsigned int ipn_poll(struct file *file, struct socket *sock,
991 poll_table *wait) {
992 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
993 return ipn_node_poll(ipn_node, file, wait);
996 /* connect netdev (from ioctl). connect a bound socket to a
997 * network device TAP or GRAB */
998 static int ipn_connect_netdev(struct socket *sock,struct ifreq *ifr)
1000 int err=0;
1001 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
1002 struct ipn_network *ipnn=ipn_node->ipn;
1003 if (!capable(CAP_NET_ADMIN))
1004 return -EPERM;
1005 if (sock->state != SS_UNCONNECTED)
1006 return -EISCONN;
1007 if (!ipnn)
1008 return -ENOTCONN; /* Maybe we need a different error for "NOT BOUND" */
1009 if (down_interruptible(&ipn_glob_mutex))
1010 return -ERESTARTSYS;
1011 if (down_interruptible(&ipnn->ipnn_mutex)) {
1012 up(&ipn_glob_mutex);
1013 return -ERESTARTSYS;
1015 ipn_node->netdev=ipn_netdev_alloc(ipn_node->net,ifr->ifr_flags,ifr->ifr_name,&err);
1016 if (ipn_node->netdev) {
1017 int portno;
1018 portno = ipn_protocol_table[ipnn->protocol]->ipn_p_newport(ipn_node);
1019 if (portno >= 0 && portno<ipnn->maxports) {
1020 sock->state = SS_CONNECTED;
1021 ipn_node->portno=portno;
1022 ipn_node->flags |= ifr->ifr_flags & IPN_NODEFLAG_DEVMASK;
1023 ipnn->connport[portno]=ipn_node;
1024 err=ipn_netdev_activate(ipn_node);
1025 if (err) {
1026 sock->state = SS_UNCONNECTED;
1027 ipn_protocol_table[ipnn->protocol]->ipn_p_delport(ipn_node);
1028 ipn_node->netdev=NULL;
1029 ipn_node->portno= -1;
1030 ipn_node->flags &= ~IPN_NODEFLAG_DEVMASK;
1031 ipnn->connport[portno]=NULL;
1032 } else {
1033 ipn_protocol_table[ipnn->protocol]->ipn_p_postnewport(ipn_node);
1034 list_del(&ipn_node->nodelist);
1035 list_add_tail(&ipn_node->nodelist,&ipnn->connectqueue);
1037 } else {
1038 ipn_netdev_close(ipn_node);
1039 err=-EADDRNOTAVAIL;
1040 ipn_node->netdev=NULL;
1042 } else
1043 err=-EINVAL;
1044 up(&ipnn->ipnn_mutex);
1045 up(&ipn_glob_mutex);
1046 return err;
1049 /* join a netdev, a socket gets connected to a persistent node
1050 * not connected to another socket */
1051 static int ipn_join_netdev(struct socket *sock,struct ifreq *ifr)
1053 int err=0;
1054 struct net_device *dev;
1055 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
1056 struct ipn_node *ipn_joined;
1057 struct ipn_network *ipnn=ipn_node->ipn;
1058 if (sock->state != SS_UNCONNECTED)
1059 return -EISCONN;
1060 if (down_interruptible(&ipn_glob_mutex))
1061 return -ERESTARTSYS;
1062 if (down_interruptible(&ipnn->ipnn_mutex)) {
1063 up(&ipn_glob_mutex);
1064 return -ERESTARTSYS;
1066 dev=__dev_get_by_name(ipn_node->net,ifr->ifr_name);
1067 if (!dev)
1068 dev=__dev_get_by_index(ipn_node->net,ifr->ifr_ifindex);
1069 if (dev && (ipn_joined=ipn_netdev2node(dev)) != NULL) { /* the interface does exist */
1070 int i;
1071 for (i=0;i<ipnn->maxports && ipn_joined != ipnn->connport[i] ;i++)
1073 if (i < ipnn->maxports) { /* found */
1074 /* ipn_joined is substituted to ipn_node */
1075 ((struct ipn_sock *)sock->sk)->node=ipn_joined;
1076 ipn_joined->flags |= IPN_NODEFLAG_INUSE;
1077 ipnn->refcnt--;
1078 kmem_cache_free(ipn_node_cache,ipn_node);
1079 } else
1080 err=-EPERM;
1081 } else
1082 err=-EADDRNOTAVAIL;
1083 up(&ipnn->ipnn_mutex);
1084 up(&ipn_glob_mutex);
1085 return err;
1088 /* set persistence of a node looking for it by interface name
1089 * (it is for sysadm, to close network interfaces)*/
1090 static int ipn_setpersist_netdev(struct ifreq *ifr, int value)
1092 struct net_device *dev;
1093 struct ipn_node *ipn_node;
1094 int err=0;
1095 if (!capable(CAP_NET_ADMIN))
1096 return -EPERM;
1097 if (down_interruptible(&ipn_glob_mutex))
1098 return -ERESTARTSYS;
1099 dev=__dev_get_by_name(&init_net,ifr->ifr_name);
1100 if (!dev)
1101 dev=__dev_get_by_index(&init_net,ifr->ifr_ifindex);
1102 if (dev && (ipn_node=ipn_netdev2node(dev)) != NULL)
1103 _ipn_setpersist(ipn_node,value);
1104 else
1105 err=-EADDRNOTAVAIL;
1106 up(&ipn_glob_mutex);
1107 return err;
1110 /* IPN IOCTL */
1112 int ipn_node_ioctl(struct ipn_node *ipn_node, unsigned int cmd, unsigned long arg) {
1113 struct ipn_network *ipnn=ipn_node->ipn;
1114 if (ipnn &&
1115 (ipn_protocol_table[ipn_node->protocol]->ipn_p_ioctl != NULL)) {
1116 int rv;
1117 if (down_interruptible(&ipnn->ipnn_mutex))
1118 return -ERESTARTSYS;
1119 rv=ipn_protocol_table[ipn_node->protocol]->ipn_p_ioctl(ipn_node,cmd,arg);
1120 up(&ipnn->ipnn_mutex);
1121 return rv;
1122 } else
1123 return -EOPNOTSUPP;
1126 static int ipn_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) {
1127 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
1128 struct ipn_network *ipnn=ipn_node->ipn;
1129 void __user* argp = (void __user*)arg;
1130 struct ifreq ifr;
1131 struct chrdevreq devr;
1133 if (ipn_node->shutdown == SHUTDOWN_XMASK)
1134 return -ECONNRESET;
1136 /* get arguments */
1137 switch (cmd) {
1138 case IPN_CHECK:
1139 return IPN_CHECK;
1140 case IPN_SETPERSIST_NETDEV:
1141 case IPN_CLRPERSIST_NETDEV:
1142 case IPN_CONN_NETDEV:
1143 case IPN_JOIN_NETDEV:
1144 case SIOCSIFHWADDR:
1145 if (copy_from_user(&ifr, argp, sizeof(ifr)))
1146 return -EFAULT;
1147 ifr.ifr_name[IFNAMSIZ-1] = '\0';
1148 break;
1149 case IPN_REGISTER_CHRDEV:
1150 case IPN_JOIN_CHRDEV:
1151 if (copy_from_user(&devr, argp, sizeof(devr)))
1152 return -EFAULT;
1153 /*printk("IPN_REGISTER_CHRDEV %p %d %d %d\n",
1154 argp, sizeof(devr),devr.major, devr.minor);*/
1155 break;
1158 /* actions for unconnected and unbound sockets */
1159 switch (cmd) {
1160 case IPN_SETPERSIST_NETDEV:
1161 return ipn_setpersist_netdev(&ifr,1);
1162 case IPN_CLRPERSIST_NETDEV:
1163 return ipn_setpersist_netdev(&ifr,0);
1164 case IPN_JOIN_CHRDEV:
1166 int rv;
1167 if (!capable(CAP_MKNOD))
1168 return -EPERM;
1169 if (ipn_node->ipn != NULL)
1170 return -EISCONN;
1171 if (down_interruptible(&ipn_glob_mutex))
1172 return -ERESTARTSYS;
1173 rv=ipn_node_bind(ipn_node, ipn_find_chrdev(&devr));
1174 up(&ipn_glob_mutex);
1175 return rv;
1177 case SIOCSIFHWADDR:
1178 if (capable(CAP_NET_ADMIN))
1179 return -EPERM;
1180 if (ipn_node->netdev && (ipn_node->flags &IPN_NODEFLAG_TAP))
1181 return dev_set_mac_address(ipn_node->netdev, &ifr.ifr_hwaddr);
1182 else
1183 return -EADDRNOTAVAIL;
1185 if (ipnn == NULL || (ipnn->flags & IPN_FLAG_TERMINATED))
1186 return -ENOTCONN;
1187 /* actions for connected or bound sockets */
1188 switch (cmd) {
1189 case IPN_CONN_NETDEV:
1190 return ipn_connect_netdev(sock,&ifr);
1191 case IPN_JOIN_NETDEV:
1192 return ipn_join_netdev(sock,&ifr);
1193 case IPN_SETPERSIST:
1194 return ipn_setpersist(ipn_node,arg);
1195 case IPN_CHRDEV_PERSIST:
1196 return ipn_chrdev_persistence(ipnn,arg);
1197 case IPN_REGISTER_CHRDEV:
1199 int rv;
1200 unsigned int reqmajor=devr.major;
1201 if (down_interruptible(&ipnn->ipnn_mutex))
1202 return -ERESTARTSYS;
1203 rv=ipn_register_chrdev(ipnn,&devr);
1204 if (reqmajor==0 && rv==0) {
1205 if (copy_to_user(argp, &devr, sizeof devr))
1206 rv=EFAULT;
1208 up(&ipnn->ipnn_mutex);
1209 return rv;
1211 case IPN_UNREGISTER_CHRDEV:
1213 int rv;
1214 if (down_interruptible(&ipnn->ipnn_mutex))
1215 return -ERESTARTSYS;
1216 rv=ipn_deregister_chrdev(ipnn);
1217 up(&ipnn->ipnn_mutex);
1218 return rv;
1220 default:
1221 return ipn_node_ioctl(ipn_node, cmd, arg);
1225 /* shutdown: close socket for input or for output.
1226 * shutdown can be called prior to connect and it is not reversible */
1227 static int ipn_shutdown(struct socket *sock, int mode) {
1228 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
1229 struct ipn_network *ipnn=ipn_node->ipn;
1230 int oldshutdown=ipn_node->shutdown;
1231 mode = (mode+1)&(RCV_SHUTDOWN|SEND_SHUTDOWN);
1233 /* define the new mode first... */
1234 ipn_node->shutdown |= mode;
1236 if(ipnn) {
1237 /* ... and wait for all pending ops to be completed */
1238 if (down_interruptible(&ipnn->ipnn_mutex)) {
1239 ipn_node->shutdown = oldshutdown;
1240 return -ERESTARTSYS;
1242 oldshutdown=ipn_node->shutdown-oldshutdown;
1243 if (sock->state == SS_CONNECTED && oldshutdown) {
1244 ipn_net_update_counters(ipnn,
1245 (ipn_node->shutdown & RCV_SHUTDOWN)?0:-1,
1246 (ipn_node->shutdown & SEND_SHUTDOWN)?0:-1);
1249 /* if recv channel has been shut down, flush the recv queue */
1250 if ((ipn_node->shutdown & RCV_SHUTDOWN))
1251 ipn_flush_recvqueue(ipn_node);
1252 up(&ipnn->ipnn_mutex);
1254 return 0;
1257 /* injectmsg: a new message is entering the ipn network.
1258 * injectmsg gets called by send and by the grab/tap node */
1259 /* handlemsg is protected from ipn_network changes: ipnn->ipnn_mutex is locked */
1260 int ipn_proto_injectmsg(struct ipn_node *from, struct msgpool_item *msg)
1262 struct ipn_network *ipnn=from->ipn;
1263 int err=0;
1264 static int recinject=0;
1265 //printk("INJECTMSG IN\n");
1266 if (recinject)
1267 ipn_protocol_table[ipnn->protocol]->ipn_p_handlemsg(from, msg);
1268 else if (down_interruptible(&ipnn->ipnn_mutex))
1269 err=-ERESTARTSYS;
1270 else {
1271 recinject=1;
1272 ipn_protocol_table[ipnn->protocol]->ipn_p_handlemsg(from, msg);
1273 recinject=0;
1274 up(&ipnn->ipnn_mutex);
1276 //printk("INJECTMSG OUT %d\n",err);
1277 return err;
1280 /* SEND MSG */
1281 int ipn_node_write(struct ipn_node *ipn_node, struct iovec *msg_iov, int len) {
1282 struct ipn_network *ipnn=ipn_node->ipn;
1283 struct msgpool_item *newmsg;
1284 int err=0;
1286 if (unlikely(ipn_node->shutdown & SEND_SHUTDOWN)) {
1287 if (ipn_node->shutdown == SHUTDOWN_XMASK)
1288 return -ECONNRESET;
1289 else
1290 return -EPIPE;
1292 if (len > ipnn->mtu)
1293 return -EOVERFLOW;
1294 newmsg=ipn_msgpool_alloc_locking(ipnn,len);
1295 if (!newmsg)
1296 return -ENOMEM;
1297 newmsg->len=len;
1298 err=memcpy_fromiovec(newmsg->data, msg_iov, len);
1299 if (!err)
1300 ipn_proto_injectmsg(ipn_node, newmsg);
1301 ipn_msgpool_put(newmsg,ipnn);
1302 return len;
1305 static int ipn_sendmsg(struct kiocb *kiocb, struct socket *sock,
1306 struct msghdr *msg, size_t len) {
1307 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
1309 if (unlikely(sock->state != SS_CONNECTED))
1310 return -ENOTCONN;
1311 else
1312 return ipn_node_write(ipn_node, msg->msg_iov, len);
1315 /* ipn_proto_sendmsg is called by protocol implementation to enqueue a
1316 * for a destination (to).*/
1317 /* enqueue a msgitem on the receiver's queue, the queue is protected by a
1318 * spinlock (msglock) */
1319 void ipn_proto_sendmsg(struct ipn_node *to, struct msgpool_item *msg)
1321 if (to) {
1322 if (to->netdev) {
1323 ipn_netdev_sendmsg(to,msg);
1324 } else {
1325 /* socket send */
1326 struct msgitem *msgitem;
1327 struct ipn_network *ipnn=to->ipn;
1328 spin_lock(&to->msglock);
1329 if (likely((to->shutdown & RCV_SHUTDOWN)==0)) {
1330 /*if (unlikely((ipnn->flags & IPN_FLAG_LOSSLESS) == 0 ||
1331 to->totmsgcount >= ipnn->msgpool_size))
1332 yield();*/
1333 if (likely(ipnn->flags & IPN_FLAG_LOSSLESS ||
1334 to->totmsgcount < ipnn->msgpool_size)) {
1335 if ((msgitem=kmem_cache_alloc(ipn_msgitem_cache,GFP_KERNEL))!=NULL) {
1336 msgitem->msg=msg;
1337 to->totmsgcount++;
1338 list_add_tail(&msgitem->list, &to->msgqueue);
1339 ipn_msgpool_hold(msg);
1343 spin_unlock(&to->msglock);
1344 wake_up_interruptible(&to->read_wait);
1349 /* enqueue an oob message. "to" is the destination */
1350 void ipn_proto_oobsendmsg(struct ipn_node *to, struct msgpool_item *msg)
1352 if (to) {
1353 if (!to->netdev) { /* no oob to netdev */
1354 struct msgitem *msgitem;
1355 struct ipn_network *ipnn=to->ipn;
1356 spin_lock(&to->msglock);
1357 if ((to->shutdown & RCV_SHUTDOWN_NO_OOB) == 0 &&
1358 (ipnn->flags & IPN_FLAG_LOSSLESS ||
1359 to->oobmsgcount < ipnn->msgpool_size)) {
1360 if ((msgitem=kmem_cache_alloc(ipn_msgitem_cache,GFP_KERNEL))!=NULL) {
1361 msgitem->msg=msg;
1362 to->totmsgcount++;
1363 to->oobmsgcount++;
1364 list_add_tail(&msgitem->list, &to->oobmsgqueue);
1365 ipn_msgpool_hold(msg);
1368 spin_unlock(&to->msglock);
1369 wake_up_interruptible(&to->read_wait);
1374 /* IPN RECV */
1375 int ipn_node_read(struct ipn_node *ipn_node, struct iovec *msg_iov, size_t len, int *msg_flags, int flags) {
1376 struct ipn_network *ipnn=ipn_node->ipn;
1377 struct msgitem *msgitem;
1378 struct msgpool_item *currmsg;
1380 if (unlikely((ipn_node->shutdown & XRCV_SHUTDOWN) == XRCV_SHUTDOWN)) {
1381 if (ipn_node->shutdown == SHUTDOWN_XMASK) /*EOF, nothing can be read*/
1382 return 0;
1383 else
1384 return -EPIPE; /*trying to read on a write only node */
1387 /* wait for a message */
1388 spin_lock(&ipn_node->msglock);
1389 while (ipn_node->totmsgcount == 0) {
1390 spin_unlock(&ipn_node->msglock);
1391 if (wait_event_interruptible(ipn_node->read_wait,
1392 !(ipn_node->totmsgcount == 0)))
1393 return -ERESTARTSYS;
1394 spin_lock(&ipn_node->msglock);
1396 /* oob gets delivered first. oob are rare */
1397 if (likely(list_empty(&ipn_node->oobmsgqueue)))
1398 msgitem=list_first_entry(&ipn_node->msgqueue, struct msgitem, list);
1399 else {
1400 msgitem=list_first_entry(&ipn_node->oobmsgqueue, struct msgitem, list);
1401 *msg_flags |= MSG_OOB;
1402 ipn_node->oobmsgcount--;
1404 list_del(&msgitem->list);
1405 ipn_node->totmsgcount--;
1406 spin_unlock(&ipn_node->msglock);
1407 currmsg=msgitem->msg;
1408 if (currmsg->len < len)
1409 len=currmsg->len;
1410 memcpy_toiovec(msg_iov, currmsg->data, len);
1411 ipn_msgpool_put(currmsg,ipnn);
1412 kmem_cache_free(ipn_msgitem_cache,msgitem);
1413 return len;
1416 static int ipn_recvmsg(struct kiocb *kiocb, struct socket *sock,
1417 struct msghdr *msg, size_t len, int flags) {
1418 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
1420 if (unlikely(sock->state != SS_CONNECTED))
1421 return -ENOTCONN;
1422 else
1423 return ipn_node_read(ipn_node, msg->msg_iov, len, &msg->msg_flags, flags);
1426 /* resize a network: change the # of communication ports (connport) */
1427 static int ipn_netresize(struct ipn_network *ipnn,int newsize)
1429 int oldsize,min;
1430 struct ipn_node **newconnport;
1431 struct ipn_node **oldconnport;
1432 int err;
1433 if (down_interruptible(&ipnn->ipnn_mutex))
1434 return -ERESTARTSYS;
1435 oldsize=ipnn->maxports;
1436 if (newsize == oldsize) {
1437 up(&ipnn->ipnn_mutex);
1438 return 0;
1440 min=oldsize;
1441 /* shrink a network. all the ports we are going to eliminate
1442 * must be unused! */
1443 if (newsize < oldsize) {
1444 int i;
1445 for (i=newsize; i<oldsize; i++)
1446 if (ipnn->connport[i]) {
1447 up(&ipnn->ipnn_mutex);
1448 return -EADDRINUSE;
1450 min=newsize;
1452 oldconnport=ipnn->connport;
1453 /* allocate the new connport array and copy the old one */
1454 newconnport=kzalloc(newsize * sizeof(struct ipn_node *),GFP_KERNEL);
1455 if (!newconnport) {
1456 up(&ipnn->ipnn_mutex);
1457 return -ENOMEM;
1459 memcpy(newconnport,oldconnport,min * sizeof(struct ipn_node *));
1460 ipnn->connport=newconnport;
1461 ipnn->maxports=newsize;
1462 /* notify the protocol that the netowrk has been resized */
1463 err=ipn_protocol_table[ipnn->protocol]->ipn_p_resizenet(ipnn,oldsize,newsize);
1464 if (err) {
1465 /* roll back if the resize operation failed for the protocol */
1466 ipnn->connport=oldconnport;
1467 ipnn->maxports=oldsize;
1468 kfree(newconnport);
1469 } else
1470 /* successful mission, network resized */
1471 kfree(oldconnport);
1472 up(&ipnn->ipnn_mutex);
1473 return err;
1476 /* IPN SETSOCKOPT */
1477 #ifndef IPN_PRE2632
1478 static int ipn_setsockopt(struct socket *sock, int level, int optname,
1479 char __user *optval, unsigned int optlen)
1480 #else
1481 static int ipn_setsockopt(struct socket *sock, int level, int optname,
1482 char __user *optval, int optlen)
1483 #endif
1485 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
1486 struct ipn_network *ipnn=ipn_node->ipn;
1488 if (ipn_node->shutdown == SHUTDOWN_XMASK)
1489 return -ECONNRESET;
1490 if (level != 0 && level != ipn_node->protocol+1)
1491 return -EPROTONOSUPPORT;
1492 if (level > 0) {
1493 /* protocol specific sockopt */
1494 if (ipnn) {
1495 int rv;
1496 if (down_interruptible(&ipnn->ipnn_mutex))
1497 return -ERESTARTSYS;
1498 rv=ipn_protocol_table[ipn_node->protocol]->ipn_p_setsockopt(ipn_node,optname,optval,optlen);
1499 up(&ipnn->ipnn_mutex);
1500 return rv;
1501 } else
1502 return -EOPNOTSUPP;
1503 } else {
1504 if (optname == IPN_SO_DESCR) {
1505 if (optlen > IPN_DESCRLEN)
1506 return -EINVAL;
1507 else {
1508 memset(ipn_node->descr,0,IPN_DESCRLEN);
1509 if (copy_from_user(ipn_node->descr,optval,optlen))
1510 ipn_node->descr[0]=0;
1511 else
1512 ipn_node->descr[optlen-1]=0;
1513 return 0;
1515 } else {
1516 if (optlen < sizeof(int))
1517 return -EINVAL;
1518 else if ((optname & IPN_SO_PREBIND) && (ipnn != NULL))
1519 return -EISCONN;
1520 else {
1521 int val;
1522 get_user(val, (int __user *) optval);
1523 if ((optname & IPN_SO_PREBIND) && !ipn_node->pbp) {
1524 struct pre_bind_parms std=STD_BIND_PARMS;
1525 ipn_node->pbp=kzalloc(sizeof(struct pre_bind_parms),GFP_KERNEL);
1526 if (!ipn_node->pbp)
1527 return -ENOMEM;
1528 *(ipn_node->pbp)=std;
1530 switch (optname) {
1531 case IPN_SO_PORT:
1532 if (sock->state == SS_UNCONNECTED)
1533 ipn_node->portno=val;
1534 else
1535 return -EISCONN;
1536 break;
1537 case IPN_SO_CHANGE_NUMNODES:
1538 if ((ipn_node->flags & IPN_NODEFLAG_BOUND)!=0) {
1539 if (val <= 0)
1540 return -EINVAL;
1541 else
1542 return ipn_netresize(ipnn,val);
1543 } else
1544 val=-ENOTCONN;
1545 break;
1546 case IPN_SO_WANT_OOB_NUMNODES:
1547 if (val)
1548 ipn_node->flags |= IPN_NODEFLAG_OOB_NUMNODES;
1549 else
1550 ipn_node->flags &= ~IPN_NODEFLAG_OOB_NUMNODES;
1551 break;
1552 case IPN_SO_HANDLE_OOB:
1553 if (val)
1554 ipn_node->shutdown &= ~RCV_SHUTDOWN_NO_OOB;
1555 else
1556 ipn_node->shutdown |= RCV_SHUTDOWN_NO_OOB;
1557 break;
1558 case IPN_SO_MTU:
1559 if (val <= 0)
1560 return -EINVAL;
1561 else
1562 ipn_node->pbp->mtu=val;
1563 break;
1564 case IPN_SO_NUMNODES:
1565 if (val <= 0)
1566 return -EINVAL;
1567 else
1568 ipn_node->pbp->maxports=val;
1569 break;
1570 case IPN_SO_MSGPOOLSIZE:
1571 if (val <= 0)
1572 return -EINVAL;
1573 else
1574 ipn_node->pbp->msgpoolsize=val;
1575 break;
1576 case IPN_SO_FLAGS:
1577 ipn_node->pbp->flags=val;
1578 break;
1579 case IPN_SO_MODE:
1580 ipn_node->pbp->mode=val;
1581 break;
1583 return 0;
1589 /* IPN GETSOCKOPT */
1590 static int ipn_getsockopt(struct socket *sock, int level, int optname,
1591 char __user *optval, int __user *optlen) {
1592 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
1593 struct ipn_network *ipnn=ipn_node->ipn;
1594 int len;
1596 if (ipn_node->shutdown == SHUTDOWN_XMASK)
1597 return -ECONNRESET;
1598 if (level != 0 && level != ipn_node->protocol+1)
1599 return -EPROTONOSUPPORT;
1600 if (level > 0) {
1601 if (ipnn) {
1602 int rv;
1603 /* protocol specific sockopt */
1604 if (down_interruptible(&ipnn->ipnn_mutex))
1605 return -ERESTARTSYS;
1606 rv=ipn_protocol_table[ipn_node->protocol]->ipn_p_getsockopt(ipn_node,optname,optval,optlen);
1607 up(&ipnn->ipnn_mutex);
1608 return rv;
1609 } else
1610 return -EOPNOTSUPP;
1611 } else {
1612 if (get_user(len, optlen))
1613 return -EFAULT;
1614 if (optname == IPN_SO_DESCR) {
1615 if (len < IPN_DESCRLEN)
1616 return -EINVAL;
1617 else {
1618 if (len > IPN_DESCRLEN)
1619 len=IPN_DESCRLEN;
1620 if(put_user(len, optlen))
1621 return -EFAULT;
1622 if(copy_to_user(optval,ipn_node->descr,len))
1623 return -EFAULT;
1624 return 0;
1626 } else {
1627 int val=-2;
1628 switch (optname) {
1629 case IPN_SO_PORT:
1630 val=ipn_node->portno;
1631 break;
1632 case IPN_SO_MTU:
1633 if (ipnn)
1634 val=ipnn->mtu;
1635 else if (ipn_node->pbp)
1636 val=ipn_node->pbp->mtu;
1637 break;
1638 case IPN_SO_NUMNODES:
1639 if (ipnn)
1640 val=ipnn->maxports;
1641 else if (ipn_node->pbp)
1642 val=ipn_node->pbp->maxports;
1643 break;
1644 case IPN_SO_MSGPOOLSIZE:
1645 if (ipnn)
1646 val=ipnn->msgpool_size;
1647 else if (ipn_node->pbp)
1648 val=ipn_node->pbp->msgpoolsize;
1649 break;
1650 case IPN_SO_FLAGS:
1651 if (ipnn)
1652 val=ipnn->flags;
1653 else if (ipn_node->pbp)
1654 val=ipn_node->pbp->flags;
1655 break;
1656 case IPN_SO_MODE:
1657 if (ipnn)
1658 val=-1;
1659 else if (ipn_node->pbp)
1660 val=ipn_node->pbp->mode;
1661 break;
1663 if (val < -1)
1664 return -EINVAL;
1665 else {
1666 if (len < sizeof(int))
1667 return -EOVERFLOW;
1668 else {
1669 len = sizeof(int);
1670 if(put_user(len, optlen))
1671 return -EFAULT;
1672 if(copy_to_user(optval,&val,len))
1673 return -EFAULT;
1674 return 0;
1681 /* BROADCAST/HUB implementation */
1683 static int ipn_bcast_newport(struct ipn_node *newport) {
1684 struct ipn_network *ipnn=newport->ipn;
1685 int i;
1686 for (i=0;i<ipnn->maxports;i++) {
1687 if (ipnn->connport[i] == NULL)
1688 return i;
1690 return -1;
1693 static int ipn_bcast_handlemsg(struct ipn_node *from,
1694 struct msgpool_item *msgitem){
1695 struct ipn_network *ipnn=from->ipn;
1697 struct ipn_node *ipn_node;
1698 list_for_each_entry(ipn_node, &ipnn->connectqueue, nodelist) {
1699 if (ipn_node != from)
1700 ipn_proto_sendmsg(ipn_node,msgitem);
1702 return 0;
1705 static void ipn_null_delport(struct ipn_node *oldport) {}
1706 static void ipn_null_postnewport(struct ipn_node *newport) {}
1707 static void ipn_null_predelport(struct ipn_node *oldport) {}
1708 static int ipn_null_newnet(struct ipn_network *newnet) {return 0;}
1709 static int ipn_null_resizenet(struct ipn_network *net,int oldsize,int newsize) {
1710 return 0;}
1711 static void ipn_null_delnet(struct ipn_network *oldnet) {}
1712 static int ipn_null_setsockopt(struct ipn_node *port,int optname,
1713 char __user *optval, int optlen) {return -EOPNOTSUPP;}
1714 static int ipn_null_getsockopt(struct ipn_node *port,int optname,
1715 char __user *optval, int *optlen) {return -EOPNOTSUPP;}
1716 static int ipn_null_ioctl(struct ipn_node *port,unsigned int request,
1717 unsigned long arg) {return -EOPNOTSUPP;}
1719 /* Protocol Registration/deregisteration */
1721 void ipn_init_protocol(struct ipn_protocol *p)
1723 if (p->ipn_p_delport == NULL) p->ipn_p_delport=ipn_null_delport;
1724 if (p->ipn_p_postnewport == NULL) p->ipn_p_postnewport=ipn_null_postnewport;
1725 if (p->ipn_p_predelport == NULL) p->ipn_p_predelport=ipn_null_predelport;
1726 if (p->ipn_p_newnet == NULL) p->ipn_p_newnet=ipn_null_newnet;
1727 if (p->ipn_p_resizenet == NULL) p->ipn_p_resizenet=ipn_null_resizenet;
1728 if (p->ipn_p_delnet == NULL) p->ipn_p_delnet=ipn_null_delnet;
1729 if (p->ipn_p_setsockopt == NULL) p->ipn_p_setsockopt=ipn_null_setsockopt;
1730 if (p->ipn_p_getsockopt == NULL) p->ipn_p_getsockopt=ipn_null_getsockopt;
1731 if (p->ipn_p_ioctl == NULL) p->ipn_p_ioctl=ipn_null_ioctl;
1734 int ipn_proto_register(int protocol,struct ipn_protocol *ipn_service)
1736 int rv=0;
1737 if (ipn_service->ipn_p_newport == NULL ||
1738 ipn_service->ipn_p_handlemsg == NULL)
1739 return -EINVAL;
1740 ipn_init_protocol(ipn_service);
1741 if (protocol > 1 && protocol <= IPN_MAX_PROTO) {
1742 protocol--;
1743 if (down_interruptible(&ipn_glob_mutex))
1744 return -ERESTARTSYS;
1745 if (ipn_protocol_table[protocol])
1746 rv= -EEXIST;
1747 else {
1748 ipn_service->refcnt=0;
1749 ipn_protocol_table[protocol]=ipn_service;
1750 printk(KERN_INFO "IPN: Registered protocol %d\n",protocol+1);
1752 up(&ipn_glob_mutex);
1753 } else
1754 rv= -EINVAL;
1755 return rv;
1758 int ipn_proto_deregister(int protocol)
1760 int rv=0;
1761 if (protocol > 1 && protocol <= IPN_MAX_PROTO) {
1762 protocol--;
1763 if (down_interruptible(&ipn_glob_mutex))
1764 return -ERESTARTSYS;
1765 if (ipn_protocol_table[protocol]) {
1766 if (ipn_protocol_table[protocol]->refcnt == 0) {
1767 ipn_protocol_table[protocol]=NULL;
1768 printk(KERN_INFO "IPN: Unregistered protocol %d\n",protocol+1);
1769 } else
1770 rv=-EADDRINUSE;
1771 } else
1772 rv= -ENOENT;
1773 up(&ipn_glob_mutex);
1774 } else
1775 rv= -EINVAL;
1776 return rv;
1779 /* MAIN SECTION */
1780 /* Module constructor/destructor */
1781 static struct net_proto_family ipn_family_ops = {
1782 .family = PF_IPN,
1783 .create = ipn_create,
1784 .owner = THIS_MODULE,
1787 /* IPN constructor */
1788 static int ipn_init(void)
1790 int rc;
1792 ipn_init_protocol(&ipn_bcast);
1793 ipn_network_cache=kmem_cache_create("ipn_network",sizeof(struct ipn_network),0,0,NULL);
1794 if (!ipn_network_cache) {
1795 printk(KERN_CRIT "%s: Cannot create ipn_network SLAB cache!\n",
1796 __FUNCTION__);
1797 rc=-ENOMEM;
1798 goto out;
1801 ipn_node_cache=kmem_cache_create("ipn_node",sizeof(struct ipn_node),0,0,NULL);
1802 if (!ipn_node_cache) {
1803 printk(KERN_CRIT "%s: Cannot create ipn_node SLAB cache!\n",
1804 __FUNCTION__);
1805 rc=-ENOMEM;
1806 goto out_net;
1809 ipn_msgitem_cache=kmem_cache_create("ipn_msgitem",sizeof(struct msgitem),0,0,NULL);
1810 if (!ipn_msgitem_cache) {
1811 printk(KERN_CRIT "%s: Cannot create ipn_msgitem SLAB cache!\n",
1812 __FUNCTION__);
1813 rc=-ENOMEM;
1814 goto out_net_node;
1817 rc=ipn_msgbuf_init();
1818 if (rc != 0) {
1819 printk(KERN_CRIT "%s: Cannot create ipn_msgbuf SLAB cache\n",
1820 __FUNCTION__);
1821 goto out_net_node_msg;
1824 rc=proto_register(&ipn_proto,1);
1825 if (rc != 0) {
1826 printk(KERN_CRIT "%s: Cannot register the protocol!\n",
1827 __FUNCTION__);
1828 goto out_net_node_msg_msgbuf;
1831 sock_register(&ipn_family_ops);
1832 ipn_netdev_init();
1833 printk(KERN_INFO "IPN: Virtual Square Project, University of Bologna 2007-09\n");
1834 return 0;
1836 out_net_node_msg_msgbuf:
1837 ipn_msgbuf_fini();
1838 out_net_node_msg:
1839 kmem_cache_destroy(ipn_msgitem_cache);
1840 out_net_node:
1841 kmem_cache_destroy(ipn_node_cache);
1842 out_net:
1843 kmem_cache_destroy(ipn_network_cache);
1844 out:
1845 return rc;
1848 /* IPN destructor */
1849 static void ipn_exit(void)
1851 ipn_netdev_fini();
1852 if (ipn_msgitem_cache)
1853 kmem_cache_destroy(ipn_msgitem_cache);
1854 if (ipn_node_cache)
1855 kmem_cache_destroy(ipn_node_cache);
1856 if (ipn_network_cache)
1857 kmem_cache_destroy(ipn_network_cache);
1858 ipn_msgbuf_fini();
1859 sock_unregister(PF_IPN);
1860 proto_unregister(&ipn_proto);
1861 printk(KERN_INFO "IPN removed\n");
1864 module_init(ipn_init);
1865 module_exit(ipn_exit);
1867 EXPORT_SYMBOL_GPL(ipn_proto_register);
1868 EXPORT_SYMBOL_GPL(ipn_proto_deregister);
1869 EXPORT_SYMBOL_GPL(ipn_proto_sendmsg);
1870 EXPORT_SYMBOL_GPL(ipn_proto_oobsendmsg);
1871 EXPORT_SYMBOL_GPL(ipn_msgpool_alloc);
1872 EXPORT_SYMBOL_GPL(ipn_msgpool_put);