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/poll.h>
26 #include <linux/list.h>
27 #include <linux/mount.h>
28 #include <linux/version.h>
31 #include <net/af_ipn.h>
34 #include "ipn_netdev.h"
36 MODULE_LICENSE("GPL");
37 MODULE_AUTHOR("VIEW-OS TEAM");
38 MODULE_DESCRIPTION("IPN Kernel Module");
40 #define IPN_MAX_PROTO 4
42 /*extension of RCV_SHUTDOWN defined in include/net/sock.h
43 * when the bit is set recv fails */
44 /* NO_OOB: do not send OOB */
45 #define RCV_SHUTDOWN_NO_OOB 4
46 /* EXTENDED MASK including OOB */
47 #define SHUTDOWN_XMASK (SHUTDOWN_MASK | RCV_SHUTDOWN_NO_OOB)
48 /* if XRCV_SHUTDOWN is all set recv fails */
49 #define XRCV_SHUTDOWN (RCV_SHUTDOWN | RCV_SHUTDOWN_NO_OOB)
51 /* Network table and hash */
52 struct hlist_head ipn_network_table
[IPN_HASH_SIZE
+ 1];
53 DEFINE_SPINLOCK(ipn_table_lock
);
54 static struct kmem_cache
*ipn_network_cache
;
55 static struct kmem_cache
*ipn_node_cache
;
56 static struct kmem_cache
*ipn_msgitem_cache
;
57 static DECLARE_MUTEX(ipn_glob_mutex
);
59 /* Protocol 1: HUB/Broadcast default protocol. Function Prototypes */
60 static int ipn_bcast_newport(struct ipn_node
*newport
);
61 static int ipn_bcast_handlemsg(struct ipn_node
*from
,
62 struct msgpool_item
*msgitem
);
64 /* default protocol IPN_BROADCAST (0) */
65 static struct ipn_protocol ipn_bcast
= {
67 .ipn_p_newport
=ipn_bcast_newport
,
68 .ipn_p_handlemsg
=ipn_bcast_handlemsg
};
70 static struct ipn_protocol
*ipn_protocol_table
[IPN_MAX_PROTO
]={&ipn_bcast
};
72 /* Socket call function prototypes */
73 static int ipn_release(struct socket
*);
74 static int ipn_bind(struct socket
*, struct sockaddr
*, int);
75 static int ipn_connect(struct socket
*, struct sockaddr
*,
76 int addr_len
, int flags
);
77 static int ipn_getname(struct socket
*, struct sockaddr
*, int *, int);
78 static unsigned int ipn_poll(struct file
*, struct socket
*, poll_table
*);
79 static int ipn_ioctl(struct socket
*, unsigned int, unsigned long);
80 static int ipn_shutdown(struct socket
*, int);
81 static int ipn_sendmsg(struct kiocb
*, struct socket
*,
82 struct msghdr
*, size_t);
83 static int ipn_recvmsg(struct kiocb
*, struct socket
*,
84 struct msghdr
*, size_t, int);
85 static int ipn_setsockopt(struct socket
*sock
, int level
, int optname
,
86 char __user
*optval
, int optlen
);
87 static int ipn_getsockopt(struct socket
*sock
, int level
, int optname
,
88 char __user
*optval
, int __user
*optlen
);
90 /* Network table Management
91 * inode->ipn_network hash table */
92 static inline void ipn_insert_network(struct hlist_head
*list
, struct ipn_network
*ipnn
)
94 spin_lock(&ipn_table_lock
);
95 hlist_add_head(&ipnn
->hnode
, list
);
96 spin_unlock(&ipn_table_lock
);
99 static inline void ipn_remove_network(struct ipn_network
*ipnn
)
101 spin_lock(&ipn_table_lock
);
102 hlist_del(&ipnn
->hnode
);
103 spin_unlock(&ipn_table_lock
);
106 static struct ipn_network
*ipn_find_network_byinode(struct inode
*i
)
108 struct ipn_network
*ipnn
;
109 struct hlist_node
*node
;
111 spin_lock(&ipn_table_lock
);
112 hlist_for_each_entry(ipnn
, node
,
113 &ipn_network_table
[i
->i_ino
& (IPN_HASH_SIZE
- 1)], hnode
) {
114 struct dentry
*dentry
= ipnn
->dentry
;
116 if(atomic_read(&ipnn
->refcnt
) > 0 && dentry
&& dentry
->d_inode
== i
)
121 spin_unlock(&ipn_table_lock
);
125 /* msgpool management
126 * msgpool_item are ipn_network dependent (each net has its own MTU)
127 * for each message sent there is one msgpool_item and many struct msgitem
128 * one for each receipient.
129 * msgitem are connected to the node's msgqueue or oobmsgqueue.
130 * when a message is delivered to a process the msgitem is deleted and
131 * the count of the msgpool_item is decreased.
132 * msgpool_item elements gets deleted automatically when count is 0*/
135 struct list_head list
;
136 struct msgpool_item
*msg
;
139 /* alloc a fresh msgpool item. count is set to 1.
142 * for each receipient
143 * enqueue messages to the process (using msgitem), ipn_msgpool_hold
145 * The message can be delivered concurrently. init count to 1 guarantees
146 * that it survives at least until is has been enqueued to all
148 static struct msgpool_item
*_ipn_msgpool_alloc(struct ipn_network
*ipnn
)
150 struct msgpool_item
*new;
151 if ((new=kmem_cache_alloc(ipnn
->msgpool_cache
,GFP_KERNEL
)) != NULL
) {
152 atomic_set(&new->count
,1);
153 atomic_inc(&ipnn
->msgpool_nelem
);
158 struct msgpool_item
*ipn_msgpool_alloc(struct ipn_network
*ipnn
,int leaky
)
160 if (leaky
&& (ipnn
->flags
& IPN_FLAG_LOSSLESS
) &&
161 atomic_read(&ipnn
->msgpool_nelem
) < ipnn
->msgpool_size
)
164 return _ipn_msgpool_alloc(ipnn
);
167 /* If the service il LOSSLESS, this msgpool call waits for an
168 * available msgpool item */
169 static struct msgpool_item
*ipn_msgpool_alloc_locking(struct ipn_network
*ipnn
)
171 if (ipnn
->flags
& IPN_FLAG_LOSSLESS
) {
172 while (atomic_read(&ipnn
->msgpool_nelem
) >= ipnn
->msgpool_size
) {
173 if (wait_event_interruptible_exclusive(ipnn
->send_wait
,
174 atomic_read(&ipnn
->msgpool_nelem
) < ipnn
->msgpool_size
))
178 return _ipn_msgpool_alloc(ipnn
);
181 static inline void ipn_msgpool_hold(struct msgpool_item
*msg
)
183 atomic_inc(&msg
->count
);
186 /* decrease count and delete msgpool_item if count == 0 */
187 void ipn_msgpool_put(struct msgpool_item
*old
,
188 struct ipn_network
*ipnn
)
190 if (atomic_dec_and_test(&old
->count
)) {
191 kmem_cache_free(ipnn
->msgpool_cache
,old
);
192 atomic_dec(&ipnn
->msgpool_nelem
);
193 if (ipnn
->flags
& IPN_FLAG_LOSSLESS
) /* could be done anyway */
194 wake_up_interruptible(&ipnn
->send_wait
);
199 static const struct proto_ops ipn_ops
= {
201 .owner
= THIS_MODULE
,
202 .release
= ipn_release
,
204 .connect
= ipn_connect
,
205 .socketpair
= sock_no_socketpair
,
206 .accept
= sock_no_accept
,
207 .getname
= ipn_getname
,
210 .listen
= sock_no_listen
,
211 .shutdown
= ipn_shutdown
,
212 .setsockopt
= ipn_setsockopt
,
213 .getsockopt
= ipn_getsockopt
,
214 .sendmsg
= ipn_sendmsg
,
215 .recvmsg
= ipn_recvmsg
,
216 .mmap
= sock_no_mmap
,
217 .sendpage
= sock_no_sendpage
,
220 static struct proto ipn_proto
= {
222 .owner
= THIS_MODULE
,
223 .obj_size
= sizeof(struct ipn_sock
),
227 * ipn_node is a separate structure, pointed by ipn_sock -> node
228 * when a node is "persistent", ipn_node survives while ipn_sock gets released*/
229 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
230 static int ipn_create(struct socket
*sock
, int protocol
)
232 static int ipn_create(struct net
*net
,struct socket
*sock
, int protocol
)
235 struct ipn_sock
*ipn_sk
;
236 struct ipn_node
*ipn_node
;
238 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
241 if (net
!= &init_net
)
242 return -EAFNOSUPPORT
;
245 if (sock
->type
!= SOCK_RAW
)
250 protocol
=IPN_BROADCAST
-1;
251 if (protocol
< 0 || protocol
>= IPN_MAX_PROTO
||
252 ipn_protocol_table
[protocol
] == NULL
)
253 return -EPROTONOSUPPORT
;
254 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
255 ipn_sk
= (struct ipn_sock
*) sk_alloc(PF_IPN
, GFP_KERNEL
, &ipn_proto
, 1);
257 ipn_sk
= (struct ipn_sock
*) sk_alloc(net
, PF_IPN
, GFP_KERNEL
, &ipn_proto
);
262 ipn_sk
->node
=ipn_node
=kmem_cache_alloc(ipn_node_cache
,GFP_KERNEL
);
264 sock_put((struct sock
*) ipn_sk
);
267 sock_init_data(sock
,(struct sock
*) ipn_sk
);
268 sock
->state
= SS_UNCONNECTED
;
269 sock
->ops
= &ipn_ops
;
270 sock
->sk
=(struct sock
*)ipn_sk
;
271 INIT_LIST_HEAD(&ipn_node
->nodelist
);
272 ipn_node
->protocol
=protocol
;
273 ipn_node
->flags
=IPN_NODEFLAG_INUSE
;
274 ipn_node
->shutdown
=RCV_SHUTDOWN_NO_OOB
;
275 ipn_node
->descr
[0]=0;
276 ipn_node
->portno
=IPN_PORTNO_ANY
;
279 ipn_node
->proto_private
=NULL
;
280 ipn_node
->totmsgcount
=0;
281 ipn_node
->oobmsgcount
=0;
282 spin_lock_init(&ipn_node
->msglock
);
283 INIT_LIST_HEAD(&ipn_node
->msgqueue
);
284 INIT_LIST_HEAD(&ipn_node
->oobmsgqueue
);
286 init_waitqueue_head(&ipn_node
->read_wait
);
291 /* update # of readers and # of writers counters for an ipn network.
292 * This function sends oob messages to nodes requesting the service */
293 static void ipn_net_update_counters(struct ipn_network
*ipnn
,
294 int chg_readers
, int chg_writers
) {
295 ipnn
->numreaders
+= chg_readers
;
296 ipnn
->numwriters
+= chg_writers
;
297 if (ipnn
->mtu
>= sizeof(struct numnode_oob
))
299 struct msgpool_item
*ipn_msg
=_ipn_msgpool_alloc(ipnn
);
301 struct numnode_oob
*oob_msg
=(struct numnode_oob
*)(ipn_msg
->data
);
302 struct ipn_node
*ipn_node
;
303 ipn_msg
->len
=sizeof(struct numnode_oob
);
304 oob_msg
->level
=IPN_ANY
;
305 oob_msg
->tag
=IPN_OOB_NUMNODE_TAG
;
306 oob_msg
->numreaders
=ipnn
->numreaders
;
307 oob_msg
->numwriters
=ipnn
->numwriters
;
308 list_for_each_entry(ipn_node
, &ipnn
->connectqueue
, nodelist
) {
309 if (ipn_node
->flags
& IPN_NODEFLAG_OOB_NUMNODES
)
310 ipn_proto_oobsendmsg(ipn_node
,ipn_msg
);
312 ipn_msgpool_put(ipn_msg
,ipnn
);
317 /* flush pending messages (for close and shutdown RCV) */
318 static void ipn_flush_recvqueue(struct ipn_node
*ipn_node
)
320 struct ipn_network
*ipnn
=ipn_node
->ipn
;
321 spin_lock(&ipn_node
->msglock
);
322 while (!list_empty(&ipn_node
->msgqueue
)) {
323 struct msgitem
*msgitem
=
324 list_first_entry(&ipn_node
->msgqueue
, struct msgitem
, list
);
325 list_del(&msgitem
->list
);
326 ipn_node
->totmsgcount
--;
327 ipn_msgpool_put(msgitem
->msg
,ipnn
);
328 kmem_cache_free(ipn_msgitem_cache
,msgitem
);
330 spin_unlock(&ipn_node
->msglock
);
333 /* flush pending oob messages (for socket close) */
334 static void ipn_flush_oobrecvqueue(struct ipn_node
*ipn_node
)
336 struct ipn_network
*ipnn
=ipn_node
->ipn
;
337 spin_lock(&ipn_node
->msglock
);
338 while (!list_empty(&ipn_node
->oobmsgqueue
)) {
339 struct msgitem
*msgitem
=
340 list_first_entry(&ipn_node
->oobmsgqueue
, struct msgitem
, list
);
341 list_del(&msgitem
->list
);
342 ipn_node
->totmsgcount
--;
343 ipn_node
->oobmsgcount
--;
344 ipn_msgpool_put(msgitem
->msg
,ipnn
);
345 kmem_cache_free(ipn_msgitem_cache
,msgitem
);
347 spin_unlock(&ipn_node
->msglock
);
350 /* Terminate node. The node is "logically" terminated. */
351 static int ipn_terminate_node(struct ipn_node
*ipn_node
)
353 struct ipn_network
*ipnn
=ipn_node
->ipn
;
355 if (down_interruptible(&ipnn
->ipnn_mutex
))
357 if (ipn_node
->portno
>= 0) {
358 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_predelport(ipn_node
);
359 ipnn
->connport
[ipn_node
->portno
]=NULL
;
361 list_del(&ipn_node
->nodelist
);
362 ipn_flush_recvqueue(ipn_node
);
363 ipn_flush_oobrecvqueue(ipn_node
);
364 if (ipn_node
->portno
>= 0)
365 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_delport(ipn_node
);
367 ipn_net_update_counters(ipnn
,
368 (ipn_node
->shutdown
& RCV_SHUTDOWN
)?0:-1,
369 (ipn_node
->shutdown
& SEND_SHUTDOWN
)?0:-1);
370 up(&ipnn
->ipnn_mutex
);
372 ipn_netdev_close(ipn_node
);
373 /* No more network elements */
374 if (atomic_dec_and_test(&ipnn
->refcnt
))
376 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_delnet(ipnn
);
377 ipn_remove_network(ipnn
);
378 ipn_protocol_table
[ipnn
->protocol
]->refcnt
--;
383 module_put(THIS_MODULE
);
384 if (ipnn
->msgpool_cache
)
385 kmem_cache_destroy(ipnn
->msgpool_cache
);
387 kfree(ipnn
->connport
);
388 kmem_cache_free(ipn_network_cache
, ipnn
);
392 kfree(ipn_node
->pbp
);
395 ipn_node
->shutdown
= SHUTDOWN_XMASK
;
399 /* release of a socket */
400 static int ipn_release (struct socket
*sock
)
402 struct ipn_sock
*ipn_sk
=(struct ipn_sock
*)sock
->sk
;
403 struct ipn_node
*ipn_node
=ipn_sk
->node
;
405 if (down_interruptible(&ipn_glob_mutex
))
407 if (ipn_node
->flags
& IPN_NODEFLAG_PERSIST
) {
408 ipn_node
->flags
&= ~IPN_NODEFLAG_INUSE
;
412 rv
=ipn_terminate_node(ipn_node
);
416 kmem_cache_free(ipn_node_cache
,ipn_node
);
420 sock_put((struct sock
*) ipn_sk
);
424 /* _set persist, change the persistence of a node,
425 * when persistence gets cleared and the node is no longer used
426 * the node is terminated and freed.
427 * ipn_glob_mutex must be locked */
428 static int _ipn_setpersist(struct ipn_node
*ipn_node
, int persist
)
432 ipn_node
->flags
|= IPN_NODEFLAG_PERSIST
;
434 ipn_node
->flags
&= ~IPN_NODEFLAG_PERSIST
;
435 if (!(ipn_node
->flags
& IPN_NODEFLAG_INUSE
)) {
436 rv
=ipn_terminate_node(ipn_node
);
438 kmem_cache_free(ipn_node_cache
,ipn_node
);
445 * lock ipn_glob_mutex and call __ipn_setpersist above */
446 static int ipn_setpersist(struct ipn_node
*ipn_node
, int persist
)
449 if (ipn_node
->dev
== NULL
)
451 if (down_interruptible(&ipn_glob_mutex
))
453 rv
=_ipn_setpersist(ipn_node
,persist
);
458 /* several network parameters can be set by setsockopt prior to bind */
459 /* struct pre_bind_parms is a temporary stucture connected to ipn_node->pbp
460 * to keep the parameter values. */
461 struct pre_bind_parms
{
462 unsigned short maxports
;
463 unsigned short flags
;
464 unsigned short msgpoolsize
;
469 /* STD_PARMS: BITS_PER_LONG nodes, no flags, BITS_PER_BYTE pending msgs,
470 * Ethernet + VLAN MTU*/
471 #define STD_BIND_PARMS {BITS_PER_LONG, 0, BITS_PER_BYTE, 1514, 0x777};
473 static int ipn_mkname(struct sockaddr_un
* sunaddr
, int len
)
475 if (len
<= sizeof(short) || len
> sizeof(*sunaddr
))
477 if (!sunaddr
|| sunaddr
->sun_family
!= AF_IPN
)
480 * This may look like an off by one error but it is a bit more
481 * subtle. 108 is the longest valid AF_IPN path for a binding.
482 * sun_path[108] doesnt as such exist. However in kernel space
483 * we are guaranteed that it is a valid memory location in our
484 * kernel address buffer.
486 ((char *)sunaddr
)[len
]=0;
487 len
= strlen(sunaddr
->sun_path
)+1+sizeof(short);
493 static int ipn_bind(struct socket
*sock
, struct sockaddr
*uaddr
, int addr_len
)
495 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)uaddr
;
496 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
498 struct ipn_network
*ipnn
;
499 struct dentry
* dentry
= NULL
;
501 struct pre_bind_parms parms
=STD_BIND_PARMS
;
503 //printk("IPN bind\n");
505 if (down_interruptible(&ipn_glob_mutex
))
507 if (sock
->state
!= SS_UNCONNECTED
||
508 ipn_node
->ipn
!= NULL
) {
513 if (ipn_node
->protocol
>= 0 &&
514 (ipn_node
->protocol
>= IPN_MAX_PROTO
||
515 ipn_protocol_table
[ipn_node
->protocol
] == NULL
)) {
516 err
= -EPROTONOSUPPORT
;
520 addr_len
= ipn_mkname(sunaddr
, addr_len
);
526 /* check if there is already a socket with that name */
527 err
= path_lookup(sunaddr
->sun_path
, LOOKUP_FOLLOW
, &nd
);
528 if (err
) { /* it does not exist, NEW IPN socket! */
530 /* Is it everything okay with the parent? */
531 err
= path_lookup(sunaddr
->sun_path
, LOOKUP_PARENT
, &nd
);
533 goto out_mknod_parent
;
534 /* Do I have the permission to create a file? */
535 dentry
= lookup_create(&nd
, 0);
536 err
= PTR_ERR(dentry
);
538 goto out_mknod_unlock
;
540 * All right, let's create it.
543 mode
= ipn_node
->pbp
->mode
;
545 mode
= SOCK_INODE(sock
)->i_mode
;
546 mode
= S_IFSOCK
| (mode
& ~current
->fs
->umask
);
548 err
= vfs_mknod(nd
.dentry
->d_inode
, dentry
, nd
.mnt
, mode
, 0);
550 err
= vfs_mknod(nd
.dentry
->d_inode
, dentry
, mode
, 0);
554 mutex_unlock(&nd
.dentry
->d_inode
->i_mutex
);
557 /* create a new ipn_network item */
559 parms
=*ipn_node
->pbp
;
560 ipnn
=kmem_cache_zalloc(ipn_network_cache
,GFP_KERNEL
);
563 goto out_mknod_dput_ipnn
;
565 ipnn
->connport
=kzalloc(parms
.maxports
* sizeof(struct ipn_node
*),GFP_KERNEL
);
566 if (!ipnn
->connport
) {
568 goto out_mknod_dput_ipnn2
;
571 /* module refcnt is incremented for each network, thus
572 * rmmod is forbidden if there are persistent node */
573 if (!try_module_get(THIS_MODULE
)) {
575 goto out_mknod_dput_ipnn2
;
577 memcpy(&ipnn
->sunaddr
,sunaddr
,addr_len
);
579 ipnn
->msgpool_cache
=kmem_cache_create(ipnn
->sunaddr
.sun_path
,sizeof(struct msgpool_item
)+ipnn
->mtu
,0,0,NULL
);
580 if (!ipnn
->msgpool_cache
) {
582 goto out_mknod_dput_putmodule
;
584 INIT_LIST_HEAD(&ipnn
->unconnectqueue
);
585 INIT_LIST_HEAD(&ipnn
->connectqueue
);
586 atomic_set(&ipnn
->refcnt
,1);
587 ipnn
->dentry
=nd
.dentry
;
589 init_MUTEX(&ipnn
->ipnn_mutex
);
590 ipnn
->sunaddr_len
=addr_len
;
591 ipnn
->protocol
=ipn_node
->protocol
;
592 if (ipnn
->protocol
< 0) ipnn
->protocol
= 0;
593 ipn_protocol_table
[ipnn
->protocol
]->refcnt
++;
594 ipnn
->flags
=parms
.flags
;
597 ipnn
->maxports
=parms
.maxports
;
598 atomic_set(&ipnn
->msgpool_nelem
,0);
599 ipnn
->msgpool_size
=parms
.msgpoolsize
;
600 ipnn
->proto_private
=NULL
;
601 init_waitqueue_head(&ipnn
->send_wait
);
602 err
=ipn_protocol_table
[ipnn
->protocol
]->ipn_p_newnet(ipnn
);
604 goto out_mknod_dput_putmodule
;
605 ipn_insert_network(&ipn_network_table
[nd
.dentry
->d_inode
->i_ino
& (IPN_HASH_SIZE
-1)],ipnn
);
607 /* join an existing network */
608 if (parms
.flags
& IPN_FLAG_EXCL
) {
612 err
= vfs_permission(&nd
, MAY_EXEC
);
616 if (!S_ISSOCK(nd
.dentry
->d_inode
->i_mode
))
618 ipnn
=ipn_find_network_byinode(nd
.dentry
->d_inode
);
619 if (!ipnn
|| (ipnn
->flags
& IPN_FLAG_TERMINATED
) ||
620 (ipnn
->flags
& IPN_FLAG_EXCL
))
622 list_add_tail(&ipn_node
->nodelist
,&ipnn
->unconnectqueue
);
623 atomic_inc(&ipnn
->refcnt
);
626 kfree(ipn_node
->pbp
);
630 ipn_node
->flags
|= IPN_NODEFLAG_BOUND
;
640 out_mknod_dput_putmodule
:
641 module_put(THIS_MODULE
);
642 out_mknod_dput_ipnn2
:
643 kfree(ipnn
->connport
);
645 kmem_cache_free(ipn_network_cache
,ipnn
);
649 mutex_unlock(&nd
.dentry
->d_inode
->i_mutex
);
659 static int ipn_connect(struct socket
*sock
, struct sockaddr
*addr
,
660 int addr_len
, int flags
){
661 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)addr
;
662 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
664 struct ipn_network
*ipnn
,*previousipnn
;
668 /* the socket cannot be connected twice */
669 if (sock
->state
!= SS_UNCONNECTED
)
672 if (down_interruptible(&ipn_glob_mutex
))
675 if ((previousipnn
=ipn_node
->ipn
) == NULL
) { /* unbound */
676 unsigned char mustshutdown
=0;
677 err
= ipn_mkname(sunaddr
, addr_len
);
681 err
= path_lookup(sunaddr
->sun_path
, LOOKUP_FOLLOW
, &nd
);
684 err
= vfs_permission(&nd
, MAY_READ
);
686 if (err
== -EACCES
|| err
== -EROFS
)
687 mustshutdown
|=RCV_SHUTDOWN
;
691 err
= vfs_permission(&nd
, MAY_WRITE
);
694 mustshutdown
|=SEND_SHUTDOWN
;
698 mustshutdown
|= ipn_node
->shutdown
;
699 /* if the combination of shutdown and permissions leaves
700 * no abilities, connect returns EACCES */
701 if (mustshutdown
== SHUTDOWN_XMASK
) {
706 ipn_node
->shutdown
=mustshutdown
;
708 if (!S_ISSOCK(nd
.dentry
->d_inode
->i_mode
)) {
712 ipnn
=ipn_find_network_byinode(nd
.dentry
->d_inode
);
713 if (!ipnn
|| (ipnn
->flags
& IPN_FLAG_TERMINATED
)) {
717 if (ipn_node
->protocol
== IPN_ANY
)
718 ipn_node
->protocol
=ipnn
->protocol
;
719 else if (ipnn
->protocol
!= ipn_node
->protocol
) {
728 if (down_interruptible(&ipnn
->ipnn_mutex
)) {
732 portno
= ipn_protocol_table
[ipnn
->protocol
]->ipn_p_newport(ipn_node
);
733 if (portno
>= 0 && portno
<ipnn
->maxports
) {
734 sock
->state
= SS_CONNECTED
;
735 ipn_node
->portno
=portno
;
736 ipnn
->connport
[portno
]=ipn_node
;
737 if (!(ipn_node
->flags
& IPN_NODEFLAG_BOUND
)) {
738 atomic_inc(&ipnn
->refcnt
);
739 list_del(&ipn_node
->nodelist
);
741 list_add_tail(&ipn_node
->nodelist
,&ipnn
->connectqueue
);
742 ipn_net_update_counters(ipnn
,
743 (ipn_node
->shutdown
& RCV_SHUTDOWN
)?0:1,
744 (ipn_node
->shutdown
& SEND_SHUTDOWN
)?0:1);
746 ipn_node
->ipn
=previousipnn
; /* undo changes on ipn_node->ipn */
749 up(&ipnn
->ipnn_mutex
);
760 static int ipn_getname(struct socket
*sock
, struct sockaddr
*uaddr
,
761 int *uaddr_len
, int peer
) {
762 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
763 struct ipn_network
*ipnn
=ipn_node
->ipn
;
764 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)uaddr
;
767 if (down_interruptible(&ipn_glob_mutex
))
770 *uaddr_len
= ipnn
->sunaddr_len
;
771 memcpy(sunaddr
,&ipnn
->sunaddr
,*uaddr_len
);
779 static unsigned int ipn_poll(struct file
*file
, struct socket
*sock
,
781 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
782 struct ipn_network
*ipnn
=ipn_node
->ipn
;
786 poll_wait(file
,&ipn_node
->read_wait
,wait
);
787 if (ipnn
->flags
& IPN_FLAG_LOSSLESS
)
788 poll_wait(file
,&ipnn
->send_wait
,wait
);
789 /* POLLIN if recv succeeds,
790 * POLL{PRI,RDNORM} if there are {oob,non-oob} messages */
791 if (ipn_node
->totmsgcount
> 0) mask
|= POLLIN
;
792 if (!(list_empty(&ipn_node
->msgqueue
))) mask
|= POLLRDNORM
;
793 if (!(list_empty(&ipn_node
->oobmsgqueue
))) mask
|= POLLPRI
;
794 if ((!(ipnn
->flags
& IPN_FLAG_LOSSLESS
)) |
795 (atomic_read(&ipnn
->msgpool_nelem
) < ipnn
->msgpool_size
))
796 mask
|= POLLOUT
| POLLWRNORM
;
801 /* connect netdev (from ioctl). connect a bound socket to a
802 * network device TAP or GRAB */
803 static int ipn_connect_netdev(struct socket
*sock
,struct ifreq
*ifr
)
806 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
807 struct ipn_network
*ipnn
=ipn_node
->ipn
;
808 if (!capable(CAP_NET_ADMIN
))
810 if (sock
->state
!= SS_UNCONNECTED
)
813 return -ENOTCONN
; /* Maybe we need a different error for "NOT BOUND" */
814 if (down_interruptible(&ipn_glob_mutex
))
816 if (down_interruptible(&ipnn
->ipnn_mutex
)) {
820 ipn_node
->dev
=ipn_netdev_alloc(ipn_node
->net
,ifr
->ifr_flags
,ifr
->ifr_name
,&err
);
823 portno
= ipn_protocol_table
[ipnn
->protocol
]->ipn_p_newport(ipn_node
);
824 if (portno
>= 0 && portno
<ipnn
->maxports
) {
825 sock
->state
= SS_CONNECTED
;
826 ipn_node
->portno
=portno
;
827 ipn_node
->flags
|= ifr
->ifr_flags
& IPN_NODEFLAG_DEVMASK
;
828 ipnn
->connport
[portno
]=ipn_node
;
829 err
=ipn_netdev_activate(ipn_node
);
831 sock
->state
= SS_UNCONNECTED
;
832 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_delport(ipn_node
);
834 ipn_node
->portno
= -1;
835 ipn_node
->flags
&= ~IPN_NODEFLAG_DEVMASK
;
836 ipnn
->connport
[portno
]=NULL
;
838 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_postnewport(ipn_node
);
839 list_del(&ipn_node
->nodelist
);
840 list_add_tail(&ipn_node
->nodelist
,&ipnn
->connectqueue
);
843 ipn_netdev_close(ipn_node
);
849 up(&ipnn
->ipnn_mutex
);
854 /* join a netdev, a socket gets connected to a persistent node
855 * not connected to another socket */
856 static int ipn_join_netdev(struct socket
*sock
,struct ifreq
*ifr
)
859 struct net_device
*dev
;
860 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
861 struct ipn_node
*ipn_joined
;
862 struct ipn_network
*ipnn
=ipn_node
->ipn
;
863 if (sock
->state
!= SS_UNCONNECTED
)
865 if (down_interruptible(&ipn_glob_mutex
))
867 if (down_interruptible(&ipnn
->ipnn_mutex
)) {
871 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
872 dev
=__dev_get_by_name(ifr
->ifr_name
);
874 dev
=__dev_get_by_name(ipn_node
->net
,ifr
->ifr_name
);
877 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
878 dev
=__dev_get_by_index(ifr
->ifr_ifindex
);
880 dev
=__dev_get_by_index(ipn_node
->net
,ifr
->ifr_ifindex
);
882 if (dev
&& (ipn_joined
=ipn_netdev2node(dev
)) != NULL
) { /* the interface does exist */
884 for (i
=0;i
<ipnn
->maxports
&& ipn_joined
!= ipnn
->connport
[i
] ;i
++)
886 if (i
< ipnn
->maxports
) { /* found */
887 /* ipn_joined is substituted to ipn_node */
888 ((struct ipn_sock
*)sock
->sk
)->node
=ipn_joined
;
889 ipn_joined
->flags
|= IPN_NODEFLAG_INUSE
;
890 atomic_dec(&ipnn
->refcnt
);
891 kmem_cache_free(ipn_node_cache
,ipn_node
);
896 up(&ipnn
->ipnn_mutex
);
901 /* set persistence of a node looking for it by interface name
902 * (it is for sysadm, to close network interfaces)*/
903 static int ipn_setpersist_netdev(struct ifreq
*ifr
, int value
)
905 struct net_device
*dev
;
906 struct ipn_node
*ipn_node
;
908 if (!capable(CAP_NET_ADMIN
))
910 if (down_interruptible(&ipn_glob_mutex
))
912 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
913 dev
=__dev_get_by_name(ifr
->ifr_name
);
915 dev
=__dev_get_by_name(&init_net
,ifr
->ifr_name
);
918 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
919 dev
=__dev_get_by_index(ifr
->ifr_ifindex
);
921 dev
=__dev_get_by_index(&init_net
,ifr
->ifr_ifindex
);
923 if (dev
&& (ipn_node
=ipn_netdev2node(dev
)) != NULL
)
924 _ipn_setpersist(ipn_node
,value
);
932 static int ipn_ioctl(struct socket
*sock
, unsigned int cmd
, unsigned long arg
) {
933 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
934 struct ipn_network
*ipnn
=ipn_node
->ipn
;
935 void __user
* argp
= (void __user
*)arg
;
938 if (ipn_node
->shutdown
== SHUTDOWN_XMASK
)
945 case IPN_SETPERSIST_NETDEV
:
946 case IPN_CLRPERSIST_NETDEV
:
947 case IPN_CONN_NETDEV
:
948 case IPN_JOIN_NETDEV
:
950 if (copy_from_user(&ifr
, argp
, sizeof ifr
))
952 ifr
.ifr_name
[IFNAMSIZ
-1] = '\0';
955 /* actions for unconnected and unbound sockets */
957 case IPN_SETPERSIST_NETDEV
:
958 return ipn_setpersist_netdev(&ifr
,1);
959 case IPN_CLRPERSIST_NETDEV
:
960 return ipn_setpersist_netdev(&ifr
,0);
962 if (capable(CAP_NET_ADMIN
))
964 if (ipn_node
->dev
&& (ipn_node
->flags
&IPN_NODEFLAG_TAP
))
965 return dev_set_mac_address(ipn_node
->dev
, &ifr
.ifr_hwaddr
);
967 return -EADDRNOTAVAIL
;
969 if (ipnn
== NULL
|| (ipnn
->flags
& IPN_FLAG_TERMINATED
))
971 /* actions for connected or bound sockets */
973 case IPN_CONN_NETDEV
:
974 return ipn_connect_netdev(sock
,&ifr
);
975 case IPN_JOIN_NETDEV
:
976 return ipn_join_netdev(sock
,&ifr
);
978 return ipn_setpersist(ipn_node
,arg
);
982 if (down_interruptible(&ipnn
->ipnn_mutex
))
984 rv
=ipn_protocol_table
[ipn_node
->protocol
]->ipn_p_ioctl(ipn_node
,cmd
,arg
);
985 up(&ipnn
->ipnn_mutex
);
992 /* shutdown: close socket for input or for output.
993 * shutdown can be called prior to connect and it is not reversible */
994 static int ipn_shutdown(struct socket
*sock
, int mode
) {
995 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
996 struct ipn_network
*ipnn
=ipn_node
->ipn
;
997 int oldshutdown
=ipn_node
->shutdown
;
998 mode
= (mode
+1)&(RCV_SHUTDOWN
|SEND_SHUTDOWN
);
1000 ipn_node
->shutdown
|= mode
;
1003 if (down_interruptible(&ipnn
->ipnn_mutex
)) {
1004 ipn_node
->shutdown
= oldshutdown
;
1005 return -ERESTARTSYS
;
1007 oldshutdown
=ipn_node
->shutdown
-oldshutdown
;
1008 if (sock
->state
== SS_CONNECTED
&& oldshutdown
) {
1009 ipn_net_update_counters(ipnn
,
1010 (ipn_node
->shutdown
& RCV_SHUTDOWN
)?0:-1,
1011 (ipn_node
->shutdown
& SEND_SHUTDOWN
)?0:-1);
1014 /* if recv channel has been shut down, flush the recv queue */
1015 if ((ipn_node
->shutdown
& RCV_SHUTDOWN
))
1016 ipn_flush_recvqueue(ipn_node
);
1017 up(&ipnn
->ipnn_mutex
);
1022 /* injectmsg: a new message is entering the ipn network.
1023 * injectmsg gets called by send and by the grab/tap node */
1024 int ipn_proto_injectmsg(struct ipn_node
*from
, struct msgpool_item
*msg
)
1026 struct ipn_network
*ipnn
=from
->ipn
;
1028 if (down_interruptible(&ipnn
->ipnn_mutex
))
1031 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_handlemsg(from
, msg
);
1032 up(&ipnn
->ipnn_mutex
);
1038 static int ipn_sendmsg(struct kiocb
*kiocb
, struct socket
*sock
,
1039 struct msghdr
*msg
, size_t len
) {
1040 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1041 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1042 struct msgpool_item
*newmsg
;
1045 if (unlikely(sock
->state
!= SS_CONNECTED
))
1047 if (unlikely(ipn_node
->shutdown
& SEND_SHUTDOWN
)) {
1048 if (ipn_node
->shutdown
== SHUTDOWN_XMASK
)
1053 if (len
> ipnn
->mtu
)
1055 newmsg
=ipn_msgpool_alloc_locking(ipnn
);
1059 err
=memcpy_fromiovec(newmsg
->data
, msg
->msg_iov
, len
);
1061 ipn_proto_injectmsg(ipn_node
, newmsg
);
1062 ipn_msgpool_put(newmsg
,ipnn
);
1066 /* enqueue an oob message. "to" is the destination */
1067 void ipn_proto_oobsendmsg(struct ipn_node
*to
, struct msgpool_item
*msg
)
1070 if (!to
->dev
) { /* no oob to netdev */
1071 struct msgitem
*msgitem
;
1072 struct ipn_network
*ipnn
=to
->ipn
;
1073 spin_lock(&to
->msglock
);
1074 if ((to
->shutdown
& RCV_SHUTDOWN_NO_OOB
) == 0 &&
1075 (ipnn
->flags
& IPN_FLAG_LOSSLESS
||
1076 to
->oobmsgcount
< ipnn
->msgpool_size
)) {
1077 if ((msgitem
=kmem_cache_alloc(ipn_msgitem_cache
,GFP_KERNEL
))!=NULL
) {
1081 list_add_tail(&msgitem
->list
, &to
->oobmsgqueue
);
1082 ipn_msgpool_hold(msg
);
1085 spin_unlock(&to
->msglock
);
1086 wake_up_interruptible(&to
->read_wait
);
1091 /* ipn_proto_sendmsg is called by protocol implementation to enqueue a
1092 * for a destination (to).*/
1093 void ipn_proto_sendmsg(struct ipn_node
*to
, struct msgpool_item
*msg
)
1097 ipn_netdev_sendmsg(to
,msg
);
1100 struct msgitem
*msgitem
;
1101 struct ipn_network
*ipnn
=to
->ipn
;
1102 spin_lock(&to
->msglock
);
1103 if ((ipnn
->flags
& IPN_FLAG_LOSSLESS
||
1104 to
->totmsgcount
< ipnn
->msgpool_size
) &&
1105 (to
->shutdown
& RCV_SHUTDOWN
)==0) {
1106 if ((msgitem
=kmem_cache_alloc(ipn_msgitem_cache
,GFP_KERNEL
))!=NULL
) {
1109 list_add_tail(&msgitem
->list
, &to
->msgqueue
);
1110 ipn_msgpool_hold(msg
);
1113 spin_unlock(&to
->msglock
);
1114 wake_up_interruptible(&to
->read_wait
);
1120 static int ipn_recvmsg(struct kiocb
*kiocb
, struct socket
*sock
,
1121 struct msghdr
*msg
, size_t len
, int flags
) {
1122 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1123 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1124 struct msgitem
*msgitem
;
1125 struct msgpool_item
*currmsg
;
1127 if (unlikely(sock
->state
!= SS_CONNECTED
))
1130 if (unlikely((ipn_node
->shutdown
& XRCV_SHUTDOWN
) == XRCV_SHUTDOWN
)) {
1131 if (ipn_node
->shutdown
== SHUTDOWN_XMASK
) /*EOF, nothing can be read*/
1134 return -EPIPE
; /*trying to read on a write only node */
1137 /* wait for a message */
1138 spin_lock(&ipn_node
->msglock
);
1139 while (ipn_node
->totmsgcount
== 0) {
1140 spin_unlock(&ipn_node
->msglock
);
1141 if (wait_event_interruptible(ipn_node
->read_wait
,
1142 !(ipn_node
->totmsgcount
== 0)))
1143 return -ERESTARTSYS
;
1144 spin_lock(&ipn_node
->msglock
);
1146 /* oob gets delivered first. oob are rare */
1147 if (likely(list_empty(&ipn_node
->oobmsgqueue
)))
1148 msgitem
=list_first_entry(&ipn_node
->msgqueue
, struct msgitem
, list
);
1150 msgitem
=list_first_entry(&ipn_node
->oobmsgqueue
, struct msgitem
, list
);
1151 msg
->msg_flags
|= MSG_OOB
;
1152 ipn_node
->oobmsgcount
--;
1154 list_del(&msgitem
->list
);
1155 ipn_node
->totmsgcount
--;
1156 spin_unlock(&ipn_node
->msglock
);
1157 currmsg
=msgitem
->msg
;
1158 if (currmsg
->len
< len
)
1160 memcpy_toiovec(msg
->msg_iov
, currmsg
->data
, len
);
1161 ipn_msgpool_put(currmsg
,ipnn
);
1162 kmem_cache_free(ipn_msgitem_cache
,msgitem
);
1167 /* resize a network: change the # of communication ports (connport) */
1168 static int ipn_netresize(struct ipn_network
*ipnn
,int newsize
)
1171 struct ipn_node
**newconnport
;
1172 struct ipn_node
**oldconnport
;
1174 if (down_interruptible(&ipnn
->ipnn_mutex
))
1175 return -ERESTARTSYS
;
1176 oldsize
=ipnn
->maxports
;
1177 if (newsize
== oldsize
) {
1178 up(&ipnn
->ipnn_mutex
);
1182 /* shrink a network. all the ports we are going to eliminate
1183 * must be unused! */
1184 if (newsize
< oldsize
) {
1186 for (i
=newsize
; i
<oldsize
; i
++)
1187 if (ipnn
->connport
[i
]) {
1188 up(&ipnn
->ipnn_mutex
);
1193 oldconnport
=ipnn
->connport
;
1194 /* allocate the new connport array and copy the old one */
1195 newconnport
=kzalloc(newsize
* sizeof(struct ipn_node
*),GFP_KERNEL
);
1197 up(&ipnn
->ipnn_mutex
);
1200 memcpy(newconnport
,oldconnport
,min
* sizeof(struct ipn_node
*));
1201 ipnn
->connport
=newconnport
;
1202 ipnn
->maxports
=newsize
;
1203 /* notify the protocol that the netowrk has been resized */
1204 err
=ipn_protocol_table
[ipnn
->protocol
]->ipn_p_resizenet(ipnn
,oldsize
,newsize
);
1206 /* roll back if the resize operation failed for the protocol */
1207 ipnn
->connport
=oldconnport
;
1208 ipnn
->maxports
=oldsize
;
1211 /* successful mission, network resized */
1213 up(&ipnn
->ipnn_mutex
);
1217 /* IPN SETSOCKOPT */
1218 static int ipn_setsockopt(struct socket
*sock
, int level
, int optname
,
1219 char __user
*optval
, int optlen
) {
1220 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1221 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1223 if (ipn_node
->shutdown
== SHUTDOWN_XMASK
)
1225 if (level
!= 0 && level
!= ipn_node
->protocol
+1)
1226 return -EPROTONOSUPPORT
;
1228 /* protocol specific sockopt */
1231 if (down_interruptible(&ipnn
->ipnn_mutex
))
1232 return -ERESTARTSYS
;
1233 rv
=ipn_protocol_table
[ipn_node
->protocol
]->ipn_p_setsockopt(ipn_node
,optname
,optval
,optlen
);
1234 up(&ipnn
->ipnn_mutex
);
1239 if (optname
== IPN_SO_DESCR
) {
1240 if (optlen
> IPN_DESCRLEN
)
1243 memset(ipn_node
->descr
,0,IPN_DESCRLEN
);
1244 if (copy_from_user(ipn_node
->descr
,optval
,optlen
))
1245 ipn_node
->descr
[0]=0;
1247 ipn_node
->descr
[optlen
-1]=0;
1251 if (optlen
< sizeof(int))
1253 else if ((optname
& IPN_SO_PREBIND
) && (ipnn
!= NULL
))
1257 get_user(val
, (int __user
*) optval
);
1258 if ((optname
& IPN_SO_PREBIND
) && !ipn_node
->pbp
) {
1259 struct pre_bind_parms std
=STD_BIND_PARMS
;
1260 ipn_node
->pbp
=kzalloc(sizeof(struct pre_bind_parms
),GFP_KERNEL
);
1263 *(ipn_node
->pbp
)=std
;
1267 if (sock
->state
== SS_UNCONNECTED
)
1268 ipn_node
->portno
=val
;
1272 case IPN_SO_CHANGE_NUMNODES
:
1273 if ((ipn_node
->flags
& IPN_NODEFLAG_BOUND
)!=0) {
1277 return ipn_netresize(ipnn
,val
);
1281 case IPN_SO_WANT_OOB_NUMNODES
:
1283 ipn_node
->flags
|= IPN_NODEFLAG_OOB_NUMNODES
;
1285 ipn_node
->flags
&= ~IPN_NODEFLAG_OOB_NUMNODES
;
1287 case IPN_SO_HANDLE_OOB
:
1289 ipn_node
->shutdown
&= ~RCV_SHUTDOWN_NO_OOB
;
1291 ipn_node
->shutdown
|= RCV_SHUTDOWN_NO_OOB
;
1297 ipn_node
->pbp
->mtu
=val
;
1299 case IPN_SO_NUMNODES
:
1303 ipn_node
->pbp
->maxports
=val
;
1305 case IPN_SO_MSGPOOLSIZE
:
1309 ipn_node
->pbp
->msgpoolsize
=val
;
1312 ipn_node
->pbp
->flags
=val
;
1315 ipn_node
->pbp
->mode
=val
;
1324 /* IPN GETSOCKOPT */
1325 static int ipn_getsockopt(struct socket
*sock
, int level
, int optname
,
1326 char __user
*optval
, int __user
*optlen
) {
1327 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1328 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1331 if (ipn_node
->shutdown
== SHUTDOWN_XMASK
)
1333 if (level
!= 0 && level
!= ipn_node
->protocol
+1)
1334 return -EPROTONOSUPPORT
;
1338 /* protocol specific sockopt */
1339 if (down_interruptible(&ipnn
->ipnn_mutex
))
1340 return -ERESTARTSYS
;
1341 rv
=ipn_protocol_table
[ipn_node
->protocol
]->ipn_p_getsockopt(ipn_node
,optname
,optval
,optlen
);
1342 up(&ipnn
->ipnn_mutex
);
1347 if (get_user(len
, optlen
))
1349 if (optname
== IPN_SO_DESCR
) {
1350 if (len
< IPN_DESCRLEN
)
1353 if (len
> IPN_DESCRLEN
)
1355 if(put_user(len
, optlen
))
1357 if(copy_to_user(optval
,ipn_node
->descr
,len
))
1365 val
=ipn_node
->portno
;
1370 else if (ipn_node
->pbp
)
1371 val
=ipn_node
->pbp
->mtu
;
1373 case IPN_SO_NUMNODES
:
1376 else if (ipn_node
->pbp
)
1377 val
=ipn_node
->pbp
->maxports
;
1379 case IPN_SO_MSGPOOLSIZE
:
1381 val
=ipnn
->msgpool_size
;
1382 else if (ipn_node
->pbp
)
1383 val
=ipn_node
->pbp
->msgpoolsize
;
1388 else if (ipn_node
->pbp
)
1389 val
=ipn_node
->pbp
->flags
;
1394 else if (ipn_node
->pbp
)
1395 val
=ipn_node
->pbp
->mode
;
1401 if (len
< sizeof(int))
1405 if(put_user(len
, optlen
))
1407 if(copy_to_user(optval
,&val
,len
))
1416 /* BROADCAST/HUB implementation */
1418 static int ipn_bcast_newport(struct ipn_node
*newport
) {
1419 struct ipn_network
*ipnn
=newport
->ipn
;
1421 for (i
=0;i
<ipnn
->maxports
;i
++) {
1422 if (ipnn
->connport
[i
] == NULL
)
1428 static int ipn_bcast_handlemsg(struct ipn_node
*from
,
1429 struct msgpool_item
*msgitem
){
1430 struct ipn_network
*ipnn
=from
->ipn
;
1432 struct ipn_node
*ipn_node
;
1433 list_for_each_entry(ipn_node
, &ipnn
->connectqueue
, nodelist
) {
1434 if (ipn_node
!= from
)
1435 ipn_proto_sendmsg(ipn_node
,msgitem
);
1440 static void ipn_null_delport(struct ipn_node
*oldport
) {}
1441 static void ipn_null_postnewport(struct ipn_node
*newport
) {}
1442 static void ipn_null_predelport(struct ipn_node
*oldport
) {}
1443 static int ipn_null_newnet(struct ipn_network
*newnet
) {return 0;}
1444 static int ipn_null_resizenet(struct ipn_network
*net
,int oldsize
,int newsize
) {
1446 static void ipn_null_delnet(struct ipn_network
*oldnet
) {}
1447 static int ipn_null_setsockopt(struct ipn_node
*port
,int optname
,
1448 char __user
*optval
, int optlen
) {return -EOPNOTSUPP
;}
1449 static int ipn_null_getsockopt(struct ipn_node
*port
,int optname
,
1450 char __user
*optval
, int *optlen
) {return -EOPNOTSUPP
;}
1451 static int ipn_null_ioctl(struct ipn_node
*port
,unsigned int request
,
1452 unsigned long arg
) {return -EOPNOTSUPP
;}
1454 /* Protocol Registration/deregisteration */
1456 void ipn_init_protocol(struct ipn_protocol
*p
)
1458 if (p
->ipn_p_delport
== NULL
) p
->ipn_p_delport
=ipn_null_delport
;
1459 if (p
->ipn_p_postnewport
== NULL
) p
->ipn_p_postnewport
=ipn_null_postnewport
;
1460 if (p
->ipn_p_predelport
== NULL
) p
->ipn_p_predelport
=ipn_null_predelport
;
1461 if (p
->ipn_p_newnet
== NULL
) p
->ipn_p_newnet
=ipn_null_newnet
;
1462 if (p
->ipn_p_resizenet
== NULL
) p
->ipn_p_resizenet
=ipn_null_resizenet
;
1463 if (p
->ipn_p_delnet
== NULL
) p
->ipn_p_delnet
=ipn_null_delnet
;
1464 if (p
->ipn_p_setsockopt
== NULL
) p
->ipn_p_setsockopt
=ipn_null_setsockopt
;
1465 if (p
->ipn_p_getsockopt
== NULL
) p
->ipn_p_getsockopt
=ipn_null_getsockopt
;
1466 if (p
->ipn_p_ioctl
== NULL
) p
->ipn_p_ioctl
=ipn_null_ioctl
;
1469 int ipn_proto_register(int protocol
,struct ipn_protocol
*ipn_service
)
1472 if (ipn_service
->ipn_p_newport
== NULL
||
1473 ipn_service
->ipn_p_handlemsg
== NULL
)
1475 ipn_init_protocol(ipn_service
);
1476 if (down_interruptible(&ipn_glob_mutex
))
1477 return -ERESTARTSYS
;
1478 if (protocol
> 1 && protocol
<= IPN_MAX_PROTO
) {
1480 if (ipn_protocol_table
[protocol
])
1483 ipn_service
->refcnt
=0;
1484 ipn_protocol_table
[protocol
]=ipn_service
;
1485 printk(KERN_INFO
"IPN: Registered protocol %d\n",protocol
+1);
1489 up(&ipn_glob_mutex
);
1493 int ipn_proto_deregister(int protocol
)
1496 if (down_interruptible(&ipn_glob_mutex
))
1497 return -ERESTARTSYS
;
1498 if (protocol
> 1 && protocol
<= IPN_MAX_PROTO
) {
1500 if (ipn_protocol_table
[protocol
]) {
1501 if (ipn_protocol_table
[protocol
]->refcnt
== 0) {
1502 ipn_protocol_table
[protocol
]=NULL
;
1503 printk(KERN_INFO
"IPN: Unregistered protocol %d\n",protocol
+1);
1510 up(&ipn_glob_mutex
);
1515 /* Module constructor/destructor */
1516 static struct net_proto_family ipn_family_ops
= {
1518 .create
= ipn_create
,
1519 .owner
= THIS_MODULE
,
1522 /* IPN constructor */
1523 static int ipn_init(void)
1527 ipn_init_protocol(&ipn_bcast
);
1528 ipn_network_cache
=kmem_cache_create("ipn_network",sizeof(struct ipn_network
),0,0,NULL
);
1529 if (!ipn_network_cache
) {
1530 printk(KERN_CRIT
"%s: Cannot create ipn_network SLAB cache!\n",
1536 ipn_node_cache
=kmem_cache_create("ipn_node",sizeof(struct ipn_node
),0,0,NULL
);
1537 if (!ipn_node_cache
) {
1538 printk(KERN_CRIT
"%s: Cannot create ipn_node SLAB cache!\n",
1544 ipn_msgitem_cache
=kmem_cache_create("ipn_msgitem",sizeof(struct msgitem
),0,0,NULL
);
1545 if (!ipn_msgitem_cache
) {
1546 printk(KERN_CRIT
"%s: Cannot create ipn_msgitem SLAB cache!\n",
1552 rc
=proto_register(&ipn_proto
,1);
1554 printk(KERN_CRIT
"%s: Cannot register the protocol!\n",
1556 goto out_net_node_msg
;
1559 sock_register(&ipn_family_ops
);
1561 printk(KERN_INFO
"IPN: Virtual Square Project, University of Bologna 2007\n");
1565 kmem_cache_destroy(ipn_msgitem_cache
);
1567 kmem_cache_destroy(ipn_node_cache
);
1569 kmem_cache_destroy(ipn_network_cache
);
1574 /* IPN destructor */
1575 static void ipn_exit(void)
1578 if (ipn_msgitem_cache
)
1579 kmem_cache_destroy(ipn_msgitem_cache
);
1581 kmem_cache_destroy(ipn_node_cache
);
1582 if (ipn_network_cache
)
1583 kmem_cache_destroy(ipn_network_cache
);
1584 sock_unregister(PF_IPN
);
1585 proto_unregister(&ipn_proto
);
1586 printk(KERN_INFO
"IPN removed\n");
1589 module_init(ipn_init
);
1590 module_exit(ipn_exit
);
1592 EXPORT_SYMBOL_GPL(ipn_proto_register
);
1593 EXPORT_SYMBOL_GPL(ipn_proto_deregister
);
1594 EXPORT_SYMBOL_GPL(ipn_proto_sendmsg
);
1595 EXPORT_SYMBOL_GPL(ipn_proto_oobsendmsg
);
1596 EXPORT_SYMBOL_GPL(ipn_msgpool_alloc
);
1597 EXPORT_SYMBOL_GPL(ipn_msgpool_put
);