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"
35 #include "ipn_msgbuf.h"
37 MODULE_LICENSE("GPL");
38 MODULE_AUTHOR("VIEW-OS TEAM");
39 MODULE_DESCRIPTION("IPN Kernel Module");
41 #define IPN_MAX_PROTO 4
43 /*extension of RCV_SHUTDOWN defined in include/net/sock.h
44 * when the bit is set recv fails */
45 /* NO_OOB: do not send OOB */
46 #define RCV_SHUTDOWN_NO_OOB 4
47 /* EXTENDED MASK including OOB */
48 #define SHUTDOWN_XMASK (SHUTDOWN_MASK | RCV_SHUTDOWN_NO_OOB)
49 /* if XRCV_SHUTDOWN is all set recv fails */
50 #define XRCV_SHUTDOWN (RCV_SHUTDOWN | RCV_SHUTDOWN_NO_OOB)
52 /* Network table and hash */
53 struct hlist_head ipn_network_table
[IPN_HASH_SIZE
+ 1];
54 /* not needed. Now protected by ipn_glob_mutex
56 * DEFINE_SPINLOCK(ipn_table_lock);
58 static struct kmem_cache
*ipn_network_cache
;
59 static struct kmem_cache
*ipn_node_cache
;
60 static struct kmem_cache
*ipn_msgitem_cache
;
61 static DECLARE_MUTEX(ipn_glob_mutex
);
63 /* Protocol 1: HUB/Broadcast default protocol. Function Prototypes */
64 static int ipn_bcast_newport(struct ipn_node
*newport
);
65 static int ipn_bcast_handlemsg(struct ipn_node
*from
,
66 struct msgpool_item
*msgitem
);
68 /* default protocol IPN_BROADCAST (0) */
69 static struct ipn_protocol ipn_bcast
= {
71 .ipn_p_newport
=ipn_bcast_newport
,
72 .ipn_p_handlemsg
=ipn_bcast_handlemsg
};
74 static struct ipn_protocol
*ipn_protocol_table
[IPN_MAX_PROTO
]={&ipn_bcast
};
76 /* Socket call function prototypes */
77 static int ipn_release(struct socket
*);
78 static int ipn_bind(struct socket
*, struct sockaddr
*, int);
79 static int ipn_connect(struct socket
*, struct sockaddr
*,
80 int addr_len
, int flags
);
81 static int ipn_getname(struct socket
*, struct sockaddr
*, int *, int);
82 static unsigned int ipn_poll(struct file
*, struct socket
*, poll_table
*);
83 static int ipn_ioctl(struct socket
*, unsigned int, unsigned long);
84 static int ipn_shutdown(struct socket
*, int);
85 static int ipn_sendmsg(struct kiocb
*, struct socket
*,
86 struct msghdr
*, size_t);
87 static int ipn_recvmsg(struct kiocb
*, struct socket
*,
88 struct msghdr
*, size_t, int);
89 static int ipn_setsockopt(struct socket
*sock
, int level
, int optname
,
90 char __user
*optval
, int optlen
);
91 static int ipn_getsockopt(struct socket
*sock
, int level
, int optname
,
92 char __user
*optval
, int __user
*optlen
);
94 /* Network table Management
95 * inode->ipn_network hash table
96 * LOCKING: MUTEX ipn_glob_mutex must be LOCKED*/
97 static inline void ipn_insert_network(struct hlist_head
*list
, struct ipn_network
*ipnn
)
99 /* *IPNTL* spin_lock(&ipn_table_lock); */
100 hlist_add_head(&ipnn
->hnode
, list
);
101 /* *IPNTL* spin_unlock(&ipn_table_lock); */
104 static inline void ipn_remove_network(struct ipn_network
*ipnn
)
106 /* *IPNTL* spin_lock(&ipn_table_lock); */
107 hlist_del(&ipnn
->hnode
);
108 /* *IPNTL* spin_unlock(&ipn_table_lock); */
111 static struct ipn_network
*ipn_find_network_byinode(struct inode
*i
)
113 struct ipn_network
*ipnn
;
114 struct hlist_node
*node
;
116 /* *IPNTL* spin_lock(&ipn_table_lock);*/
117 hlist_for_each_entry(ipnn
, node
,
118 &ipn_network_table
[i
->i_ino
& (IPN_HASH_SIZE
- 1)], hnode
) {
119 struct dentry
*dentry
= ipnn
->dentry
;
121 if(ipnn
->refcnt
> 0 && dentry
&& dentry
->d_inode
== i
)
126 /* *IPNTL* spin_unlock(&ipn_table_lock); */
130 /* msgpool management
131 * msgpool_item are ipn_network dependent (each net has its own MTU)
132 * for each message sent there is one msgpool_item and many struct msgitem
133 * one for each receipient.
134 * msgitem are connected to the node's msgqueue or oobmsgqueue.
135 * when a message is delivered to a process the msgitem is deleted and
136 * the count of the msgpool_item is decreased.
137 * msgpool_item elements gets deleted automatically when count is 0*/
140 struct list_head list
;
141 struct msgpool_item
*msg
;
144 /* alloc a fresh msgpool item. count is set to 1.
147 * for each receipient
148 * enqueue messages to the process (using msgitem), ipn_msgpool_hold
150 * The message can be delivered concurrently. init count to 1 guarantees
151 * that it survives at least until is has been enqueued to all
153 static struct msgpool_item
*_ipn_msgpool_alloc(struct ipn_network
*ipnn
)
155 struct msgpool_item
*new;
156 if ((new=kmem_cache_alloc(ipnn
->msgpool_cache
,GFP_KERNEL
)) != NULL
) {
157 atomic_set(&new->count
,1);
158 atomic_inc(&ipnn
->msgpool_nelem
);
163 struct msgpool_item
*ipn_msgpool_alloc(struct ipn_network
*ipnn
,int leaky
)
165 if (leaky
&& (ipnn
->flags
& IPN_FLAG_LOSSLESS
) &&
166 atomic_read(&ipnn
->msgpool_nelem
) < ipnn
->msgpool_size
)
169 return _ipn_msgpool_alloc(ipnn
);
172 /* If the service il LOSSLESS, this msgpool call waits for an
173 * available msgpool item */
174 static struct msgpool_item
*ipn_msgpool_alloc_locking(struct ipn_network
*ipnn
)
176 if (ipnn
->flags
& IPN_FLAG_LOSSLESS
) {
177 while (atomic_read(&ipnn
->msgpool_nelem
) >= ipnn
->msgpool_size
) {
178 if (wait_event_interruptible_exclusive(ipnn
->send_wait
,
179 atomic_read(&ipnn
->msgpool_nelem
) < ipnn
->msgpool_size
))
183 return _ipn_msgpool_alloc(ipnn
);
186 static inline void ipn_msgpool_hold(struct msgpool_item
*msg
)
188 atomic_inc(&msg
->count
);
191 /* decrease count and delete msgpool_item if count == 0 */
192 void ipn_msgpool_put(struct msgpool_item
*old
,
193 struct ipn_network
*ipnn
)
195 if (atomic_dec_and_test(&old
->count
)) {
196 kmem_cache_free(ipnn
->msgpool_cache
,old
);
197 atomic_dec(&ipnn
->msgpool_nelem
);
198 if (ipnn
->flags
& IPN_FLAG_LOSSLESS
) /* this could be done anyway */
199 wake_up_interruptible(&ipnn
->send_wait
);
204 static const struct proto_ops ipn_ops
= {
206 .owner
= THIS_MODULE
,
207 .release
= ipn_release
,
209 .connect
= ipn_connect
,
210 .socketpair
= sock_no_socketpair
,
211 .accept
= sock_no_accept
,
212 .getname
= ipn_getname
,
215 .listen
= sock_no_listen
,
216 .shutdown
= ipn_shutdown
,
217 .setsockopt
= ipn_setsockopt
,
218 .getsockopt
= ipn_getsockopt
,
219 .sendmsg
= ipn_sendmsg
,
220 .recvmsg
= ipn_recvmsg
,
221 .mmap
= sock_no_mmap
,
222 .sendpage
= sock_no_sendpage
,
225 static struct proto ipn_proto
= {
227 .owner
= THIS_MODULE
,
228 .obj_size
= sizeof(struct ipn_sock
),
232 * ipn_node is a separate structure, pointed by ipn_sock -> node
233 * when a node is "persistent", ipn_node survives while ipn_sock gets released*/
234 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
235 static int ipn_create(struct socket
*sock
, int protocol
)
237 static int ipn_create(struct net
*net
,struct socket
*sock
, int protocol
)
240 struct ipn_sock
*ipn_sk
;
241 struct ipn_node
*ipn_node
;
243 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
246 if (net
!= &init_net
)
247 return -EAFNOSUPPORT
;
250 if (sock
->type
!= SOCK_RAW
)
255 protocol
=IPN_BROADCAST
-1;
256 if (protocol
< 0 || protocol
>= IPN_MAX_PROTO
||
257 ipn_protocol_table
[protocol
] == NULL
)
258 return -EPROTONOSUPPORT
;
259 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
260 ipn_sk
= (struct ipn_sock
*) sk_alloc(PF_IPN
, GFP_KERNEL
, &ipn_proto
, 1);
262 ipn_sk
= (struct ipn_sock
*) sk_alloc(net
, PF_IPN
, GFP_KERNEL
, &ipn_proto
);
267 ipn_sk
->node
=ipn_node
=kmem_cache_alloc(ipn_node_cache
,GFP_KERNEL
);
269 sock_put((struct sock
*) ipn_sk
);
272 sock_init_data(sock
,(struct sock
*) ipn_sk
);
273 sock
->state
= SS_UNCONNECTED
;
274 sock
->ops
= &ipn_ops
;
275 sock
->sk
=(struct sock
*)ipn_sk
;
276 INIT_LIST_HEAD(&ipn_node
->nodelist
);
277 ipn_node
->protocol
=protocol
;
278 ipn_node
->flags
=IPN_NODEFLAG_INUSE
;
279 ipn_node
->shutdown
=RCV_SHUTDOWN_NO_OOB
;
280 ipn_node
->descr
[0]=0;
281 ipn_node
->portno
=IPN_PORTNO_ANY
;
284 ipn_node
->proto_private
=NULL
;
285 ipn_node
->totmsgcount
=0;
286 ipn_node
->oobmsgcount
=0;
287 spin_lock_init(&ipn_node
->msglock
);
288 INIT_LIST_HEAD(&ipn_node
->msgqueue
);
289 INIT_LIST_HEAD(&ipn_node
->oobmsgqueue
);
291 init_waitqueue_head(&ipn_node
->read_wait
);
296 /* update # of readers and # of writers counters for an ipn network.
297 * This function sends oob messages to nodes requesting the service */
298 /* LOCKING ipnn_mutex is locked */
299 static void ipn_net_update_counters(struct ipn_network
*ipnn
,
300 int chg_readers
, int chg_writers
) {
301 ipnn
->numreaders
+= chg_readers
;
302 ipnn
->numwriters
+= chg_writers
;
303 if (ipnn
->mtu
>= sizeof(struct numnode_oob
))
305 struct msgpool_item
*ipn_msg
=_ipn_msgpool_alloc(ipnn
);
307 struct numnode_oob
*oob_msg
=(struct numnode_oob
*)(ipn_msg
->data
);
308 struct ipn_node
*ipn_node
;
309 ipn_msg
->len
=sizeof(struct numnode_oob
);
310 oob_msg
->level
=IPN_ANY
;
311 oob_msg
->tag
=IPN_OOB_NUMNODE_TAG
;
312 oob_msg
->numreaders
=ipnn
->numreaders
;
313 oob_msg
->numwriters
=ipnn
->numwriters
;
314 list_for_each_entry(ipn_node
, &ipnn
->connectqueue
, nodelist
) {
315 if (ipn_node
->flags
& IPN_NODEFLAG_OOB_NUMNODES
)
316 ipn_proto_oobsendmsg(ipn_node
,ipn_msg
);
318 ipn_msgpool_put(ipn_msg
,ipnn
);
323 /* flush pending messages (for close and shutdown RCV) */
324 /* LOCKING: ipnn_mutex is locked */
325 static void ipn_flush_recvqueue(struct ipn_node
*ipn_node
)
327 struct ipn_network
*ipnn
=ipn_node
->ipn
;
328 spin_lock(&ipn_node
->msglock
);
329 while (!list_empty(&ipn_node
->msgqueue
)) {
330 struct msgitem
*msgitem
=
331 list_first_entry(&ipn_node
->msgqueue
, struct msgitem
, list
);
332 list_del(&msgitem
->list
);
333 ipn_node
->totmsgcount
--;
334 ipn_msgpool_put(msgitem
->msg
,ipnn
);
335 kmem_cache_free(ipn_msgitem_cache
,msgitem
);
337 spin_unlock(&ipn_node
->msglock
);
340 /* flush pending oob messages (for socket close) */
341 /* LOCKING: ipnn_mutex is locked */
342 static void ipn_flush_oobrecvqueue(struct ipn_node
*ipn_node
)
344 struct ipn_network
*ipnn
=ipn_node
->ipn
;
345 spin_lock(&ipn_node
->msglock
);
346 while (!list_empty(&ipn_node
->oobmsgqueue
)) {
347 struct msgitem
*msgitem
=
348 list_first_entry(&ipn_node
->oobmsgqueue
, struct msgitem
, list
);
349 list_del(&msgitem
->list
);
350 ipn_node
->totmsgcount
--;
351 ipn_node
->oobmsgcount
--;
352 ipn_msgpool_put(msgitem
->msg
,ipnn
);
353 kmem_cache_free(ipn_msgitem_cache
,msgitem
);
355 spin_unlock(&ipn_node
->msglock
);
358 /* Terminate node. The node is "logically" terminated. */
359 /* LOCKING: ipn_glob_lock must be locked here */
360 static int ipn_terminate_node(struct ipn_node
*ipn_node
)
362 struct ipn_network
*ipnn
=ipn_node
->ipn
;
364 if (down_interruptible(&ipnn
->ipnn_mutex
))
366 if (ipn_node
->portno
>= 0) {
367 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_predelport(ipn_node
);
368 ipnn
->connport
[ipn_node
->portno
]=NULL
;
370 list_del(&ipn_node
->nodelist
);
371 ipn_flush_recvqueue(ipn_node
);
372 ipn_flush_oobrecvqueue(ipn_node
);
373 if (ipn_node
->portno
>= 0)
374 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_delport(ipn_node
);
376 ipn_net_update_counters(ipnn
,
377 (ipn_node
->shutdown
& RCV_SHUTDOWN
)?0:-1,
378 (ipn_node
->shutdown
& SEND_SHUTDOWN
)?0:-1);
379 ipn_node
->shutdown
= SHUTDOWN_XMASK
;
380 up(&ipnn
->ipnn_mutex
);
382 ipn_netdev_close(ipn_node
);
383 /* No more network elements */
385 if (ipnn
->refcnt
== 0)
387 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_delnet(ipnn
);
388 ipn_remove_network(ipnn
);
389 ipn_protocol_table
[ipnn
->protocol
]->refcnt
--;
394 if (ipnn
->msgpool_cache
)
395 ipn_msgbuf_put(ipnn
->msgpool_cache
);
397 kfree(ipnn
->connport
);
398 kmem_cache_free(ipn_network_cache
, ipnn
);
399 module_put(THIS_MODULE
);
403 kfree(ipn_node
->pbp
);
409 /* release of a socket */
410 static int ipn_release (struct socket
*sock
)
412 struct ipn_sock
*ipn_sk
=(struct ipn_sock
*)sock
->sk
;
413 struct ipn_node
*ipn_node
=ipn_sk
->node
;
415 if (down_interruptible(&ipn_glob_mutex
))
417 if (ipn_node
->flags
& IPN_NODEFLAG_PERSIST
) {
418 ipn_node
->flags
&= ~IPN_NODEFLAG_INUSE
;
422 rv
=ipn_terminate_node(ipn_node
);
426 kmem_cache_free(ipn_node_cache
,ipn_node
);
430 sock_put((struct sock
*) ipn_sk
);
434 /* _set persist, change the persistence of a node,
435 * when persistence gets cleared and the node is no longer used
436 * the node is terminated and freed.
437 * ipn_glob_mutex must be locked */
438 static int _ipn_setpersist(struct ipn_node
*ipn_node
, int persist
)
442 ipn_node
->flags
|= IPN_NODEFLAG_PERSIST
;
444 ipn_node
->flags
&= ~IPN_NODEFLAG_PERSIST
;
445 if (!(ipn_node
->flags
& IPN_NODEFLAG_INUSE
)) {
446 rv
=ipn_terminate_node(ipn_node
);
448 kmem_cache_free(ipn_node_cache
,ipn_node
);
455 * lock ipn_glob_mutex and call __ipn_setpersist above */
456 static int ipn_setpersist(struct ipn_node
*ipn_node
, int persist
)
459 if (ipn_node
->dev
== NULL
)
461 if (down_interruptible(&ipn_glob_mutex
))
463 rv
=_ipn_setpersist(ipn_node
,persist
);
468 /* several network parameters can be set by setsockopt prior to bind */
469 /* struct pre_bind_parms is a temporary stucture connected to ipn_node->pbp
470 * to keep the parameter values. */
471 struct pre_bind_parms
{
472 unsigned short maxports
;
473 unsigned short flags
;
474 unsigned short msgpoolsize
;
479 /* STD_PARMS: BITS_PER_LONG nodes, no flags, BITS_PER_BYTE pending msgs,
480 * Ethernet + VLAN MTU*/
481 #define STD_BIND_PARMS {BITS_PER_LONG, 0, BITS_PER_BYTE, 1514, 0x777};
483 static int ipn_mkname(struct sockaddr_un
* sunaddr
, int len
)
485 if (len
<= sizeof(short) || len
> sizeof(*sunaddr
))
487 if (!sunaddr
|| sunaddr
->sun_family
!= AF_IPN
)
490 * This may look like an off by one error but it is a bit more
491 * subtle. 108 is the longest valid AF_IPN path for a binding.
492 * sun_path[108] doesnt as such exist. However in kernel space
493 * we are guaranteed that it is a valid memory location in our
494 * kernel address buffer.
496 ((char *)sunaddr
)[len
]=0;
497 len
= strlen(sunaddr
->sun_path
)+1+sizeof(short);
502 static int ipn_bind(struct socket
*sock
, struct sockaddr
*uaddr
, int addr_len
)
504 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)uaddr
;
505 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
507 struct ipn_network
*ipnn
;
508 struct dentry
* dentry
= NULL
;
510 struct pre_bind_parms parms
=STD_BIND_PARMS
;
512 //printk("IPN bind\n");
514 if (down_interruptible(&ipn_glob_mutex
))
516 if (sock
->state
!= SS_UNCONNECTED
||
517 ipn_node
->ipn
!= NULL
) {
522 if (ipn_node
->protocol
>= 0 &&
523 (ipn_node
->protocol
>= IPN_MAX_PROTO
||
524 ipn_protocol_table
[ipn_node
->protocol
] == NULL
)) {
525 err
= -EPROTONOSUPPORT
;
529 addr_len
= ipn_mkname(sunaddr
, addr_len
);
535 /* check if there is already an ipn-network socket with that name */
536 err
= path_lookup(sunaddr
->sun_path
, LOOKUP_FOLLOW
, &nd
);
537 if (err
) { /* it does not exist, NEW IPN socket! */
539 /* Is it everything okay with the parent? */
540 err
= path_lookup(sunaddr
->sun_path
, LOOKUP_PARENT
, &nd
);
542 goto out_mknod_parent
;
543 /* Do I have the permission to create a file? */
544 dentry
= lookup_create(&nd
, 0);
545 err
= PTR_ERR(dentry
);
547 goto out_mknod_unlock
;
549 * All right, let's create it.
552 mode
= ipn_node
->pbp
->mode
;
554 mode
= SOCK_INODE(sock
)->i_mode
;
555 mode
= S_IFSOCK
| (mode
& ~current
->fs
->umask
);
557 err
= vfs_mknod(nd
.dentry
->d_inode
, dentry
, nd
.mnt
, mode
, 0);
559 err
= vfs_mknod(nd
.dentry
->d_inode
, dentry
, mode
, 0);
563 mutex_unlock(&nd
.dentry
->d_inode
->i_mutex
);
566 /* create a new ipn_network item */
568 parms
=*ipn_node
->pbp
;
569 ipnn
=kmem_cache_zalloc(ipn_network_cache
,GFP_KERNEL
);
572 goto out_mknod_dput_ipnn
;
574 ipnn
->connport
=kzalloc(parms
.maxports
* sizeof(struct ipn_node
*),GFP_KERNEL
);
575 if (!ipnn
->connport
) {
577 goto out_mknod_dput_ipnn2
;
580 /* module refcnt is incremented for each network, thus
581 * rmmod is forbidden if there are persistent node */
582 if (!try_module_get(THIS_MODULE
)) {
584 goto out_mknod_dput_ipnn2
;
586 memcpy(&ipnn
->sunaddr
,sunaddr
,addr_len
);
588 ipnn
->msgpool_cache
=ipn_msgbuf_get(ipnn
->mtu
);
589 if (!ipnn
->msgpool_cache
) {
591 goto out_mknod_dput_putmodule
;
593 INIT_LIST_HEAD(&ipnn
->unconnectqueue
);
594 INIT_LIST_HEAD(&ipnn
->connectqueue
);
596 ipnn
->dentry
=nd
.dentry
;
598 init_MUTEX(&ipnn
->ipnn_mutex
);
599 ipnn
->sunaddr_len
=addr_len
;
600 ipnn
->protocol
=ipn_node
->protocol
;
601 if (ipnn
->protocol
< 0) ipnn
->protocol
= 0;
602 ipn_protocol_table
[ipnn
->protocol
]->refcnt
++;
603 ipnn
->flags
=parms
.flags
;
606 ipnn
->maxports
=parms
.maxports
;
607 atomic_set(&ipnn
->msgpool_nelem
,0);
608 ipnn
->msgpool_size
=parms
.msgpoolsize
;
609 ipnn
->proto_private
=NULL
;
610 init_waitqueue_head(&ipnn
->send_wait
);
611 err
=ipn_protocol_table
[ipnn
->protocol
]->ipn_p_newnet(ipnn
);
613 goto out_mknod_dput_putmodule
;
614 ipn_insert_network(&ipn_network_table
[nd
.dentry
->d_inode
->i_ino
& (IPN_HASH_SIZE
-1)],ipnn
);
616 /* join an existing network */
617 if (parms
.flags
& IPN_FLAG_EXCL
) {
621 err
= vfs_permission(&nd
, MAY_EXEC
);
625 if (!S_ISSOCK(nd
.dentry
->d_inode
->i_mode
))
627 ipnn
=ipn_find_network_byinode(nd
.dentry
->d_inode
);
628 if (!ipnn
|| (ipnn
->flags
& IPN_FLAG_TERMINATED
) ||
629 (ipnn
->flags
& IPN_FLAG_EXCL
))
631 list_add_tail(&ipn_node
->nodelist
,&ipnn
->unconnectqueue
);
635 kfree(ipn_node
->pbp
);
639 ipn_node
->flags
|= IPN_NODEFLAG_BOUND
;
649 out_mknod_dput_putmodule
:
650 module_put(THIS_MODULE
);
651 out_mknod_dput_ipnn2
:
652 kfree(ipnn
->connport
);
654 kmem_cache_free(ipn_network_cache
,ipnn
);
658 mutex_unlock(&nd
.dentry
->d_inode
->i_mutex
);
668 static int ipn_connect(struct socket
*sock
, struct sockaddr
*addr
,
669 int addr_len
, int flags
){
670 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)addr
;
671 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
673 struct ipn_network
*ipnn
,*previousipnn
;
677 /* the socket cannot be connected twice */
678 if (sock
->state
!= SS_UNCONNECTED
)
681 if (down_interruptible(&ipn_glob_mutex
))
684 if ((previousipnn
=ipn_node
->ipn
) == NULL
) { /* unbound */
685 unsigned char mustshutdown
=0;
686 err
= ipn_mkname(sunaddr
, addr_len
);
690 err
= path_lookup(sunaddr
->sun_path
, LOOKUP_FOLLOW
, &nd
);
693 err
= vfs_permission(&nd
, MAY_READ
);
695 if (err
== -EACCES
|| err
== -EROFS
)
696 mustshutdown
|=RCV_SHUTDOWN
;
700 err
= vfs_permission(&nd
, MAY_WRITE
);
703 mustshutdown
|=SEND_SHUTDOWN
;
707 mustshutdown
|= ipn_node
->shutdown
;
708 /* if the combination of shutdown and permissions leaves
709 * no abilities, connect returns EACCES */
710 if (mustshutdown
== SHUTDOWN_XMASK
) {
715 ipn_node
->shutdown
=mustshutdown
;
717 if (!S_ISSOCK(nd
.dentry
->d_inode
->i_mode
)) {
721 ipnn
=ipn_find_network_byinode(nd
.dentry
->d_inode
);
722 if (!ipnn
|| (ipnn
->flags
& IPN_FLAG_TERMINATED
)) {
726 if (ipn_node
->protocol
== IPN_ANY
)
727 ipn_node
->protocol
=ipnn
->protocol
;
728 else if (ipnn
->protocol
!= ipn_node
->protocol
) {
737 if (down_interruptible(&ipnn
->ipnn_mutex
)) {
741 portno
= ipn_protocol_table
[ipnn
->protocol
]->ipn_p_newport(ipn_node
);
742 if (portno
>= 0 && portno
<ipnn
->maxports
) {
743 sock
->state
= SS_CONNECTED
;
744 ipn_node
->portno
=portno
;
745 ipnn
->connport
[portno
]=ipn_node
;
746 if (!(ipn_node
->flags
& IPN_NODEFLAG_BOUND
)) {
748 list_del(&ipn_node
->nodelist
);
750 list_add_tail(&ipn_node
->nodelist
,&ipnn
->connectqueue
);
751 ipn_net_update_counters(ipnn
,
752 (ipn_node
->shutdown
& RCV_SHUTDOWN
)?0:1,
753 (ipn_node
->shutdown
& SEND_SHUTDOWN
)?0:1);
755 ipn_node
->ipn
=previousipnn
; /* undo changes on ipn_node->ipn */
758 up(&ipnn
->ipnn_mutex
);
769 static int ipn_getname(struct socket
*sock
, struct sockaddr
*uaddr
,
770 int *uaddr_len
, int peer
) {
771 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
772 struct ipn_network
*ipnn
=ipn_node
->ipn
;
773 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)uaddr
;
776 if (down_interruptible(&ipn_glob_mutex
))
779 *uaddr_len
= ipnn
->sunaddr_len
;
780 memcpy(sunaddr
,&ipnn
->sunaddr
,*uaddr_len
);
788 static unsigned int ipn_poll(struct file
*file
, struct socket
*sock
,
790 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
791 struct ipn_network
*ipnn
=ipn_node
->ipn
;
795 poll_wait(file
,&ipn_node
->read_wait
,wait
);
796 if (ipnn
->flags
& IPN_FLAG_LOSSLESS
)
797 poll_wait(file
,&ipnn
->send_wait
,wait
);
798 /* POLLIN if recv succeeds,
799 * POLL{PRI,RDNORM} if there are {oob,non-oob} messages */
800 if (ipn_node
->totmsgcount
> 0) mask
|= POLLIN
;
801 if (!(list_empty(&ipn_node
->msgqueue
))) mask
|= POLLRDNORM
;
802 if (!(list_empty(&ipn_node
->oobmsgqueue
))) mask
|= POLLPRI
;
803 if ((!(ipnn
->flags
& IPN_FLAG_LOSSLESS
)) |
804 (atomic_read(&ipnn
->msgpool_nelem
) < ipnn
->msgpool_size
))
805 mask
|= POLLOUT
| POLLWRNORM
;
810 /* connect netdev (from ioctl). connect a bound socket to a
811 * network device TAP or GRAB */
812 static int ipn_connect_netdev(struct socket
*sock
,struct ifreq
*ifr
)
815 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
816 struct ipn_network
*ipnn
=ipn_node
->ipn
;
817 if (!capable(CAP_NET_ADMIN
))
819 if (sock
->state
!= SS_UNCONNECTED
)
822 return -ENOTCONN
; /* Maybe we need a different error for "NOT BOUND" */
823 if (down_interruptible(&ipn_glob_mutex
))
825 if (down_interruptible(&ipnn
->ipnn_mutex
)) {
829 ipn_node
->dev
=ipn_netdev_alloc(ipn_node
->net
,ifr
->ifr_flags
,ifr
->ifr_name
,&err
);
832 portno
= ipn_protocol_table
[ipnn
->protocol
]->ipn_p_newport(ipn_node
);
833 if (portno
>= 0 && portno
<ipnn
->maxports
) {
834 sock
->state
= SS_CONNECTED
;
835 ipn_node
->portno
=portno
;
836 ipn_node
->flags
|= ifr
->ifr_flags
& IPN_NODEFLAG_DEVMASK
;
837 ipnn
->connport
[portno
]=ipn_node
;
838 err
=ipn_netdev_activate(ipn_node
);
840 sock
->state
= SS_UNCONNECTED
;
841 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_delport(ipn_node
);
843 ipn_node
->portno
= -1;
844 ipn_node
->flags
&= ~IPN_NODEFLAG_DEVMASK
;
845 ipnn
->connport
[portno
]=NULL
;
847 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_postnewport(ipn_node
);
848 list_del(&ipn_node
->nodelist
);
849 list_add_tail(&ipn_node
->nodelist
,&ipnn
->connectqueue
);
852 ipn_netdev_close(ipn_node
);
858 up(&ipnn
->ipnn_mutex
);
863 /* join a netdev, a socket gets connected to a persistent node
864 * not connected to another socket */
865 static int ipn_join_netdev(struct socket
*sock
,struct ifreq
*ifr
)
868 struct net_device
*dev
;
869 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
870 struct ipn_node
*ipn_joined
;
871 struct ipn_network
*ipnn
=ipn_node
->ipn
;
872 if (sock
->state
!= SS_UNCONNECTED
)
874 if (down_interruptible(&ipn_glob_mutex
))
876 if (down_interruptible(&ipnn
->ipnn_mutex
)) {
880 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
881 dev
=__dev_get_by_name(ifr
->ifr_name
);
883 dev
=__dev_get_by_name(ipn_node
->net
,ifr
->ifr_name
);
886 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
887 dev
=__dev_get_by_index(ifr
->ifr_ifindex
);
889 dev
=__dev_get_by_index(ipn_node
->net
,ifr
->ifr_ifindex
);
891 if (dev
&& (ipn_joined
=ipn_netdev2node(dev
)) != NULL
) { /* the interface does exist */
893 for (i
=0;i
<ipnn
->maxports
&& ipn_joined
!= ipnn
->connport
[i
] ;i
++)
895 if (i
< ipnn
->maxports
) { /* found */
896 /* ipn_joined is substituted to ipn_node */
897 ((struct ipn_sock
*)sock
->sk
)->node
=ipn_joined
;
898 ipn_joined
->flags
|= IPN_NODEFLAG_INUSE
;
900 kmem_cache_free(ipn_node_cache
,ipn_node
);
905 up(&ipnn
->ipnn_mutex
);
910 /* set persistence of a node looking for it by interface name
911 * (it is for sysadm, to close network interfaces)*/
912 static int ipn_setpersist_netdev(struct ifreq
*ifr
, int value
)
914 struct net_device
*dev
;
915 struct ipn_node
*ipn_node
;
917 if (!capable(CAP_NET_ADMIN
))
919 if (down_interruptible(&ipn_glob_mutex
))
921 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
922 dev
=__dev_get_by_name(ifr
->ifr_name
);
924 dev
=__dev_get_by_name(&init_net
,ifr
->ifr_name
);
927 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
928 dev
=__dev_get_by_index(ifr
->ifr_ifindex
);
930 dev
=__dev_get_by_index(&init_net
,ifr
->ifr_ifindex
);
932 if (dev
&& (ipn_node
=ipn_netdev2node(dev
)) != NULL
)
933 _ipn_setpersist(ipn_node
,value
);
941 static int ipn_ioctl(struct socket
*sock
, unsigned int cmd
, unsigned long arg
) {
942 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
943 struct ipn_network
*ipnn
=ipn_node
->ipn
;
944 void __user
* argp
= (void __user
*)arg
;
947 if (ipn_node
->shutdown
== SHUTDOWN_XMASK
)
954 case IPN_SETPERSIST_NETDEV
:
955 case IPN_CLRPERSIST_NETDEV
:
956 case IPN_CONN_NETDEV
:
957 case IPN_JOIN_NETDEV
:
959 if (copy_from_user(&ifr
, argp
, sizeof ifr
))
961 ifr
.ifr_name
[IFNAMSIZ
-1] = '\0';
964 /* actions for unconnected and unbound sockets */
966 case IPN_SETPERSIST_NETDEV
:
967 return ipn_setpersist_netdev(&ifr
,1);
968 case IPN_CLRPERSIST_NETDEV
:
969 return ipn_setpersist_netdev(&ifr
,0);
971 if (capable(CAP_NET_ADMIN
))
973 if (ipn_node
->dev
&& (ipn_node
->flags
&IPN_NODEFLAG_TAP
))
974 return dev_set_mac_address(ipn_node
->dev
, &ifr
.ifr_hwaddr
);
976 return -EADDRNOTAVAIL
;
978 if (ipnn
== NULL
|| (ipnn
->flags
& IPN_FLAG_TERMINATED
))
980 /* actions for connected or bound sockets */
982 case IPN_CONN_NETDEV
:
983 return ipn_connect_netdev(sock
,&ifr
);
984 case IPN_JOIN_NETDEV
:
985 return ipn_join_netdev(sock
,&ifr
);
987 return ipn_setpersist(ipn_node
,arg
);
991 if (down_interruptible(&ipnn
->ipnn_mutex
))
993 rv
=ipn_protocol_table
[ipn_node
->protocol
]->ipn_p_ioctl(ipn_node
,cmd
,arg
);
994 up(&ipnn
->ipnn_mutex
);
1001 /* shutdown: close socket for input or for output.
1002 * shutdown can be called prior to connect and it is not reversible */
1003 static int ipn_shutdown(struct socket
*sock
, int mode
) {
1004 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1005 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1006 int oldshutdown
=ipn_node
->shutdown
;
1007 mode
= (mode
+1)&(RCV_SHUTDOWN
|SEND_SHUTDOWN
);
1009 ipn_node
->shutdown
|= mode
;
1012 if (down_interruptible(&ipnn
->ipnn_mutex
)) {
1013 ipn_node
->shutdown
= oldshutdown
;
1014 return -ERESTARTSYS
;
1016 oldshutdown
=ipn_node
->shutdown
-oldshutdown
;
1017 if (sock
->state
== SS_CONNECTED
&& oldshutdown
) {
1018 ipn_net_update_counters(ipnn
,
1019 (ipn_node
->shutdown
& RCV_SHUTDOWN
)?0:-1,
1020 (ipn_node
->shutdown
& SEND_SHUTDOWN
)?0:-1);
1023 /* if recv channel has been shut down, flush the recv queue */
1024 if ((ipn_node
->shutdown
& RCV_SHUTDOWN
))
1025 ipn_flush_recvqueue(ipn_node
);
1026 up(&ipnn
->ipnn_mutex
);
1031 /* injectmsg: a new message is entering the ipn network.
1032 * injectmsg gets called by send and by the grab/tap node */
1033 int ipn_proto_injectmsg(struct ipn_node
*from
, struct msgpool_item
*msg
)
1035 struct ipn_network
*ipnn
=from
->ipn
;
1037 if (down_interruptible(&ipnn
->ipnn_mutex
))
1040 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_handlemsg(from
, msg
);
1041 up(&ipnn
->ipnn_mutex
);
1047 static int ipn_sendmsg(struct kiocb
*kiocb
, struct socket
*sock
,
1048 struct msghdr
*msg
, size_t len
) {
1049 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1050 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1051 struct msgpool_item
*newmsg
;
1054 if (unlikely(sock
->state
!= SS_CONNECTED
))
1056 if (unlikely(ipn_node
->shutdown
& SEND_SHUTDOWN
)) {
1057 if (ipn_node
->shutdown
== SHUTDOWN_XMASK
)
1062 if (len
> ipnn
->mtu
)
1064 newmsg
=ipn_msgpool_alloc_locking(ipnn
);
1068 err
=memcpy_fromiovec(newmsg
->data
, msg
->msg_iov
, len
);
1070 ipn_proto_injectmsg(ipn_node
, newmsg
);
1071 ipn_msgpool_put(newmsg
,ipnn
);
1075 /* enqueue an oob message. "to" is the destination */
1076 void ipn_proto_oobsendmsg(struct ipn_node
*to
, struct msgpool_item
*msg
)
1079 if (!to
->dev
) { /* no oob to netdev */
1080 struct msgitem
*msgitem
;
1081 struct ipn_network
*ipnn
=to
->ipn
;
1082 spin_lock(&to
->msglock
);
1083 if ((to
->shutdown
& RCV_SHUTDOWN_NO_OOB
) == 0 &&
1084 (ipnn
->flags
& IPN_FLAG_LOSSLESS
||
1085 to
->oobmsgcount
< ipnn
->msgpool_size
)) {
1086 if ((msgitem
=kmem_cache_alloc(ipn_msgitem_cache
,GFP_KERNEL
))!=NULL
) {
1090 list_add_tail(&msgitem
->list
, &to
->oobmsgqueue
);
1091 ipn_msgpool_hold(msg
);
1094 spin_unlock(&to
->msglock
);
1095 wake_up_interruptible(&to
->read_wait
);
1100 /* ipn_proto_sendmsg is called by protocol implementation to enqueue a
1101 * for a destination (to).*/
1102 void ipn_proto_sendmsg(struct ipn_node
*to
, struct msgpool_item
*msg
)
1106 ipn_netdev_sendmsg(to
,msg
);
1109 struct msgitem
*msgitem
;
1110 struct ipn_network
*ipnn
=to
->ipn
;
1111 spin_lock(&to
->msglock
);
1112 if (likely((to
->shutdown
& RCV_SHUTDOWN
)==0)) {
1113 /*if (unlikely((ipnn->flags & IPN_FLAG_LOSSLESS) == 0 ||
1114 to->totmsgcount >= ipnn->msgpool_size))
1116 if (likely(ipnn
->flags
& IPN_FLAG_LOSSLESS
||
1117 to
->totmsgcount
< ipnn
->msgpool_size
)) {
1118 if ((msgitem
=kmem_cache_alloc(ipn_msgitem_cache
,GFP_KERNEL
))!=NULL
) {
1121 list_add_tail(&msgitem
->list
, &to
->msgqueue
);
1122 ipn_msgpool_hold(msg
);
1126 spin_unlock(&to
->msglock
);
1127 wake_up_interruptible(&to
->read_wait
);
1133 static int ipn_recvmsg(struct kiocb
*kiocb
, struct socket
*sock
,
1134 struct msghdr
*msg
, size_t len
, int flags
) {
1135 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1136 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1137 struct msgitem
*msgitem
;
1138 struct msgpool_item
*currmsg
;
1140 if (unlikely(sock
->state
!= SS_CONNECTED
))
1143 if (unlikely((ipn_node
->shutdown
& XRCV_SHUTDOWN
) == XRCV_SHUTDOWN
)) {
1144 if (ipn_node
->shutdown
== SHUTDOWN_XMASK
) /*EOF, nothing can be read*/
1147 return -EPIPE
; /*trying to read on a write only node */
1150 /* wait for a message */
1151 spin_lock(&ipn_node
->msglock
);
1152 while (ipn_node
->totmsgcount
== 0) {
1153 spin_unlock(&ipn_node
->msglock
);
1154 if (wait_event_interruptible(ipn_node
->read_wait
,
1155 !(ipn_node
->totmsgcount
== 0)))
1156 return -ERESTARTSYS
;
1157 spin_lock(&ipn_node
->msglock
);
1159 /* oob gets delivered first. oob are rare */
1160 if (likely(list_empty(&ipn_node
->oobmsgqueue
)))
1161 msgitem
=list_first_entry(&ipn_node
->msgqueue
, struct msgitem
, list
);
1163 msgitem
=list_first_entry(&ipn_node
->oobmsgqueue
, struct msgitem
, list
);
1164 msg
->msg_flags
|= MSG_OOB
;
1165 ipn_node
->oobmsgcount
--;
1167 list_del(&msgitem
->list
);
1168 ipn_node
->totmsgcount
--;
1169 spin_unlock(&ipn_node
->msglock
);
1170 currmsg
=msgitem
->msg
;
1171 if (currmsg
->len
< len
)
1173 memcpy_toiovec(msg
->msg_iov
, currmsg
->data
, len
);
1174 ipn_msgpool_put(currmsg
,ipnn
);
1175 kmem_cache_free(ipn_msgitem_cache
,msgitem
);
1180 /* resize a network: change the # of communication ports (connport) */
1181 static int ipn_netresize(struct ipn_network
*ipnn
,int newsize
)
1184 struct ipn_node
**newconnport
;
1185 struct ipn_node
**oldconnport
;
1187 if (down_interruptible(&ipnn
->ipnn_mutex
))
1188 return -ERESTARTSYS
;
1189 oldsize
=ipnn
->maxports
;
1190 if (newsize
== oldsize
) {
1191 up(&ipnn
->ipnn_mutex
);
1195 /* shrink a network. all the ports we are going to eliminate
1196 * must be unused! */
1197 if (newsize
< oldsize
) {
1199 for (i
=newsize
; i
<oldsize
; i
++)
1200 if (ipnn
->connport
[i
]) {
1201 up(&ipnn
->ipnn_mutex
);
1206 oldconnport
=ipnn
->connport
;
1207 /* allocate the new connport array and copy the old one */
1208 newconnport
=kzalloc(newsize
* sizeof(struct ipn_node
*),GFP_KERNEL
);
1210 up(&ipnn
->ipnn_mutex
);
1213 memcpy(newconnport
,oldconnport
,min
* sizeof(struct ipn_node
*));
1214 ipnn
->connport
=newconnport
;
1215 ipnn
->maxports
=newsize
;
1216 /* notify the protocol that the netowrk has been resized */
1217 err
=ipn_protocol_table
[ipnn
->protocol
]->ipn_p_resizenet(ipnn
,oldsize
,newsize
);
1219 /* roll back if the resize operation failed for the protocol */
1220 ipnn
->connport
=oldconnport
;
1221 ipnn
->maxports
=oldsize
;
1224 /* successful mission, network resized */
1226 up(&ipnn
->ipnn_mutex
);
1230 /* IPN SETSOCKOPT */
1231 static int ipn_setsockopt(struct socket
*sock
, int level
, int optname
,
1232 char __user
*optval
, int optlen
) {
1233 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1234 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1236 if (ipn_node
->shutdown
== SHUTDOWN_XMASK
)
1238 if (level
!= 0 && level
!= ipn_node
->protocol
+1)
1239 return -EPROTONOSUPPORT
;
1241 /* protocol specific sockopt */
1244 if (down_interruptible(&ipnn
->ipnn_mutex
))
1245 return -ERESTARTSYS
;
1246 rv
=ipn_protocol_table
[ipn_node
->protocol
]->ipn_p_setsockopt(ipn_node
,optname
,optval
,optlen
);
1247 up(&ipnn
->ipnn_mutex
);
1252 if (optname
== IPN_SO_DESCR
) {
1253 if (optlen
> IPN_DESCRLEN
)
1256 memset(ipn_node
->descr
,0,IPN_DESCRLEN
);
1257 if (copy_from_user(ipn_node
->descr
,optval
,optlen
))
1258 ipn_node
->descr
[0]=0;
1260 ipn_node
->descr
[optlen
-1]=0;
1264 if (optlen
< sizeof(int))
1266 else if ((optname
& IPN_SO_PREBIND
) && (ipnn
!= NULL
))
1270 get_user(val
, (int __user
*) optval
);
1271 if ((optname
& IPN_SO_PREBIND
) && !ipn_node
->pbp
) {
1272 struct pre_bind_parms std
=STD_BIND_PARMS
;
1273 ipn_node
->pbp
=kzalloc(sizeof(struct pre_bind_parms
),GFP_KERNEL
);
1276 *(ipn_node
->pbp
)=std
;
1280 if (sock
->state
== SS_UNCONNECTED
)
1281 ipn_node
->portno
=val
;
1285 case IPN_SO_CHANGE_NUMNODES
:
1286 if ((ipn_node
->flags
& IPN_NODEFLAG_BOUND
)!=0) {
1290 return ipn_netresize(ipnn
,val
);
1294 case IPN_SO_WANT_OOB_NUMNODES
:
1296 ipn_node
->flags
|= IPN_NODEFLAG_OOB_NUMNODES
;
1298 ipn_node
->flags
&= ~IPN_NODEFLAG_OOB_NUMNODES
;
1300 case IPN_SO_HANDLE_OOB
:
1302 ipn_node
->shutdown
&= ~RCV_SHUTDOWN_NO_OOB
;
1304 ipn_node
->shutdown
|= RCV_SHUTDOWN_NO_OOB
;
1310 ipn_node
->pbp
->mtu
=val
;
1312 case IPN_SO_NUMNODES
:
1316 ipn_node
->pbp
->maxports
=val
;
1318 case IPN_SO_MSGPOOLSIZE
:
1322 ipn_node
->pbp
->msgpoolsize
=val
;
1325 ipn_node
->pbp
->flags
=val
;
1328 ipn_node
->pbp
->mode
=val
;
1337 /* IPN GETSOCKOPT */
1338 static int ipn_getsockopt(struct socket
*sock
, int level
, int optname
,
1339 char __user
*optval
, int __user
*optlen
) {
1340 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1341 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1344 if (ipn_node
->shutdown
== SHUTDOWN_XMASK
)
1346 if (level
!= 0 && level
!= ipn_node
->protocol
+1)
1347 return -EPROTONOSUPPORT
;
1351 /* protocol specific sockopt */
1352 if (down_interruptible(&ipnn
->ipnn_mutex
))
1353 return -ERESTARTSYS
;
1354 rv
=ipn_protocol_table
[ipn_node
->protocol
]->ipn_p_getsockopt(ipn_node
,optname
,optval
,optlen
);
1355 up(&ipnn
->ipnn_mutex
);
1360 if (get_user(len
, optlen
))
1362 if (optname
== IPN_SO_DESCR
) {
1363 if (len
< IPN_DESCRLEN
)
1366 if (len
> IPN_DESCRLEN
)
1368 if(put_user(len
, optlen
))
1370 if(copy_to_user(optval
,ipn_node
->descr
,len
))
1378 val
=ipn_node
->portno
;
1383 else if (ipn_node
->pbp
)
1384 val
=ipn_node
->pbp
->mtu
;
1386 case IPN_SO_NUMNODES
:
1389 else if (ipn_node
->pbp
)
1390 val
=ipn_node
->pbp
->maxports
;
1392 case IPN_SO_MSGPOOLSIZE
:
1394 val
=ipnn
->msgpool_size
;
1395 else if (ipn_node
->pbp
)
1396 val
=ipn_node
->pbp
->msgpoolsize
;
1401 else if (ipn_node
->pbp
)
1402 val
=ipn_node
->pbp
->flags
;
1407 else if (ipn_node
->pbp
)
1408 val
=ipn_node
->pbp
->mode
;
1414 if (len
< sizeof(int))
1418 if(put_user(len
, optlen
))
1420 if(copy_to_user(optval
,&val
,len
))
1429 /* BROADCAST/HUB implementation */
1431 static int ipn_bcast_newport(struct ipn_node
*newport
) {
1432 struct ipn_network
*ipnn
=newport
->ipn
;
1434 for (i
=0;i
<ipnn
->maxports
;i
++) {
1435 if (ipnn
->connport
[i
] == NULL
)
1441 static int ipn_bcast_handlemsg(struct ipn_node
*from
,
1442 struct msgpool_item
*msgitem
){
1443 struct ipn_network
*ipnn
=from
->ipn
;
1445 struct ipn_node
*ipn_node
;
1446 list_for_each_entry(ipn_node
, &ipnn
->connectqueue
, nodelist
) {
1447 if (ipn_node
!= from
)
1448 ipn_proto_sendmsg(ipn_node
,msgitem
);
1453 static void ipn_null_delport(struct ipn_node
*oldport
) {}
1454 static void ipn_null_postnewport(struct ipn_node
*newport
) {}
1455 static void ipn_null_predelport(struct ipn_node
*oldport
) {}
1456 static int ipn_null_newnet(struct ipn_network
*newnet
) {return 0;}
1457 static int ipn_null_resizenet(struct ipn_network
*net
,int oldsize
,int newsize
) {
1459 static void ipn_null_delnet(struct ipn_network
*oldnet
) {}
1460 static int ipn_null_setsockopt(struct ipn_node
*port
,int optname
,
1461 char __user
*optval
, int optlen
) {return -EOPNOTSUPP
;}
1462 static int ipn_null_getsockopt(struct ipn_node
*port
,int optname
,
1463 char __user
*optval
, int *optlen
) {return -EOPNOTSUPP
;}
1464 static int ipn_null_ioctl(struct ipn_node
*port
,unsigned int request
,
1465 unsigned long arg
) {return -EOPNOTSUPP
;}
1467 /* Protocol Registration/deregisteration */
1469 void ipn_init_protocol(struct ipn_protocol
*p
)
1471 if (p
->ipn_p_delport
== NULL
) p
->ipn_p_delport
=ipn_null_delport
;
1472 if (p
->ipn_p_postnewport
== NULL
) p
->ipn_p_postnewport
=ipn_null_postnewport
;
1473 if (p
->ipn_p_predelport
== NULL
) p
->ipn_p_predelport
=ipn_null_predelport
;
1474 if (p
->ipn_p_newnet
== NULL
) p
->ipn_p_newnet
=ipn_null_newnet
;
1475 if (p
->ipn_p_resizenet
== NULL
) p
->ipn_p_resizenet
=ipn_null_resizenet
;
1476 if (p
->ipn_p_delnet
== NULL
) p
->ipn_p_delnet
=ipn_null_delnet
;
1477 if (p
->ipn_p_setsockopt
== NULL
) p
->ipn_p_setsockopt
=ipn_null_setsockopt
;
1478 if (p
->ipn_p_getsockopt
== NULL
) p
->ipn_p_getsockopt
=ipn_null_getsockopt
;
1479 if (p
->ipn_p_ioctl
== NULL
) p
->ipn_p_ioctl
=ipn_null_ioctl
;
1482 int ipn_proto_register(int protocol
,struct ipn_protocol
*ipn_service
)
1485 if (ipn_service
->ipn_p_newport
== NULL
||
1486 ipn_service
->ipn_p_handlemsg
== NULL
)
1488 ipn_init_protocol(ipn_service
);
1489 if (down_interruptible(&ipn_glob_mutex
))
1490 return -ERESTARTSYS
;
1491 if (protocol
> 1 && protocol
<= IPN_MAX_PROTO
) {
1493 if (ipn_protocol_table
[protocol
])
1496 ipn_service
->refcnt
=0;
1497 ipn_protocol_table
[protocol
]=ipn_service
;
1498 printk(KERN_INFO
"IPN: Registered protocol %d\n",protocol
+1);
1502 up(&ipn_glob_mutex
);
1506 int ipn_proto_deregister(int protocol
)
1509 if (down_interruptible(&ipn_glob_mutex
))
1510 return -ERESTARTSYS
;
1511 if (protocol
> 1 && protocol
<= IPN_MAX_PROTO
) {
1513 if (ipn_protocol_table
[protocol
]) {
1514 if (ipn_protocol_table
[protocol
]->refcnt
== 0) {
1515 ipn_protocol_table
[protocol
]=NULL
;
1516 printk(KERN_INFO
"IPN: Unregistered protocol %d\n",protocol
+1);
1523 up(&ipn_glob_mutex
);
1528 /* Module constructor/destructor */
1529 static struct net_proto_family ipn_family_ops
= {
1531 .create
= ipn_create
,
1532 .owner
= THIS_MODULE
,
1535 /* IPN constructor */
1536 static int ipn_init(void)
1540 ipn_init_protocol(&ipn_bcast
);
1541 ipn_network_cache
=kmem_cache_create("ipn_network",sizeof(struct ipn_network
),0,0,NULL
);
1542 if (!ipn_network_cache
) {
1543 printk(KERN_CRIT
"%s: Cannot create ipn_network SLAB cache!\n",
1549 ipn_node_cache
=kmem_cache_create("ipn_node",sizeof(struct ipn_node
),0,0,NULL
);
1550 if (!ipn_node_cache
) {
1551 printk(KERN_CRIT
"%s: Cannot create ipn_node SLAB cache!\n",
1557 ipn_msgitem_cache
=kmem_cache_create("ipn_msgitem",sizeof(struct msgitem
),0,0,NULL
);
1558 if (!ipn_msgitem_cache
) {
1559 printk(KERN_CRIT
"%s: Cannot create ipn_msgitem SLAB cache!\n",
1565 rc
=ipn_msgbuf_init();
1567 printk(KERN_CRIT
"%s: Cannot create ipn_msgbuf SLAB cache\n",
1569 goto out_net_node_msg
;
1572 rc
=proto_register(&ipn_proto
,1);
1574 printk(KERN_CRIT
"%s: Cannot register the protocol!\n",
1576 goto out_net_node_msg_msgbuf
;
1579 sock_register(&ipn_family_ops
);
1581 printk(KERN_INFO
"IPN: Virtual Square Project, University of Bologna 2007\n");
1584 out_net_node_msg_msgbuf
:
1587 kmem_cache_destroy(ipn_msgitem_cache
);
1589 kmem_cache_destroy(ipn_node_cache
);
1591 kmem_cache_destroy(ipn_network_cache
);
1596 /* IPN destructor */
1597 static void ipn_exit(void)
1600 if (ipn_msgitem_cache
)
1601 kmem_cache_destroy(ipn_msgitem_cache
);
1603 kmem_cache_destroy(ipn_node_cache
);
1604 if (ipn_network_cache
)
1605 kmem_cache_destroy(ipn_network_cache
);
1607 sock_unregister(PF_IPN
);
1608 proto_unregister(&ipn_proto
);
1609 printk(KERN_INFO
"IPN removed\n");
1612 module_init(ipn_init
);
1613 module_exit(ipn_exit
);
1615 EXPORT_SYMBOL_GPL(ipn_proto_register
);
1616 EXPORT_SYMBOL_GPL(ipn_proto_deregister
);
1617 EXPORT_SYMBOL_GPL(ipn_proto_sendmsg
);
1618 EXPORT_SYMBOL_GPL(ipn_proto_oobsendmsg
);
1619 EXPORT_SYMBOL_GPL(ipn_msgpool_alloc
);
1620 EXPORT_SYMBOL_GPL(ipn_msgpool_put
);