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 EXTREEEEMELY EXPERIMENTAL
21 #include <linux/init.h>
22 #include <linux/module.h>
23 #include <linux/socket.h>
24 #include <linux/poll.h>
26 #include <linux/list.h>
27 #include <linux/mount.h>
30 #include "ipn_netdev.h"
33 MODULE_LICENSE("GPL");
34 MODULE_AUTHOR("VIEW-OS TEAM");
35 MODULE_DESCRIPTION("IPN-VDE Kernel Module");
39 /* Network table and hash */
40 struct hlist_head ipn_network_table
[IPN_HASH_SIZE
+ 1];
41 DEFINE_SPINLOCK(ipn_table_lock
);
42 static struct kmem_cache
*ipn_node_cache
;
43 static struct kmem_cache
*ipn_msgitem_cache
;
44 static DECLARE_MUTEX(ipn_glob_mutex
);
46 /* Protocol 1: HUB/Broadcast default protocol. Function Prototypes */
47 static int ipn_bcast_newport(struct ipn_node
*newport
);
48 static int ipn_bcast_handlemsg(struct ipn_node
*from
,
49 struct msgpool_item
*msgitem
,
53 static struct ipn_protocol ipn_bcast
= {
55 .ipn_p_newport
=ipn_bcast_newport
,
56 .ipn_p_handlemsg
=ipn_bcast_handlemsg
};
57 static struct ipn_protocol
*ipn_protocol_table
[MAX_PROTO
]={&ipn_bcast
};
59 /* Socket call function prototypes */
60 static int ipn_release(struct socket
*);
61 static int ipn_bind(struct socket
*, struct sockaddr
*, int);
62 static int ipn_connect(struct socket
*, struct sockaddr
*,
63 int addr_len
, int flags
);
64 static int ipn_getname(struct socket
*, struct sockaddr
*, int *, int);
65 static unsigned int ipn_poll(struct file
*, struct socket
*, poll_table
*);
66 static int ipn_ioctl(struct socket
*, unsigned int, unsigned long);
67 static int ipn_shutdown(struct socket
*, int);
68 static int ipn_sendmsg(struct kiocb
*, struct socket
*,
69 struct msghdr
*, size_t);
70 static int ipn_recvmsg(struct kiocb
*, struct socket
*,
71 struct msghdr
*, size_t, int);
72 static int ipn_setsockopt(struct socket
*sock
, int level
, int optname
,
73 char __user
*optval
, int optlen
);
74 static int ipn_getsockopt(struct socket
*sock
, int level
, int optname
,
75 char __user
*optval
, int __user
*optlen
);
77 /* Network table Management */
78 static inline void ipn_insert_network(struct hlist_head
*list
, struct ipn_network
*ipnn
)
80 spin_lock(&ipn_table_lock
);
81 hlist_add_head(&ipnn
->hnode
, list
);
82 spin_unlock(&ipn_table_lock
);
85 static inline void ipn_remove_network(struct ipn_network
*ipnn
)
87 spin_lock(&ipn_table_lock
);
88 hlist_del(&ipnn
->hnode
);
89 spin_unlock(&ipn_table_lock
);
92 static struct ipn_network
*ipn_find_network_byinode(struct inode
*i
)
94 struct ipn_network
*ipnn
;
95 struct hlist_node
*node
;
97 spin_lock(&ipn_table_lock
);
98 hlist_for_each_entry(ipnn
, node
,
99 &ipn_network_table
[i
->i_ino
& (IPN_HASH_SIZE
- 1)], hnode
) {
100 struct dentry
*dentry
= ipnn
->dentry
;
102 if(atomic_read(&ipnn
->refcnt
) > 0 && dentry
&& dentry
->d_inode
== i
)
107 spin_unlock(&ipn_table_lock
);
111 static int ipn_check_node_connected(struct ipn_node
*ipn_node
)
113 struct ipn_network
*ipnn
;
114 struct hlist_node
*node
;
116 spin_lock(&ipn_table_lock
);
117 for (i
=0;i
<IPN_HASH_SIZE
;i
++)
119 hlist_for_each_entry(ipnn
, node
, &ipn_network_table
[i
], hnode
) {
120 for(j
=0;j
<ipnn
->maxports
;j
++) {
121 if (ipnn
->connport
[j
] == ipn_node
) {
122 spin_unlock(&ipn_table_lock
);
128 spin_unlock(&ipn_table_lock
);
132 static int ipn_setpersist(struct ipn_node
*ipn_node
, int persist
);
133 static void cleanup_persistent_nodes(void)
135 struct ipn_network
*ipnn
;
136 struct hlist_node
*node
;
138 spin_lock(&ipn_table_lock
);
139 for (i
=0;i
<IPN_HASH_SIZE
;i
++)
141 hlist_for_each_entry(ipnn
, node
, &ipn_network_table
[i
], hnode
) {
142 for(j
=0;j
<ipnn
->maxports
;j
++) {
143 struct ipn_node
*ipn_node
=ipnn
->connport
[j
];
144 if (ipn_node
&& (ipn_node
->dev
) && (ipn_node
->flags
& IPN_NODEFLAG_INUSE
) == 0)
145 ipn_setpersist(ipn_node
,0);
149 spin_unlock(&ipn_table_lock
);
152 /* msgpool management */
155 struct list_head list
;
156 struct msgpool_item
*msg
;
159 struct msgpool_item
*ipn_msgpool_alloc(struct ipn_network
*ipnn
)
161 struct msgpool_item
*new;
162 new=kmem_cache_alloc(ipnn
->msgpool_cache
,GFP_KERNEL
);
163 atomic_set(&new->count
,1);
164 atomic_inc(&ipnn
->msgpool_nelem
);
168 static struct msgpool_item
*ipn_msgpool_alloc_locking(struct ipn_network
*ipnn
)
170 if (ipnn
->flags
& IPN_FLAG_LOSSLESS
) {
171 while (atomic_read(&ipnn
->msgpool_nelem
) >= ipnn
->msgpool_size
) {
172 if (wait_event_interruptible_exclusive(ipnn
->send_wait
,
173 atomic_read(&ipnn
->msgpool_nelem
) < ipnn
->msgpool_size
))
177 return ipn_msgpool_alloc(ipnn
);
180 static inline void ipn_msgpool_hold(struct msgpool_item
*msg
)
182 atomic_inc(&msg
->count
);
185 void ipn_msgpool_put(struct msgpool_item
*old
,
186 struct ipn_network
*ipnn
)
188 if (atomic_dec_and_test(&old
->count
)) {
189 kmem_cache_free(ipnn
->msgpool_cache
,old
);
190 atomic_dec(&ipnn
->msgpool_nelem
);
191 if (ipnn
->flags
& IPN_FLAG_LOSSLESS
) /* could be done anyway */
192 wake_up_interruptible(&ipnn
->send_wait
);
197 static const struct proto_ops ipn_ops
= {
199 .owner
= THIS_MODULE
,
200 .release
= ipn_release
,
202 .connect
= ipn_connect
,
203 .socketpair
= sock_no_socketpair
,
204 .accept
= sock_no_accept
,
205 .getname
= ipn_getname
,
208 .listen
= sock_no_listen
,
209 .shutdown
= ipn_shutdown
,
210 .setsockopt
= ipn_setsockopt
,
211 .getsockopt
= ipn_getsockopt
,
212 .sendmsg
= ipn_sendmsg
,
213 .recvmsg
= ipn_recvmsg
,
214 .mmap
= sock_no_mmap
,
215 .sendpage
= sock_no_sendpage
,
218 static struct proto ipn_proto
= {
220 .owner
= THIS_MODULE
,
221 .obj_size
= sizeof(struct ipn_sock
),
224 static int ipn_create(struct socket
*sock
, int protocol
)
226 struct ipn_sock
*ipn_sk
;
227 struct ipn_node
*ipn_node
;
228 //printk("IPN create %d\n",protocol);
229 if (sock
->type
!= SOCK_RAW
)
234 protocol
=IPN_BROADCAST
-1;
235 if (protocol
< 0 || protocol
>= MAX_PROTO
||
236 ipn_protocol_table
[protocol
] == NULL
)
237 return -EPROTONOSUPPORT
;
238 ipn_sk
= (struct ipn_sock
*) sk_alloc(PF_IPN
, GFP_KERNEL
, &ipn_proto
, 1);
242 ipn_sk
->node
=ipn_node
=kmem_cache_alloc(ipn_node_cache
,GFP_KERNEL
);
244 sock_put((struct sock
*) ipn_sk
);
247 sock_init_data(sock
,(struct sock
*) ipn_sk
);
248 sock
->state
= SS_UNCONNECTED
;
249 sock
->ops
= &ipn_ops
;
250 sock
->sk
=(struct sock
*)ipn_sk
;
251 ipn_node
->protocol
=protocol
;
252 ipn_node
->flags
=IPN_NODEFLAG_INUSE
;
253 ipn_node
->shutdown
=0;
254 ipn_node
->descr
[0]=0;
255 ipn_node
->portno
=IPN_PORTNO_ANY
;
257 ipn_node
->proto_private
=NULL
;
258 spin_lock_init(&ipn_node
->msglock
);
259 INIT_LIST_HEAD(&ipn_node
->msgqueue
);
261 init_waitqueue_head(&ipn_node
->read_wait
);
266 static void ipn_flush_recvqueue(struct ipn_node
*ipn_node
)
268 struct ipn_network
*ipnn
=ipn_node
->ipn
;
269 /* delete all pending msgs */
270 spin_lock(&ipn_node
->msglock
);
271 while (!list_empty(&ipn_node
->msgqueue
)) {
272 struct msgitem
*msgitem
=
273 list_first_entry(&ipn_node
->msgqueue
, struct msgitem
, list
);
274 list_del(&msgitem
->list
);
275 ipn_node
->msgcount
--;
276 ipn_msgpool_put(msgitem
->msg
,ipnn
);
277 kmem_cache_free(ipn_msgitem_cache
,msgitem
);
279 spin_unlock(&ipn_node
->msglock
);
282 static int ipn_release_node(struct ipn_node
*ipn_node
)
284 if (!(ipn_node
->flags
& IPN_NODEFLAG_PERSIST
))
286 struct ipn_network
*ipnn
=ipn_node
->ipn
;
288 if (down_interruptible(&ipnn
->ipnn_mutex
)) {
292 if (ipn_node
->portno
>= 0) {
293 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_predelport(ipn_node
);
294 ipnn
->connport
[ipn_node
->portno
]=NULL
;
296 up(&ipnn
->ipnn_mutex
);
297 ipn_flush_recvqueue(ipn_node
);
299 ipn_netdev_close(ipn_node
);
300 if (ipn_node
->portno
>= 0) {
301 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_delport(ipn_node
);
304 /* No more network elements */
305 if (atomic_dec_and_test(&ipnn
->refcnt
))
307 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_delnet(ipnn
);
308 ipn_remove_network(ipnn
);
309 ipn_protocol_table
[ipnn
->protocol
]->refcnt
--;
314 module_put(THIS_MODULE
);
315 if (ipnn
->msgpool_cache
)
316 kmem_cache_destroy(ipnn
->msgpool_cache
);
321 kfree(ipn_node
->pbp
);
324 kmem_cache_free(ipn_node_cache
,ipn_node
);
326 ipn_node
->flags
&= ~IPN_NODEFLAG_INUSE
;
330 static int ipn_release (struct socket
*sock
)
332 struct ipn_sock
*ipn_sk
=(struct ipn_sock
*)sock
->sk
;
333 struct ipn_node
*ipn_node
=ipn_sk
->node
;
335 //printk("IPN release\n");
336 if (down_interruptible(&ipn_glob_mutex
))
338 rv
=ipn_release_node(ipn_node
);
340 sock_put((struct sock
*) ipn_sk
);
345 static int ipn_setpersist(struct ipn_node
*ipn_node
, int persist
)
347 if (ipn_node
->dev
== NULL
)
349 if (down_interruptible(&ipn_glob_mutex
))
352 ipn_node
->flags
|= IPN_NODEFLAG_PERSIST
;
354 ipn_node
->flags
&= ~IPN_NODEFLAG_PERSIST
;
355 if (!(ipn_node
->flags
& IPN_NODEFLAG_INUSE
))
356 ipn_release_node(ipn_node
);
363 struct pre_bind_parms
{
364 unsigned short maxports
;
365 unsigned short flags
;
366 unsigned short msgpoolsize
;
371 /* STD_PARMS: BITS_PER_BYTE nodes, no flags, BITS_PER_BYTE pending msgs,
372 * Ethernet + VLAN MTU*/
373 #define STD_BIND_PARMS {BITS_PER_BYTE, 0, BITS_PER_BYTE, 1514, 0x777};
375 static int ipn_mkname(struct sockaddr_un
* sunaddr
, int len
)
377 if (len
<= sizeof(short) || len
> sizeof(*sunaddr
))
379 if (!sunaddr
|| sunaddr
->sun_family
!= AF_IPN
)
382 * This may look like an off by one error but it is a bit more
383 * subtle. 108 is the longest valid AF_IPN path for a binding.
384 * sun_path[108] doesnt as such exist. However in kernel space
385 * we are guaranteed that it is a valid memory location in our
386 * kernel address buffer.
388 ((char *)sunaddr
)[len
]=0;
389 len
= strlen(sunaddr
->sun_path
)+1+sizeof(short);
394 static int ipn_bind(struct socket
*sock
, struct sockaddr
*uaddr
, int addr_len
)
396 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)uaddr
;
397 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
399 struct ipn_network
*ipnn
;
400 struct dentry
* dentry
= NULL
;
402 struct pre_bind_parms parms
=STD_BIND_PARMS
;
404 //printk("IPN bind\n");
406 if (down_interruptible(&ipn_glob_mutex
))
408 if (sock
->state
!= SS_UNCONNECTED
||
409 ipn_node
->ipn
!= NULL
) {
414 if (ipn_node
->protocol
>= 0 &&
415 (ipn_node
->protocol
>= MAX_PROTO
||
416 ipn_protocol_table
[ipn_node
->protocol
] == NULL
)) {
417 err
= -EPROTONOSUPPORT
;
421 addr_len
= ipn_mkname(sunaddr
, addr_len
);
427 /* check if there is already a socket with that name */
428 err
= path_lookup(sunaddr
->sun_path
, LOOKUP_FOLLOW
, &nd
);
429 if (err
) { /* it does not exist, NEW IPN socket! */
431 /* Is it everything okay with the parent? */
432 err
= path_lookup(sunaddr
->sun_path
, LOOKUP_PARENT
, &nd
);
434 goto out_mknod_parent
;
435 /* Do I have the permission to create a file? */
436 dentry
= lookup_create(&nd
, 0);
437 err
= PTR_ERR(dentry
);
439 goto out_mknod_unlock
;
441 * All right, let's create it.
444 mode
= ipn_node
->pbp
->mode
;
446 mode
= SOCK_INODE(sock
)->i_mode
;
447 mode
= S_IFSOCK
| (mode
& ~current
->fs
->umask
);
448 err
= vfs_mknod(nd
.dentry
->d_inode
, dentry
, mode
, 0);
451 mutex_unlock(&nd
.dentry
->d_inode
->i_mutex
);
454 /* create a new ipn_network item */
456 parms
=*ipn_node
->pbp
;
457 /* kzalloc the network structure (it cannot be a slub because the size depends
459 ipnn
=kzalloc(sizeof(struct ipn_network
) +
460 (parms
.maxports
* sizeof(struct ipn_node
*)),GFP_KERNEL
);
465 if (!try_module_get(THIS_MODULE
)) {
469 memcpy(&ipnn
->sunaddr
,sunaddr
,addr_len
);
471 ipnn
->msgpool_cache
=kmem_cache_create(ipnn
->sunaddr
.sun_path
,sizeof(struct msgpool_item
)+ipnn
->mtu
,0,0,NULL
,NULL
);
472 if (!ipnn
->msgpool_cache
) {
474 goto out_mknod_dput_putmodule
;
476 atomic_set(&ipnn
->refcnt
,1);
477 ipnn
->dentry
=nd
.dentry
;
479 init_MUTEX(&ipnn
->ipnn_mutex
);
480 ipnn
->sunaddr_len
=addr_len
;
481 ipnn
->protocol
=ipn_node
->protocol
;
482 if (ipnn
->protocol
< 0) ipnn
->protocol
= 0;
483 ipn_protocol_table
[ipnn
->protocol
]->refcnt
++;
484 ipnn
->flags
=parms
.flags
;
485 ipnn
->maxports
=parms
.maxports
;
486 atomic_set(&ipnn
->msgpool_nelem
,0);
487 ipnn
->msgpool_size
=parms
.msgpoolsize
;
488 ipnn
->proto_private
=NULL
;
489 init_waitqueue_head(&ipnn
->send_wait
);
490 err
=ipn_protocol_table
[ipnn
->protocol
]->ipn_p_newnet(ipnn
);
492 goto out_mknod_dput_putmodule
;
493 ipn_insert_network(&ipn_network_table
[nd
.dentry
->d_inode
->i_ino
& (IPN_HASH_SIZE
-1)],ipnn
);
495 err
= vfs_permission(&nd
, MAY_EXEC
);
499 if (!S_ISSOCK(nd
.dentry
->d_inode
->i_mode
))
501 ipnn
=ipn_find_network_byinode(nd
.dentry
->d_inode
);
504 atomic_inc(&ipnn
->refcnt
);
507 kfree(ipn_node
->pbp
);
511 ipn_node
->flags
|= IPN_NODEFLAG_BOUND
;
521 out_mknod_dput_putmodule
:
522 module_put(THIS_MODULE
);
526 mutex_unlock(&nd
.dentry
->d_inode
->i_mutex
);
535 static int ipn_connect(struct socket
*sock
, struct sockaddr
*addr
,
536 int addr_len
, int flags
){
537 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)addr
;
538 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
540 struct ipn_network
*ipnn
,*previousipnn
;
544 /* the socket cannot be connected twice */
545 if (sock
->state
!= SS_UNCONNECTED
)
548 if (down_interruptible(&ipn_glob_mutex
))
551 if ((previousipnn
=ipn_node
->ipn
) == NULL
) { /* unbound */
552 unsigned char mustshutdown
=0;
553 err
= ipn_mkname(sunaddr
, addr_len
);
557 err
= path_lookup(sunaddr
->sun_path
, LOOKUP_FOLLOW
, &nd
);
560 err
= vfs_permission(&nd
, MAY_READ
);
562 if (err
== -EACCES
|| err
== -EROFS
)
563 mustshutdown
|=RCV_SHUTDOWN
;
567 err
= vfs_permission(&nd
, MAY_WRITE
);
570 mustshutdown
|=SEND_SHUTDOWN
;
574 /*printk("NODE %d PERM %d BOTH %d\n",ipn_node->shutdown,mustshutdown,mustshutdown|ipn_node->shutdown);*/
575 mustshutdown
|= ipn_node
->shutdown
;
576 if (mustshutdown
== (RCV_SHUTDOWN
| SEND_SHUTDOWN
)) {
581 ipn_node
->shutdown
=mustshutdown
;
583 if (!S_ISSOCK(nd
.dentry
->d_inode
->i_mode
)) {
587 ipnn
=ipn_find_network_byinode(nd
.dentry
->d_inode
);
592 if (ipn_node
->protocol
== IPN_ANY
)
593 ipn_node
->protocol
=ipnn
->protocol
;
594 else if (ipnn
->protocol
!= ipn_node
->protocol
) {
603 /* is it possible to close ipn_glob_mutex here? */
604 if (down_interruptible(&ipnn
->ipnn_mutex
)) {
608 portno
= ipn_protocol_table
[ipnn
->protocol
]->ipn_p_newport(ipn_node
);
609 if (portno
>= 0 && portno
<ipnn
->maxports
) {
610 sock
->state
= SS_CONNECTED
;
611 ipn_node
->portno
=portno
;
612 ipnn
->connport
[portno
]=ipn_node
;
613 if (!(ipn_node
->flags
& IPN_NODEFLAG_BOUND
))
614 atomic_inc(&ipnn
->refcnt
);
616 ipn_node
->ipn
=previousipnn
; /* undo changes on ipn_node->ipn */
619 up(&ipnn
->ipnn_mutex
);
630 static int ipn_getname(struct socket
*sock
, struct sockaddr
*uaddr
,
631 int *uaddr_len
, int peer
) {
632 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
633 struct ipn_network
*ipnn
=ipn_node
->ipn
;
634 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)uaddr
;
638 *uaddr_len
= ipnn
->sunaddr_len
;
639 memcpy(sunaddr
,&ipnn
->sunaddr
,*uaddr_len
);
645 static unsigned int ipn_poll(struct file
*file
, struct socket
*sock
,
647 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
648 struct ipn_network
*ipnn
=ipn_node
->ipn
;
652 poll_wait(file
,&ipn_node
->read_wait
,wait
);
653 if (ipnn
->flags
& IPN_FLAG_LOSSLESS
)
654 poll_wait(file
,&ipnn
->send_wait
,wait
);
655 if (!(list_empty(&ipn_node
->msgqueue
))) mask
|= POLLIN
| POLLRDNORM
;
656 if ((!(ipnn
->flags
& IPN_FLAG_LOSSLESS
)) |
657 (atomic_read(&ipnn
->msgpool_nelem
) < ipnn
->msgpool_size
))
658 mask
|= POLLOUT
| POLLWRNORM
;
663 static int ipn_connect_netdev(struct socket
*sock
,struct ifreq
*ifr
)
666 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
667 struct ipn_network
*ipnn
=ipn_node
->ipn
;
668 if (!capable(CAP_NET_ADMIN
))
670 if (sock
->state
!= SS_UNCONNECTED
)
673 return -ENOTCONN
; /* Maybe we need a different error for "NOT BOUND" */
674 if (down_interruptible(&ipn_glob_mutex
))
676 if (down_interruptible(&ipnn
->ipnn_mutex
)) {
680 ipn_node
->dev
=ipn_netdev_alloc(ifr
->ifr_flags
,ifr
->ifr_name
,&err
);
683 portno
= ipn_protocol_table
[ipnn
->protocol
]->ipn_p_newport(ipn_node
);
684 if (portno
>= 0 && portno
<ipnn
->maxports
) {
685 sock
->state
= SS_CONNECTED
;
686 ipn_node
->portno
=portno
;
687 ipn_node
->flags
|= ifr
->ifr_flags
& IPN_NODEFLAG_DEVMASK
;
688 ipnn
->connport
[portno
]=ipn_node
;
689 err
=ipn_netdev_activate(ipn_node
);
691 sock
->state
= SS_UNCONNECTED
;
692 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_delport(ipn_node
);
694 ipn_node
->portno
= -1;
695 ipn_node
->flags
&= ~IPN_NODEFLAG_DEVMASK
;
696 ipnn
->connport
[portno
]=NULL
;
698 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_postnewport(ipn_node
);
700 ipn_netdev_close(ipn_node
); /*unregister unregistered dev problem!*/
706 up(&ipnn
->ipnn_mutex
);
711 static int ipn_join_netdev(struct socket
*sock
,struct ifreq
*ifr
)
714 struct net_device
*dev
;
715 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
716 struct ipn_node
*ipn_joined
;
717 struct ipn_network
*ipnn
=ipn_node
->ipn
;
718 if (sock
->state
!= SS_UNCONNECTED
)
720 if (down_interruptible(&ipn_glob_mutex
))
722 if (down_interruptible(&ipnn
->ipnn_mutex
)) {
726 dev
=dev_get_by_name(ifr
->ifr_name
);
728 dev
=dev_get_by_index(ifr
->ifr_ifindex
);
729 if (dev
&& (ipn_joined
=ipn_netdev2node(dev
)) != NULL
) { /* the interface does exist */
731 for (i
=0;i
<ipnn
->maxports
&& ipn_joined
!= ipnn
->connport
[i
] ;i
++)
733 if (i
< ipnn
->maxports
) { /* found */
734 /* ipn_joined is substituted to ipn_node */
735 ((struct ipn_sock
*)sock
->sk
)->node
=ipn_joined
;
736 ipn_node
->flags
|= IPN_NODEFLAG_INUSE
;
737 atomic_dec(&ipnn
->refcnt
);
738 kmem_cache_free(ipn_node_cache
,ipn_node
);
743 up(&ipnn
->ipnn_mutex
);
748 static int ipn_setpersist_netdev(struct ifreq
*ifr
, int value
)
750 struct net_device
*dev
;
751 struct ipn_node
*ipn_node
;
753 if (!capable(CAP_NET_ADMIN
))
755 if (down_interruptible(&ipn_glob_mutex
))
757 dev
=dev_get_by_name(ifr
->ifr_name
);
759 dev
=dev_get_by_index(ifr
->ifr_ifindex
);
760 if (dev
&& (ipn_node
=ipn_netdev2node(dev
)) != NULL
&&
761 ipn_check_node_connected(ipn_node
) ) { /* the interface does exist */
762 ipn_setpersist(ipn_node
,value
);
769 static int ipn_ioctl(struct socket
*sock
, unsigned int cmd
, unsigned long arg
) {
770 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
771 struct ipn_network
*ipnn
=ipn_node
->ipn
;
772 void __user
* argp
= (void __user
*)arg
;
776 case IPN_SETPERSIST_NETDEV
:
777 case IPN_CLRPERSIST_NETDEV
:
778 case IPN_CONN_NETDEV
:
779 case IPN_JOIN_NETDEV
:
781 if (copy_from_user(&ifr
, argp
, sizeof ifr
))
783 ifr
.ifr_name
[IFNAMSIZ
-1] = '\0';
787 case IPN_SETPERSIST_NETDEV
:
788 return ipn_setpersist_netdev(&ifr
,1);
789 case IPN_CLRPERSIST_NETDEV
:
790 return ipn_setpersist_netdev(&ifr
,0);
792 if (capable(CAP_NET_ADMIN
))
794 if (ipn_node
->dev
&& (ipn_node
->flags
&IPN_NODEFLAG_TAP
))
795 return dev_set_mac_address(ipn_node
->dev
, &ifr
.ifr_hwaddr
);
797 return -EADDRNOTAVAIL
;
799 if (ipn_node
->ipn
== NULL
)
802 case IPN_CONN_NETDEV
:
803 return ipn_connect_netdev(sock
,&ifr
);
804 case IPN_JOIN_NETDEV
:
805 return ipn_join_netdev(sock
,&ifr
);
807 return ipn_setpersist(ipn_node
,arg
);
811 if (down_interruptible(&ipnn
->ipnn_mutex
))
813 rv
=ipn_protocol_table
[ipn_node
->protocol
]->ipn_p_ioctl(ipn_node
,cmd
,arg
);
814 up(&ipnn
->ipnn_mutex
);
821 static int ipn_shutdown(struct socket
*sock
, int mode
) {
822 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
823 struct ipn_network
*ipnn
=ipn_node
->ipn
;
824 mode
= (mode
+1)&(RCV_SHUTDOWN
|SEND_SHUTDOWN
);
826 ipn_node
->shutdown
|= mode
;
828 /* if recv channel has been shut down, flush the recv queue */
829 if ((ipn_node
->shutdown
& RCV_SHUTDOWN
) && ipnn
)
830 ipn_flush_recvqueue(ipn_node
);
835 int ipn_proto_injectmsg(struct ipn_node
*from
, struct msgpool_item
*msg
,
838 struct ipn_network
*ipnn
=from
->ipn
;
840 if (down_interruptible(&ipnn
->ipnn_mutex
))
843 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_handlemsg(from
, msg
, depth
);
844 up(&ipnn
->ipnn_mutex
);
850 static int ipn_sendmsg(struct kiocb
*kiocb
, struct socket
*sock
,
851 struct msghdr
*msg
, size_t len
) {
852 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
853 struct ipn_network
*ipnn
=ipn_node
->ipn
;
854 struct msgpool_item
*newmsg
;
857 if (sock
->state
!= SS_CONNECTED
)
859 if (ipn_node
->shutdown
& SEND_SHUTDOWN
)
863 newmsg
=ipn_msgpool_alloc_locking(ipnn
);
867 err
=memcpy_fromiovec(newmsg
->data
, msg
->msg_iov
, len
);
869 ipn_proto_injectmsg(ipn_node
, newmsg
, 0);
870 ipn_msgpool_put(newmsg
,ipnn
);
874 void ipn_proto_sendmsg(struct ipn_node
*to
, struct msgpool_item
*msg
,
878 /* printk("SEND MSG TO %d\n",to->portno); */
880 ipn_netdev_sendmsg(to
,msg
);
883 struct msgitem
*msgitem
;
884 struct ipn_network
*ipnn
=to
->ipn
;
885 spin_lock(&to
->msglock
);
886 if (ipnn
->flags
& IPN_FLAG_LOSSLESS
||
887 to
->msgcount
< ipnn
->msgpool_size
) {
888 msgitem
=kmem_cache_alloc(ipn_msgitem_cache
,GFP_KERNEL
);
891 list_add_tail(&msgitem
->list
, &to
->msgqueue
);
892 ipn_msgpool_hold(msg
);
894 spin_unlock(&to
->msglock
);
895 wake_up_interruptible(&to
->read_wait
);
900 static int ipn_recvmsg(struct kiocb
*kiocb
, struct socket
*sock
,
901 struct msghdr
*msg
, size_t len
, int flags
) {
902 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
903 struct ipn_network
*ipnn
=ipn_node
->ipn
;
904 struct msgitem
*msgitem
;
905 struct msgpool_item
*currmsg
;
907 if (sock
->state
!= SS_CONNECTED
)
909 if (ipn_node
->shutdown
& RCV_SHUTDOWN
)
912 spin_lock(&ipn_node
->msglock
);
913 while (list_empty(&ipn_node
->msgqueue
)) {
914 spin_unlock(&ipn_node
->msglock
);
915 if (wait_event_interruptible(ipn_node
->read_wait
,
916 !(list_empty(&ipn_node
->msgqueue
))))
918 spin_lock(&ipn_node
->msglock
);
920 msgitem
=list_first_entry(&ipn_node
->msgqueue
, struct msgitem
, list
);
921 list_del(&msgitem
->list
);
922 ipn_node
->msgcount
--;
923 spin_unlock(&ipn_node
->msglock
);
924 currmsg
=msgitem
->msg
;
925 if (currmsg
->len
< len
)
927 memcpy_toiovec(msg
->msg_iov
, currmsg
->data
, len
);
928 ipn_msgpool_put(currmsg
,ipnn
);
929 kmem_cache_free(ipn_msgitem_cache
,msgitem
);
934 static int ipn_setsockopt(struct socket
*sock
, int level
, int optname
,
935 char __user
*optval
, int optlen
) {
936 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
937 struct ipn_network
*ipnn
=ipn_node
->ipn
;
939 if (level
!= 0 && level
!= ipn_node
->protocol
+1)
940 return -EPROTONOSUPPORT
;
944 if (down_interruptible(&ipnn
->ipnn_mutex
))
946 rv
=ipn_protocol_table
[ipn_node
->protocol
]->ipn_p_setsockopt(ipn_node
,optname
,optval
,optlen
);
947 up(&ipnn
->ipnn_mutex
);
952 if (optname
== IPN_SO_DESCR
) {
953 if (optlen
> IPN_DESCRLEN
)
956 memcpy(ipn_node
->descr
,optval
,optlen
);
957 ipn_node
->descr
[optlen
-1]=0;
961 if (optlen
< sizeof(int))
963 else if ((optname
& IPN_SO_PREBIND
) && (ipnn
!= NULL
))
966 int val
=*((int *)optval
);
967 if ((optname
& IPN_SO_PREBIND
) && !ipn_node
->pbp
) {
968 struct pre_bind_parms std
=STD_BIND_PARMS
;
969 ipn_node
->pbp
=kzalloc(sizeof(struct pre_bind_parms
),GFP_KERNEL
);
972 *(ipn_node
->pbp
)=std
;
976 if (sock
->state
== SS_UNCONNECTED
)
977 ipn_node
->portno
=val
;
982 ipn_node
->pbp
->mtu
=val
;
984 case IPN_SO_NUMNODES
:
985 ipn_node
->pbp
->maxports
=val
;
987 case IPN_SO_MSGPOOLSIZE
:
988 ipn_node
->pbp
->msgpoolsize
=val
;
991 ipn_node
->pbp
->flags
=val
;
994 ipn_node
->pbp
->mode
=val
;
1003 static int ipn_getsockopt(struct socket
*sock
, int level
, int optname
,
1004 char __user
*optval
, int __user
*optlen
) {
1005 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1006 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1008 if (level
!= 0 && level
!= ipn_node
->protocol
+1)
1009 return -EPROTONOSUPPORT
;
1013 if (down_interruptible(&ipnn
->ipnn_mutex
))
1014 return -ERESTARTSYS
;
1015 rv
=ipn_protocol_table
[ipn_node
->protocol
]->ipn_p_getsockopt(ipn_node
,optname
,optval
,optlen
);
1016 up(&ipnn
->ipnn_mutex
);
1021 if (optname
== IPN_SO_DESCR
) {
1022 if (*optlen
< IPN_DESCRLEN
)
1025 if (*optlen
> IPN_DESCRLEN
)
1026 *optlen
=IPN_DESCRLEN
;
1027 memcpy(optval
,ipn_node
->descr
,*optlen
);
1034 val
=ipn_node
->portno
;
1039 else if (ipn_node
->pbp
)
1040 val
=ipn_node
->pbp
->mtu
;
1042 case IPN_SO_NUMNODES
:
1045 else if (ipn_node
->pbp
)
1046 val
=ipn_node
->pbp
->maxports
;
1048 case IPN_SO_MSGPOOLSIZE
:
1050 val
=ipnn
->msgpool_size
;
1051 else if (ipn_node
->pbp
)
1052 val
=ipn_node
->pbp
->msgpoolsize
;
1057 else if (ipn_node
->pbp
)
1058 val
=ipn_node
->pbp
->flags
;
1063 else if (ipn_node
->pbp
)
1064 val
=ipn_node
->pbp
->mode
;
1070 if (*optlen
< sizeof(int))
1073 *optlen
=sizeof(int);
1074 *((int *) optval
) = val
;
1082 /* BROADCAST/HUB implementation */
1084 static int ipn_bcast_newport(struct ipn_node
*newport
) {
1085 struct ipn_network
*ipnn
=newport
->ipn
;
1087 for (i
=0;i
<ipnn
->maxports
;i
++) {
1088 if (ipnn
->connport
[i
] == NULL
)
1094 static int ipn_bcast_handlemsg(struct ipn_node
*from
,
1095 struct msgpool_item
*msgitem
,
1097 struct ipn_network
*ipnn
=from
->ipn
;
1099 for (i
=0; i
<ipnn
->maxports
; i
++)
1100 if (ipnn
->connport
[i
] && ipnn
->connport
[i
] != from
)
1101 ipn_proto_sendmsg(ipnn
->connport
[i
],msgitem
,depth
);
1105 static void ipn_null_delport(struct ipn_node
*oldport
) {}
1106 static void ipn_null_postnewport(struct ipn_node
*newport
) {}
1107 static void ipn_null_predelport(struct ipn_node
*oldport
) {}
1108 static int ipn_null_newnet(struct ipn_network
*newnet
) {return 0;}
1109 static void ipn_null_delnet(struct ipn_network
*oldnet
) {}
1110 static int ipn_null_setsockopt(struct ipn_node
*port
,int optname
,
1111 char __user
*optval
, int optlen
) {return -EOPNOTSUPP
;}
1112 static int ipn_null_getsockopt(struct ipn_node
*port
,int optname
,
1113 char __user
*optval
, int *optlen
) {return -EOPNOTSUPP
;}
1114 static int ipn_null_ioctl(struct ipn_node
*port
,unsigned int request
,
1115 unsigned long arg
) {return -EOPNOTSUPP
;}
1117 /* Protocol Registration/deregisteration */
1119 void ipn_init_protocol(struct ipn_protocol
*p
)
1121 if (p
->ipn_p_delport
== NULL
) p
->ipn_p_delport
=ipn_null_delport
;
1122 if (p
->ipn_p_postnewport
== NULL
) p
->ipn_p_postnewport
=ipn_null_postnewport
;
1123 if (p
->ipn_p_predelport
== NULL
) p
->ipn_p_predelport
=ipn_null_predelport
;
1124 if (p
->ipn_p_newnet
== NULL
) p
->ipn_p_newnet
=ipn_null_newnet
;
1125 if (p
->ipn_p_delnet
== NULL
) p
->ipn_p_delnet
=ipn_null_delnet
;
1126 if (p
->ipn_p_setsockopt
== NULL
) p
->ipn_p_setsockopt
=ipn_null_setsockopt
;
1127 if (p
->ipn_p_getsockopt
== NULL
) p
->ipn_p_getsockopt
=ipn_null_getsockopt
;
1128 if (p
->ipn_p_ioctl
== NULL
) p
->ipn_p_ioctl
=ipn_null_ioctl
;
1131 int ipn_proto_register(int protocol
,struct ipn_protocol
*ipn_service
)
1134 if (ipn_service
->ipn_p_newport
== NULL
||
1135 ipn_service
->ipn_p_handlemsg
== NULL
)
1137 ipn_init_protocol(ipn_service
);
1138 if (down_interruptible(&ipn_glob_mutex
))
1139 return -ERESTARTSYS
;
1140 if (protocol
> 1 && protocol
<= MAX_PROTO
) {
1142 if (ipn_protocol_table
[protocol
])
1145 ipn_service
->refcnt
=0;
1146 ipn_protocol_table
[protocol
]=ipn_service
;
1147 printk(KERN_INFO
"IPN-VDE: Registered protocol %d\n",protocol
+1);
1151 up(&ipn_glob_mutex
);
1155 int ipn_proto_deregister(int protocol
)
1158 if (down_interruptible(&ipn_glob_mutex
))
1159 return -ERESTARTSYS
;
1160 if (protocol
> 1 && protocol
<= MAX_PROTO
) {
1162 if (ipn_protocol_table
[protocol
]) {
1163 if (ipn_protocol_table
[protocol
]->refcnt
== 0) {
1164 ipn_protocol_table
[protocol
]=NULL
;
1165 printk(KERN_INFO
"IPN-VDE: Unregistered protocol %d\n",protocol
+1);
1172 up(&ipn_glob_mutex
);
1177 /* Module constructor/destructor */
1178 static struct net_proto_family ipn_family_ops
= {
1180 .create
= ipn_create
,
1181 .owner
= THIS_MODULE
,
1184 static int ipn_init(void)
1188 ipn_init_protocol(&ipn_bcast
);
1189 ipn_node_cache
=kmem_cache_create("ipn_node",sizeof(struct ipn_node
),0,0,NULL
,NULL
);
1190 if (!ipn_node_cache
) {
1191 printk(KERN_CRIT
"%s: Cannot create ipn_node SLAB cache!\n",
1197 ipn_msgitem_cache
=kmem_cache_create("ipn_msgitem",sizeof(struct msgitem
),0,0,NULL
,NULL
);
1198 if (!ipn_msgitem_cache
) {
1199 kmem_cache_destroy(ipn_node_cache
);
1200 printk(KERN_CRIT
"%s: Cannot create ipn_msgitem SLAB cache!\n",
1206 if ((rc
=ipn_hash_init()) < 0) {
1207 kmem_cache_destroy(ipn_node_cache
);
1208 kmem_cache_destroy(ipn_msgitem_cache
);
1209 printk(KERN_CRIT
"%s: Cannot startup hash table management!\n",
1214 rc
=proto_register(&ipn_proto
,1);
1216 printk(KERN_CRIT
"%s: Cannot create ipn_node SLAB cache!\n",
1221 sock_register(&ipn_family_ops
);
1223 printk(KERN_INFO
"IPN-VDE: Virtual Square Project, University of Bologna (c) 2007\n");
1228 static void ipn_exit(void)
1230 cleanup_persistent_nodes();
1233 if (ipn_msgitem_cache
)
1234 kmem_cache_destroy(ipn_msgitem_cache
);
1236 kmem_cache_destroy(ipn_node_cache
);
1237 sock_unregister(PF_IPN
);
1238 proto_unregister(&ipn_proto
);
1239 printk(KERN_INFO
"IPN-VDE removed\n");
1242 module_init(ipn_init
);
1243 module_exit(ipn_exit
);
1245 EXPORT_SYMBOL_GPL(ipn_proto_register
);
1246 EXPORT_SYMBOL_GPL(ipn_proto_deregister
);
1247 EXPORT_SYMBOL_GPL(ipn_proto_sendmsg
);
1248 EXPORT_SYMBOL_GPL(ipn_msgpool_alloc
);
1249 EXPORT_SYMBOL_GPL(ipn_msgpool_put
);