Some parameters changed in net.h (2.6.32/2.6.33)
[vde.git] / ipn / af_ipn.c
blob90c95fa65df471ead8e8955d051c7e4841327ce2
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 if (down_interruptible(&ipnn->ipnn_mutex))
1318 err=-ERESTARTSYS;
1319 else {
1320 ipn_protocol_table[ipnn->protocol]->ipn_p_handlemsg(from, msg);
1321 up(&ipnn->ipnn_mutex);
1323 return err;
1326 /* SEND MSG */
1327 int ipn_node_write(struct ipn_node *ipn_node, struct iovec *msg_iov, int len) {
1328 struct ipn_network *ipnn=ipn_node->ipn;
1329 struct msgpool_item *newmsg;
1330 int err=0;
1332 if (unlikely(ipn_node->shutdown & SEND_SHUTDOWN)) {
1333 if (ipn_node->shutdown == SHUTDOWN_XMASK)
1334 return -ECONNRESET;
1335 else
1336 return -EPIPE;
1338 if (len > ipnn->mtu)
1339 return -EOVERFLOW;
1340 newmsg=ipn_msgpool_alloc_locking(ipnn,len);
1341 if (!newmsg)
1342 return -ENOMEM;
1343 newmsg->len=len;
1344 err=memcpy_fromiovec(newmsg->data, msg_iov, len);
1345 if (!err)
1346 ipn_proto_injectmsg(ipn_node, newmsg);
1347 ipn_msgpool_put(newmsg,ipnn);
1348 return len;
1351 static int ipn_sendmsg(struct kiocb *kiocb, struct socket *sock,
1352 struct msghdr *msg, size_t len) {
1353 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
1355 if (unlikely(sock->state != SS_CONNECTED))
1356 return -ENOTCONN;
1357 else
1358 return ipn_node_write(ipn_node, msg->msg_iov, len);
1361 /* ipn_proto_sendmsg is called by protocol implementation to enqueue a
1362 * for a destination (to).*/
1363 /* enqueue a msgitem on the receiver's queue, the queue is protected by a
1364 * spinlock (msglock) */
1365 void ipn_proto_sendmsg(struct ipn_node *to, struct msgpool_item *msg)
1367 if (to) {
1368 if (to->netdev) {
1369 ipn_netdev_sendmsg(to,msg);
1370 } else {
1371 /* socket send */
1372 struct msgitem *msgitem;
1373 struct ipn_network *ipnn=to->ipn;
1374 spin_lock(&to->msglock);
1375 if (likely((to->shutdown & RCV_SHUTDOWN)==0)) {
1376 /*if (unlikely((ipnn->flags & IPN_FLAG_LOSSLESS) == 0 ||
1377 to->totmsgcount >= ipnn->msgpool_size))
1378 yield();*/
1379 if (likely(ipnn->flags & IPN_FLAG_LOSSLESS ||
1380 to->totmsgcount < ipnn->msgpool_size)) {
1381 if ((msgitem=kmem_cache_alloc(ipn_msgitem_cache,GFP_KERNEL))!=NULL) {
1382 msgitem->msg=msg;
1383 to->totmsgcount++;
1384 list_add_tail(&msgitem->list, &to->msgqueue);
1385 ipn_msgpool_hold(msg);
1389 spin_unlock(&to->msglock);
1390 wake_up_interruptible(&to->read_wait);
1395 /* enqueue an oob message. "to" is the destination */
1396 void ipn_proto_oobsendmsg(struct ipn_node *to, struct msgpool_item *msg)
1398 if (to) {
1399 if (!to->netdev) { /* no oob to netdev */
1400 struct msgitem *msgitem;
1401 struct ipn_network *ipnn=to->ipn;
1402 spin_lock(&to->msglock);
1403 if ((to->shutdown & RCV_SHUTDOWN_NO_OOB) == 0 &&
1404 (ipnn->flags & IPN_FLAG_LOSSLESS ||
1405 to->oobmsgcount < ipnn->msgpool_size)) {
1406 if ((msgitem=kmem_cache_alloc(ipn_msgitem_cache,GFP_KERNEL))!=NULL) {
1407 msgitem->msg=msg;
1408 to->totmsgcount++;
1409 to->oobmsgcount++;
1410 list_add_tail(&msgitem->list, &to->oobmsgqueue);
1411 ipn_msgpool_hold(msg);
1414 spin_unlock(&to->msglock);
1415 wake_up_interruptible(&to->read_wait);
1420 /* IPN RECV */
1421 int ipn_node_read(struct ipn_node *ipn_node, struct iovec *msg_iov, size_t len, int *msg_flags, int flags) {
1422 struct ipn_network *ipnn=ipn_node->ipn;
1423 struct msgitem *msgitem;
1424 struct msgpool_item *currmsg;
1426 if (unlikely((ipn_node->shutdown & XRCV_SHUTDOWN) == XRCV_SHUTDOWN)) {
1427 if (ipn_node->shutdown == SHUTDOWN_XMASK) /*EOF, nothing can be read*/
1428 return 0;
1429 else
1430 return -EPIPE; /*trying to read on a write only node */
1433 /* wait for a message */
1434 spin_lock(&ipn_node->msglock);
1435 while (ipn_node->totmsgcount == 0) {
1436 spin_unlock(&ipn_node->msglock);
1437 if (wait_event_interruptible(ipn_node->read_wait,
1438 !(ipn_node->totmsgcount == 0)))
1439 return -ERESTARTSYS;
1440 spin_lock(&ipn_node->msglock);
1442 /* oob gets delivered first. oob are rare */
1443 if (likely(list_empty(&ipn_node->oobmsgqueue)))
1444 msgitem=list_first_entry(&ipn_node->msgqueue, struct msgitem, list);
1445 else {
1446 msgitem=list_first_entry(&ipn_node->oobmsgqueue, struct msgitem, list);
1447 *msg_flags |= MSG_OOB;
1448 ipn_node->oobmsgcount--;
1450 list_del(&msgitem->list);
1451 ipn_node->totmsgcount--;
1452 spin_unlock(&ipn_node->msglock);
1453 currmsg=msgitem->msg;
1454 if (currmsg->len < len)
1455 len=currmsg->len;
1456 memcpy_toiovec(msg_iov, currmsg->data, len);
1457 ipn_msgpool_put(currmsg,ipnn);
1458 kmem_cache_free(ipn_msgitem_cache,msgitem);
1459 return len;
1462 static int ipn_recvmsg(struct kiocb *kiocb, struct socket *sock,
1463 struct msghdr *msg, size_t len, int flags) {
1464 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
1466 if (unlikely(sock->state != SS_CONNECTED))
1467 return -ENOTCONN;
1468 else
1469 return ipn_node_read(ipn_node, msg->msg_iov, len, &msg->msg_flags, flags);
1472 /* resize a network: change the # of communication ports (connport) */
1473 static int ipn_netresize(struct ipn_network *ipnn,int newsize)
1475 int oldsize,min;
1476 struct ipn_node **newconnport;
1477 struct ipn_node **oldconnport;
1478 int err;
1479 if (down_interruptible(&ipnn->ipnn_mutex))
1480 return -ERESTARTSYS;
1481 oldsize=ipnn->maxports;
1482 if (newsize == oldsize) {
1483 up(&ipnn->ipnn_mutex);
1484 return 0;
1486 min=oldsize;
1487 /* shrink a network. all the ports we are going to eliminate
1488 * must be unused! */
1489 if (newsize < oldsize) {
1490 int i;
1491 for (i=newsize; i<oldsize; i++)
1492 if (ipnn->connport[i]) {
1493 up(&ipnn->ipnn_mutex);
1494 return -EADDRINUSE;
1496 min=newsize;
1498 oldconnport=ipnn->connport;
1499 /* allocate the new connport array and copy the old one */
1500 newconnport=kzalloc(newsize * sizeof(struct ipn_node *),GFP_KERNEL);
1501 if (!newconnport) {
1502 up(&ipnn->ipnn_mutex);
1503 return -ENOMEM;
1505 memcpy(newconnport,oldconnport,min * sizeof(struct ipn_node *));
1506 ipnn->connport=newconnport;
1507 ipnn->maxports=newsize;
1508 /* notify the protocol that the netowrk has been resized */
1509 err=ipn_protocol_table[ipnn->protocol]->ipn_p_resizenet(ipnn,oldsize,newsize);
1510 if (err) {
1511 /* roll back if the resize operation failed for the protocol */
1512 ipnn->connport=oldconnport;
1513 ipnn->maxports=oldsize;
1514 kfree(newconnport);
1515 } else
1516 /* successful mission, network resized */
1517 kfree(oldconnport);
1518 up(&ipnn->ipnn_mutex);
1519 return err;
1522 /* IPN SETSOCKOPT */
1523 #ifndef IPN_PRE2632
1524 static int ipn_setsockopt(struct socket *sock, int level, int optname,
1525 char __user *optval, unsigned int optlen)
1526 #else
1527 static int ipn_setsockopt(struct socket *sock, int level, int optname,
1528 char __user *optval, int optlen)
1529 #endif
1531 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
1532 struct ipn_network *ipnn=ipn_node->ipn;
1534 if (ipn_node->shutdown == SHUTDOWN_XMASK)
1535 return -ECONNRESET;
1536 if (level != 0 && level != ipn_node->protocol+1)
1537 return -EPROTONOSUPPORT;
1538 if (level > 0) {
1539 /* protocol specific sockopt */
1540 if (ipnn) {
1541 int rv;
1542 if (down_interruptible(&ipnn->ipnn_mutex))
1543 return -ERESTARTSYS;
1544 rv=ipn_protocol_table[ipn_node->protocol]->ipn_p_setsockopt(ipn_node,optname,optval,optlen);
1545 up(&ipnn->ipnn_mutex);
1546 return rv;
1547 } else
1548 return -EOPNOTSUPP;
1549 } else {
1550 if (optname == IPN_SO_DESCR) {
1551 if (optlen > IPN_DESCRLEN)
1552 return -EINVAL;
1553 else {
1554 memset(ipn_node->descr,0,IPN_DESCRLEN);
1555 if (copy_from_user(ipn_node->descr,optval,optlen))
1556 ipn_node->descr[0]=0;
1557 else
1558 ipn_node->descr[optlen-1]=0;
1559 return 0;
1561 } else {
1562 if (optlen < sizeof(int))
1563 return -EINVAL;
1564 else if ((optname & IPN_SO_PREBIND) && (ipnn != NULL))
1565 return -EISCONN;
1566 else {
1567 int val;
1568 get_user(val, (int __user *) optval);
1569 if ((optname & IPN_SO_PREBIND) && !ipn_node->pbp) {
1570 struct pre_bind_parms std=STD_BIND_PARMS;
1571 ipn_node->pbp=kzalloc(sizeof(struct pre_bind_parms),GFP_KERNEL);
1572 if (!ipn_node->pbp)
1573 return -ENOMEM;
1574 *(ipn_node->pbp)=std;
1576 switch (optname) {
1577 case IPN_SO_PORT:
1578 if (sock->state == SS_UNCONNECTED)
1579 ipn_node->portno=val;
1580 else
1581 return -EISCONN;
1582 break;
1583 case IPN_SO_CHANGE_NUMNODES:
1584 if ((ipn_node->flags & IPN_NODEFLAG_BOUND)!=0) {
1585 if (val <= 0)
1586 return -EINVAL;
1587 else
1588 return ipn_netresize(ipnn,val);
1589 } else
1590 val=-ENOTCONN;
1591 break;
1592 case IPN_SO_WANT_OOB_NUMNODES:
1593 if (val)
1594 ipn_node->flags |= IPN_NODEFLAG_OOB_NUMNODES;
1595 else
1596 ipn_node->flags &= ~IPN_NODEFLAG_OOB_NUMNODES;
1597 break;
1598 case IPN_SO_HANDLE_OOB:
1599 if (val)
1600 ipn_node->shutdown &= ~RCV_SHUTDOWN_NO_OOB;
1601 else
1602 ipn_node->shutdown |= RCV_SHUTDOWN_NO_OOB;
1603 break;
1604 case IPN_SO_MTU:
1605 if (val <= 0)
1606 return -EINVAL;
1607 else
1608 ipn_node->pbp->mtu=val;
1609 break;
1610 case IPN_SO_NUMNODES:
1611 if (val <= 0)
1612 return -EINVAL;
1613 else
1614 ipn_node->pbp->maxports=val;
1615 break;
1616 case IPN_SO_MSGPOOLSIZE:
1617 if (val <= 0)
1618 return -EINVAL;
1619 else
1620 ipn_node->pbp->msgpoolsize=val;
1621 break;
1622 case IPN_SO_FLAGS:
1623 ipn_node->pbp->flags=val;
1624 break;
1625 case IPN_SO_MODE:
1626 ipn_node->pbp->mode=val;
1627 break;
1629 return 0;
1635 /* IPN GETSOCKOPT */
1636 static int ipn_getsockopt(struct socket *sock, int level, int optname,
1637 char __user *optval, int __user *optlen) {
1638 struct ipn_node *ipn_node=((struct ipn_sock *)sock->sk)->node;
1639 struct ipn_network *ipnn=ipn_node->ipn;
1640 int len;
1642 if (ipn_node->shutdown == SHUTDOWN_XMASK)
1643 return -ECONNRESET;
1644 if (level != 0 && level != ipn_node->protocol+1)
1645 return -EPROTONOSUPPORT;
1646 if (level > 0) {
1647 if (ipnn) {
1648 int rv;
1649 /* protocol specific sockopt */
1650 if (down_interruptible(&ipnn->ipnn_mutex))
1651 return -ERESTARTSYS;
1652 rv=ipn_protocol_table[ipn_node->protocol]->ipn_p_getsockopt(ipn_node,optname,optval,optlen);
1653 up(&ipnn->ipnn_mutex);
1654 return rv;
1655 } else
1656 return -EOPNOTSUPP;
1657 } else {
1658 if (get_user(len, optlen))
1659 return -EFAULT;
1660 if (optname == IPN_SO_DESCR) {
1661 if (len < IPN_DESCRLEN)
1662 return -EINVAL;
1663 else {
1664 if (len > IPN_DESCRLEN)
1665 len=IPN_DESCRLEN;
1666 if(put_user(len, optlen))
1667 return -EFAULT;
1668 if(copy_to_user(optval,ipn_node->descr,len))
1669 return -EFAULT;
1670 return 0;
1672 } else {
1673 int val=-2;
1674 switch (optname) {
1675 case IPN_SO_PORT:
1676 val=ipn_node->portno;
1677 break;
1678 case IPN_SO_MTU:
1679 if (ipnn)
1680 val=ipnn->mtu;
1681 else if (ipn_node->pbp)
1682 val=ipn_node->pbp->mtu;
1683 break;
1684 case IPN_SO_NUMNODES:
1685 if (ipnn)
1686 val=ipnn->maxports;
1687 else if (ipn_node->pbp)
1688 val=ipn_node->pbp->maxports;
1689 break;
1690 case IPN_SO_MSGPOOLSIZE:
1691 if (ipnn)
1692 val=ipnn->msgpool_size;
1693 else if (ipn_node->pbp)
1694 val=ipn_node->pbp->msgpoolsize;
1695 break;
1696 case IPN_SO_FLAGS:
1697 if (ipnn)
1698 val=ipnn->flags;
1699 else if (ipn_node->pbp)
1700 val=ipn_node->pbp->flags;
1701 break;
1702 case IPN_SO_MODE:
1703 if (ipnn)
1704 val=-1;
1705 else if (ipn_node->pbp)
1706 val=ipn_node->pbp->mode;
1707 break;
1709 if (val < -1)
1710 return -EINVAL;
1711 else {
1712 if (len < sizeof(int))
1713 return -EOVERFLOW;
1714 else {
1715 len = sizeof(int);
1716 if(put_user(len, optlen))
1717 return -EFAULT;
1718 if(copy_to_user(optval,&val,len))
1719 return -EFAULT;
1720 return 0;
1727 /* BROADCAST/HUB implementation */
1729 static int ipn_bcast_newport(struct ipn_node *newport) {
1730 struct ipn_network *ipnn=newport->ipn;
1731 int i;
1732 for (i=0;i<ipnn->maxports;i++) {
1733 if (ipnn->connport[i] == NULL)
1734 return i;
1736 return -1;
1739 static int ipn_bcast_handlemsg(struct ipn_node *from,
1740 struct msgpool_item *msgitem){
1741 struct ipn_network *ipnn=from->ipn;
1743 struct ipn_node *ipn_node;
1744 list_for_each_entry(ipn_node, &ipnn->connectqueue, nodelist) {
1745 if (ipn_node != from)
1746 ipn_proto_sendmsg(ipn_node,msgitem);
1748 return 0;
1751 static void ipn_null_delport(struct ipn_node *oldport) {}
1752 static void ipn_null_postnewport(struct ipn_node *newport) {}
1753 static void ipn_null_predelport(struct ipn_node *oldport) {}
1754 static int ipn_null_newnet(struct ipn_network *newnet) {return 0;}
1755 static int ipn_null_resizenet(struct ipn_network *net,int oldsize,int newsize) {
1756 return 0;}
1757 static void ipn_null_delnet(struct ipn_network *oldnet) {}
1758 static int ipn_null_setsockopt(struct ipn_node *port,int optname,
1759 char __user *optval, int optlen) {return -EOPNOTSUPP;}
1760 static int ipn_null_getsockopt(struct ipn_node *port,int optname,
1761 char __user *optval, int *optlen) {return -EOPNOTSUPP;}
1762 static int ipn_null_ioctl(struct ipn_node *port,unsigned int request,
1763 unsigned long arg) {return -EOPNOTSUPP;}
1765 /* Protocol Registration/deregisteration */
1767 void ipn_init_protocol(struct ipn_protocol *p)
1769 if (p->ipn_p_delport == NULL) p->ipn_p_delport=ipn_null_delport;
1770 if (p->ipn_p_postnewport == NULL) p->ipn_p_postnewport=ipn_null_postnewport;
1771 if (p->ipn_p_predelport == NULL) p->ipn_p_predelport=ipn_null_predelport;
1772 if (p->ipn_p_newnet == NULL) p->ipn_p_newnet=ipn_null_newnet;
1773 if (p->ipn_p_resizenet == NULL) p->ipn_p_resizenet=ipn_null_resizenet;
1774 if (p->ipn_p_delnet == NULL) p->ipn_p_delnet=ipn_null_delnet;
1775 if (p->ipn_p_setsockopt == NULL) p->ipn_p_setsockopt=ipn_null_setsockopt;
1776 if (p->ipn_p_getsockopt == NULL) p->ipn_p_getsockopt=ipn_null_getsockopt;
1777 if (p->ipn_p_ioctl == NULL) p->ipn_p_ioctl=ipn_null_ioctl;
1780 int ipn_proto_register(int protocol,struct ipn_protocol *ipn_service)
1782 int rv=0;
1783 if (ipn_service->ipn_p_newport == NULL ||
1784 ipn_service->ipn_p_handlemsg == NULL)
1785 return -EINVAL;
1786 ipn_init_protocol(ipn_service);
1787 if (protocol > 1 && protocol <= IPN_MAX_PROTO) {
1788 protocol--;
1789 if (down_interruptible(&ipn_glob_mutex))
1790 return -ERESTARTSYS;
1791 if (ipn_protocol_table[protocol])
1792 rv= -EEXIST;
1793 else {
1794 ipn_service->refcnt=0;
1795 ipn_protocol_table[protocol]=ipn_service;
1796 printk(KERN_INFO "IPN: Registered protocol %d\n",protocol+1);
1798 up(&ipn_glob_mutex);
1799 } else
1800 rv= -EINVAL;
1801 return rv;
1804 int ipn_proto_deregister(int protocol)
1806 int rv=0;
1807 if (protocol > 1 && protocol <= IPN_MAX_PROTO) {
1808 protocol--;
1809 if (down_interruptible(&ipn_glob_mutex))
1810 return -ERESTARTSYS;
1811 if (ipn_protocol_table[protocol]) {
1812 if (ipn_protocol_table[protocol]->refcnt == 0) {
1813 ipn_protocol_table[protocol]=NULL;
1814 printk(KERN_INFO "IPN: Unregistered protocol %d\n",protocol+1);
1815 } else
1816 rv=-EADDRINUSE;
1817 } else
1818 rv= -ENOENT;
1819 up(&ipn_glob_mutex);
1820 } else
1821 rv= -EINVAL;
1822 return rv;
1825 /* MAIN SECTION */
1826 /* Module constructor/destructor */
1827 static struct net_proto_family ipn_family_ops = {
1828 .family = PF_IPN,
1829 .create = ipn_create,
1830 .owner = THIS_MODULE,
1833 /* IPN constructor */
1834 static int ipn_init(void)
1836 int rc;
1838 ipn_init_protocol(&ipn_bcast);
1839 ipn_network_cache=kmem_cache_create("ipn_network",sizeof(struct ipn_network),0,0,NULL);
1840 if (!ipn_network_cache) {
1841 printk(KERN_CRIT "%s: Cannot create ipn_network SLAB cache!\n",
1842 __FUNCTION__);
1843 rc=-ENOMEM;
1844 goto out;
1847 ipn_node_cache=kmem_cache_create("ipn_node",sizeof(struct ipn_node),0,0,NULL);
1848 if (!ipn_node_cache) {
1849 printk(KERN_CRIT "%s: Cannot create ipn_node SLAB cache!\n",
1850 __FUNCTION__);
1851 rc=-ENOMEM;
1852 goto out_net;
1855 ipn_msgitem_cache=kmem_cache_create("ipn_msgitem",sizeof(struct msgitem),0,0,NULL);
1856 if (!ipn_msgitem_cache) {
1857 printk(KERN_CRIT "%s: Cannot create ipn_msgitem SLAB cache!\n",
1858 __FUNCTION__);
1859 rc=-ENOMEM;
1860 goto out_net_node;
1863 rc=ipn_msgbuf_init();
1864 if (rc != 0) {
1865 printk(KERN_CRIT "%s: Cannot create ipn_msgbuf SLAB cache\n",
1866 __FUNCTION__);
1867 goto out_net_node_msg;
1870 rc=proto_register(&ipn_proto,1);
1871 if (rc != 0) {
1872 printk(KERN_CRIT "%s: Cannot register the protocol!\n",
1873 __FUNCTION__);
1874 goto out_net_node_msg_msgbuf;
1877 sock_register(&ipn_family_ops);
1878 ipn_netdev_init();
1879 printk(KERN_INFO "IPN: Virtual Square Project, University of Bologna 2007-09\n");
1880 return 0;
1882 out_net_node_msg_msgbuf:
1883 ipn_msgbuf_fini();
1884 out_net_node_msg:
1885 kmem_cache_destroy(ipn_msgitem_cache);
1886 out_net_node:
1887 kmem_cache_destroy(ipn_node_cache);
1888 out_net:
1889 kmem_cache_destroy(ipn_network_cache);
1890 out:
1891 return rc;
1894 /* IPN destructor */
1895 static void ipn_exit(void)
1897 ipn_netdev_fini();
1898 if (ipn_msgitem_cache)
1899 kmem_cache_destroy(ipn_msgitem_cache);
1900 if (ipn_node_cache)
1901 kmem_cache_destroy(ipn_node_cache);
1902 if (ipn_network_cache)
1903 kmem_cache_destroy(ipn_network_cache);
1904 ipn_msgbuf_fini();
1905 sock_unregister(PF_IPN);
1906 proto_unregister(&ipn_proto);
1907 printk(KERN_INFO "IPN removed\n");
1910 module_init(ipn_init);
1911 module_exit(ipn_exit);
1913 EXPORT_SYMBOL_GPL(ipn_proto_register);
1914 EXPORT_SYMBOL_GPL(ipn_proto_deregister);
1915 EXPORT_SYMBOL_GPL(ipn_proto_sendmsg);
1916 EXPORT_SYMBOL_GPL(ipn_proto_oobsendmsg);
1917 EXPORT_SYMBOL_GPL(ipn_msgpool_alloc);
1918 EXPORT_SYMBOL_GPL(ipn_msgpool_put);