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)
59 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,1,0)
63 /*extension of RCV_SHUTDOWN defined in include/net/sock.h
64 * when the bit is set recv fails */
65 /* NO_OOB: do not send OOB */
66 #define RCV_SHUTDOWN_NO_OOB 4
67 /* EXTENDED MASK including OOB */
68 #define SHUTDOWN_XMASK (SHUTDOWN_MASK | RCV_SHUTDOWN_NO_OOB)
69 /* if XRCV_SHUTDOWN is all set recv fails */
70 #define XRCV_SHUTDOWN (RCV_SHUTDOWN | RCV_SHUTDOWN_NO_OOB)
72 /* Global MUTEX: this is locked to add/delete/modify networks
73 * this is *not* locked just to send/receive msgs */
75 static DECLARE_MUTEX(ipn_glob_mutex
);
77 static DEFINE_SEMAPHORE(ipn_glob_mutex
);
79 /* Network table and hash */
80 struct hlist_head ipn_network_table
[IPN_HASH_SIZE
+ 1];
81 /* slab(s) for fast data structure allocation */
82 static struct kmem_cache
*ipn_network_cache
;
83 static struct kmem_cache
*ipn_node_cache
;
84 static struct kmem_cache
*ipn_msgitem_cache
;
86 /* Protocol 1: HUB/Broadcast default protocol. Function Prototypes */
87 static int ipn_bcast_newport(struct ipn_node
*newport
);
88 static int ipn_bcast_handlemsg(struct ipn_node
*from
,
89 struct msgpool_item
*msgitem
);
91 /* default protocol IPN_BROADCAST (0) */
92 static struct ipn_protocol ipn_bcast
= {
94 .ipn_p_newport
=ipn_bcast_newport
,
95 .ipn_p_handlemsg
=ipn_bcast_handlemsg
};
97 static struct ipn_protocol
*ipn_protocol_table
[IPN_MAX_PROTO
]={&ipn_bcast
};
99 /* Socket call function prototypes */
100 static int ipn_release(struct socket
*);
101 static int ipn_bind(struct socket
*, struct sockaddr
*, int);
102 static int ipn_connect(struct socket
*, struct sockaddr
*,
103 int addr_len
, int flags
);
104 static int ipn_getname(struct socket
*, struct sockaddr
*, int *, int);
105 static unsigned int ipn_poll(struct file
*, struct socket
*, poll_table
*);
106 static int ipn_ioctl(struct socket
*, unsigned int, unsigned long);
107 static int ipn_shutdown(struct socket
*, int);
108 static int ipn_sendmsg(struct kiocb
*, struct socket
*,
109 struct msghdr
*, size_t);
110 static int ipn_recvmsg(struct kiocb
*, struct socket
*,
111 struct msghdr
*, size_t, int);
113 static int ipn_setsockopt(struct socket
*sock
, int level
, int optname
,
114 char __user
*optval
, unsigned int optlen
);
116 static int ipn_setsockopt(struct socket
*sock
, int level
, int optname
,
117 char __user
*optval
, int optlen
);
119 static int ipn_getsockopt(struct socket
*sock
, int level
, int optname
,
120 char __user
*optval
, int __user
*optlen
);
122 /* Network table Management
123 * inode->ipn_network hash table
124 * protected by ipn_glob_mutex */
125 static inline void ipn_insert_network(struct hlist_head
*list
, struct ipn_network
*ipnn
)
127 hlist_add_head(&ipnn
->hnode
, list
);
130 static inline void ipn_remove_network(struct ipn_network
*ipnn
)
132 hlist_del(&ipnn
->hnode
);
135 static struct ipn_network
*ipn_find_network_byinode(struct inode
*i
)
137 struct ipn_network
*ipnn
;
138 struct hlist_node
*node
;
140 hlist_for_each_entry(ipnn
, node
,
141 &ipn_network_table
[i
->i_ino
& (IPN_HASH_SIZE
- 1)], hnode
) {
142 struct dentry
*dentry
= ipnn
->dentry
;
144 if(dentry
&& dentry
->d_inode
== i
)
150 struct ipn_network
*ipn_find_network_byfun(
151 int (*fun
)(struct ipn_network
*,void *),void *funarg
)
153 struct ipn_network
*ipnn
;
154 struct hlist_node
*node
;
157 for (ipn_table_scan
=0;ipn_table_scan
<IPN_HASH_SIZE
;ipn_table_scan
++) {
158 hlist_for_each_entry(ipnn
, node
, &ipn_network_table
[ipn_table_scan
], hnode
) {
166 /* msgpool management
167 * msgpool_item are ipn_network dependent (each net has its own MTU)
168 * for each message sent there is one msgpool_item and many struct msgitem
169 * one for each receipient.
170 * msgitem are connected to the node's msgqueue or oobmsgqueue.
171 * when a message is delivered to a process the msgitem is deleted and
172 * the count of the msgpool_item is decreased.
173 * msgpool_item elements gets deleted automatically when count is 0*/
176 struct list_head list
;
177 struct msgpool_item
*msg
;
180 /* alloc a fresh msgpool item. count is set to 1.
183 * for each receipient
184 * enqueue messages to the process (using msgitem), ipn_msgpool_hold
186 * The message can be delivered concurrently. init count to 1 guarantees
187 * that it survives at least until is has been enqueued to all
189 /* msgpool_cache == NULL when the ipn network is using the flexible allocation,
190 * i.e. the msgpool item gets allocated by kmalloc instead of using slab */
191 static inline struct msgpool_item
*_ipn_msgpool_alloc(struct ipn_network
*ipnn
, int len
)
193 struct msgpool_item
*new;
194 if ((new=ipnn
->msgpool_cache
?
195 kmem_cache_alloc(ipnn
->msgpool_cache
,GFP_KERNEL
):
196 kmalloc(sizeof(struct msgpool_item
)+len
,GFP_KERNEL
)) != NULL
) {
197 atomic_set(&new->count
,1);
198 atomic_inc(&ipnn
->msgpool_nelem
);
203 /* ipn_msgpool_alloc returns null when leaky, lossless and no space is left
204 * for new msgpool items. When !leaky, it can allocate more msgpooli items,
205 * this temporary overassignment blocks new messages to be delivered until
206 * there is space for a msgpool item to be allocated */
207 struct msgpool_item
*ipn_msgpool_alloc(struct ipn_network
*ipnn
,int leaky
,int len
)
209 if (leaky
&& (ipnn
->flags
& IPN_FLAG_LOSSLESS
) &&
210 atomic_read(&ipnn
->msgpool_nelem
) < ipnn
->msgpool_size
)
213 return _ipn_msgpool_alloc(ipnn
,len
);
216 /* If the service il LOSSLESS, this msgpool call waits for an
217 * available msgpool item */
218 static inline struct msgpool_item
*ipn_msgpool_alloc_locking(struct ipn_network
*ipnn
,int len
)
220 if (ipnn
->flags
& IPN_FLAG_LOSSLESS
) {
221 while (atomic_read(&ipnn
->msgpool_nelem
) >= ipnn
->msgpool_size
) {
222 if (wait_event_interruptible_exclusive(ipnn
->send_wait
,
223 atomic_read(&ipnn
->msgpool_nelem
) < ipnn
->msgpool_size
))
227 return _ipn_msgpool_alloc(ipnn
,len
);
230 /* register one more user for this msgpool item (i.e. count++) */
231 static inline void ipn_msgpool_hold(struct msgpool_item
*msg
)
233 atomic_inc(&msg
->count
);
236 /* decrease count and delete msgpool_item if count == 0 */
237 void ipn_msgpool_put(struct msgpool_item
*old
,
238 struct ipn_network
*ipnn
)
240 if (atomic_dec_and_test(&old
->count
)) {
241 if (ipnn
->msgpool_cache
)
242 kmem_cache_free(ipnn
->msgpool_cache
,old
);
245 atomic_dec(&ipnn
->msgpool_nelem
);
246 if (ipnn
->flags
& IPN_FLAG_LOSSLESS
) /* this could be done anyway */
247 wake_up_interruptible(&ipnn
->send_wait
);
252 static const struct proto_ops ipn_ops
= {
254 .owner
= THIS_MODULE
,
255 .release
= ipn_release
,
257 .connect
= ipn_connect
,
258 .socketpair
= sock_no_socketpair
,
259 .accept
= sock_no_accept
,
260 .getname
= ipn_getname
,
263 .listen
= sock_no_listen
,
264 .shutdown
= ipn_shutdown
,
265 .setsockopt
= ipn_setsockopt
,
266 .getsockopt
= ipn_getsockopt
,
267 .sendmsg
= ipn_sendmsg
,
268 .recvmsg
= ipn_recvmsg
,
269 .mmap
= sock_no_mmap
,
270 .sendpage
= sock_no_sendpage
,
273 static struct proto ipn_proto
= {
275 .owner
= THIS_MODULE
,
276 .obj_size
= sizeof(struct ipn_sock
),
279 /* create a new ipn_node */
280 struct ipn_node
*ipn_node_create(struct net
*net
)
282 struct ipn_node
*ipn_node
;
283 ipn_node
=kmem_cache_alloc(ipn_node_cache
,GFP_KERNEL
);
284 if (ipn_node
!=NULL
) {
285 INIT_LIST_HEAD(&ipn_node
->nodelist
);
286 ipn_node
->protocol
=0; /* BROADCAST, caller must set a proper value */
287 ipn_node
->flags
=IPN_NODEFLAG_INUSE
;
288 ipn_node
->shutdown
=RCV_SHUTDOWN_NO_OOB
;
289 ipn_node
->descr
[0]=0;
290 ipn_node
->portno
=IPN_PORTNO_ANY
;
292 ipn_node
->netdev
=NULL
;
294 ipn_node
->proto_private
=NULL
;
295 ipn_node
->totmsgcount
=0;
296 ipn_node
->oobmsgcount
=0;
297 spin_lock_init(&ipn_node
->msglock
);
298 INIT_LIST_HEAD(&ipn_node
->msgqueue
);
299 INIT_LIST_HEAD(&ipn_node
->oobmsgqueue
);
301 init_waitqueue_head(&ipn_node
->read_wait
);
308 * ipn_node is a separate structure, pointed by ipn_sock -> node
309 * when a node is "persistent", ipn_node survives while ipn_sock gets released*/
311 static int ipn_create(struct net
*net
,struct socket
*sock
, int protocol
)
313 static int ipn_create(struct net
*net
,struct socket
*sock
,
314 int protocol
, int kern
)
317 struct ipn_sock
*ipn_sk
;
319 if (net
!= &init_net
)
320 return -EAFNOSUPPORT
;
322 if (sock
->type
!= SOCK_RAW
)
327 protocol
=IPN_BROADCAST
-1;
328 if (protocol
< 0 || protocol
>= IPN_MAX_PROTO
||
329 ipn_protocol_table
[protocol
] == NULL
)
330 return -EPROTONOSUPPORT
;
331 ipn_sk
= (struct ipn_sock
*) sk_alloc(net
, PF_IPN
, GFP_KERNEL
, &ipn_proto
);
335 if ((ipn_sk
->node
=ipn_node_create(net
))==NULL
) {
336 sock_put((struct sock
*) ipn_sk
);
339 ipn_sk
->node
->protocol
=protocol
;
340 sock_init_data(sock
,(struct sock
*) ipn_sk
);
341 sock
->state
= SS_UNCONNECTED
;
342 sock
->ops
= &ipn_ops
;
343 sock
->sk
=(struct sock
*)ipn_sk
;
347 /* update # of readers and # of writers counters for an ipn network.
348 * This function sends oob messages to nodes requesting the service */
349 /* LOCKING ipnn_mutex is locked */
350 static void ipn_net_update_counters(struct ipn_network
*ipnn
,
351 int chg_readers
, int chg_writers
) {
352 ipnn
->numreaders
+= chg_readers
;
353 ipnn
->numwriters
+= chg_writers
;
354 if (ipnn
->mtu
>= sizeof(struct numnode_oob
))
356 struct msgpool_item
*ipn_msg
=_ipn_msgpool_alloc(ipnn
,sizeof(struct numnode_oob
));
358 struct numnode_oob
*oob_msg
=(struct numnode_oob
*)(ipn_msg
->data
);
359 struct ipn_node
*ipn_node
;
360 ipn_msg
->len
=sizeof(struct numnode_oob
);
361 oob_msg
->level
=IPN_ANY
;
362 oob_msg
->tag
=IPN_OOB_NUMNODE_TAG
;
363 oob_msg
->numreaders
=ipnn
->numreaders
;
364 oob_msg
->numwriters
=ipnn
->numwriters
;
365 list_for_each_entry(ipn_node
, &ipnn
->connectqueue
, nodelist
) {
366 if (ipn_node
->flags
& IPN_NODEFLAG_OOB_NUMNODES
)
367 ipn_proto_oobsendmsg(ipn_node
,ipn_msg
);
369 ipn_msgpool_put(ipn_msg
,ipnn
);
374 /* flush pending messages (for close and shutdown RCV) */
375 /* LOCKING: ipnn_mutex is locked */
376 static void ipn_flush_recvqueue(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
->msgqueue
)) {
381 struct msgitem
*msgitem
=
382 list_first_entry(&ipn_node
->msgqueue
, struct msgitem
, list
);
383 list_del(&msgitem
->list
);
384 ipn_node
->totmsgcount
--;
385 ipn_msgpool_put(msgitem
->msg
,ipnn
);
386 kmem_cache_free(ipn_msgitem_cache
,msgitem
);
388 spin_unlock(&ipn_node
->msglock
);
391 /* flush pending oob messages (for socket close) */
392 /* LOCKING: ipnn_mutex is locked */
393 static void ipn_flush_oobrecvqueue(struct ipn_node
*ipn_node
)
395 struct ipn_network
*ipnn
=ipn_node
->ipn
;
396 spin_lock(&ipn_node
->msglock
);
397 while (!list_empty(&ipn_node
->oobmsgqueue
)) {
398 struct msgitem
*msgitem
=
399 list_first_entry(&ipn_node
->oobmsgqueue
, struct msgitem
, list
);
400 list_del(&msgitem
->list
);
401 ipn_node
->totmsgcount
--;
402 ipn_node
->oobmsgcount
--;
403 ipn_msgpool_put(msgitem
->msg
,ipnn
);
404 kmem_cache_free(ipn_msgitem_cache
,msgitem
);
406 spin_unlock(&ipn_node
->msglock
);
409 /* Terminate node. The node is "logically" terminated. */
410 /* LOCKING: ipn_glob_lock must be locked here */
411 static int ipn_terminate_node(struct ipn_node
*ipn_node
)
413 struct ipn_network
*ipnn
=ipn_node
->ipn
;
415 if (down_interruptible(&ipnn
->ipnn_mutex
))
417 if (ipn_node
->portno
>= 0) {
418 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_predelport(ipn_node
);
419 ipnn
->connport
[ipn_node
->portno
]=NULL
;
421 list_del(&ipn_node
->nodelist
);
422 ipn_flush_recvqueue(ipn_node
);
423 ipn_flush_oobrecvqueue(ipn_node
);
424 if (ipn_node
->portno
>= 0)
425 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_delport(ipn_node
);
427 ipn_net_update_counters(ipnn
,
428 (ipn_node
->shutdown
& RCV_SHUTDOWN
)?0:-1,
429 (ipn_node
->shutdown
& SEND_SHUTDOWN
)?0:-1);
430 ipn_node
->shutdown
= SHUTDOWN_XMASK
;
431 up(&ipnn
->ipnn_mutex
);
432 if (ipn_node
->netdev
)
433 ipn_netdev_close(ipn_node
);
434 /* No more network elements */
436 if (ipnn
->refcnt
== 0 && !ipn_is_persistent_chrdev(ipnn
))
439 ipn_deregister_chrdev(ipnn
);
440 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_delnet(ipnn
);
441 ipn_remove_network(ipnn
);
442 ipn_protocol_table
[ipnn
->protocol
]->refcnt
--;
447 if (ipnn
->msgpool_cache
)
448 ipn_msgbuf_put(ipnn
->msgpool_cache
);
450 kfree(ipnn
->connport
);
451 kmem_cache_free(ipn_network_cache
, ipnn
);
452 module_put(THIS_MODULE
);
456 kfree(ipn_node
->pbp
);
462 /* release of an ipn_node */
463 int ipn_node_release(struct ipn_node
*ipn_node
)
466 if (down_interruptible(&ipn_glob_mutex
))
468 if (ipn_node
->flags
& IPN_NODEFLAG_PERSIST
) {
469 ipn_node
->flags
&= ~IPN_NODEFLAG_INUSE
;
473 rv
=ipn_terminate_node(ipn_node
);
477 kmem_cache_free(ipn_node_cache
,ipn_node
);
483 /* release of an ipn socket */
484 static int ipn_release (struct socket
*sock
)
486 struct ipn_sock
*ipn_sk
=(struct ipn_sock
*)sock
->sk
;
487 struct ipn_node
*ipn_node
=ipn_sk
->node
;
488 int rv
=ipn_node_release(ipn_node
);
490 sock_put((struct sock
*) ipn_sk
);
494 /* _set persist, change the persistence of a node,
495 * when persistence gets cleared and the node is no longer used
496 * the node is terminated and freed.
497 * ipn_glob_mutex must be locked */
498 static int _ipn_setpersist(struct ipn_node
*ipn_node
, int persist
)
502 ipn_node
->flags
|= IPN_NODEFLAG_PERSIST
;
504 ipn_node
->flags
&= ~IPN_NODEFLAG_PERSIST
;
505 if (!(ipn_node
->flags
& IPN_NODEFLAG_INUSE
)) {
506 rv
=ipn_terminate_node(ipn_node
);
508 kmem_cache_free(ipn_node_cache
,ipn_node
);
515 * lock ipn_glob_mutex and call __ipn_setpersist above */
516 static int ipn_setpersist(struct ipn_node
*ipn_node
, int persist
)
519 if (ipn_node
->netdev
== NULL
)
521 if (down_interruptible(&ipn_glob_mutex
))
523 rv
=_ipn_setpersist(ipn_node
,persist
);
528 /* several network parameters can be set by setsockopt prior to bind */
529 /* struct pre_bind_parms is a temporary stucture connected to ipn_node->pbp
530 * to keep the parameter values. */
531 struct pre_bind_parms
{
532 unsigned short maxports
;
533 unsigned short flags
;
534 unsigned short msgpoolsize
;
539 /* STD_PARMS: BITS_PER_LONG nodes, no flags, BITS_PER_BYTE pending msgs,
540 * Ethernet + VLAN MTU*/
541 #define STD_BIND_PARMS {BITS_PER_LONG, 0, BITS_PER_BYTE, 1514, 0777};
543 static int ipn_mkname(struct sockaddr_un
* sunaddr
, int len
)
545 if (len
<= sizeof(short) || len
> sizeof(*sunaddr
))
547 if (!sunaddr
|| sunaddr
->sun_family
!= AF_IPN
)
550 * This may look like an off by one error but it is a bit more
551 * subtle. 108 is the longest valid AF_IPN path for a binding.
552 * sun_path[108] doesnt as such exist. However in kernel space
553 * we are guaranteed that it is a valid memory location in our
554 * kernel address buffer.
556 ((char *)sunaddr
)[len
]=0;
557 len
= strlen(sunaddr
->sun_path
)+1+sizeof(short);
561 static int ipn_node_bind(struct ipn_node
*ipn_node
, struct ipn_network
*ipnn
)
565 if (ipn_node
->ipn
!= NULL
)
568 list_add_tail(&ipn_node
->nodelist
,&ipnn
->unconnectqueue
);
570 ipn_node
->flags
|= IPN_NODEFLAG_BOUND
;
576 static int ipn_bind(struct socket
*sock
, struct sockaddr
*uaddr
, int addr_len
)
578 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)uaddr
;
579 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
581 struct ipn_network
*ipnn
;
582 struct dentry
* dentry
= NULL
;
584 struct pre_bind_parms parms
=STD_BIND_PARMS
;
586 //printk("IPN bind\n");
588 if (down_interruptible(&ipn_glob_mutex
))
590 if (sock
->state
!= SS_UNCONNECTED
||
591 ipn_node
->ipn
!= NULL
) {
596 if (ipn_node
->protocol
>= 0 &&
597 (ipn_node
->protocol
>= IPN_MAX_PROTO
||
598 ipn_protocol_table
[ipn_node
->protocol
] == NULL
)) {
599 err
= -EPROTONOSUPPORT
;
603 addr_len
= ipn_mkname(sunaddr
, addr_len
);
609 /* check if there is already an ipn-network socket with that name */
610 err
= kern_path(sunaddr
->sun_path
, LOOKUP_FOLLOW
, &path
);
611 if (err
) { /* it does not exist, NEW IPN socket! */
613 /* Do I have the permission to create a file? */
614 dentry
= kern_path_create(AT_FDCWD
, sunaddr
->sun_path
, &path
, 0);
615 err
= PTR_ERR(dentry
);
617 goto out_mknod_unlock
;
619 * All right, let's create it.
622 mode
= ipn_node
->pbp
->mode
;
624 mode
= SOCK_INODE(sock
)->i_mode
;
625 mode
= S_IFSOCK
| (mode
& ~current_umask());
626 err
= mnt_want_write(path
.mnt
);
631 err
= vfs_mknod(path
.dentry
->d_inode
, dentry
, path
.mnt
, mode
, 0);
633 err
= vfs_mknod(path
.dentry
->d_inode
, dentry
, mode
, 0);
636 mnt_drop_write(path
.mnt
);
639 mutex_unlock(&path
.dentry
->d_inode
->i_mutex
);
641 path
.dentry
= dentry
;
642 /* create a new ipn_network item */
644 parms
=*ipn_node
->pbp
;
645 ipnn
=kmem_cache_zalloc(ipn_network_cache
,GFP_KERNEL
);
648 goto out_mknod_dput_ipnn
;
650 ipnn
->connport
=kzalloc(parms
.maxports
* sizeof(struct ipn_node
*),GFP_KERNEL
);
651 if (!ipnn
->connport
) {
653 goto out_mknod_dput_ipnn2
;
656 /* module refcnt is incremented for each network, thus
657 * rmmod is forbidden if there are persistent nodes */
658 if (!try_module_get(THIS_MODULE
)) {
660 goto out_mknod_dput_ipnn2
;
662 memcpy(&ipnn
->sunaddr
,sunaddr
,addr_len
);
664 ipnn
->flags
=parms
.flags
;
665 if (ipnn
->flags
& IPN_FLAG_FLEXMTU
)
666 ipnn
->msgpool_cache
= NULL
;
668 ipnn
->msgpool_cache
=ipn_msgbuf_get(ipnn
->mtu
);
669 if (!ipnn
->msgpool_cache
) {
671 goto out_mknod_dput_putmodule
;
674 INIT_LIST_HEAD(&ipnn
->unconnectqueue
);
675 INIT_LIST_HEAD(&ipnn
->connectqueue
);
677 ipnn
->dentry
=path
.dentry
;
679 sema_init(&ipnn
->ipnn_mutex
,1);
680 ipnn
->sunaddr_len
=addr_len
;
681 ipnn
->protocol
=ipn_node
->protocol
;
682 if (ipnn
->protocol
< 0) ipnn
->protocol
= 0;
683 ipn_protocol_table
[ipnn
->protocol
]->refcnt
++;
686 ipnn
->maxports
=parms
.maxports
;
687 atomic_set(&ipnn
->msgpool_nelem
,0);
688 ipnn
->msgpool_size
=parms
.msgpoolsize
;
689 ipnn
->proto_private
=NULL
;
690 init_waitqueue_head(&ipnn
->send_wait
);
692 err
=ipn_protocol_table
[ipnn
->protocol
]->ipn_p_newnet(ipnn
);
694 goto out_mknod_dput_putmodule
;
695 ipn_insert_network(&ipn_network_table
[path
.dentry
->d_inode
->i_ino
& (IPN_HASH_SIZE
-1)],ipnn
);
697 /* join an existing network */
698 if (parms
.flags
& IPN_FLAG_EXCL
) {
702 err
= inode_permission(path
.dentry
->d_inode
, MAY_EXEC
);
706 if (!S_ISSOCK(path
.dentry
->d_inode
->i_mode
))
708 ipnn
=ipn_find_network_byinode(path
.dentry
->d_inode
);
709 if (!ipnn
|| (ipnn
->flags
& IPN_FLAG_TERMINATED
) ||
710 (ipnn
->flags
& IPN_FLAG_EXCL
))
714 kfree(ipn_node
->pbp
);
717 ipn_node_bind(ipn_node
,ipnn
);
727 out_mknod_dput_putmodule
:
728 module_put(THIS_MODULE
);
729 out_mknod_dput_ipnn2
:
730 kfree(ipnn
->connport
);
732 kmem_cache_free(ipn_network_cache
,ipnn
);
735 mutex_unlock(&path
.dentry
->d_inode
->i_mutex
);
745 static int ipn_bind(struct socket
*sock
, struct sockaddr
*uaddr
, int addr_len
)
747 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)uaddr
;
748 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
750 struct ipn_network
*ipnn
;
751 struct dentry
* dentry
= NULL
;
753 struct pre_bind_parms parms
=STD_BIND_PARMS
;
755 //printk("IPN bind\n");
757 if (down_interruptible(&ipn_glob_mutex
))
759 if (sock
->state
!= SS_UNCONNECTED
||
760 ipn_node
->ipn
!= NULL
) {
765 if (ipn_node
->protocol
>= 0 &&
766 (ipn_node
->protocol
>= IPN_MAX_PROTO
||
767 ipn_protocol_table
[ipn_node
->protocol
] == NULL
)) {
768 err
= -EPROTONOSUPPORT
;
772 addr_len
= ipn_mkname(sunaddr
, addr_len
);
778 /* check if there is already an ipn-network socket with that name */
780 err
= kern_path(sunaddr
->sun_path
, LOOKUP_FOLLOW
, &nd
.path
);
782 err
= path_lookup(sunaddr
->sun_path
, LOOKUP_FOLLOW
, &nd
);
784 if (err
) { /* it does not exist, NEW IPN socket! */
786 /* Is it everything okay with the parent? */
788 err
= kern_path_parent(sunaddr
->sun_path
, &nd
);
790 err
= path_lookup(sunaddr
->sun_path
, LOOKUP_PARENT
, &nd
);
793 goto out_mknod_parent
;
794 /* Do I have the permission to create a file? */
795 dentry
= lookup_create(&nd
, 0);
796 err
= PTR_ERR(dentry
);
798 goto out_mknod_unlock
;
800 * All right, let's create it.
803 mode
= ipn_node
->pbp
->mode
;
805 mode
= SOCK_INODE(sock
)->i_mode
;
807 mode
= S_IFSOCK
| (mode
& ~current
->fs
->umask
);
809 mode
= S_IFSOCK
| (mode
& ~current_umask());
812 err
= vfs_mknod(nd
.path
.dentry
->d_inode
, dentry
, nd
.path
.mnt
, mode
, 0);
814 err
= vfs_mknod(nd
.path
.dentry
->d_inode
, dentry
, mode
, 0);
818 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
819 dput(nd
.path
.dentry
);
820 nd
.path
.dentry
= dentry
;
821 /* create a new ipn_network item */
823 parms
=*ipn_node
->pbp
;
824 ipnn
=kmem_cache_zalloc(ipn_network_cache
,GFP_KERNEL
);
827 goto out_mknod_dput_ipnn
;
829 ipnn
->connport
=kzalloc(parms
.maxports
* sizeof(struct ipn_node
*),GFP_KERNEL
);
830 if (!ipnn
->connport
) {
832 goto out_mknod_dput_ipnn2
;
835 /* module refcnt is incremented for each network, thus
836 * rmmod is forbidden if there are persistent nodes */
837 if (!try_module_get(THIS_MODULE
)) {
839 goto out_mknod_dput_ipnn2
;
841 memcpy(&ipnn
->sunaddr
,sunaddr
,addr_len
);
843 ipnn
->flags
=parms
.flags
;
844 if (ipnn
->flags
& IPN_FLAG_FLEXMTU
)
845 ipnn
->msgpool_cache
= NULL
;
847 ipnn
->msgpool_cache
=ipn_msgbuf_get(ipnn
->mtu
);
848 if (!ipnn
->msgpool_cache
) {
850 goto out_mknod_dput_putmodule
;
853 INIT_LIST_HEAD(&ipnn
->unconnectqueue
);
854 INIT_LIST_HEAD(&ipnn
->connectqueue
);
856 ipnn
->dentry
=nd
.path
.dentry
;
857 ipnn
->mnt
=nd
.path
.mnt
;
858 sema_init(&ipnn
->ipnn_mutex
,1);
859 ipnn
->sunaddr_len
=addr_len
;
860 ipnn
->protocol
=ipn_node
->protocol
;
861 if (ipnn
->protocol
< 0) ipnn
->protocol
= 0;
862 ipn_protocol_table
[ipnn
->protocol
]->refcnt
++;
865 ipnn
->maxports
=parms
.maxports
;
866 atomic_set(&ipnn
->msgpool_nelem
,0);
867 ipnn
->msgpool_size
=parms
.msgpoolsize
;
868 ipnn
->proto_private
=NULL
;
869 init_waitqueue_head(&ipnn
->send_wait
);
871 err
=ipn_protocol_table
[ipnn
->protocol
]->ipn_p_newnet(ipnn
);
873 goto out_mknod_dput_putmodule
;
874 ipn_insert_network(&ipn_network_table
[nd
.path
.dentry
->d_inode
->i_ino
& (IPN_HASH_SIZE
-1)],ipnn
);
876 /* join an existing network */
877 if (parms
.flags
& IPN_FLAG_EXCL
) {
881 err
= inode_permission(nd
.path
.dentry
->d_inode
, MAY_EXEC
);
885 if (!S_ISSOCK(nd
.path
.dentry
->d_inode
->i_mode
))
887 ipnn
=ipn_find_network_byinode(nd
.path
.dentry
->d_inode
);
888 if (!ipnn
|| (ipnn
->flags
& IPN_FLAG_TERMINATED
) ||
889 (ipnn
->flags
& IPN_FLAG_EXCL
))
893 kfree(ipn_node
->pbp
);
896 ipn_node_bind(ipn_node
,ipnn
);
906 out_mknod_dput_putmodule
:
907 module_put(THIS_MODULE
);
908 out_mknod_dput_ipnn2
:
909 kfree(ipnn
->connport
);
911 kmem_cache_free(ipn_network_cache
,ipnn
);
915 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
926 static int ipn_connect(struct socket
*sock
, struct sockaddr
*addr
,
927 int addr_len
, int flags
){
928 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)addr
;
929 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
934 #define ndpath nd.path
937 struct ipn_network
*ipnn
,*previousipnn
;
941 /* the socket cannot be connected twice */
942 if (sock
->state
!= SS_UNCONNECTED
)
945 if (down_interruptible(&ipn_glob_mutex
))
948 if ((previousipnn
=ipn_node
->ipn
) == NULL
) { /* unbound */
949 unsigned char mustshutdown
=0;
950 err
= ipn_mkname(sunaddr
, addr_len
);
955 err
= kern_path(sunaddr
->sun_path
, LOOKUP_FOLLOW
, &ndpath
);
957 err
= path_lookup(sunaddr
->sun_path
, LOOKUP_FOLLOW
, &nd
);
961 err
= inode_permission(ndpath
.dentry
->d_inode
, MAY_READ
);
963 if (err
== -EACCES
|| err
== -EROFS
)
964 mustshutdown
|=RCV_SHUTDOWN
;
968 err
= inode_permission(ndpath
.dentry
->d_inode
, MAY_WRITE
);
971 mustshutdown
|=SEND_SHUTDOWN
;
975 mustshutdown
|= ipn_node
->shutdown
;
976 /* if the combination of shutdown and permissions leaves
977 * no abilities, connect returns EACCES */
978 if (mustshutdown
== SHUTDOWN_XMASK
) {
983 ipn_node
->shutdown
=mustshutdown
;
985 if (!S_ISSOCK(ndpath
.dentry
->d_inode
->i_mode
)) {
989 ipnn
=ipn_find_network_byinode(ndpath
.dentry
->d_inode
);
990 if (!ipnn
|| (ipnn
->flags
& IPN_FLAG_TERMINATED
)) {
994 if (ipn_node
->protocol
== IPN_ANY
)
995 ipn_node
->protocol
=ipnn
->protocol
;
996 else if (ipnn
->protocol
!= ipn_node
->protocol
) {
1005 if (down_interruptible(&ipnn
->ipnn_mutex
)) {
1009 portno
= ipn_protocol_table
[ipnn
->protocol
]->ipn_p_newport(ipn_node
);
1010 if (portno
>= 0 && portno
<ipnn
->maxports
) {
1011 sock
->state
= SS_CONNECTED
;
1012 ipn_node
->portno
=portno
;
1013 ipnn
->connport
[portno
]=ipn_node
;
1014 if (!(ipn_node
->flags
& IPN_NODEFLAG_BOUND
))
1017 list_del(&ipn_node
->nodelist
);
1018 list_add_tail(&ipn_node
->nodelist
,&ipnn
->connectqueue
);
1019 ipn_net_update_counters(ipnn
,
1020 (ipn_node
->shutdown
& RCV_SHUTDOWN
)?0:1,
1021 (ipn_node
->shutdown
& SEND_SHUTDOWN
)?0:1);
1023 ipn_node
->ipn
=previousipnn
; /* undo changes on ipn_node->ipn */
1026 up(&ipnn
->ipnn_mutex
);
1027 up(&ipn_glob_mutex
);
1033 up(&ipn_glob_mutex
);
1037 int ipn_node_create_connect(struct ipn_node
**ipn_node_out
,
1038 struct ipn_network
*(* ipnn_map
)(void *),void *ipnn_map_arg
) {
1039 struct ipn_node
*ipn_node
;
1040 struct ipn_network
*ipnn
;
1043 ipn_node
=ipn_node_create(&init_net
);
1044 if (down_interruptible(&ipn_glob_mutex
)) {
1046 goto err_ipn_node_release
;
1048 ipnn
=ipnn_map(ipnn_map_arg
);
1050 ipn_node
->protocol
=ipnn
->protocol
;
1051 if (down_interruptible(&ipnn
->ipnn_mutex
)) {
1055 portno
= ipn_protocol_table
[ipnn
->protocol
]->ipn_p_newport(ipn_node
);
1056 if (portno
>= 0 && portno
<ipnn
->maxports
) {
1057 ipn_node
->portno
=portno
;
1058 ipnn
->connport
[portno
]=ipn_node
;
1059 if (!(ipn_node
->flags
& IPN_NODEFLAG_BOUND
)) {
1061 list_del(&ipn_node
->nodelist
);
1063 *ipn_node_out
=ipn_node
;
1064 list_add_tail(&ipn_node
->nodelist
,&ipnn
->connectqueue
);
1065 ipn_net_update_counters(ipnn
,
1066 (ipn_node
->shutdown
& RCV_SHUTDOWN
)?0:1,
1067 (ipn_node
->shutdown
& SEND_SHUTDOWN
)?0:1);
1074 up(&ipnn
->ipnn_mutex
);
1075 up(&ipn_glob_mutex
);
1078 up(&ipnn
->ipnn_mutex
);
1080 up(&ipn_glob_mutex
);
1081 err_ipn_node_release
:
1082 ipn_node_release(ipn_node
);
1087 int ipn_node_connect(struct ipn_node
*ipn_node
)
1089 struct ipn_network
*ipnn
;
1092 if (down_interruptible(&ipn_glob_mutex
))
1093 return -ERESTARTSYS
;
1096 if (down_interruptible(&ipnn
->ipnn_mutex
)) {
1100 portno
= ipn_protocol_table
[ipnn
->protocol
]->ipn_p_newport(ipn_node
);
1101 if (portno
>= 0 && portno
<ipnn
->maxports
) {
1102 ipn_node
->portno
=portno
;
1103 ipnn
->connport
[portno
]=ipn_node
;
1104 if (!(ipn_node
->flags
& IPN_NODEFLAG_BOUND
)) {
1106 list_del(&ipn_node
->nodelist
);
1108 list_add_tail(&ipn_node
->nodelist
,&ipnn
->connectqueue
);
1109 ipn_net_update_counters(ipnn
,
1110 (ipn_node
->shutdown
& RCV_SHUTDOWN
)?0:1,
1111 (ipn_node
->shutdown
& SEND_SHUTDOWN
)?0:1);
1117 up(&ipnn
->ipnn_mutex
);
1118 up(&ipn_glob_mutex
);
1121 up(&ipn_glob_mutex
);
1125 static int ipn_getname(struct socket
*sock
, struct sockaddr
*uaddr
,
1126 int *uaddr_len
, int peer
) {
1127 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1128 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1129 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)uaddr
;
1132 if (down_interruptible(&ipn_glob_mutex
))
1133 return -ERESTARTSYS
;
1135 *uaddr_len
= ipnn
->sunaddr_len
;
1136 memcpy(sunaddr
,&ipnn
->sunaddr
,*uaddr_len
);
1139 up(&ipn_glob_mutex
);
1144 unsigned int ipn_node_poll(struct ipn_node
*ipn_node
, struct file
*file
, poll_table
*wait
) {
1145 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1146 unsigned int mask
=0;
1149 poll_wait(file
,&ipn_node
->read_wait
,wait
);
1150 if (ipnn
->flags
& IPN_FLAG_LOSSLESS
)
1151 poll_wait(file
,&ipnn
->send_wait
,wait
);
1152 /* POLLIN if recv succeeds,
1153 * POLL{PRI,RDNORM} if there are {oob,non-oob} messages */
1154 if (ipn_node
->totmsgcount
> 0) mask
|= POLLIN
;
1155 if (!(list_empty(&ipn_node
->msgqueue
))) mask
|= POLLRDNORM
;
1156 if (!(list_empty(&ipn_node
->oobmsgqueue
))) mask
|= POLLPRI
;
1157 if ((!(ipnn
->flags
& IPN_FLAG_LOSSLESS
)) |
1158 (atomic_read(&ipnn
->msgpool_nelem
) < ipnn
->msgpool_size
))
1159 mask
|= POLLOUT
| POLLWRNORM
;
1164 static unsigned int ipn_poll(struct file
*file
, struct socket
*sock
,
1166 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1167 return ipn_node_poll(ipn_node
, file
, wait
);
1170 /* connect netdev (from ioctl). connect a bound socket to a
1171 * network device TAP or GRAB */
1172 static int ipn_connect_netdev(struct socket
*sock
,struct ifreq
*ifr
)
1175 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1176 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1177 if (!capable(CAP_NET_ADMIN
))
1179 if (sock
->state
!= SS_UNCONNECTED
)
1182 return -ENOTCONN
; /* Maybe we need a different error for "NOT BOUND" */
1183 if (down_interruptible(&ipn_glob_mutex
))
1184 return -ERESTARTSYS
;
1185 if (down_interruptible(&ipnn
->ipnn_mutex
)) {
1186 up(&ipn_glob_mutex
);
1187 return -ERESTARTSYS
;
1189 ipn_node
->netdev
=ipn_netdev_alloc(ipn_node
->net
,ifr
->ifr_flags
,ifr
->ifr_name
,&err
);
1190 if (ipn_node
->netdev
) {
1192 portno
= ipn_protocol_table
[ipnn
->protocol
]->ipn_p_newport(ipn_node
);
1193 if (portno
>= 0 && portno
<ipnn
->maxports
) {
1194 sock
->state
= SS_CONNECTED
;
1195 ipn_node
->portno
=portno
;
1196 ipn_node
->flags
|= ifr
->ifr_flags
& IPN_NODEFLAG_DEVMASK
;
1197 ipnn
->connport
[portno
]=ipn_node
;
1198 err
=ipn_netdev_activate(ipn_node
);
1200 sock
->state
= SS_UNCONNECTED
;
1201 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_delport(ipn_node
);
1202 ipn_node
->netdev
=NULL
;
1203 ipn_node
->portno
= -1;
1204 ipn_node
->flags
&= ~IPN_NODEFLAG_DEVMASK
;
1205 ipnn
->connport
[portno
]=NULL
;
1207 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_postnewport(ipn_node
);
1208 list_del(&ipn_node
->nodelist
);
1209 list_add_tail(&ipn_node
->nodelist
,&ipnn
->connectqueue
);
1212 ipn_netdev_close(ipn_node
);
1214 ipn_node
->netdev
=NULL
;
1218 up(&ipnn
->ipnn_mutex
);
1219 up(&ipn_glob_mutex
);
1223 /* join a netdev, a socket gets connected to a persistent node
1224 * not connected to another socket */
1225 static int ipn_join_netdev(struct socket
*sock
,struct ifreq
*ifr
)
1228 struct net_device
*dev
;
1229 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1230 struct ipn_node
*ipn_joined
;
1231 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1232 if (sock
->state
!= SS_UNCONNECTED
)
1234 if (down_interruptible(&ipn_glob_mutex
))
1235 return -ERESTARTSYS
;
1236 if (down_interruptible(&ipnn
->ipnn_mutex
)) {
1237 up(&ipn_glob_mutex
);
1238 return -ERESTARTSYS
;
1240 dev
=__dev_get_by_name(ipn_node
->net
,ifr
->ifr_name
);
1242 dev
=__dev_get_by_index(ipn_node
->net
,ifr
->ifr_ifindex
);
1243 if (dev
&& (ipn_joined
=ipn_netdev2node(dev
)) != NULL
) { /* the interface does exist */
1245 for (i
=0;i
<ipnn
->maxports
&& ipn_joined
!= ipnn
->connport
[i
] ;i
++)
1247 if (i
< ipnn
->maxports
) { /* found */
1248 /* ipn_joined is substituted to ipn_node */
1249 ((struct ipn_sock
*)sock
->sk
)->node
=ipn_joined
;
1250 ipn_joined
->flags
|= IPN_NODEFLAG_INUSE
;
1252 kmem_cache_free(ipn_node_cache
,ipn_node
);
1257 up(&ipnn
->ipnn_mutex
);
1258 up(&ipn_glob_mutex
);
1262 /* set persistence of a node looking for it by interface name
1263 * (it is for sysadm, to close network interfaces)*/
1264 static int ipn_setpersist_netdev(struct ifreq
*ifr
, int value
)
1266 struct net_device
*dev
;
1267 struct ipn_node
*ipn_node
;
1269 if (!capable(CAP_NET_ADMIN
))
1271 if (down_interruptible(&ipn_glob_mutex
))
1272 return -ERESTARTSYS
;
1273 dev
=__dev_get_by_name(&init_net
,ifr
->ifr_name
);
1275 dev
=__dev_get_by_index(&init_net
,ifr
->ifr_ifindex
);
1276 if (dev
&& (ipn_node
=ipn_netdev2node(dev
)) != NULL
)
1277 _ipn_setpersist(ipn_node
,value
);
1280 up(&ipn_glob_mutex
);
1286 int ipn_node_ioctl(struct ipn_node
*ipn_node
, unsigned int cmd
, unsigned long arg
) {
1287 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1289 (ipn_protocol_table
[ipn_node
->protocol
]->ipn_p_ioctl
!= NULL
)) {
1291 if (down_interruptible(&ipnn
->ipnn_mutex
))
1292 return -ERESTARTSYS
;
1293 rv
=ipn_protocol_table
[ipn_node
->protocol
]->ipn_p_ioctl(ipn_node
,cmd
,arg
);
1294 up(&ipnn
->ipnn_mutex
);
1300 static int ipn_ioctl(struct socket
*sock
, unsigned int cmd
, unsigned long arg
) {
1301 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1302 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1303 void __user
* argp
= (void __user
*)arg
;
1305 struct chrdevreq devr
;
1307 if (ipn_node
->shutdown
== SHUTDOWN_XMASK
)
1314 case IPN_SETPERSIST_NETDEV
:
1315 case IPN_CLRPERSIST_NETDEV
:
1316 case IPN_CONN_NETDEV
:
1317 case IPN_JOIN_NETDEV
:
1319 if (copy_from_user(&ifr
, argp
, sizeof(ifr
)))
1321 ifr
.ifr_name
[IFNAMSIZ
-1] = '\0';
1323 case IPN_REGISTER_CHRDEV
:
1324 case IPN_JOIN_CHRDEV
:
1325 if (copy_from_user(&devr
, argp
, sizeof(devr
)))
1327 /*printk("IPN_REGISTER_CHRDEV %p %d %d %d\n",
1328 argp, sizeof(devr),devr.major, devr.minor);*/
1332 /* actions for unconnected and unbound sockets */
1334 case IPN_SETPERSIST_NETDEV
:
1335 return ipn_setpersist_netdev(&ifr
,1);
1336 case IPN_CLRPERSIST_NETDEV
:
1337 return ipn_setpersist_netdev(&ifr
,0);
1338 case IPN_JOIN_CHRDEV
:
1341 if (!capable(CAP_MKNOD
))
1343 if (ipn_node
->ipn
!= NULL
)
1345 if (down_interruptible(&ipn_glob_mutex
))
1346 return -ERESTARTSYS
;
1347 rv
=ipn_node_bind(ipn_node
, ipn_find_chrdev(&devr
));
1348 up(&ipn_glob_mutex
);
1352 if (capable(CAP_NET_ADMIN
))
1354 if (ipn_node
->netdev
&& (ipn_node
->flags
&IPN_NODEFLAG_TAP
))
1355 return dev_set_mac_address(ipn_node
->netdev
, &ifr
.ifr_hwaddr
);
1357 return -EADDRNOTAVAIL
;
1359 if (ipnn
== NULL
|| (ipnn
->flags
& IPN_FLAG_TERMINATED
))
1361 /* actions for connected or bound sockets */
1363 case IPN_CONN_NETDEV
:
1364 return ipn_connect_netdev(sock
,&ifr
);
1365 case IPN_JOIN_NETDEV
:
1366 return ipn_join_netdev(sock
,&ifr
);
1367 case IPN_SETPERSIST
:
1368 return ipn_setpersist(ipn_node
,arg
);
1369 case IPN_CHRDEV_PERSIST
:
1370 return ipn_chrdev_persistence(ipnn
,arg
);
1371 case IPN_REGISTER_CHRDEV
:
1374 unsigned int reqmajor
=devr
.major
;
1375 if (down_interruptible(&ipnn
->ipnn_mutex
))
1376 return -ERESTARTSYS
;
1377 rv
=ipn_register_chrdev(ipnn
,&devr
);
1378 if (reqmajor
==0 && rv
==0) {
1379 if (copy_to_user(argp
, &devr
, sizeof devr
))
1382 up(&ipnn
->ipnn_mutex
);
1385 case IPN_UNREGISTER_CHRDEV
:
1388 if (down_interruptible(&ipnn
->ipnn_mutex
))
1389 return -ERESTARTSYS
;
1390 rv
=ipn_deregister_chrdev(ipnn
);
1391 up(&ipnn
->ipnn_mutex
);
1395 return ipn_node_ioctl(ipn_node
, cmd
, arg
);
1399 /* shutdown: close socket for input or for output.
1400 * shutdown can be called prior to connect and it is not reversible */
1401 static int ipn_shutdown(struct socket
*sock
, int mode
) {
1402 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1403 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1404 int oldshutdown
=ipn_node
->shutdown
;
1405 mode
= (mode
+1)&(RCV_SHUTDOWN
|SEND_SHUTDOWN
);
1407 /* define the new mode first... */
1408 ipn_node
->shutdown
|= mode
;
1411 /* ... and wait for all pending ops to be completed */
1412 if (down_interruptible(&ipnn
->ipnn_mutex
)) {
1413 ipn_node
->shutdown
= oldshutdown
;
1414 return -ERESTARTSYS
;
1416 oldshutdown
=ipn_node
->shutdown
-oldshutdown
;
1417 if (sock
->state
== SS_CONNECTED
&& oldshutdown
) {
1418 ipn_net_update_counters(ipnn
,
1419 (ipn_node
->shutdown
& RCV_SHUTDOWN
)?0:-1,
1420 (ipn_node
->shutdown
& SEND_SHUTDOWN
)?0:-1);
1423 /* if recv channel has been shut down, flush the recv queue */
1424 if ((ipn_node
->shutdown
& RCV_SHUTDOWN
))
1425 ipn_flush_recvqueue(ipn_node
);
1426 up(&ipnn
->ipnn_mutex
);
1431 /* injectmsg: a new message is entering the ipn network.
1432 * injectmsg gets called by send and by the grab/tap node */
1433 /* handlemsg is protected from ipn_network changes: ipnn->ipnn_mutex is locked */
1434 int ipn_proto_injectmsg(struct ipn_node
*from
, struct msgpool_item
*msg
)
1436 struct ipn_network
*ipnn
=from
->ipn
;
1438 static int recinject
=0;
1439 //printk("INJECTMSG IN\n");
1441 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_handlemsg(from
, msg
);
1442 else if (down_interruptible(&ipnn
->ipnn_mutex
))
1446 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_handlemsg(from
, msg
);
1448 up(&ipnn
->ipnn_mutex
);
1450 //printk("INJECTMSG OUT %d\n",err);
1455 int ipn_node_write(struct ipn_node
*ipn_node
, struct iovec
*msg_iov
, int len
) {
1456 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1457 struct msgpool_item
*newmsg
;
1460 if (unlikely(ipn_node
->shutdown
& SEND_SHUTDOWN
)) {
1461 if (ipn_node
->shutdown
== SHUTDOWN_XMASK
)
1466 if (len
> ipnn
->mtu
)
1468 newmsg
=ipn_msgpool_alloc_locking(ipnn
,len
);
1472 err
=memcpy_fromiovec(newmsg
->data
, msg_iov
, len
);
1474 ipn_proto_injectmsg(ipn_node
, newmsg
);
1475 ipn_msgpool_put(newmsg
,ipnn
);
1479 static int ipn_sendmsg(struct kiocb
*kiocb
, struct socket
*sock
,
1480 struct msghdr
*msg
, size_t len
) {
1481 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1483 if (unlikely(sock
->state
!= SS_CONNECTED
))
1486 return ipn_node_write(ipn_node
, msg
->msg_iov
, len
);
1489 /* ipn_proto_sendmsg is called by protocol implementation to enqueue a
1490 * for a destination (to).*/
1491 /* enqueue a msgitem on the receiver's queue, the queue is protected by a
1492 * spinlock (msglock) */
1493 void ipn_proto_sendmsg(struct ipn_node
*to
, struct msgpool_item
*msg
)
1497 ipn_netdev_sendmsg(to
,msg
);
1500 struct msgitem
*msgitem
;
1501 struct ipn_network
*ipnn
=to
->ipn
;
1502 spin_lock(&to
->msglock
);
1503 if (likely((to
->shutdown
& RCV_SHUTDOWN
)==0)) {
1504 /*if (unlikely((ipnn->flags & IPN_FLAG_LOSSLESS) == 0 ||
1505 to->totmsgcount >= ipnn->msgpool_size))
1507 if (likely(ipnn
->flags
& IPN_FLAG_LOSSLESS
||
1508 to
->totmsgcount
< ipnn
->msgpool_size
)) {
1509 if ((msgitem
=kmem_cache_alloc(ipn_msgitem_cache
,GFP_KERNEL
))!=NULL
) {
1512 list_add_tail(&msgitem
->list
, &to
->msgqueue
);
1513 ipn_msgpool_hold(msg
);
1517 spin_unlock(&to
->msglock
);
1518 wake_up_interruptible(&to
->read_wait
);
1523 /* enqueue an oob message. "to" is the destination */
1524 void ipn_proto_oobsendmsg(struct ipn_node
*to
, struct msgpool_item
*msg
)
1527 if (!to
->netdev
) { /* no oob to netdev */
1528 struct msgitem
*msgitem
;
1529 struct ipn_network
*ipnn
=to
->ipn
;
1530 spin_lock(&to
->msglock
);
1531 if ((to
->shutdown
& RCV_SHUTDOWN_NO_OOB
) == 0 &&
1532 (ipnn
->flags
& IPN_FLAG_LOSSLESS
||
1533 to
->oobmsgcount
< ipnn
->msgpool_size
)) {
1534 if ((msgitem
=kmem_cache_alloc(ipn_msgitem_cache
,GFP_KERNEL
))!=NULL
) {
1538 list_add_tail(&msgitem
->list
, &to
->oobmsgqueue
);
1539 ipn_msgpool_hold(msg
);
1542 spin_unlock(&to
->msglock
);
1543 wake_up_interruptible(&to
->read_wait
);
1549 int ipn_node_read(struct ipn_node
*ipn_node
, struct iovec
*msg_iov
, size_t len
, int *msg_flags
, int flags
) {
1550 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1551 struct msgitem
*msgitem
;
1552 struct msgpool_item
*currmsg
;
1554 if (unlikely((ipn_node
->shutdown
& XRCV_SHUTDOWN
) == XRCV_SHUTDOWN
)) {
1555 if (ipn_node
->shutdown
== SHUTDOWN_XMASK
) /*EOF, nothing can be read*/
1558 return -EPIPE
; /*trying to read on a write only node */
1561 /* wait for a message */
1562 spin_lock(&ipn_node
->msglock
);
1563 while (ipn_node
->totmsgcount
== 0) {
1564 spin_unlock(&ipn_node
->msglock
);
1565 if (wait_event_interruptible(ipn_node
->read_wait
,
1566 !(ipn_node
->totmsgcount
== 0)))
1567 return -ERESTARTSYS
;
1568 spin_lock(&ipn_node
->msglock
);
1570 /* oob gets delivered first. oob are rare */
1571 if (likely(list_empty(&ipn_node
->oobmsgqueue
)))
1572 msgitem
=list_first_entry(&ipn_node
->msgqueue
, struct msgitem
, list
);
1574 msgitem
=list_first_entry(&ipn_node
->oobmsgqueue
, struct msgitem
, list
);
1575 *msg_flags
|= MSG_OOB
;
1576 ipn_node
->oobmsgcount
--;
1578 list_del(&msgitem
->list
);
1579 ipn_node
->totmsgcount
--;
1580 spin_unlock(&ipn_node
->msglock
);
1581 currmsg
=msgitem
->msg
;
1582 if (currmsg
->len
< len
)
1584 memcpy_toiovec(msg_iov
, currmsg
->data
, len
);
1585 ipn_msgpool_put(currmsg
,ipnn
);
1586 kmem_cache_free(ipn_msgitem_cache
,msgitem
);
1590 static int ipn_recvmsg(struct kiocb
*kiocb
, struct socket
*sock
,
1591 struct msghdr
*msg
, size_t len
, int flags
) {
1592 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1594 if (unlikely(sock
->state
!= SS_CONNECTED
))
1597 return ipn_node_read(ipn_node
, msg
->msg_iov
, len
, &msg
->msg_flags
, flags
);
1600 /* resize a network: change the # of communication ports (connport) */
1601 static int ipn_netresize(struct ipn_network
*ipnn
,int newsize
)
1604 struct ipn_node
**newconnport
;
1605 struct ipn_node
**oldconnport
;
1607 if (down_interruptible(&ipnn
->ipnn_mutex
))
1608 return -ERESTARTSYS
;
1609 oldsize
=ipnn
->maxports
;
1610 if (newsize
== oldsize
) {
1611 up(&ipnn
->ipnn_mutex
);
1615 /* shrink a network. all the ports we are going to eliminate
1616 * must be unused! */
1617 if (newsize
< oldsize
) {
1619 for (i
=newsize
; i
<oldsize
; i
++)
1620 if (ipnn
->connport
[i
]) {
1621 up(&ipnn
->ipnn_mutex
);
1626 oldconnport
=ipnn
->connport
;
1627 /* allocate the new connport array and copy the old one */
1628 newconnport
=kzalloc(newsize
* sizeof(struct ipn_node
*),GFP_KERNEL
);
1630 up(&ipnn
->ipnn_mutex
);
1633 memcpy(newconnport
,oldconnport
,min
* sizeof(struct ipn_node
*));
1634 ipnn
->connport
=newconnport
;
1635 ipnn
->maxports
=newsize
;
1636 /* notify the protocol that the netowrk has been resized */
1637 err
=ipn_protocol_table
[ipnn
->protocol
]->ipn_p_resizenet(ipnn
,oldsize
,newsize
);
1639 /* roll back if the resize operation failed for the protocol */
1640 ipnn
->connport
=oldconnport
;
1641 ipnn
->maxports
=oldsize
;
1644 /* successful mission, network resized */
1646 up(&ipnn
->ipnn_mutex
);
1650 /* IPN SETSOCKOPT */
1652 static int ipn_setsockopt(struct socket
*sock
, int level
, int optname
,
1653 char __user
*optval
, unsigned int optlen
)
1655 static int ipn_setsockopt(struct socket
*sock
, int level
, int optname
,
1656 char __user
*optval
, int optlen
)
1659 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1660 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1662 if (ipn_node
->shutdown
== SHUTDOWN_XMASK
)
1664 if (level
!= 0 && level
!= ipn_node
->protocol
+1)
1665 return -EPROTONOSUPPORT
;
1667 /* protocol specific sockopt */
1670 if (down_interruptible(&ipnn
->ipnn_mutex
))
1671 return -ERESTARTSYS
;
1672 rv
=ipn_protocol_table
[ipn_node
->protocol
]->ipn_p_setsockopt(ipn_node
,optname
,optval
,optlen
);
1673 up(&ipnn
->ipnn_mutex
);
1678 if (optname
== IPN_SO_DESCR
) {
1679 if (optlen
> IPN_DESCRLEN
)
1682 memset(ipn_node
->descr
,0,IPN_DESCRLEN
);
1683 if (copy_from_user(ipn_node
->descr
,optval
,optlen
))
1684 ipn_node
->descr
[0]=0;
1686 ipn_node
->descr
[optlen
-1]=0;
1690 if (optlen
< sizeof(int))
1692 else if ((optname
& IPN_SO_PREBIND
) && (ipnn
!= NULL
))
1696 get_user(val
, (int __user
*) optval
);
1697 if ((optname
& IPN_SO_PREBIND
) && !ipn_node
->pbp
) {
1698 struct pre_bind_parms std
=STD_BIND_PARMS
;
1699 ipn_node
->pbp
=kzalloc(sizeof(struct pre_bind_parms
),GFP_KERNEL
);
1702 *(ipn_node
->pbp
)=std
;
1706 if (sock
->state
== SS_UNCONNECTED
)
1707 ipn_node
->portno
=val
;
1711 case IPN_SO_CHANGE_NUMNODES
:
1712 if ((ipn_node
->flags
& IPN_NODEFLAG_BOUND
)!=0) {
1716 return ipn_netresize(ipnn
,val
);
1720 case IPN_SO_WANT_OOB_NUMNODES
:
1722 ipn_node
->flags
|= IPN_NODEFLAG_OOB_NUMNODES
;
1724 ipn_node
->flags
&= ~IPN_NODEFLAG_OOB_NUMNODES
;
1726 case IPN_SO_HANDLE_OOB
:
1728 ipn_node
->shutdown
&= ~RCV_SHUTDOWN_NO_OOB
;
1730 ipn_node
->shutdown
|= RCV_SHUTDOWN_NO_OOB
;
1736 ipn_node
->pbp
->mtu
=val
;
1738 case IPN_SO_NUMNODES
:
1742 ipn_node
->pbp
->maxports
=val
;
1744 case IPN_SO_MSGPOOLSIZE
:
1748 ipn_node
->pbp
->msgpoolsize
=val
;
1751 ipn_node
->pbp
->flags
=val
;
1754 ipn_node
->pbp
->mode
=val
;
1763 /* IPN GETSOCKOPT */
1764 static int ipn_getsockopt(struct socket
*sock
, int level
, int optname
,
1765 char __user
*optval
, int __user
*optlen
) {
1766 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1767 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1770 if (ipn_node
->shutdown
== SHUTDOWN_XMASK
)
1772 if (level
!= 0 && level
!= ipn_node
->protocol
+1)
1773 return -EPROTONOSUPPORT
;
1777 /* protocol specific sockopt */
1778 if (down_interruptible(&ipnn
->ipnn_mutex
))
1779 return -ERESTARTSYS
;
1780 rv
=ipn_protocol_table
[ipn_node
->protocol
]->ipn_p_getsockopt(ipn_node
,optname
,optval
,optlen
);
1781 up(&ipnn
->ipnn_mutex
);
1786 if (get_user(len
, optlen
))
1788 if (optname
== IPN_SO_DESCR
) {
1789 if (len
< IPN_DESCRLEN
)
1792 if (len
> IPN_DESCRLEN
)
1794 if(put_user(len
, optlen
))
1796 if(copy_to_user(optval
,ipn_node
->descr
,len
))
1804 val
=ipn_node
->portno
;
1809 else if (ipn_node
->pbp
)
1810 val
=ipn_node
->pbp
->mtu
;
1812 case IPN_SO_NUMNODES
:
1815 else if (ipn_node
->pbp
)
1816 val
=ipn_node
->pbp
->maxports
;
1818 case IPN_SO_MSGPOOLSIZE
:
1820 val
=ipnn
->msgpool_size
;
1821 else if (ipn_node
->pbp
)
1822 val
=ipn_node
->pbp
->msgpoolsize
;
1827 else if (ipn_node
->pbp
)
1828 val
=ipn_node
->pbp
->flags
;
1833 else if (ipn_node
->pbp
)
1834 val
=ipn_node
->pbp
->mode
;
1840 if (len
< sizeof(int))
1844 if(put_user(len
, optlen
))
1846 if(copy_to_user(optval
,&val
,len
))
1855 /* BROADCAST/HUB implementation */
1857 static int ipn_bcast_newport(struct ipn_node
*newport
) {
1858 struct ipn_network
*ipnn
=newport
->ipn
;
1860 for (i
=0;i
<ipnn
->maxports
;i
++) {
1861 if (ipnn
->connport
[i
] == NULL
)
1867 static int ipn_bcast_handlemsg(struct ipn_node
*from
,
1868 struct msgpool_item
*msgitem
){
1869 struct ipn_network
*ipnn
=from
->ipn
;
1871 struct ipn_node
*ipn_node
;
1872 list_for_each_entry(ipn_node
, &ipnn
->connectqueue
, nodelist
) {
1873 if (ipn_node
!= from
)
1874 ipn_proto_sendmsg(ipn_node
,msgitem
);
1879 static void ipn_null_delport(struct ipn_node
*oldport
) {}
1880 static void ipn_null_postnewport(struct ipn_node
*newport
) {}
1881 static void ipn_null_predelport(struct ipn_node
*oldport
) {}
1882 static int ipn_null_newnet(struct ipn_network
*newnet
) {return 0;}
1883 static int ipn_null_resizenet(struct ipn_network
*net
,int oldsize
,int newsize
) {
1885 static void ipn_null_delnet(struct ipn_network
*oldnet
) {}
1886 static int ipn_null_setsockopt(struct ipn_node
*port
,int optname
,
1887 char __user
*optval
, int optlen
) {return -EOPNOTSUPP
;}
1888 static int ipn_null_getsockopt(struct ipn_node
*port
,int optname
,
1889 char __user
*optval
, int *optlen
) {return -EOPNOTSUPP
;}
1890 static int ipn_null_ioctl(struct ipn_node
*port
,unsigned int request
,
1891 unsigned long arg
) {return -EOPNOTSUPP
;}
1893 /* Protocol Registration/deregisteration */
1895 void ipn_init_protocol(struct ipn_protocol
*p
)
1897 if (p
->ipn_p_delport
== NULL
) p
->ipn_p_delport
=ipn_null_delport
;
1898 if (p
->ipn_p_postnewport
== NULL
) p
->ipn_p_postnewport
=ipn_null_postnewport
;
1899 if (p
->ipn_p_predelport
== NULL
) p
->ipn_p_predelport
=ipn_null_predelport
;
1900 if (p
->ipn_p_newnet
== NULL
) p
->ipn_p_newnet
=ipn_null_newnet
;
1901 if (p
->ipn_p_resizenet
== NULL
) p
->ipn_p_resizenet
=ipn_null_resizenet
;
1902 if (p
->ipn_p_delnet
== NULL
) p
->ipn_p_delnet
=ipn_null_delnet
;
1903 if (p
->ipn_p_setsockopt
== NULL
) p
->ipn_p_setsockopt
=ipn_null_setsockopt
;
1904 if (p
->ipn_p_getsockopt
== NULL
) p
->ipn_p_getsockopt
=ipn_null_getsockopt
;
1905 if (p
->ipn_p_ioctl
== NULL
) p
->ipn_p_ioctl
=ipn_null_ioctl
;
1908 int ipn_proto_register(int protocol
,struct ipn_protocol
*ipn_service
)
1911 if (ipn_service
->ipn_p_newport
== NULL
||
1912 ipn_service
->ipn_p_handlemsg
== NULL
)
1914 ipn_init_protocol(ipn_service
);
1915 if (protocol
> 1 && protocol
<= IPN_MAX_PROTO
) {
1917 if (down_interruptible(&ipn_glob_mutex
))
1918 return -ERESTARTSYS
;
1919 if (ipn_protocol_table
[protocol
])
1922 ipn_service
->refcnt
=0;
1923 ipn_protocol_table
[protocol
]=ipn_service
;
1924 printk(KERN_INFO
"IPN: Registered protocol %d\n",protocol
+1);
1926 up(&ipn_glob_mutex
);
1932 int ipn_proto_deregister(int protocol
)
1935 if (protocol
> 1 && protocol
<= IPN_MAX_PROTO
) {
1937 if (down_interruptible(&ipn_glob_mutex
))
1938 return -ERESTARTSYS
;
1939 if (ipn_protocol_table
[protocol
]) {
1940 if (ipn_protocol_table
[protocol
]->refcnt
== 0) {
1941 ipn_protocol_table
[protocol
]=NULL
;
1942 printk(KERN_INFO
"IPN: Unregistered protocol %d\n",protocol
+1);
1947 up(&ipn_glob_mutex
);
1954 /* Module constructor/destructor */
1955 static struct net_proto_family ipn_family_ops
= {
1957 .create
= ipn_create
,
1958 .owner
= THIS_MODULE
,
1961 /* IPN constructor */
1962 static int ipn_init(void)
1966 ipn_init_protocol(&ipn_bcast
);
1967 ipn_network_cache
=kmem_cache_create("ipn_network",sizeof(struct ipn_network
),0,0,NULL
);
1968 if (!ipn_network_cache
) {
1969 printk(KERN_CRIT
"%s: Cannot create ipn_network SLAB cache!\n",
1975 ipn_node_cache
=kmem_cache_create("ipn_node",sizeof(struct ipn_node
),0,0,NULL
);
1976 if (!ipn_node_cache
) {
1977 printk(KERN_CRIT
"%s: Cannot create ipn_node SLAB cache!\n",
1983 ipn_msgitem_cache
=kmem_cache_create("ipn_msgitem",sizeof(struct msgitem
),0,0,NULL
);
1984 if (!ipn_msgitem_cache
) {
1985 printk(KERN_CRIT
"%s: Cannot create ipn_msgitem SLAB cache!\n",
1991 rc
=ipn_msgbuf_init();
1993 printk(KERN_CRIT
"%s: Cannot create ipn_msgbuf SLAB cache\n",
1995 goto out_net_node_msg
;
1998 rc
=proto_register(&ipn_proto
,1);
2000 printk(KERN_CRIT
"%s: Cannot register the protocol!\n",
2002 goto out_net_node_msg_msgbuf
;
2005 sock_register(&ipn_family_ops
);
2007 printk(KERN_INFO
"IPN: Virtual Square Project, University of Bologna 2007-09\n");
2010 out_net_node_msg_msgbuf
:
2013 kmem_cache_destroy(ipn_msgitem_cache
);
2015 kmem_cache_destroy(ipn_node_cache
);
2017 kmem_cache_destroy(ipn_network_cache
);
2022 /* IPN destructor */
2023 static void ipn_exit(void)
2026 if (ipn_msgitem_cache
)
2027 kmem_cache_destroy(ipn_msgitem_cache
);
2029 kmem_cache_destroy(ipn_node_cache
);
2030 if (ipn_network_cache
)
2031 kmem_cache_destroy(ipn_network_cache
);
2033 sock_unregister(PF_IPN
);
2034 proto_unregister(&ipn_proto
);
2035 printk(KERN_INFO
"IPN removed\n");
2038 module_init(ipn_init
);
2039 module_exit(ipn_exit
);
2041 EXPORT_SYMBOL_GPL(ipn_proto_register
);
2042 EXPORT_SYMBOL_GPL(ipn_proto_deregister
);
2043 EXPORT_SYMBOL_GPL(ipn_proto_sendmsg
);
2044 EXPORT_SYMBOL_GPL(ipn_proto_oobsendmsg
);
2045 EXPORT_SYMBOL_GPL(ipn_msgpool_alloc
);
2046 EXPORT_SYMBOL_GPL(ipn_msgpool_put
);