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>
27 #include <linux/list.h>
28 #include <linux/mount.h>
29 #include <linux/version.h>
32 #include <net/af_ipn.h>
35 #include "ipn_netdev.h"
36 #include "ipn_msgbuf.h"
38 MODULE_LICENSE("GPL");
39 MODULE_AUTHOR("VIEW-OS TEAM");
40 MODULE_DESCRIPTION("IPN Kernel Module");
42 #define IPN_MAX_PROTO 4
43 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25)
47 /*extension of RCV_SHUTDOWN defined in include/net/sock.h
48 * when the bit is set recv fails */
49 /* NO_OOB: do not send OOB */
50 #define RCV_SHUTDOWN_NO_OOB 4
51 /* EXTENDED MASK including OOB */
52 #define SHUTDOWN_XMASK (SHUTDOWN_MASK | RCV_SHUTDOWN_NO_OOB)
53 /* if XRCV_SHUTDOWN is all set recv fails */
54 #define XRCV_SHUTDOWN (RCV_SHUTDOWN | RCV_SHUTDOWN_NO_OOB)
56 /* Network table and hash */
57 struct hlist_head ipn_network_table
[IPN_HASH_SIZE
+ 1];
58 /* not needed. Now protected by ipn_glob_mutex
60 * DEFINE_SPINLOCK(ipn_table_lock);
62 static struct kmem_cache
*ipn_network_cache
;
63 static struct kmem_cache
*ipn_node_cache
;
64 static struct kmem_cache
*ipn_msgitem_cache
;
65 static DECLARE_MUTEX(ipn_glob_mutex
);
67 /* Protocol 1: HUB/Broadcast default protocol. Function Prototypes */
68 static int ipn_bcast_newport(struct ipn_node
*newport
);
69 static int ipn_bcast_handlemsg(struct ipn_node
*from
,
70 struct msgpool_item
*msgitem
);
72 /* default protocol IPN_BROADCAST (0) */
73 static struct ipn_protocol ipn_bcast
= {
75 .ipn_p_newport
=ipn_bcast_newport
,
76 .ipn_p_handlemsg
=ipn_bcast_handlemsg
};
78 static struct ipn_protocol
*ipn_protocol_table
[IPN_MAX_PROTO
]={&ipn_bcast
};
80 /* Socket call function prototypes */
81 static int ipn_release(struct socket
*);
82 static int ipn_bind(struct socket
*, struct sockaddr
*, int);
83 static int ipn_connect(struct socket
*, struct sockaddr
*,
84 int addr_len
, int flags
);
85 static int ipn_getname(struct socket
*, struct sockaddr
*, int *, int);
86 static unsigned int ipn_poll(struct file
*, struct socket
*, poll_table
*);
87 static int ipn_ioctl(struct socket
*, unsigned int, unsigned long);
88 static int ipn_shutdown(struct socket
*, int);
89 static int ipn_sendmsg(struct kiocb
*, struct socket
*,
90 struct msghdr
*, size_t);
91 static int ipn_recvmsg(struct kiocb
*, struct socket
*,
92 struct msghdr
*, size_t, int);
93 static int ipn_setsockopt(struct socket
*sock
, int level
, int optname
,
94 char __user
*optval
, int optlen
);
95 static int ipn_getsockopt(struct socket
*sock
, int level
, int optname
,
96 char __user
*optval
, int __user
*optlen
);
98 /* Network table Management
99 * inode->ipn_network hash table
100 * LOCKING: MUTEX ipn_glob_mutex must be LOCKED*/
101 static inline void ipn_insert_network(struct hlist_head
*list
, struct ipn_network
*ipnn
)
103 /* *IPNTL* spin_lock(&ipn_table_lock); */
104 hlist_add_head(&ipnn
->hnode
, list
);
105 /* *IPNTL* spin_unlock(&ipn_table_lock); */
108 static inline void ipn_remove_network(struct ipn_network
*ipnn
)
110 /* *IPNTL* spin_lock(&ipn_table_lock); */
111 hlist_del(&ipnn
->hnode
);
112 /* *IPNTL* spin_unlock(&ipn_table_lock); */
115 static struct ipn_network
*ipn_find_network_byinode(struct inode
*i
)
117 struct ipn_network
*ipnn
;
118 struct hlist_node
*node
;
120 /* *IPNTL* spin_lock(&ipn_table_lock);*/
121 hlist_for_each_entry(ipnn
, node
,
122 &ipn_network_table
[i
->i_ino
& (IPN_HASH_SIZE
- 1)], hnode
) {
123 struct dentry
*dentry
= ipnn
->dentry
;
125 if(ipnn
->refcnt
> 0 && dentry
&& dentry
->d_inode
== i
)
130 /* *IPNTL* spin_unlock(&ipn_table_lock); */
134 /* msgpool management
135 * msgpool_item are ipn_network dependent (each net has its own MTU)
136 * for each message sent there is one msgpool_item and many struct msgitem
137 * one for each receipient.
138 * msgitem are connected to the node's msgqueue or oobmsgqueue.
139 * when a message is delivered to a process the msgitem is deleted and
140 * the count of the msgpool_item is decreased.
141 * msgpool_item elements gets deleted automatically when count is 0*/
144 struct list_head list
;
145 struct msgpool_item
*msg
;
148 /* alloc a fresh msgpool item. count is set to 1.
151 * for each receipient
152 * enqueue messages to the process (using msgitem), ipn_msgpool_hold
154 * The message can be delivered concurrently. init count to 1 guarantees
155 * that it survives at least until is has been enqueued to all
157 static struct msgpool_item
*_ipn_msgpool_alloc(struct ipn_network
*ipnn
)
159 struct msgpool_item
*new;
160 if ((new=kmem_cache_alloc(ipnn
->msgpool_cache
,GFP_KERNEL
)) != NULL
) {
161 atomic_set(&new->count
,1);
162 atomic_inc(&ipnn
->msgpool_nelem
);
167 struct msgpool_item
*ipn_msgpool_alloc(struct ipn_network
*ipnn
,int leaky
)
169 if (leaky
&& (ipnn
->flags
& IPN_FLAG_LOSSLESS
) &&
170 atomic_read(&ipnn
->msgpool_nelem
) < ipnn
->msgpool_size
)
173 return _ipn_msgpool_alloc(ipnn
);
176 /* If the service il LOSSLESS, this msgpool call waits for an
177 * available msgpool item */
178 static struct msgpool_item
*ipn_msgpool_alloc_locking(struct ipn_network
*ipnn
)
180 if (ipnn
->flags
& IPN_FLAG_LOSSLESS
) {
181 while (atomic_read(&ipnn
->msgpool_nelem
) >= ipnn
->msgpool_size
) {
182 if (wait_event_interruptible_exclusive(ipnn
->send_wait
,
183 atomic_read(&ipnn
->msgpool_nelem
) < ipnn
->msgpool_size
))
187 return _ipn_msgpool_alloc(ipnn
);
190 static inline void ipn_msgpool_hold(struct msgpool_item
*msg
)
192 atomic_inc(&msg
->count
);
195 /* decrease count and delete msgpool_item if count == 0 */
196 void ipn_msgpool_put(struct msgpool_item
*old
,
197 struct ipn_network
*ipnn
)
199 if (atomic_dec_and_test(&old
->count
)) {
200 kmem_cache_free(ipnn
->msgpool_cache
,old
);
201 atomic_dec(&ipnn
->msgpool_nelem
);
202 if (ipnn
->flags
& IPN_FLAG_LOSSLESS
) /* this could be done anyway */
203 wake_up_interruptible(&ipnn
->send_wait
);
208 static const struct proto_ops ipn_ops
= {
210 .owner
= THIS_MODULE
,
211 .release
= ipn_release
,
213 .connect
= ipn_connect
,
214 .socketpair
= sock_no_socketpair
,
215 .accept
= sock_no_accept
,
216 .getname
= ipn_getname
,
219 .listen
= sock_no_listen
,
220 .shutdown
= ipn_shutdown
,
221 .setsockopt
= ipn_setsockopt
,
222 .getsockopt
= ipn_getsockopt
,
223 .sendmsg
= ipn_sendmsg
,
224 .recvmsg
= ipn_recvmsg
,
225 .mmap
= sock_no_mmap
,
226 .sendpage
= sock_no_sendpage
,
229 static struct proto ipn_proto
= {
231 .owner
= THIS_MODULE
,
232 .obj_size
= sizeof(struct ipn_sock
),
236 * ipn_node is a separate structure, pointed by ipn_sock -> node
237 * when a node is "persistent", ipn_node survives while ipn_sock gets released*/
238 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
239 static int ipn_create(struct socket
*sock
, int protocol
)
241 static int ipn_create(struct net
*net
,struct socket
*sock
, int protocol
)
244 struct ipn_sock
*ipn_sk
;
245 struct ipn_node
*ipn_node
;
247 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
250 if (net
!= &init_net
)
251 return -EAFNOSUPPORT
;
254 if (sock
->type
!= SOCK_RAW
)
259 protocol
=IPN_BROADCAST
-1;
260 if (protocol
< 0 || protocol
>= IPN_MAX_PROTO
||
261 ipn_protocol_table
[protocol
] == NULL
)
262 return -EPROTONOSUPPORT
;
263 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
264 ipn_sk
= (struct ipn_sock
*) sk_alloc(PF_IPN
, GFP_KERNEL
, &ipn_proto
, 1);
266 ipn_sk
= (struct ipn_sock
*) sk_alloc(net
, PF_IPN
, GFP_KERNEL
, &ipn_proto
);
271 ipn_sk
->node
=ipn_node
=kmem_cache_alloc(ipn_node_cache
,GFP_KERNEL
);
273 sock_put((struct sock
*) ipn_sk
);
276 sock_init_data(sock
,(struct sock
*) ipn_sk
);
277 sock
->state
= SS_UNCONNECTED
;
278 sock
->ops
= &ipn_ops
;
279 sock
->sk
=(struct sock
*)ipn_sk
;
280 INIT_LIST_HEAD(&ipn_node
->nodelist
);
281 ipn_node
->protocol
=protocol
;
282 ipn_node
->flags
=IPN_NODEFLAG_INUSE
;
283 ipn_node
->shutdown
=RCV_SHUTDOWN_NO_OOB
;
284 ipn_node
->descr
[0]=0;
285 ipn_node
->portno
=IPN_PORTNO_ANY
;
288 ipn_node
->proto_private
=NULL
;
289 ipn_node
->totmsgcount
=0;
290 ipn_node
->oobmsgcount
=0;
291 spin_lock_init(&ipn_node
->msglock
);
292 INIT_LIST_HEAD(&ipn_node
->msgqueue
);
293 INIT_LIST_HEAD(&ipn_node
->oobmsgqueue
);
295 init_waitqueue_head(&ipn_node
->read_wait
);
300 /* update # of readers and # of writers counters for an ipn network.
301 * This function sends oob messages to nodes requesting the service */
302 /* LOCKING ipnn_mutex is locked */
303 static void ipn_net_update_counters(struct ipn_network
*ipnn
,
304 int chg_readers
, int chg_writers
) {
305 ipnn
->numreaders
+= chg_readers
;
306 ipnn
->numwriters
+= chg_writers
;
307 if (ipnn
->mtu
>= sizeof(struct numnode_oob
))
309 struct msgpool_item
*ipn_msg
=_ipn_msgpool_alloc(ipnn
);
311 struct numnode_oob
*oob_msg
=(struct numnode_oob
*)(ipn_msg
->data
);
312 struct ipn_node
*ipn_node
;
313 ipn_msg
->len
=sizeof(struct numnode_oob
);
314 oob_msg
->level
=IPN_ANY
;
315 oob_msg
->tag
=IPN_OOB_NUMNODE_TAG
;
316 oob_msg
->numreaders
=ipnn
->numreaders
;
317 oob_msg
->numwriters
=ipnn
->numwriters
;
318 list_for_each_entry(ipn_node
, &ipnn
->connectqueue
, nodelist
) {
319 if (ipn_node
->flags
& IPN_NODEFLAG_OOB_NUMNODES
)
320 ipn_proto_oobsendmsg(ipn_node
,ipn_msg
);
322 ipn_msgpool_put(ipn_msg
,ipnn
);
327 /* flush pending messages (for close and shutdown RCV) */
328 /* LOCKING: ipnn_mutex is locked */
329 static void ipn_flush_recvqueue(struct ipn_node
*ipn_node
)
331 struct ipn_network
*ipnn
=ipn_node
->ipn
;
332 spin_lock(&ipn_node
->msglock
);
333 while (!list_empty(&ipn_node
->msgqueue
)) {
334 struct msgitem
*msgitem
=
335 list_first_entry(&ipn_node
->msgqueue
, struct msgitem
, list
);
336 list_del(&msgitem
->list
);
337 ipn_node
->totmsgcount
--;
338 ipn_msgpool_put(msgitem
->msg
,ipnn
);
339 kmem_cache_free(ipn_msgitem_cache
,msgitem
);
341 spin_unlock(&ipn_node
->msglock
);
344 /* flush pending oob messages (for socket close) */
345 /* LOCKING: ipnn_mutex is locked */
346 static void ipn_flush_oobrecvqueue(struct ipn_node
*ipn_node
)
348 struct ipn_network
*ipnn
=ipn_node
->ipn
;
349 spin_lock(&ipn_node
->msglock
);
350 while (!list_empty(&ipn_node
->oobmsgqueue
)) {
351 struct msgitem
*msgitem
=
352 list_first_entry(&ipn_node
->oobmsgqueue
, struct msgitem
, list
);
353 list_del(&msgitem
->list
);
354 ipn_node
->totmsgcount
--;
355 ipn_node
->oobmsgcount
--;
356 ipn_msgpool_put(msgitem
->msg
,ipnn
);
357 kmem_cache_free(ipn_msgitem_cache
,msgitem
);
359 spin_unlock(&ipn_node
->msglock
);
362 /* Terminate node. The node is "logically" terminated. */
363 /* LOCKING: ipn_glob_lock must be locked here */
364 static int ipn_terminate_node(struct ipn_node
*ipn_node
)
366 struct ipn_network
*ipnn
=ipn_node
->ipn
;
368 if (down_interruptible(&ipnn
->ipnn_mutex
))
370 if (ipn_node
->portno
>= 0) {
371 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_predelport(ipn_node
);
372 ipnn
->connport
[ipn_node
->portno
]=NULL
;
374 list_del(&ipn_node
->nodelist
);
375 ipn_flush_recvqueue(ipn_node
);
376 ipn_flush_oobrecvqueue(ipn_node
);
377 if (ipn_node
->portno
>= 0)
378 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_delport(ipn_node
);
380 ipn_net_update_counters(ipnn
,
381 (ipn_node
->shutdown
& RCV_SHUTDOWN
)?0:-1,
382 (ipn_node
->shutdown
& SEND_SHUTDOWN
)?0:-1);
383 ipn_node
->shutdown
= SHUTDOWN_XMASK
;
384 up(&ipnn
->ipnn_mutex
);
386 ipn_netdev_close(ipn_node
);
387 /* No more network elements */
389 if (ipnn
->refcnt
== 0)
391 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_delnet(ipnn
);
392 ipn_remove_network(ipnn
);
393 ipn_protocol_table
[ipnn
->protocol
]->refcnt
--;
398 if (ipnn
->msgpool_cache
)
399 ipn_msgbuf_put(ipnn
->msgpool_cache
);
401 kfree(ipnn
->connport
);
402 kmem_cache_free(ipn_network_cache
, ipnn
);
403 module_put(THIS_MODULE
);
407 kfree(ipn_node
->pbp
);
413 /* release of a socket */
414 static int ipn_release (struct socket
*sock
)
416 struct ipn_sock
*ipn_sk
=(struct ipn_sock
*)sock
->sk
;
417 struct ipn_node
*ipn_node
=ipn_sk
->node
;
419 if (down_interruptible(&ipn_glob_mutex
))
421 if (ipn_node
->flags
& IPN_NODEFLAG_PERSIST
) {
422 ipn_node
->flags
&= ~IPN_NODEFLAG_INUSE
;
426 rv
=ipn_terminate_node(ipn_node
);
430 kmem_cache_free(ipn_node_cache
,ipn_node
);
434 sock_put((struct sock
*) ipn_sk
);
438 /* _set persist, change the persistence of a node,
439 * when persistence gets cleared and the node is no longer used
440 * the node is terminated and freed.
441 * ipn_glob_mutex must be locked */
442 static int _ipn_setpersist(struct ipn_node
*ipn_node
, int persist
)
446 ipn_node
->flags
|= IPN_NODEFLAG_PERSIST
;
448 ipn_node
->flags
&= ~IPN_NODEFLAG_PERSIST
;
449 if (!(ipn_node
->flags
& IPN_NODEFLAG_INUSE
)) {
450 rv
=ipn_terminate_node(ipn_node
);
452 kmem_cache_free(ipn_node_cache
,ipn_node
);
459 * lock ipn_glob_mutex and call __ipn_setpersist above */
460 static int ipn_setpersist(struct ipn_node
*ipn_node
, int persist
)
463 if (ipn_node
->dev
== NULL
)
465 if (down_interruptible(&ipn_glob_mutex
))
467 rv
=_ipn_setpersist(ipn_node
,persist
);
472 /* several network parameters can be set by setsockopt prior to bind */
473 /* struct pre_bind_parms is a temporary stucture connected to ipn_node->pbp
474 * to keep the parameter values. */
475 struct pre_bind_parms
{
476 unsigned short maxports
;
477 unsigned short flags
;
478 unsigned short msgpoolsize
;
483 /* STD_PARMS: BITS_PER_LONG nodes, no flags, BITS_PER_BYTE pending msgs,
484 * Ethernet + VLAN MTU*/
485 #define STD_BIND_PARMS {BITS_PER_LONG, 0, BITS_PER_BYTE, 1514, 0777};
487 static int ipn_mkname(struct sockaddr_un
* sunaddr
, int len
)
489 if (len
<= sizeof(short) || len
> sizeof(*sunaddr
))
491 if (!sunaddr
|| sunaddr
->sun_family
!= AF_IPN
)
494 * This may look like an off by one error but it is a bit more
495 * subtle. 108 is the longest valid AF_IPN path for a binding.
496 * sun_path[108] doesnt as such exist. However in kernel space
497 * we are guaranteed that it is a valid memory location in our
498 * kernel address buffer.
500 ((char *)sunaddr
)[len
]=0;
501 len
= strlen(sunaddr
->sun_path
)+1+sizeof(short);
506 static int ipn_bind(struct socket
*sock
, struct sockaddr
*uaddr
, int addr_len
)
508 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)uaddr
;
509 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
511 struct ipn_network
*ipnn
;
512 struct dentry
* dentry
= NULL
;
514 struct pre_bind_parms parms
=STD_BIND_PARMS
;
516 //printk("IPN bind\n");
518 if (down_interruptible(&ipn_glob_mutex
))
520 if (sock
->state
!= SS_UNCONNECTED
||
521 ipn_node
->ipn
!= NULL
) {
526 if (ipn_node
->protocol
>= 0 &&
527 (ipn_node
->protocol
>= IPN_MAX_PROTO
||
528 ipn_protocol_table
[ipn_node
->protocol
] == NULL
)) {
529 err
= -EPROTONOSUPPORT
;
533 addr_len
= ipn_mkname(sunaddr
, addr_len
);
539 /* check if there is already an ipn-network socket with that name */
540 err
= path_lookup(sunaddr
->sun_path
, LOOKUP_FOLLOW
, &nd
);
541 if (err
) { /* it does not exist, NEW IPN socket! */
543 /* Is it everything okay with the parent? */
544 err
= path_lookup(sunaddr
->sun_path
, LOOKUP_PARENT
, &nd
);
546 goto out_mknod_parent
;
547 /* Do I have the permission to create a file? */
548 dentry
= lookup_create(&nd
, 0);
549 err
= PTR_ERR(dentry
);
551 goto out_mknod_unlock
;
553 * All right, let's create it.
556 mode
= ipn_node
->pbp
->mode
;
558 mode
= SOCK_INODE(sock
)->i_mode
;
559 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 30)
560 mode
= S_IFSOCK
| (mode
& ~current
->fs
->umask
);
562 mode
= S_IFSOCK
| (mode
& ~current_umask());
566 err
= vfs_mknod(nd
.path
.dentry
->d_inode
, dentry
, nd
.path
.mnt
, mode
, 0);
568 err
= vfs_mknod(nd
.path
.dentry
->d_inode
, dentry
, mode
, 0);
572 err
= vfs_mknod(nd
.dentry
->d_inode
, dentry
, nd
.path
.mnt
, mode
, 0);
574 err
= vfs_mknod(nd
.dentry
->d_inode
, dentry
, mode
, 0);
580 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
581 dput(nd
.path
.dentry
);
582 nd
.path
.dentry
= dentry
;
584 mutex_unlock(&nd
.dentry
->d_inode
->i_mutex
);
588 /* create a new ipn_network item */
590 parms
=*ipn_node
->pbp
;
591 ipnn
=kmem_cache_zalloc(ipn_network_cache
,GFP_KERNEL
);
594 goto out_mknod_dput_ipnn
;
596 ipnn
->connport
=kzalloc(parms
.maxports
* sizeof(struct ipn_node
*),GFP_KERNEL
);
597 if (!ipnn
->connport
) {
599 goto out_mknod_dput_ipnn2
;
602 /* module refcnt is incremented for each network, thus
603 * rmmod is forbidden if there are persistent node */
604 if (!try_module_get(THIS_MODULE
)) {
606 goto out_mknod_dput_ipnn2
;
608 memcpy(&ipnn
->sunaddr
,sunaddr
,addr_len
);
610 ipnn
->msgpool_cache
=ipn_msgbuf_get(ipnn
->mtu
);
611 if (!ipnn
->msgpool_cache
) {
613 goto out_mknod_dput_putmodule
;
615 INIT_LIST_HEAD(&ipnn
->unconnectqueue
);
616 INIT_LIST_HEAD(&ipnn
->connectqueue
);
619 ipnn
->dentry
=nd
.path
.dentry
;
620 ipnn
->mnt
=nd
.path
.mnt
;
622 ipnn
->dentry
=nd
.dentry
;
625 init_MUTEX(&ipnn
->ipnn_mutex
);
626 ipnn
->sunaddr_len
=addr_len
;
627 ipnn
->protocol
=ipn_node
->protocol
;
628 if (ipnn
->protocol
< 0) ipnn
->protocol
= 0;
629 ipn_protocol_table
[ipnn
->protocol
]->refcnt
++;
630 ipnn
->flags
=parms
.flags
;
633 ipnn
->maxports
=parms
.maxports
;
634 atomic_set(&ipnn
->msgpool_nelem
,0);
635 ipnn
->msgpool_size
=parms
.msgpoolsize
;
636 ipnn
->proto_private
=NULL
;
637 init_waitqueue_head(&ipnn
->send_wait
);
638 err
=ipn_protocol_table
[ipnn
->protocol
]->ipn_p_newnet(ipnn
);
640 goto out_mknod_dput_putmodule
;
642 ipn_insert_network(&ipn_network_table
[nd
.path
.dentry
->d_inode
->i_ino
& (IPN_HASH_SIZE
-1)],ipnn
);
644 ipn_insert_network(&ipn_network_table
[nd
.dentry
->d_inode
->i_ino
& (IPN_HASH_SIZE
-1)],ipnn
);
647 /* join an existing network */
648 if (parms
.flags
& IPN_FLAG_EXCL
) {
652 err
= inode_permission(nd
.path
.dentry
->d_inode
, MAY_EXEC
);
657 if (!S_ISSOCK(nd
.path
.dentry
->d_inode
->i_mode
))
659 ipnn
=ipn_find_network_byinode(nd
.path
.dentry
->d_inode
);
661 if (!S_ISSOCK(nd
.dentry
->d_inode
->i_mode
))
663 ipnn
=ipn_find_network_byinode(nd
.dentry
->d_inode
);
665 if (!ipnn
|| (ipnn
->flags
& IPN_FLAG_TERMINATED
) ||
666 (ipnn
->flags
& IPN_FLAG_EXCL
))
668 list_add_tail(&ipn_node
->nodelist
,&ipnn
->unconnectqueue
);
672 kfree(ipn_node
->pbp
);
676 ipn_node
->flags
|= IPN_NODEFLAG_BOUND
;
690 out_mknod_dput_putmodule
:
691 module_put(THIS_MODULE
);
692 out_mknod_dput_ipnn2
:
693 kfree(ipnn
->connport
);
695 kmem_cache_free(ipn_network_cache
,ipnn
);
700 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
703 mutex_unlock(&nd
.dentry
->d_inode
->i_mutex
);
714 static int ipn_connect(struct socket
*sock
, struct sockaddr
*addr
,
715 int addr_len
, int flags
){
716 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)addr
;
717 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
719 struct ipn_network
*ipnn
,*previousipnn
;
723 /* the socket cannot be connected twice */
724 if (sock
->state
!= SS_UNCONNECTED
)
727 if (down_interruptible(&ipn_glob_mutex
))
730 if ((previousipnn
=ipn_node
->ipn
) == NULL
) { /* unbound */
731 unsigned char mustshutdown
=0;
732 err
= ipn_mkname(sunaddr
, addr_len
);
736 err
= path_lookup(sunaddr
->sun_path
, LOOKUP_FOLLOW
, &nd
);
739 err
= inode_permission(nd
.path
.dentry
->d_inode
, MAY_READ
);
741 if (err
== -EACCES
|| err
== -EROFS
)
742 mustshutdown
|=RCV_SHUTDOWN
;
746 err
= inode_permission(nd
.path
.dentry
->d_inode
, MAY_WRITE
);
749 mustshutdown
|=SEND_SHUTDOWN
;
753 mustshutdown
|= ipn_node
->shutdown
;
754 /* if the combination of shutdown and permissions leaves
755 * no abilities, connect returns EACCES */
756 if (mustshutdown
== SHUTDOWN_XMASK
) {
761 ipn_node
->shutdown
=mustshutdown
;
764 if (!S_ISSOCK(nd
.path
.dentry
->d_inode
->i_mode
)) {
768 ipnn
=ipn_find_network_byinode(nd
.path
.dentry
->d_inode
);
770 if (!S_ISSOCK(nd
.dentry
->d_inode
->i_mode
)) {
774 ipnn
=ipn_find_network_byinode(nd
.dentry
->d_inode
);
776 if (!ipnn
|| (ipnn
->flags
& IPN_FLAG_TERMINATED
)) {
780 if (ipn_node
->protocol
== IPN_ANY
)
781 ipn_node
->protocol
=ipnn
->protocol
;
782 else if (ipnn
->protocol
!= ipn_node
->protocol
) {
795 if (down_interruptible(&ipnn
->ipnn_mutex
)) {
799 portno
= ipn_protocol_table
[ipnn
->protocol
]->ipn_p_newport(ipn_node
);
800 if (portno
>= 0 && portno
<ipnn
->maxports
) {
801 sock
->state
= SS_CONNECTED
;
802 ipn_node
->portno
=portno
;
803 ipnn
->connport
[portno
]=ipn_node
;
804 if (!(ipn_node
->flags
& IPN_NODEFLAG_BOUND
)) {
806 list_del(&ipn_node
->nodelist
);
808 list_add_tail(&ipn_node
->nodelist
,&ipnn
->connectqueue
);
809 ipn_net_update_counters(ipnn
,
810 (ipn_node
->shutdown
& RCV_SHUTDOWN
)?0:1,
811 (ipn_node
->shutdown
& SEND_SHUTDOWN
)?0:1);
813 ipn_node
->ipn
=previousipnn
; /* undo changes on ipn_node->ipn */
816 up(&ipnn
->ipnn_mutex
);
831 static int ipn_getname(struct socket
*sock
, struct sockaddr
*uaddr
,
832 int *uaddr_len
, int peer
) {
833 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
834 struct ipn_network
*ipnn
=ipn_node
->ipn
;
835 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)uaddr
;
838 if (down_interruptible(&ipn_glob_mutex
))
841 *uaddr_len
= ipnn
->sunaddr_len
;
842 memcpy(sunaddr
,&ipnn
->sunaddr
,*uaddr_len
);
850 static unsigned int ipn_poll(struct file
*file
, struct socket
*sock
,
852 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
853 struct ipn_network
*ipnn
=ipn_node
->ipn
;
857 poll_wait(file
,&ipn_node
->read_wait
,wait
);
858 if (ipnn
->flags
& IPN_FLAG_LOSSLESS
)
859 poll_wait(file
,&ipnn
->send_wait
,wait
);
860 /* POLLIN if recv succeeds,
861 * POLL{PRI,RDNORM} if there are {oob,non-oob} messages */
862 if (ipn_node
->totmsgcount
> 0) mask
|= POLLIN
;
863 if (!(list_empty(&ipn_node
->msgqueue
))) mask
|= POLLRDNORM
;
864 if (!(list_empty(&ipn_node
->oobmsgqueue
))) mask
|= POLLPRI
;
865 if ((!(ipnn
->flags
& IPN_FLAG_LOSSLESS
)) |
866 (atomic_read(&ipnn
->msgpool_nelem
) < ipnn
->msgpool_size
))
867 mask
|= POLLOUT
| POLLWRNORM
;
872 /* connect netdev (from ioctl). connect a bound socket to a
873 * network device TAP or GRAB */
874 static int ipn_connect_netdev(struct socket
*sock
,struct ifreq
*ifr
)
877 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
878 struct ipn_network
*ipnn
=ipn_node
->ipn
;
879 if (!capable(CAP_NET_ADMIN
))
881 if (sock
->state
!= SS_UNCONNECTED
)
884 return -ENOTCONN
; /* Maybe we need a different error for "NOT BOUND" */
885 if (down_interruptible(&ipn_glob_mutex
))
887 if (down_interruptible(&ipnn
->ipnn_mutex
)) {
891 ipn_node
->dev
=ipn_netdev_alloc(ipn_node
->net
,ifr
->ifr_flags
,ifr
->ifr_name
,&err
);
894 portno
= ipn_protocol_table
[ipnn
->protocol
]->ipn_p_newport(ipn_node
);
895 if (portno
>= 0 && portno
<ipnn
->maxports
) {
896 sock
->state
= SS_CONNECTED
;
897 ipn_node
->portno
=portno
;
898 ipn_node
->flags
|= ifr
->ifr_flags
& IPN_NODEFLAG_DEVMASK
;
899 ipnn
->connport
[portno
]=ipn_node
;
900 err
=ipn_netdev_activate(ipn_node
);
902 sock
->state
= SS_UNCONNECTED
;
903 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_delport(ipn_node
);
905 ipn_node
->portno
= -1;
906 ipn_node
->flags
&= ~IPN_NODEFLAG_DEVMASK
;
907 ipnn
->connport
[portno
]=NULL
;
909 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_postnewport(ipn_node
);
910 list_del(&ipn_node
->nodelist
);
911 list_add_tail(&ipn_node
->nodelist
,&ipnn
->connectqueue
);
914 ipn_netdev_close(ipn_node
);
920 up(&ipnn
->ipnn_mutex
);
925 /* join a netdev, a socket gets connected to a persistent node
926 * not connected to another socket */
927 static int ipn_join_netdev(struct socket
*sock
,struct ifreq
*ifr
)
930 struct net_device
*dev
;
931 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
932 struct ipn_node
*ipn_joined
;
933 struct ipn_network
*ipnn
=ipn_node
->ipn
;
934 if (sock
->state
!= SS_UNCONNECTED
)
936 if (down_interruptible(&ipn_glob_mutex
))
938 if (down_interruptible(&ipnn
->ipnn_mutex
)) {
942 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
943 dev
=__dev_get_by_name(ifr
->ifr_name
);
945 dev
=__dev_get_by_name(ipn_node
->net
,ifr
->ifr_name
);
948 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
949 dev
=__dev_get_by_index(ifr
->ifr_ifindex
);
951 dev
=__dev_get_by_index(ipn_node
->net
,ifr
->ifr_ifindex
);
953 if (dev
&& (ipn_joined
=ipn_netdev2node(dev
)) != NULL
) { /* the interface does exist */
955 for (i
=0;i
<ipnn
->maxports
&& ipn_joined
!= ipnn
->connport
[i
] ;i
++)
957 if (i
< ipnn
->maxports
) { /* found */
958 /* ipn_joined is substituted to ipn_node */
959 ((struct ipn_sock
*)sock
->sk
)->node
=ipn_joined
;
960 ipn_joined
->flags
|= IPN_NODEFLAG_INUSE
;
962 kmem_cache_free(ipn_node_cache
,ipn_node
);
967 up(&ipnn
->ipnn_mutex
);
972 /* set persistence of a node looking for it by interface name
973 * (it is for sysadm, to close network interfaces)*/
974 static int ipn_setpersist_netdev(struct ifreq
*ifr
, int value
)
976 struct net_device
*dev
;
977 struct ipn_node
*ipn_node
;
979 if (!capable(CAP_NET_ADMIN
))
981 if (down_interruptible(&ipn_glob_mutex
))
983 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
984 dev
=__dev_get_by_name(ifr
->ifr_name
);
986 dev
=__dev_get_by_name(&init_net
,ifr
->ifr_name
);
989 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
990 dev
=__dev_get_by_index(ifr
->ifr_ifindex
);
992 dev
=__dev_get_by_index(&init_net
,ifr
->ifr_ifindex
);
994 if (dev
&& (ipn_node
=ipn_netdev2node(dev
)) != NULL
)
995 _ipn_setpersist(ipn_node
,value
);
1003 static int ipn_ioctl(struct socket
*sock
, unsigned int cmd
, unsigned long arg
) {
1004 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1005 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1006 void __user
* argp
= (void __user
*)arg
;
1009 if (ipn_node
->shutdown
== SHUTDOWN_XMASK
)
1016 case IPN_SETPERSIST_NETDEV
:
1017 case IPN_CLRPERSIST_NETDEV
:
1018 case IPN_CONN_NETDEV
:
1019 case IPN_JOIN_NETDEV
:
1021 if (copy_from_user(&ifr
, argp
, sizeof ifr
))
1023 ifr
.ifr_name
[IFNAMSIZ
-1] = '\0';
1026 /* actions for unconnected and unbound sockets */
1028 case IPN_SETPERSIST_NETDEV
:
1029 return ipn_setpersist_netdev(&ifr
,1);
1030 case IPN_CLRPERSIST_NETDEV
:
1031 return ipn_setpersist_netdev(&ifr
,0);
1033 if (capable(CAP_NET_ADMIN
))
1035 if (ipn_node
->dev
&& (ipn_node
->flags
&IPN_NODEFLAG_TAP
))
1036 return dev_set_mac_address(ipn_node
->dev
, &ifr
.ifr_hwaddr
);
1038 return -EADDRNOTAVAIL
;
1040 if (ipnn
== NULL
|| (ipnn
->flags
& IPN_FLAG_TERMINATED
))
1042 /* actions for connected or bound sockets */
1044 case IPN_CONN_NETDEV
:
1045 return ipn_connect_netdev(sock
,&ifr
);
1046 case IPN_JOIN_NETDEV
:
1047 return ipn_join_netdev(sock
,&ifr
);
1048 case IPN_SETPERSIST
:
1049 return ipn_setpersist(ipn_node
,arg
);
1053 if (down_interruptible(&ipnn
->ipnn_mutex
))
1054 return -ERESTARTSYS
;
1055 rv
=ipn_protocol_table
[ipn_node
->protocol
]->ipn_p_ioctl(ipn_node
,cmd
,arg
);
1056 up(&ipnn
->ipnn_mutex
);
1063 /* shutdown: close socket for input or for output.
1064 * shutdown can be called prior to connect and it is not reversible */
1065 static int ipn_shutdown(struct socket
*sock
, int mode
) {
1066 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1067 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1068 int oldshutdown
=ipn_node
->shutdown
;
1069 mode
= (mode
+1)&(RCV_SHUTDOWN
|SEND_SHUTDOWN
);
1071 ipn_node
->shutdown
|= mode
;
1074 if (down_interruptible(&ipnn
->ipnn_mutex
)) {
1075 ipn_node
->shutdown
= oldshutdown
;
1076 return -ERESTARTSYS
;
1078 oldshutdown
=ipn_node
->shutdown
-oldshutdown
;
1079 if (sock
->state
== SS_CONNECTED
&& oldshutdown
) {
1080 ipn_net_update_counters(ipnn
,
1081 (ipn_node
->shutdown
& RCV_SHUTDOWN
)?0:-1,
1082 (ipn_node
->shutdown
& SEND_SHUTDOWN
)?0:-1);
1085 /* if recv channel has been shut down, flush the recv queue */
1086 if ((ipn_node
->shutdown
& RCV_SHUTDOWN
))
1087 ipn_flush_recvqueue(ipn_node
);
1088 up(&ipnn
->ipnn_mutex
);
1093 /* injectmsg: a new message is entering the ipn network.
1094 * injectmsg gets called by send and by the grab/tap node */
1095 int ipn_proto_injectmsg(struct ipn_node
*from
, struct msgpool_item
*msg
)
1097 struct ipn_network
*ipnn
=from
->ipn
;
1099 if (down_interruptible(&ipnn
->ipnn_mutex
))
1102 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_handlemsg(from
, msg
);
1103 up(&ipnn
->ipnn_mutex
);
1109 static int ipn_sendmsg(struct kiocb
*kiocb
, struct socket
*sock
,
1110 struct msghdr
*msg
, size_t len
) {
1111 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1112 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1113 struct msgpool_item
*newmsg
;
1116 if (unlikely(sock
->state
!= SS_CONNECTED
))
1118 if (unlikely(ipn_node
->shutdown
& SEND_SHUTDOWN
)) {
1119 if (ipn_node
->shutdown
== SHUTDOWN_XMASK
)
1124 if (len
> ipnn
->mtu
)
1126 newmsg
=ipn_msgpool_alloc_locking(ipnn
);
1130 err
=memcpy_fromiovec(newmsg
->data
, msg
->msg_iov
, len
);
1132 ipn_proto_injectmsg(ipn_node
, newmsg
);
1133 ipn_msgpool_put(newmsg
,ipnn
);
1137 /* enqueue an oob message. "to" is the destination */
1138 void ipn_proto_oobsendmsg(struct ipn_node
*to
, struct msgpool_item
*msg
)
1141 if (!to
->dev
) { /* no oob to netdev */
1142 struct msgitem
*msgitem
;
1143 struct ipn_network
*ipnn
=to
->ipn
;
1144 spin_lock(&to
->msglock
);
1145 if ((to
->shutdown
& RCV_SHUTDOWN_NO_OOB
) == 0 &&
1146 (ipnn
->flags
& IPN_FLAG_LOSSLESS
||
1147 to
->oobmsgcount
< ipnn
->msgpool_size
)) {
1148 if ((msgitem
=kmem_cache_alloc(ipn_msgitem_cache
,GFP_KERNEL
))!=NULL
) {
1152 list_add_tail(&msgitem
->list
, &to
->oobmsgqueue
);
1153 ipn_msgpool_hold(msg
);
1156 spin_unlock(&to
->msglock
);
1157 wake_up_interruptible(&to
->read_wait
);
1162 /* ipn_proto_sendmsg is called by protocol implementation to enqueue a
1163 * for a destination (to).*/
1164 void ipn_proto_sendmsg(struct ipn_node
*to
, struct msgpool_item
*msg
)
1168 ipn_netdev_sendmsg(to
,msg
);
1171 struct msgitem
*msgitem
;
1172 struct ipn_network
*ipnn
=to
->ipn
;
1173 spin_lock(&to
->msglock
);
1174 if (likely((to
->shutdown
& RCV_SHUTDOWN
)==0)) {
1175 /*if (unlikely((ipnn->flags & IPN_FLAG_LOSSLESS) == 0 ||
1176 to->totmsgcount >= ipnn->msgpool_size))
1178 if (likely(ipnn
->flags
& IPN_FLAG_LOSSLESS
||
1179 to
->totmsgcount
< ipnn
->msgpool_size
)) {
1180 if ((msgitem
=kmem_cache_alloc(ipn_msgitem_cache
,GFP_KERNEL
))!=NULL
) {
1183 list_add_tail(&msgitem
->list
, &to
->msgqueue
);
1184 ipn_msgpool_hold(msg
);
1188 spin_unlock(&to
->msglock
);
1189 wake_up_interruptible(&to
->read_wait
);
1195 static int ipn_recvmsg(struct kiocb
*kiocb
, struct socket
*sock
,
1196 struct msghdr
*msg
, size_t len
, int flags
) {
1197 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1198 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1199 struct msgitem
*msgitem
;
1200 struct msgpool_item
*currmsg
;
1202 if (unlikely(sock
->state
!= SS_CONNECTED
))
1205 if (unlikely((ipn_node
->shutdown
& XRCV_SHUTDOWN
) == XRCV_SHUTDOWN
)) {
1206 if (ipn_node
->shutdown
== SHUTDOWN_XMASK
) /*EOF, nothing can be read*/
1209 return -EPIPE
; /*trying to read on a write only node */
1212 /* wait for a message */
1213 spin_lock(&ipn_node
->msglock
);
1214 while (ipn_node
->totmsgcount
== 0) {
1215 spin_unlock(&ipn_node
->msglock
);
1216 if (wait_event_interruptible(ipn_node
->read_wait
,
1217 !(ipn_node
->totmsgcount
== 0)))
1218 return -ERESTARTSYS
;
1219 spin_lock(&ipn_node
->msglock
);
1221 /* oob gets delivered first. oob are rare */
1222 if (likely(list_empty(&ipn_node
->oobmsgqueue
)))
1223 msgitem
=list_first_entry(&ipn_node
->msgqueue
, struct msgitem
, list
);
1225 msgitem
=list_first_entry(&ipn_node
->oobmsgqueue
, struct msgitem
, list
);
1226 msg
->msg_flags
|= MSG_OOB
;
1227 ipn_node
->oobmsgcount
--;
1229 list_del(&msgitem
->list
);
1230 ipn_node
->totmsgcount
--;
1231 spin_unlock(&ipn_node
->msglock
);
1232 currmsg
=msgitem
->msg
;
1233 if (currmsg
->len
< len
)
1235 memcpy_toiovec(msg
->msg_iov
, currmsg
->data
, len
);
1236 ipn_msgpool_put(currmsg
,ipnn
);
1237 kmem_cache_free(ipn_msgitem_cache
,msgitem
);
1242 /* resize a network: change the # of communication ports (connport) */
1243 static int ipn_netresize(struct ipn_network
*ipnn
,int newsize
)
1246 struct ipn_node
**newconnport
;
1247 struct ipn_node
**oldconnport
;
1249 if (down_interruptible(&ipnn
->ipnn_mutex
))
1250 return -ERESTARTSYS
;
1251 oldsize
=ipnn
->maxports
;
1252 if (newsize
== oldsize
) {
1253 up(&ipnn
->ipnn_mutex
);
1257 /* shrink a network. all the ports we are going to eliminate
1258 * must be unused! */
1259 if (newsize
< oldsize
) {
1261 for (i
=newsize
; i
<oldsize
; i
++)
1262 if (ipnn
->connport
[i
]) {
1263 up(&ipnn
->ipnn_mutex
);
1268 oldconnport
=ipnn
->connport
;
1269 /* allocate the new connport array and copy the old one */
1270 newconnport
=kzalloc(newsize
* sizeof(struct ipn_node
*),GFP_KERNEL
);
1272 up(&ipnn
->ipnn_mutex
);
1275 memcpy(newconnport
,oldconnport
,min
* sizeof(struct ipn_node
*));
1276 ipnn
->connport
=newconnport
;
1277 ipnn
->maxports
=newsize
;
1278 /* notify the protocol that the netowrk has been resized */
1279 err
=ipn_protocol_table
[ipnn
->protocol
]->ipn_p_resizenet(ipnn
,oldsize
,newsize
);
1281 /* roll back if the resize operation failed for the protocol */
1282 ipnn
->connport
=oldconnport
;
1283 ipnn
->maxports
=oldsize
;
1286 /* successful mission, network resized */
1288 up(&ipnn
->ipnn_mutex
);
1292 /* IPN SETSOCKOPT */
1293 static int ipn_setsockopt(struct socket
*sock
, int level
, int optname
,
1294 char __user
*optval
, int optlen
) {
1295 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1296 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1298 if (ipn_node
->shutdown
== SHUTDOWN_XMASK
)
1300 if (level
!= 0 && level
!= ipn_node
->protocol
+1)
1301 return -EPROTONOSUPPORT
;
1303 /* protocol specific sockopt */
1306 if (down_interruptible(&ipnn
->ipnn_mutex
))
1307 return -ERESTARTSYS
;
1308 rv
=ipn_protocol_table
[ipn_node
->protocol
]->ipn_p_setsockopt(ipn_node
,optname
,optval
,optlen
);
1309 up(&ipnn
->ipnn_mutex
);
1314 if (optname
== IPN_SO_DESCR
) {
1315 if (optlen
> IPN_DESCRLEN
)
1318 memset(ipn_node
->descr
,0,IPN_DESCRLEN
);
1319 if (copy_from_user(ipn_node
->descr
,optval
,optlen
))
1320 ipn_node
->descr
[0]=0;
1322 ipn_node
->descr
[optlen
-1]=0;
1326 if (optlen
< sizeof(int))
1328 else if ((optname
& IPN_SO_PREBIND
) && (ipnn
!= NULL
))
1332 get_user(val
, (int __user
*) optval
);
1333 if ((optname
& IPN_SO_PREBIND
) && !ipn_node
->pbp
) {
1334 struct pre_bind_parms std
=STD_BIND_PARMS
;
1335 ipn_node
->pbp
=kzalloc(sizeof(struct pre_bind_parms
),GFP_KERNEL
);
1338 *(ipn_node
->pbp
)=std
;
1342 if (sock
->state
== SS_UNCONNECTED
)
1343 ipn_node
->portno
=val
;
1347 case IPN_SO_CHANGE_NUMNODES
:
1348 if ((ipn_node
->flags
& IPN_NODEFLAG_BOUND
)!=0) {
1352 return ipn_netresize(ipnn
,val
);
1356 case IPN_SO_WANT_OOB_NUMNODES
:
1358 ipn_node
->flags
|= IPN_NODEFLAG_OOB_NUMNODES
;
1360 ipn_node
->flags
&= ~IPN_NODEFLAG_OOB_NUMNODES
;
1362 case IPN_SO_HANDLE_OOB
:
1364 ipn_node
->shutdown
&= ~RCV_SHUTDOWN_NO_OOB
;
1366 ipn_node
->shutdown
|= RCV_SHUTDOWN_NO_OOB
;
1372 ipn_node
->pbp
->mtu
=val
;
1374 case IPN_SO_NUMNODES
:
1378 ipn_node
->pbp
->maxports
=val
;
1380 case IPN_SO_MSGPOOLSIZE
:
1384 ipn_node
->pbp
->msgpoolsize
=val
;
1387 ipn_node
->pbp
->flags
=val
;
1390 ipn_node
->pbp
->mode
=val
;
1399 /* IPN GETSOCKOPT */
1400 static int ipn_getsockopt(struct socket
*sock
, int level
, int optname
,
1401 char __user
*optval
, int __user
*optlen
) {
1402 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1403 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1406 if (ipn_node
->shutdown
== SHUTDOWN_XMASK
)
1408 if (level
!= 0 && level
!= ipn_node
->protocol
+1)
1409 return -EPROTONOSUPPORT
;
1413 /* protocol specific sockopt */
1414 if (down_interruptible(&ipnn
->ipnn_mutex
))
1415 return -ERESTARTSYS
;
1416 rv
=ipn_protocol_table
[ipn_node
->protocol
]->ipn_p_getsockopt(ipn_node
,optname
,optval
,optlen
);
1417 up(&ipnn
->ipnn_mutex
);
1422 if (get_user(len
, optlen
))
1424 if (optname
== IPN_SO_DESCR
) {
1425 if (len
< IPN_DESCRLEN
)
1428 if (len
> IPN_DESCRLEN
)
1430 if(put_user(len
, optlen
))
1432 if(copy_to_user(optval
,ipn_node
->descr
,len
))
1440 val
=ipn_node
->portno
;
1445 else if (ipn_node
->pbp
)
1446 val
=ipn_node
->pbp
->mtu
;
1448 case IPN_SO_NUMNODES
:
1451 else if (ipn_node
->pbp
)
1452 val
=ipn_node
->pbp
->maxports
;
1454 case IPN_SO_MSGPOOLSIZE
:
1456 val
=ipnn
->msgpool_size
;
1457 else if (ipn_node
->pbp
)
1458 val
=ipn_node
->pbp
->msgpoolsize
;
1463 else if (ipn_node
->pbp
)
1464 val
=ipn_node
->pbp
->flags
;
1469 else if (ipn_node
->pbp
)
1470 val
=ipn_node
->pbp
->mode
;
1476 if (len
< sizeof(int))
1480 if(put_user(len
, optlen
))
1482 if(copy_to_user(optval
,&val
,len
))
1491 /* BROADCAST/HUB implementation */
1493 static int ipn_bcast_newport(struct ipn_node
*newport
) {
1494 struct ipn_network
*ipnn
=newport
->ipn
;
1496 for (i
=0;i
<ipnn
->maxports
;i
++) {
1497 if (ipnn
->connport
[i
] == NULL
)
1503 static int ipn_bcast_handlemsg(struct ipn_node
*from
,
1504 struct msgpool_item
*msgitem
){
1505 struct ipn_network
*ipnn
=from
->ipn
;
1507 struct ipn_node
*ipn_node
;
1508 list_for_each_entry(ipn_node
, &ipnn
->connectqueue
, nodelist
) {
1509 if (ipn_node
!= from
)
1510 ipn_proto_sendmsg(ipn_node
,msgitem
);
1515 static void ipn_null_delport(struct ipn_node
*oldport
) {}
1516 static void ipn_null_postnewport(struct ipn_node
*newport
) {}
1517 static void ipn_null_predelport(struct ipn_node
*oldport
) {}
1518 static int ipn_null_newnet(struct ipn_network
*newnet
) {return 0;}
1519 static int ipn_null_resizenet(struct ipn_network
*net
,int oldsize
,int newsize
) {
1521 static void ipn_null_delnet(struct ipn_network
*oldnet
) {}
1522 static int ipn_null_setsockopt(struct ipn_node
*port
,int optname
,
1523 char __user
*optval
, int optlen
) {return -EOPNOTSUPP
;}
1524 static int ipn_null_getsockopt(struct ipn_node
*port
,int optname
,
1525 char __user
*optval
, int *optlen
) {return -EOPNOTSUPP
;}
1526 static int ipn_null_ioctl(struct ipn_node
*port
,unsigned int request
,
1527 unsigned long arg
) {return -EOPNOTSUPP
;}
1529 /* Protocol Registration/deregisteration */
1531 void ipn_init_protocol(struct ipn_protocol
*p
)
1533 if (p
->ipn_p_delport
== NULL
) p
->ipn_p_delport
=ipn_null_delport
;
1534 if (p
->ipn_p_postnewport
== NULL
) p
->ipn_p_postnewport
=ipn_null_postnewport
;
1535 if (p
->ipn_p_predelport
== NULL
) p
->ipn_p_predelport
=ipn_null_predelport
;
1536 if (p
->ipn_p_newnet
== NULL
) p
->ipn_p_newnet
=ipn_null_newnet
;
1537 if (p
->ipn_p_resizenet
== NULL
) p
->ipn_p_resizenet
=ipn_null_resizenet
;
1538 if (p
->ipn_p_delnet
== NULL
) p
->ipn_p_delnet
=ipn_null_delnet
;
1539 if (p
->ipn_p_setsockopt
== NULL
) p
->ipn_p_setsockopt
=ipn_null_setsockopt
;
1540 if (p
->ipn_p_getsockopt
== NULL
) p
->ipn_p_getsockopt
=ipn_null_getsockopt
;
1541 if (p
->ipn_p_ioctl
== NULL
) p
->ipn_p_ioctl
=ipn_null_ioctl
;
1544 int ipn_proto_register(int protocol
,struct ipn_protocol
*ipn_service
)
1547 if (ipn_service
->ipn_p_newport
== NULL
||
1548 ipn_service
->ipn_p_handlemsg
== NULL
)
1550 ipn_init_protocol(ipn_service
);
1551 if (down_interruptible(&ipn_glob_mutex
))
1552 return -ERESTARTSYS
;
1553 if (protocol
> 1 && protocol
<= IPN_MAX_PROTO
) {
1555 if (ipn_protocol_table
[protocol
])
1558 ipn_service
->refcnt
=0;
1559 ipn_protocol_table
[protocol
]=ipn_service
;
1560 printk(KERN_INFO
"IPN: Registered protocol %d\n",protocol
+1);
1564 up(&ipn_glob_mutex
);
1568 int ipn_proto_deregister(int protocol
)
1571 if (down_interruptible(&ipn_glob_mutex
))
1572 return -ERESTARTSYS
;
1573 if (protocol
> 1 && protocol
<= IPN_MAX_PROTO
) {
1575 if (ipn_protocol_table
[protocol
]) {
1576 if (ipn_protocol_table
[protocol
]->refcnt
== 0) {
1577 ipn_protocol_table
[protocol
]=NULL
;
1578 printk(KERN_INFO
"IPN: Unregistered protocol %d\n",protocol
+1);
1585 up(&ipn_glob_mutex
);
1590 /* Module constructor/destructor */
1591 static struct net_proto_family ipn_family_ops
= {
1593 .create
= ipn_create
,
1594 .owner
= THIS_MODULE
,
1597 /* IPN constructor */
1598 static int ipn_init(void)
1602 ipn_init_protocol(&ipn_bcast
);
1603 ipn_network_cache
=kmem_cache_create("ipn_network",sizeof(struct ipn_network
),0,0,NULL
);
1604 if (!ipn_network_cache
) {
1605 printk(KERN_CRIT
"%s: Cannot create ipn_network SLAB cache!\n",
1611 ipn_node_cache
=kmem_cache_create("ipn_node",sizeof(struct ipn_node
),0,0,NULL
);
1612 if (!ipn_node_cache
) {
1613 printk(KERN_CRIT
"%s: Cannot create ipn_node SLAB cache!\n",
1619 ipn_msgitem_cache
=kmem_cache_create("ipn_msgitem",sizeof(struct msgitem
),0,0,NULL
);
1620 if (!ipn_msgitem_cache
) {
1621 printk(KERN_CRIT
"%s: Cannot create ipn_msgitem SLAB cache!\n",
1627 rc
=ipn_msgbuf_init();
1629 printk(KERN_CRIT
"%s: Cannot create ipn_msgbuf SLAB cache\n",
1631 goto out_net_node_msg
;
1634 rc
=proto_register(&ipn_proto
,1);
1636 printk(KERN_CRIT
"%s: Cannot register the protocol!\n",
1638 goto out_net_node_msg_msgbuf
;
1641 sock_register(&ipn_family_ops
);
1643 printk(KERN_INFO
"IPN: Virtual Square Project, University of Bologna 2007\n");
1646 out_net_node_msg_msgbuf
:
1649 kmem_cache_destroy(ipn_msgitem_cache
);
1651 kmem_cache_destroy(ipn_node_cache
);
1653 kmem_cache_destroy(ipn_network_cache
);
1658 /* IPN destructor */
1659 static void ipn_exit(void)
1662 if (ipn_msgitem_cache
)
1663 kmem_cache_destroy(ipn_msgitem_cache
);
1665 kmem_cache_destroy(ipn_node_cache
);
1666 if (ipn_network_cache
)
1667 kmem_cache_destroy(ipn_network_cache
);
1669 sock_unregister(PF_IPN
);
1670 proto_unregister(&ipn_proto
);
1671 printk(KERN_INFO
"IPN removed\n");
1674 module_init(ipn_init
);
1675 module_exit(ipn_exit
);
1677 EXPORT_SYMBOL_GPL(ipn_proto_register
);
1678 EXPORT_SYMBOL_GPL(ipn_proto_deregister
);
1679 EXPORT_SYMBOL_GPL(ipn_proto_sendmsg
);
1680 EXPORT_SYMBOL_GPL(ipn_proto_oobsendmsg
);
1681 EXPORT_SYMBOL_GPL(ipn_msgpool_alloc
);
1682 EXPORT_SYMBOL_GPL(ipn_msgpool_put
);