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,30)
47 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,32)
50 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,33)
53 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,37)
56 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,39)
60 /*extension of RCV_SHUTDOWN defined in include/net/sock.h
61 * when the bit is set recv fails */
62 /* NO_OOB: do not send OOB */
63 #define RCV_SHUTDOWN_NO_OOB 4
64 /* EXTENDED MASK including OOB */
65 #define SHUTDOWN_XMASK (SHUTDOWN_MASK | RCV_SHUTDOWN_NO_OOB)
66 /* if XRCV_SHUTDOWN is all set recv fails */
67 #define XRCV_SHUTDOWN (RCV_SHUTDOWN | RCV_SHUTDOWN_NO_OOB)
69 /* Global MUTEX: this is locked to add/delete/modify networks
70 * this is *not* locked just to send/receive msgs */
72 static DECLARE_MUTEX(ipn_glob_mutex
);
74 static DEFINE_SEMAPHORE(ipn_glob_mutex
);
76 /* Network table and hash */
77 struct hlist_head ipn_network_table
[IPN_HASH_SIZE
+ 1];
78 /* slab(s) for fast data structure allocation */
79 static struct kmem_cache
*ipn_network_cache
;
80 static struct kmem_cache
*ipn_node_cache
;
81 static struct kmem_cache
*ipn_msgitem_cache
;
83 /* Protocol 1: HUB/Broadcast default protocol. Function Prototypes */
84 static int ipn_bcast_newport(struct ipn_node
*newport
);
85 static int ipn_bcast_handlemsg(struct ipn_node
*from
,
86 struct msgpool_item
*msgitem
);
88 /* default protocol IPN_BROADCAST (0) */
89 static struct ipn_protocol ipn_bcast
= {
91 .ipn_p_newport
=ipn_bcast_newport
,
92 .ipn_p_handlemsg
=ipn_bcast_handlemsg
};
94 static struct ipn_protocol
*ipn_protocol_table
[IPN_MAX_PROTO
]={&ipn_bcast
};
96 /* Socket call function prototypes */
97 static int ipn_release(struct socket
*);
98 static int ipn_bind(struct socket
*, struct sockaddr
*, int);
99 static int ipn_connect(struct socket
*, struct sockaddr
*,
100 int addr_len
, int flags
);
101 static int ipn_getname(struct socket
*, struct sockaddr
*, int *, int);
102 static unsigned int ipn_poll(struct file
*, struct socket
*, poll_table
*);
103 static int ipn_ioctl(struct socket
*, unsigned int, unsigned long);
104 static int ipn_shutdown(struct socket
*, int);
105 static int ipn_sendmsg(struct kiocb
*, struct socket
*,
106 struct msghdr
*, size_t);
107 static int ipn_recvmsg(struct kiocb
*, struct socket
*,
108 struct msghdr
*, size_t, int);
110 static int ipn_setsockopt(struct socket
*sock
, int level
, int optname
,
111 char __user
*optval
, unsigned int optlen
);
113 static int ipn_setsockopt(struct socket
*sock
, int level
, int optname
,
114 char __user
*optval
, int optlen
);
116 static int ipn_getsockopt(struct socket
*sock
, int level
, int optname
,
117 char __user
*optval
, int __user
*optlen
);
119 /* Network table Management
120 * inode->ipn_network hash table
121 * protected by ipn_glob_mutex */
122 static inline void ipn_insert_network(struct hlist_head
*list
, struct ipn_network
*ipnn
)
124 hlist_add_head(&ipnn
->hnode
, list
);
127 static inline void ipn_remove_network(struct ipn_network
*ipnn
)
129 hlist_del(&ipnn
->hnode
);
132 static struct ipn_network
*ipn_find_network_byinode(struct inode
*i
)
134 struct ipn_network
*ipnn
;
135 struct hlist_node
*node
;
137 hlist_for_each_entry(ipnn
, node
,
138 &ipn_network_table
[i
->i_ino
& (IPN_HASH_SIZE
- 1)], hnode
) {
139 struct dentry
*dentry
= ipnn
->dentry
;
141 if(dentry
&& dentry
->d_inode
== i
)
147 struct ipn_network
*ipn_find_network_byfun(
148 int (*fun
)(struct ipn_network
*,void *),void *funarg
)
150 struct ipn_network
*ipnn
;
151 struct hlist_node
*node
;
154 for (ipn_table_scan
=0;ipn_table_scan
<IPN_HASH_SIZE
;ipn_table_scan
++) {
155 hlist_for_each_entry(ipnn
, node
, &ipn_network_table
[ipn_table_scan
], hnode
) {
163 /* msgpool management
164 * msgpool_item are ipn_network dependent (each net has its own MTU)
165 * for each message sent there is one msgpool_item and many struct msgitem
166 * one for each receipient.
167 * msgitem are connected to the node's msgqueue or oobmsgqueue.
168 * when a message is delivered to a process the msgitem is deleted and
169 * the count of the msgpool_item is decreased.
170 * msgpool_item elements gets deleted automatically when count is 0*/
173 struct list_head list
;
174 struct msgpool_item
*msg
;
177 /* alloc a fresh msgpool item. count is set to 1.
180 * for each receipient
181 * enqueue messages to the process (using msgitem), ipn_msgpool_hold
183 * The message can be delivered concurrently. init count to 1 guarantees
184 * that it survives at least until is has been enqueued to all
186 /* msgpool_cache == NULL when the ipn network is using the flexible allocation,
187 * i.e. the msgpool item gets allocated by kmalloc instead of using slab */
188 static inline struct msgpool_item
*_ipn_msgpool_alloc(struct ipn_network
*ipnn
, int len
)
190 struct msgpool_item
*new;
191 if ((new=ipnn
->msgpool_cache
?
192 kmem_cache_alloc(ipnn
->msgpool_cache
,GFP_KERNEL
):
193 kmalloc(sizeof(struct msgpool_item
)+len
,GFP_KERNEL
)) != NULL
) {
194 atomic_set(&new->count
,1);
195 atomic_inc(&ipnn
->msgpool_nelem
);
200 /* ipn_msgpool_alloc returns null when leaky, lossless and no space is left
201 * for new msgpool items. When !leaky, it can allocate more msgpooli items,
202 * this temporary overassignment blocks new messages to be delivered until
203 * there is space for a msgpool item to be allocated */
204 struct msgpool_item
*ipn_msgpool_alloc(struct ipn_network
*ipnn
,int leaky
,int len
)
206 if (leaky
&& (ipnn
->flags
& IPN_FLAG_LOSSLESS
) &&
207 atomic_read(&ipnn
->msgpool_nelem
) < ipnn
->msgpool_size
)
210 return _ipn_msgpool_alloc(ipnn
,len
);
213 /* If the service il LOSSLESS, this msgpool call waits for an
214 * available msgpool item */
215 static inline struct msgpool_item
*ipn_msgpool_alloc_locking(struct ipn_network
*ipnn
,int len
)
217 if (ipnn
->flags
& IPN_FLAG_LOSSLESS
) {
218 while (atomic_read(&ipnn
->msgpool_nelem
) >= ipnn
->msgpool_size
) {
219 if (wait_event_interruptible_exclusive(ipnn
->send_wait
,
220 atomic_read(&ipnn
->msgpool_nelem
) < ipnn
->msgpool_size
))
224 return _ipn_msgpool_alloc(ipnn
,len
);
227 /* register one more user for this msgpool item (i.e. count++) */
228 static inline void ipn_msgpool_hold(struct msgpool_item
*msg
)
230 atomic_inc(&msg
->count
);
233 /* decrease count and delete msgpool_item if count == 0 */
234 void ipn_msgpool_put(struct msgpool_item
*old
,
235 struct ipn_network
*ipnn
)
237 if (atomic_dec_and_test(&old
->count
)) {
238 if (ipnn
->msgpool_cache
)
239 kmem_cache_free(ipnn
->msgpool_cache
,old
);
242 atomic_dec(&ipnn
->msgpool_nelem
);
243 if (ipnn
->flags
& IPN_FLAG_LOSSLESS
) /* this could be done anyway */
244 wake_up_interruptible(&ipnn
->send_wait
);
249 static const struct proto_ops ipn_ops
= {
251 .owner
= THIS_MODULE
,
252 .release
= ipn_release
,
254 .connect
= ipn_connect
,
255 .socketpair
= sock_no_socketpair
,
256 .accept
= sock_no_accept
,
257 .getname
= ipn_getname
,
260 .listen
= sock_no_listen
,
261 .shutdown
= ipn_shutdown
,
262 .setsockopt
= ipn_setsockopt
,
263 .getsockopt
= ipn_getsockopt
,
264 .sendmsg
= ipn_sendmsg
,
265 .recvmsg
= ipn_recvmsg
,
266 .mmap
= sock_no_mmap
,
267 .sendpage
= sock_no_sendpage
,
270 static struct proto ipn_proto
= {
272 .owner
= THIS_MODULE
,
273 .obj_size
= sizeof(struct ipn_sock
),
276 /* create a new ipn_node */
277 struct ipn_node
*ipn_node_create(struct net
*net
)
279 struct ipn_node
*ipn_node
;
280 ipn_node
=kmem_cache_alloc(ipn_node_cache
,GFP_KERNEL
);
281 if (ipn_node
!=NULL
) {
282 INIT_LIST_HEAD(&ipn_node
->nodelist
);
283 ipn_node
->protocol
=0; /* BROADCAST, caller must set a proper value */
284 ipn_node
->flags
=IPN_NODEFLAG_INUSE
;
285 ipn_node
->shutdown
=RCV_SHUTDOWN_NO_OOB
;
286 ipn_node
->descr
[0]=0;
287 ipn_node
->portno
=IPN_PORTNO_ANY
;
289 ipn_node
->netdev
=NULL
;
291 ipn_node
->proto_private
=NULL
;
292 ipn_node
->totmsgcount
=0;
293 ipn_node
->oobmsgcount
=0;
294 spin_lock_init(&ipn_node
->msglock
);
295 INIT_LIST_HEAD(&ipn_node
->msgqueue
);
296 INIT_LIST_HEAD(&ipn_node
->oobmsgqueue
);
298 init_waitqueue_head(&ipn_node
->read_wait
);
305 * ipn_node is a separate structure, pointed by ipn_sock -> node
306 * when a node is "persistent", ipn_node survives while ipn_sock gets released*/
308 static int ipn_create(struct net
*net
,struct socket
*sock
, int protocol
)
310 static int ipn_create(struct net
*net
,struct socket
*sock
,
311 int protocol
, int kern
)
314 struct ipn_sock
*ipn_sk
;
316 if (net
!= &init_net
)
317 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 ipn_sk
= (struct ipn_sock
*) sk_alloc(net
, PF_IPN
, GFP_KERNEL
, &ipn_proto
);
332 if ((ipn_sk
->node
=ipn_node_create(net
))==NULL
) {
333 sock_put((struct sock
*) ipn_sk
);
336 ipn_sk
->node
->protocol
=protocol
;
337 sock_init_data(sock
,(struct sock
*) ipn_sk
);
338 sock
->state
= SS_UNCONNECTED
;
339 sock
->ops
= &ipn_ops
;
340 sock
->sk
=(struct sock
*)ipn_sk
;
344 /* update # of readers and # of writers counters for an ipn network.
345 * This function sends oob messages to nodes requesting the service */
346 /* LOCKING ipnn_mutex is locked */
347 static void ipn_net_update_counters(struct ipn_network
*ipnn
,
348 int chg_readers
, int chg_writers
) {
349 ipnn
->numreaders
+= chg_readers
;
350 ipnn
->numwriters
+= chg_writers
;
351 if (ipnn
->mtu
>= sizeof(struct numnode_oob
))
353 struct msgpool_item
*ipn_msg
=_ipn_msgpool_alloc(ipnn
,sizeof(struct numnode_oob
));
355 struct numnode_oob
*oob_msg
=(struct numnode_oob
*)(ipn_msg
->data
);
356 struct ipn_node
*ipn_node
;
357 ipn_msg
->len
=sizeof(struct numnode_oob
);
358 oob_msg
->level
=IPN_ANY
;
359 oob_msg
->tag
=IPN_OOB_NUMNODE_TAG
;
360 oob_msg
->numreaders
=ipnn
->numreaders
;
361 oob_msg
->numwriters
=ipnn
->numwriters
;
362 list_for_each_entry(ipn_node
, &ipnn
->connectqueue
, nodelist
) {
363 if (ipn_node
->flags
& IPN_NODEFLAG_OOB_NUMNODES
)
364 ipn_proto_oobsendmsg(ipn_node
,ipn_msg
);
366 ipn_msgpool_put(ipn_msg
,ipnn
);
371 /* flush pending messages (for close and shutdown RCV) */
372 /* LOCKING: ipnn_mutex is locked */
373 static void ipn_flush_recvqueue(struct ipn_node
*ipn_node
)
375 struct ipn_network
*ipnn
=ipn_node
->ipn
;
376 spin_lock(&ipn_node
->msglock
);
377 while (!list_empty(&ipn_node
->msgqueue
)) {
378 struct msgitem
*msgitem
=
379 list_first_entry(&ipn_node
->msgqueue
, struct msgitem
, list
);
380 list_del(&msgitem
->list
);
381 ipn_node
->totmsgcount
--;
382 ipn_msgpool_put(msgitem
->msg
,ipnn
);
383 kmem_cache_free(ipn_msgitem_cache
,msgitem
);
385 spin_unlock(&ipn_node
->msglock
);
388 /* flush pending oob messages (for socket close) */
389 /* LOCKING: ipnn_mutex is locked */
390 static void ipn_flush_oobrecvqueue(struct ipn_node
*ipn_node
)
392 struct ipn_network
*ipnn
=ipn_node
->ipn
;
393 spin_lock(&ipn_node
->msglock
);
394 while (!list_empty(&ipn_node
->oobmsgqueue
)) {
395 struct msgitem
*msgitem
=
396 list_first_entry(&ipn_node
->oobmsgqueue
, struct msgitem
, list
);
397 list_del(&msgitem
->list
);
398 ipn_node
->totmsgcount
--;
399 ipn_node
->oobmsgcount
--;
400 ipn_msgpool_put(msgitem
->msg
,ipnn
);
401 kmem_cache_free(ipn_msgitem_cache
,msgitem
);
403 spin_unlock(&ipn_node
->msglock
);
406 /* Terminate node. The node is "logically" terminated. */
407 /* LOCKING: ipn_glob_lock must be locked here */
408 static int ipn_terminate_node(struct ipn_node
*ipn_node
)
410 struct ipn_network
*ipnn
=ipn_node
->ipn
;
412 if (down_interruptible(&ipnn
->ipnn_mutex
))
414 if (ipn_node
->portno
>= 0) {
415 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_predelport(ipn_node
);
416 ipnn
->connport
[ipn_node
->portno
]=NULL
;
418 list_del(&ipn_node
->nodelist
);
419 ipn_flush_recvqueue(ipn_node
);
420 ipn_flush_oobrecvqueue(ipn_node
);
421 if (ipn_node
->portno
>= 0)
422 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_delport(ipn_node
);
424 ipn_net_update_counters(ipnn
,
425 (ipn_node
->shutdown
& RCV_SHUTDOWN
)?0:-1,
426 (ipn_node
->shutdown
& SEND_SHUTDOWN
)?0:-1);
427 ipn_node
->shutdown
= SHUTDOWN_XMASK
;
428 up(&ipnn
->ipnn_mutex
);
429 if (ipn_node
->netdev
)
430 ipn_netdev_close(ipn_node
);
431 /* No more network elements */
433 if (ipnn
->refcnt
== 0 && !ipn_is_persistent_chrdev(ipnn
))
436 ipn_deregister_chrdev(ipnn
);
437 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_delnet(ipnn
);
438 ipn_remove_network(ipnn
);
439 ipn_protocol_table
[ipnn
->protocol
]->refcnt
--;
444 if (ipnn
->msgpool_cache
)
445 ipn_msgbuf_put(ipnn
->msgpool_cache
);
447 kfree(ipnn
->connport
);
448 kmem_cache_free(ipn_network_cache
, ipnn
);
449 module_put(THIS_MODULE
);
453 kfree(ipn_node
->pbp
);
459 /* release of an ipn_node */
460 int ipn_node_release(struct ipn_node
*ipn_node
)
463 if (down_interruptible(&ipn_glob_mutex
))
465 if (ipn_node
->flags
& IPN_NODEFLAG_PERSIST
) {
466 ipn_node
->flags
&= ~IPN_NODEFLAG_INUSE
;
470 rv
=ipn_terminate_node(ipn_node
);
474 kmem_cache_free(ipn_node_cache
,ipn_node
);
480 /* release of an ipn socket */
481 static int ipn_release (struct socket
*sock
)
483 struct ipn_sock
*ipn_sk
=(struct ipn_sock
*)sock
->sk
;
484 struct ipn_node
*ipn_node
=ipn_sk
->node
;
485 int rv
=ipn_node_release(ipn_node
);
487 sock_put((struct sock
*) ipn_sk
);
491 /* _set persist, change the persistence of a node,
492 * when persistence gets cleared and the node is no longer used
493 * the node is terminated and freed.
494 * ipn_glob_mutex must be locked */
495 static int _ipn_setpersist(struct ipn_node
*ipn_node
, int persist
)
499 ipn_node
->flags
|= IPN_NODEFLAG_PERSIST
;
501 ipn_node
->flags
&= ~IPN_NODEFLAG_PERSIST
;
502 if (!(ipn_node
->flags
& IPN_NODEFLAG_INUSE
)) {
503 rv
=ipn_terminate_node(ipn_node
);
505 kmem_cache_free(ipn_node_cache
,ipn_node
);
512 * lock ipn_glob_mutex and call __ipn_setpersist above */
513 static int ipn_setpersist(struct ipn_node
*ipn_node
, int persist
)
516 if (ipn_node
->netdev
== NULL
)
518 if (down_interruptible(&ipn_glob_mutex
))
520 rv
=_ipn_setpersist(ipn_node
,persist
);
525 /* several network parameters can be set by setsockopt prior to bind */
526 /* struct pre_bind_parms is a temporary stucture connected to ipn_node->pbp
527 * to keep the parameter values. */
528 struct pre_bind_parms
{
529 unsigned short maxports
;
530 unsigned short flags
;
531 unsigned short msgpoolsize
;
536 /* STD_PARMS: BITS_PER_LONG nodes, no flags, BITS_PER_BYTE pending msgs,
537 * Ethernet + VLAN MTU*/
538 #define STD_BIND_PARMS {BITS_PER_LONG, 0, BITS_PER_BYTE, 1514, 0777};
540 static int ipn_mkname(struct sockaddr_un
* sunaddr
, int len
)
542 if (len
<= sizeof(short) || len
> sizeof(*sunaddr
))
544 if (!sunaddr
|| sunaddr
->sun_family
!= AF_IPN
)
547 * This may look like an off by one error but it is a bit more
548 * subtle. 108 is the longest valid AF_IPN path for a binding.
549 * sun_path[108] doesnt as such exist. However in kernel space
550 * we are guaranteed that it is a valid memory location in our
551 * kernel address buffer.
553 ((char *)sunaddr
)[len
]=0;
554 len
= strlen(sunaddr
->sun_path
)+1+sizeof(short);
558 static int ipn_node_bind(struct ipn_node
*ipn_node
, struct ipn_network
*ipnn
)
562 if (ipn_node
->ipn
!= NULL
)
565 list_add_tail(&ipn_node
->nodelist
,&ipnn
->unconnectqueue
);
567 ipn_node
->flags
|= IPN_NODEFLAG_BOUND
;
572 static int ipn_bind(struct socket
*sock
, struct sockaddr
*uaddr
, int addr_len
)
574 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)uaddr
;
575 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
577 struct ipn_network
*ipnn
;
578 struct dentry
* dentry
= NULL
;
580 struct pre_bind_parms parms
=STD_BIND_PARMS
;
582 //printk("IPN bind\n");
584 if (down_interruptible(&ipn_glob_mutex
))
586 if (sock
->state
!= SS_UNCONNECTED
||
587 ipn_node
->ipn
!= NULL
) {
592 if (ipn_node
->protocol
>= 0 &&
593 (ipn_node
->protocol
>= IPN_MAX_PROTO
||
594 ipn_protocol_table
[ipn_node
->protocol
] == NULL
)) {
595 err
= -EPROTONOSUPPORT
;
599 addr_len
= ipn_mkname(sunaddr
, addr_len
);
605 /* check if there is already an ipn-network socket with that name */
607 err
= kern_path(sunaddr
->sun_path
, LOOKUP_FOLLOW
, &nd
.path
);
609 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? */
615 err
= kern_path_parent(sunaddr
->sun_path
, &nd
);
617 err
= path_lookup(sunaddr
->sun_path
, LOOKUP_PARENT
, &nd
);
620 goto out_mknod_parent
;
621 /* Do I have the permission to create a file? */
622 dentry
= lookup_create(&nd
, 0);
623 err
= PTR_ERR(dentry
);
625 goto out_mknod_unlock
;
627 * All right, let's create it.
630 mode
= ipn_node
->pbp
->mode
;
632 mode
= SOCK_INODE(sock
)->i_mode
;
634 mode
= S_IFSOCK
| (mode
& ~current
->fs
->umask
);
636 mode
= S_IFSOCK
| (mode
& ~current_umask());
639 err
= vfs_mknod(nd
.path
.dentry
->d_inode
, dentry
, nd
.path
.mnt
, mode
, 0);
641 err
= vfs_mknod(nd
.path
.dentry
->d_inode
, dentry
, mode
, 0);
645 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
646 dput(nd
.path
.dentry
);
647 nd
.path
.dentry
= dentry
;
648 /* create a new ipn_network item */
650 parms
=*ipn_node
->pbp
;
651 ipnn
=kmem_cache_zalloc(ipn_network_cache
,GFP_KERNEL
);
654 goto out_mknod_dput_ipnn
;
656 ipnn
->connport
=kzalloc(parms
.maxports
* sizeof(struct ipn_node
*),GFP_KERNEL
);
657 if (!ipnn
->connport
) {
659 goto out_mknod_dput_ipnn2
;
662 /* module refcnt is incremented for each network, thus
663 * rmmod is forbidden if there are persistent nodes */
664 if (!try_module_get(THIS_MODULE
)) {
666 goto out_mknod_dput_ipnn2
;
668 memcpy(&ipnn
->sunaddr
,sunaddr
,addr_len
);
670 ipnn
->flags
=parms
.flags
;
671 if (ipnn
->flags
& IPN_FLAG_FLEXMTU
)
672 ipnn
->msgpool_cache
= NULL
;
674 ipnn
->msgpool_cache
=ipn_msgbuf_get(ipnn
->mtu
);
675 if (!ipnn
->msgpool_cache
) {
677 goto out_mknod_dput_putmodule
;
680 INIT_LIST_HEAD(&ipnn
->unconnectqueue
);
681 INIT_LIST_HEAD(&ipnn
->connectqueue
);
683 ipnn
->dentry
=nd
.path
.dentry
;
684 ipnn
->mnt
=nd
.path
.mnt
;
685 sema_init(&ipnn
->ipnn_mutex
,1);
686 ipnn
->sunaddr_len
=addr_len
;
687 ipnn
->protocol
=ipn_node
->protocol
;
688 if (ipnn
->protocol
< 0) ipnn
->protocol
= 0;
689 ipn_protocol_table
[ipnn
->protocol
]->refcnt
++;
692 ipnn
->maxports
=parms
.maxports
;
693 atomic_set(&ipnn
->msgpool_nelem
,0);
694 ipnn
->msgpool_size
=parms
.msgpoolsize
;
695 ipnn
->proto_private
=NULL
;
696 init_waitqueue_head(&ipnn
->send_wait
);
698 err
=ipn_protocol_table
[ipnn
->protocol
]->ipn_p_newnet(ipnn
);
700 goto out_mknod_dput_putmodule
;
701 ipn_insert_network(&ipn_network_table
[nd
.path
.dentry
->d_inode
->i_ino
& (IPN_HASH_SIZE
-1)],ipnn
);
703 /* join an existing network */
704 if (parms
.flags
& IPN_FLAG_EXCL
) {
708 err
= inode_permission(nd
.path
.dentry
->d_inode
, MAY_EXEC
);
712 if (!S_ISSOCK(nd
.path
.dentry
->d_inode
->i_mode
))
714 ipnn
=ipn_find_network_byinode(nd
.path
.dentry
->d_inode
);
715 if (!ipnn
|| (ipnn
->flags
& IPN_FLAG_TERMINATED
) ||
716 (ipnn
->flags
& IPN_FLAG_EXCL
))
720 kfree(ipn_node
->pbp
);
723 ipn_node_bind(ipn_node
,ipnn
);
733 out_mknod_dput_putmodule
:
734 module_put(THIS_MODULE
);
735 out_mknod_dput_ipnn2
:
736 kfree(ipnn
->connport
);
738 kmem_cache_free(ipn_network_cache
,ipnn
);
742 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
752 static int ipn_connect(struct socket
*sock
, struct sockaddr
*addr
,
753 int addr_len
, int flags
){
754 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)addr
;
755 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
760 #define ndpath nd.path
763 struct ipn_network
*ipnn
,*previousipnn
;
767 /* the socket cannot be connected twice */
768 if (sock
->state
!= SS_UNCONNECTED
)
771 if (down_interruptible(&ipn_glob_mutex
))
774 if ((previousipnn
=ipn_node
->ipn
) == NULL
) { /* unbound */
775 unsigned char mustshutdown
=0;
776 err
= ipn_mkname(sunaddr
, addr_len
);
781 err
= kern_path(sunaddr
->sun_path
, LOOKUP_FOLLOW
, &ndpath
);
783 err
= path_lookup(sunaddr
->sun_path
, LOOKUP_FOLLOW
, &nd
);
787 err
= inode_permission(ndpath
.dentry
->d_inode
, MAY_READ
);
789 if (err
== -EACCES
|| err
== -EROFS
)
790 mustshutdown
|=RCV_SHUTDOWN
;
794 err
= inode_permission(ndpath
.dentry
->d_inode
, MAY_WRITE
);
797 mustshutdown
|=SEND_SHUTDOWN
;
801 mustshutdown
|= ipn_node
->shutdown
;
802 /* if the combination of shutdown and permissions leaves
803 * no abilities, connect returns EACCES */
804 if (mustshutdown
== SHUTDOWN_XMASK
) {
809 ipn_node
->shutdown
=mustshutdown
;
811 if (!S_ISSOCK(ndpath
.dentry
->d_inode
->i_mode
)) {
815 ipnn
=ipn_find_network_byinode(ndpath
.dentry
->d_inode
);
816 if (!ipnn
|| (ipnn
->flags
& IPN_FLAG_TERMINATED
)) {
820 if (ipn_node
->protocol
== IPN_ANY
)
821 ipn_node
->protocol
=ipnn
->protocol
;
822 else if (ipnn
->protocol
!= ipn_node
->protocol
) {
831 if (down_interruptible(&ipnn
->ipnn_mutex
)) {
835 portno
= ipn_protocol_table
[ipnn
->protocol
]->ipn_p_newport(ipn_node
);
836 if (portno
>= 0 && portno
<ipnn
->maxports
) {
837 sock
->state
= SS_CONNECTED
;
838 ipn_node
->portno
=portno
;
839 ipnn
->connport
[portno
]=ipn_node
;
840 if (!(ipn_node
->flags
& IPN_NODEFLAG_BOUND
))
843 list_del(&ipn_node
->nodelist
);
844 list_add_tail(&ipn_node
->nodelist
,&ipnn
->connectqueue
);
845 ipn_net_update_counters(ipnn
,
846 (ipn_node
->shutdown
& RCV_SHUTDOWN
)?0:1,
847 (ipn_node
->shutdown
& SEND_SHUTDOWN
)?0:1);
849 ipn_node
->ipn
=previousipnn
; /* undo changes on ipn_node->ipn */
852 up(&ipnn
->ipnn_mutex
);
863 int ipn_node_create_connect(struct ipn_node
**ipn_node_out
,
864 struct ipn_network
*(* ipnn_map
)(void *),void *ipnn_map_arg
) {
865 struct ipn_node
*ipn_node
;
866 struct ipn_network
*ipnn
;
869 ipn_node
=ipn_node_create(&init_net
);
870 if (down_interruptible(&ipn_glob_mutex
)) {
872 goto err_ipn_node_release
;
874 ipnn
=ipnn_map(ipnn_map_arg
);
876 ipn_node
->protocol
=ipnn
->protocol
;
877 if (down_interruptible(&ipnn
->ipnn_mutex
)) {
881 portno
= ipn_protocol_table
[ipnn
->protocol
]->ipn_p_newport(ipn_node
);
882 if (portno
>= 0 && portno
<ipnn
->maxports
) {
883 ipn_node
->portno
=portno
;
884 ipnn
->connport
[portno
]=ipn_node
;
885 if (!(ipn_node
->flags
& IPN_NODEFLAG_BOUND
)) {
887 list_del(&ipn_node
->nodelist
);
889 *ipn_node_out
=ipn_node
;
890 list_add_tail(&ipn_node
->nodelist
,&ipnn
->connectqueue
);
891 ipn_net_update_counters(ipnn
,
892 (ipn_node
->shutdown
& RCV_SHUTDOWN
)?0:1,
893 (ipn_node
->shutdown
& SEND_SHUTDOWN
)?0:1);
900 up(&ipnn
->ipnn_mutex
);
904 up(&ipnn
->ipnn_mutex
);
907 err_ipn_node_release
:
908 ipn_node_release(ipn_node
);
913 int ipn_node_connect(struct ipn_node
*ipn_node
)
915 struct ipn_network
*ipnn
;
918 if (down_interruptible(&ipn_glob_mutex
))
922 if (down_interruptible(&ipnn
->ipnn_mutex
)) {
926 portno
= ipn_protocol_table
[ipnn
->protocol
]->ipn_p_newport(ipn_node
);
927 if (portno
>= 0 && portno
<ipnn
->maxports
) {
928 ipn_node
->portno
=portno
;
929 ipnn
->connport
[portno
]=ipn_node
;
930 if (!(ipn_node
->flags
& IPN_NODEFLAG_BOUND
)) {
932 list_del(&ipn_node
->nodelist
);
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);
943 up(&ipnn
->ipnn_mutex
);
951 static int ipn_getname(struct socket
*sock
, struct sockaddr
*uaddr
,
952 int *uaddr_len
, int peer
) {
953 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
954 struct ipn_network
*ipnn
=ipn_node
->ipn
;
955 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)uaddr
;
958 if (down_interruptible(&ipn_glob_mutex
))
961 *uaddr_len
= ipnn
->sunaddr_len
;
962 memcpy(sunaddr
,&ipnn
->sunaddr
,*uaddr_len
);
970 unsigned int ipn_node_poll(struct ipn_node
*ipn_node
, struct file
*file
, poll_table
*wait
) {
971 struct ipn_network
*ipnn
=ipn_node
->ipn
;
975 poll_wait(file
,&ipn_node
->read_wait
,wait
);
976 if (ipnn
->flags
& IPN_FLAG_LOSSLESS
)
977 poll_wait(file
,&ipnn
->send_wait
,wait
);
978 /* POLLIN if recv succeeds,
979 * POLL{PRI,RDNORM} if there are {oob,non-oob} messages */
980 if (ipn_node
->totmsgcount
> 0) mask
|= POLLIN
;
981 if (!(list_empty(&ipn_node
->msgqueue
))) mask
|= POLLRDNORM
;
982 if (!(list_empty(&ipn_node
->oobmsgqueue
))) mask
|= POLLPRI
;
983 if ((!(ipnn
->flags
& IPN_FLAG_LOSSLESS
)) |
984 (atomic_read(&ipnn
->msgpool_nelem
) < ipnn
->msgpool_size
))
985 mask
|= POLLOUT
| POLLWRNORM
;
990 static unsigned int ipn_poll(struct file
*file
, struct socket
*sock
,
992 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
993 return ipn_node_poll(ipn_node
, file
, wait
);
996 /* connect netdev (from ioctl). connect a bound socket to a
997 * network device TAP or GRAB */
998 static int ipn_connect_netdev(struct socket
*sock
,struct ifreq
*ifr
)
1001 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1002 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1003 if (!capable(CAP_NET_ADMIN
))
1005 if (sock
->state
!= SS_UNCONNECTED
)
1008 return -ENOTCONN
; /* Maybe we need a different error for "NOT BOUND" */
1009 if (down_interruptible(&ipn_glob_mutex
))
1010 return -ERESTARTSYS
;
1011 if (down_interruptible(&ipnn
->ipnn_mutex
)) {
1012 up(&ipn_glob_mutex
);
1013 return -ERESTARTSYS
;
1015 ipn_node
->netdev
=ipn_netdev_alloc(ipn_node
->net
,ifr
->ifr_flags
,ifr
->ifr_name
,&err
);
1016 if (ipn_node
->netdev
) {
1018 portno
= ipn_protocol_table
[ipnn
->protocol
]->ipn_p_newport(ipn_node
);
1019 if (portno
>= 0 && portno
<ipnn
->maxports
) {
1020 sock
->state
= SS_CONNECTED
;
1021 ipn_node
->portno
=portno
;
1022 ipn_node
->flags
|= ifr
->ifr_flags
& IPN_NODEFLAG_DEVMASK
;
1023 ipnn
->connport
[portno
]=ipn_node
;
1024 err
=ipn_netdev_activate(ipn_node
);
1026 sock
->state
= SS_UNCONNECTED
;
1027 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_delport(ipn_node
);
1028 ipn_node
->netdev
=NULL
;
1029 ipn_node
->portno
= -1;
1030 ipn_node
->flags
&= ~IPN_NODEFLAG_DEVMASK
;
1031 ipnn
->connport
[portno
]=NULL
;
1033 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_postnewport(ipn_node
);
1034 list_del(&ipn_node
->nodelist
);
1035 list_add_tail(&ipn_node
->nodelist
,&ipnn
->connectqueue
);
1038 ipn_netdev_close(ipn_node
);
1040 ipn_node
->netdev
=NULL
;
1044 up(&ipnn
->ipnn_mutex
);
1045 up(&ipn_glob_mutex
);
1049 /* join a netdev, a socket gets connected to a persistent node
1050 * not connected to another socket */
1051 static int ipn_join_netdev(struct socket
*sock
,struct ifreq
*ifr
)
1054 struct net_device
*dev
;
1055 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1056 struct ipn_node
*ipn_joined
;
1057 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1058 if (sock
->state
!= SS_UNCONNECTED
)
1060 if (down_interruptible(&ipn_glob_mutex
))
1061 return -ERESTARTSYS
;
1062 if (down_interruptible(&ipnn
->ipnn_mutex
)) {
1063 up(&ipn_glob_mutex
);
1064 return -ERESTARTSYS
;
1066 dev
=__dev_get_by_name(ipn_node
->net
,ifr
->ifr_name
);
1068 dev
=__dev_get_by_index(ipn_node
->net
,ifr
->ifr_ifindex
);
1069 if (dev
&& (ipn_joined
=ipn_netdev2node(dev
)) != NULL
) { /* the interface does exist */
1071 for (i
=0;i
<ipnn
->maxports
&& ipn_joined
!= ipnn
->connport
[i
] ;i
++)
1073 if (i
< ipnn
->maxports
) { /* found */
1074 /* ipn_joined is substituted to ipn_node */
1075 ((struct ipn_sock
*)sock
->sk
)->node
=ipn_joined
;
1076 ipn_joined
->flags
|= IPN_NODEFLAG_INUSE
;
1078 kmem_cache_free(ipn_node_cache
,ipn_node
);
1083 up(&ipnn
->ipnn_mutex
);
1084 up(&ipn_glob_mutex
);
1088 /* set persistence of a node looking for it by interface name
1089 * (it is for sysadm, to close network interfaces)*/
1090 static int ipn_setpersist_netdev(struct ifreq
*ifr
, int value
)
1092 struct net_device
*dev
;
1093 struct ipn_node
*ipn_node
;
1095 if (!capable(CAP_NET_ADMIN
))
1097 if (down_interruptible(&ipn_glob_mutex
))
1098 return -ERESTARTSYS
;
1099 dev
=__dev_get_by_name(&init_net
,ifr
->ifr_name
);
1101 dev
=__dev_get_by_index(&init_net
,ifr
->ifr_ifindex
);
1102 if (dev
&& (ipn_node
=ipn_netdev2node(dev
)) != NULL
)
1103 _ipn_setpersist(ipn_node
,value
);
1106 up(&ipn_glob_mutex
);
1112 int ipn_node_ioctl(struct ipn_node
*ipn_node
, unsigned int cmd
, unsigned long arg
) {
1113 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1115 (ipn_protocol_table
[ipn_node
->protocol
]->ipn_p_ioctl
!= NULL
)) {
1117 if (down_interruptible(&ipnn
->ipnn_mutex
))
1118 return -ERESTARTSYS
;
1119 rv
=ipn_protocol_table
[ipn_node
->protocol
]->ipn_p_ioctl(ipn_node
,cmd
,arg
);
1120 up(&ipnn
->ipnn_mutex
);
1126 static int ipn_ioctl(struct socket
*sock
, unsigned int cmd
, unsigned long arg
) {
1127 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1128 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1129 void __user
* argp
= (void __user
*)arg
;
1131 struct chrdevreq devr
;
1133 if (ipn_node
->shutdown
== SHUTDOWN_XMASK
)
1140 case IPN_SETPERSIST_NETDEV
:
1141 case IPN_CLRPERSIST_NETDEV
:
1142 case IPN_CONN_NETDEV
:
1143 case IPN_JOIN_NETDEV
:
1145 if (copy_from_user(&ifr
, argp
, sizeof(ifr
)))
1147 ifr
.ifr_name
[IFNAMSIZ
-1] = '\0';
1149 case IPN_REGISTER_CHRDEV
:
1150 case IPN_JOIN_CHRDEV
:
1151 if (copy_from_user(&devr
, argp
, sizeof(devr
)))
1153 /*printk("IPN_REGISTER_CHRDEV %p %d %d %d\n",
1154 argp, sizeof(devr),devr.major, devr.minor);*/
1158 /* actions for unconnected and unbound sockets */
1160 case IPN_SETPERSIST_NETDEV
:
1161 return ipn_setpersist_netdev(&ifr
,1);
1162 case IPN_CLRPERSIST_NETDEV
:
1163 return ipn_setpersist_netdev(&ifr
,0);
1164 case IPN_JOIN_CHRDEV
:
1167 if (!capable(CAP_MKNOD
))
1169 if (ipn_node
->ipn
!= NULL
)
1171 if (down_interruptible(&ipn_glob_mutex
))
1172 return -ERESTARTSYS
;
1173 rv
=ipn_node_bind(ipn_node
, ipn_find_chrdev(&devr
));
1174 up(&ipn_glob_mutex
);
1178 if (capable(CAP_NET_ADMIN
))
1180 if (ipn_node
->netdev
&& (ipn_node
->flags
&IPN_NODEFLAG_TAP
))
1181 return dev_set_mac_address(ipn_node
->netdev
, &ifr
.ifr_hwaddr
);
1183 return -EADDRNOTAVAIL
;
1185 if (ipnn
== NULL
|| (ipnn
->flags
& IPN_FLAG_TERMINATED
))
1187 /* actions for connected or bound sockets */
1189 case IPN_CONN_NETDEV
:
1190 return ipn_connect_netdev(sock
,&ifr
);
1191 case IPN_JOIN_NETDEV
:
1192 return ipn_join_netdev(sock
,&ifr
);
1193 case IPN_SETPERSIST
:
1194 return ipn_setpersist(ipn_node
,arg
);
1195 case IPN_CHRDEV_PERSIST
:
1196 return ipn_chrdev_persistence(ipnn
,arg
);
1197 case IPN_REGISTER_CHRDEV
:
1200 unsigned int reqmajor
=devr
.major
;
1201 if (down_interruptible(&ipnn
->ipnn_mutex
))
1202 return -ERESTARTSYS
;
1203 rv
=ipn_register_chrdev(ipnn
,&devr
);
1204 if (reqmajor
==0 && rv
==0) {
1205 if (copy_to_user(argp
, &devr
, sizeof devr
))
1208 up(&ipnn
->ipnn_mutex
);
1211 case IPN_UNREGISTER_CHRDEV
:
1214 if (down_interruptible(&ipnn
->ipnn_mutex
))
1215 return -ERESTARTSYS
;
1216 rv
=ipn_deregister_chrdev(ipnn
);
1217 up(&ipnn
->ipnn_mutex
);
1221 return ipn_node_ioctl(ipn_node
, cmd
, arg
);
1225 /* shutdown: close socket for input or for output.
1226 * shutdown can be called prior to connect and it is not reversible */
1227 static int ipn_shutdown(struct socket
*sock
, int mode
) {
1228 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1229 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1230 int oldshutdown
=ipn_node
->shutdown
;
1231 mode
= (mode
+1)&(RCV_SHUTDOWN
|SEND_SHUTDOWN
);
1233 /* define the new mode first... */
1234 ipn_node
->shutdown
|= mode
;
1237 /* ... and wait for all pending ops to be completed */
1238 if (down_interruptible(&ipnn
->ipnn_mutex
)) {
1239 ipn_node
->shutdown
= oldshutdown
;
1240 return -ERESTARTSYS
;
1242 oldshutdown
=ipn_node
->shutdown
-oldshutdown
;
1243 if (sock
->state
== SS_CONNECTED
&& oldshutdown
) {
1244 ipn_net_update_counters(ipnn
,
1245 (ipn_node
->shutdown
& RCV_SHUTDOWN
)?0:-1,
1246 (ipn_node
->shutdown
& SEND_SHUTDOWN
)?0:-1);
1249 /* if recv channel has been shut down, flush the recv queue */
1250 if ((ipn_node
->shutdown
& RCV_SHUTDOWN
))
1251 ipn_flush_recvqueue(ipn_node
);
1252 up(&ipnn
->ipnn_mutex
);
1257 /* injectmsg: a new message is entering the ipn network.
1258 * injectmsg gets called by send and by the grab/tap node */
1259 /* handlemsg is protected from ipn_network changes: ipnn->ipnn_mutex is locked */
1260 int ipn_proto_injectmsg(struct ipn_node
*from
, struct msgpool_item
*msg
)
1262 struct ipn_network
*ipnn
=from
->ipn
;
1264 static int recinject
=0;
1265 //printk("INJECTMSG IN\n");
1267 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_handlemsg(from
, msg
);
1268 else if (down_interruptible(&ipnn
->ipnn_mutex
))
1272 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_handlemsg(from
, msg
);
1274 up(&ipnn
->ipnn_mutex
);
1276 //printk("INJECTMSG OUT %d\n",err);
1281 int ipn_node_write(struct ipn_node
*ipn_node
, struct iovec
*msg_iov
, int len
) {
1282 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1283 struct msgpool_item
*newmsg
;
1286 if (unlikely(ipn_node
->shutdown
& SEND_SHUTDOWN
)) {
1287 if (ipn_node
->shutdown
== SHUTDOWN_XMASK
)
1292 if (len
> ipnn
->mtu
)
1294 newmsg
=ipn_msgpool_alloc_locking(ipnn
,len
);
1298 err
=memcpy_fromiovec(newmsg
->data
, msg_iov
, len
);
1300 ipn_proto_injectmsg(ipn_node
, newmsg
);
1301 ipn_msgpool_put(newmsg
,ipnn
);
1305 static int ipn_sendmsg(struct kiocb
*kiocb
, struct socket
*sock
,
1306 struct msghdr
*msg
, size_t len
) {
1307 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1309 if (unlikely(sock
->state
!= SS_CONNECTED
))
1312 return ipn_node_write(ipn_node
, msg
->msg_iov
, len
);
1315 /* ipn_proto_sendmsg is called by protocol implementation to enqueue a
1316 * for a destination (to).*/
1317 /* enqueue a msgitem on the receiver's queue, the queue is protected by a
1318 * spinlock (msglock) */
1319 void ipn_proto_sendmsg(struct ipn_node
*to
, struct msgpool_item
*msg
)
1323 ipn_netdev_sendmsg(to
,msg
);
1326 struct msgitem
*msgitem
;
1327 struct ipn_network
*ipnn
=to
->ipn
;
1328 spin_lock(&to
->msglock
);
1329 if (likely((to
->shutdown
& RCV_SHUTDOWN
)==0)) {
1330 /*if (unlikely((ipnn->flags & IPN_FLAG_LOSSLESS) == 0 ||
1331 to->totmsgcount >= ipnn->msgpool_size))
1333 if (likely(ipnn
->flags
& IPN_FLAG_LOSSLESS
||
1334 to
->totmsgcount
< ipnn
->msgpool_size
)) {
1335 if ((msgitem
=kmem_cache_alloc(ipn_msgitem_cache
,GFP_KERNEL
))!=NULL
) {
1338 list_add_tail(&msgitem
->list
, &to
->msgqueue
);
1339 ipn_msgpool_hold(msg
);
1343 spin_unlock(&to
->msglock
);
1344 wake_up_interruptible(&to
->read_wait
);
1349 /* enqueue an oob message. "to" is the destination */
1350 void ipn_proto_oobsendmsg(struct ipn_node
*to
, struct msgpool_item
*msg
)
1353 if (!to
->netdev
) { /* no oob to netdev */
1354 struct msgitem
*msgitem
;
1355 struct ipn_network
*ipnn
=to
->ipn
;
1356 spin_lock(&to
->msglock
);
1357 if ((to
->shutdown
& RCV_SHUTDOWN_NO_OOB
) == 0 &&
1358 (ipnn
->flags
& IPN_FLAG_LOSSLESS
||
1359 to
->oobmsgcount
< ipnn
->msgpool_size
)) {
1360 if ((msgitem
=kmem_cache_alloc(ipn_msgitem_cache
,GFP_KERNEL
))!=NULL
) {
1364 list_add_tail(&msgitem
->list
, &to
->oobmsgqueue
);
1365 ipn_msgpool_hold(msg
);
1368 spin_unlock(&to
->msglock
);
1369 wake_up_interruptible(&to
->read_wait
);
1375 int ipn_node_read(struct ipn_node
*ipn_node
, struct iovec
*msg_iov
, size_t len
, int *msg_flags
, int flags
) {
1376 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1377 struct msgitem
*msgitem
;
1378 struct msgpool_item
*currmsg
;
1380 if (unlikely((ipn_node
->shutdown
& XRCV_SHUTDOWN
) == XRCV_SHUTDOWN
)) {
1381 if (ipn_node
->shutdown
== SHUTDOWN_XMASK
) /*EOF, nothing can be read*/
1384 return -EPIPE
; /*trying to read on a write only node */
1387 /* wait for a message */
1388 spin_lock(&ipn_node
->msglock
);
1389 while (ipn_node
->totmsgcount
== 0) {
1390 spin_unlock(&ipn_node
->msglock
);
1391 if (wait_event_interruptible(ipn_node
->read_wait
,
1392 !(ipn_node
->totmsgcount
== 0)))
1393 return -ERESTARTSYS
;
1394 spin_lock(&ipn_node
->msglock
);
1396 /* oob gets delivered first. oob are rare */
1397 if (likely(list_empty(&ipn_node
->oobmsgqueue
)))
1398 msgitem
=list_first_entry(&ipn_node
->msgqueue
, struct msgitem
, list
);
1400 msgitem
=list_first_entry(&ipn_node
->oobmsgqueue
, struct msgitem
, list
);
1401 *msg_flags
|= MSG_OOB
;
1402 ipn_node
->oobmsgcount
--;
1404 list_del(&msgitem
->list
);
1405 ipn_node
->totmsgcount
--;
1406 spin_unlock(&ipn_node
->msglock
);
1407 currmsg
=msgitem
->msg
;
1408 if (currmsg
->len
< len
)
1410 memcpy_toiovec(msg_iov
, currmsg
->data
, len
);
1411 ipn_msgpool_put(currmsg
,ipnn
);
1412 kmem_cache_free(ipn_msgitem_cache
,msgitem
);
1416 static int ipn_recvmsg(struct kiocb
*kiocb
, struct socket
*sock
,
1417 struct msghdr
*msg
, size_t len
, int flags
) {
1418 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1420 if (unlikely(sock
->state
!= SS_CONNECTED
))
1423 return ipn_node_read(ipn_node
, msg
->msg_iov
, len
, &msg
->msg_flags
, flags
);
1426 /* resize a network: change the # of communication ports (connport) */
1427 static int ipn_netresize(struct ipn_network
*ipnn
,int newsize
)
1430 struct ipn_node
**newconnport
;
1431 struct ipn_node
**oldconnport
;
1433 if (down_interruptible(&ipnn
->ipnn_mutex
))
1434 return -ERESTARTSYS
;
1435 oldsize
=ipnn
->maxports
;
1436 if (newsize
== oldsize
) {
1437 up(&ipnn
->ipnn_mutex
);
1441 /* shrink a network. all the ports we are going to eliminate
1442 * must be unused! */
1443 if (newsize
< oldsize
) {
1445 for (i
=newsize
; i
<oldsize
; i
++)
1446 if (ipnn
->connport
[i
]) {
1447 up(&ipnn
->ipnn_mutex
);
1452 oldconnport
=ipnn
->connport
;
1453 /* allocate the new connport array and copy the old one */
1454 newconnport
=kzalloc(newsize
* sizeof(struct ipn_node
*),GFP_KERNEL
);
1456 up(&ipnn
->ipnn_mutex
);
1459 memcpy(newconnport
,oldconnport
,min
* sizeof(struct ipn_node
*));
1460 ipnn
->connport
=newconnport
;
1461 ipnn
->maxports
=newsize
;
1462 /* notify the protocol that the netowrk has been resized */
1463 err
=ipn_protocol_table
[ipnn
->protocol
]->ipn_p_resizenet(ipnn
,oldsize
,newsize
);
1465 /* roll back if the resize operation failed for the protocol */
1466 ipnn
->connport
=oldconnport
;
1467 ipnn
->maxports
=oldsize
;
1470 /* successful mission, network resized */
1472 up(&ipnn
->ipnn_mutex
);
1476 /* IPN SETSOCKOPT */
1478 static int ipn_setsockopt(struct socket
*sock
, int level
, int optname
,
1479 char __user
*optval
, unsigned int optlen
)
1481 static int ipn_setsockopt(struct socket
*sock
, int level
, int optname
,
1482 char __user
*optval
, int optlen
)
1485 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1486 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1488 if (ipn_node
->shutdown
== SHUTDOWN_XMASK
)
1490 if (level
!= 0 && level
!= ipn_node
->protocol
+1)
1491 return -EPROTONOSUPPORT
;
1493 /* protocol specific sockopt */
1496 if (down_interruptible(&ipnn
->ipnn_mutex
))
1497 return -ERESTARTSYS
;
1498 rv
=ipn_protocol_table
[ipn_node
->protocol
]->ipn_p_setsockopt(ipn_node
,optname
,optval
,optlen
);
1499 up(&ipnn
->ipnn_mutex
);
1504 if (optname
== IPN_SO_DESCR
) {
1505 if (optlen
> IPN_DESCRLEN
)
1508 memset(ipn_node
->descr
,0,IPN_DESCRLEN
);
1509 if (copy_from_user(ipn_node
->descr
,optval
,optlen
))
1510 ipn_node
->descr
[0]=0;
1512 ipn_node
->descr
[optlen
-1]=0;
1516 if (optlen
< sizeof(int))
1518 else if ((optname
& IPN_SO_PREBIND
) && (ipnn
!= NULL
))
1522 get_user(val
, (int __user
*) optval
);
1523 if ((optname
& IPN_SO_PREBIND
) && !ipn_node
->pbp
) {
1524 struct pre_bind_parms std
=STD_BIND_PARMS
;
1525 ipn_node
->pbp
=kzalloc(sizeof(struct pre_bind_parms
),GFP_KERNEL
);
1528 *(ipn_node
->pbp
)=std
;
1532 if (sock
->state
== SS_UNCONNECTED
)
1533 ipn_node
->portno
=val
;
1537 case IPN_SO_CHANGE_NUMNODES
:
1538 if ((ipn_node
->flags
& IPN_NODEFLAG_BOUND
)!=0) {
1542 return ipn_netresize(ipnn
,val
);
1546 case IPN_SO_WANT_OOB_NUMNODES
:
1548 ipn_node
->flags
|= IPN_NODEFLAG_OOB_NUMNODES
;
1550 ipn_node
->flags
&= ~IPN_NODEFLAG_OOB_NUMNODES
;
1552 case IPN_SO_HANDLE_OOB
:
1554 ipn_node
->shutdown
&= ~RCV_SHUTDOWN_NO_OOB
;
1556 ipn_node
->shutdown
|= RCV_SHUTDOWN_NO_OOB
;
1562 ipn_node
->pbp
->mtu
=val
;
1564 case IPN_SO_NUMNODES
:
1568 ipn_node
->pbp
->maxports
=val
;
1570 case IPN_SO_MSGPOOLSIZE
:
1574 ipn_node
->pbp
->msgpoolsize
=val
;
1577 ipn_node
->pbp
->flags
=val
;
1580 ipn_node
->pbp
->mode
=val
;
1589 /* IPN GETSOCKOPT */
1590 static int ipn_getsockopt(struct socket
*sock
, int level
, int optname
,
1591 char __user
*optval
, int __user
*optlen
) {
1592 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1593 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1596 if (ipn_node
->shutdown
== SHUTDOWN_XMASK
)
1598 if (level
!= 0 && level
!= ipn_node
->protocol
+1)
1599 return -EPROTONOSUPPORT
;
1603 /* protocol specific sockopt */
1604 if (down_interruptible(&ipnn
->ipnn_mutex
))
1605 return -ERESTARTSYS
;
1606 rv
=ipn_protocol_table
[ipn_node
->protocol
]->ipn_p_getsockopt(ipn_node
,optname
,optval
,optlen
);
1607 up(&ipnn
->ipnn_mutex
);
1612 if (get_user(len
, optlen
))
1614 if (optname
== IPN_SO_DESCR
) {
1615 if (len
< IPN_DESCRLEN
)
1618 if (len
> IPN_DESCRLEN
)
1620 if(put_user(len
, optlen
))
1622 if(copy_to_user(optval
,ipn_node
->descr
,len
))
1630 val
=ipn_node
->portno
;
1635 else if (ipn_node
->pbp
)
1636 val
=ipn_node
->pbp
->mtu
;
1638 case IPN_SO_NUMNODES
:
1641 else if (ipn_node
->pbp
)
1642 val
=ipn_node
->pbp
->maxports
;
1644 case IPN_SO_MSGPOOLSIZE
:
1646 val
=ipnn
->msgpool_size
;
1647 else if (ipn_node
->pbp
)
1648 val
=ipn_node
->pbp
->msgpoolsize
;
1653 else if (ipn_node
->pbp
)
1654 val
=ipn_node
->pbp
->flags
;
1659 else if (ipn_node
->pbp
)
1660 val
=ipn_node
->pbp
->mode
;
1666 if (len
< sizeof(int))
1670 if(put_user(len
, optlen
))
1672 if(copy_to_user(optval
,&val
,len
))
1681 /* BROADCAST/HUB implementation */
1683 static int ipn_bcast_newport(struct ipn_node
*newport
) {
1684 struct ipn_network
*ipnn
=newport
->ipn
;
1686 for (i
=0;i
<ipnn
->maxports
;i
++) {
1687 if (ipnn
->connport
[i
] == NULL
)
1693 static int ipn_bcast_handlemsg(struct ipn_node
*from
,
1694 struct msgpool_item
*msgitem
){
1695 struct ipn_network
*ipnn
=from
->ipn
;
1697 struct ipn_node
*ipn_node
;
1698 list_for_each_entry(ipn_node
, &ipnn
->connectqueue
, nodelist
) {
1699 if (ipn_node
!= from
)
1700 ipn_proto_sendmsg(ipn_node
,msgitem
);
1705 static void ipn_null_delport(struct ipn_node
*oldport
) {}
1706 static void ipn_null_postnewport(struct ipn_node
*newport
) {}
1707 static void ipn_null_predelport(struct ipn_node
*oldport
) {}
1708 static int ipn_null_newnet(struct ipn_network
*newnet
) {return 0;}
1709 static int ipn_null_resizenet(struct ipn_network
*net
,int oldsize
,int newsize
) {
1711 static void ipn_null_delnet(struct ipn_network
*oldnet
) {}
1712 static int ipn_null_setsockopt(struct ipn_node
*port
,int optname
,
1713 char __user
*optval
, int optlen
) {return -EOPNOTSUPP
;}
1714 static int ipn_null_getsockopt(struct ipn_node
*port
,int optname
,
1715 char __user
*optval
, int *optlen
) {return -EOPNOTSUPP
;}
1716 static int ipn_null_ioctl(struct ipn_node
*port
,unsigned int request
,
1717 unsigned long arg
) {return -EOPNOTSUPP
;}
1719 /* Protocol Registration/deregisteration */
1721 void ipn_init_protocol(struct ipn_protocol
*p
)
1723 if (p
->ipn_p_delport
== NULL
) p
->ipn_p_delport
=ipn_null_delport
;
1724 if (p
->ipn_p_postnewport
== NULL
) p
->ipn_p_postnewport
=ipn_null_postnewport
;
1725 if (p
->ipn_p_predelport
== NULL
) p
->ipn_p_predelport
=ipn_null_predelport
;
1726 if (p
->ipn_p_newnet
== NULL
) p
->ipn_p_newnet
=ipn_null_newnet
;
1727 if (p
->ipn_p_resizenet
== NULL
) p
->ipn_p_resizenet
=ipn_null_resizenet
;
1728 if (p
->ipn_p_delnet
== NULL
) p
->ipn_p_delnet
=ipn_null_delnet
;
1729 if (p
->ipn_p_setsockopt
== NULL
) p
->ipn_p_setsockopt
=ipn_null_setsockopt
;
1730 if (p
->ipn_p_getsockopt
== NULL
) p
->ipn_p_getsockopt
=ipn_null_getsockopt
;
1731 if (p
->ipn_p_ioctl
== NULL
) p
->ipn_p_ioctl
=ipn_null_ioctl
;
1734 int ipn_proto_register(int protocol
,struct ipn_protocol
*ipn_service
)
1737 if (ipn_service
->ipn_p_newport
== NULL
||
1738 ipn_service
->ipn_p_handlemsg
== NULL
)
1740 ipn_init_protocol(ipn_service
);
1741 if (protocol
> 1 && protocol
<= IPN_MAX_PROTO
) {
1743 if (down_interruptible(&ipn_glob_mutex
))
1744 return -ERESTARTSYS
;
1745 if (ipn_protocol_table
[protocol
])
1748 ipn_service
->refcnt
=0;
1749 ipn_protocol_table
[protocol
]=ipn_service
;
1750 printk(KERN_INFO
"IPN: Registered protocol %d\n",protocol
+1);
1752 up(&ipn_glob_mutex
);
1758 int ipn_proto_deregister(int protocol
)
1761 if (protocol
> 1 && protocol
<= IPN_MAX_PROTO
) {
1763 if (down_interruptible(&ipn_glob_mutex
))
1764 return -ERESTARTSYS
;
1765 if (ipn_protocol_table
[protocol
]) {
1766 if (ipn_protocol_table
[protocol
]->refcnt
== 0) {
1767 ipn_protocol_table
[protocol
]=NULL
;
1768 printk(KERN_INFO
"IPN: Unregistered protocol %d\n",protocol
+1);
1773 up(&ipn_glob_mutex
);
1780 /* Module constructor/destructor */
1781 static struct net_proto_family ipn_family_ops
= {
1783 .create
= ipn_create
,
1784 .owner
= THIS_MODULE
,
1787 /* IPN constructor */
1788 static int ipn_init(void)
1792 ipn_init_protocol(&ipn_bcast
);
1793 ipn_network_cache
=kmem_cache_create("ipn_network",sizeof(struct ipn_network
),0,0,NULL
);
1794 if (!ipn_network_cache
) {
1795 printk(KERN_CRIT
"%s: Cannot create ipn_network SLAB cache!\n",
1801 ipn_node_cache
=kmem_cache_create("ipn_node",sizeof(struct ipn_node
),0,0,NULL
);
1802 if (!ipn_node_cache
) {
1803 printk(KERN_CRIT
"%s: Cannot create ipn_node SLAB cache!\n",
1809 ipn_msgitem_cache
=kmem_cache_create("ipn_msgitem",sizeof(struct msgitem
),0,0,NULL
);
1810 if (!ipn_msgitem_cache
) {
1811 printk(KERN_CRIT
"%s: Cannot create ipn_msgitem SLAB cache!\n",
1817 rc
=ipn_msgbuf_init();
1819 printk(KERN_CRIT
"%s: Cannot create ipn_msgbuf SLAB cache\n",
1821 goto out_net_node_msg
;
1824 rc
=proto_register(&ipn_proto
,1);
1826 printk(KERN_CRIT
"%s: Cannot register the protocol!\n",
1828 goto out_net_node_msg_msgbuf
;
1831 sock_register(&ipn_family_ops
);
1833 printk(KERN_INFO
"IPN: Virtual Square Project, University of Bologna 2007-09\n");
1836 out_net_node_msg_msgbuf
:
1839 kmem_cache_destroy(ipn_msgitem_cache
);
1841 kmem_cache_destroy(ipn_node_cache
);
1843 kmem_cache_destroy(ipn_network_cache
);
1848 /* IPN destructor */
1849 static void ipn_exit(void)
1852 if (ipn_msgitem_cache
)
1853 kmem_cache_destroy(ipn_msgitem_cache
);
1855 kmem_cache_destroy(ipn_node_cache
);
1856 if (ipn_network_cache
)
1857 kmem_cache_destroy(ipn_network_cache
);
1859 sock_unregister(PF_IPN
);
1860 proto_unregister(&ipn_proto
);
1861 printk(KERN_INFO
"IPN removed\n");
1864 module_init(ipn_init
);
1865 module_exit(ipn_exit
);
1867 EXPORT_SYMBOL_GPL(ipn_proto_register
);
1868 EXPORT_SYMBOL_GPL(ipn_proto_deregister
);
1869 EXPORT_SYMBOL_GPL(ipn_proto_sendmsg
);
1870 EXPORT_SYMBOL_GPL(ipn_proto_oobsendmsg
);
1871 EXPORT_SYMBOL_GPL(ipn_msgpool_alloc
);
1872 EXPORT_SYMBOL_GPL(ipn_msgpool_put
);