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)
62 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,9,0)
66 /*extension of RCV_SHUTDOWN defined in include/net/sock.h
67 * when the bit is set recv fails */
68 /* NO_OOB: do not send OOB */
69 #define RCV_SHUTDOWN_NO_OOB 4
70 /* EXTENDED MASK including OOB */
71 #define SHUTDOWN_XMASK (SHUTDOWN_MASK | RCV_SHUTDOWN_NO_OOB)
72 /* if XRCV_SHUTDOWN is all set recv fails */
73 #define XRCV_SHUTDOWN (RCV_SHUTDOWN | RCV_SHUTDOWN_NO_OOB)
75 /* Global MUTEX: this is locked to add/delete/modify networks
76 * this is *not* locked just to send/receive msgs */
78 static DECLARE_MUTEX(ipn_glob_mutex
);
80 static DEFINE_SEMAPHORE(ipn_glob_mutex
);
82 /* Network table and hash */
83 struct hlist_head ipn_network_table
[IPN_HASH_SIZE
+ 1];
84 /* slab(s) for fast data structure allocation */
85 static struct kmem_cache
*ipn_network_cache
;
86 static struct kmem_cache
*ipn_node_cache
;
87 static struct kmem_cache
*ipn_msgitem_cache
;
89 /* Protocol 1: HUB/Broadcast default protocol. Function Prototypes */
90 static int ipn_bcast_newport(struct ipn_node
*newport
);
91 static int ipn_bcast_handlemsg(struct ipn_node
*from
,
92 struct msgpool_item
*msgitem
);
94 /* default protocol IPN_BROADCAST (0) */
95 static struct ipn_protocol ipn_bcast
= {
97 .ipn_p_newport
=ipn_bcast_newport
,
98 .ipn_p_handlemsg
=ipn_bcast_handlemsg
};
100 static struct ipn_protocol
*ipn_protocol_table
[IPN_MAX_PROTO
]={&ipn_bcast
};
102 /* Socket call function prototypes */
103 static int ipn_release(struct socket
*);
104 static int ipn_bind(struct socket
*, struct sockaddr
*, int);
105 static int ipn_connect(struct socket
*, struct sockaddr
*,
106 int addr_len
, int flags
);
107 static int ipn_getname(struct socket
*, struct sockaddr
*, int *, int);
108 static unsigned int ipn_poll(struct file
*, struct socket
*, poll_table
*);
109 static int ipn_ioctl(struct socket
*, unsigned int, unsigned long);
110 static int ipn_shutdown(struct socket
*, int);
111 static int ipn_sendmsg(struct kiocb
*, struct socket
*,
112 struct msghdr
*, size_t);
113 static int ipn_recvmsg(struct kiocb
*, struct socket
*,
114 struct msghdr
*, size_t, int);
116 static int ipn_setsockopt(struct socket
*sock
, int level
, int optname
,
117 char __user
*optval
, unsigned int optlen
);
119 static int ipn_setsockopt(struct socket
*sock
, int level
, int optname
,
120 char __user
*optval
, int optlen
);
122 static int ipn_getsockopt(struct socket
*sock
, int level
, int optname
,
123 char __user
*optval
, int __user
*optlen
);
125 /* Network table Management
126 * inode->ipn_network hash table
127 * protected by ipn_glob_mutex */
128 static inline void ipn_insert_network(struct hlist_head
*list
, struct ipn_network
*ipnn
)
130 hlist_add_head(&ipnn
->hnode
, list
);
133 static inline void ipn_remove_network(struct ipn_network
*ipnn
)
135 hlist_del(&ipnn
->hnode
);
138 static struct ipn_network
*ipn_find_network_byinode(struct inode
*i
)
140 struct ipn_network
*ipnn
;
142 struct hlist_node
*node
;
144 hlist_for_each_entry(ipnn
, node
,
145 &ipn_network_table
[i
->i_ino
& (IPN_HASH_SIZE
- 1)], hnode
)
147 hlist_for_each_entry(ipnn
,
148 &ipn_network_table
[i
->i_ino
& (IPN_HASH_SIZE
- 1)], hnode
)
151 struct dentry
*dentry
= ipnn
->dentry
;
153 if(dentry
&& dentry
->d_inode
== i
)
159 struct ipn_network
*ipn_find_network_byfun(
160 int (*fun
)(struct ipn_network
*,void *),void *funarg
)
162 struct ipn_network
*ipnn
;
165 for (ipn_table_scan
=0;ipn_table_scan
<IPN_HASH_SIZE
;ipn_table_scan
++) {
167 struct hlist_node
*node
;
168 hlist_for_each_entry(ipnn
, node
, &ipn_network_table
[ipn_table_scan
], hnode
)
170 hlist_for_each_entry(ipnn
, &ipn_network_table
[ipn_table_scan
], hnode
)
181 /* msgpool management
182 * msgpool_item are ipn_network dependent (each net has its own MTU)
183 * for each message sent there is one msgpool_item and many struct msgitem
184 * one for each receipient.
185 * msgitem are connected to the node's msgqueue or oobmsgqueue.
186 * when a message is delivered to a process the msgitem is deleted and
187 * the count of the msgpool_item is decreased.
188 * msgpool_item elements gets deleted automatically when count is 0*/
191 struct list_head list
;
192 struct msgpool_item
*msg
;
195 /* alloc a fresh msgpool item. count is set to 1.
198 * for each receipient
199 * enqueue messages to the process (using msgitem), ipn_msgpool_hold
201 * The message can be delivered concurrently. init count to 1 guarantees
202 * that it survives at least until is has been enqueued to all
204 /* msgpool_cache == NULL when the ipn network is using the flexible allocation,
205 * i.e. the msgpool item gets allocated by kmalloc instead of using slab */
206 static inline struct msgpool_item
*_ipn_msgpool_alloc(struct ipn_network
*ipnn
, int len
)
208 struct msgpool_item
*new;
209 if ((new=ipnn
->msgpool_cache
?
210 kmem_cache_alloc(ipnn
->msgpool_cache
,GFP_KERNEL
):
211 kmalloc(sizeof(struct msgpool_item
)+len
,GFP_KERNEL
)) != NULL
) {
212 atomic_set(&new->count
,1);
213 atomic_inc(&ipnn
->msgpool_nelem
);
218 /* ipn_msgpool_alloc returns null when leaky, lossless and no space is left
219 * for new msgpool items. When !leaky, it can allocate more msgpooli items,
220 * this temporary overassignment blocks new messages to be delivered until
221 * there is space for a msgpool item to be allocated */
222 struct msgpool_item
*ipn_msgpool_alloc(struct ipn_network
*ipnn
,int leaky
,int len
)
224 if (leaky
&& (ipnn
->flags
& IPN_FLAG_LOSSLESS
) &&
225 atomic_read(&ipnn
->msgpool_nelem
) < ipnn
->msgpool_size
)
228 return _ipn_msgpool_alloc(ipnn
,len
);
231 /* If the service il LOSSLESS, this msgpool call waits for an
232 * available msgpool item */
233 static inline struct msgpool_item
*ipn_msgpool_alloc_locking(struct ipn_network
*ipnn
,int len
)
235 if (ipnn
->flags
& IPN_FLAG_LOSSLESS
) {
236 while (atomic_read(&ipnn
->msgpool_nelem
) >= ipnn
->msgpool_size
) {
237 if (wait_event_interruptible_exclusive(ipnn
->send_wait
,
238 atomic_read(&ipnn
->msgpool_nelem
) < ipnn
->msgpool_size
))
242 return _ipn_msgpool_alloc(ipnn
,len
);
245 /* register one more user for this msgpool item (i.e. count++) */
246 static inline void ipn_msgpool_hold(struct msgpool_item
*msg
)
248 atomic_inc(&msg
->count
);
251 /* decrease count and delete msgpool_item if count == 0 */
252 void ipn_msgpool_put(struct msgpool_item
*old
,
253 struct ipn_network
*ipnn
)
255 if (atomic_dec_and_test(&old
->count
)) {
256 if (ipnn
->msgpool_cache
)
257 kmem_cache_free(ipnn
->msgpool_cache
,old
);
260 atomic_dec(&ipnn
->msgpool_nelem
);
261 if (ipnn
->flags
& IPN_FLAG_LOSSLESS
) /* this could be done anyway */
262 wake_up_interruptible(&ipnn
->send_wait
);
267 static const struct proto_ops ipn_ops
= {
269 .owner
= THIS_MODULE
,
270 .release
= ipn_release
,
272 .connect
= ipn_connect
,
273 .socketpair
= sock_no_socketpair
,
274 .accept
= sock_no_accept
,
275 .getname
= ipn_getname
,
278 .listen
= sock_no_listen
,
279 .shutdown
= ipn_shutdown
,
280 .setsockopt
= ipn_setsockopt
,
281 .getsockopt
= ipn_getsockopt
,
282 .sendmsg
= ipn_sendmsg
,
283 .recvmsg
= ipn_recvmsg
,
284 .mmap
= sock_no_mmap
,
285 .sendpage
= sock_no_sendpage
,
288 static struct proto ipn_proto
= {
290 .owner
= THIS_MODULE
,
291 .obj_size
= sizeof(struct ipn_sock
),
294 /* create a new ipn_node */
295 struct ipn_node
*ipn_node_create(struct net
*net
)
297 struct ipn_node
*ipn_node
;
298 ipn_node
=kmem_cache_alloc(ipn_node_cache
,GFP_KERNEL
);
299 if (ipn_node
!=NULL
) {
300 INIT_LIST_HEAD(&ipn_node
->nodelist
);
301 ipn_node
->protocol
=0; /* BROADCAST, caller must set a proper value */
302 ipn_node
->flags
=IPN_NODEFLAG_INUSE
;
303 ipn_node
->shutdown
=RCV_SHUTDOWN_NO_OOB
;
304 ipn_node
->descr
[0]=0;
305 ipn_node
->portno
=IPN_PORTNO_ANY
;
307 ipn_node
->netdev
=NULL
;
309 ipn_node
->proto_private
=NULL
;
310 ipn_node
->totmsgcount
=0;
311 ipn_node
->oobmsgcount
=0;
312 spin_lock_init(&ipn_node
->msglock
);
313 INIT_LIST_HEAD(&ipn_node
->msgqueue
);
314 INIT_LIST_HEAD(&ipn_node
->oobmsgqueue
);
316 init_waitqueue_head(&ipn_node
->read_wait
);
323 * ipn_node is a separate structure, pointed by ipn_sock -> node
324 * when a node is "persistent", ipn_node survives while ipn_sock gets released*/
326 static int ipn_create(struct net
*net
,struct socket
*sock
, int protocol
)
328 static int ipn_create(struct net
*net
,struct socket
*sock
,
329 int protocol
, int kern
)
332 struct ipn_sock
*ipn_sk
;
334 if (net
!= &init_net
)
335 return -EAFNOSUPPORT
;
337 if (sock
->type
!= SOCK_RAW
)
342 protocol
=IPN_BROADCAST
-1;
343 if (protocol
< 0 || protocol
>= IPN_MAX_PROTO
||
344 ipn_protocol_table
[protocol
] == NULL
)
345 return -EPROTONOSUPPORT
;
346 ipn_sk
= (struct ipn_sock
*) sk_alloc(net
, PF_IPN
, GFP_KERNEL
, &ipn_proto
);
350 if ((ipn_sk
->node
=ipn_node_create(net
))==NULL
) {
351 sock_put((struct sock
*) ipn_sk
);
354 ipn_sk
->node
->protocol
=protocol
;
355 sock_init_data(sock
,(struct sock
*) ipn_sk
);
356 sock
->state
= SS_UNCONNECTED
;
357 sock
->ops
= &ipn_ops
;
358 sock
->sk
=(struct sock
*)ipn_sk
;
362 /* update # of readers and # of writers counters for an ipn network.
363 * This function sends oob messages to nodes requesting the service */
364 /* LOCKING ipnn_mutex is locked */
365 static void ipn_net_update_counters(struct ipn_network
*ipnn
,
366 int chg_readers
, int chg_writers
) {
367 ipnn
->numreaders
+= chg_readers
;
368 ipnn
->numwriters
+= chg_writers
;
369 if (ipnn
->mtu
>= sizeof(struct numnode_oob
))
371 struct msgpool_item
*ipn_msg
=_ipn_msgpool_alloc(ipnn
,sizeof(struct numnode_oob
));
373 struct numnode_oob
*oob_msg
=(struct numnode_oob
*)(ipn_msg
->data
);
374 struct ipn_node
*ipn_node
;
375 ipn_msg
->len
=sizeof(struct numnode_oob
);
376 oob_msg
->level
=IPN_ANY
;
377 oob_msg
->tag
=IPN_OOB_NUMNODE_TAG
;
378 oob_msg
->numreaders
=ipnn
->numreaders
;
379 oob_msg
->numwriters
=ipnn
->numwriters
;
380 list_for_each_entry(ipn_node
, &ipnn
->connectqueue
, nodelist
) {
381 if (ipn_node
->flags
& IPN_NODEFLAG_OOB_NUMNODES
)
382 ipn_proto_oobsendmsg(ipn_node
,ipn_msg
);
384 ipn_msgpool_put(ipn_msg
,ipnn
);
389 /* flush pending messages (for close and shutdown RCV) */
390 /* LOCKING: ipnn_mutex is locked */
391 static void ipn_flush_recvqueue(struct ipn_node
*ipn_node
)
393 struct ipn_network
*ipnn
=ipn_node
->ipn
;
394 spin_lock(&ipn_node
->msglock
);
395 while (!list_empty(&ipn_node
->msgqueue
)) {
396 struct msgitem
*msgitem
=
397 list_first_entry(&ipn_node
->msgqueue
, struct msgitem
, list
);
398 list_del(&msgitem
->list
);
399 ipn_node
->totmsgcount
--;
400 ipn_msgpool_put(msgitem
->msg
,ipnn
);
401 kmem_cache_free(ipn_msgitem_cache
,msgitem
);
403 spin_unlock(&ipn_node
->msglock
);
406 /* flush pending oob messages (for socket close) */
407 /* LOCKING: ipnn_mutex is locked */
408 static void ipn_flush_oobrecvqueue(struct ipn_node
*ipn_node
)
410 struct ipn_network
*ipnn
=ipn_node
->ipn
;
411 spin_lock(&ipn_node
->msglock
);
412 while (!list_empty(&ipn_node
->oobmsgqueue
)) {
413 struct msgitem
*msgitem
=
414 list_first_entry(&ipn_node
->oobmsgqueue
, struct msgitem
, list
);
415 list_del(&msgitem
->list
);
416 ipn_node
->totmsgcount
--;
417 ipn_node
->oobmsgcount
--;
418 ipn_msgpool_put(msgitem
->msg
,ipnn
);
419 kmem_cache_free(ipn_msgitem_cache
,msgitem
);
421 spin_unlock(&ipn_node
->msglock
);
424 /* Terminate node. The node is "logically" terminated. */
425 /* LOCKING: ipn_glob_lock must be locked here */
426 static int ipn_terminate_node(struct ipn_node
*ipn_node
)
428 struct ipn_network
*ipnn
=ipn_node
->ipn
;
430 if (down_interruptible(&ipnn
->ipnn_mutex
))
432 if (ipn_node
->portno
>= 0) {
433 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_predelport(ipn_node
);
434 ipnn
->connport
[ipn_node
->portno
]=NULL
;
436 list_del(&ipn_node
->nodelist
);
437 ipn_flush_recvqueue(ipn_node
);
438 ipn_flush_oobrecvqueue(ipn_node
);
439 if (ipn_node
->portno
>= 0)
440 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_delport(ipn_node
);
442 ipn_net_update_counters(ipnn
,
443 (ipn_node
->shutdown
& RCV_SHUTDOWN
)?0:-1,
444 (ipn_node
->shutdown
& SEND_SHUTDOWN
)?0:-1);
445 ipn_node
->shutdown
= SHUTDOWN_XMASK
;
446 up(&ipnn
->ipnn_mutex
);
447 if (ipn_node
->netdev
)
448 ipn_netdev_close(ipn_node
);
449 /* No more network elements */
451 if (ipnn
->refcnt
== 0 && !ipn_is_persistent_chrdev(ipnn
))
454 ipn_deregister_chrdev(ipnn
);
455 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_delnet(ipnn
);
456 ipn_remove_network(ipnn
);
457 ipn_protocol_table
[ipnn
->protocol
]->refcnt
--;
462 if (ipnn
->msgpool_cache
)
463 ipn_msgbuf_put(ipnn
->msgpool_cache
);
465 kfree(ipnn
->connport
);
466 kmem_cache_free(ipn_network_cache
, ipnn
);
467 module_put(THIS_MODULE
);
471 kfree(ipn_node
->pbp
);
477 /* release of an ipn_node */
478 int ipn_node_release(struct ipn_node
*ipn_node
)
481 if (down_interruptible(&ipn_glob_mutex
))
483 if (ipn_node
->flags
& IPN_NODEFLAG_PERSIST
) {
484 ipn_node
->flags
&= ~IPN_NODEFLAG_INUSE
;
488 rv
=ipn_terminate_node(ipn_node
);
492 kmem_cache_free(ipn_node_cache
,ipn_node
);
498 /* release of an ipn socket */
499 static int ipn_release (struct socket
*sock
)
501 struct ipn_sock
*ipn_sk
=(struct ipn_sock
*)sock
->sk
;
502 struct ipn_node
*ipn_node
=ipn_sk
->node
;
503 int rv
=ipn_node_release(ipn_node
);
505 sock_put((struct sock
*) ipn_sk
);
509 /* _set persist, change the persistence of a node,
510 * when persistence gets cleared and the node is no longer used
511 * the node is terminated and freed.
512 * ipn_glob_mutex must be locked */
513 static int _ipn_setpersist(struct ipn_node
*ipn_node
, int persist
)
517 ipn_node
->flags
|= IPN_NODEFLAG_PERSIST
;
519 ipn_node
->flags
&= ~IPN_NODEFLAG_PERSIST
;
520 if (!(ipn_node
->flags
& IPN_NODEFLAG_INUSE
)) {
521 rv
=ipn_terminate_node(ipn_node
);
523 kmem_cache_free(ipn_node_cache
,ipn_node
);
530 * lock ipn_glob_mutex and call __ipn_setpersist above */
531 static int ipn_setpersist(struct ipn_node
*ipn_node
, int persist
)
534 if (ipn_node
->netdev
== NULL
)
536 if (down_interruptible(&ipn_glob_mutex
))
538 rv
=_ipn_setpersist(ipn_node
,persist
);
543 /* several network parameters can be set by setsockopt prior to bind */
544 /* struct pre_bind_parms is a temporary stucture connected to ipn_node->pbp
545 * to keep the parameter values. */
546 struct pre_bind_parms
{
547 unsigned short maxports
;
548 unsigned short flags
;
549 unsigned short msgpoolsize
;
554 /* STD_PARMS: BITS_PER_LONG nodes, no flags, BITS_PER_BYTE pending msgs,
555 * Ethernet + VLAN MTU*/
556 #define STD_BIND_PARMS {BITS_PER_LONG, 0, BITS_PER_BYTE, 1514, 0777};
558 static int ipn_mkname(struct sockaddr_un
* sunaddr
, int len
)
560 if (len
<= sizeof(short) || len
> sizeof(*sunaddr
))
562 if (!sunaddr
|| sunaddr
->sun_family
!= AF_IPN
)
565 * This may look like an off by one error but it is a bit more
566 * subtle. 108 is the longest valid AF_IPN path for a binding.
567 * sun_path[108] doesnt as such exist. However in kernel space
568 * we are guaranteed that it is a valid memory location in our
569 * kernel address buffer.
571 ((char *)sunaddr
)[len
]=0;
572 len
= strlen(sunaddr
->sun_path
)+1+sizeof(short);
576 static int ipn_node_bind(struct ipn_node
*ipn_node
, struct ipn_network
*ipnn
)
580 if (ipn_node
->ipn
!= NULL
)
583 list_add_tail(&ipn_node
->nodelist
,&ipnn
->unconnectqueue
);
585 ipn_node
->flags
|= IPN_NODEFLAG_BOUND
;
591 static int ipn_bind(struct socket
*sock
, struct sockaddr
*uaddr
, int addr_len
)
593 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)uaddr
;
594 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
596 struct ipn_network
*ipnn
;
597 struct dentry
* dentry
= NULL
;
599 struct pre_bind_parms parms
=STD_BIND_PARMS
;
601 //printk("IPN bind\n");
603 if (down_interruptible(&ipn_glob_mutex
))
605 if (sock
->state
!= SS_UNCONNECTED
||
606 ipn_node
->ipn
!= NULL
) {
611 if (ipn_node
->protocol
>= 0 &&
612 (ipn_node
->protocol
>= IPN_MAX_PROTO
||
613 ipn_protocol_table
[ipn_node
->protocol
] == NULL
)) {
614 err
= -EPROTONOSUPPORT
;
618 addr_len
= ipn_mkname(sunaddr
, addr_len
);
624 /* check if there is already an ipn-network socket with that name */
625 err
= kern_path(sunaddr
->sun_path
, LOOKUP_FOLLOW
, &path
);
626 if (err
) { /* it does not exist, NEW IPN socket! */
628 /* Do I have the permission to create a file? */
629 dentry
= kern_path_create(AT_FDCWD
, sunaddr
->sun_path
, &path
, 0);
630 err
= PTR_ERR(dentry
);
632 goto out_mknod_unlock
;
634 * All right, let's create it.
637 mode
= ipn_node
->pbp
->mode
;
639 mode
= SOCK_INODE(sock
)->i_mode
;
640 mode
= S_IFSOCK
| (mode
& ~current_umask());
641 err
= mnt_want_write(path
.mnt
);
646 err
= vfs_mknod(path
.dentry
->d_inode
, dentry
, path
.mnt
, mode
, 0);
648 err
= vfs_mknod(path
.dentry
->d_inode
, dentry
, mode
, 0);
651 mnt_drop_write(path
.mnt
);
654 mutex_unlock(&path
.dentry
->d_inode
->i_mutex
);
656 path
.dentry
= dentry
;
657 /* create a new ipn_network item */
659 parms
=*ipn_node
->pbp
;
660 ipnn
=kmem_cache_zalloc(ipn_network_cache
,GFP_KERNEL
);
663 goto out_mknod_dput_ipnn
;
665 ipnn
->connport
=kzalloc(parms
.maxports
* sizeof(struct ipn_node
*),GFP_KERNEL
);
666 if (!ipnn
->connport
) {
668 goto out_mknod_dput_ipnn2
;
671 /* module refcnt is incremented for each network, thus
672 * rmmod is forbidden if there are persistent nodes */
673 if (!try_module_get(THIS_MODULE
)) {
675 goto out_mknod_dput_ipnn2
;
677 memcpy(&ipnn
->sunaddr
,sunaddr
,addr_len
);
679 ipnn
->flags
=parms
.flags
;
680 if (ipnn
->flags
& IPN_FLAG_FLEXMTU
)
681 ipnn
->msgpool_cache
= NULL
;
683 ipnn
->msgpool_cache
=ipn_msgbuf_get(ipnn
->mtu
);
684 if (!ipnn
->msgpool_cache
) {
686 goto out_mknod_dput_putmodule
;
689 INIT_LIST_HEAD(&ipnn
->unconnectqueue
);
690 INIT_LIST_HEAD(&ipnn
->connectqueue
);
692 ipnn
->dentry
=path
.dentry
;
694 sema_init(&ipnn
->ipnn_mutex
,1);
695 ipnn
->sunaddr_len
=addr_len
;
696 ipnn
->protocol
=ipn_node
->protocol
;
697 if (ipnn
->protocol
< 0) ipnn
->protocol
= 0;
698 ipn_protocol_table
[ipnn
->protocol
]->refcnt
++;
701 ipnn
->maxports
=parms
.maxports
;
702 atomic_set(&ipnn
->msgpool_nelem
,0);
703 ipnn
->msgpool_size
=parms
.msgpoolsize
;
704 ipnn
->proto_private
=NULL
;
705 init_waitqueue_head(&ipnn
->send_wait
);
707 err
=ipn_protocol_table
[ipnn
->protocol
]->ipn_p_newnet(ipnn
);
709 goto out_mknod_dput_putmodule
;
710 ipn_insert_network(&ipn_network_table
[path
.dentry
->d_inode
->i_ino
& (IPN_HASH_SIZE
-1)],ipnn
);
712 /* join an existing network */
713 if (parms
.flags
& IPN_FLAG_EXCL
) {
717 err
= inode_permission(path
.dentry
->d_inode
, MAY_EXEC
);
721 if (!S_ISSOCK(path
.dentry
->d_inode
->i_mode
))
723 ipnn
=ipn_find_network_byinode(path
.dentry
->d_inode
);
724 if (!ipnn
|| (ipnn
->flags
& IPN_FLAG_TERMINATED
) ||
725 (ipnn
->flags
& IPN_FLAG_EXCL
))
729 kfree(ipn_node
->pbp
);
732 ipn_node_bind(ipn_node
,ipnn
);
742 out_mknod_dput_putmodule
:
743 module_put(THIS_MODULE
);
744 out_mknod_dput_ipnn2
:
745 kfree(ipnn
->connport
);
747 kmem_cache_free(ipn_network_cache
,ipnn
);
750 mutex_unlock(&path
.dentry
->d_inode
->i_mutex
);
760 static int ipn_bind(struct socket
*sock
, struct sockaddr
*uaddr
, int addr_len
)
762 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)uaddr
;
763 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
765 struct ipn_network
*ipnn
;
766 struct dentry
* dentry
= NULL
;
768 struct pre_bind_parms parms
=STD_BIND_PARMS
;
770 //printk("IPN bind\n");
772 if (down_interruptible(&ipn_glob_mutex
))
774 if (sock
->state
!= SS_UNCONNECTED
||
775 ipn_node
->ipn
!= NULL
) {
780 if (ipn_node
->protocol
>= 0 &&
781 (ipn_node
->protocol
>= IPN_MAX_PROTO
||
782 ipn_protocol_table
[ipn_node
->protocol
] == NULL
)) {
783 err
= -EPROTONOSUPPORT
;
787 addr_len
= ipn_mkname(sunaddr
, addr_len
);
793 /* check if there is already an ipn-network socket with that name */
795 err
= kern_path(sunaddr
->sun_path
, LOOKUP_FOLLOW
, &nd
.path
);
797 err
= path_lookup(sunaddr
->sun_path
, LOOKUP_FOLLOW
, &nd
);
799 if (err
) { /* it does not exist, NEW IPN socket! */
801 /* Is it everything okay with the parent? */
803 err
= kern_path_parent(sunaddr
->sun_path
, &nd
);
805 err
= path_lookup(sunaddr
->sun_path
, LOOKUP_PARENT
, &nd
);
808 goto out_mknod_parent
;
809 /* Do I have the permission to create a file? */
810 dentry
= lookup_create(&nd
, 0);
811 err
= PTR_ERR(dentry
);
813 goto out_mknod_unlock
;
815 * All right, let's create it.
818 mode
= ipn_node
->pbp
->mode
;
820 mode
= SOCK_INODE(sock
)->i_mode
;
822 mode
= S_IFSOCK
| (mode
& ~current
->fs
->umask
);
824 mode
= S_IFSOCK
| (mode
& ~current_umask());
827 err
= vfs_mknod(nd
.path
.dentry
->d_inode
, dentry
, nd
.path
.mnt
, mode
, 0);
829 err
= vfs_mknod(nd
.path
.dentry
->d_inode
, dentry
, mode
, 0);
833 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
834 dput(nd
.path
.dentry
);
835 nd
.path
.dentry
= dentry
;
836 /* create a new ipn_network item */
838 parms
=*ipn_node
->pbp
;
839 ipnn
=kmem_cache_zalloc(ipn_network_cache
,GFP_KERNEL
);
842 goto out_mknod_dput_ipnn
;
844 ipnn
->connport
=kzalloc(parms
.maxports
* sizeof(struct ipn_node
*),GFP_KERNEL
);
845 if (!ipnn
->connport
) {
847 goto out_mknod_dput_ipnn2
;
850 /* module refcnt is incremented for each network, thus
851 * rmmod is forbidden if there are persistent nodes */
852 if (!try_module_get(THIS_MODULE
)) {
854 goto out_mknod_dput_ipnn2
;
856 memcpy(&ipnn
->sunaddr
,sunaddr
,addr_len
);
858 ipnn
->flags
=parms
.flags
;
859 if (ipnn
->flags
& IPN_FLAG_FLEXMTU
)
860 ipnn
->msgpool_cache
= NULL
;
862 ipnn
->msgpool_cache
=ipn_msgbuf_get(ipnn
->mtu
);
863 if (!ipnn
->msgpool_cache
) {
865 goto out_mknod_dput_putmodule
;
868 INIT_LIST_HEAD(&ipnn
->unconnectqueue
);
869 INIT_LIST_HEAD(&ipnn
->connectqueue
);
871 ipnn
->dentry
=nd
.path
.dentry
;
872 ipnn
->mnt
=nd
.path
.mnt
;
873 sema_init(&ipnn
->ipnn_mutex
,1);
874 ipnn
->sunaddr_len
=addr_len
;
875 ipnn
->protocol
=ipn_node
->protocol
;
876 if (ipnn
->protocol
< 0) ipnn
->protocol
= 0;
877 ipn_protocol_table
[ipnn
->protocol
]->refcnt
++;
880 ipnn
->maxports
=parms
.maxports
;
881 atomic_set(&ipnn
->msgpool_nelem
,0);
882 ipnn
->msgpool_size
=parms
.msgpoolsize
;
883 ipnn
->proto_private
=NULL
;
884 init_waitqueue_head(&ipnn
->send_wait
);
886 err
=ipn_protocol_table
[ipnn
->protocol
]->ipn_p_newnet(ipnn
);
888 goto out_mknod_dput_putmodule
;
889 ipn_insert_network(&ipn_network_table
[nd
.path
.dentry
->d_inode
->i_ino
& (IPN_HASH_SIZE
-1)],ipnn
);
891 /* join an existing network */
892 if (parms
.flags
& IPN_FLAG_EXCL
) {
896 err
= inode_permission(nd
.path
.dentry
->d_inode
, MAY_EXEC
);
900 if (!S_ISSOCK(nd
.path
.dentry
->d_inode
->i_mode
))
902 ipnn
=ipn_find_network_byinode(nd
.path
.dentry
->d_inode
);
903 if (!ipnn
|| (ipnn
->flags
& IPN_FLAG_TERMINATED
) ||
904 (ipnn
->flags
& IPN_FLAG_EXCL
))
908 kfree(ipn_node
->pbp
);
911 ipn_node_bind(ipn_node
,ipnn
);
921 out_mknod_dput_putmodule
:
922 module_put(THIS_MODULE
);
923 out_mknod_dput_ipnn2
:
924 kfree(ipnn
->connport
);
926 kmem_cache_free(ipn_network_cache
,ipnn
);
930 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
941 static int ipn_connect(struct socket
*sock
, struct sockaddr
*addr
,
942 int addr_len
, int flags
){
943 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)addr
;
944 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
949 #define ndpath nd.path
952 struct ipn_network
*ipnn
,*previousipnn
;
956 /* the socket cannot be connected twice */
957 if (sock
->state
!= SS_UNCONNECTED
)
960 if (down_interruptible(&ipn_glob_mutex
))
963 if ((previousipnn
=ipn_node
->ipn
) == NULL
) { /* unbound */
964 unsigned char mustshutdown
=0;
965 err
= ipn_mkname(sunaddr
, addr_len
);
970 err
= kern_path(sunaddr
->sun_path
, LOOKUP_FOLLOW
, &ndpath
);
972 err
= path_lookup(sunaddr
->sun_path
, LOOKUP_FOLLOW
, &nd
);
976 err
= inode_permission(ndpath
.dentry
->d_inode
, MAY_READ
);
978 if (err
== -EACCES
|| err
== -EROFS
)
979 mustshutdown
|=RCV_SHUTDOWN
;
983 err
= inode_permission(ndpath
.dentry
->d_inode
, MAY_WRITE
);
986 mustshutdown
|=SEND_SHUTDOWN
;
990 mustshutdown
|= ipn_node
->shutdown
;
991 /* if the combination of shutdown and permissions leaves
992 * no abilities, connect returns EACCES */
993 if (mustshutdown
== SHUTDOWN_XMASK
) {
998 ipn_node
->shutdown
=mustshutdown
;
1000 if (!S_ISSOCK(ndpath
.dentry
->d_inode
->i_mode
)) {
1001 err
= -ECONNREFUSED
;
1004 ipnn
=ipn_find_network_byinode(ndpath
.dentry
->d_inode
);
1005 if (!ipnn
|| (ipnn
->flags
& IPN_FLAG_TERMINATED
)) {
1006 err
= -ECONNREFUSED
;
1009 if (ipn_node
->protocol
== IPN_ANY
)
1010 ipn_node
->protocol
=ipnn
->protocol
;
1011 else if (ipnn
->protocol
!= ipn_node
->protocol
) {
1020 if (down_interruptible(&ipnn
->ipnn_mutex
)) {
1024 portno
= ipn_protocol_table
[ipnn
->protocol
]->ipn_p_newport(ipn_node
);
1025 if (portno
>= 0 && portno
<ipnn
->maxports
) {
1026 sock
->state
= SS_CONNECTED
;
1027 ipn_node
->portno
=portno
;
1028 ipnn
->connport
[portno
]=ipn_node
;
1029 if (!(ipn_node
->flags
& IPN_NODEFLAG_BOUND
))
1032 list_del(&ipn_node
->nodelist
);
1033 list_add_tail(&ipn_node
->nodelist
,&ipnn
->connectqueue
);
1034 ipn_net_update_counters(ipnn
,
1035 (ipn_node
->shutdown
& RCV_SHUTDOWN
)?0:1,
1036 (ipn_node
->shutdown
& SEND_SHUTDOWN
)?0:1);
1038 ipn_node
->ipn
=previousipnn
; /* undo changes on ipn_node->ipn */
1041 up(&ipnn
->ipnn_mutex
);
1042 up(&ipn_glob_mutex
);
1048 up(&ipn_glob_mutex
);
1052 int ipn_node_create_connect(struct ipn_node
**ipn_node_out
,
1053 struct ipn_network
*(* ipnn_map
)(void *),void *ipnn_map_arg
) {
1054 struct ipn_node
*ipn_node
;
1055 struct ipn_network
*ipnn
;
1058 ipn_node
=ipn_node_create(&init_net
);
1059 if (down_interruptible(&ipn_glob_mutex
)) {
1061 goto err_ipn_node_release
;
1063 ipnn
=ipnn_map(ipnn_map_arg
);
1065 ipn_node
->protocol
=ipnn
->protocol
;
1066 if (down_interruptible(&ipnn
->ipnn_mutex
)) {
1070 portno
= ipn_protocol_table
[ipnn
->protocol
]->ipn_p_newport(ipn_node
);
1071 if (portno
>= 0 && portno
<ipnn
->maxports
) {
1072 ipn_node
->portno
=portno
;
1073 ipnn
->connport
[portno
]=ipn_node
;
1074 if (!(ipn_node
->flags
& IPN_NODEFLAG_BOUND
)) {
1076 list_del(&ipn_node
->nodelist
);
1078 *ipn_node_out
=ipn_node
;
1079 list_add_tail(&ipn_node
->nodelist
,&ipnn
->connectqueue
);
1080 ipn_net_update_counters(ipnn
,
1081 (ipn_node
->shutdown
& RCV_SHUTDOWN
)?0:1,
1082 (ipn_node
->shutdown
& SEND_SHUTDOWN
)?0:1);
1089 up(&ipnn
->ipnn_mutex
);
1090 up(&ipn_glob_mutex
);
1093 up(&ipnn
->ipnn_mutex
);
1095 up(&ipn_glob_mutex
);
1096 err_ipn_node_release
:
1097 ipn_node_release(ipn_node
);
1102 int ipn_node_connect(struct ipn_node
*ipn_node
)
1104 struct ipn_network
*ipnn
;
1107 if (down_interruptible(&ipn_glob_mutex
))
1108 return -ERESTARTSYS
;
1111 if (down_interruptible(&ipnn
->ipnn_mutex
)) {
1115 portno
= ipn_protocol_table
[ipnn
->protocol
]->ipn_p_newport(ipn_node
);
1116 if (portno
>= 0 && portno
<ipnn
->maxports
) {
1117 ipn_node
->portno
=portno
;
1118 ipnn
->connport
[portno
]=ipn_node
;
1119 if (!(ipn_node
->flags
& IPN_NODEFLAG_BOUND
)) {
1121 list_del(&ipn_node
->nodelist
);
1123 list_add_tail(&ipn_node
->nodelist
,&ipnn
->connectqueue
);
1124 ipn_net_update_counters(ipnn
,
1125 (ipn_node
->shutdown
& RCV_SHUTDOWN
)?0:1,
1126 (ipn_node
->shutdown
& SEND_SHUTDOWN
)?0:1);
1132 up(&ipnn
->ipnn_mutex
);
1133 up(&ipn_glob_mutex
);
1136 up(&ipn_glob_mutex
);
1140 static int ipn_getname(struct socket
*sock
, struct sockaddr
*uaddr
,
1141 int *uaddr_len
, int peer
) {
1142 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1143 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1144 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)uaddr
;
1147 if (down_interruptible(&ipn_glob_mutex
))
1148 return -ERESTARTSYS
;
1150 *uaddr_len
= ipnn
->sunaddr_len
;
1151 memcpy(sunaddr
,&ipnn
->sunaddr
,*uaddr_len
);
1154 up(&ipn_glob_mutex
);
1159 unsigned int ipn_node_poll(struct ipn_node
*ipn_node
, struct file
*file
, poll_table
*wait
) {
1160 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1161 unsigned int mask
=0;
1164 poll_wait(file
,&ipn_node
->read_wait
,wait
);
1165 if (ipnn
->flags
& IPN_FLAG_LOSSLESS
)
1166 poll_wait(file
,&ipnn
->send_wait
,wait
);
1167 /* POLLIN if recv succeeds,
1168 * POLL{PRI,RDNORM} if there are {oob,non-oob} messages */
1169 if (ipn_node
->totmsgcount
> 0) mask
|= POLLIN
;
1170 if (!(list_empty(&ipn_node
->msgqueue
))) mask
|= POLLRDNORM
;
1171 if (!(list_empty(&ipn_node
->oobmsgqueue
))) mask
|= POLLPRI
;
1172 if ((!(ipnn
->flags
& IPN_FLAG_LOSSLESS
)) |
1173 (atomic_read(&ipnn
->msgpool_nelem
) < ipnn
->msgpool_size
))
1174 mask
|= POLLOUT
| POLLWRNORM
;
1179 static unsigned int ipn_poll(struct file
*file
, struct socket
*sock
,
1181 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1182 return ipn_node_poll(ipn_node
, file
, wait
);
1185 /* connect netdev (from ioctl). connect a bound socket to a
1186 * network device TAP or GRAB */
1187 static int ipn_connect_netdev(struct socket
*sock
,struct ifreq
*ifr
)
1190 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1191 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1192 if (!capable(CAP_NET_ADMIN
))
1194 if (sock
->state
!= SS_UNCONNECTED
)
1197 return -ENOTCONN
; /* Maybe we need a different error for "NOT BOUND" */
1198 if (down_interruptible(&ipn_glob_mutex
))
1199 return -ERESTARTSYS
;
1200 if (down_interruptible(&ipnn
->ipnn_mutex
)) {
1201 up(&ipn_glob_mutex
);
1202 return -ERESTARTSYS
;
1204 ipn_node
->netdev
=ipn_netdev_alloc(ipn_node
->net
,ifr
->ifr_flags
,ifr
->ifr_name
,&err
);
1205 if (ipn_node
->netdev
) {
1207 portno
= ipn_protocol_table
[ipnn
->protocol
]->ipn_p_newport(ipn_node
);
1208 if (portno
>= 0 && portno
<ipnn
->maxports
) {
1209 sock
->state
= SS_CONNECTED
;
1210 ipn_node
->portno
=portno
;
1211 ipn_node
->flags
|= ifr
->ifr_flags
& IPN_NODEFLAG_DEVMASK
;
1212 ipnn
->connport
[portno
]=ipn_node
;
1213 err
=ipn_netdev_activate(ipn_node
);
1215 sock
->state
= SS_UNCONNECTED
;
1216 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_delport(ipn_node
);
1217 ipn_node
->netdev
=NULL
;
1218 ipn_node
->portno
= -1;
1219 ipn_node
->flags
&= ~IPN_NODEFLAG_DEVMASK
;
1220 ipnn
->connport
[portno
]=NULL
;
1222 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_postnewport(ipn_node
);
1223 list_del(&ipn_node
->nodelist
);
1224 list_add_tail(&ipn_node
->nodelist
,&ipnn
->connectqueue
);
1227 ipn_netdev_close(ipn_node
);
1229 ipn_node
->netdev
=NULL
;
1233 up(&ipnn
->ipnn_mutex
);
1234 up(&ipn_glob_mutex
);
1238 /* join a netdev, a socket gets connected to a persistent node
1239 * not connected to another socket */
1240 static int ipn_join_netdev(struct socket
*sock
,struct ifreq
*ifr
)
1243 struct net_device
*dev
;
1244 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1245 struct ipn_node
*ipn_joined
;
1246 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1247 if (sock
->state
!= SS_UNCONNECTED
)
1249 if (down_interruptible(&ipn_glob_mutex
))
1250 return -ERESTARTSYS
;
1251 if (down_interruptible(&ipnn
->ipnn_mutex
)) {
1252 up(&ipn_glob_mutex
);
1253 return -ERESTARTSYS
;
1255 dev
=__dev_get_by_name(ipn_node
->net
,ifr
->ifr_name
);
1257 dev
=__dev_get_by_index(ipn_node
->net
,ifr
->ifr_ifindex
);
1258 if (dev
&& (ipn_joined
=ipn_netdev2node(dev
)) != NULL
) { /* the interface does exist */
1260 for (i
=0;i
<ipnn
->maxports
&& ipn_joined
!= ipnn
->connport
[i
] ;i
++)
1262 if (i
< ipnn
->maxports
) { /* found */
1263 /* ipn_joined is substituted to ipn_node */
1264 ((struct ipn_sock
*)sock
->sk
)->node
=ipn_joined
;
1265 ipn_joined
->flags
|= IPN_NODEFLAG_INUSE
;
1267 kmem_cache_free(ipn_node_cache
,ipn_node
);
1272 up(&ipnn
->ipnn_mutex
);
1273 up(&ipn_glob_mutex
);
1277 /* set persistence of a node looking for it by interface name
1278 * (it is for sysadm, to close network interfaces)*/
1279 static int ipn_setpersist_netdev(struct ifreq
*ifr
, int value
)
1281 struct net_device
*dev
;
1282 struct ipn_node
*ipn_node
;
1284 if (!capable(CAP_NET_ADMIN
))
1286 if (down_interruptible(&ipn_glob_mutex
))
1287 return -ERESTARTSYS
;
1288 dev
=__dev_get_by_name(&init_net
,ifr
->ifr_name
);
1290 dev
=__dev_get_by_index(&init_net
,ifr
->ifr_ifindex
);
1291 if (dev
&& (ipn_node
=ipn_netdev2node(dev
)) != NULL
)
1292 _ipn_setpersist(ipn_node
,value
);
1295 up(&ipn_glob_mutex
);
1301 int ipn_node_ioctl(struct ipn_node
*ipn_node
, unsigned int cmd
, unsigned long arg
) {
1302 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1304 (ipn_protocol_table
[ipn_node
->protocol
]->ipn_p_ioctl
!= NULL
)) {
1306 if (down_interruptible(&ipnn
->ipnn_mutex
))
1307 return -ERESTARTSYS
;
1308 rv
=ipn_protocol_table
[ipn_node
->protocol
]->ipn_p_ioctl(ipn_node
,cmd
,arg
);
1309 up(&ipnn
->ipnn_mutex
);
1315 static int ipn_ioctl(struct socket
*sock
, unsigned int cmd
, unsigned long arg
) {
1316 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1317 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1318 void __user
* argp
= (void __user
*)arg
;
1320 struct chrdevreq devr
;
1322 if (ipn_node
->shutdown
== SHUTDOWN_XMASK
)
1329 case IPN_SETPERSIST_NETDEV
:
1330 case IPN_CLRPERSIST_NETDEV
:
1331 case IPN_CONN_NETDEV
:
1332 case IPN_JOIN_NETDEV
:
1334 if (copy_from_user(&ifr
, argp
, sizeof(ifr
)))
1336 ifr
.ifr_name
[IFNAMSIZ
-1] = '\0';
1338 case IPN_REGISTER_CHRDEV
:
1339 case IPN_JOIN_CHRDEV
:
1340 if (copy_from_user(&devr
, argp
, sizeof(devr
)))
1342 /*printk("IPN_REGISTER_CHRDEV %p %d %d %d\n",
1343 argp, sizeof(devr),devr.major, devr.minor);*/
1347 /* actions for unconnected and unbound sockets */
1349 case IPN_SETPERSIST_NETDEV
:
1350 return ipn_setpersist_netdev(&ifr
,1);
1351 case IPN_CLRPERSIST_NETDEV
:
1352 return ipn_setpersist_netdev(&ifr
,0);
1353 case IPN_JOIN_CHRDEV
:
1356 if (!capable(CAP_MKNOD
))
1358 if (ipn_node
->ipn
!= NULL
)
1360 if (down_interruptible(&ipn_glob_mutex
))
1361 return -ERESTARTSYS
;
1362 rv
=ipn_node_bind(ipn_node
, ipn_find_chrdev(&devr
));
1363 up(&ipn_glob_mutex
);
1367 if (capable(CAP_NET_ADMIN
))
1369 if (ipn_node
->netdev
&& (ipn_node
->flags
&IPN_NODEFLAG_TAP
))
1370 return dev_set_mac_address(ipn_node
->netdev
, &ifr
.ifr_hwaddr
);
1372 return -EADDRNOTAVAIL
;
1374 if (ipnn
== NULL
|| (ipnn
->flags
& IPN_FLAG_TERMINATED
))
1376 /* actions for connected or bound sockets */
1378 case IPN_CONN_NETDEV
:
1379 return ipn_connect_netdev(sock
,&ifr
);
1380 case IPN_JOIN_NETDEV
:
1381 return ipn_join_netdev(sock
,&ifr
);
1382 case IPN_SETPERSIST
:
1383 return ipn_setpersist(ipn_node
,arg
);
1384 case IPN_CHRDEV_PERSIST
:
1385 return ipn_chrdev_persistence(ipnn
,arg
);
1386 case IPN_REGISTER_CHRDEV
:
1389 unsigned int reqmajor
=devr
.major
;
1390 if (down_interruptible(&ipnn
->ipnn_mutex
))
1391 return -ERESTARTSYS
;
1392 rv
=ipn_register_chrdev(ipnn
,&devr
);
1393 if (reqmajor
==0 && rv
==0) {
1394 if (copy_to_user(argp
, &devr
, sizeof devr
))
1397 up(&ipnn
->ipnn_mutex
);
1400 case IPN_UNREGISTER_CHRDEV
:
1403 if (down_interruptible(&ipnn
->ipnn_mutex
))
1404 return -ERESTARTSYS
;
1405 rv
=ipn_deregister_chrdev(ipnn
);
1406 up(&ipnn
->ipnn_mutex
);
1410 return ipn_node_ioctl(ipn_node
, cmd
, arg
);
1414 /* shutdown: close socket for input or for output.
1415 * shutdown can be called prior to connect and it is not reversible */
1416 static int ipn_shutdown(struct socket
*sock
, int mode
) {
1417 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1418 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1419 int oldshutdown
=ipn_node
->shutdown
;
1420 mode
= (mode
+1)&(RCV_SHUTDOWN
|SEND_SHUTDOWN
);
1422 /* define the new mode first... */
1423 ipn_node
->shutdown
|= mode
;
1426 /* ... and wait for all pending ops to be completed */
1427 if (down_interruptible(&ipnn
->ipnn_mutex
)) {
1428 ipn_node
->shutdown
= oldshutdown
;
1429 return -ERESTARTSYS
;
1431 oldshutdown
=ipn_node
->shutdown
-oldshutdown
;
1432 if (sock
->state
== SS_CONNECTED
&& oldshutdown
) {
1433 ipn_net_update_counters(ipnn
,
1434 (ipn_node
->shutdown
& RCV_SHUTDOWN
)?0:-1,
1435 (ipn_node
->shutdown
& SEND_SHUTDOWN
)?0:-1);
1438 /* if recv channel has been shut down, flush the recv queue */
1439 if ((ipn_node
->shutdown
& RCV_SHUTDOWN
))
1440 ipn_flush_recvqueue(ipn_node
);
1441 up(&ipnn
->ipnn_mutex
);
1446 /* injectmsg: a new message is entering the ipn network.
1447 * injectmsg gets called by send and by the grab/tap node */
1448 /* handlemsg is protected from ipn_network changes: ipnn->ipnn_mutex is locked */
1449 int ipn_proto_injectmsg(struct ipn_node
*from
, struct msgpool_item
*msg
)
1451 struct ipn_network
*ipnn
=from
->ipn
;
1453 static int recinject
=0;
1454 //printk("INJECTMSG IN\n");
1456 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_handlemsg(from
, msg
);
1457 else if (down_interruptible(&ipnn
->ipnn_mutex
))
1461 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_handlemsg(from
, msg
);
1463 up(&ipnn
->ipnn_mutex
);
1465 //printk("INJECTMSG OUT %d\n",err);
1470 int ipn_node_write(struct ipn_node
*ipn_node
, struct iovec
*msg_iov
, int len
) {
1471 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1472 struct msgpool_item
*newmsg
;
1475 if (unlikely(ipn_node
->shutdown
& SEND_SHUTDOWN
)) {
1476 if (ipn_node
->shutdown
== SHUTDOWN_XMASK
)
1481 if (len
> ipnn
->mtu
)
1483 newmsg
=ipn_msgpool_alloc_locking(ipnn
,len
);
1487 err
=memcpy_fromiovec(newmsg
->data
, msg_iov
, len
);
1489 ipn_proto_injectmsg(ipn_node
, newmsg
);
1490 ipn_msgpool_put(newmsg
,ipnn
);
1494 static int ipn_sendmsg(struct kiocb
*kiocb
, struct socket
*sock
,
1495 struct msghdr
*msg
, size_t len
) {
1496 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1498 if (unlikely(sock
->state
!= SS_CONNECTED
))
1501 return ipn_node_write(ipn_node
, msg
->msg_iov
, len
);
1504 /* ipn_proto_sendmsg is called by protocol implementation to enqueue a
1505 * for a destination (to).*/
1506 /* enqueue a msgitem on the receiver's queue, the queue is protected by a
1507 * spinlock (msglock) */
1508 void ipn_proto_sendmsg(struct ipn_node
*to
, struct msgpool_item
*msg
)
1512 ipn_netdev_sendmsg(to
,msg
);
1515 struct msgitem
*msgitem
;
1516 struct ipn_network
*ipnn
=to
->ipn
;
1517 spin_lock(&to
->msglock
);
1518 if (likely((to
->shutdown
& RCV_SHUTDOWN
)==0)) {
1519 /*if (unlikely((ipnn->flags & IPN_FLAG_LOSSLESS) == 0 ||
1520 to->totmsgcount >= ipnn->msgpool_size))
1522 if (likely(ipnn
->flags
& IPN_FLAG_LOSSLESS
||
1523 to
->totmsgcount
< ipnn
->msgpool_size
)) {
1524 if ((msgitem
=kmem_cache_alloc(ipn_msgitem_cache
,GFP_KERNEL
))!=NULL
) {
1527 list_add_tail(&msgitem
->list
, &to
->msgqueue
);
1528 ipn_msgpool_hold(msg
);
1532 spin_unlock(&to
->msglock
);
1533 wake_up_interruptible(&to
->read_wait
);
1538 /* enqueue an oob message. "to" is the destination */
1539 void ipn_proto_oobsendmsg(struct ipn_node
*to
, struct msgpool_item
*msg
)
1542 if (!to
->netdev
) { /* no oob to netdev */
1543 struct msgitem
*msgitem
;
1544 struct ipn_network
*ipnn
=to
->ipn
;
1545 spin_lock(&to
->msglock
);
1546 if ((to
->shutdown
& RCV_SHUTDOWN_NO_OOB
) == 0 &&
1547 (ipnn
->flags
& IPN_FLAG_LOSSLESS
||
1548 to
->oobmsgcount
< ipnn
->msgpool_size
)) {
1549 if ((msgitem
=kmem_cache_alloc(ipn_msgitem_cache
,GFP_KERNEL
))!=NULL
) {
1553 list_add_tail(&msgitem
->list
, &to
->oobmsgqueue
);
1554 ipn_msgpool_hold(msg
);
1557 spin_unlock(&to
->msglock
);
1558 wake_up_interruptible(&to
->read_wait
);
1564 int ipn_node_read(struct ipn_node
*ipn_node
, struct iovec
*msg_iov
, size_t len
, int *msg_flags
, int flags
) {
1565 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1566 struct msgitem
*msgitem
;
1567 struct msgpool_item
*currmsg
;
1569 if (unlikely((ipn_node
->shutdown
& XRCV_SHUTDOWN
) == XRCV_SHUTDOWN
)) {
1570 if (ipn_node
->shutdown
== SHUTDOWN_XMASK
) /*EOF, nothing can be read*/
1573 return -EPIPE
; /*trying to read on a write only node */
1576 /* wait for a message */
1577 spin_lock(&ipn_node
->msglock
);
1578 while (ipn_node
->totmsgcount
== 0) {
1579 spin_unlock(&ipn_node
->msglock
);
1580 if (flags
& MSG_DONTWAIT
)
1581 return -EWOULDBLOCK
;
1582 if (wait_event_interruptible(ipn_node
->read_wait
,
1583 !(ipn_node
->totmsgcount
== 0)))
1584 return -ERESTARTSYS
;
1585 spin_lock(&ipn_node
->msglock
);
1587 /* oob gets delivered first. oob are rare */
1588 if (likely(list_empty(&ipn_node
->oobmsgqueue
)))
1589 msgitem
=list_first_entry(&ipn_node
->msgqueue
, struct msgitem
, list
);
1591 msgitem
=list_first_entry(&ipn_node
->oobmsgqueue
, struct msgitem
, list
);
1592 *msg_flags
|= MSG_OOB
;
1593 ipn_node
->oobmsgcount
--;
1595 list_del(&msgitem
->list
);
1596 ipn_node
->totmsgcount
--;
1597 spin_unlock(&ipn_node
->msglock
);
1598 currmsg
=msgitem
->msg
;
1599 if (currmsg
->len
< len
)
1601 memcpy_toiovec(msg_iov
, currmsg
->data
, len
);
1602 ipn_msgpool_put(currmsg
,ipnn
);
1603 kmem_cache_free(ipn_msgitem_cache
,msgitem
);
1607 static int ipn_recvmsg(struct kiocb
*kiocb
, struct socket
*sock
,
1608 struct msghdr
*msg
, size_t len
, int flags
) {
1609 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1611 if (unlikely(sock
->state
!= SS_CONNECTED
))
1614 return ipn_node_read(ipn_node
, msg
->msg_iov
, len
, &msg
->msg_flags
, flags
);
1617 /* resize a network: change the # of communication ports (connport) */
1618 static int ipn_netresize(struct ipn_network
*ipnn
,int newsize
)
1621 struct ipn_node
**newconnport
;
1622 struct ipn_node
**oldconnport
;
1624 if (down_interruptible(&ipnn
->ipnn_mutex
))
1625 return -ERESTARTSYS
;
1626 oldsize
=ipnn
->maxports
;
1627 if (newsize
== oldsize
) {
1628 up(&ipnn
->ipnn_mutex
);
1632 /* shrink a network. all the ports we are going to eliminate
1633 * must be unused! */
1634 if (newsize
< oldsize
) {
1636 for (i
=newsize
; i
<oldsize
; i
++)
1637 if (ipnn
->connport
[i
]) {
1638 up(&ipnn
->ipnn_mutex
);
1643 oldconnport
=ipnn
->connport
;
1644 /* allocate the new connport array and copy the old one */
1645 newconnport
=kzalloc(newsize
* sizeof(struct ipn_node
*),GFP_KERNEL
);
1647 up(&ipnn
->ipnn_mutex
);
1650 memcpy(newconnport
,oldconnport
,min
* sizeof(struct ipn_node
*));
1651 ipnn
->connport
=newconnport
;
1652 ipnn
->maxports
=newsize
;
1653 /* notify the protocol that the netowrk has been resized */
1654 err
=ipn_protocol_table
[ipnn
->protocol
]->ipn_p_resizenet(ipnn
,oldsize
,newsize
);
1656 /* roll back if the resize operation failed for the protocol */
1657 ipnn
->connport
=oldconnport
;
1658 ipnn
->maxports
=oldsize
;
1661 /* successful mission, network resized */
1663 up(&ipnn
->ipnn_mutex
);
1667 /* IPN SETSOCKOPT */
1669 static int ipn_setsockopt(struct socket
*sock
, int level
, int optname
,
1670 char __user
*optval
, unsigned int optlen
)
1672 static int ipn_setsockopt(struct socket
*sock
, int level
, int optname
,
1673 char __user
*optval
, int optlen
)
1676 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1677 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1679 if (ipn_node
->shutdown
== SHUTDOWN_XMASK
)
1681 if (level
!= 0 && level
!= ipn_node
->protocol
+1)
1682 return -EPROTONOSUPPORT
;
1684 /* protocol specific sockopt */
1687 if (down_interruptible(&ipnn
->ipnn_mutex
))
1688 return -ERESTARTSYS
;
1689 rv
=ipn_protocol_table
[ipn_node
->protocol
]->ipn_p_setsockopt(ipn_node
,optname
,optval
,optlen
);
1690 up(&ipnn
->ipnn_mutex
);
1695 if (optname
== IPN_SO_DESCR
) {
1696 if (optlen
> IPN_DESCRLEN
)
1699 memset(ipn_node
->descr
,0,IPN_DESCRLEN
);
1700 if (copy_from_user(ipn_node
->descr
,optval
,optlen
))
1701 ipn_node
->descr
[0]=0;
1703 ipn_node
->descr
[optlen
-1]=0;
1707 if (optlen
< sizeof(int))
1709 else if ((optname
& IPN_SO_PREBIND
) && (ipnn
!= NULL
))
1713 get_user(val
, (int __user
*) optval
);
1714 if ((optname
& IPN_SO_PREBIND
) && !ipn_node
->pbp
) {
1715 struct pre_bind_parms std
=STD_BIND_PARMS
;
1716 ipn_node
->pbp
=kzalloc(sizeof(struct pre_bind_parms
),GFP_KERNEL
);
1719 *(ipn_node
->pbp
)=std
;
1723 if (sock
->state
== SS_UNCONNECTED
)
1724 ipn_node
->portno
=val
;
1728 case IPN_SO_CHANGE_NUMNODES
:
1729 if ((ipn_node
->flags
& IPN_NODEFLAG_BOUND
)!=0) {
1733 return ipn_netresize(ipnn
,val
);
1737 case IPN_SO_WANT_OOB_NUMNODES
:
1739 ipn_node
->flags
|= IPN_NODEFLAG_OOB_NUMNODES
;
1741 ipn_node
->flags
&= ~IPN_NODEFLAG_OOB_NUMNODES
;
1743 case IPN_SO_HANDLE_OOB
:
1745 ipn_node
->shutdown
&= ~RCV_SHUTDOWN_NO_OOB
;
1747 ipn_node
->shutdown
|= RCV_SHUTDOWN_NO_OOB
;
1753 ipn_node
->pbp
->mtu
=val
;
1755 case IPN_SO_NUMNODES
:
1759 ipn_node
->pbp
->maxports
=val
;
1761 case IPN_SO_MSGPOOLSIZE
:
1765 ipn_node
->pbp
->msgpoolsize
=val
;
1768 ipn_node
->pbp
->flags
=val
;
1771 ipn_node
->pbp
->mode
=val
;
1780 /* IPN GETSOCKOPT */
1781 static int ipn_getsockopt(struct socket
*sock
, int level
, int optname
,
1782 char __user
*optval
, int __user
*optlen
) {
1783 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1784 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1787 if (ipn_node
->shutdown
== SHUTDOWN_XMASK
)
1789 if (level
!= 0 && level
!= ipn_node
->protocol
+1)
1790 return -EPROTONOSUPPORT
;
1794 /* protocol specific sockopt */
1795 if (down_interruptible(&ipnn
->ipnn_mutex
))
1796 return -ERESTARTSYS
;
1797 rv
=ipn_protocol_table
[ipn_node
->protocol
]->ipn_p_getsockopt(ipn_node
,optname
,optval
,optlen
);
1798 up(&ipnn
->ipnn_mutex
);
1803 if (get_user(len
, optlen
))
1805 if (optname
== IPN_SO_DESCR
) {
1806 if (len
< IPN_DESCRLEN
)
1809 if (len
> IPN_DESCRLEN
)
1811 if(put_user(len
, optlen
))
1813 if(copy_to_user(optval
,ipn_node
->descr
,len
))
1821 val
=ipn_node
->portno
;
1826 else if (ipn_node
->pbp
)
1827 val
=ipn_node
->pbp
->mtu
;
1829 case IPN_SO_NUMNODES
:
1832 else if (ipn_node
->pbp
)
1833 val
=ipn_node
->pbp
->maxports
;
1835 case IPN_SO_MSGPOOLSIZE
:
1837 val
=ipnn
->msgpool_size
;
1838 else if (ipn_node
->pbp
)
1839 val
=ipn_node
->pbp
->msgpoolsize
;
1844 else if (ipn_node
->pbp
)
1845 val
=ipn_node
->pbp
->flags
;
1850 else if (ipn_node
->pbp
)
1851 val
=ipn_node
->pbp
->mode
;
1857 if (len
< sizeof(int))
1861 if(put_user(len
, optlen
))
1863 if(copy_to_user(optval
,&val
,len
))
1872 /* BROADCAST/HUB implementation */
1874 static int ipn_bcast_newport(struct ipn_node
*newport
) {
1875 struct ipn_network
*ipnn
=newport
->ipn
;
1877 for (i
=0;i
<ipnn
->maxports
;i
++) {
1878 if (ipnn
->connport
[i
] == NULL
)
1884 static int ipn_bcast_handlemsg(struct ipn_node
*from
,
1885 struct msgpool_item
*msgitem
){
1886 struct ipn_network
*ipnn
=from
->ipn
;
1888 struct ipn_node
*ipn_node
;
1889 list_for_each_entry(ipn_node
, &ipnn
->connectqueue
, nodelist
) {
1890 if (ipn_node
!= from
)
1891 ipn_proto_sendmsg(ipn_node
,msgitem
);
1896 static void ipn_null_delport(struct ipn_node
*oldport
) {}
1897 static void ipn_null_postnewport(struct ipn_node
*newport
) {}
1898 static void ipn_null_predelport(struct ipn_node
*oldport
) {}
1899 static int ipn_null_newnet(struct ipn_network
*newnet
) {return 0;}
1900 static int ipn_null_resizenet(struct ipn_network
*net
,int oldsize
,int newsize
) {
1902 static void ipn_null_delnet(struct ipn_network
*oldnet
) {}
1903 static int ipn_null_setsockopt(struct ipn_node
*port
,int optname
,
1904 char __user
*optval
, int optlen
) {return -EOPNOTSUPP
;}
1905 static int ipn_null_getsockopt(struct ipn_node
*port
,int optname
,
1906 char __user
*optval
, int *optlen
) {return -EOPNOTSUPP
;}
1907 static int ipn_null_ioctl(struct ipn_node
*port
,unsigned int request
,
1908 unsigned long arg
) {return -EOPNOTSUPP
;}
1910 /* Protocol Registration/deregisteration */
1912 void ipn_init_protocol(struct ipn_protocol
*p
)
1914 if (p
->ipn_p_delport
== NULL
) p
->ipn_p_delport
=ipn_null_delport
;
1915 if (p
->ipn_p_postnewport
== NULL
) p
->ipn_p_postnewport
=ipn_null_postnewport
;
1916 if (p
->ipn_p_predelport
== NULL
) p
->ipn_p_predelport
=ipn_null_predelport
;
1917 if (p
->ipn_p_newnet
== NULL
) p
->ipn_p_newnet
=ipn_null_newnet
;
1918 if (p
->ipn_p_resizenet
== NULL
) p
->ipn_p_resizenet
=ipn_null_resizenet
;
1919 if (p
->ipn_p_delnet
== NULL
) p
->ipn_p_delnet
=ipn_null_delnet
;
1920 if (p
->ipn_p_setsockopt
== NULL
) p
->ipn_p_setsockopt
=ipn_null_setsockopt
;
1921 if (p
->ipn_p_getsockopt
== NULL
) p
->ipn_p_getsockopt
=ipn_null_getsockopt
;
1922 if (p
->ipn_p_ioctl
== NULL
) p
->ipn_p_ioctl
=ipn_null_ioctl
;
1925 int ipn_proto_register(int protocol
,struct ipn_protocol
*ipn_service
)
1928 if (ipn_service
->ipn_p_newport
== NULL
||
1929 ipn_service
->ipn_p_handlemsg
== NULL
)
1931 ipn_init_protocol(ipn_service
);
1932 if (protocol
> 1 && protocol
<= IPN_MAX_PROTO
) {
1934 if (down_interruptible(&ipn_glob_mutex
))
1935 return -ERESTARTSYS
;
1936 if (ipn_protocol_table
[protocol
])
1939 ipn_service
->refcnt
=0;
1940 ipn_protocol_table
[protocol
]=ipn_service
;
1941 printk(KERN_INFO
"IPN: Registered protocol %d\n",protocol
+1);
1943 up(&ipn_glob_mutex
);
1949 int ipn_proto_deregister(int protocol
)
1952 if (protocol
> 1 && protocol
<= IPN_MAX_PROTO
) {
1954 if (down_interruptible(&ipn_glob_mutex
))
1955 return -ERESTARTSYS
;
1956 if (ipn_protocol_table
[protocol
]) {
1957 if (ipn_protocol_table
[protocol
]->refcnt
== 0) {
1958 ipn_protocol_table
[protocol
]=NULL
;
1959 printk(KERN_INFO
"IPN: Unregistered protocol %d\n",protocol
+1);
1964 up(&ipn_glob_mutex
);
1971 /* Module constructor/destructor */
1972 static struct net_proto_family ipn_family_ops
= {
1974 .create
= ipn_create
,
1975 .owner
= THIS_MODULE
,
1978 /* IPN constructor */
1979 static int ipn_init(void)
1983 ipn_init_protocol(&ipn_bcast
);
1984 ipn_network_cache
=kmem_cache_create("ipn_network",sizeof(struct ipn_network
),0,0,NULL
);
1985 if (!ipn_network_cache
) {
1986 printk(KERN_CRIT
"%s: Cannot create ipn_network SLAB cache!\n",
1992 ipn_node_cache
=kmem_cache_create("ipn_node",sizeof(struct ipn_node
),0,0,NULL
);
1993 if (!ipn_node_cache
) {
1994 printk(KERN_CRIT
"%s: Cannot create ipn_node SLAB cache!\n",
2000 ipn_msgitem_cache
=kmem_cache_create("ipn_msgitem",sizeof(struct msgitem
),0,0,NULL
);
2001 if (!ipn_msgitem_cache
) {
2002 printk(KERN_CRIT
"%s: Cannot create ipn_msgitem SLAB cache!\n",
2008 rc
=ipn_msgbuf_init();
2010 printk(KERN_CRIT
"%s: Cannot create ipn_msgbuf SLAB cache\n",
2012 goto out_net_node_msg
;
2015 rc
=proto_register(&ipn_proto
,1);
2017 printk(KERN_CRIT
"%s: Cannot register the protocol!\n",
2019 goto out_net_node_msg_msgbuf
;
2022 sock_register(&ipn_family_ops
);
2024 printk(KERN_INFO
"IPN: Virtual Square Project, University of Bologna 2007-09\n");
2027 out_net_node_msg_msgbuf
:
2030 kmem_cache_destroy(ipn_msgitem_cache
);
2032 kmem_cache_destroy(ipn_node_cache
);
2034 kmem_cache_destroy(ipn_network_cache
);
2039 /* IPN destructor */
2040 static void ipn_exit(void)
2043 if (ipn_msgitem_cache
)
2044 kmem_cache_destroy(ipn_msgitem_cache
);
2046 kmem_cache_destroy(ipn_node_cache
);
2047 if (ipn_network_cache
)
2048 kmem_cache_destroy(ipn_network_cache
);
2050 sock_unregister(PF_IPN
);
2051 proto_unregister(&ipn_proto
);
2052 printk(KERN_INFO
"IPN removed\n");
2055 module_init(ipn_init
);
2056 module_exit(ipn_exit
);
2058 EXPORT_SYMBOL_GPL(ipn_proto_register
);
2059 EXPORT_SYMBOL_GPL(ipn_proto_deregister
);
2060 EXPORT_SYMBOL_GPL(ipn_proto_sendmsg
);
2061 EXPORT_SYMBOL_GPL(ipn_proto_oobsendmsg
);
2062 EXPORT_SYMBOL_GPL(ipn_msgpool_alloc
);
2063 EXPORT_SYMBOL_GPL(ipn_msgpool_put
);