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)
48 /*extension of RCV_SHUTDOWN defined in include/net/sock.h
49 * when the bit is set recv fails */
50 /* NO_OOB: do not send OOB */
51 #define RCV_SHUTDOWN_NO_OOB 4
52 /* EXTENDED MASK including OOB */
53 #define SHUTDOWN_XMASK (SHUTDOWN_MASK | RCV_SHUTDOWN_NO_OOB)
54 /* if XRCV_SHUTDOWN is all set recv fails */
55 #define XRCV_SHUTDOWN (RCV_SHUTDOWN | RCV_SHUTDOWN_NO_OOB)
57 /* Global MUTEX: this is locked to add/delete/modify networks
58 * this is *not* locked just to send/receive msgs */
59 static DECLARE_MUTEX(ipn_glob_mutex
);
60 /* Network table and hash */
61 struct hlist_head ipn_network_table
[IPN_HASH_SIZE
+ 1];
62 /* slab(s) for fast data structure allocation */
63 static struct kmem_cache
*ipn_network_cache
;
64 static struct kmem_cache
*ipn_node_cache
;
65 static struct kmem_cache
*ipn_msgitem_cache
;
67 /* Protocol 1: HUB/Broadcast default protocol. Function Prototypes */
68 static int ipn_bcast_newport(struct ipn_node
*newport
);
69 static int ipn_bcast_handlemsg(struct ipn_node
*from
,
70 struct msgpool_item
*msgitem
);
72 /* default protocol IPN_BROADCAST (0) */
73 static struct ipn_protocol ipn_bcast
= {
75 .ipn_p_newport
=ipn_bcast_newport
,
76 .ipn_p_handlemsg
=ipn_bcast_handlemsg
};
78 static struct ipn_protocol
*ipn_protocol_table
[IPN_MAX_PROTO
]={&ipn_bcast
};
80 /* Socket call function prototypes */
81 static int ipn_release(struct socket
*);
82 static int ipn_bind(struct socket
*, struct sockaddr
*, int);
83 static int ipn_connect(struct socket
*, struct sockaddr
*,
84 int addr_len
, int flags
);
85 static int ipn_getname(struct socket
*, struct sockaddr
*, int *, int);
86 static unsigned int ipn_poll(struct file
*, struct socket
*, poll_table
*);
87 static int ipn_ioctl(struct socket
*, unsigned int, unsigned long);
88 static int ipn_shutdown(struct socket
*, int);
89 static int ipn_sendmsg(struct kiocb
*, struct socket
*,
90 struct msghdr
*, size_t);
91 static int ipn_recvmsg(struct kiocb
*, struct socket
*,
92 struct msghdr
*, size_t, int);
93 static int ipn_setsockopt(struct socket
*sock
, int level
, int optname
,
94 char __user
*optval
, int optlen
);
95 static int ipn_getsockopt(struct socket
*sock
, int level
, int optname
,
96 char __user
*optval
, int __user
*optlen
);
98 /* Network table Management
99 * inode->ipn_network hash table
100 * protected by ipn_glob_mutex */
101 static inline void ipn_insert_network(struct hlist_head
*list
, struct ipn_network
*ipnn
)
103 hlist_add_head(&ipnn
->hnode
, list
);
106 static inline void ipn_remove_network(struct ipn_network
*ipnn
)
108 hlist_del(&ipnn
->hnode
);
111 static struct ipn_network
*ipn_find_network_byinode(struct inode
*i
)
113 struct ipn_network
*ipnn
;
114 struct hlist_node
*node
;
116 hlist_for_each_entry(ipnn
, node
,
117 &ipn_network_table
[i
->i_ino
& (IPN_HASH_SIZE
- 1)], hnode
) {
118 struct dentry
*dentry
= ipnn
->dentry
;
120 if(dentry
&& dentry
->d_inode
== i
)
126 struct ipn_network
*ipn_find_network_byfun(
127 int (*fun
)(struct ipn_network
*,void *),void *funarg
)
129 struct ipn_network
*ipnn
;
130 struct hlist_node
*node
;
133 for (ipn_table_scan
=0;ipn_table_scan
<IPN_HASH_SIZE
;ipn_table_scan
++) {
134 hlist_for_each_entry(ipnn
, node
, &ipn_network_table
[ipn_table_scan
], hnode
) {
142 /* msgpool management
143 * msgpool_item are ipn_network dependent (each net has its own MTU)
144 * for each message sent there is one msgpool_item and many struct msgitem
145 * one for each receipient.
146 * msgitem are connected to the node's msgqueue or oobmsgqueue.
147 * when a message is delivered to a process the msgitem is deleted and
148 * the count of the msgpool_item is decreased.
149 * msgpool_item elements gets deleted automatically when count is 0*/
152 struct list_head list
;
153 struct msgpool_item
*msg
;
156 /* alloc a fresh msgpool item. count is set to 1.
159 * for each receipient
160 * enqueue messages to the process (using msgitem), ipn_msgpool_hold
162 * The message can be delivered concurrently. init count to 1 guarantees
163 * that it survives at least until is has been enqueued to all
165 /* msgpool_cache == NULL when the ipn network is using the flexible allocation,
166 * i.e. the msgpool item gets allocated by kmalloc instead of using slab */
167 static inline struct msgpool_item
*_ipn_msgpool_alloc(struct ipn_network
*ipnn
, int len
)
169 struct msgpool_item
*new;
170 if ((new=ipnn
->msgpool_cache
?
171 kmem_cache_alloc(ipnn
->msgpool_cache
,GFP_KERNEL
):
172 kmalloc(sizeof(struct msgpool_item
)+len
,GFP_KERNEL
)) != NULL
) {
173 atomic_set(&new->count
,1);
174 atomic_inc(&ipnn
->msgpool_nelem
);
179 /* ipn_msgpool_alloc returns null when leaky, lossless and no space is left
180 * for new msgpool items. When !leaky, it can allocate more msgpooli items,
181 * this temporary overassignment blocks new messages to be delivered until
182 * there is space for a msgpool item to be allocated */
183 struct msgpool_item
*ipn_msgpool_alloc(struct ipn_network
*ipnn
,int leaky
,int len
)
185 if (leaky
&& (ipnn
->flags
& IPN_FLAG_LOSSLESS
) &&
186 atomic_read(&ipnn
->msgpool_nelem
) < ipnn
->msgpool_size
)
189 return _ipn_msgpool_alloc(ipnn
,len
);
192 /* If the service il LOSSLESS, this msgpool call waits for an
193 * available msgpool item */
194 static inline struct msgpool_item
*ipn_msgpool_alloc_locking(struct ipn_network
*ipnn
,int len
)
196 if (ipnn
->flags
& IPN_FLAG_LOSSLESS
) {
197 while (atomic_read(&ipnn
->msgpool_nelem
) >= ipnn
->msgpool_size
) {
198 if (wait_event_interruptible_exclusive(ipnn
->send_wait
,
199 atomic_read(&ipnn
->msgpool_nelem
) < ipnn
->msgpool_size
))
203 return _ipn_msgpool_alloc(ipnn
,len
);
206 /* register one more user for this msgpool item (i.e. count++) */
207 static inline void ipn_msgpool_hold(struct msgpool_item
*msg
)
209 atomic_inc(&msg
->count
);
212 /* decrease count and delete msgpool_item if count == 0 */
213 void ipn_msgpool_put(struct msgpool_item
*old
,
214 struct ipn_network
*ipnn
)
216 if (atomic_dec_and_test(&old
->count
)) {
217 if (ipnn
->msgpool_cache
)
218 kmem_cache_free(ipnn
->msgpool_cache
,old
);
221 atomic_dec(&ipnn
->msgpool_nelem
);
222 if (ipnn
->flags
& IPN_FLAG_LOSSLESS
) /* this could be done anyway */
223 wake_up_interruptible(&ipnn
->send_wait
);
228 static const struct proto_ops ipn_ops
= {
230 .owner
= THIS_MODULE
,
231 .release
= ipn_release
,
233 .connect
= ipn_connect
,
234 .socketpair
= sock_no_socketpair
,
235 .accept
= sock_no_accept
,
236 .getname
= ipn_getname
,
239 .listen
= sock_no_listen
,
240 .shutdown
= ipn_shutdown
,
241 .setsockopt
= ipn_setsockopt
,
242 .getsockopt
= ipn_getsockopt
,
243 .sendmsg
= ipn_sendmsg
,
244 .recvmsg
= ipn_recvmsg
,
245 .mmap
= sock_no_mmap
,
246 .sendpage
= sock_no_sendpage
,
249 static struct proto ipn_proto
= {
251 .owner
= THIS_MODULE
,
252 .obj_size
= sizeof(struct ipn_sock
),
255 /* create a new ipn_node */
256 struct ipn_node
*ipn_node_create(struct net
*net
)
258 struct ipn_node
*ipn_node
;
259 ipn_node
=kmem_cache_alloc(ipn_node_cache
,GFP_KERNEL
);
260 if (ipn_node
!=NULL
) {
261 INIT_LIST_HEAD(&ipn_node
->nodelist
);
262 ipn_node
->protocol
=0; /* BROADCAST, caller must set a proper value */
263 ipn_node
->flags
=IPN_NODEFLAG_INUSE
;
264 ipn_node
->shutdown
=RCV_SHUTDOWN_NO_OOB
;
265 ipn_node
->descr
[0]=0;
266 ipn_node
->portno
=IPN_PORTNO_ANY
;
268 ipn_node
->netdev
=NULL
;
270 ipn_node
->proto_private
=NULL
;
271 ipn_node
->totmsgcount
=0;
272 ipn_node
->oobmsgcount
=0;
273 spin_lock_init(&ipn_node
->msglock
);
274 INIT_LIST_HEAD(&ipn_node
->msgqueue
);
275 INIT_LIST_HEAD(&ipn_node
->oobmsgqueue
);
277 init_waitqueue_head(&ipn_node
->read_wait
);
284 * ipn_node is a separate structure, pointed by ipn_sock -> node
285 * when a node is "persistent", ipn_node survives while ipn_sock gets released*/
286 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
287 static int ipn_create(struct socket
*sock
, int protocol
)
289 static int ipn_create(struct net
*net
,struct socket
*sock
, int protocol
)
292 struct ipn_sock
*ipn_sk
;
294 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
297 if (net
!= &init_net
)
298 return -EAFNOSUPPORT
;
301 if (sock
->type
!= SOCK_RAW
)
306 protocol
=IPN_BROADCAST
-1;
307 if (protocol
< 0 || protocol
>= IPN_MAX_PROTO
||
308 ipn_protocol_table
[protocol
] == NULL
)
309 return -EPROTONOSUPPORT
;
310 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
311 ipn_sk
= (struct ipn_sock
*) sk_alloc(PF_IPN
, GFP_KERNEL
, &ipn_proto
, 1);
313 ipn_sk
= (struct ipn_sock
*) sk_alloc(net
, PF_IPN
, GFP_KERNEL
, &ipn_proto
);
318 if ((ipn_sk
->node
=ipn_node_create(net
))==NULL
) {
319 sock_put((struct sock
*) ipn_sk
);
322 ipn_sk
->node
->protocol
=protocol
;
323 sock_init_data(sock
,(struct sock
*) ipn_sk
);
324 sock
->state
= SS_UNCONNECTED
;
325 sock
->ops
= &ipn_ops
;
326 sock
->sk
=(struct sock
*)ipn_sk
;
330 /* update # of readers and # of writers counters for an ipn network.
331 * This function sends oob messages to nodes requesting the service */
332 /* LOCKING ipnn_mutex is locked */
333 static void ipn_net_update_counters(struct ipn_network
*ipnn
,
334 int chg_readers
, int chg_writers
) {
335 ipnn
->numreaders
+= chg_readers
;
336 ipnn
->numwriters
+= chg_writers
;
337 if (ipnn
->mtu
>= sizeof(struct numnode_oob
))
339 struct msgpool_item
*ipn_msg
=_ipn_msgpool_alloc(ipnn
,sizeof(struct numnode_oob
));
341 struct numnode_oob
*oob_msg
=(struct numnode_oob
*)(ipn_msg
->data
);
342 struct ipn_node
*ipn_node
;
343 ipn_msg
->len
=sizeof(struct numnode_oob
);
344 oob_msg
->level
=IPN_ANY
;
345 oob_msg
->tag
=IPN_OOB_NUMNODE_TAG
;
346 oob_msg
->numreaders
=ipnn
->numreaders
;
347 oob_msg
->numwriters
=ipnn
->numwriters
;
348 list_for_each_entry(ipn_node
, &ipnn
->connectqueue
, nodelist
) {
349 if (ipn_node
->flags
& IPN_NODEFLAG_OOB_NUMNODES
)
350 ipn_proto_oobsendmsg(ipn_node
,ipn_msg
);
352 ipn_msgpool_put(ipn_msg
,ipnn
);
357 /* flush pending messages (for close and shutdown RCV) */
358 /* LOCKING: ipnn_mutex is locked */
359 static void ipn_flush_recvqueue(struct ipn_node
*ipn_node
)
361 struct ipn_network
*ipnn
=ipn_node
->ipn
;
362 spin_lock(&ipn_node
->msglock
);
363 while (!list_empty(&ipn_node
->msgqueue
)) {
364 struct msgitem
*msgitem
=
365 list_first_entry(&ipn_node
->msgqueue
, struct msgitem
, list
);
366 list_del(&msgitem
->list
);
367 ipn_node
->totmsgcount
--;
368 ipn_msgpool_put(msgitem
->msg
,ipnn
);
369 kmem_cache_free(ipn_msgitem_cache
,msgitem
);
371 spin_unlock(&ipn_node
->msglock
);
374 /* flush pending oob messages (for socket close) */
375 /* LOCKING: ipnn_mutex is locked */
376 static void ipn_flush_oobrecvqueue(struct ipn_node
*ipn_node
)
378 struct ipn_network
*ipnn
=ipn_node
->ipn
;
379 spin_lock(&ipn_node
->msglock
);
380 while (!list_empty(&ipn_node
->oobmsgqueue
)) {
381 struct msgitem
*msgitem
=
382 list_first_entry(&ipn_node
->oobmsgqueue
, struct msgitem
, list
);
383 list_del(&msgitem
->list
);
384 ipn_node
->totmsgcount
--;
385 ipn_node
->oobmsgcount
--;
386 ipn_msgpool_put(msgitem
->msg
,ipnn
);
387 kmem_cache_free(ipn_msgitem_cache
,msgitem
);
389 spin_unlock(&ipn_node
->msglock
);
392 /* Terminate node. The node is "logically" terminated. */
393 /* LOCKING: ipn_glob_lock must be locked here */
394 static int ipn_terminate_node(struct ipn_node
*ipn_node
)
396 struct ipn_network
*ipnn
=ipn_node
->ipn
;
398 if (down_interruptible(&ipnn
->ipnn_mutex
))
400 if (ipn_node
->portno
>= 0) {
401 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_predelport(ipn_node
);
402 ipnn
->connport
[ipn_node
->portno
]=NULL
;
404 list_del(&ipn_node
->nodelist
);
405 ipn_flush_recvqueue(ipn_node
);
406 ipn_flush_oobrecvqueue(ipn_node
);
407 if (ipn_node
->portno
>= 0)
408 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_delport(ipn_node
);
410 ipn_net_update_counters(ipnn
,
411 (ipn_node
->shutdown
& RCV_SHUTDOWN
)?0:-1,
412 (ipn_node
->shutdown
& SEND_SHUTDOWN
)?0:-1);
413 ipn_node
->shutdown
= SHUTDOWN_XMASK
;
414 up(&ipnn
->ipnn_mutex
);
415 if (ipn_node
->netdev
)
416 ipn_netdev_close(ipn_node
);
417 /* No more network elements */
419 if (ipnn
->refcnt
== 0 && !ipn_is_persistent_chrdev(ipnn
))
422 ipn_deregister_chrdev(ipnn
);
423 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_delnet(ipnn
);
424 ipn_remove_network(ipnn
);
425 ipn_protocol_table
[ipnn
->protocol
]->refcnt
--;
430 if (ipnn
->msgpool_cache
)
431 ipn_msgbuf_put(ipnn
->msgpool_cache
);
433 kfree(ipnn
->connport
);
434 kmem_cache_free(ipn_network_cache
, ipnn
);
435 module_put(THIS_MODULE
);
439 kfree(ipn_node
->pbp
);
445 /* release of an ipn_node */
446 int ipn_node_release(struct ipn_node
*ipn_node
)
449 if (down_interruptible(&ipn_glob_mutex
))
451 if (ipn_node
->flags
& IPN_NODEFLAG_PERSIST
) {
452 ipn_node
->flags
&= ~IPN_NODEFLAG_INUSE
;
456 rv
=ipn_terminate_node(ipn_node
);
460 kmem_cache_free(ipn_node_cache
,ipn_node
);
466 /* release of an ipn socket */
467 static int ipn_release (struct socket
*sock
)
469 struct ipn_sock
*ipn_sk
=(struct ipn_sock
*)sock
->sk
;
470 struct ipn_node
*ipn_node
=ipn_sk
->node
;
471 int rv
=ipn_node_release(ipn_node
);
473 sock_put((struct sock
*) ipn_sk
);
477 /* _set persist, change the persistence of a node,
478 * when persistence gets cleared and the node is no longer used
479 * the node is terminated and freed.
480 * ipn_glob_mutex must be locked */
481 static int _ipn_setpersist(struct ipn_node
*ipn_node
, int persist
)
485 ipn_node
->flags
|= IPN_NODEFLAG_PERSIST
;
487 ipn_node
->flags
&= ~IPN_NODEFLAG_PERSIST
;
488 if (!(ipn_node
->flags
& IPN_NODEFLAG_INUSE
)) {
489 rv
=ipn_terminate_node(ipn_node
);
491 kmem_cache_free(ipn_node_cache
,ipn_node
);
498 * lock ipn_glob_mutex and call __ipn_setpersist above */
499 static int ipn_setpersist(struct ipn_node
*ipn_node
, int persist
)
502 if (ipn_node
->netdev
== NULL
)
504 if (down_interruptible(&ipn_glob_mutex
))
506 rv
=_ipn_setpersist(ipn_node
,persist
);
511 /* several network parameters can be set by setsockopt prior to bind */
512 /* struct pre_bind_parms is a temporary stucture connected to ipn_node->pbp
513 * to keep the parameter values. */
514 struct pre_bind_parms
{
515 unsigned short maxports
;
516 unsigned short flags
;
517 unsigned short msgpoolsize
;
522 /* STD_PARMS: BITS_PER_LONG nodes, no flags, BITS_PER_BYTE pending msgs,
523 * Ethernet + VLAN MTU*/
524 #define STD_BIND_PARMS {BITS_PER_LONG, 0, BITS_PER_BYTE, 1514, 0777};
526 static int ipn_mkname(struct sockaddr_un
* sunaddr
, int len
)
528 if (len
<= sizeof(short) || len
> sizeof(*sunaddr
))
530 if (!sunaddr
|| sunaddr
->sun_family
!= AF_IPN
)
533 * This may look like an off by one error but it is a bit more
534 * subtle. 108 is the longest valid AF_IPN path for a binding.
535 * sun_path[108] doesnt as such exist. However in kernel space
536 * we are guaranteed that it is a valid memory location in our
537 * kernel address buffer.
539 ((char *)sunaddr
)[len
]=0;
540 len
= strlen(sunaddr
->sun_path
)+1+sizeof(short);
544 static int ipn_node_bind(struct ipn_node
*ipn_node
, struct ipn_network
*ipnn
)
548 if (ipn_node
->ipn
!= NULL
)
551 list_add_tail(&ipn_node
->nodelist
,&ipnn
->unconnectqueue
);
553 ipn_node
->flags
|= IPN_NODEFLAG_BOUND
;
558 static int ipn_bind(struct socket
*sock
, struct sockaddr
*uaddr
, int addr_len
)
560 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)uaddr
;
561 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
563 struct ipn_network
*ipnn
;
564 struct dentry
* dentry
= NULL
;
566 struct pre_bind_parms parms
=STD_BIND_PARMS
;
568 //printk("IPN bind\n");
570 if (down_interruptible(&ipn_glob_mutex
))
572 if (sock
->state
!= SS_UNCONNECTED
||
573 ipn_node
->ipn
!= NULL
) {
578 if (ipn_node
->protocol
>= 0 &&
579 (ipn_node
->protocol
>= IPN_MAX_PROTO
||
580 ipn_protocol_table
[ipn_node
->protocol
] == NULL
)) {
581 err
= -EPROTONOSUPPORT
;
585 addr_len
= ipn_mkname(sunaddr
, addr_len
);
591 /* check if there is already an ipn-network socket with that name */
592 err
= path_lookup(sunaddr
->sun_path
, LOOKUP_FOLLOW
, &nd
);
593 if (err
) { /* it does not exist, NEW IPN socket! */
595 /* Is it everything okay with the parent? */
596 err
= path_lookup(sunaddr
->sun_path
, LOOKUP_PARENT
, &nd
);
598 goto out_mknod_parent
;
599 /* Do I have the permission to create a file? */
600 dentry
= lookup_create(&nd
, 0);
601 err
= PTR_ERR(dentry
);
603 goto out_mknod_unlock
;
605 * All right, let's create it.
608 mode
= ipn_node
->pbp
->mode
;
610 mode
= SOCK_INODE(sock
)->i_mode
;
611 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 30)
612 mode
= S_IFSOCK
| (mode
& ~current
->fs
->umask
);
614 mode
= S_IFSOCK
| (mode
& ~current_umask());
618 err
= vfs_mknod(nd
.path
.dentry
->d_inode
, dentry
, nd
.path
.mnt
, mode
, 0);
620 err
= vfs_mknod(nd
.path
.dentry
->d_inode
, dentry
, mode
, 0);
624 err
= vfs_mknod(nd
.dentry
->d_inode
, dentry
, nd
.path
.mnt
, mode
, 0);
626 err
= vfs_mknod(nd
.dentry
->d_inode
, dentry
, mode
, 0);
632 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
633 dput(nd
.path
.dentry
);
634 nd
.path
.dentry
= dentry
;
636 mutex_unlock(&nd
.dentry
->d_inode
->i_mutex
);
640 /* create a new ipn_network item */
642 parms
=*ipn_node
->pbp
;
643 ipnn
=kmem_cache_zalloc(ipn_network_cache
,GFP_KERNEL
);
646 goto out_mknod_dput_ipnn
;
648 ipnn
->connport
=kzalloc(parms
.maxports
* sizeof(struct ipn_node
*),GFP_KERNEL
);
649 if (!ipnn
->connport
) {
651 goto out_mknod_dput_ipnn2
;
654 /* module refcnt is incremented for each network, thus
655 * rmmod is forbidden if there are persistent nodes */
656 if (!try_module_get(THIS_MODULE
)) {
658 goto out_mknod_dput_ipnn2
;
660 memcpy(&ipnn
->sunaddr
,sunaddr
,addr_len
);
662 ipnn
->flags
=parms
.flags
;
663 if (ipnn
->flags
& IPN_FLAG_FLEXMTU
)
664 ipnn
->msgpool_cache
= NULL
;
666 ipnn
->msgpool_cache
=ipn_msgbuf_get(ipnn
->mtu
);
667 if (!ipnn
->msgpool_cache
) {
669 goto out_mknod_dput_putmodule
;
672 INIT_LIST_HEAD(&ipnn
->unconnectqueue
);
673 INIT_LIST_HEAD(&ipnn
->connectqueue
);
676 ipnn
->dentry
=nd
.path
.dentry
;
677 ipnn
->mnt
=nd
.path
.mnt
;
679 ipnn
->dentry
=nd
.dentry
;
682 init_MUTEX(&ipnn
->ipnn_mutex
);
683 ipnn
->sunaddr_len
=addr_len
;
684 ipnn
->protocol
=ipn_node
->protocol
;
685 if (ipnn
->protocol
< 0) ipnn
->protocol
= 0;
686 ipn_protocol_table
[ipnn
->protocol
]->refcnt
++;
689 ipnn
->maxports
=parms
.maxports
;
690 atomic_set(&ipnn
->msgpool_nelem
,0);
691 ipnn
->msgpool_size
=parms
.msgpoolsize
;
692 ipnn
->proto_private
=NULL
;
693 init_waitqueue_head(&ipnn
->send_wait
);
695 err
=ipn_protocol_table
[ipnn
->protocol
]->ipn_p_newnet(ipnn
);
697 goto out_mknod_dput_putmodule
;
699 ipn_insert_network(&ipn_network_table
[nd
.path
.dentry
->d_inode
->i_ino
& (IPN_HASH_SIZE
-1)],ipnn
);
701 ipn_insert_network(&ipn_network_table
[nd
.dentry
->d_inode
->i_ino
& (IPN_HASH_SIZE
-1)],ipnn
);
704 /* join an existing network */
705 if (parms
.flags
& IPN_FLAG_EXCL
) {
709 err
= inode_permission(nd
.path
.dentry
->d_inode
, MAY_EXEC
);
714 if (!S_ISSOCK(nd
.path
.dentry
->d_inode
->i_mode
))
716 ipnn
=ipn_find_network_byinode(nd
.path
.dentry
->d_inode
);
718 if (!S_ISSOCK(nd
.dentry
->d_inode
->i_mode
))
720 ipnn
=ipn_find_network_byinode(nd
.dentry
->d_inode
);
722 if (!ipnn
|| (ipnn
->flags
& IPN_FLAG_TERMINATED
) ||
723 (ipnn
->flags
& IPN_FLAG_EXCL
))
727 kfree(ipn_node
->pbp
);
730 ipn_node_bind(ipn_node
,ipnn
);
744 out_mknod_dput_putmodule
:
745 module_put(THIS_MODULE
);
746 out_mknod_dput_ipnn2
:
747 kfree(ipnn
->connport
);
749 kmem_cache_free(ipn_network_cache
,ipnn
);
754 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
757 mutex_unlock(&nd
.dentry
->d_inode
->i_mutex
);
768 static int ipn_connect(struct socket
*sock
, struct sockaddr
*addr
,
769 int addr_len
, int flags
){
770 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)addr
;
771 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
773 struct ipn_network
*ipnn
,*previousipnn
;
777 /* the socket cannot be connected twice */
778 if (sock
->state
!= SS_UNCONNECTED
)
781 if (down_interruptible(&ipn_glob_mutex
))
784 if ((previousipnn
=ipn_node
->ipn
) == NULL
) { /* unbound */
785 unsigned char mustshutdown
=0;
786 err
= ipn_mkname(sunaddr
, addr_len
);
790 err
= path_lookup(sunaddr
->sun_path
, LOOKUP_FOLLOW
, &nd
);
793 err
= inode_permission(nd
.path
.dentry
->d_inode
, MAY_READ
);
795 if (err
== -EACCES
|| err
== -EROFS
)
796 mustshutdown
|=RCV_SHUTDOWN
;
800 err
= inode_permission(nd
.path
.dentry
->d_inode
, MAY_WRITE
);
803 mustshutdown
|=SEND_SHUTDOWN
;
807 mustshutdown
|= ipn_node
->shutdown
;
808 /* if the combination of shutdown and permissions leaves
809 * no abilities, connect returns EACCES */
810 if (mustshutdown
== SHUTDOWN_XMASK
) {
815 ipn_node
->shutdown
=mustshutdown
;
818 if (!S_ISSOCK(nd
.path
.dentry
->d_inode
->i_mode
)) {
822 ipnn
=ipn_find_network_byinode(nd
.path
.dentry
->d_inode
);
824 if (!S_ISSOCK(nd
.dentry
->d_inode
->i_mode
)) {
828 ipnn
=ipn_find_network_byinode(nd
.dentry
->d_inode
);
830 if (!ipnn
|| (ipnn
->flags
& IPN_FLAG_TERMINATED
)) {
834 if (ipn_node
->protocol
== IPN_ANY
)
835 ipn_node
->protocol
=ipnn
->protocol
;
836 else if (ipnn
->protocol
!= ipn_node
->protocol
) {
849 if (down_interruptible(&ipnn
->ipnn_mutex
)) {
853 portno
= ipn_protocol_table
[ipnn
->protocol
]->ipn_p_newport(ipn_node
);
854 if (portno
>= 0 && portno
<ipnn
->maxports
) {
855 sock
->state
= SS_CONNECTED
;
856 ipn_node
->portno
=portno
;
857 ipnn
->connport
[portno
]=ipn_node
;
858 if (!(ipn_node
->flags
& IPN_NODEFLAG_BOUND
))
861 list_del(&ipn_node
->nodelist
);
862 list_add_tail(&ipn_node
->nodelist
,&ipnn
->connectqueue
);
863 ipn_net_update_counters(ipnn
,
864 (ipn_node
->shutdown
& RCV_SHUTDOWN
)?0:1,
865 (ipn_node
->shutdown
& SEND_SHUTDOWN
)?0:1);
867 ipn_node
->ipn
=previousipnn
; /* undo changes on ipn_node->ipn */
870 up(&ipnn
->ipnn_mutex
);
885 int ipn_node_create_connect(struct ipn_node
**ipn_node_out
,
886 struct ipn_network
*(* ipnn_map
)(void *),void *ipnn_map_arg
) {
887 struct ipn_node
*ipn_node
;
888 struct ipn_network
*ipnn
;
891 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
892 ipn_node
=ipn_node_create(NULL
);
894 ipn_node
=ipn_node_create(&init_net
);
896 if (down_interruptible(&ipn_glob_mutex
)) {
898 goto err_ipn_node_release
;
900 ipnn
=ipnn_map(ipnn_map_arg
);
902 ipn_node
->protocol
=ipnn
->protocol
;
903 if (down_interruptible(&ipnn
->ipnn_mutex
)) {
907 portno
= ipn_protocol_table
[ipnn
->protocol
]->ipn_p_newport(ipn_node
);
908 if (portno
>= 0 && portno
<ipnn
->maxports
) {
909 ipn_node
->portno
=portno
;
910 ipnn
->connport
[portno
]=ipn_node
;
911 if (!(ipn_node
->flags
& IPN_NODEFLAG_BOUND
)) {
913 list_del(&ipn_node
->nodelist
);
915 *ipn_node_out
=ipn_node
;
916 list_add_tail(&ipn_node
->nodelist
,&ipnn
->connectqueue
);
917 ipn_net_update_counters(ipnn
,
918 (ipn_node
->shutdown
& RCV_SHUTDOWN
)?0:1,
919 (ipn_node
->shutdown
& SEND_SHUTDOWN
)?0:1);
926 up(&ipnn
->ipnn_mutex
);
930 up(&ipnn
->ipnn_mutex
);
933 err_ipn_node_release
:
934 ipn_node_release(ipn_node
);
939 int ipn_node_connect(struct ipn_node
*ipn_node
)
941 struct ipn_network
*ipnn
;
944 if (down_interruptible(&ipn_glob_mutex
))
948 if (down_interruptible(&ipnn
->ipnn_mutex
)) {
952 portno
= ipn_protocol_table
[ipnn
->protocol
]->ipn_p_newport(ipn_node
);
953 if (portno
>= 0 && portno
<ipnn
->maxports
) {
954 ipn_node
->portno
=portno
;
955 ipnn
->connport
[portno
]=ipn_node
;
956 if (!(ipn_node
->flags
& IPN_NODEFLAG_BOUND
)) {
958 list_del(&ipn_node
->nodelist
);
960 list_add_tail(&ipn_node
->nodelist
,&ipnn
->connectqueue
);
961 ipn_net_update_counters(ipnn
,
962 (ipn_node
->shutdown
& RCV_SHUTDOWN
)?0:1,
963 (ipn_node
->shutdown
& SEND_SHUTDOWN
)?0:1);
969 up(&ipnn
->ipnn_mutex
);
977 static int ipn_getname(struct socket
*sock
, struct sockaddr
*uaddr
,
978 int *uaddr_len
, int peer
) {
979 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
980 struct ipn_network
*ipnn
=ipn_node
->ipn
;
981 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)uaddr
;
984 if (down_interruptible(&ipn_glob_mutex
))
987 *uaddr_len
= ipnn
->sunaddr_len
;
988 memcpy(sunaddr
,&ipnn
->sunaddr
,*uaddr_len
);
996 unsigned int ipn_node_poll(struct ipn_node
*ipn_node
, struct file
*file
, poll_table
*wait
) {
997 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1001 poll_wait(file
,&ipn_node
->read_wait
,wait
);
1002 if (ipnn
->flags
& IPN_FLAG_LOSSLESS
)
1003 poll_wait(file
,&ipnn
->send_wait
,wait
);
1004 /* POLLIN if recv succeeds,
1005 * POLL{PRI,RDNORM} if there are {oob,non-oob} messages */
1006 if (ipn_node
->totmsgcount
> 0) mask
|= POLLIN
;
1007 if (!(list_empty(&ipn_node
->msgqueue
))) mask
|= POLLRDNORM
;
1008 if (!(list_empty(&ipn_node
->oobmsgqueue
))) mask
|= POLLPRI
;
1009 if ((!(ipnn
->flags
& IPN_FLAG_LOSSLESS
)) |
1010 (atomic_read(&ipnn
->msgpool_nelem
) < ipnn
->msgpool_size
))
1011 mask
|= POLLOUT
| POLLWRNORM
;
1016 static unsigned int ipn_poll(struct file
*file
, struct socket
*sock
,
1018 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1019 return ipn_node_poll(ipn_node
, file
, wait
);
1022 /* connect netdev (from ioctl). connect a bound socket to a
1023 * network device TAP or GRAB */
1024 static int ipn_connect_netdev(struct socket
*sock
,struct ifreq
*ifr
)
1027 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1028 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1029 if (!capable(CAP_NET_ADMIN
))
1031 if (sock
->state
!= SS_UNCONNECTED
)
1034 return -ENOTCONN
; /* Maybe we need a different error for "NOT BOUND" */
1035 if (down_interruptible(&ipn_glob_mutex
))
1036 return -ERESTARTSYS
;
1037 if (down_interruptible(&ipnn
->ipnn_mutex
)) {
1038 up(&ipn_glob_mutex
);
1039 return -ERESTARTSYS
;
1041 ipn_node
->netdev
=ipn_netdev_alloc(ipn_node
->net
,ifr
->ifr_flags
,ifr
->ifr_name
,&err
);
1042 if (ipn_node
->netdev
) {
1044 portno
= ipn_protocol_table
[ipnn
->protocol
]->ipn_p_newport(ipn_node
);
1045 if (portno
>= 0 && portno
<ipnn
->maxports
) {
1046 sock
->state
= SS_CONNECTED
;
1047 ipn_node
->portno
=portno
;
1048 ipn_node
->flags
|= ifr
->ifr_flags
& IPN_NODEFLAG_DEVMASK
;
1049 ipnn
->connport
[portno
]=ipn_node
;
1050 err
=ipn_netdev_activate(ipn_node
);
1052 sock
->state
= SS_UNCONNECTED
;
1053 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_delport(ipn_node
);
1054 ipn_node
->netdev
=NULL
;
1055 ipn_node
->portno
= -1;
1056 ipn_node
->flags
&= ~IPN_NODEFLAG_DEVMASK
;
1057 ipnn
->connport
[portno
]=NULL
;
1059 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_postnewport(ipn_node
);
1060 list_del(&ipn_node
->nodelist
);
1061 list_add_tail(&ipn_node
->nodelist
,&ipnn
->connectqueue
);
1064 ipn_netdev_close(ipn_node
);
1066 ipn_node
->netdev
=NULL
;
1070 up(&ipnn
->ipnn_mutex
);
1071 up(&ipn_glob_mutex
);
1075 /* join a netdev, a socket gets connected to a persistent node
1076 * not connected to another socket */
1077 static int ipn_join_netdev(struct socket
*sock
,struct ifreq
*ifr
)
1080 struct net_device
*dev
;
1081 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1082 struct ipn_node
*ipn_joined
;
1083 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1084 if (sock
->state
!= SS_UNCONNECTED
)
1086 if (down_interruptible(&ipn_glob_mutex
))
1087 return -ERESTARTSYS
;
1088 if (down_interruptible(&ipnn
->ipnn_mutex
)) {
1089 up(&ipn_glob_mutex
);
1090 return -ERESTARTSYS
;
1092 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
1093 dev
=__dev_get_by_name(ifr
->ifr_name
);
1095 dev
=__dev_get_by_name(ipn_node
->net
,ifr
->ifr_name
);
1098 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
1099 dev
=__dev_get_by_index(ifr
->ifr_ifindex
);
1101 dev
=__dev_get_by_index(ipn_node
->net
,ifr
->ifr_ifindex
);
1103 if (dev
&& (ipn_joined
=ipn_netdev2node(dev
)) != NULL
) { /* the interface does exist */
1105 for (i
=0;i
<ipnn
->maxports
&& ipn_joined
!= ipnn
->connport
[i
] ;i
++)
1107 if (i
< ipnn
->maxports
) { /* found */
1108 /* ipn_joined is substituted to ipn_node */
1109 ((struct ipn_sock
*)sock
->sk
)->node
=ipn_joined
;
1110 ipn_joined
->flags
|= IPN_NODEFLAG_INUSE
;
1112 kmem_cache_free(ipn_node_cache
,ipn_node
);
1117 up(&ipnn
->ipnn_mutex
);
1118 up(&ipn_glob_mutex
);
1122 /* set persistence of a node looking for it by interface name
1123 * (it is for sysadm, to close network interfaces)*/
1124 static int ipn_setpersist_netdev(struct ifreq
*ifr
, int value
)
1126 struct net_device
*dev
;
1127 struct ipn_node
*ipn_node
;
1129 if (!capable(CAP_NET_ADMIN
))
1131 if (down_interruptible(&ipn_glob_mutex
))
1132 return -ERESTARTSYS
;
1133 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
1134 dev
=__dev_get_by_name(ifr
->ifr_name
);
1136 dev
=__dev_get_by_name(&init_net
,ifr
->ifr_name
);
1139 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
1140 dev
=__dev_get_by_index(ifr
->ifr_ifindex
);
1142 dev
=__dev_get_by_index(&init_net
,ifr
->ifr_ifindex
);
1144 if (dev
&& (ipn_node
=ipn_netdev2node(dev
)) != NULL
)
1145 _ipn_setpersist(ipn_node
,value
);
1148 up(&ipn_glob_mutex
);
1154 int ipn_node_ioctl(struct ipn_node
*ipn_node
, unsigned int cmd
, unsigned long arg
) {
1155 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1157 (ipn_protocol_table
[ipn_node
->protocol
]->ipn_p_ioctl
!= NULL
)) {
1159 if (down_interruptible(&ipnn
->ipnn_mutex
))
1160 return -ERESTARTSYS
;
1161 rv
=ipn_protocol_table
[ipn_node
->protocol
]->ipn_p_ioctl(ipn_node
,cmd
,arg
);
1162 up(&ipnn
->ipnn_mutex
);
1168 static int ipn_ioctl(struct socket
*sock
, unsigned int cmd
, unsigned long arg
) {
1169 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1170 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1171 void __user
* argp
= (void __user
*)arg
;
1173 struct chrdevreq devr
;
1175 if (ipn_node
->shutdown
== SHUTDOWN_XMASK
)
1182 case IPN_SETPERSIST_NETDEV
:
1183 case IPN_CLRPERSIST_NETDEV
:
1184 case IPN_CONN_NETDEV
:
1185 case IPN_JOIN_NETDEV
:
1187 if (copy_from_user(&ifr
, argp
, sizeof(ifr
)))
1189 ifr
.ifr_name
[IFNAMSIZ
-1] = '\0';
1191 case IPN_REGISTER_CHRDEV
:
1192 case IPN_JOIN_CHRDEV
:
1193 if (copy_from_user(&devr
, argp
, sizeof(devr
)))
1195 /*printk("IPN_REGISTER_CHRDEV %p %d %d %d\n",
1196 argp, sizeof(devr),devr.major, devr.minor);*/
1200 /* actions for unconnected and unbound sockets */
1202 case IPN_SETPERSIST_NETDEV
:
1203 return ipn_setpersist_netdev(&ifr
,1);
1204 case IPN_CLRPERSIST_NETDEV
:
1205 return ipn_setpersist_netdev(&ifr
,0);
1206 case IPN_JOIN_CHRDEV
:
1209 if (!capable(CAP_MKNOD
))
1211 if (ipn_node
->ipn
!= NULL
)
1213 if (down_interruptible(&ipn_glob_mutex
))
1214 return -ERESTARTSYS
;
1215 rv
=ipn_node_bind(ipn_node
, ipn_find_chrdev(&devr
));
1216 up(&ipn_glob_mutex
);
1220 if (capable(CAP_NET_ADMIN
))
1222 if (ipn_node
->netdev
&& (ipn_node
->flags
&IPN_NODEFLAG_TAP
))
1223 return dev_set_mac_address(ipn_node
->netdev
, &ifr
.ifr_hwaddr
);
1225 return -EADDRNOTAVAIL
;
1227 if (ipnn
== NULL
|| (ipnn
->flags
& IPN_FLAG_TERMINATED
))
1229 /* actions for connected or bound sockets */
1231 case IPN_CONN_NETDEV
:
1232 return ipn_connect_netdev(sock
,&ifr
);
1233 case IPN_JOIN_NETDEV
:
1234 return ipn_join_netdev(sock
,&ifr
);
1235 case IPN_SETPERSIST
:
1236 return ipn_setpersist(ipn_node
,arg
);
1237 case IPN_CHRDEV_PERSIST
:
1238 return ipn_chrdev_persistence(ipnn
,arg
);
1239 case IPN_REGISTER_CHRDEV
:
1242 if (down_interruptible(&ipnn
->ipnn_mutex
))
1243 return -ERESTARTSYS
;
1244 rv
=ipn_register_chrdev(ipnn
,&devr
);
1245 if (devr
.major
==0 && rv
==0) {
1246 if (copy_to_user(&devr
, argp
, sizeof devr
))
1249 up(&ipnn
->ipnn_mutex
);
1252 case IPN_UNREGISTER_CHRDEV
:
1255 if (down_interruptible(&ipnn
->ipnn_mutex
))
1256 return -ERESTARTSYS
;
1257 rv
=ipn_deregister_chrdev(ipnn
);
1258 up(&ipnn
->ipnn_mutex
);
1262 return ipn_node_ioctl(ipn_node
, cmd
, arg
);
1266 /* shutdown: close socket for input or for output.
1267 * shutdown can be called prior to connect and it is not reversible */
1268 static int ipn_shutdown(struct socket
*sock
, int mode
) {
1269 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1270 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1271 int oldshutdown
=ipn_node
->shutdown
;
1272 mode
= (mode
+1)&(RCV_SHUTDOWN
|SEND_SHUTDOWN
);
1274 /* define the new mode first... */
1275 ipn_node
->shutdown
|= mode
;
1278 /* ... and wait for all pending ops to be completed */
1279 if (down_interruptible(&ipnn
->ipnn_mutex
)) {
1280 ipn_node
->shutdown
= oldshutdown
;
1281 return -ERESTARTSYS
;
1283 oldshutdown
=ipn_node
->shutdown
-oldshutdown
;
1284 if (sock
->state
== SS_CONNECTED
&& oldshutdown
) {
1285 ipn_net_update_counters(ipnn
,
1286 (ipn_node
->shutdown
& RCV_SHUTDOWN
)?0:-1,
1287 (ipn_node
->shutdown
& SEND_SHUTDOWN
)?0:-1);
1290 /* if recv channel has been shut down, flush the recv queue */
1291 if ((ipn_node
->shutdown
& RCV_SHUTDOWN
))
1292 ipn_flush_recvqueue(ipn_node
);
1293 up(&ipnn
->ipnn_mutex
);
1298 /* injectmsg: a new message is entering the ipn network.
1299 * injectmsg gets called by send and by the grab/tap node */
1300 /* handlemsg is protected from ipn_network changes: ipnn->ipnn_mutex is locked */
1301 int ipn_proto_injectmsg(struct ipn_node
*from
, struct msgpool_item
*msg
)
1303 struct ipn_network
*ipnn
=from
->ipn
;
1305 if (down_interruptible(&ipnn
->ipnn_mutex
))
1308 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_handlemsg(from
, msg
);
1309 up(&ipnn
->ipnn_mutex
);
1315 int ipn_node_write(struct ipn_node
*ipn_node
, struct iovec
*msg_iov
, int len
) {
1316 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1317 struct msgpool_item
*newmsg
;
1320 if (unlikely(ipn_node
->shutdown
& SEND_SHUTDOWN
)) {
1321 if (ipn_node
->shutdown
== SHUTDOWN_XMASK
)
1326 if (len
> ipnn
->mtu
)
1328 newmsg
=ipn_msgpool_alloc_locking(ipnn
,len
);
1332 err
=memcpy_fromiovec(newmsg
->data
, msg_iov
, len
);
1334 ipn_proto_injectmsg(ipn_node
, newmsg
);
1335 ipn_msgpool_put(newmsg
,ipnn
);
1339 static int ipn_sendmsg(struct kiocb
*kiocb
, struct socket
*sock
,
1340 struct msghdr
*msg
, size_t len
) {
1341 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1343 if (unlikely(sock
->state
!= SS_CONNECTED
))
1346 return ipn_node_write(ipn_node
, msg
->msg_iov
, len
);
1349 /* ipn_proto_sendmsg is called by protocol implementation to enqueue a
1350 * for a destination (to).*/
1351 /* enqueue a msgitem on the receiver's queue, the queue is protected by a
1352 * spinlock (msglock) */
1353 void ipn_proto_sendmsg(struct ipn_node
*to
, struct msgpool_item
*msg
)
1357 ipn_netdev_sendmsg(to
,msg
);
1360 struct msgitem
*msgitem
;
1361 struct ipn_network
*ipnn
=to
->ipn
;
1362 spin_lock(&to
->msglock
);
1363 if (likely((to
->shutdown
& RCV_SHUTDOWN
)==0)) {
1364 /*if (unlikely((ipnn->flags & IPN_FLAG_LOSSLESS) == 0 ||
1365 to->totmsgcount >= ipnn->msgpool_size))
1367 if (likely(ipnn
->flags
& IPN_FLAG_LOSSLESS
||
1368 to
->totmsgcount
< ipnn
->msgpool_size
)) {
1369 if ((msgitem
=kmem_cache_alloc(ipn_msgitem_cache
,GFP_KERNEL
))!=NULL
) {
1372 list_add_tail(&msgitem
->list
, &to
->msgqueue
);
1373 ipn_msgpool_hold(msg
);
1377 spin_unlock(&to
->msglock
);
1378 wake_up_interruptible(&to
->read_wait
);
1383 /* enqueue an oob message. "to" is the destination */
1384 void ipn_proto_oobsendmsg(struct ipn_node
*to
, struct msgpool_item
*msg
)
1387 if (!to
->netdev
) { /* no oob to netdev */
1388 struct msgitem
*msgitem
;
1389 struct ipn_network
*ipnn
=to
->ipn
;
1390 spin_lock(&to
->msglock
);
1391 if ((to
->shutdown
& RCV_SHUTDOWN_NO_OOB
) == 0 &&
1392 (ipnn
->flags
& IPN_FLAG_LOSSLESS
||
1393 to
->oobmsgcount
< ipnn
->msgpool_size
)) {
1394 if ((msgitem
=kmem_cache_alloc(ipn_msgitem_cache
,GFP_KERNEL
))!=NULL
) {
1398 list_add_tail(&msgitem
->list
, &to
->oobmsgqueue
);
1399 ipn_msgpool_hold(msg
);
1402 spin_unlock(&to
->msglock
);
1403 wake_up_interruptible(&to
->read_wait
);
1409 int ipn_node_read(struct ipn_node
*ipn_node
, struct iovec
*msg_iov
, size_t len
, int *msg_flags
, int flags
) {
1410 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1411 struct msgitem
*msgitem
;
1412 struct msgpool_item
*currmsg
;
1414 if (unlikely((ipn_node
->shutdown
& XRCV_SHUTDOWN
) == XRCV_SHUTDOWN
)) {
1415 if (ipn_node
->shutdown
== SHUTDOWN_XMASK
) /*EOF, nothing can be read*/
1418 return -EPIPE
; /*trying to read on a write only node */
1421 /* wait for a message */
1422 spin_lock(&ipn_node
->msglock
);
1423 while (ipn_node
->totmsgcount
== 0) {
1424 spin_unlock(&ipn_node
->msglock
);
1425 if (wait_event_interruptible(ipn_node
->read_wait
,
1426 !(ipn_node
->totmsgcount
== 0)))
1427 return -ERESTARTSYS
;
1428 spin_lock(&ipn_node
->msglock
);
1430 /* oob gets delivered first. oob are rare */
1431 if (likely(list_empty(&ipn_node
->oobmsgqueue
)))
1432 msgitem
=list_first_entry(&ipn_node
->msgqueue
, struct msgitem
, list
);
1434 msgitem
=list_first_entry(&ipn_node
->oobmsgqueue
, struct msgitem
, list
);
1435 *msg_flags
|= MSG_OOB
;
1436 ipn_node
->oobmsgcount
--;
1438 list_del(&msgitem
->list
);
1439 ipn_node
->totmsgcount
--;
1440 spin_unlock(&ipn_node
->msglock
);
1441 currmsg
=msgitem
->msg
;
1442 if (currmsg
->len
< len
)
1444 memcpy_toiovec(msg_iov
, currmsg
->data
, len
);
1445 ipn_msgpool_put(currmsg
,ipnn
);
1446 kmem_cache_free(ipn_msgitem_cache
,msgitem
);
1450 static int ipn_recvmsg(struct kiocb
*kiocb
, struct socket
*sock
,
1451 struct msghdr
*msg
, size_t len
, int flags
) {
1452 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1454 if (unlikely(sock
->state
!= SS_CONNECTED
))
1457 return ipn_node_read(ipn_node
, msg
->msg_iov
, len
, &msg
->msg_flags
, flags
);
1460 /* resize a network: change the # of communication ports (connport) */
1461 static int ipn_netresize(struct ipn_network
*ipnn
,int newsize
)
1464 struct ipn_node
**newconnport
;
1465 struct ipn_node
**oldconnport
;
1467 if (down_interruptible(&ipnn
->ipnn_mutex
))
1468 return -ERESTARTSYS
;
1469 oldsize
=ipnn
->maxports
;
1470 if (newsize
== oldsize
) {
1471 up(&ipnn
->ipnn_mutex
);
1475 /* shrink a network. all the ports we are going to eliminate
1476 * must be unused! */
1477 if (newsize
< oldsize
) {
1479 for (i
=newsize
; i
<oldsize
; i
++)
1480 if (ipnn
->connport
[i
]) {
1481 up(&ipnn
->ipnn_mutex
);
1486 oldconnport
=ipnn
->connport
;
1487 /* allocate the new connport array and copy the old one */
1488 newconnport
=kzalloc(newsize
* sizeof(struct ipn_node
*),GFP_KERNEL
);
1490 up(&ipnn
->ipnn_mutex
);
1493 memcpy(newconnport
,oldconnport
,min
* sizeof(struct ipn_node
*));
1494 ipnn
->connport
=newconnport
;
1495 ipnn
->maxports
=newsize
;
1496 /* notify the protocol that the netowrk has been resized */
1497 err
=ipn_protocol_table
[ipnn
->protocol
]->ipn_p_resizenet(ipnn
,oldsize
,newsize
);
1499 /* roll back if the resize operation failed for the protocol */
1500 ipnn
->connport
=oldconnport
;
1501 ipnn
->maxports
=oldsize
;
1504 /* successful mission, network resized */
1506 up(&ipnn
->ipnn_mutex
);
1510 /* IPN SETSOCKOPT */
1511 static int ipn_setsockopt(struct socket
*sock
, int level
, int optname
,
1512 char __user
*optval
, int optlen
) {
1513 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1514 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1516 if (ipn_node
->shutdown
== SHUTDOWN_XMASK
)
1518 if (level
!= 0 && level
!= ipn_node
->protocol
+1)
1519 return -EPROTONOSUPPORT
;
1521 /* protocol specific sockopt */
1524 if (down_interruptible(&ipnn
->ipnn_mutex
))
1525 return -ERESTARTSYS
;
1526 rv
=ipn_protocol_table
[ipn_node
->protocol
]->ipn_p_setsockopt(ipn_node
,optname
,optval
,optlen
);
1527 up(&ipnn
->ipnn_mutex
);
1532 if (optname
== IPN_SO_DESCR
) {
1533 if (optlen
> IPN_DESCRLEN
)
1536 memset(ipn_node
->descr
,0,IPN_DESCRLEN
);
1537 if (copy_from_user(ipn_node
->descr
,optval
,optlen
))
1538 ipn_node
->descr
[0]=0;
1540 ipn_node
->descr
[optlen
-1]=0;
1544 if (optlen
< sizeof(int))
1546 else if ((optname
& IPN_SO_PREBIND
) && (ipnn
!= NULL
))
1550 get_user(val
, (int __user
*) optval
);
1551 if ((optname
& IPN_SO_PREBIND
) && !ipn_node
->pbp
) {
1552 struct pre_bind_parms std
=STD_BIND_PARMS
;
1553 ipn_node
->pbp
=kzalloc(sizeof(struct pre_bind_parms
),GFP_KERNEL
);
1556 *(ipn_node
->pbp
)=std
;
1560 if (sock
->state
== SS_UNCONNECTED
)
1561 ipn_node
->portno
=val
;
1565 case IPN_SO_CHANGE_NUMNODES
:
1566 if ((ipn_node
->flags
& IPN_NODEFLAG_BOUND
)!=0) {
1570 return ipn_netresize(ipnn
,val
);
1574 case IPN_SO_WANT_OOB_NUMNODES
:
1576 ipn_node
->flags
|= IPN_NODEFLAG_OOB_NUMNODES
;
1578 ipn_node
->flags
&= ~IPN_NODEFLAG_OOB_NUMNODES
;
1580 case IPN_SO_HANDLE_OOB
:
1582 ipn_node
->shutdown
&= ~RCV_SHUTDOWN_NO_OOB
;
1584 ipn_node
->shutdown
|= RCV_SHUTDOWN_NO_OOB
;
1590 ipn_node
->pbp
->mtu
=val
;
1592 case IPN_SO_NUMNODES
:
1596 ipn_node
->pbp
->maxports
=val
;
1598 case IPN_SO_MSGPOOLSIZE
:
1602 ipn_node
->pbp
->msgpoolsize
=val
;
1605 ipn_node
->pbp
->flags
=val
;
1608 ipn_node
->pbp
->mode
=val
;
1617 /* IPN GETSOCKOPT */
1618 static int ipn_getsockopt(struct socket
*sock
, int level
, int optname
,
1619 char __user
*optval
, int __user
*optlen
) {
1620 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1621 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1624 if (ipn_node
->shutdown
== SHUTDOWN_XMASK
)
1626 if (level
!= 0 && level
!= ipn_node
->protocol
+1)
1627 return -EPROTONOSUPPORT
;
1631 /* protocol specific sockopt */
1632 if (down_interruptible(&ipnn
->ipnn_mutex
))
1633 return -ERESTARTSYS
;
1634 rv
=ipn_protocol_table
[ipn_node
->protocol
]->ipn_p_getsockopt(ipn_node
,optname
,optval
,optlen
);
1635 up(&ipnn
->ipnn_mutex
);
1640 if (get_user(len
, optlen
))
1642 if (optname
== IPN_SO_DESCR
) {
1643 if (len
< IPN_DESCRLEN
)
1646 if (len
> IPN_DESCRLEN
)
1648 if(put_user(len
, optlen
))
1650 if(copy_to_user(optval
,ipn_node
->descr
,len
))
1658 val
=ipn_node
->portno
;
1663 else if (ipn_node
->pbp
)
1664 val
=ipn_node
->pbp
->mtu
;
1666 case IPN_SO_NUMNODES
:
1669 else if (ipn_node
->pbp
)
1670 val
=ipn_node
->pbp
->maxports
;
1672 case IPN_SO_MSGPOOLSIZE
:
1674 val
=ipnn
->msgpool_size
;
1675 else if (ipn_node
->pbp
)
1676 val
=ipn_node
->pbp
->msgpoolsize
;
1681 else if (ipn_node
->pbp
)
1682 val
=ipn_node
->pbp
->flags
;
1687 else if (ipn_node
->pbp
)
1688 val
=ipn_node
->pbp
->mode
;
1694 if (len
< sizeof(int))
1698 if(put_user(len
, optlen
))
1700 if(copy_to_user(optval
,&val
,len
))
1709 /* BROADCAST/HUB implementation */
1711 static int ipn_bcast_newport(struct ipn_node
*newport
) {
1712 struct ipn_network
*ipnn
=newport
->ipn
;
1714 for (i
=0;i
<ipnn
->maxports
;i
++) {
1715 if (ipnn
->connport
[i
] == NULL
)
1721 static int ipn_bcast_handlemsg(struct ipn_node
*from
,
1722 struct msgpool_item
*msgitem
){
1723 struct ipn_network
*ipnn
=from
->ipn
;
1725 struct ipn_node
*ipn_node
;
1726 list_for_each_entry(ipn_node
, &ipnn
->connectqueue
, nodelist
) {
1727 if (ipn_node
!= from
)
1728 ipn_proto_sendmsg(ipn_node
,msgitem
);
1733 static void ipn_null_delport(struct ipn_node
*oldport
) {}
1734 static void ipn_null_postnewport(struct ipn_node
*newport
) {}
1735 static void ipn_null_predelport(struct ipn_node
*oldport
) {}
1736 static int ipn_null_newnet(struct ipn_network
*newnet
) {return 0;}
1737 static int ipn_null_resizenet(struct ipn_network
*net
,int oldsize
,int newsize
) {
1739 static void ipn_null_delnet(struct ipn_network
*oldnet
) {}
1740 static int ipn_null_setsockopt(struct ipn_node
*port
,int optname
,
1741 char __user
*optval
, int optlen
) {return -EOPNOTSUPP
;}
1742 static int ipn_null_getsockopt(struct ipn_node
*port
,int optname
,
1743 char __user
*optval
, int *optlen
) {return -EOPNOTSUPP
;}
1744 static int ipn_null_ioctl(struct ipn_node
*port
,unsigned int request
,
1745 unsigned long arg
) {return -EOPNOTSUPP
;}
1747 /* Protocol Registration/deregisteration */
1749 void ipn_init_protocol(struct ipn_protocol
*p
)
1751 if (p
->ipn_p_delport
== NULL
) p
->ipn_p_delport
=ipn_null_delport
;
1752 if (p
->ipn_p_postnewport
== NULL
) p
->ipn_p_postnewport
=ipn_null_postnewport
;
1753 if (p
->ipn_p_predelport
== NULL
) p
->ipn_p_predelport
=ipn_null_predelport
;
1754 if (p
->ipn_p_newnet
== NULL
) p
->ipn_p_newnet
=ipn_null_newnet
;
1755 if (p
->ipn_p_resizenet
== NULL
) p
->ipn_p_resizenet
=ipn_null_resizenet
;
1756 if (p
->ipn_p_delnet
== NULL
) p
->ipn_p_delnet
=ipn_null_delnet
;
1757 if (p
->ipn_p_setsockopt
== NULL
) p
->ipn_p_setsockopt
=ipn_null_setsockopt
;
1758 if (p
->ipn_p_getsockopt
== NULL
) p
->ipn_p_getsockopt
=ipn_null_getsockopt
;
1759 if (p
->ipn_p_ioctl
== NULL
) p
->ipn_p_ioctl
=ipn_null_ioctl
;
1762 int ipn_proto_register(int protocol
,struct ipn_protocol
*ipn_service
)
1765 if (ipn_service
->ipn_p_newport
== NULL
||
1766 ipn_service
->ipn_p_handlemsg
== NULL
)
1768 ipn_init_protocol(ipn_service
);
1769 if (protocol
> 1 && protocol
<= IPN_MAX_PROTO
) {
1771 if (down_interruptible(&ipn_glob_mutex
))
1772 return -ERESTARTSYS
;
1773 if (ipn_protocol_table
[protocol
])
1776 ipn_service
->refcnt
=0;
1777 ipn_protocol_table
[protocol
]=ipn_service
;
1778 printk(KERN_INFO
"IPN: Registered protocol %d\n",protocol
+1);
1780 up(&ipn_glob_mutex
);
1786 int ipn_proto_deregister(int protocol
)
1789 if (protocol
> 1 && protocol
<= IPN_MAX_PROTO
) {
1791 if (down_interruptible(&ipn_glob_mutex
))
1792 return -ERESTARTSYS
;
1793 if (ipn_protocol_table
[protocol
]) {
1794 if (ipn_protocol_table
[protocol
]->refcnt
== 0) {
1795 ipn_protocol_table
[protocol
]=NULL
;
1796 printk(KERN_INFO
"IPN: Unregistered protocol %d\n",protocol
+1);
1801 up(&ipn_glob_mutex
);
1808 /* Module constructor/destructor */
1809 static struct net_proto_family ipn_family_ops
= {
1811 .create
= ipn_create
,
1812 .owner
= THIS_MODULE
,
1815 /* IPN constructor */
1816 static int ipn_init(void)
1820 ipn_init_protocol(&ipn_bcast
);
1821 ipn_network_cache
=kmem_cache_create("ipn_network",sizeof(struct ipn_network
),0,0,NULL
);
1822 if (!ipn_network_cache
) {
1823 printk(KERN_CRIT
"%s: Cannot create ipn_network SLAB cache!\n",
1829 ipn_node_cache
=kmem_cache_create("ipn_node",sizeof(struct ipn_node
),0,0,NULL
);
1830 if (!ipn_node_cache
) {
1831 printk(KERN_CRIT
"%s: Cannot create ipn_node SLAB cache!\n",
1837 ipn_msgitem_cache
=kmem_cache_create("ipn_msgitem",sizeof(struct msgitem
),0,0,NULL
);
1838 if (!ipn_msgitem_cache
) {
1839 printk(KERN_CRIT
"%s: Cannot create ipn_msgitem SLAB cache!\n",
1845 rc
=ipn_msgbuf_init();
1847 printk(KERN_CRIT
"%s: Cannot create ipn_msgbuf SLAB cache\n",
1849 goto out_net_node_msg
;
1852 rc
=proto_register(&ipn_proto
,1);
1854 printk(KERN_CRIT
"%s: Cannot register the protocol!\n",
1856 goto out_net_node_msg_msgbuf
;
1859 sock_register(&ipn_family_ops
);
1861 printk(KERN_INFO
"IPN: Virtual Square Project, University of Bologna 2007-09\n");
1864 out_net_node_msg_msgbuf
:
1867 kmem_cache_destroy(ipn_msgitem_cache
);
1869 kmem_cache_destroy(ipn_node_cache
);
1871 kmem_cache_destroy(ipn_network_cache
);
1876 /* IPN destructor */
1877 static void ipn_exit(void)
1880 if (ipn_msgitem_cache
)
1881 kmem_cache_destroy(ipn_msgitem_cache
);
1883 kmem_cache_destroy(ipn_node_cache
);
1884 if (ipn_network_cache
)
1885 kmem_cache_destroy(ipn_network_cache
);
1887 sock_unregister(PF_IPN
);
1888 proto_unregister(&ipn_proto
);
1889 printk(KERN_INFO
"IPN removed\n");
1892 module_init(ipn_init
);
1893 module_exit(ipn_exit
);
1895 EXPORT_SYMBOL_GPL(ipn_proto_register
);
1896 EXPORT_SYMBOL_GPL(ipn_proto_deregister
);
1897 EXPORT_SYMBOL_GPL(ipn_proto_sendmsg
);
1898 EXPORT_SYMBOL_GPL(ipn_proto_oobsendmsg
);
1899 EXPORT_SYMBOL_GPL(ipn_msgpool_alloc
);
1900 EXPORT_SYMBOL_GPL(ipn_msgpool_put
);