bugfix: chrdev major number assignemt (TNX Marco Dalla Via)
[vde.git] / ipn / af_ipn.c
blob68c3af8e4e93b4f9b8d2a45c1ecd9e6ba6d72ab5
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
48 /*extension of RCV_SHUTDOWN defined in include/net/sock.h
49 * when the bit is set recv fails */
50 /* NO_OOB: do not send OOB */
51 #define RCV_SHUTDOWN_NO_OOB 4
52 /* EXTENDED MASK including OOB */
53 #define SHUTDOWN_XMASK (SHUTDOWN_MASK | RCV_SHUTDOWN_NO_OOB)
54 /* if XRCV_SHUTDOWN is all set recv fails */
55 #define XRCV_SHUTDOWN (RCV_SHUTDOWN | RCV_SHUTDOWN_NO_OOB)
57 /* Global MUTEX: this is locked to add/delete/modify networks
58 * this is *not* locked just to send/receive msgs */
59 static DECLARE_MUTEX(ipn_glob_mutex);
60 /* Network table and hash */
61 struct hlist_head ipn_network_table[IPN_HASH_SIZE + 1];
62 /* slab(s) for fast data structure allocation */
63 static struct kmem_cache *ipn_network_cache;
64 static struct kmem_cache *ipn_node_cache;
65 static struct kmem_cache *ipn_msgitem_cache;
67 /* Protocol 1: HUB/Broadcast default protocol. Function Prototypes */
68 static int ipn_bcast_newport(struct ipn_node *newport);
69 static int ipn_bcast_handlemsg(struct ipn_node *from,
70 struct msgpool_item *msgitem);
72 /* default protocol IPN_BROADCAST (0) */
73 static struct ipn_protocol ipn_bcast = {
74 .refcnt=0,
75 .ipn_p_newport=ipn_bcast_newport,
76 .ipn_p_handlemsg=ipn_bcast_handlemsg};
77 /* Protocol table */
78 static struct ipn_protocol *ipn_protocol_table[IPN_MAX_PROTO]={&ipn_bcast};
80 /* Socket call function prototypes */
81 static int ipn_release(struct socket *);
82 static int ipn_bind(struct socket *, struct sockaddr *, int);
83 static int ipn_connect(struct socket *, struct sockaddr *,
84 int addr_len, int flags);
85 static int ipn_getname(struct socket *, struct sockaddr *, int *, int);
86 static unsigned int ipn_poll(struct file *, struct socket *, poll_table *);
87 static int ipn_ioctl(struct socket *, unsigned int, unsigned long);
88 static int ipn_shutdown(struct socket *, int);
89 static int ipn_sendmsg(struct kiocb *, struct socket *,
90 struct msghdr *, size_t);
91 static int ipn_recvmsg(struct kiocb *, struct socket *,
92 struct msghdr *, size_t, int);
93 static int ipn_setsockopt(struct socket *sock, int level, int optname,
94 char __user *optval, int optlen);
95 static int ipn_getsockopt(struct socket *sock, int level, int optname,
96 char __user *optval, int __user *optlen);
98 /* Network table Management
99 * inode->ipn_network hash table
100 * protected by ipn_glob_mutex */
101 static inline void ipn_insert_network(struct hlist_head *list, struct ipn_network *ipnn)
103 hlist_add_head(&ipnn->hnode, list);
106 static inline void ipn_remove_network(struct ipn_network *ipnn)
108 hlist_del(&ipnn->hnode);
111 static struct ipn_network *ipn_find_network_byinode(struct inode *i)
113 struct ipn_network *ipnn;
114 struct hlist_node *node;
116 hlist_for_each_entry(ipnn, node,
117 &ipn_network_table[i->i_ino & (IPN_HASH_SIZE - 1)], hnode) {
118 struct dentry *dentry = ipnn->dentry;
120 if(dentry && dentry->d_inode == i)
121 return ipnn;
123 return NULL;
126 struct ipn_network *ipn_find_network_byfun(
127 int (*fun)(struct ipn_network *,void *),void *funarg)
129 struct ipn_network *ipnn;
130 struct hlist_node *node;
131 int ipn_table_scan;
133 for (ipn_table_scan=0;ipn_table_scan<IPN_HASH_SIZE;ipn_table_scan++) {
134 hlist_for_each_entry(ipnn, node, &ipn_network_table[ipn_table_scan], hnode) {
135 if(fun(ipnn,funarg))
136 return ipnn;
139 return NULL;
142 /* msgpool management
143 * msgpool_item are ipn_network dependent (each net has its own MTU)
144 * for each message sent there is one msgpool_item and many struct msgitem
145 * one for each receipient.
146 * msgitem are connected to the node's msgqueue or oobmsgqueue.
147 * when a message is delivered to a process the msgitem is deleted and
148 * the count of the msgpool_item is decreased.
149 * msgpool_item elements gets deleted automatically when count is 0*/
151 struct msgitem {
152 struct list_head list;
153 struct msgpool_item *msg;
156 /* alloc a fresh msgpool item. count is set to 1.
157 * the typical use is
158 * ipn_msgpool_alloc
159 * for each receipient
160 * enqueue messages to the process (using msgitem), ipn_msgpool_hold
161 * ipn_msgpool_put
162 * The message can be delivered concurrently. init count to 1 guarantees
163 * that it survives at least until is has been enqueued to all
164 * receivers */
165 /* msgpool_cache == NULL when the ipn network is using the flexible allocation,
166 * i.e. the msgpool item gets allocated by kmalloc instead of using slab */
167 static inline struct msgpool_item *_ipn_msgpool_alloc(struct ipn_network *ipnn, int len)
169 struct msgpool_item *new;
170 if ((new=ipnn->msgpool_cache?
171 kmem_cache_alloc(ipnn->msgpool_cache,GFP_KERNEL):
172 kmalloc(sizeof(struct msgpool_item)+len,GFP_KERNEL)) != NULL) {
173 atomic_set(&new->count,1);
174 atomic_inc(&ipnn->msgpool_nelem);
176 return new;
179 /* ipn_msgpool_alloc returns null when leaky, lossless and no space is left
180 * for new msgpool items. When !leaky, it can allocate more msgpooli items,
181 * this temporary overassignment blocks new messages to be delivered until
182 * there is space for a msgpool item to be allocated */
183 struct msgpool_item *ipn_msgpool_alloc(struct ipn_network *ipnn,int leaky,int len)
185 if (leaky && (ipnn->flags & IPN_FLAG_LOSSLESS) &&
186 atomic_read(&ipnn->msgpool_nelem) < ipnn->msgpool_size)
187 return NULL;
188 else
189 return _ipn_msgpool_alloc(ipnn,len);
192 /* If the service il LOSSLESS, this msgpool call waits for an
193 * available msgpool item */
194 static inline struct msgpool_item *ipn_msgpool_alloc_locking(struct ipn_network *ipnn,int len)
196 if (ipnn->flags & IPN_FLAG_LOSSLESS) {
197 while (atomic_read(&ipnn->msgpool_nelem) >= ipnn->msgpool_size) {
198 if (wait_event_interruptible_exclusive(ipnn->send_wait,
199 atomic_read(&ipnn->msgpool_nelem) < ipnn->msgpool_size))
200 return NULL;
203 return _ipn_msgpool_alloc(ipnn,len);
206 /* register one more user for this msgpool item (i.e. count++) */
207 static inline void ipn_msgpool_hold(struct msgpool_item *msg)
209 atomic_inc(&msg->count);
212 /* decrease count and delete msgpool_item if count == 0 */
213 void ipn_msgpool_put(struct msgpool_item *old,
214 struct ipn_network *ipnn)
216 if (atomic_dec_and_test(&old->count)) {
217 if (ipnn->msgpool_cache)
218 kmem_cache_free(ipnn->msgpool_cache,old);
219 else
220 kfree(old);
221 atomic_dec(&ipnn->msgpool_nelem);
222 if (ipnn->flags & IPN_FLAG_LOSSLESS) /* this could be done anyway */
223 wake_up_interruptible(&ipnn->send_wait);
227 /* socket calls */
228 static const struct proto_ops ipn_ops = {
229 .family = PF_IPN,
230 .owner = THIS_MODULE,
231 .release = ipn_release,
232 .bind = ipn_bind,
233 .connect = ipn_connect,
234 .socketpair = sock_no_socketpair,
235 .accept = sock_no_accept,
236 .getname = ipn_getname,
237 .poll = ipn_poll,
238 .ioctl = ipn_ioctl,
239 .listen = sock_no_listen,
240 .shutdown = ipn_shutdown,
241 .setsockopt = ipn_setsockopt,
242 .getsockopt = ipn_getsockopt,
243 .sendmsg = ipn_sendmsg,
244 .recvmsg = ipn_recvmsg,
245 .mmap = sock_no_mmap,
246 .sendpage = sock_no_sendpage,
249 static struct proto ipn_proto = {
250 .name = "IPN",
251 .owner = THIS_MODULE,
252 .obj_size = sizeof(struct ipn_sock),
255 /* create a new ipn_node */
256 struct ipn_node *ipn_node_create(struct net *net)
258 struct ipn_node *ipn_node;
259 ipn_node=kmem_cache_alloc(ipn_node_cache,GFP_KERNEL);
260 if (ipn_node!=NULL) {
261 INIT_LIST_HEAD(&ipn_node->nodelist);
262 ipn_node->protocol=0; /* BROADCAST, caller must set a proper value */
263 ipn_node->flags=IPN_NODEFLAG_INUSE;
264 ipn_node->shutdown=RCV_SHUTDOWN_NO_OOB;
265 ipn_node->descr[0]=0;
266 ipn_node->portno=IPN_PORTNO_ANY;
267 ipn_node->net=net;
268 ipn_node->netdev=NULL;
269 ipn_node->chrdev=0;
270 ipn_node->proto_private=NULL;
271 ipn_node->totmsgcount=0;
272 ipn_node->oobmsgcount=0;
273 spin_lock_init(&ipn_node->msglock);
274 INIT_LIST_HEAD(&ipn_node->msgqueue);
275 INIT_LIST_HEAD(&ipn_node->oobmsgqueue);
276 ipn_node->ipn=NULL;
277 init_waitqueue_head(&ipn_node->read_wait);
278 ipn_node->pbp=NULL;
280 return ipn_node;
283 /* create a socket
284 * ipn_node is a separate structure, pointed by ipn_sock -> node
285 * when a node is "persistent", ipn_node survives while ipn_sock gets released*/
286 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
287 static int ipn_create(struct socket *sock, int protocol)
288 #else
289 static int ipn_create(struct net *net,struct socket *sock, int protocol)
290 #endif
292 struct ipn_sock *ipn_sk;
294 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
295 void *net=NULL;
296 #else
297 if (net != &init_net)
298 return -EAFNOSUPPORT;
299 #endif
301 if (sock->type != SOCK_RAW)
302 return -EPROTOTYPE;
303 if (protocol > 0)
304 protocol=protocol-1;
305 else
306 protocol=IPN_BROADCAST-1;
307 if (protocol < 0 || protocol >= IPN_MAX_PROTO ||
308 ipn_protocol_table[protocol] == NULL)
309 return -EPROTONOSUPPORT;
310 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
311 ipn_sk = (struct ipn_sock *) sk_alloc(PF_IPN, GFP_KERNEL, &ipn_proto, 1);
312 #else
313 ipn_sk = (struct ipn_sock *) sk_alloc(net, PF_IPN, GFP_KERNEL, &ipn_proto);
314 #endif
316 if (!ipn_sk)
317 return -ENOMEM;
318 if ((ipn_sk->node=ipn_node_create(net))==NULL) {
319 sock_put((struct sock *) ipn_sk);
320 return -ENOMEM;
322 ipn_sk->node->protocol=protocol;
323 sock_init_data(sock,(struct sock *) ipn_sk);
324 sock->state = SS_UNCONNECTED;
325 sock->ops = &ipn_ops;
326 sock->sk=(struct sock *)ipn_sk;
327 return 0;
330 /* update # of readers and # of writers counters for an ipn network.
331 * This function sends oob messages to nodes requesting the service */
332 /* LOCKING ipnn_mutex is locked */
333 static void ipn_net_update_counters(struct ipn_network *ipnn,
334 int chg_readers, int chg_writers) {
335 ipnn->numreaders += chg_readers;
336 ipnn->numwriters += chg_writers;
337 if (ipnn->mtu >= sizeof(struct numnode_oob))
339 struct msgpool_item *ipn_msg=_ipn_msgpool_alloc(ipnn,sizeof(struct numnode_oob));
340 if (ipn_msg) {
341 struct numnode_oob *oob_msg=(struct numnode_oob *)(ipn_msg->data);
342 struct ipn_node *ipn_node;
343 ipn_msg->len=sizeof(struct numnode_oob);
344 oob_msg->level=IPN_ANY;
345 oob_msg->tag=IPN_OOB_NUMNODE_TAG;
346 oob_msg->numreaders=ipnn->numreaders;
347 oob_msg->numwriters=ipnn->numwriters;
348 list_for_each_entry(ipn_node, &ipnn->connectqueue, nodelist) {
349 if (ipn_node->flags & IPN_NODEFLAG_OOB_NUMNODES)
350 ipn_proto_oobsendmsg(ipn_node,ipn_msg);
352 ipn_msgpool_put(ipn_msg,ipnn);
357 /* flush pending messages (for close and shutdown RCV) */
358 /* LOCKING: ipnn_mutex is locked */
359 static void ipn_flush_recvqueue(struct ipn_node *ipn_node)
361 struct ipn_network *ipnn=ipn_node->ipn;
362 spin_lock(&ipn_node->msglock);
363 while (!list_empty(&ipn_node->msgqueue)) {
364 struct msgitem *msgitem=
365 list_first_entry(&ipn_node->msgqueue, struct msgitem, list);
366 list_del(&msgitem->list);
367 ipn_node->totmsgcount--;
368 ipn_msgpool_put(msgitem->msg,ipnn);
369 kmem_cache_free(ipn_msgitem_cache,msgitem);
371 spin_unlock(&ipn_node->msglock);
374 /* flush pending oob messages (for socket close) */
375 /* LOCKING: ipnn_mutex is locked */
376 static void ipn_flush_oobrecvqueue(struct ipn_node *ipn_node)
378 struct ipn_network *ipnn=ipn_node->ipn;
379 spin_lock(&ipn_node->msglock);
380 while (!list_empty(&ipn_node->oobmsgqueue)) {
381 struct msgitem *msgitem=
382 list_first_entry(&ipn_node->oobmsgqueue, struct msgitem, list);
383 list_del(&msgitem->list);
384 ipn_node->totmsgcount--;
385 ipn_node->oobmsgcount--;
386 ipn_msgpool_put(msgitem->msg,ipnn);
387 kmem_cache_free(ipn_msgitem_cache,msgitem);
389 spin_unlock(&ipn_node->msglock);
392 /* Terminate node. The node is "logically" terminated. */
393 /* LOCKING: ipn_glob_lock must be locked here */
394 static int ipn_terminate_node(struct ipn_node *ipn_node)
396 struct ipn_network *ipnn=ipn_node->ipn;
397 if (ipnn) {
398 if (down_interruptible(&ipnn->ipnn_mutex))
399 return -ERESTARTSYS;
400 if (ipn_node->portno >= 0) {
401 ipn_protocol_table[ipnn->protocol]->ipn_p_predelport(ipn_node);
402 ipnn->connport[ipn_node->portno]=NULL;
404 list_del(&ipn_node->nodelist);
405 ipn_flush_recvqueue(ipn_node);
406 ipn_flush_oobrecvqueue(ipn_node);
407 if (ipn_node->portno >= 0)
408 ipn_protocol_table[ipnn->protocol]->ipn_p_delport(ipn_node);
409 ipn_node->ipn=NULL;
410 ipn_net_update_counters(ipnn,
411 (ipn_node->shutdown & RCV_SHUTDOWN)?0:-1,
412 (ipn_node->shutdown & SEND_SHUTDOWN)?0:-1);
413 ipn_node->shutdown = SHUTDOWN_XMASK;
414 up(&ipnn->ipnn_mutex);
415 if (ipn_node->netdev)
416 ipn_netdev_close(ipn_node);
417 /* No more network elements */
418 ipnn->refcnt--;
419 if (ipnn->refcnt == 0 && !ipn_is_persistent_chrdev(ipnn))
421 if (ipnn->chrdev)
422 ipn_deregister_chrdev(ipnn);
423 ipn_protocol_table[ipnn->protocol]->ipn_p_delnet(ipnn);
424 ipn_remove_network(ipnn);
425 ipn_protocol_table[ipnn->protocol]->refcnt--;
426 if (ipnn->dentry) {
427 dput(ipnn->dentry);
428 mntput(ipnn->mnt);
430 if (ipnn->msgpool_cache)
431 ipn_msgbuf_put(ipnn->msgpool_cache);
432 if (ipnn->connport)
433 kfree(ipnn->connport);
434 kmem_cache_free(ipn_network_cache, ipnn);
435 module_put(THIS_MODULE);
438 if (ipn_node->pbp) {
439 kfree(ipn_node->pbp);
440 ipn_node->pbp=NULL;
442 return 0;
445 /* release of an ipn_node */
446 int ipn_node_release(struct ipn_node *ipn_node)
448 int rv;
449 if (down_interruptible(&ipn_glob_mutex))
450 return -ERESTARTSYS;
451 if (ipn_node->flags & IPN_NODEFLAG_PERSIST) {
452 ipn_node->flags &= ~IPN_NODEFLAG_INUSE;
453 rv=0;
454 up(&ipn_glob_mutex);
455 } else {
456 rv=ipn_terminate_node(ipn_node);
457 up(&ipn_glob_mutex);
458 if (rv==0) {
459 ipn_netdevsync();
460 kmem_cache_free(ipn_node_cache,ipn_node);
463 return rv;
466 /* release of an ipn socket */
467 static int ipn_release (struct socket *sock)
469 struct ipn_sock *ipn_sk=(struct ipn_sock *)sock->sk;
470 struct ipn_node *ipn_node=ipn_sk->node;
471 int rv=ipn_node_release(ipn_node);
472 if (rv == 0)
473 sock_put((struct sock *) ipn_sk);
474 return rv;
477 /* _set persist, change the persistence of a node,
478 * when persistence gets cleared and the node is no longer used
479 * the node is terminated and freed.
480 * ipn_glob_mutex must be locked */
481 static int _ipn_setpersist(struct ipn_node *ipn_node, int persist)
483 int rv=0;
484 if (persist)
485 ipn_node->flags |= IPN_NODEFLAG_PERSIST;
486 else {
487 ipn_node->flags &= ~IPN_NODEFLAG_PERSIST;
488 if (!(ipn_node->flags & IPN_NODEFLAG_INUSE)) {
489 rv=ipn_terminate_node(ipn_node);
490 if (rv==0)
491 kmem_cache_free(ipn_node_cache,ipn_node);
494 return rv;
497 /* ipn_setpersist
498 * lock ipn_glob_mutex and call __ipn_setpersist above */
499 static int ipn_setpersist(struct ipn_node *ipn_node, int persist)
501 int rv=0;
502 if (ipn_node->netdev == NULL)
503 return -ENODEV;
504 if (down_interruptible(&ipn_glob_mutex))
505 return -ERESTARTSYS;
506 rv=_ipn_setpersist(ipn_node,persist);
507 up(&ipn_glob_mutex);
508 return rv;
511 /* several network parameters can be set by setsockopt prior to bind */
512 /* struct pre_bind_parms is a temporary stucture connected to ipn_node->pbp
513 * to keep the parameter values. */
514 struct pre_bind_parms {
515 unsigned short maxports;
516 unsigned short flags;
517 unsigned short msgpoolsize;
518 unsigned short mtu;
519 unsigned short mode;
522 /* STD_PARMS: BITS_PER_LONG nodes, no flags, BITS_PER_BYTE pending msgs,
523 * Ethernet + VLAN MTU*/
524 #define STD_BIND_PARMS {BITS_PER_LONG, 0, BITS_PER_BYTE, 1514, 0777};
526 static int ipn_mkname(struct sockaddr_un * sunaddr, int len)
528 if (len <= sizeof(short) || len > sizeof(*sunaddr))
529 return -EINVAL;
530 if (!sunaddr || sunaddr->sun_family != AF_IPN)
531 return -EINVAL;
533 * This may look like an off by one error but it is a bit more
534 * subtle. 108 is the longest valid AF_IPN path for a binding.
535 * sun_path[108] doesnt as such exist. However in kernel space
536 * we are guaranteed that it is a valid memory location in our
537 * kernel address buffer.
539 ((char *)sunaddr)[len]=0;
540 len = strlen(sunaddr->sun_path)+1+sizeof(short);
541 return len;
544 static int ipn_node_bind(struct ipn_node *ipn_node, struct ipn_network *ipnn)
546 if (ipnn == NULL)
547 return -ENOENT;
548 if (ipn_node->ipn != NULL)
549 return -EISCONN;
550 ipnn->refcnt++;
551 list_add_tail(&ipn_node->nodelist,&ipnn->unconnectqueue);
552 ipn_node->ipn=ipnn;
553 ipn_node->flags |= IPN_NODEFLAG_BOUND;
554 return 0;
557 /* IPN BIND */
558 static int ipn_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
560 struct sockaddr_un *sunaddr=(struct sockaddr_un *)uaddr;
561 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
562 struct nameidata nd;
563 struct ipn_network *ipnn;
564 struct dentry * dentry = NULL;
565 int err;
566 struct pre_bind_parms parms=STD_BIND_PARMS;
568 //printk("IPN bind\n");
570 if (down_interruptible(&ipn_glob_mutex))
571 return -ERESTARTSYS;
572 if (sock->state != SS_UNCONNECTED ||
573 ipn_node->ipn != NULL) {
574 err= -EISCONN;
575 goto out;
578 if (ipn_node->protocol >= 0 &&
579 (ipn_node->protocol >= IPN_MAX_PROTO ||
580 ipn_protocol_table[ipn_node->protocol] == NULL)) {
581 err= -EPROTONOSUPPORT;
582 goto out;
585 addr_len = ipn_mkname(sunaddr, addr_len);
586 if (addr_len < 0) {
587 err=addr_len;
588 goto out;
591 /* check if there is already an ipn-network socket with that name */
592 err = path_lookup(sunaddr->sun_path, LOOKUP_FOLLOW, &nd);
593 if (err) { /* it does not exist, NEW IPN socket! */
594 unsigned int mode;
595 /* Is it everything okay with the parent? */
596 err = path_lookup(sunaddr->sun_path, LOOKUP_PARENT, &nd);
597 if (err)
598 goto out_mknod_parent;
599 /* Do I have the permission to create a file? */
600 dentry = lookup_create(&nd, 0);
601 err = PTR_ERR(dentry);
602 if (IS_ERR(dentry))
603 goto out_mknod_unlock;
605 * All right, let's create it.
607 if (ipn_node->pbp)
608 mode = ipn_node->pbp->mode;
609 else
610 mode = SOCK_INODE(sock)->i_mode;
611 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 30)
612 mode = S_IFSOCK | (mode & ~current->fs->umask);
613 #else
614 mode = S_IFSOCK | (mode & ~current_umask());
615 #endif
616 #ifndef IPN_PRE2625
617 #ifdef APPARMOR
618 err = vfs_mknod(nd.path.dentry->d_inode, dentry, nd.path.mnt, mode, 0);
619 #else
620 err = vfs_mknod(nd.path.dentry->d_inode, dentry, mode, 0);
621 #endif
622 #else
623 #ifdef APPARMOR
624 err = vfs_mknod(nd.dentry->d_inode, dentry, nd.path.mnt, mode, 0);
625 #else
626 err = vfs_mknod(nd.dentry->d_inode, dentry, mode, 0);
627 #endif
628 #endif
629 if (err)
630 goto out_mknod_dput;
631 #ifndef IPN_PRE2625
632 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
633 dput(nd.path.dentry);
634 nd.path.dentry = dentry;
635 #else
636 mutex_unlock(&nd.dentry->d_inode->i_mutex);
637 dput(nd.dentry);
638 nd.dentry = dentry;
639 #endif
640 /* create a new ipn_network item */
641 if (ipn_node->pbp)
642 parms=*ipn_node->pbp;
643 ipnn=kmem_cache_zalloc(ipn_network_cache,GFP_KERNEL);
644 if (!ipnn) {
645 err=-ENOMEM;
646 goto out_mknod_dput_ipnn;
648 ipnn->connport=kzalloc(parms.maxports * sizeof(struct ipn_node *),GFP_KERNEL);
649 if (!ipnn->connport) {
650 err=-ENOMEM;
651 goto out_mknod_dput_ipnn2;
654 /* module refcnt is incremented for each network, thus
655 * rmmod is forbidden if there are persistent nodes */
656 if (!try_module_get(THIS_MODULE)) {
657 err = -EINVAL;
658 goto out_mknod_dput_ipnn2;
660 memcpy(&ipnn->sunaddr,sunaddr,addr_len);
661 ipnn->mtu=parms.mtu;
662 ipnn->flags=parms.flags;
663 if (ipnn->flags & IPN_FLAG_FLEXMTU)
664 ipnn->msgpool_cache= NULL;
665 else {
666 ipnn->msgpool_cache=ipn_msgbuf_get(ipnn->mtu);
667 if (!ipnn->msgpool_cache) {
668 err=-ENOMEM;
669 goto out_mknod_dput_putmodule;
672 INIT_LIST_HEAD(&ipnn->unconnectqueue);
673 INIT_LIST_HEAD(&ipnn->connectqueue);
674 ipnn->refcnt=0;
675 #ifndef IPN_PRE2625
676 ipnn->dentry=nd.path.dentry;
677 ipnn->mnt=nd.path.mnt;
678 #else
679 ipnn->dentry=nd.dentry;
680 ipnn->mnt=nd.mnt;
681 #endif
682 init_MUTEX(&ipnn->ipnn_mutex);
683 ipnn->sunaddr_len=addr_len;
684 ipnn->protocol=ipn_node->protocol;
685 if (ipnn->protocol < 0) ipnn->protocol = 0;
686 ipn_protocol_table[ipnn->protocol]->refcnt++;
687 ipnn->numreaders=0;
688 ipnn->numwriters=0;
689 ipnn->maxports=parms.maxports;
690 atomic_set(&ipnn->msgpool_nelem,0);
691 ipnn->msgpool_size=parms.msgpoolsize;
692 ipnn->proto_private=NULL;
693 init_waitqueue_head(&ipnn->send_wait);
694 ipnn->chrdev=NULL;
695 err=ipn_protocol_table[ipnn->protocol]->ipn_p_newnet(ipnn);
696 if (err)
697 goto out_mknod_dput_putmodule;
698 #ifndef IPN_PRE2625
699 ipn_insert_network(&ipn_network_table[nd.path.dentry->d_inode->i_ino & (IPN_HASH_SIZE-1)],ipnn);
700 #else
701 ipn_insert_network(&ipn_network_table[nd.dentry->d_inode->i_ino & (IPN_HASH_SIZE-1)],ipnn);
702 #endif
703 } else {
704 /* join an existing network */
705 if (parms.flags & IPN_FLAG_EXCL) {
706 err=-EEXIST;
707 goto put_fail;
709 err = inode_permission(nd.path.dentry->d_inode, MAY_EXEC);
710 if (err)
711 goto put_fail;
712 err = -ECONNREFUSED;
713 #ifndef IPN_PRE2625
714 if (!S_ISSOCK(nd.path.dentry->d_inode->i_mode))
715 goto put_fail;
716 ipnn=ipn_find_network_byinode(nd.path.dentry->d_inode);
717 #else
718 if (!S_ISSOCK(nd.dentry->d_inode->i_mode))
719 goto put_fail;
720 ipnn=ipn_find_network_byinode(nd.dentry->d_inode);
721 #endif
722 if (!ipnn || (ipnn->flags & IPN_FLAG_TERMINATED) ||
723 (ipnn->flags & IPN_FLAG_EXCL))
724 goto put_fail;
726 if (ipn_node->pbp) {
727 kfree(ipn_node->pbp);
728 ipn_node->pbp=NULL;
730 ipn_node_bind(ipn_node,ipnn);
731 up(&ipn_glob_mutex);
732 return 0;
734 put_fail:
735 #ifndef IPN_PRE2625
736 path_put(&nd.path);
737 #else
738 path_release(&nd);
739 #endif
740 out:
741 up(&ipn_glob_mutex);
742 return err;
744 out_mknod_dput_putmodule:
745 module_put(THIS_MODULE);
746 out_mknod_dput_ipnn2:
747 kfree(ipnn->connport);
748 out_mknod_dput_ipnn:
749 kmem_cache_free(ipn_network_cache,ipnn);
750 out_mknod_dput:
751 dput(dentry);
752 out_mknod_unlock:
753 #ifndef IPN_PRE2625
754 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
755 path_put(&nd.path);
756 #else
757 mutex_unlock(&nd.dentry->d_inode->i_mutex);
758 path_release(&nd);
759 #endif
760 out_mknod_parent:
761 if (err==-EEXIST)
762 err=-EADDRINUSE;
763 up(&ipn_glob_mutex);
764 return err;
767 /* IPN CONNECT */
768 static int ipn_connect(struct socket *sock, struct sockaddr *addr,
769 int addr_len, int flags){
770 struct sockaddr_un *sunaddr=(struct sockaddr_un*)addr;
771 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
772 struct nameidata nd;
773 struct ipn_network *ipnn,*previousipnn;
774 int err=0;
775 int portno;
777 /* the socket cannot be connected twice */
778 if (sock->state != SS_UNCONNECTED)
779 return EISCONN;
781 if (down_interruptible(&ipn_glob_mutex))
782 return -ERESTARTSYS;
784 if ((previousipnn=ipn_node->ipn) == NULL) { /* unbound */
785 unsigned char mustshutdown=0;
786 err = ipn_mkname(sunaddr, addr_len);
787 if (err < 0)
788 goto out;
789 addr_len=err;
790 err = path_lookup(sunaddr->sun_path, LOOKUP_FOLLOW, &nd);
791 if (err)
792 goto out;
793 err = inode_permission(nd.path.dentry->d_inode, MAY_READ);
794 if (err) {
795 if (err == -EACCES || err == -EROFS)
796 mustshutdown|=RCV_SHUTDOWN;
797 else
798 goto put_fail;
800 err = inode_permission(nd.path.dentry->d_inode, MAY_WRITE);
801 if (err) {
802 if (err == -EACCES)
803 mustshutdown|=SEND_SHUTDOWN;
804 else
805 goto put_fail;
807 mustshutdown |= ipn_node->shutdown;
808 /* if the combination of shutdown and permissions leaves
809 * no abilities, connect returns EACCES */
810 if (mustshutdown == SHUTDOWN_XMASK) {
811 err=-EACCES;
812 goto put_fail;
813 } else {
814 err=0;
815 ipn_node->shutdown=mustshutdown;
817 #ifndef IPN_PRE2625
818 if (!S_ISSOCK(nd.path.dentry->d_inode->i_mode)) {
819 err = -ECONNREFUSED;
820 goto put_fail;
822 ipnn=ipn_find_network_byinode(nd.path.dentry->d_inode);
823 #else
824 if (!S_ISSOCK(nd.dentry->d_inode->i_mode)) {
825 err = -ECONNREFUSED;
826 goto put_fail;
828 ipnn=ipn_find_network_byinode(nd.dentry->d_inode);
829 #endif
830 if (!ipnn || (ipnn->flags & IPN_FLAG_TERMINATED)) {
831 err = -ECONNREFUSED;
832 goto put_fail;
834 if (ipn_node->protocol == IPN_ANY)
835 ipn_node->protocol=ipnn->protocol;
836 else if (ipnn->protocol != ipn_node->protocol) {
837 err = -EPROTO;
838 goto put_fail;
840 #ifndef IPN_PRE2625
841 path_put(&nd.path);
842 #else
843 path_release(&nd);
844 #endif
845 ipn_node->ipn=ipnn;
846 } else
847 ipnn=ipn_node->ipn;
849 if (down_interruptible(&ipnn->ipnn_mutex)) {
850 err=-ERESTARTSYS;
851 goto out;
853 portno = ipn_protocol_table[ipnn->protocol]->ipn_p_newport(ipn_node);
854 if (portno >= 0 && portno<ipnn->maxports) {
855 sock->state = SS_CONNECTED;
856 ipn_node->portno=portno;
857 ipnn->connport[portno]=ipn_node;
858 if (!(ipn_node->flags & IPN_NODEFLAG_BOUND))
859 ipnn->refcnt++;
860 else
861 list_del(&ipn_node->nodelist);
862 list_add_tail(&ipn_node->nodelist,&ipnn->connectqueue);
863 ipn_net_update_counters(ipnn,
864 (ipn_node->shutdown & RCV_SHUTDOWN)?0:1,
865 (ipn_node->shutdown & SEND_SHUTDOWN)?0:1);
866 } else {
867 ipn_node->ipn=previousipnn; /* undo changes on ipn_node->ipn */
868 err=-EADDRNOTAVAIL;
870 up(&ipnn->ipnn_mutex);
871 up(&ipn_glob_mutex);
872 return err;
874 put_fail:
875 #ifndef IPN_PRE2625
876 path_put(&nd.path);
877 #else
878 path_release(&nd);
879 #endif
880 out:
881 up(&ipn_glob_mutex);
882 return err;
885 int ipn_node_create_connect(struct ipn_node **ipn_node_out,
886 struct ipn_network *(* ipnn_map)(void *),void *ipnn_map_arg) {
887 struct ipn_node *ipn_node;
888 struct ipn_network *ipnn;
889 int err=0;
890 int portno;
891 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
892 ipn_node=ipn_node_create(NULL);
893 #else
894 ipn_node=ipn_node_create(&init_net);
895 #endif
896 if (down_interruptible(&ipn_glob_mutex)) {
897 err=-ERESTARTSYS;
898 goto err_ipn_node_release;
900 ipnn=ipnn_map(ipnn_map_arg);
901 ipn_node->ipn=ipnn;
902 ipn_node->protocol=ipnn->protocol;
903 if (down_interruptible(&ipnn->ipnn_mutex)) {
904 err=-ERESTARTSYS;
905 goto out_glob;
907 portno = ipn_protocol_table[ipnn->protocol]->ipn_p_newport(ipn_node);
908 if (portno >= 0 && portno<ipnn->maxports) {
909 ipn_node->portno=portno;
910 ipnn->connport[portno]=ipn_node;
911 if (!(ipn_node->flags & IPN_NODEFLAG_BOUND)) {
912 ipnn->refcnt++;
913 list_del(&ipn_node->nodelist);
915 *ipn_node_out=ipn_node;
916 list_add_tail(&ipn_node->nodelist,&ipnn->connectqueue);
917 ipn_net_update_counters(ipnn,
918 (ipn_node->shutdown & RCV_SHUTDOWN)?0:1,
919 (ipn_node->shutdown & SEND_SHUTDOWN)?0:1);
920 } else {
921 ipn_node->ipn=NULL;
922 err=-EADDRNOTAVAIL;
923 goto out_glob_ipnn;
926 up(&ipnn->ipnn_mutex);
927 up(&ipn_glob_mutex);
928 return err;
929 out_glob_ipnn:
930 up(&ipnn->ipnn_mutex);
931 out_glob:
932 up(&ipn_glob_mutex);
933 err_ipn_node_release:
934 ipn_node_release(ipn_node);
935 ipn_node_out=NULL;
936 return err;
939 int ipn_node_connect(struct ipn_node *ipn_node)
941 struct ipn_network *ipnn;
942 int err=0;
943 int portno;
944 if (down_interruptible(&ipn_glob_mutex))
945 return -ERESTARTSYS;
947 ipnn=ipn_node->ipn;
948 if (down_interruptible(&ipnn->ipnn_mutex)) {
949 err=-ERESTARTSYS;
950 goto out;
952 portno = ipn_protocol_table[ipnn->protocol]->ipn_p_newport(ipn_node);
953 if (portno >= 0 && portno<ipnn->maxports) {
954 ipn_node->portno=portno;
955 ipnn->connport[portno]=ipn_node;
956 if (!(ipn_node->flags & IPN_NODEFLAG_BOUND)) {
957 ipnn->refcnt++;
958 list_del(&ipn_node->nodelist);
960 list_add_tail(&ipn_node->nodelist,&ipnn->connectqueue);
961 ipn_net_update_counters(ipnn,
962 (ipn_node->shutdown & RCV_SHUTDOWN)?0:1,
963 (ipn_node->shutdown & SEND_SHUTDOWN)?0:1);
964 } else {
965 ipn_node->ipn=NULL;
966 err=-EADDRNOTAVAIL;
969 up(&ipnn->ipnn_mutex);
970 up(&ipn_glob_mutex);
971 return err;
972 out:
973 up(&ipn_glob_mutex);
974 return err;
977 static int ipn_getname(struct socket *sock, struct sockaddr *uaddr,
978 int *uaddr_len, int peer) {
979 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
980 struct ipn_network *ipnn=ipn_node->ipn;
981 struct sockaddr_un *sunaddr=(struct sockaddr_un *)uaddr;
982 int err=0;
984 if (down_interruptible(&ipn_glob_mutex))
985 return -ERESTARTSYS;
986 if (ipnn) {
987 *uaddr_len = ipnn->sunaddr_len;
988 memcpy(sunaddr,&ipnn->sunaddr,*uaddr_len);
989 } else
990 err = -ENOTCONN;
991 up(&ipn_glob_mutex);
992 return err;
995 /* IPN POLL */
996 unsigned int ipn_node_poll(struct ipn_node *ipn_node, struct file *file, poll_table *wait) {
997 struct ipn_network *ipnn=ipn_node->ipn;
998 unsigned int mask=0;
1000 if (ipnn) {
1001 poll_wait(file,&ipn_node->read_wait,wait);
1002 if (ipnn->flags & IPN_FLAG_LOSSLESS)
1003 poll_wait(file,&ipnn->send_wait,wait);
1004 /* POLLIN if recv succeeds,
1005 * POLL{PRI,RDNORM} if there are {oob,non-oob} messages */
1006 if (ipn_node->totmsgcount > 0) mask |= POLLIN;
1007 if (!(list_empty(&ipn_node->msgqueue))) mask |= POLLRDNORM;
1008 if (!(list_empty(&ipn_node->oobmsgqueue))) mask |= POLLPRI;
1009 if ((!(ipnn->flags & IPN_FLAG_LOSSLESS)) |
1010 (atomic_read(&ipnn->msgpool_nelem) < ipnn->msgpool_size))
1011 mask |= POLLOUT | POLLWRNORM;
1013 return mask;
1016 static unsigned int ipn_poll(struct file *file, struct socket *sock,
1017 poll_table *wait) {
1018 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
1019 return ipn_node_poll(ipn_node, file, wait);
1022 /* connect netdev (from ioctl). connect a bound socket to a
1023 * network device TAP or GRAB */
1024 static int ipn_connect_netdev(struct socket *sock,struct ifreq *ifr)
1026 int err=0;
1027 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
1028 struct ipn_network *ipnn=ipn_node->ipn;
1029 if (!capable(CAP_NET_ADMIN))
1030 return -EPERM;
1031 if (sock->state != SS_UNCONNECTED)
1032 return -EISCONN;
1033 if (!ipnn)
1034 return -ENOTCONN; /* Maybe we need a different error for "NOT BOUND" */
1035 if (down_interruptible(&ipn_glob_mutex))
1036 return -ERESTARTSYS;
1037 if (down_interruptible(&ipnn->ipnn_mutex)) {
1038 up(&ipn_glob_mutex);
1039 return -ERESTARTSYS;
1041 ipn_node->netdev=ipn_netdev_alloc(ipn_node->net,ifr->ifr_flags,ifr->ifr_name,&err);
1042 if (ipn_node->netdev) {
1043 int portno;
1044 portno = ipn_protocol_table[ipnn->protocol]->ipn_p_newport(ipn_node);
1045 if (portno >= 0 && portno<ipnn->maxports) {
1046 sock->state = SS_CONNECTED;
1047 ipn_node->portno=portno;
1048 ipn_node->flags |= ifr->ifr_flags & IPN_NODEFLAG_DEVMASK;
1049 ipnn->connport[portno]=ipn_node;
1050 err=ipn_netdev_activate(ipn_node);
1051 if (err) {
1052 sock->state = SS_UNCONNECTED;
1053 ipn_protocol_table[ipnn->protocol]->ipn_p_delport(ipn_node);
1054 ipn_node->netdev=NULL;
1055 ipn_node->portno= -1;
1056 ipn_node->flags &= ~IPN_NODEFLAG_DEVMASK;
1057 ipnn->connport[portno]=NULL;
1058 } else {
1059 ipn_protocol_table[ipnn->protocol]->ipn_p_postnewport(ipn_node);
1060 list_del(&ipn_node->nodelist);
1061 list_add_tail(&ipn_node->nodelist,&ipnn->connectqueue);
1063 } else {
1064 ipn_netdev_close(ipn_node);
1065 err=-EADDRNOTAVAIL;
1066 ipn_node->netdev=NULL;
1068 } else
1069 err=-EINVAL;
1070 up(&ipnn->ipnn_mutex);
1071 up(&ipn_glob_mutex);
1072 return err;
1075 /* join a netdev, a socket gets connected to a persistent node
1076 * not connected to another socket */
1077 static int ipn_join_netdev(struct socket *sock,struct ifreq *ifr)
1079 int err=0;
1080 struct net_device *dev;
1081 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
1082 struct ipn_node *ipn_joined;
1083 struct ipn_network *ipnn=ipn_node->ipn;
1084 if (sock->state != SS_UNCONNECTED)
1085 return -EISCONN;
1086 if (down_interruptible(&ipn_glob_mutex))
1087 return -ERESTARTSYS;
1088 if (down_interruptible(&ipnn->ipnn_mutex)) {
1089 up(&ipn_glob_mutex);
1090 return -ERESTARTSYS;
1092 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
1093 dev=__dev_get_by_name(ifr->ifr_name);
1094 #else
1095 dev=__dev_get_by_name(ipn_node->net,ifr->ifr_name);
1096 #endif
1097 if (!dev)
1098 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
1099 dev=__dev_get_by_index(ifr->ifr_ifindex);
1100 #else
1101 dev=__dev_get_by_index(ipn_node->net,ifr->ifr_ifindex);
1102 #endif
1103 if (dev && (ipn_joined=ipn_netdev2node(dev)) != NULL) { /* the interface does exist */
1104 int i;
1105 for (i=0;i<ipnn->maxports && ipn_joined != ipnn->connport[i] ;i++)
1107 if (i < ipnn->maxports) { /* found */
1108 /* ipn_joined is substituted to ipn_node */
1109 ((struct ipn_sock *)sock->sk)->node=ipn_joined;
1110 ipn_joined->flags |= IPN_NODEFLAG_INUSE;
1111 ipnn->refcnt--;
1112 kmem_cache_free(ipn_node_cache,ipn_node);
1113 } else
1114 err=-EPERM;
1115 } else
1116 err=-EADDRNOTAVAIL;
1117 up(&ipnn->ipnn_mutex);
1118 up(&ipn_glob_mutex);
1119 return err;
1122 /* set persistence of a node looking for it by interface name
1123 * (it is for sysadm, to close network interfaces)*/
1124 static int ipn_setpersist_netdev(struct ifreq *ifr, int value)
1126 struct net_device *dev;
1127 struct ipn_node *ipn_node;
1128 int err=0;
1129 if (!capable(CAP_NET_ADMIN))
1130 return -EPERM;
1131 if (down_interruptible(&ipn_glob_mutex))
1132 return -ERESTARTSYS;
1133 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
1134 dev=__dev_get_by_name(ifr->ifr_name);
1135 #else
1136 dev=__dev_get_by_name(&init_net,ifr->ifr_name);
1137 #endif
1138 if (!dev)
1139 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
1140 dev=__dev_get_by_index(ifr->ifr_ifindex);
1141 #else
1142 dev=__dev_get_by_index(&init_net,ifr->ifr_ifindex);
1143 #endif
1144 if (dev && (ipn_node=ipn_netdev2node(dev)) != NULL)
1145 _ipn_setpersist(ipn_node,value);
1146 else
1147 err=-EADDRNOTAVAIL;
1148 up(&ipn_glob_mutex);
1149 return err;
1152 /* IPN IOCTL */
1154 int ipn_node_ioctl(struct ipn_node *ipn_node, unsigned int cmd, unsigned long arg) {
1155 struct ipn_network *ipnn=ipn_node->ipn;
1156 if (ipnn &&
1157 (ipn_protocol_table[ipn_node->protocol]->ipn_p_ioctl != NULL)) {
1158 int rv;
1159 if (down_interruptible(&ipnn->ipnn_mutex))
1160 return -ERESTARTSYS;
1161 rv=ipn_protocol_table[ipn_node->protocol]->ipn_p_ioctl(ipn_node,cmd,arg);
1162 up(&ipnn->ipnn_mutex);
1163 return rv;
1164 } else
1165 return -EOPNOTSUPP;
1168 static int ipn_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) {
1169 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
1170 struct ipn_network *ipnn=ipn_node->ipn;
1171 void __user* argp = (void __user*)arg;
1172 struct ifreq ifr;
1173 struct chrdevreq devr;
1175 if (ipn_node->shutdown == SHUTDOWN_XMASK)
1176 return -ECONNRESET;
1178 /* get arguments */
1179 switch (cmd) {
1180 case IPN_CHECK:
1181 return IPN_CHECK;
1182 case IPN_SETPERSIST_NETDEV:
1183 case IPN_CLRPERSIST_NETDEV:
1184 case IPN_CONN_NETDEV:
1185 case IPN_JOIN_NETDEV:
1186 case SIOCSIFHWADDR:
1187 if (copy_from_user(&ifr, argp, sizeof(ifr)))
1188 return -EFAULT;
1189 ifr.ifr_name[IFNAMSIZ-1] = '\0';
1190 break;
1191 case IPN_REGISTER_CHRDEV:
1192 case IPN_JOIN_CHRDEV:
1193 if (copy_from_user(&devr, argp, sizeof(devr)))
1194 return -EFAULT;
1195 /*printk("IPN_REGISTER_CHRDEV %p %d %d %d\n",
1196 argp, sizeof(devr),devr.major, devr.minor);*/
1197 break;
1200 /* actions for unconnected and unbound sockets */
1201 switch (cmd) {
1202 case IPN_SETPERSIST_NETDEV:
1203 return ipn_setpersist_netdev(&ifr,1);
1204 case IPN_CLRPERSIST_NETDEV:
1205 return ipn_setpersist_netdev(&ifr,0);
1206 case IPN_JOIN_CHRDEV:
1208 int rv;
1209 if (!capable(CAP_MKNOD))
1210 return -EPERM;
1211 if (ipn_node->ipn != NULL)
1212 return -EISCONN;
1213 if (down_interruptible(&ipn_glob_mutex))
1214 return -ERESTARTSYS;
1215 rv=ipn_node_bind(ipn_node, ipn_find_chrdev(&devr));
1216 up(&ipn_glob_mutex);
1217 return rv;
1219 case SIOCSIFHWADDR:
1220 if (capable(CAP_NET_ADMIN))
1221 return -EPERM;
1222 if (ipn_node->netdev && (ipn_node->flags &IPN_NODEFLAG_TAP))
1223 return dev_set_mac_address(ipn_node->netdev, &ifr.ifr_hwaddr);
1224 else
1225 return -EADDRNOTAVAIL;
1227 if (ipnn == NULL || (ipnn->flags & IPN_FLAG_TERMINATED))
1228 return -ENOTCONN;
1229 /* actions for connected or bound sockets */
1230 switch (cmd) {
1231 case IPN_CONN_NETDEV:
1232 return ipn_connect_netdev(sock,&ifr);
1233 case IPN_JOIN_NETDEV:
1234 return ipn_join_netdev(sock,&ifr);
1235 case IPN_SETPERSIST:
1236 return ipn_setpersist(ipn_node,arg);
1237 case IPN_CHRDEV_PERSIST:
1238 return ipn_chrdev_persistence(ipnn,arg);
1239 case IPN_REGISTER_CHRDEV:
1241 int rv;
1242 unsigned int reqmajor=devr.major;
1243 if (down_interruptible(&ipnn->ipnn_mutex))
1244 return -ERESTARTSYS;
1245 rv=ipn_register_chrdev(ipnn,&devr);
1246 if (reqmajor==0 && rv==0) {
1247 if (copy_to_user(argp, &devr, sizeof devr))
1248 rv=EFAULT;
1250 up(&ipnn->ipnn_mutex);
1251 return rv;
1253 case IPN_UNREGISTER_CHRDEV:
1255 int rv;
1256 if (down_interruptible(&ipnn->ipnn_mutex))
1257 return -ERESTARTSYS;
1258 rv=ipn_deregister_chrdev(ipnn);
1259 up(&ipnn->ipnn_mutex);
1260 return rv;
1262 default:
1263 return ipn_node_ioctl(ipn_node, cmd, arg);
1267 /* shutdown: close socket for input or for output.
1268 * shutdown can be called prior to connect and it is not reversible */
1269 static int ipn_shutdown(struct socket *sock, int mode) {
1270 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
1271 struct ipn_network *ipnn=ipn_node->ipn;
1272 int oldshutdown=ipn_node->shutdown;
1273 mode = (mode+1)&(RCV_SHUTDOWN|SEND_SHUTDOWN);
1275 /* define the new mode first... */
1276 ipn_node->shutdown |= mode;
1278 if(ipnn) {
1279 /* ... and wait for all pending ops to be completed */
1280 if (down_interruptible(&ipnn->ipnn_mutex)) {
1281 ipn_node->shutdown = oldshutdown;
1282 return -ERESTARTSYS;
1284 oldshutdown=ipn_node->shutdown-oldshutdown;
1285 if (sock->state == SS_CONNECTED && oldshutdown) {
1286 ipn_net_update_counters(ipnn,
1287 (ipn_node->shutdown & RCV_SHUTDOWN)?0:-1,
1288 (ipn_node->shutdown & SEND_SHUTDOWN)?0:-1);
1291 /* if recv channel has been shut down, flush the recv queue */
1292 if ((ipn_node->shutdown & RCV_SHUTDOWN))
1293 ipn_flush_recvqueue(ipn_node);
1294 up(&ipnn->ipnn_mutex);
1296 return 0;
1299 /* injectmsg: a new message is entering the ipn network.
1300 * injectmsg gets called by send and by the grab/tap node */
1301 /* handlemsg is protected from ipn_network changes: ipnn->ipnn_mutex is locked */
1302 int ipn_proto_injectmsg(struct ipn_node *from, struct msgpool_item *msg)
1304 struct ipn_network *ipnn=from->ipn;
1305 int err=0;
1306 if (down_interruptible(&ipnn->ipnn_mutex))
1307 err=-ERESTARTSYS;
1308 else {
1309 ipn_protocol_table[ipnn->protocol]->ipn_p_handlemsg(from, msg);
1310 up(&ipnn->ipnn_mutex);
1312 return err;
1315 /* SEND MSG */
1316 int ipn_node_write(struct ipn_node *ipn_node, struct iovec *msg_iov, int len) {
1317 struct ipn_network *ipnn=ipn_node->ipn;
1318 struct msgpool_item *newmsg;
1319 int err=0;
1321 if (unlikely(ipn_node->shutdown & SEND_SHUTDOWN)) {
1322 if (ipn_node->shutdown == SHUTDOWN_XMASK)
1323 return -ECONNRESET;
1324 else
1325 return -EPIPE;
1327 if (len > ipnn->mtu)
1328 return -EOVERFLOW;
1329 newmsg=ipn_msgpool_alloc_locking(ipnn,len);
1330 if (!newmsg)
1331 return -ENOMEM;
1332 newmsg->len=len;
1333 err=memcpy_fromiovec(newmsg->data, msg_iov, len);
1334 if (!err)
1335 ipn_proto_injectmsg(ipn_node, newmsg);
1336 ipn_msgpool_put(newmsg,ipnn);
1337 return len;
1340 static int ipn_sendmsg(struct kiocb *kiocb, struct socket *sock,
1341 struct msghdr *msg, size_t len) {
1342 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
1344 if (unlikely(sock->state != SS_CONNECTED))
1345 return -ENOTCONN;
1346 else
1347 return ipn_node_write(ipn_node, msg->msg_iov, len);
1350 /* ipn_proto_sendmsg is called by protocol implementation to enqueue a
1351 * for a destination (to).*/
1352 /* enqueue a msgitem on the receiver's queue, the queue is protected by a
1353 * spinlock (msglock) */
1354 void ipn_proto_sendmsg(struct ipn_node *to, struct msgpool_item *msg)
1356 if (to) {
1357 if (to->netdev) {
1358 ipn_netdev_sendmsg(to,msg);
1359 } else {
1360 /* socket send */
1361 struct msgitem *msgitem;
1362 struct ipn_network *ipnn=to->ipn;
1363 spin_lock(&to->msglock);
1364 if (likely((to->shutdown & RCV_SHUTDOWN)==0)) {
1365 /*if (unlikely((ipnn->flags & IPN_FLAG_LOSSLESS) == 0 ||
1366 to->totmsgcount >= ipnn->msgpool_size))
1367 yield();*/
1368 if (likely(ipnn->flags & IPN_FLAG_LOSSLESS ||
1369 to->totmsgcount < ipnn->msgpool_size)) {
1370 if ((msgitem=kmem_cache_alloc(ipn_msgitem_cache,GFP_KERNEL))!=NULL) {
1371 msgitem->msg=msg;
1372 to->totmsgcount++;
1373 list_add_tail(&msgitem->list, &to->msgqueue);
1374 ipn_msgpool_hold(msg);
1378 spin_unlock(&to->msglock);
1379 wake_up_interruptible(&to->read_wait);
1384 /* enqueue an oob message. "to" is the destination */
1385 void ipn_proto_oobsendmsg(struct ipn_node *to, struct msgpool_item *msg)
1387 if (to) {
1388 if (!to->netdev) { /* no oob to netdev */
1389 struct msgitem *msgitem;
1390 struct ipn_network *ipnn=to->ipn;
1391 spin_lock(&to->msglock);
1392 if ((to->shutdown & RCV_SHUTDOWN_NO_OOB) == 0 &&
1393 (ipnn->flags & IPN_FLAG_LOSSLESS ||
1394 to->oobmsgcount < ipnn->msgpool_size)) {
1395 if ((msgitem=kmem_cache_alloc(ipn_msgitem_cache,GFP_KERNEL))!=NULL) {
1396 msgitem->msg=msg;
1397 to->totmsgcount++;
1398 to->oobmsgcount++;
1399 list_add_tail(&msgitem->list, &to->oobmsgqueue);
1400 ipn_msgpool_hold(msg);
1403 spin_unlock(&to->msglock);
1404 wake_up_interruptible(&to->read_wait);
1409 /* IPN RECV */
1410 int ipn_node_read(struct ipn_node *ipn_node, struct iovec *msg_iov, size_t len, int *msg_flags, int flags) {
1411 struct ipn_network *ipnn=ipn_node->ipn;
1412 struct msgitem *msgitem;
1413 struct msgpool_item *currmsg;
1415 if (unlikely((ipn_node->shutdown & XRCV_SHUTDOWN) == XRCV_SHUTDOWN)) {
1416 if (ipn_node->shutdown == SHUTDOWN_XMASK) /*EOF, nothing can be read*/
1417 return 0;
1418 else
1419 return -EPIPE; /*trying to read on a write only node */
1422 /* wait for a message */
1423 spin_lock(&ipn_node->msglock);
1424 while (ipn_node->totmsgcount == 0) {
1425 spin_unlock(&ipn_node->msglock);
1426 if (wait_event_interruptible(ipn_node->read_wait,
1427 !(ipn_node->totmsgcount == 0)))
1428 return -ERESTARTSYS;
1429 spin_lock(&ipn_node->msglock);
1431 /* oob gets delivered first. oob are rare */
1432 if (likely(list_empty(&ipn_node->oobmsgqueue)))
1433 msgitem=list_first_entry(&ipn_node->msgqueue, struct msgitem, list);
1434 else {
1435 msgitem=list_first_entry(&ipn_node->oobmsgqueue, struct msgitem, list);
1436 *msg_flags |= MSG_OOB;
1437 ipn_node->oobmsgcount--;
1439 list_del(&msgitem->list);
1440 ipn_node->totmsgcount--;
1441 spin_unlock(&ipn_node->msglock);
1442 currmsg=msgitem->msg;
1443 if (currmsg->len < len)
1444 len=currmsg->len;
1445 memcpy_toiovec(msg_iov, currmsg->data, len);
1446 ipn_msgpool_put(currmsg,ipnn);
1447 kmem_cache_free(ipn_msgitem_cache,msgitem);
1448 return len;
1451 static int ipn_recvmsg(struct kiocb *kiocb, struct socket *sock,
1452 struct msghdr *msg, size_t len, int flags) {
1453 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
1455 if (unlikely(sock->state != SS_CONNECTED))
1456 return -ENOTCONN;
1457 else
1458 return ipn_node_read(ipn_node, msg->msg_iov, len, &msg->msg_flags, flags);
1461 /* resize a network: change the # of communication ports (connport) */
1462 static int ipn_netresize(struct ipn_network *ipnn,int newsize)
1464 int oldsize,min;
1465 struct ipn_node **newconnport;
1466 struct ipn_node **oldconnport;
1467 int err;
1468 if (down_interruptible(&ipnn->ipnn_mutex))
1469 return -ERESTARTSYS;
1470 oldsize=ipnn->maxports;
1471 if (newsize == oldsize) {
1472 up(&ipnn->ipnn_mutex);
1473 return 0;
1475 min=oldsize;
1476 /* shrink a network. all the ports we are going to eliminate
1477 * must be unused! */
1478 if (newsize < oldsize) {
1479 int i;
1480 for (i=newsize; i<oldsize; i++)
1481 if (ipnn->connport[i]) {
1482 up(&ipnn->ipnn_mutex);
1483 return -EADDRINUSE;
1485 min=newsize;
1487 oldconnport=ipnn->connport;
1488 /* allocate the new connport array and copy the old one */
1489 newconnport=kzalloc(newsize * sizeof(struct ipn_node *),GFP_KERNEL);
1490 if (!newconnport) {
1491 up(&ipnn->ipnn_mutex);
1492 return -ENOMEM;
1494 memcpy(newconnport,oldconnport,min * sizeof(struct ipn_node *));
1495 ipnn->connport=newconnport;
1496 ipnn->maxports=newsize;
1497 /* notify the protocol that the netowrk has been resized */
1498 err=ipn_protocol_table[ipnn->protocol]->ipn_p_resizenet(ipnn,oldsize,newsize);
1499 if (err) {
1500 /* roll back if the resize operation failed for the protocol */
1501 ipnn->connport=oldconnport;
1502 ipnn->maxports=oldsize;
1503 kfree(newconnport);
1504 } else
1505 /* successful mission, network resized */
1506 kfree(oldconnport);
1507 up(&ipnn->ipnn_mutex);
1508 return err;
1511 /* IPN SETSOCKOPT */
1512 static int ipn_setsockopt(struct socket *sock, int level, int optname,
1513 char __user *optval, int optlen) {
1514 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
1515 struct ipn_network *ipnn=ipn_node->ipn;
1517 if (ipn_node->shutdown == SHUTDOWN_XMASK)
1518 return -ECONNRESET;
1519 if (level != 0 && level != ipn_node->protocol+1)
1520 return -EPROTONOSUPPORT;
1521 if (level > 0) {
1522 /* protocol specific sockopt */
1523 if (ipnn) {
1524 int rv;
1525 if (down_interruptible(&ipnn->ipnn_mutex))
1526 return -ERESTARTSYS;
1527 rv=ipn_protocol_table[ipn_node->protocol]->ipn_p_setsockopt(ipn_node,optname,optval,optlen);
1528 up(&ipnn->ipnn_mutex);
1529 return rv;
1530 } else
1531 return -EOPNOTSUPP;
1532 } else {
1533 if (optname == IPN_SO_DESCR) {
1534 if (optlen > IPN_DESCRLEN)
1535 return -EINVAL;
1536 else {
1537 memset(ipn_node->descr,0,IPN_DESCRLEN);
1538 if (copy_from_user(ipn_node->descr,optval,optlen))
1539 ipn_node->descr[0]=0;
1540 else
1541 ipn_node->descr[optlen-1]=0;
1542 return 0;
1544 } else {
1545 if (optlen < sizeof(int))
1546 return -EINVAL;
1547 else if ((optname & IPN_SO_PREBIND) && (ipnn != NULL))
1548 return -EISCONN;
1549 else {
1550 int val;
1551 get_user(val, (int __user *) optval);
1552 if ((optname & IPN_SO_PREBIND) && !ipn_node->pbp) {
1553 struct pre_bind_parms std=STD_BIND_PARMS;
1554 ipn_node->pbp=kzalloc(sizeof(struct pre_bind_parms),GFP_KERNEL);
1555 if (!ipn_node->pbp)
1556 return -ENOMEM;
1557 *(ipn_node->pbp)=std;
1559 switch (optname) {
1560 case IPN_SO_PORT:
1561 if (sock->state == SS_UNCONNECTED)
1562 ipn_node->portno=val;
1563 else
1564 return -EISCONN;
1565 break;
1566 case IPN_SO_CHANGE_NUMNODES:
1567 if ((ipn_node->flags & IPN_NODEFLAG_BOUND)!=0) {
1568 if (val <= 0)
1569 return -EINVAL;
1570 else
1571 return ipn_netresize(ipnn,val);
1572 } else
1573 val=-ENOTCONN;
1574 break;
1575 case IPN_SO_WANT_OOB_NUMNODES:
1576 if (val)
1577 ipn_node->flags |= IPN_NODEFLAG_OOB_NUMNODES;
1578 else
1579 ipn_node->flags &= ~IPN_NODEFLAG_OOB_NUMNODES;
1580 break;
1581 case IPN_SO_HANDLE_OOB:
1582 if (val)
1583 ipn_node->shutdown &= ~RCV_SHUTDOWN_NO_OOB;
1584 else
1585 ipn_node->shutdown |= RCV_SHUTDOWN_NO_OOB;
1586 break;
1587 case IPN_SO_MTU:
1588 if (val <= 0)
1589 return -EINVAL;
1590 else
1591 ipn_node->pbp->mtu=val;
1592 break;
1593 case IPN_SO_NUMNODES:
1594 if (val <= 0)
1595 return -EINVAL;
1596 else
1597 ipn_node->pbp->maxports=val;
1598 break;
1599 case IPN_SO_MSGPOOLSIZE:
1600 if (val <= 0)
1601 return -EINVAL;
1602 else
1603 ipn_node->pbp->msgpoolsize=val;
1604 break;
1605 case IPN_SO_FLAGS:
1606 ipn_node->pbp->flags=val;
1607 break;
1608 case IPN_SO_MODE:
1609 ipn_node->pbp->mode=val;
1610 break;
1612 return 0;
1618 /* IPN GETSOCKOPT */
1619 static int ipn_getsockopt(struct socket *sock, int level, int optname,
1620 char __user *optval, int __user *optlen) {
1621 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
1622 struct ipn_network *ipnn=ipn_node->ipn;
1623 int len;
1625 if (ipn_node->shutdown == SHUTDOWN_XMASK)
1626 return -ECONNRESET;
1627 if (level != 0 && level != ipn_node->protocol+1)
1628 return -EPROTONOSUPPORT;
1629 if (level > 0) {
1630 if (ipnn) {
1631 int rv;
1632 /* protocol specific sockopt */
1633 if (down_interruptible(&ipnn->ipnn_mutex))
1634 return -ERESTARTSYS;
1635 rv=ipn_protocol_table[ipn_node->protocol]->ipn_p_getsockopt(ipn_node,optname,optval,optlen);
1636 up(&ipnn->ipnn_mutex);
1637 return rv;
1638 } else
1639 return -EOPNOTSUPP;
1640 } else {
1641 if (get_user(len, optlen))
1642 return -EFAULT;
1643 if (optname == IPN_SO_DESCR) {
1644 if (len < IPN_DESCRLEN)
1645 return -EINVAL;
1646 else {
1647 if (len > IPN_DESCRLEN)
1648 len=IPN_DESCRLEN;
1649 if(put_user(len, optlen))
1650 return -EFAULT;
1651 if(copy_to_user(optval,ipn_node->descr,len))
1652 return -EFAULT;
1653 return 0;
1655 } else {
1656 int val=-2;
1657 switch (optname) {
1658 case IPN_SO_PORT:
1659 val=ipn_node->portno;
1660 break;
1661 case IPN_SO_MTU:
1662 if (ipnn)
1663 val=ipnn->mtu;
1664 else if (ipn_node->pbp)
1665 val=ipn_node->pbp->mtu;
1666 break;
1667 case IPN_SO_NUMNODES:
1668 if (ipnn)
1669 val=ipnn->maxports;
1670 else if (ipn_node->pbp)
1671 val=ipn_node->pbp->maxports;
1672 break;
1673 case IPN_SO_MSGPOOLSIZE:
1674 if (ipnn)
1675 val=ipnn->msgpool_size;
1676 else if (ipn_node->pbp)
1677 val=ipn_node->pbp->msgpoolsize;
1678 break;
1679 case IPN_SO_FLAGS:
1680 if (ipnn)
1681 val=ipnn->flags;
1682 else if (ipn_node->pbp)
1683 val=ipn_node->pbp->flags;
1684 break;
1685 case IPN_SO_MODE:
1686 if (ipnn)
1687 val=-1;
1688 else if (ipn_node->pbp)
1689 val=ipn_node->pbp->mode;
1690 break;
1692 if (val < -1)
1693 return -EINVAL;
1694 else {
1695 if (len < sizeof(int))
1696 return -EOVERFLOW;
1697 else {
1698 len = sizeof(int);
1699 if(put_user(len, optlen))
1700 return -EFAULT;
1701 if(copy_to_user(optval,&val,len))
1702 return -EFAULT;
1703 return 0;
1710 /* BROADCAST/HUB implementation */
1712 static int ipn_bcast_newport(struct ipn_node *newport) {
1713 struct ipn_network *ipnn=newport->ipn;
1714 int i;
1715 for (i=0;i<ipnn->maxports;i++) {
1716 if (ipnn->connport[i] == NULL)
1717 return i;
1719 return -1;
1722 static int ipn_bcast_handlemsg(struct ipn_node *from,
1723 struct msgpool_item *msgitem){
1724 struct ipn_network *ipnn=from->ipn;
1726 struct ipn_node *ipn_node;
1727 list_for_each_entry(ipn_node, &ipnn->connectqueue, nodelist) {
1728 if (ipn_node != from)
1729 ipn_proto_sendmsg(ipn_node,msgitem);
1731 return 0;
1734 static void ipn_null_delport(struct ipn_node *oldport) {}
1735 static void ipn_null_postnewport(struct ipn_node *newport) {}
1736 static void ipn_null_predelport(struct ipn_node *oldport) {}
1737 static int ipn_null_newnet(struct ipn_network *newnet) {return 0;}
1738 static int ipn_null_resizenet(struct ipn_network *net,int oldsize,int newsize) {
1739 return 0;}
1740 static void ipn_null_delnet(struct ipn_network *oldnet) {}
1741 static int ipn_null_setsockopt(struct ipn_node *port,int optname,
1742 char __user *optval, int optlen) {return -EOPNOTSUPP;}
1743 static int ipn_null_getsockopt(struct ipn_node *port,int optname,
1744 char __user *optval, int *optlen) {return -EOPNOTSUPP;}
1745 static int ipn_null_ioctl(struct ipn_node *port,unsigned int request,
1746 unsigned long arg) {return -EOPNOTSUPP;}
1748 /* Protocol Registration/deregisteration */
1750 void ipn_init_protocol(struct ipn_protocol *p)
1752 if (p->ipn_p_delport == NULL) p->ipn_p_delport=ipn_null_delport;
1753 if (p->ipn_p_postnewport == NULL) p->ipn_p_postnewport=ipn_null_postnewport;
1754 if (p->ipn_p_predelport == NULL) p->ipn_p_predelport=ipn_null_predelport;
1755 if (p->ipn_p_newnet == NULL) p->ipn_p_newnet=ipn_null_newnet;
1756 if (p->ipn_p_resizenet == NULL) p->ipn_p_resizenet=ipn_null_resizenet;
1757 if (p->ipn_p_delnet == NULL) p->ipn_p_delnet=ipn_null_delnet;
1758 if (p->ipn_p_setsockopt == NULL) p->ipn_p_setsockopt=ipn_null_setsockopt;
1759 if (p->ipn_p_getsockopt == NULL) p->ipn_p_getsockopt=ipn_null_getsockopt;
1760 if (p->ipn_p_ioctl == NULL) p->ipn_p_ioctl=ipn_null_ioctl;
1763 int ipn_proto_register(int protocol,struct ipn_protocol *ipn_service)
1765 int rv=0;
1766 if (ipn_service->ipn_p_newport == NULL ||
1767 ipn_service->ipn_p_handlemsg == NULL)
1768 return -EINVAL;
1769 ipn_init_protocol(ipn_service);
1770 if (protocol > 1 && protocol <= IPN_MAX_PROTO) {
1771 protocol--;
1772 if (down_interruptible(&ipn_glob_mutex))
1773 return -ERESTARTSYS;
1774 if (ipn_protocol_table[protocol])
1775 rv= -EEXIST;
1776 else {
1777 ipn_service->refcnt=0;
1778 ipn_protocol_table[protocol]=ipn_service;
1779 printk(KERN_INFO "IPN: Registered protocol %d\n",protocol+1);
1781 up(&ipn_glob_mutex);
1782 } else
1783 rv= -EINVAL;
1784 return rv;
1787 int ipn_proto_deregister(int protocol)
1789 int rv=0;
1790 if (protocol > 1 && protocol <= IPN_MAX_PROTO) {
1791 protocol--;
1792 if (down_interruptible(&ipn_glob_mutex))
1793 return -ERESTARTSYS;
1794 if (ipn_protocol_table[protocol]) {
1795 if (ipn_protocol_table[protocol]->refcnt == 0) {
1796 ipn_protocol_table[protocol]=NULL;
1797 printk(KERN_INFO "IPN: Unregistered protocol %d\n",protocol+1);
1798 } else
1799 rv=-EADDRINUSE;
1800 } else
1801 rv= -ENOENT;
1802 up(&ipn_glob_mutex);
1803 } else
1804 rv= -EINVAL;
1805 return rv;
1808 /* MAIN SECTION */
1809 /* Module constructor/destructor */
1810 static struct net_proto_family ipn_family_ops = {
1811 .family = PF_IPN,
1812 .create = ipn_create,
1813 .owner = THIS_MODULE,
1816 /* IPN constructor */
1817 static int ipn_init(void)
1819 int rc;
1821 ipn_init_protocol(&ipn_bcast);
1822 ipn_network_cache=kmem_cache_create("ipn_network",sizeof(struct ipn_network),0,0,NULL);
1823 if (!ipn_network_cache) {
1824 printk(KERN_CRIT "%s: Cannot create ipn_network SLAB cache!\n",
1825 __FUNCTION__);
1826 rc=-ENOMEM;
1827 goto out;
1830 ipn_node_cache=kmem_cache_create("ipn_node",sizeof(struct ipn_node),0,0,NULL);
1831 if (!ipn_node_cache) {
1832 printk(KERN_CRIT "%s: Cannot create ipn_node SLAB cache!\n",
1833 __FUNCTION__);
1834 rc=-ENOMEM;
1835 goto out_net;
1838 ipn_msgitem_cache=kmem_cache_create("ipn_msgitem",sizeof(struct msgitem),0,0,NULL);
1839 if (!ipn_msgitem_cache) {
1840 printk(KERN_CRIT "%s: Cannot create ipn_msgitem SLAB cache!\n",
1841 __FUNCTION__);
1842 rc=-ENOMEM;
1843 goto out_net_node;
1846 rc=ipn_msgbuf_init();
1847 if (rc != 0) {
1848 printk(KERN_CRIT "%s: Cannot create ipn_msgbuf SLAB cache\n",
1849 __FUNCTION__);
1850 goto out_net_node_msg;
1853 rc=proto_register(&ipn_proto,1);
1854 if (rc != 0) {
1855 printk(KERN_CRIT "%s: Cannot register the protocol!\n",
1856 __FUNCTION__);
1857 goto out_net_node_msg_msgbuf;
1860 sock_register(&ipn_family_ops);
1861 ipn_netdev_init();
1862 printk(KERN_INFO "IPN: Virtual Square Project, University of Bologna 2007-09\n");
1863 return 0;
1865 out_net_node_msg_msgbuf:
1866 ipn_msgbuf_fini();
1867 out_net_node_msg:
1868 kmem_cache_destroy(ipn_msgitem_cache);
1869 out_net_node:
1870 kmem_cache_destroy(ipn_node_cache);
1871 out_net:
1872 kmem_cache_destroy(ipn_network_cache);
1873 out:
1874 return rc;
1877 /* IPN destructor */
1878 static void ipn_exit(void)
1880 ipn_netdev_fini();
1881 if (ipn_msgitem_cache)
1882 kmem_cache_destroy(ipn_msgitem_cache);
1883 if (ipn_node_cache)
1884 kmem_cache_destroy(ipn_node_cache);
1885 if (ipn_network_cache)
1886 kmem_cache_destroy(ipn_network_cache);
1887 ipn_msgbuf_fini();
1888 sock_unregister(PF_IPN);
1889 proto_unregister(&ipn_proto);
1890 printk(KERN_INFO "IPN removed\n");
1893 module_init(ipn_init);
1894 module_exit(ipn_exit);
1896 EXPORT_SYMBOL_GPL(ipn_proto_register);
1897 EXPORT_SYMBOL_GPL(ipn_proto_deregister);
1898 EXPORT_SYMBOL_GPL(ipn_proto_sendmsg);
1899 EXPORT_SYMBOL_GPL(ipn_proto_oobsendmsg);
1900 EXPORT_SYMBOL_GPL(ipn_msgpool_alloc);
1901 EXPORT_SYMBOL_GPL(ipn_msgpool_put);