2 * Main inter process networking (virtual distributed ethernet) module
3 * (part of the View-OS project: wiki.virtualsquare.org)
5 * Copyright (C) 2007 Renzo Davoli (renzo@cs.unibo.it)
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * Due to this file being licensed under the GPL there is controversy over
13 * whether this permits you to write a module that #includes this file
14 * without placing your module under the GPL. Please consult a lawyer for
15 * advice before doing this.
17 * WARNING: THIS CODE IS ALREADY EXPERIMENTAL
21 #include <linux/init.h>
22 #include <linux/module.h>
23 #include <linux/socket.h>
24 #include <linux/namei.h>
25 #include <linux/poll.h>
27 #include <linux/list.h>
28 #include <linux/mount.h>
29 #include <linux/version.h>
32 #include <net/af_ipn.h>
35 #include "ipn_netdev.h"
36 #include "ipn_msgbuf.h"
37 #include "ipn_chrdev.h"
39 MODULE_LICENSE("GPL");
40 MODULE_AUTHOR("VIEW-OS TEAM");
41 MODULE_DESCRIPTION("IPN Kernel Module");
43 #define IPN_MAX_PROTO 4
44 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25)
47 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,32)
50 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,37)
54 /*extension of RCV_SHUTDOWN defined in include/net/sock.h
55 * when the bit is set recv fails */
56 /* NO_OOB: do not send OOB */
57 #define RCV_SHUTDOWN_NO_OOB 4
58 /* EXTENDED MASK including OOB */
59 #define SHUTDOWN_XMASK (SHUTDOWN_MASK | RCV_SHUTDOWN_NO_OOB)
60 /* if XRCV_SHUTDOWN is all set recv fails */
61 #define XRCV_SHUTDOWN (RCV_SHUTDOWN | RCV_SHUTDOWN_NO_OOB)
63 /* Global MUTEX: this is locked to add/delete/modify networks
64 * this is *not* locked just to send/receive msgs */
66 static DECLARE_MUTEX(ipn_glob_mutex
);
68 static DEFINE_SEMAPHORE(ipn_glob_mutex
);
70 /* Network table and hash */
71 struct hlist_head ipn_network_table
[IPN_HASH_SIZE
+ 1];
72 /* slab(s) for fast data structure allocation */
73 static struct kmem_cache
*ipn_network_cache
;
74 static struct kmem_cache
*ipn_node_cache
;
75 static struct kmem_cache
*ipn_msgitem_cache
;
77 /* Protocol 1: HUB/Broadcast default protocol. Function Prototypes */
78 static int ipn_bcast_newport(struct ipn_node
*newport
);
79 static int ipn_bcast_handlemsg(struct ipn_node
*from
,
80 struct msgpool_item
*msgitem
);
82 /* default protocol IPN_BROADCAST (0) */
83 static struct ipn_protocol ipn_bcast
= {
85 .ipn_p_newport
=ipn_bcast_newport
,
86 .ipn_p_handlemsg
=ipn_bcast_handlemsg
};
88 static struct ipn_protocol
*ipn_protocol_table
[IPN_MAX_PROTO
]={&ipn_bcast
};
90 /* Socket call function prototypes */
91 static int ipn_release(struct socket
*);
92 static int ipn_bind(struct socket
*, struct sockaddr
*, int);
93 static int ipn_connect(struct socket
*, struct sockaddr
*,
94 int addr_len
, int flags
);
95 static int ipn_getname(struct socket
*, struct sockaddr
*, int *, int);
96 static unsigned int ipn_poll(struct file
*, struct socket
*, poll_table
*);
97 static int ipn_ioctl(struct socket
*, unsigned int, unsigned long);
98 static int ipn_shutdown(struct socket
*, int);
99 static int ipn_sendmsg(struct kiocb
*, struct socket
*,
100 struct msghdr
*, size_t);
101 static int ipn_recvmsg(struct kiocb
*, struct socket
*,
102 struct msghdr
*, size_t, int);
104 static int ipn_setsockopt(struct socket
*sock
, int level
, int optname
,
105 char __user
*optval
, unsigned int optlen
);
107 static int ipn_setsockopt(struct socket
*sock
, int level
, int optname
,
108 char __user
*optval
, int optlen
);
110 static int ipn_getsockopt(struct socket
*sock
, int level
, int optname
,
111 char __user
*optval
, int __user
*optlen
);
113 /* Network table Management
114 * inode->ipn_network hash table
115 * protected by ipn_glob_mutex */
116 static inline void ipn_insert_network(struct hlist_head
*list
, struct ipn_network
*ipnn
)
118 hlist_add_head(&ipnn
->hnode
, list
);
121 static inline void ipn_remove_network(struct ipn_network
*ipnn
)
123 hlist_del(&ipnn
->hnode
);
126 static struct ipn_network
*ipn_find_network_byinode(struct inode
*i
)
128 struct ipn_network
*ipnn
;
129 struct hlist_node
*node
;
131 hlist_for_each_entry(ipnn
, node
,
132 &ipn_network_table
[i
->i_ino
& (IPN_HASH_SIZE
- 1)], hnode
) {
133 struct dentry
*dentry
= ipnn
->dentry
;
135 if(dentry
&& dentry
->d_inode
== i
)
141 struct ipn_network
*ipn_find_network_byfun(
142 int (*fun
)(struct ipn_network
*,void *),void *funarg
)
144 struct ipn_network
*ipnn
;
145 struct hlist_node
*node
;
148 for (ipn_table_scan
=0;ipn_table_scan
<IPN_HASH_SIZE
;ipn_table_scan
++) {
149 hlist_for_each_entry(ipnn
, node
, &ipn_network_table
[ipn_table_scan
], hnode
) {
157 /* msgpool management
158 * msgpool_item are ipn_network dependent (each net has its own MTU)
159 * for each message sent there is one msgpool_item and many struct msgitem
160 * one for each receipient.
161 * msgitem are connected to the node's msgqueue or oobmsgqueue.
162 * when a message is delivered to a process the msgitem is deleted and
163 * the count of the msgpool_item is decreased.
164 * msgpool_item elements gets deleted automatically when count is 0*/
167 struct list_head list
;
168 struct msgpool_item
*msg
;
171 /* alloc a fresh msgpool item. count is set to 1.
174 * for each receipient
175 * enqueue messages to the process (using msgitem), ipn_msgpool_hold
177 * The message can be delivered concurrently. init count to 1 guarantees
178 * that it survives at least until is has been enqueued to all
180 /* msgpool_cache == NULL when the ipn network is using the flexible allocation,
181 * i.e. the msgpool item gets allocated by kmalloc instead of using slab */
182 static inline struct msgpool_item
*_ipn_msgpool_alloc(struct ipn_network
*ipnn
, int len
)
184 struct msgpool_item
*new;
185 if ((new=ipnn
->msgpool_cache
?
186 kmem_cache_alloc(ipnn
->msgpool_cache
,GFP_KERNEL
):
187 kmalloc(sizeof(struct msgpool_item
)+len
,GFP_KERNEL
)) != NULL
) {
188 atomic_set(&new->count
,1);
189 atomic_inc(&ipnn
->msgpool_nelem
);
194 /* ipn_msgpool_alloc returns null when leaky, lossless and no space is left
195 * for new msgpool items. When !leaky, it can allocate more msgpooli items,
196 * this temporary overassignment blocks new messages to be delivered until
197 * there is space for a msgpool item to be allocated */
198 struct msgpool_item
*ipn_msgpool_alloc(struct ipn_network
*ipnn
,int leaky
,int len
)
200 if (leaky
&& (ipnn
->flags
& IPN_FLAG_LOSSLESS
) &&
201 atomic_read(&ipnn
->msgpool_nelem
) < ipnn
->msgpool_size
)
204 return _ipn_msgpool_alloc(ipnn
,len
);
207 /* If the service il LOSSLESS, this msgpool call waits for an
208 * available msgpool item */
209 static inline struct msgpool_item
*ipn_msgpool_alloc_locking(struct ipn_network
*ipnn
,int len
)
211 if (ipnn
->flags
& IPN_FLAG_LOSSLESS
) {
212 while (atomic_read(&ipnn
->msgpool_nelem
) >= ipnn
->msgpool_size
) {
213 if (wait_event_interruptible_exclusive(ipnn
->send_wait
,
214 atomic_read(&ipnn
->msgpool_nelem
) < ipnn
->msgpool_size
))
218 return _ipn_msgpool_alloc(ipnn
,len
);
221 /* register one more user for this msgpool item (i.e. count++) */
222 static inline void ipn_msgpool_hold(struct msgpool_item
*msg
)
224 atomic_inc(&msg
->count
);
227 /* decrease count and delete msgpool_item if count == 0 */
228 void ipn_msgpool_put(struct msgpool_item
*old
,
229 struct ipn_network
*ipnn
)
231 if (atomic_dec_and_test(&old
->count
)) {
232 if (ipnn
->msgpool_cache
)
233 kmem_cache_free(ipnn
->msgpool_cache
,old
);
236 atomic_dec(&ipnn
->msgpool_nelem
);
237 if (ipnn
->flags
& IPN_FLAG_LOSSLESS
) /* this could be done anyway */
238 wake_up_interruptible(&ipnn
->send_wait
);
243 static const struct proto_ops ipn_ops
= {
245 .owner
= THIS_MODULE
,
246 .release
= ipn_release
,
248 .connect
= ipn_connect
,
249 .socketpair
= sock_no_socketpair
,
250 .accept
= sock_no_accept
,
251 .getname
= ipn_getname
,
254 .listen
= sock_no_listen
,
255 .shutdown
= ipn_shutdown
,
256 .setsockopt
= ipn_setsockopt
,
257 .getsockopt
= ipn_getsockopt
,
258 .sendmsg
= ipn_sendmsg
,
259 .recvmsg
= ipn_recvmsg
,
260 .mmap
= sock_no_mmap
,
261 .sendpage
= sock_no_sendpage
,
264 static struct proto ipn_proto
= {
266 .owner
= THIS_MODULE
,
267 .obj_size
= sizeof(struct ipn_sock
),
270 /* create a new ipn_node */
271 struct ipn_node
*ipn_node_create(struct net
*net
)
273 struct ipn_node
*ipn_node
;
274 ipn_node
=kmem_cache_alloc(ipn_node_cache
,GFP_KERNEL
);
275 if (ipn_node
!=NULL
) {
276 INIT_LIST_HEAD(&ipn_node
->nodelist
);
277 ipn_node
->protocol
=0; /* BROADCAST, caller must set a proper value */
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
;
283 ipn_node
->netdev
=NULL
;
285 ipn_node
->proto_private
=NULL
;
286 ipn_node
->totmsgcount
=0;
287 ipn_node
->oobmsgcount
=0;
288 spin_lock_init(&ipn_node
->msglock
);
289 INIT_LIST_HEAD(&ipn_node
->msgqueue
);
290 INIT_LIST_HEAD(&ipn_node
->oobmsgqueue
);
292 init_waitqueue_head(&ipn_node
->read_wait
);
299 * ipn_node is a separate structure, pointed by ipn_sock -> node
300 * when a node is "persistent", ipn_node survives while ipn_sock gets released*/
301 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
302 static int ipn_create(struct socket
*sock
, int protocol
)
303 #elif LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 33)
304 static int ipn_create(struct net
*net
,struct socket
*sock
, int protocol
)
306 static int ipn_create(struct net
*net
,struct socket
*sock
,
307 int protocol
, int kern
)
310 struct ipn_sock
*ipn_sk
;
312 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
315 if (net
!= &init_net
)
316 return -EAFNOSUPPORT
;
319 if (sock
->type
!= SOCK_RAW
)
324 protocol
=IPN_BROADCAST
-1;
325 if (protocol
< 0 || protocol
>= IPN_MAX_PROTO
||
326 ipn_protocol_table
[protocol
] == NULL
)
327 return -EPROTONOSUPPORT
;
328 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
329 ipn_sk
= (struct ipn_sock
*) sk_alloc(PF_IPN
, GFP_KERNEL
, &ipn_proto
, 1);
331 ipn_sk
= (struct ipn_sock
*) sk_alloc(net
, PF_IPN
, GFP_KERNEL
, &ipn_proto
);
336 if ((ipn_sk
->node
=ipn_node_create(net
))==NULL
) {
337 sock_put((struct sock
*) ipn_sk
);
340 ipn_sk
->node
->protocol
=protocol
;
341 sock_init_data(sock
,(struct sock
*) ipn_sk
);
342 sock
->state
= SS_UNCONNECTED
;
343 sock
->ops
= &ipn_ops
;
344 sock
->sk
=(struct sock
*)ipn_sk
;
348 /* update # of readers and # of writers counters for an ipn network.
349 * This function sends oob messages to nodes requesting the service */
350 /* LOCKING ipnn_mutex is locked */
351 static void ipn_net_update_counters(struct ipn_network
*ipnn
,
352 int chg_readers
, int chg_writers
) {
353 ipnn
->numreaders
+= chg_readers
;
354 ipnn
->numwriters
+= chg_writers
;
355 if (ipnn
->mtu
>= sizeof(struct numnode_oob
))
357 struct msgpool_item
*ipn_msg
=_ipn_msgpool_alloc(ipnn
,sizeof(struct numnode_oob
));
359 struct numnode_oob
*oob_msg
=(struct numnode_oob
*)(ipn_msg
->data
);
360 struct ipn_node
*ipn_node
;
361 ipn_msg
->len
=sizeof(struct numnode_oob
);
362 oob_msg
->level
=IPN_ANY
;
363 oob_msg
->tag
=IPN_OOB_NUMNODE_TAG
;
364 oob_msg
->numreaders
=ipnn
->numreaders
;
365 oob_msg
->numwriters
=ipnn
->numwriters
;
366 list_for_each_entry(ipn_node
, &ipnn
->connectqueue
, nodelist
) {
367 if (ipn_node
->flags
& IPN_NODEFLAG_OOB_NUMNODES
)
368 ipn_proto_oobsendmsg(ipn_node
,ipn_msg
);
370 ipn_msgpool_put(ipn_msg
,ipnn
);
375 /* flush pending messages (for close and shutdown RCV) */
376 /* LOCKING: ipnn_mutex is locked */
377 static void ipn_flush_recvqueue(struct ipn_node
*ipn_node
)
379 struct ipn_network
*ipnn
=ipn_node
->ipn
;
380 spin_lock(&ipn_node
->msglock
);
381 while (!list_empty(&ipn_node
->msgqueue
)) {
382 struct msgitem
*msgitem
=
383 list_first_entry(&ipn_node
->msgqueue
, struct msgitem
, list
);
384 list_del(&msgitem
->list
);
385 ipn_node
->totmsgcount
--;
386 ipn_msgpool_put(msgitem
->msg
,ipnn
);
387 kmem_cache_free(ipn_msgitem_cache
,msgitem
);
389 spin_unlock(&ipn_node
->msglock
);
392 /* flush pending oob messages (for socket close) */
393 /* LOCKING: ipnn_mutex is locked */
394 static void ipn_flush_oobrecvqueue(struct ipn_node
*ipn_node
)
396 struct ipn_network
*ipnn
=ipn_node
->ipn
;
397 spin_lock(&ipn_node
->msglock
);
398 while (!list_empty(&ipn_node
->oobmsgqueue
)) {
399 struct msgitem
*msgitem
=
400 list_first_entry(&ipn_node
->oobmsgqueue
, struct msgitem
, list
);
401 list_del(&msgitem
->list
);
402 ipn_node
->totmsgcount
--;
403 ipn_node
->oobmsgcount
--;
404 ipn_msgpool_put(msgitem
->msg
,ipnn
);
405 kmem_cache_free(ipn_msgitem_cache
,msgitem
);
407 spin_unlock(&ipn_node
->msglock
);
410 /* Terminate node. The node is "logically" terminated. */
411 /* LOCKING: ipn_glob_lock must be locked here */
412 static int ipn_terminate_node(struct ipn_node
*ipn_node
)
414 struct ipn_network
*ipnn
=ipn_node
->ipn
;
416 if (down_interruptible(&ipnn
->ipnn_mutex
))
418 if (ipn_node
->portno
>= 0) {
419 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_predelport(ipn_node
);
420 ipnn
->connport
[ipn_node
->portno
]=NULL
;
422 list_del(&ipn_node
->nodelist
);
423 ipn_flush_recvqueue(ipn_node
);
424 ipn_flush_oobrecvqueue(ipn_node
);
425 if (ipn_node
->portno
>= 0)
426 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_delport(ipn_node
);
428 ipn_net_update_counters(ipnn
,
429 (ipn_node
->shutdown
& RCV_SHUTDOWN
)?0:-1,
430 (ipn_node
->shutdown
& SEND_SHUTDOWN
)?0:-1);
431 ipn_node
->shutdown
= SHUTDOWN_XMASK
;
432 up(&ipnn
->ipnn_mutex
);
433 if (ipn_node
->netdev
)
434 ipn_netdev_close(ipn_node
);
435 /* No more network elements */
437 if (ipnn
->refcnt
== 0 && !ipn_is_persistent_chrdev(ipnn
))
440 ipn_deregister_chrdev(ipnn
);
441 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_delnet(ipnn
);
442 ipn_remove_network(ipnn
);
443 ipn_protocol_table
[ipnn
->protocol
]->refcnt
--;
448 if (ipnn
->msgpool_cache
)
449 ipn_msgbuf_put(ipnn
->msgpool_cache
);
451 kfree(ipnn
->connport
);
452 kmem_cache_free(ipn_network_cache
, ipnn
);
453 module_put(THIS_MODULE
);
457 kfree(ipn_node
->pbp
);
463 /* release of an ipn_node */
464 int ipn_node_release(struct ipn_node
*ipn_node
)
467 if (down_interruptible(&ipn_glob_mutex
))
469 if (ipn_node
->flags
& IPN_NODEFLAG_PERSIST
) {
470 ipn_node
->flags
&= ~IPN_NODEFLAG_INUSE
;
474 rv
=ipn_terminate_node(ipn_node
);
478 kmem_cache_free(ipn_node_cache
,ipn_node
);
484 /* release of an ipn socket */
485 static int ipn_release (struct socket
*sock
)
487 struct ipn_sock
*ipn_sk
=(struct ipn_sock
*)sock
->sk
;
488 struct ipn_node
*ipn_node
=ipn_sk
->node
;
489 int rv
=ipn_node_release(ipn_node
);
491 sock_put((struct sock
*) ipn_sk
);
495 /* _set persist, change the persistence of a node,
496 * when persistence gets cleared and the node is no longer used
497 * the node is terminated and freed.
498 * ipn_glob_mutex must be locked */
499 static int _ipn_setpersist(struct ipn_node
*ipn_node
, int persist
)
503 ipn_node
->flags
|= IPN_NODEFLAG_PERSIST
;
505 ipn_node
->flags
&= ~IPN_NODEFLAG_PERSIST
;
506 if (!(ipn_node
->flags
& IPN_NODEFLAG_INUSE
)) {
507 rv
=ipn_terminate_node(ipn_node
);
509 kmem_cache_free(ipn_node_cache
,ipn_node
);
516 * lock ipn_glob_mutex and call __ipn_setpersist above */
517 static int ipn_setpersist(struct ipn_node
*ipn_node
, int persist
)
520 if (ipn_node
->netdev
== NULL
)
522 if (down_interruptible(&ipn_glob_mutex
))
524 rv
=_ipn_setpersist(ipn_node
,persist
);
529 /* several network parameters can be set by setsockopt prior to bind */
530 /* struct pre_bind_parms is a temporary stucture connected to ipn_node->pbp
531 * to keep the parameter values. */
532 struct pre_bind_parms
{
533 unsigned short maxports
;
534 unsigned short flags
;
535 unsigned short msgpoolsize
;
540 /* STD_PARMS: BITS_PER_LONG nodes, no flags, BITS_PER_BYTE pending msgs,
541 * Ethernet + VLAN MTU*/
542 #define STD_BIND_PARMS {BITS_PER_LONG, 0, BITS_PER_BYTE, 1514, 0777};
544 static int ipn_mkname(struct sockaddr_un
* sunaddr
, int len
)
546 if (len
<= sizeof(short) || len
> sizeof(*sunaddr
))
548 if (!sunaddr
|| sunaddr
->sun_family
!= AF_IPN
)
551 * This may look like an off by one error but it is a bit more
552 * subtle. 108 is the longest valid AF_IPN path for a binding.
553 * sun_path[108] doesnt as such exist. However in kernel space
554 * we are guaranteed that it is a valid memory location in our
555 * kernel address buffer.
557 ((char *)sunaddr
)[len
]=0;
558 len
= strlen(sunaddr
->sun_path
)+1+sizeof(short);
562 static int ipn_node_bind(struct ipn_node
*ipn_node
, struct ipn_network
*ipnn
)
566 if (ipn_node
->ipn
!= NULL
)
569 list_add_tail(&ipn_node
->nodelist
,&ipnn
->unconnectqueue
);
571 ipn_node
->flags
|= IPN_NODEFLAG_BOUND
;
576 static int ipn_bind(struct socket
*sock
, struct sockaddr
*uaddr
, int addr_len
)
578 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)uaddr
;
579 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
581 struct ipn_network
*ipnn
;
582 struct dentry
* dentry
= NULL
;
584 struct pre_bind_parms parms
=STD_BIND_PARMS
;
586 //printk("IPN bind\n");
588 if (down_interruptible(&ipn_glob_mutex
))
590 if (sock
->state
!= SS_UNCONNECTED
||
591 ipn_node
->ipn
!= NULL
) {
596 if (ipn_node
->protocol
>= 0 &&
597 (ipn_node
->protocol
>= IPN_MAX_PROTO
||
598 ipn_protocol_table
[ipn_node
->protocol
] == NULL
)) {
599 err
= -EPROTONOSUPPORT
;
603 addr_len
= ipn_mkname(sunaddr
, addr_len
);
609 /* check if there is already an ipn-network socket with that name */
610 err
= path_lookup(sunaddr
->sun_path
, LOOKUP_FOLLOW
, &nd
);
611 if (err
) { /* it does not exist, NEW IPN socket! */
613 /* Is it everything okay with the parent? */
614 err
= path_lookup(sunaddr
->sun_path
, LOOKUP_PARENT
, &nd
);
616 goto out_mknod_parent
;
617 /* Do I have the permission to create a file? */
618 dentry
= lookup_create(&nd
, 0);
619 err
= PTR_ERR(dentry
);
621 goto out_mknod_unlock
;
623 * All right, let's create it.
626 mode
= ipn_node
->pbp
->mode
;
628 mode
= SOCK_INODE(sock
)->i_mode
;
629 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 30)
630 mode
= S_IFSOCK
| (mode
& ~current
->fs
->umask
);
632 mode
= S_IFSOCK
| (mode
& ~current_umask());
636 err
= vfs_mknod(nd
.path
.dentry
->d_inode
, dentry
, nd
.path
.mnt
, mode
, 0);
638 err
= vfs_mknod(nd
.path
.dentry
->d_inode
, dentry
, mode
, 0);
642 err
= vfs_mknod(nd
.dentry
->d_inode
, dentry
, nd
.path
.mnt
, mode
, 0);
644 err
= vfs_mknod(nd
.dentry
->d_inode
, dentry
, mode
, 0);
650 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
651 dput(nd
.path
.dentry
);
652 nd
.path
.dentry
= dentry
;
654 mutex_unlock(&nd
.dentry
->d_inode
->i_mutex
);
658 /* create a new ipn_network item */
660 parms
=*ipn_node
->pbp
;
661 ipnn
=kmem_cache_zalloc(ipn_network_cache
,GFP_KERNEL
);
664 goto out_mknod_dput_ipnn
;
666 ipnn
->connport
=kzalloc(parms
.maxports
* sizeof(struct ipn_node
*),GFP_KERNEL
);
667 if (!ipnn
->connport
) {
669 goto out_mknod_dput_ipnn2
;
672 /* module refcnt is incremented for each network, thus
673 * rmmod is forbidden if there are persistent nodes */
674 if (!try_module_get(THIS_MODULE
)) {
676 goto out_mknod_dput_ipnn2
;
678 memcpy(&ipnn
->sunaddr
,sunaddr
,addr_len
);
680 ipnn
->flags
=parms
.flags
;
681 if (ipnn
->flags
& IPN_FLAG_FLEXMTU
)
682 ipnn
->msgpool_cache
= NULL
;
684 ipnn
->msgpool_cache
=ipn_msgbuf_get(ipnn
->mtu
);
685 if (!ipnn
->msgpool_cache
) {
687 goto out_mknod_dput_putmodule
;
690 INIT_LIST_HEAD(&ipnn
->unconnectqueue
);
691 INIT_LIST_HEAD(&ipnn
->connectqueue
);
694 ipnn
->dentry
=nd
.path
.dentry
;
695 ipnn
->mnt
=nd
.path
.mnt
;
697 ipnn
->dentry
=nd
.dentry
;
700 sema_init(&ipnn
->ipnn_mutex
,1);
701 ipnn
->sunaddr_len
=addr_len
;
702 ipnn
->protocol
=ipn_node
->protocol
;
703 if (ipnn
->protocol
< 0) ipnn
->protocol
= 0;
704 ipn_protocol_table
[ipnn
->protocol
]->refcnt
++;
707 ipnn
->maxports
=parms
.maxports
;
708 atomic_set(&ipnn
->msgpool_nelem
,0);
709 ipnn
->msgpool_size
=parms
.msgpoolsize
;
710 ipnn
->proto_private
=NULL
;
711 init_waitqueue_head(&ipnn
->send_wait
);
713 err
=ipn_protocol_table
[ipnn
->protocol
]->ipn_p_newnet(ipnn
);
715 goto out_mknod_dput_putmodule
;
717 ipn_insert_network(&ipn_network_table
[nd
.path
.dentry
->d_inode
->i_ino
& (IPN_HASH_SIZE
-1)],ipnn
);
719 ipn_insert_network(&ipn_network_table
[nd
.dentry
->d_inode
->i_ino
& (IPN_HASH_SIZE
-1)],ipnn
);
722 /* join an existing network */
723 if (parms
.flags
& IPN_FLAG_EXCL
) {
727 err
= inode_permission(nd
.path
.dentry
->d_inode
, MAY_EXEC
);
732 if (!S_ISSOCK(nd
.path
.dentry
->d_inode
->i_mode
))
734 ipnn
=ipn_find_network_byinode(nd
.path
.dentry
->d_inode
);
736 if (!S_ISSOCK(nd
.dentry
->d_inode
->i_mode
))
738 ipnn
=ipn_find_network_byinode(nd
.dentry
->d_inode
);
740 if (!ipnn
|| (ipnn
->flags
& IPN_FLAG_TERMINATED
) ||
741 (ipnn
->flags
& IPN_FLAG_EXCL
))
745 kfree(ipn_node
->pbp
);
748 ipn_node_bind(ipn_node
,ipnn
);
762 out_mknod_dput_putmodule
:
763 module_put(THIS_MODULE
);
764 out_mknod_dput_ipnn2
:
765 kfree(ipnn
->connport
);
767 kmem_cache_free(ipn_network_cache
,ipnn
);
772 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
775 mutex_unlock(&nd
.dentry
->d_inode
->i_mutex
);
786 static int ipn_connect(struct socket
*sock
, struct sockaddr
*addr
,
787 int addr_len
, int flags
){
788 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)addr
;
789 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
791 struct ipn_network
*ipnn
,*previousipnn
;
795 /* the socket cannot be connected twice */
796 if (sock
->state
!= SS_UNCONNECTED
)
799 if (down_interruptible(&ipn_glob_mutex
))
802 if ((previousipnn
=ipn_node
->ipn
) == NULL
) { /* unbound */
803 unsigned char mustshutdown
=0;
804 err
= ipn_mkname(sunaddr
, addr_len
);
808 err
= path_lookup(sunaddr
->sun_path
, LOOKUP_FOLLOW
, &nd
);
811 err
= inode_permission(nd
.path
.dentry
->d_inode
, MAY_READ
);
813 if (err
== -EACCES
|| err
== -EROFS
)
814 mustshutdown
|=RCV_SHUTDOWN
;
818 err
= inode_permission(nd
.path
.dentry
->d_inode
, MAY_WRITE
);
821 mustshutdown
|=SEND_SHUTDOWN
;
825 mustshutdown
|= ipn_node
->shutdown
;
826 /* if the combination of shutdown and permissions leaves
827 * no abilities, connect returns EACCES */
828 if (mustshutdown
== SHUTDOWN_XMASK
) {
833 ipn_node
->shutdown
=mustshutdown
;
836 if (!S_ISSOCK(nd
.path
.dentry
->d_inode
->i_mode
)) {
840 ipnn
=ipn_find_network_byinode(nd
.path
.dentry
->d_inode
);
842 if (!S_ISSOCK(nd
.dentry
->d_inode
->i_mode
)) {
846 ipnn
=ipn_find_network_byinode(nd
.dentry
->d_inode
);
848 if (!ipnn
|| (ipnn
->flags
& IPN_FLAG_TERMINATED
)) {
852 if (ipn_node
->protocol
== IPN_ANY
)
853 ipn_node
->protocol
=ipnn
->protocol
;
854 else if (ipnn
->protocol
!= ipn_node
->protocol
) {
867 if (down_interruptible(&ipnn
->ipnn_mutex
)) {
871 portno
= ipn_protocol_table
[ipnn
->protocol
]->ipn_p_newport(ipn_node
);
872 if (portno
>= 0 && portno
<ipnn
->maxports
) {
873 sock
->state
= SS_CONNECTED
;
874 ipn_node
->portno
=portno
;
875 ipnn
->connport
[portno
]=ipn_node
;
876 if (!(ipn_node
->flags
& IPN_NODEFLAG_BOUND
))
879 list_del(&ipn_node
->nodelist
);
880 list_add_tail(&ipn_node
->nodelist
,&ipnn
->connectqueue
);
881 ipn_net_update_counters(ipnn
,
882 (ipn_node
->shutdown
& RCV_SHUTDOWN
)?0:1,
883 (ipn_node
->shutdown
& SEND_SHUTDOWN
)?0:1);
885 ipn_node
->ipn
=previousipnn
; /* undo changes on ipn_node->ipn */
888 up(&ipnn
->ipnn_mutex
);
903 int ipn_node_create_connect(struct ipn_node
**ipn_node_out
,
904 struct ipn_network
*(* ipnn_map
)(void *),void *ipnn_map_arg
) {
905 struct ipn_node
*ipn_node
;
906 struct ipn_network
*ipnn
;
909 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
910 ipn_node
=ipn_node_create(NULL
);
912 ipn_node
=ipn_node_create(&init_net
);
914 if (down_interruptible(&ipn_glob_mutex
)) {
916 goto err_ipn_node_release
;
918 ipnn
=ipnn_map(ipnn_map_arg
);
920 ipn_node
->protocol
=ipnn
->protocol
;
921 if (down_interruptible(&ipnn
->ipnn_mutex
)) {
925 portno
= ipn_protocol_table
[ipnn
->protocol
]->ipn_p_newport(ipn_node
);
926 if (portno
>= 0 && portno
<ipnn
->maxports
) {
927 ipn_node
->portno
=portno
;
928 ipnn
->connport
[portno
]=ipn_node
;
929 if (!(ipn_node
->flags
& IPN_NODEFLAG_BOUND
)) {
931 list_del(&ipn_node
->nodelist
);
933 *ipn_node_out
=ipn_node
;
934 list_add_tail(&ipn_node
->nodelist
,&ipnn
->connectqueue
);
935 ipn_net_update_counters(ipnn
,
936 (ipn_node
->shutdown
& RCV_SHUTDOWN
)?0:1,
937 (ipn_node
->shutdown
& SEND_SHUTDOWN
)?0:1);
944 up(&ipnn
->ipnn_mutex
);
948 up(&ipnn
->ipnn_mutex
);
951 err_ipn_node_release
:
952 ipn_node_release(ipn_node
);
957 int ipn_node_connect(struct ipn_node
*ipn_node
)
959 struct ipn_network
*ipnn
;
962 if (down_interruptible(&ipn_glob_mutex
))
966 if (down_interruptible(&ipnn
->ipnn_mutex
)) {
970 portno
= ipn_protocol_table
[ipnn
->protocol
]->ipn_p_newport(ipn_node
);
971 if (portno
>= 0 && portno
<ipnn
->maxports
) {
972 ipn_node
->portno
=portno
;
973 ipnn
->connport
[portno
]=ipn_node
;
974 if (!(ipn_node
->flags
& IPN_NODEFLAG_BOUND
)) {
976 list_del(&ipn_node
->nodelist
);
978 list_add_tail(&ipn_node
->nodelist
,&ipnn
->connectqueue
);
979 ipn_net_update_counters(ipnn
,
980 (ipn_node
->shutdown
& RCV_SHUTDOWN
)?0:1,
981 (ipn_node
->shutdown
& SEND_SHUTDOWN
)?0:1);
987 up(&ipnn
->ipnn_mutex
);
995 static int ipn_getname(struct socket
*sock
, struct sockaddr
*uaddr
,
996 int *uaddr_len
, int peer
) {
997 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
998 struct ipn_network
*ipnn
=ipn_node
->ipn
;
999 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)uaddr
;
1002 if (down_interruptible(&ipn_glob_mutex
))
1003 return -ERESTARTSYS
;
1005 *uaddr_len
= ipnn
->sunaddr_len
;
1006 memcpy(sunaddr
,&ipnn
->sunaddr
,*uaddr_len
);
1009 up(&ipn_glob_mutex
);
1014 unsigned int ipn_node_poll(struct ipn_node
*ipn_node
, struct file
*file
, poll_table
*wait
) {
1015 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1016 unsigned int mask
=0;
1019 poll_wait(file
,&ipn_node
->read_wait
,wait
);
1020 if (ipnn
->flags
& IPN_FLAG_LOSSLESS
)
1021 poll_wait(file
,&ipnn
->send_wait
,wait
);
1022 /* POLLIN if recv succeeds,
1023 * POLL{PRI,RDNORM} if there are {oob,non-oob} messages */
1024 if (ipn_node
->totmsgcount
> 0) mask
|= POLLIN
;
1025 if (!(list_empty(&ipn_node
->msgqueue
))) mask
|= POLLRDNORM
;
1026 if (!(list_empty(&ipn_node
->oobmsgqueue
))) mask
|= POLLPRI
;
1027 if ((!(ipnn
->flags
& IPN_FLAG_LOSSLESS
)) |
1028 (atomic_read(&ipnn
->msgpool_nelem
) < ipnn
->msgpool_size
))
1029 mask
|= POLLOUT
| POLLWRNORM
;
1034 static unsigned int ipn_poll(struct file
*file
, struct socket
*sock
,
1036 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1037 return ipn_node_poll(ipn_node
, file
, wait
);
1040 /* connect netdev (from ioctl). connect a bound socket to a
1041 * network device TAP or GRAB */
1042 static int ipn_connect_netdev(struct socket
*sock
,struct ifreq
*ifr
)
1045 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1046 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1047 if (!capable(CAP_NET_ADMIN
))
1049 if (sock
->state
!= SS_UNCONNECTED
)
1052 return -ENOTCONN
; /* Maybe we need a different error for "NOT BOUND" */
1053 if (down_interruptible(&ipn_glob_mutex
))
1054 return -ERESTARTSYS
;
1055 if (down_interruptible(&ipnn
->ipnn_mutex
)) {
1056 up(&ipn_glob_mutex
);
1057 return -ERESTARTSYS
;
1059 ipn_node
->netdev
=ipn_netdev_alloc(ipn_node
->net
,ifr
->ifr_flags
,ifr
->ifr_name
,&err
);
1060 if (ipn_node
->netdev
) {
1062 portno
= ipn_protocol_table
[ipnn
->protocol
]->ipn_p_newport(ipn_node
);
1063 if (portno
>= 0 && portno
<ipnn
->maxports
) {
1064 sock
->state
= SS_CONNECTED
;
1065 ipn_node
->portno
=portno
;
1066 ipn_node
->flags
|= ifr
->ifr_flags
& IPN_NODEFLAG_DEVMASK
;
1067 ipnn
->connport
[portno
]=ipn_node
;
1068 err
=ipn_netdev_activate(ipn_node
);
1070 sock
->state
= SS_UNCONNECTED
;
1071 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_delport(ipn_node
);
1072 ipn_node
->netdev
=NULL
;
1073 ipn_node
->portno
= -1;
1074 ipn_node
->flags
&= ~IPN_NODEFLAG_DEVMASK
;
1075 ipnn
->connport
[portno
]=NULL
;
1077 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_postnewport(ipn_node
);
1078 list_del(&ipn_node
->nodelist
);
1079 list_add_tail(&ipn_node
->nodelist
,&ipnn
->connectqueue
);
1082 ipn_netdev_close(ipn_node
);
1084 ipn_node
->netdev
=NULL
;
1088 up(&ipnn
->ipnn_mutex
);
1089 up(&ipn_glob_mutex
);
1093 /* join a netdev, a socket gets connected to a persistent node
1094 * not connected to another socket */
1095 static int ipn_join_netdev(struct socket
*sock
,struct ifreq
*ifr
)
1098 struct net_device
*dev
;
1099 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1100 struct ipn_node
*ipn_joined
;
1101 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1102 if (sock
->state
!= SS_UNCONNECTED
)
1104 if (down_interruptible(&ipn_glob_mutex
))
1105 return -ERESTARTSYS
;
1106 if (down_interruptible(&ipnn
->ipnn_mutex
)) {
1107 up(&ipn_glob_mutex
);
1108 return -ERESTARTSYS
;
1110 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
1111 dev
=__dev_get_by_name(ifr
->ifr_name
);
1113 dev
=__dev_get_by_name(ipn_node
->net
,ifr
->ifr_name
);
1116 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
1117 dev
=__dev_get_by_index(ifr
->ifr_ifindex
);
1119 dev
=__dev_get_by_index(ipn_node
->net
,ifr
->ifr_ifindex
);
1121 if (dev
&& (ipn_joined
=ipn_netdev2node(dev
)) != NULL
) { /* the interface does exist */
1123 for (i
=0;i
<ipnn
->maxports
&& ipn_joined
!= ipnn
->connport
[i
] ;i
++)
1125 if (i
< ipnn
->maxports
) { /* found */
1126 /* ipn_joined is substituted to ipn_node */
1127 ((struct ipn_sock
*)sock
->sk
)->node
=ipn_joined
;
1128 ipn_joined
->flags
|= IPN_NODEFLAG_INUSE
;
1130 kmem_cache_free(ipn_node_cache
,ipn_node
);
1135 up(&ipnn
->ipnn_mutex
);
1136 up(&ipn_glob_mutex
);
1140 /* set persistence of a node looking for it by interface name
1141 * (it is for sysadm, to close network interfaces)*/
1142 static int ipn_setpersist_netdev(struct ifreq
*ifr
, int value
)
1144 struct net_device
*dev
;
1145 struct ipn_node
*ipn_node
;
1147 if (!capable(CAP_NET_ADMIN
))
1149 if (down_interruptible(&ipn_glob_mutex
))
1150 return -ERESTARTSYS
;
1151 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
1152 dev
=__dev_get_by_name(ifr
->ifr_name
);
1154 dev
=__dev_get_by_name(&init_net
,ifr
->ifr_name
);
1157 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
1158 dev
=__dev_get_by_index(ifr
->ifr_ifindex
);
1160 dev
=__dev_get_by_index(&init_net
,ifr
->ifr_ifindex
);
1162 if (dev
&& (ipn_node
=ipn_netdev2node(dev
)) != NULL
)
1163 _ipn_setpersist(ipn_node
,value
);
1166 up(&ipn_glob_mutex
);
1172 int ipn_node_ioctl(struct ipn_node
*ipn_node
, unsigned int cmd
, unsigned long arg
) {
1173 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1175 (ipn_protocol_table
[ipn_node
->protocol
]->ipn_p_ioctl
!= NULL
)) {
1177 if (down_interruptible(&ipnn
->ipnn_mutex
))
1178 return -ERESTARTSYS
;
1179 rv
=ipn_protocol_table
[ipn_node
->protocol
]->ipn_p_ioctl(ipn_node
,cmd
,arg
);
1180 up(&ipnn
->ipnn_mutex
);
1186 static int ipn_ioctl(struct socket
*sock
, unsigned int cmd
, unsigned long arg
) {
1187 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1188 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1189 void __user
* argp
= (void __user
*)arg
;
1191 struct chrdevreq devr
;
1193 if (ipn_node
->shutdown
== SHUTDOWN_XMASK
)
1200 case IPN_SETPERSIST_NETDEV
:
1201 case IPN_CLRPERSIST_NETDEV
:
1202 case IPN_CONN_NETDEV
:
1203 case IPN_JOIN_NETDEV
:
1205 if (copy_from_user(&ifr
, argp
, sizeof(ifr
)))
1207 ifr
.ifr_name
[IFNAMSIZ
-1] = '\0';
1209 case IPN_REGISTER_CHRDEV
:
1210 case IPN_JOIN_CHRDEV
:
1211 if (copy_from_user(&devr
, argp
, sizeof(devr
)))
1213 /*printk("IPN_REGISTER_CHRDEV %p %d %d %d\n",
1214 argp, sizeof(devr),devr.major, devr.minor);*/
1218 /* actions for unconnected and unbound sockets */
1220 case IPN_SETPERSIST_NETDEV
:
1221 return ipn_setpersist_netdev(&ifr
,1);
1222 case IPN_CLRPERSIST_NETDEV
:
1223 return ipn_setpersist_netdev(&ifr
,0);
1224 case IPN_JOIN_CHRDEV
:
1227 if (!capable(CAP_MKNOD
))
1229 if (ipn_node
->ipn
!= NULL
)
1231 if (down_interruptible(&ipn_glob_mutex
))
1232 return -ERESTARTSYS
;
1233 rv
=ipn_node_bind(ipn_node
, ipn_find_chrdev(&devr
));
1234 up(&ipn_glob_mutex
);
1238 if (capable(CAP_NET_ADMIN
))
1240 if (ipn_node
->netdev
&& (ipn_node
->flags
&IPN_NODEFLAG_TAP
))
1241 return dev_set_mac_address(ipn_node
->netdev
, &ifr
.ifr_hwaddr
);
1243 return -EADDRNOTAVAIL
;
1245 if (ipnn
== NULL
|| (ipnn
->flags
& IPN_FLAG_TERMINATED
))
1247 /* actions for connected or bound sockets */
1249 case IPN_CONN_NETDEV
:
1250 return ipn_connect_netdev(sock
,&ifr
);
1251 case IPN_JOIN_NETDEV
:
1252 return ipn_join_netdev(sock
,&ifr
);
1253 case IPN_SETPERSIST
:
1254 return ipn_setpersist(ipn_node
,arg
);
1255 case IPN_CHRDEV_PERSIST
:
1256 return ipn_chrdev_persistence(ipnn
,arg
);
1257 case IPN_REGISTER_CHRDEV
:
1260 unsigned int reqmajor
=devr
.major
;
1261 if (down_interruptible(&ipnn
->ipnn_mutex
))
1262 return -ERESTARTSYS
;
1263 rv
=ipn_register_chrdev(ipnn
,&devr
);
1264 if (reqmajor
==0 && rv
==0) {
1265 if (copy_to_user(argp
, &devr
, sizeof devr
))
1268 up(&ipnn
->ipnn_mutex
);
1271 case IPN_UNREGISTER_CHRDEV
:
1274 if (down_interruptible(&ipnn
->ipnn_mutex
))
1275 return -ERESTARTSYS
;
1276 rv
=ipn_deregister_chrdev(ipnn
);
1277 up(&ipnn
->ipnn_mutex
);
1281 return ipn_node_ioctl(ipn_node
, cmd
, arg
);
1285 /* shutdown: close socket for input or for output.
1286 * shutdown can be called prior to connect and it is not reversible */
1287 static int ipn_shutdown(struct socket
*sock
, int mode
) {
1288 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1289 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1290 int oldshutdown
=ipn_node
->shutdown
;
1291 mode
= (mode
+1)&(RCV_SHUTDOWN
|SEND_SHUTDOWN
);
1293 /* define the new mode first... */
1294 ipn_node
->shutdown
|= mode
;
1297 /* ... and wait for all pending ops to be completed */
1298 if (down_interruptible(&ipnn
->ipnn_mutex
)) {
1299 ipn_node
->shutdown
= oldshutdown
;
1300 return -ERESTARTSYS
;
1302 oldshutdown
=ipn_node
->shutdown
-oldshutdown
;
1303 if (sock
->state
== SS_CONNECTED
&& oldshutdown
) {
1304 ipn_net_update_counters(ipnn
,
1305 (ipn_node
->shutdown
& RCV_SHUTDOWN
)?0:-1,
1306 (ipn_node
->shutdown
& SEND_SHUTDOWN
)?0:-1);
1309 /* if recv channel has been shut down, flush the recv queue */
1310 if ((ipn_node
->shutdown
& RCV_SHUTDOWN
))
1311 ipn_flush_recvqueue(ipn_node
);
1312 up(&ipnn
->ipnn_mutex
);
1317 /* injectmsg: a new message is entering the ipn network.
1318 * injectmsg gets called by send and by the grab/tap node */
1319 /* handlemsg is protected from ipn_network changes: ipnn->ipnn_mutex is locked */
1320 int ipn_proto_injectmsg(struct ipn_node
*from
, struct msgpool_item
*msg
)
1322 struct ipn_network
*ipnn
=from
->ipn
;
1324 static int recinject
=0;
1325 //printk("INJECTMSG IN\n");
1327 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_handlemsg(from
, msg
);
1328 else if (down_interruptible(&ipnn
->ipnn_mutex
))
1332 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_handlemsg(from
, msg
);
1334 up(&ipnn
->ipnn_mutex
);
1336 //printk("INJECTMSG OUT %d\n",err);
1341 int ipn_node_write(struct ipn_node
*ipn_node
, struct iovec
*msg_iov
, int len
) {
1342 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1343 struct msgpool_item
*newmsg
;
1346 if (unlikely(ipn_node
->shutdown
& SEND_SHUTDOWN
)) {
1347 if (ipn_node
->shutdown
== SHUTDOWN_XMASK
)
1352 if (len
> ipnn
->mtu
)
1354 newmsg
=ipn_msgpool_alloc_locking(ipnn
,len
);
1358 err
=memcpy_fromiovec(newmsg
->data
, msg_iov
, len
);
1360 ipn_proto_injectmsg(ipn_node
, newmsg
);
1361 ipn_msgpool_put(newmsg
,ipnn
);
1365 static int ipn_sendmsg(struct kiocb
*kiocb
, struct socket
*sock
,
1366 struct msghdr
*msg
, size_t len
) {
1367 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1369 if (unlikely(sock
->state
!= SS_CONNECTED
))
1372 return ipn_node_write(ipn_node
, msg
->msg_iov
, len
);
1375 /* ipn_proto_sendmsg is called by protocol implementation to enqueue a
1376 * for a destination (to).*/
1377 /* enqueue a msgitem on the receiver's queue, the queue is protected by a
1378 * spinlock (msglock) */
1379 void ipn_proto_sendmsg(struct ipn_node
*to
, struct msgpool_item
*msg
)
1383 ipn_netdev_sendmsg(to
,msg
);
1386 struct msgitem
*msgitem
;
1387 struct ipn_network
*ipnn
=to
->ipn
;
1388 spin_lock(&to
->msglock
);
1389 if (likely((to
->shutdown
& RCV_SHUTDOWN
)==0)) {
1390 /*if (unlikely((ipnn->flags & IPN_FLAG_LOSSLESS) == 0 ||
1391 to->totmsgcount >= ipnn->msgpool_size))
1393 if (likely(ipnn
->flags
& IPN_FLAG_LOSSLESS
||
1394 to
->totmsgcount
< ipnn
->msgpool_size
)) {
1395 if ((msgitem
=kmem_cache_alloc(ipn_msgitem_cache
,GFP_KERNEL
))!=NULL
) {
1398 list_add_tail(&msgitem
->list
, &to
->msgqueue
);
1399 ipn_msgpool_hold(msg
);
1403 spin_unlock(&to
->msglock
);
1404 wake_up_interruptible(&to
->read_wait
);
1409 /* enqueue an oob message. "to" is the destination */
1410 void ipn_proto_oobsendmsg(struct ipn_node
*to
, struct msgpool_item
*msg
)
1413 if (!to
->netdev
) { /* no oob to netdev */
1414 struct msgitem
*msgitem
;
1415 struct ipn_network
*ipnn
=to
->ipn
;
1416 spin_lock(&to
->msglock
);
1417 if ((to
->shutdown
& RCV_SHUTDOWN_NO_OOB
) == 0 &&
1418 (ipnn
->flags
& IPN_FLAG_LOSSLESS
||
1419 to
->oobmsgcount
< ipnn
->msgpool_size
)) {
1420 if ((msgitem
=kmem_cache_alloc(ipn_msgitem_cache
,GFP_KERNEL
))!=NULL
) {
1424 list_add_tail(&msgitem
->list
, &to
->oobmsgqueue
);
1425 ipn_msgpool_hold(msg
);
1428 spin_unlock(&to
->msglock
);
1429 wake_up_interruptible(&to
->read_wait
);
1435 int ipn_node_read(struct ipn_node
*ipn_node
, struct iovec
*msg_iov
, size_t len
, int *msg_flags
, int flags
) {
1436 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1437 struct msgitem
*msgitem
;
1438 struct msgpool_item
*currmsg
;
1440 if (unlikely((ipn_node
->shutdown
& XRCV_SHUTDOWN
) == XRCV_SHUTDOWN
)) {
1441 if (ipn_node
->shutdown
== SHUTDOWN_XMASK
) /*EOF, nothing can be read*/
1444 return -EPIPE
; /*trying to read on a write only node */
1447 /* wait for a message */
1448 spin_lock(&ipn_node
->msglock
);
1449 while (ipn_node
->totmsgcount
== 0) {
1450 spin_unlock(&ipn_node
->msglock
);
1451 if (wait_event_interruptible(ipn_node
->read_wait
,
1452 !(ipn_node
->totmsgcount
== 0)))
1453 return -ERESTARTSYS
;
1454 spin_lock(&ipn_node
->msglock
);
1456 /* oob gets delivered first. oob are rare */
1457 if (likely(list_empty(&ipn_node
->oobmsgqueue
)))
1458 msgitem
=list_first_entry(&ipn_node
->msgqueue
, struct msgitem
, list
);
1460 msgitem
=list_first_entry(&ipn_node
->oobmsgqueue
, struct msgitem
, list
);
1461 *msg_flags
|= MSG_OOB
;
1462 ipn_node
->oobmsgcount
--;
1464 list_del(&msgitem
->list
);
1465 ipn_node
->totmsgcount
--;
1466 spin_unlock(&ipn_node
->msglock
);
1467 currmsg
=msgitem
->msg
;
1468 if (currmsg
->len
< len
)
1470 memcpy_toiovec(msg_iov
, currmsg
->data
, len
);
1471 ipn_msgpool_put(currmsg
,ipnn
);
1472 kmem_cache_free(ipn_msgitem_cache
,msgitem
);
1476 static int ipn_recvmsg(struct kiocb
*kiocb
, struct socket
*sock
,
1477 struct msghdr
*msg
, size_t len
, int flags
) {
1478 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1480 if (unlikely(sock
->state
!= SS_CONNECTED
))
1483 return ipn_node_read(ipn_node
, msg
->msg_iov
, len
, &msg
->msg_flags
, flags
);
1486 /* resize a network: change the # of communication ports (connport) */
1487 static int ipn_netresize(struct ipn_network
*ipnn
,int newsize
)
1490 struct ipn_node
**newconnport
;
1491 struct ipn_node
**oldconnport
;
1493 if (down_interruptible(&ipnn
->ipnn_mutex
))
1494 return -ERESTARTSYS
;
1495 oldsize
=ipnn
->maxports
;
1496 if (newsize
== oldsize
) {
1497 up(&ipnn
->ipnn_mutex
);
1501 /* shrink a network. all the ports we are going to eliminate
1502 * must be unused! */
1503 if (newsize
< oldsize
) {
1505 for (i
=newsize
; i
<oldsize
; i
++)
1506 if (ipnn
->connport
[i
]) {
1507 up(&ipnn
->ipnn_mutex
);
1512 oldconnport
=ipnn
->connport
;
1513 /* allocate the new connport array and copy the old one */
1514 newconnport
=kzalloc(newsize
* sizeof(struct ipn_node
*),GFP_KERNEL
);
1516 up(&ipnn
->ipnn_mutex
);
1519 memcpy(newconnport
,oldconnport
,min
* sizeof(struct ipn_node
*));
1520 ipnn
->connport
=newconnport
;
1521 ipnn
->maxports
=newsize
;
1522 /* notify the protocol that the netowrk has been resized */
1523 err
=ipn_protocol_table
[ipnn
->protocol
]->ipn_p_resizenet(ipnn
,oldsize
,newsize
);
1525 /* roll back if the resize operation failed for the protocol */
1526 ipnn
->connport
=oldconnport
;
1527 ipnn
->maxports
=oldsize
;
1530 /* successful mission, network resized */
1532 up(&ipnn
->ipnn_mutex
);
1536 /* IPN SETSOCKOPT */
1538 static int ipn_setsockopt(struct socket
*sock
, int level
, int optname
,
1539 char __user
*optval
, unsigned int optlen
)
1541 static int ipn_setsockopt(struct socket
*sock
, int level
, int optname
,
1542 char __user
*optval
, int optlen
)
1545 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1546 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1548 if (ipn_node
->shutdown
== SHUTDOWN_XMASK
)
1550 if (level
!= 0 && level
!= ipn_node
->protocol
+1)
1551 return -EPROTONOSUPPORT
;
1553 /* protocol specific sockopt */
1556 if (down_interruptible(&ipnn
->ipnn_mutex
))
1557 return -ERESTARTSYS
;
1558 rv
=ipn_protocol_table
[ipn_node
->protocol
]->ipn_p_setsockopt(ipn_node
,optname
,optval
,optlen
);
1559 up(&ipnn
->ipnn_mutex
);
1564 if (optname
== IPN_SO_DESCR
) {
1565 if (optlen
> IPN_DESCRLEN
)
1568 memset(ipn_node
->descr
,0,IPN_DESCRLEN
);
1569 if (copy_from_user(ipn_node
->descr
,optval
,optlen
))
1570 ipn_node
->descr
[0]=0;
1572 ipn_node
->descr
[optlen
-1]=0;
1576 if (optlen
< sizeof(int))
1578 else if ((optname
& IPN_SO_PREBIND
) && (ipnn
!= NULL
))
1582 get_user(val
, (int __user
*) optval
);
1583 if ((optname
& IPN_SO_PREBIND
) && !ipn_node
->pbp
) {
1584 struct pre_bind_parms std
=STD_BIND_PARMS
;
1585 ipn_node
->pbp
=kzalloc(sizeof(struct pre_bind_parms
),GFP_KERNEL
);
1588 *(ipn_node
->pbp
)=std
;
1592 if (sock
->state
== SS_UNCONNECTED
)
1593 ipn_node
->portno
=val
;
1597 case IPN_SO_CHANGE_NUMNODES
:
1598 if ((ipn_node
->flags
& IPN_NODEFLAG_BOUND
)!=0) {
1602 return ipn_netresize(ipnn
,val
);
1606 case IPN_SO_WANT_OOB_NUMNODES
:
1608 ipn_node
->flags
|= IPN_NODEFLAG_OOB_NUMNODES
;
1610 ipn_node
->flags
&= ~IPN_NODEFLAG_OOB_NUMNODES
;
1612 case IPN_SO_HANDLE_OOB
:
1614 ipn_node
->shutdown
&= ~RCV_SHUTDOWN_NO_OOB
;
1616 ipn_node
->shutdown
|= RCV_SHUTDOWN_NO_OOB
;
1622 ipn_node
->pbp
->mtu
=val
;
1624 case IPN_SO_NUMNODES
:
1628 ipn_node
->pbp
->maxports
=val
;
1630 case IPN_SO_MSGPOOLSIZE
:
1634 ipn_node
->pbp
->msgpoolsize
=val
;
1637 ipn_node
->pbp
->flags
=val
;
1640 ipn_node
->pbp
->mode
=val
;
1649 /* IPN GETSOCKOPT */
1650 static int ipn_getsockopt(struct socket
*sock
, int level
, int optname
,
1651 char __user
*optval
, int __user
*optlen
) {
1652 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1653 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1656 if (ipn_node
->shutdown
== SHUTDOWN_XMASK
)
1658 if (level
!= 0 && level
!= ipn_node
->protocol
+1)
1659 return -EPROTONOSUPPORT
;
1663 /* protocol specific sockopt */
1664 if (down_interruptible(&ipnn
->ipnn_mutex
))
1665 return -ERESTARTSYS
;
1666 rv
=ipn_protocol_table
[ipn_node
->protocol
]->ipn_p_getsockopt(ipn_node
,optname
,optval
,optlen
);
1667 up(&ipnn
->ipnn_mutex
);
1672 if (get_user(len
, optlen
))
1674 if (optname
== IPN_SO_DESCR
) {
1675 if (len
< IPN_DESCRLEN
)
1678 if (len
> IPN_DESCRLEN
)
1680 if(put_user(len
, optlen
))
1682 if(copy_to_user(optval
,ipn_node
->descr
,len
))
1690 val
=ipn_node
->portno
;
1695 else if (ipn_node
->pbp
)
1696 val
=ipn_node
->pbp
->mtu
;
1698 case IPN_SO_NUMNODES
:
1701 else if (ipn_node
->pbp
)
1702 val
=ipn_node
->pbp
->maxports
;
1704 case IPN_SO_MSGPOOLSIZE
:
1706 val
=ipnn
->msgpool_size
;
1707 else if (ipn_node
->pbp
)
1708 val
=ipn_node
->pbp
->msgpoolsize
;
1713 else if (ipn_node
->pbp
)
1714 val
=ipn_node
->pbp
->flags
;
1719 else if (ipn_node
->pbp
)
1720 val
=ipn_node
->pbp
->mode
;
1726 if (len
< sizeof(int))
1730 if(put_user(len
, optlen
))
1732 if(copy_to_user(optval
,&val
,len
))
1741 /* BROADCAST/HUB implementation */
1743 static int ipn_bcast_newport(struct ipn_node
*newport
) {
1744 struct ipn_network
*ipnn
=newport
->ipn
;
1746 for (i
=0;i
<ipnn
->maxports
;i
++) {
1747 if (ipnn
->connport
[i
] == NULL
)
1753 static int ipn_bcast_handlemsg(struct ipn_node
*from
,
1754 struct msgpool_item
*msgitem
){
1755 struct ipn_network
*ipnn
=from
->ipn
;
1757 struct ipn_node
*ipn_node
;
1758 list_for_each_entry(ipn_node
, &ipnn
->connectqueue
, nodelist
) {
1759 if (ipn_node
!= from
)
1760 ipn_proto_sendmsg(ipn_node
,msgitem
);
1765 static void ipn_null_delport(struct ipn_node
*oldport
) {}
1766 static void ipn_null_postnewport(struct ipn_node
*newport
) {}
1767 static void ipn_null_predelport(struct ipn_node
*oldport
) {}
1768 static int ipn_null_newnet(struct ipn_network
*newnet
) {return 0;}
1769 static int ipn_null_resizenet(struct ipn_network
*net
,int oldsize
,int newsize
) {
1771 static void ipn_null_delnet(struct ipn_network
*oldnet
) {}
1772 static int ipn_null_setsockopt(struct ipn_node
*port
,int optname
,
1773 char __user
*optval
, int optlen
) {return -EOPNOTSUPP
;}
1774 static int ipn_null_getsockopt(struct ipn_node
*port
,int optname
,
1775 char __user
*optval
, int *optlen
) {return -EOPNOTSUPP
;}
1776 static int ipn_null_ioctl(struct ipn_node
*port
,unsigned int request
,
1777 unsigned long arg
) {return -EOPNOTSUPP
;}
1779 /* Protocol Registration/deregisteration */
1781 void ipn_init_protocol(struct ipn_protocol
*p
)
1783 if (p
->ipn_p_delport
== NULL
) p
->ipn_p_delport
=ipn_null_delport
;
1784 if (p
->ipn_p_postnewport
== NULL
) p
->ipn_p_postnewport
=ipn_null_postnewport
;
1785 if (p
->ipn_p_predelport
== NULL
) p
->ipn_p_predelport
=ipn_null_predelport
;
1786 if (p
->ipn_p_newnet
== NULL
) p
->ipn_p_newnet
=ipn_null_newnet
;
1787 if (p
->ipn_p_resizenet
== NULL
) p
->ipn_p_resizenet
=ipn_null_resizenet
;
1788 if (p
->ipn_p_delnet
== NULL
) p
->ipn_p_delnet
=ipn_null_delnet
;
1789 if (p
->ipn_p_setsockopt
== NULL
) p
->ipn_p_setsockopt
=ipn_null_setsockopt
;
1790 if (p
->ipn_p_getsockopt
== NULL
) p
->ipn_p_getsockopt
=ipn_null_getsockopt
;
1791 if (p
->ipn_p_ioctl
== NULL
) p
->ipn_p_ioctl
=ipn_null_ioctl
;
1794 int ipn_proto_register(int protocol
,struct ipn_protocol
*ipn_service
)
1797 if (ipn_service
->ipn_p_newport
== NULL
||
1798 ipn_service
->ipn_p_handlemsg
== NULL
)
1800 ipn_init_protocol(ipn_service
);
1801 if (protocol
> 1 && protocol
<= IPN_MAX_PROTO
) {
1803 if (down_interruptible(&ipn_glob_mutex
))
1804 return -ERESTARTSYS
;
1805 if (ipn_protocol_table
[protocol
])
1808 ipn_service
->refcnt
=0;
1809 ipn_protocol_table
[protocol
]=ipn_service
;
1810 printk(KERN_INFO
"IPN: Registered protocol %d\n",protocol
+1);
1812 up(&ipn_glob_mutex
);
1818 int ipn_proto_deregister(int protocol
)
1821 if (protocol
> 1 && protocol
<= IPN_MAX_PROTO
) {
1823 if (down_interruptible(&ipn_glob_mutex
))
1824 return -ERESTARTSYS
;
1825 if (ipn_protocol_table
[protocol
]) {
1826 if (ipn_protocol_table
[protocol
]->refcnt
== 0) {
1827 ipn_protocol_table
[protocol
]=NULL
;
1828 printk(KERN_INFO
"IPN: Unregistered protocol %d\n",protocol
+1);
1833 up(&ipn_glob_mutex
);
1840 /* Module constructor/destructor */
1841 static struct net_proto_family ipn_family_ops
= {
1843 .create
= ipn_create
,
1844 .owner
= THIS_MODULE
,
1847 /* IPN constructor */
1848 static int ipn_init(void)
1852 ipn_init_protocol(&ipn_bcast
);
1853 ipn_network_cache
=kmem_cache_create("ipn_network",sizeof(struct ipn_network
),0,0,NULL
);
1854 if (!ipn_network_cache
) {
1855 printk(KERN_CRIT
"%s: Cannot create ipn_network SLAB cache!\n",
1861 ipn_node_cache
=kmem_cache_create("ipn_node",sizeof(struct ipn_node
),0,0,NULL
);
1862 if (!ipn_node_cache
) {
1863 printk(KERN_CRIT
"%s: Cannot create ipn_node SLAB cache!\n",
1869 ipn_msgitem_cache
=kmem_cache_create("ipn_msgitem",sizeof(struct msgitem
),0,0,NULL
);
1870 if (!ipn_msgitem_cache
) {
1871 printk(KERN_CRIT
"%s: Cannot create ipn_msgitem SLAB cache!\n",
1877 rc
=ipn_msgbuf_init();
1879 printk(KERN_CRIT
"%s: Cannot create ipn_msgbuf SLAB cache\n",
1881 goto out_net_node_msg
;
1884 rc
=proto_register(&ipn_proto
,1);
1886 printk(KERN_CRIT
"%s: Cannot register the protocol!\n",
1888 goto out_net_node_msg_msgbuf
;
1891 sock_register(&ipn_family_ops
);
1893 printk(KERN_INFO
"IPN: Virtual Square Project, University of Bologna 2007-09\n");
1896 out_net_node_msg_msgbuf
:
1899 kmem_cache_destroy(ipn_msgitem_cache
);
1901 kmem_cache_destroy(ipn_node_cache
);
1903 kmem_cache_destroy(ipn_network_cache
);
1908 /* IPN destructor */
1909 static void ipn_exit(void)
1912 if (ipn_msgitem_cache
)
1913 kmem_cache_destroy(ipn_msgitem_cache
);
1915 kmem_cache_destroy(ipn_node_cache
);
1916 if (ipn_network_cache
)
1917 kmem_cache_destroy(ipn_network_cache
);
1919 sock_unregister(PF_IPN
);
1920 proto_unregister(&ipn_proto
);
1921 printk(KERN_INFO
"IPN removed\n");
1924 module_init(ipn_init
);
1925 module_exit(ipn_exit
);
1927 EXPORT_SYMBOL_GPL(ipn_proto_register
);
1928 EXPORT_SYMBOL_GPL(ipn_proto_deregister
);
1929 EXPORT_SYMBOL_GPL(ipn_proto_sendmsg
);
1930 EXPORT_SYMBOL_GPL(ipn_proto_oobsendmsg
);
1931 EXPORT_SYMBOL_GPL(ipn_msgpool_alloc
);
1932 EXPORT_SYMBOL_GPL(ipn_msgpool_put
);