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