2 * Main inter process networking (virtual distributed ethernet) module
3 * (part of the View-OS project: wiki.virtualsquare.org)
5 * Copyright (C) 2007 Renzo Davoli (renzo@cs.unibo.it)
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * Due to this file being licensed under the GPL there is controversy over
13 * whether this permits you to write a module that #includes this file
14 * without placing your module under the GPL. Please consult a lawyer for
15 * advice before doing this.
17 * WARNING: THIS CODE IS ALREADY EXPERIMENTAL
21 #include <linux/init.h>
22 #include <linux/module.h>
23 #include <linux/socket.h>
24 #include <linux/namei.h>
25 #include <linux/poll.h>
27 #include <linux/list.h>
28 #include <linux/mount.h>
29 #include <linux/version.h>
32 #include <net/af_ipn.h>
35 #include "ipn_netdev.h"
36 #include "ipn_msgbuf.h"
37 #include "ipn_chrdev.h"
39 MODULE_LICENSE("GPL");
40 MODULE_AUTHOR("VIEW-OS TEAM");
41 MODULE_DESCRIPTION("IPN Kernel Module");
43 #define IPN_MAX_PROTO 4
44 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25)
47 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,32)
51 /*extension of RCV_SHUTDOWN defined in include/net/sock.h
52 * when the bit is set recv fails */
53 /* NO_OOB: do not send OOB */
54 #define RCV_SHUTDOWN_NO_OOB 4
55 /* EXTENDED MASK including OOB */
56 #define SHUTDOWN_XMASK (SHUTDOWN_MASK | RCV_SHUTDOWN_NO_OOB)
57 /* if XRCV_SHUTDOWN is all set recv fails */
58 #define XRCV_SHUTDOWN (RCV_SHUTDOWN | RCV_SHUTDOWN_NO_OOB)
60 /* Global MUTEX: this is locked to add/delete/modify networks
61 * this is *not* locked just to send/receive msgs */
62 static DECLARE_MUTEX(ipn_glob_mutex
);
63 /* Network table and hash */
64 struct hlist_head ipn_network_table
[IPN_HASH_SIZE
+ 1];
65 /* slab(s) for fast data structure allocation */
66 static struct kmem_cache
*ipn_network_cache
;
67 static struct kmem_cache
*ipn_node_cache
;
68 static struct kmem_cache
*ipn_msgitem_cache
;
70 /* Protocol 1: HUB/Broadcast default protocol. Function Prototypes */
71 static int ipn_bcast_newport(struct ipn_node
*newport
);
72 static int ipn_bcast_handlemsg(struct ipn_node
*from
,
73 struct msgpool_item
*msgitem
);
75 /* default protocol IPN_BROADCAST (0) */
76 static struct ipn_protocol ipn_bcast
= {
78 .ipn_p_newport
=ipn_bcast_newport
,
79 .ipn_p_handlemsg
=ipn_bcast_handlemsg
};
81 static struct ipn_protocol
*ipn_protocol_table
[IPN_MAX_PROTO
]={&ipn_bcast
};
83 /* Socket call function prototypes */
84 static int ipn_release(struct socket
*);
85 static int ipn_bind(struct socket
*, struct sockaddr
*, int);
86 static int ipn_connect(struct socket
*, struct sockaddr
*,
87 int addr_len
, int flags
);
88 static int ipn_getname(struct socket
*, struct sockaddr
*, int *, int);
89 static unsigned int ipn_poll(struct file
*, struct socket
*, poll_table
*);
90 static int ipn_ioctl(struct socket
*, unsigned int, unsigned long);
91 static int ipn_shutdown(struct socket
*, int);
92 static int ipn_sendmsg(struct kiocb
*, struct socket
*,
93 struct msghdr
*, size_t);
94 static int ipn_recvmsg(struct kiocb
*, struct socket
*,
95 struct msghdr
*, size_t, int);
97 static int ipn_setsockopt(struct socket
*sock
, int level
, int optname
,
98 char __user
*optval
, unsigned int optlen
);
100 static int ipn_setsockopt(struct socket
*sock
, int level
, int optname
,
101 char __user
*optval
, int optlen
);
103 static int ipn_getsockopt(struct socket
*sock
, int level
, int optname
,
104 char __user
*optval
, int __user
*optlen
);
106 /* Network table Management
107 * inode->ipn_network hash table
108 * protected by ipn_glob_mutex */
109 static inline void ipn_insert_network(struct hlist_head
*list
, struct ipn_network
*ipnn
)
111 hlist_add_head(&ipnn
->hnode
, list
);
114 static inline void ipn_remove_network(struct ipn_network
*ipnn
)
116 hlist_del(&ipnn
->hnode
);
119 static struct ipn_network
*ipn_find_network_byinode(struct inode
*i
)
121 struct ipn_network
*ipnn
;
122 struct hlist_node
*node
;
124 hlist_for_each_entry(ipnn
, node
,
125 &ipn_network_table
[i
->i_ino
& (IPN_HASH_SIZE
- 1)], hnode
) {
126 struct dentry
*dentry
= ipnn
->dentry
;
128 if(dentry
&& dentry
->d_inode
== i
)
134 struct ipn_network
*ipn_find_network_byfun(
135 int (*fun
)(struct ipn_network
*,void *),void *funarg
)
137 struct ipn_network
*ipnn
;
138 struct hlist_node
*node
;
141 for (ipn_table_scan
=0;ipn_table_scan
<IPN_HASH_SIZE
;ipn_table_scan
++) {
142 hlist_for_each_entry(ipnn
, node
, &ipn_network_table
[ipn_table_scan
], hnode
) {
150 /* msgpool management
151 * msgpool_item are ipn_network dependent (each net has its own MTU)
152 * for each message sent there is one msgpool_item and many struct msgitem
153 * one for each receipient.
154 * msgitem are connected to the node's msgqueue or oobmsgqueue.
155 * when a message is delivered to a process the msgitem is deleted and
156 * the count of the msgpool_item is decreased.
157 * msgpool_item elements gets deleted automatically when count is 0*/
160 struct list_head list
;
161 struct msgpool_item
*msg
;
164 /* alloc a fresh msgpool item. count is set to 1.
167 * for each receipient
168 * enqueue messages to the process (using msgitem), ipn_msgpool_hold
170 * The message can be delivered concurrently. init count to 1 guarantees
171 * that it survives at least until is has been enqueued to all
173 /* msgpool_cache == NULL when the ipn network is using the flexible allocation,
174 * i.e. the msgpool item gets allocated by kmalloc instead of using slab */
175 static inline struct msgpool_item
*_ipn_msgpool_alloc(struct ipn_network
*ipnn
, int len
)
177 struct msgpool_item
*new;
178 if ((new=ipnn
->msgpool_cache
?
179 kmem_cache_alloc(ipnn
->msgpool_cache
,GFP_KERNEL
):
180 kmalloc(sizeof(struct msgpool_item
)+len
,GFP_KERNEL
)) != NULL
) {
181 atomic_set(&new->count
,1);
182 atomic_inc(&ipnn
->msgpool_nelem
);
187 /* ipn_msgpool_alloc returns null when leaky, lossless and no space is left
188 * for new msgpool items. When !leaky, it can allocate more msgpooli items,
189 * this temporary overassignment blocks new messages to be delivered until
190 * there is space for a msgpool item to be allocated */
191 struct msgpool_item
*ipn_msgpool_alloc(struct ipn_network
*ipnn
,int leaky
,int len
)
193 if (leaky
&& (ipnn
->flags
& IPN_FLAG_LOSSLESS
) &&
194 atomic_read(&ipnn
->msgpool_nelem
) < ipnn
->msgpool_size
)
197 return _ipn_msgpool_alloc(ipnn
,len
);
200 /* If the service il LOSSLESS, this msgpool call waits for an
201 * available msgpool item */
202 static inline struct msgpool_item
*ipn_msgpool_alloc_locking(struct ipn_network
*ipnn
,int len
)
204 if (ipnn
->flags
& IPN_FLAG_LOSSLESS
) {
205 while (atomic_read(&ipnn
->msgpool_nelem
) >= ipnn
->msgpool_size
) {
206 if (wait_event_interruptible_exclusive(ipnn
->send_wait
,
207 atomic_read(&ipnn
->msgpool_nelem
) < ipnn
->msgpool_size
))
211 return _ipn_msgpool_alloc(ipnn
,len
);
214 /* register one more user for this msgpool item (i.e. count++) */
215 static inline void ipn_msgpool_hold(struct msgpool_item
*msg
)
217 atomic_inc(&msg
->count
);
220 /* decrease count and delete msgpool_item if count == 0 */
221 void ipn_msgpool_put(struct msgpool_item
*old
,
222 struct ipn_network
*ipnn
)
224 if (atomic_dec_and_test(&old
->count
)) {
225 if (ipnn
->msgpool_cache
)
226 kmem_cache_free(ipnn
->msgpool_cache
,old
);
229 atomic_dec(&ipnn
->msgpool_nelem
);
230 if (ipnn
->flags
& IPN_FLAG_LOSSLESS
) /* this could be done anyway */
231 wake_up_interruptible(&ipnn
->send_wait
);
236 static const struct proto_ops ipn_ops
= {
238 .owner
= THIS_MODULE
,
239 .release
= ipn_release
,
241 .connect
= ipn_connect
,
242 .socketpair
= sock_no_socketpair
,
243 .accept
= sock_no_accept
,
244 .getname
= ipn_getname
,
247 .listen
= sock_no_listen
,
248 .shutdown
= ipn_shutdown
,
249 .setsockopt
= ipn_setsockopt
,
250 .getsockopt
= ipn_getsockopt
,
251 .sendmsg
= ipn_sendmsg
,
252 .recvmsg
= ipn_recvmsg
,
253 .mmap
= sock_no_mmap
,
254 .sendpage
= sock_no_sendpage
,
257 static struct proto ipn_proto
= {
259 .owner
= THIS_MODULE
,
260 .obj_size
= sizeof(struct ipn_sock
),
263 /* create a new ipn_node */
264 struct ipn_node
*ipn_node_create(struct net
*net
)
266 struct ipn_node
*ipn_node
;
267 ipn_node
=kmem_cache_alloc(ipn_node_cache
,GFP_KERNEL
);
268 if (ipn_node
!=NULL
) {
269 INIT_LIST_HEAD(&ipn_node
->nodelist
);
270 ipn_node
->protocol
=0; /* BROADCAST, caller must set a proper value */
271 ipn_node
->flags
=IPN_NODEFLAG_INUSE
;
272 ipn_node
->shutdown
=RCV_SHUTDOWN_NO_OOB
;
273 ipn_node
->descr
[0]=0;
274 ipn_node
->portno
=IPN_PORTNO_ANY
;
276 ipn_node
->netdev
=NULL
;
278 ipn_node
->proto_private
=NULL
;
279 ipn_node
->totmsgcount
=0;
280 ipn_node
->oobmsgcount
=0;
281 spin_lock_init(&ipn_node
->msglock
);
282 INIT_LIST_HEAD(&ipn_node
->msgqueue
);
283 INIT_LIST_HEAD(&ipn_node
->oobmsgqueue
);
285 init_waitqueue_head(&ipn_node
->read_wait
);
292 * ipn_node is a separate structure, pointed by ipn_sock -> node
293 * when a node is "persistent", ipn_node survives while ipn_sock gets released*/
294 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
295 static int ipn_create(struct socket
*sock
, int protocol
)
296 #elif LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 33)
297 static int ipn_create(struct net
*net
,struct socket
*sock
, int protocol
)
299 static int ipn_create(struct net
*net
,struct socket
*sock
,
300 int protocol
, int kern
)
303 struct ipn_sock
*ipn_sk
;
305 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
308 if (net
!= &init_net
)
309 return -EAFNOSUPPORT
;
312 if (sock
->type
!= SOCK_RAW
)
317 protocol
=IPN_BROADCAST
-1;
318 if (protocol
< 0 || protocol
>= IPN_MAX_PROTO
||
319 ipn_protocol_table
[protocol
] == NULL
)
320 return -EPROTONOSUPPORT
;
321 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
322 ipn_sk
= (struct ipn_sock
*) sk_alloc(PF_IPN
, GFP_KERNEL
, &ipn_proto
, 1);
324 ipn_sk
= (struct ipn_sock
*) sk_alloc(net
, PF_IPN
, GFP_KERNEL
, &ipn_proto
);
329 if ((ipn_sk
->node
=ipn_node_create(net
))==NULL
) {
330 sock_put((struct sock
*) ipn_sk
);
333 ipn_sk
->node
->protocol
=protocol
;
334 sock_init_data(sock
,(struct sock
*) ipn_sk
);
335 sock
->state
= SS_UNCONNECTED
;
336 sock
->ops
= &ipn_ops
;
337 sock
->sk
=(struct sock
*)ipn_sk
;
341 /* update # of readers and # of writers counters for an ipn network.
342 * This function sends oob messages to nodes requesting the service */
343 /* LOCKING ipnn_mutex is locked */
344 static void ipn_net_update_counters(struct ipn_network
*ipnn
,
345 int chg_readers
, int chg_writers
) {
346 ipnn
->numreaders
+= chg_readers
;
347 ipnn
->numwriters
+= chg_writers
;
348 if (ipnn
->mtu
>= sizeof(struct numnode_oob
))
350 struct msgpool_item
*ipn_msg
=_ipn_msgpool_alloc(ipnn
,sizeof(struct numnode_oob
));
352 struct numnode_oob
*oob_msg
=(struct numnode_oob
*)(ipn_msg
->data
);
353 struct ipn_node
*ipn_node
;
354 ipn_msg
->len
=sizeof(struct numnode_oob
);
355 oob_msg
->level
=IPN_ANY
;
356 oob_msg
->tag
=IPN_OOB_NUMNODE_TAG
;
357 oob_msg
->numreaders
=ipnn
->numreaders
;
358 oob_msg
->numwriters
=ipnn
->numwriters
;
359 list_for_each_entry(ipn_node
, &ipnn
->connectqueue
, nodelist
) {
360 if (ipn_node
->flags
& IPN_NODEFLAG_OOB_NUMNODES
)
361 ipn_proto_oobsendmsg(ipn_node
,ipn_msg
);
363 ipn_msgpool_put(ipn_msg
,ipnn
);
368 /* flush pending messages (for close and shutdown RCV) */
369 /* LOCKING: ipnn_mutex is locked */
370 static void ipn_flush_recvqueue(struct ipn_node
*ipn_node
)
372 struct ipn_network
*ipnn
=ipn_node
->ipn
;
373 spin_lock(&ipn_node
->msglock
);
374 while (!list_empty(&ipn_node
->msgqueue
)) {
375 struct msgitem
*msgitem
=
376 list_first_entry(&ipn_node
->msgqueue
, struct msgitem
, list
);
377 list_del(&msgitem
->list
);
378 ipn_node
->totmsgcount
--;
379 ipn_msgpool_put(msgitem
->msg
,ipnn
);
380 kmem_cache_free(ipn_msgitem_cache
,msgitem
);
382 spin_unlock(&ipn_node
->msglock
);
385 /* flush pending oob messages (for socket close) */
386 /* LOCKING: ipnn_mutex is locked */
387 static void ipn_flush_oobrecvqueue(struct ipn_node
*ipn_node
)
389 struct ipn_network
*ipnn
=ipn_node
->ipn
;
390 spin_lock(&ipn_node
->msglock
);
391 while (!list_empty(&ipn_node
->oobmsgqueue
)) {
392 struct msgitem
*msgitem
=
393 list_first_entry(&ipn_node
->oobmsgqueue
, struct msgitem
, list
);
394 list_del(&msgitem
->list
);
395 ipn_node
->totmsgcount
--;
396 ipn_node
->oobmsgcount
--;
397 ipn_msgpool_put(msgitem
->msg
,ipnn
);
398 kmem_cache_free(ipn_msgitem_cache
,msgitem
);
400 spin_unlock(&ipn_node
->msglock
);
403 /* Terminate node. The node is "logically" terminated. */
404 /* LOCKING: ipn_glob_lock must be locked here */
405 static int ipn_terminate_node(struct ipn_node
*ipn_node
)
407 struct ipn_network
*ipnn
=ipn_node
->ipn
;
409 if (down_interruptible(&ipnn
->ipnn_mutex
))
411 if (ipn_node
->portno
>= 0) {
412 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_predelport(ipn_node
);
413 ipnn
->connport
[ipn_node
->portno
]=NULL
;
415 list_del(&ipn_node
->nodelist
);
416 ipn_flush_recvqueue(ipn_node
);
417 ipn_flush_oobrecvqueue(ipn_node
);
418 if (ipn_node
->portno
>= 0)
419 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_delport(ipn_node
);
421 ipn_net_update_counters(ipnn
,
422 (ipn_node
->shutdown
& RCV_SHUTDOWN
)?0:-1,
423 (ipn_node
->shutdown
& SEND_SHUTDOWN
)?0:-1);
424 ipn_node
->shutdown
= SHUTDOWN_XMASK
;
425 up(&ipnn
->ipnn_mutex
);
426 if (ipn_node
->netdev
)
427 ipn_netdev_close(ipn_node
);
428 /* No more network elements */
430 if (ipnn
->refcnt
== 0 && !ipn_is_persistent_chrdev(ipnn
))
433 ipn_deregister_chrdev(ipnn
);
434 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_delnet(ipnn
);
435 ipn_remove_network(ipnn
);
436 ipn_protocol_table
[ipnn
->protocol
]->refcnt
--;
441 if (ipnn
->msgpool_cache
)
442 ipn_msgbuf_put(ipnn
->msgpool_cache
);
444 kfree(ipnn
->connport
);
445 kmem_cache_free(ipn_network_cache
, ipnn
);
446 module_put(THIS_MODULE
);
450 kfree(ipn_node
->pbp
);
456 /* release of an ipn_node */
457 int ipn_node_release(struct ipn_node
*ipn_node
)
460 if (down_interruptible(&ipn_glob_mutex
))
462 if (ipn_node
->flags
& IPN_NODEFLAG_PERSIST
) {
463 ipn_node
->flags
&= ~IPN_NODEFLAG_INUSE
;
467 rv
=ipn_terminate_node(ipn_node
);
471 kmem_cache_free(ipn_node_cache
,ipn_node
);
477 /* release of an ipn socket */
478 static int ipn_release (struct socket
*sock
)
480 struct ipn_sock
*ipn_sk
=(struct ipn_sock
*)sock
->sk
;
481 struct ipn_node
*ipn_node
=ipn_sk
->node
;
482 int rv
=ipn_node_release(ipn_node
);
484 sock_put((struct sock
*) ipn_sk
);
488 /* _set persist, change the persistence of a node,
489 * when persistence gets cleared and the node is no longer used
490 * the node is terminated and freed.
491 * ipn_glob_mutex must be locked */
492 static int _ipn_setpersist(struct ipn_node
*ipn_node
, int persist
)
496 ipn_node
->flags
|= IPN_NODEFLAG_PERSIST
;
498 ipn_node
->flags
&= ~IPN_NODEFLAG_PERSIST
;
499 if (!(ipn_node
->flags
& IPN_NODEFLAG_INUSE
)) {
500 rv
=ipn_terminate_node(ipn_node
);
502 kmem_cache_free(ipn_node_cache
,ipn_node
);
509 * lock ipn_glob_mutex and call __ipn_setpersist above */
510 static int ipn_setpersist(struct ipn_node
*ipn_node
, int persist
)
513 if (ipn_node
->netdev
== NULL
)
515 if (down_interruptible(&ipn_glob_mutex
))
517 rv
=_ipn_setpersist(ipn_node
,persist
);
522 /* several network parameters can be set by setsockopt prior to bind */
523 /* struct pre_bind_parms is a temporary stucture connected to ipn_node->pbp
524 * to keep the parameter values. */
525 struct pre_bind_parms
{
526 unsigned short maxports
;
527 unsigned short flags
;
528 unsigned short msgpoolsize
;
533 /* STD_PARMS: BITS_PER_LONG nodes, no flags, BITS_PER_BYTE pending msgs,
534 * Ethernet + VLAN MTU*/
535 #define STD_BIND_PARMS {BITS_PER_LONG, 0, BITS_PER_BYTE, 1514, 0777};
537 static int ipn_mkname(struct sockaddr_un
* sunaddr
, int len
)
539 if (len
<= sizeof(short) || len
> sizeof(*sunaddr
))
541 if (!sunaddr
|| sunaddr
->sun_family
!= AF_IPN
)
544 * This may look like an off by one error but it is a bit more
545 * subtle. 108 is the longest valid AF_IPN path for a binding.
546 * sun_path[108] doesnt as such exist. However in kernel space
547 * we are guaranteed that it is a valid memory location in our
548 * kernel address buffer.
550 ((char *)sunaddr
)[len
]=0;
551 len
= strlen(sunaddr
->sun_path
)+1+sizeof(short);
555 static int ipn_node_bind(struct ipn_node
*ipn_node
, struct ipn_network
*ipnn
)
559 if (ipn_node
->ipn
!= NULL
)
562 list_add_tail(&ipn_node
->nodelist
,&ipnn
->unconnectqueue
);
564 ipn_node
->flags
|= IPN_NODEFLAG_BOUND
;
569 static int ipn_bind(struct socket
*sock
, struct sockaddr
*uaddr
, int addr_len
)
571 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)uaddr
;
572 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
574 struct ipn_network
*ipnn
;
575 struct dentry
* dentry
= NULL
;
577 struct pre_bind_parms parms
=STD_BIND_PARMS
;
579 //printk("IPN bind\n");
581 if (down_interruptible(&ipn_glob_mutex
))
583 if (sock
->state
!= SS_UNCONNECTED
||
584 ipn_node
->ipn
!= NULL
) {
589 if (ipn_node
->protocol
>= 0 &&
590 (ipn_node
->protocol
>= IPN_MAX_PROTO
||
591 ipn_protocol_table
[ipn_node
->protocol
] == NULL
)) {
592 err
= -EPROTONOSUPPORT
;
596 addr_len
= ipn_mkname(sunaddr
, addr_len
);
602 /* check if there is already an ipn-network socket with that name */
603 err
= path_lookup(sunaddr
->sun_path
, LOOKUP_FOLLOW
, &nd
);
604 if (err
) { /* it does not exist, NEW IPN socket! */
606 /* Is it everything okay with the parent? */
607 err
= path_lookup(sunaddr
->sun_path
, LOOKUP_PARENT
, &nd
);
609 goto out_mknod_parent
;
610 /* Do I have the permission to create a file? */
611 dentry
= lookup_create(&nd
, 0);
612 err
= PTR_ERR(dentry
);
614 goto out_mknod_unlock
;
616 * All right, let's create it.
619 mode
= ipn_node
->pbp
->mode
;
621 mode
= SOCK_INODE(sock
)->i_mode
;
622 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 30)
623 mode
= S_IFSOCK
| (mode
& ~current
->fs
->umask
);
625 mode
= S_IFSOCK
| (mode
& ~current_umask());
629 err
= vfs_mknod(nd
.path
.dentry
->d_inode
, dentry
, nd
.path
.mnt
, mode
, 0);
631 err
= vfs_mknod(nd
.path
.dentry
->d_inode
, dentry
, mode
, 0);
635 err
= vfs_mknod(nd
.dentry
->d_inode
, dentry
, nd
.path
.mnt
, mode
, 0);
637 err
= vfs_mknod(nd
.dentry
->d_inode
, dentry
, mode
, 0);
643 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
644 dput(nd
.path
.dentry
);
645 nd
.path
.dentry
= dentry
;
647 mutex_unlock(&nd
.dentry
->d_inode
->i_mutex
);
651 /* create a new ipn_network item */
653 parms
=*ipn_node
->pbp
;
654 ipnn
=kmem_cache_zalloc(ipn_network_cache
,GFP_KERNEL
);
657 goto out_mknod_dput_ipnn
;
659 ipnn
->connport
=kzalloc(parms
.maxports
* sizeof(struct ipn_node
*),GFP_KERNEL
);
660 if (!ipnn
->connport
) {
662 goto out_mknod_dput_ipnn2
;
665 /* module refcnt is incremented for each network, thus
666 * rmmod is forbidden if there are persistent nodes */
667 if (!try_module_get(THIS_MODULE
)) {
669 goto out_mknod_dput_ipnn2
;
671 memcpy(&ipnn
->sunaddr
,sunaddr
,addr_len
);
673 ipnn
->flags
=parms
.flags
;
674 if (ipnn
->flags
& IPN_FLAG_FLEXMTU
)
675 ipnn
->msgpool_cache
= NULL
;
677 ipnn
->msgpool_cache
=ipn_msgbuf_get(ipnn
->mtu
);
678 if (!ipnn
->msgpool_cache
) {
680 goto out_mknod_dput_putmodule
;
683 INIT_LIST_HEAD(&ipnn
->unconnectqueue
);
684 INIT_LIST_HEAD(&ipnn
->connectqueue
);
687 ipnn
->dentry
=nd
.path
.dentry
;
688 ipnn
->mnt
=nd
.path
.mnt
;
690 ipnn
->dentry
=nd
.dentry
;
693 init_MUTEX(&ipnn
->ipnn_mutex
);
694 ipnn
->sunaddr_len
=addr_len
;
695 ipnn
->protocol
=ipn_node
->protocol
;
696 if (ipnn
->protocol
< 0) ipnn
->protocol
= 0;
697 ipn_protocol_table
[ipnn
->protocol
]->refcnt
++;
700 ipnn
->maxports
=parms
.maxports
;
701 atomic_set(&ipnn
->msgpool_nelem
,0);
702 ipnn
->msgpool_size
=parms
.msgpoolsize
;
703 ipnn
->proto_private
=NULL
;
704 init_waitqueue_head(&ipnn
->send_wait
);
706 err
=ipn_protocol_table
[ipnn
->protocol
]->ipn_p_newnet(ipnn
);
708 goto out_mknod_dput_putmodule
;
710 ipn_insert_network(&ipn_network_table
[nd
.path
.dentry
->d_inode
->i_ino
& (IPN_HASH_SIZE
-1)],ipnn
);
712 ipn_insert_network(&ipn_network_table
[nd
.dentry
->d_inode
->i_ino
& (IPN_HASH_SIZE
-1)],ipnn
);
715 /* join an existing network */
716 if (parms
.flags
& IPN_FLAG_EXCL
) {
720 err
= inode_permission(nd
.path
.dentry
->d_inode
, MAY_EXEC
);
725 if (!S_ISSOCK(nd
.path
.dentry
->d_inode
->i_mode
))
727 ipnn
=ipn_find_network_byinode(nd
.path
.dentry
->d_inode
);
729 if (!S_ISSOCK(nd
.dentry
->d_inode
->i_mode
))
731 ipnn
=ipn_find_network_byinode(nd
.dentry
->d_inode
);
733 if (!ipnn
|| (ipnn
->flags
& IPN_FLAG_TERMINATED
) ||
734 (ipnn
->flags
& IPN_FLAG_EXCL
))
738 kfree(ipn_node
->pbp
);
741 ipn_node_bind(ipn_node
,ipnn
);
755 out_mknod_dput_putmodule
:
756 module_put(THIS_MODULE
);
757 out_mknod_dput_ipnn2
:
758 kfree(ipnn
->connport
);
760 kmem_cache_free(ipn_network_cache
,ipnn
);
765 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
768 mutex_unlock(&nd
.dentry
->d_inode
->i_mutex
);
779 static int ipn_connect(struct socket
*sock
, struct sockaddr
*addr
,
780 int addr_len
, int flags
){
781 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)addr
;
782 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
784 struct ipn_network
*ipnn
,*previousipnn
;
788 /* the socket cannot be connected twice */
789 if (sock
->state
!= SS_UNCONNECTED
)
792 if (down_interruptible(&ipn_glob_mutex
))
795 if ((previousipnn
=ipn_node
->ipn
) == NULL
) { /* unbound */
796 unsigned char mustshutdown
=0;
797 err
= ipn_mkname(sunaddr
, addr_len
);
801 err
= path_lookup(sunaddr
->sun_path
, LOOKUP_FOLLOW
, &nd
);
804 err
= inode_permission(nd
.path
.dentry
->d_inode
, MAY_READ
);
806 if (err
== -EACCES
|| err
== -EROFS
)
807 mustshutdown
|=RCV_SHUTDOWN
;
811 err
= inode_permission(nd
.path
.dentry
->d_inode
, MAY_WRITE
);
814 mustshutdown
|=SEND_SHUTDOWN
;
818 mustshutdown
|= ipn_node
->shutdown
;
819 /* if the combination of shutdown and permissions leaves
820 * no abilities, connect returns EACCES */
821 if (mustshutdown
== SHUTDOWN_XMASK
) {
826 ipn_node
->shutdown
=mustshutdown
;
829 if (!S_ISSOCK(nd
.path
.dentry
->d_inode
->i_mode
)) {
833 ipnn
=ipn_find_network_byinode(nd
.path
.dentry
->d_inode
);
835 if (!S_ISSOCK(nd
.dentry
->d_inode
->i_mode
)) {
839 ipnn
=ipn_find_network_byinode(nd
.dentry
->d_inode
);
841 if (!ipnn
|| (ipnn
->flags
& IPN_FLAG_TERMINATED
)) {
845 if (ipn_node
->protocol
== IPN_ANY
)
846 ipn_node
->protocol
=ipnn
->protocol
;
847 else if (ipnn
->protocol
!= ipn_node
->protocol
) {
860 if (down_interruptible(&ipnn
->ipnn_mutex
)) {
864 portno
= ipn_protocol_table
[ipnn
->protocol
]->ipn_p_newport(ipn_node
);
865 if (portno
>= 0 && portno
<ipnn
->maxports
) {
866 sock
->state
= SS_CONNECTED
;
867 ipn_node
->portno
=portno
;
868 ipnn
->connport
[portno
]=ipn_node
;
869 if (!(ipn_node
->flags
& IPN_NODEFLAG_BOUND
))
872 list_del(&ipn_node
->nodelist
);
873 list_add_tail(&ipn_node
->nodelist
,&ipnn
->connectqueue
);
874 ipn_net_update_counters(ipnn
,
875 (ipn_node
->shutdown
& RCV_SHUTDOWN
)?0:1,
876 (ipn_node
->shutdown
& SEND_SHUTDOWN
)?0:1);
878 ipn_node
->ipn
=previousipnn
; /* undo changes on ipn_node->ipn */
881 up(&ipnn
->ipnn_mutex
);
896 int ipn_node_create_connect(struct ipn_node
**ipn_node_out
,
897 struct ipn_network
*(* ipnn_map
)(void *),void *ipnn_map_arg
) {
898 struct ipn_node
*ipn_node
;
899 struct ipn_network
*ipnn
;
902 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
903 ipn_node
=ipn_node_create(NULL
);
905 ipn_node
=ipn_node_create(&init_net
);
907 if (down_interruptible(&ipn_glob_mutex
)) {
909 goto err_ipn_node_release
;
911 ipnn
=ipnn_map(ipnn_map_arg
);
913 ipn_node
->protocol
=ipnn
->protocol
;
914 if (down_interruptible(&ipnn
->ipnn_mutex
)) {
918 portno
= ipn_protocol_table
[ipnn
->protocol
]->ipn_p_newport(ipn_node
);
919 if (portno
>= 0 && portno
<ipnn
->maxports
) {
920 ipn_node
->portno
=portno
;
921 ipnn
->connport
[portno
]=ipn_node
;
922 if (!(ipn_node
->flags
& IPN_NODEFLAG_BOUND
)) {
924 list_del(&ipn_node
->nodelist
);
926 *ipn_node_out
=ipn_node
;
927 list_add_tail(&ipn_node
->nodelist
,&ipnn
->connectqueue
);
928 ipn_net_update_counters(ipnn
,
929 (ipn_node
->shutdown
& RCV_SHUTDOWN
)?0:1,
930 (ipn_node
->shutdown
& SEND_SHUTDOWN
)?0:1);
937 up(&ipnn
->ipnn_mutex
);
941 up(&ipnn
->ipnn_mutex
);
944 err_ipn_node_release
:
945 ipn_node_release(ipn_node
);
950 int ipn_node_connect(struct ipn_node
*ipn_node
)
952 struct ipn_network
*ipnn
;
955 if (down_interruptible(&ipn_glob_mutex
))
959 if (down_interruptible(&ipnn
->ipnn_mutex
)) {
963 portno
= ipn_protocol_table
[ipnn
->protocol
]->ipn_p_newport(ipn_node
);
964 if (portno
>= 0 && portno
<ipnn
->maxports
) {
965 ipn_node
->portno
=portno
;
966 ipnn
->connport
[portno
]=ipn_node
;
967 if (!(ipn_node
->flags
& IPN_NODEFLAG_BOUND
)) {
969 list_del(&ipn_node
->nodelist
);
971 list_add_tail(&ipn_node
->nodelist
,&ipnn
->connectqueue
);
972 ipn_net_update_counters(ipnn
,
973 (ipn_node
->shutdown
& RCV_SHUTDOWN
)?0:1,
974 (ipn_node
->shutdown
& SEND_SHUTDOWN
)?0:1);
980 up(&ipnn
->ipnn_mutex
);
988 static int ipn_getname(struct socket
*sock
, struct sockaddr
*uaddr
,
989 int *uaddr_len
, int peer
) {
990 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
991 struct ipn_network
*ipnn
=ipn_node
->ipn
;
992 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)uaddr
;
995 if (down_interruptible(&ipn_glob_mutex
))
998 *uaddr_len
= ipnn
->sunaddr_len
;
999 memcpy(sunaddr
,&ipnn
->sunaddr
,*uaddr_len
);
1002 up(&ipn_glob_mutex
);
1007 unsigned int ipn_node_poll(struct ipn_node
*ipn_node
, struct file
*file
, poll_table
*wait
) {
1008 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1009 unsigned int mask
=0;
1012 poll_wait(file
,&ipn_node
->read_wait
,wait
);
1013 if (ipnn
->flags
& IPN_FLAG_LOSSLESS
)
1014 poll_wait(file
,&ipnn
->send_wait
,wait
);
1015 /* POLLIN if recv succeeds,
1016 * POLL{PRI,RDNORM} if there are {oob,non-oob} messages */
1017 if (ipn_node
->totmsgcount
> 0) mask
|= POLLIN
;
1018 if (!(list_empty(&ipn_node
->msgqueue
))) mask
|= POLLRDNORM
;
1019 if (!(list_empty(&ipn_node
->oobmsgqueue
))) mask
|= POLLPRI
;
1020 if ((!(ipnn
->flags
& IPN_FLAG_LOSSLESS
)) |
1021 (atomic_read(&ipnn
->msgpool_nelem
) < ipnn
->msgpool_size
))
1022 mask
|= POLLOUT
| POLLWRNORM
;
1027 static unsigned int ipn_poll(struct file
*file
, struct socket
*sock
,
1029 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1030 return ipn_node_poll(ipn_node
, file
, wait
);
1033 /* connect netdev (from ioctl). connect a bound socket to a
1034 * network device TAP or GRAB */
1035 static int ipn_connect_netdev(struct socket
*sock
,struct ifreq
*ifr
)
1038 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1039 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1040 if (!capable(CAP_NET_ADMIN
))
1042 if (sock
->state
!= SS_UNCONNECTED
)
1045 return -ENOTCONN
; /* Maybe we need a different error for "NOT BOUND" */
1046 if (down_interruptible(&ipn_glob_mutex
))
1047 return -ERESTARTSYS
;
1048 if (down_interruptible(&ipnn
->ipnn_mutex
)) {
1049 up(&ipn_glob_mutex
);
1050 return -ERESTARTSYS
;
1052 ipn_node
->netdev
=ipn_netdev_alloc(ipn_node
->net
,ifr
->ifr_flags
,ifr
->ifr_name
,&err
);
1053 if (ipn_node
->netdev
) {
1055 portno
= ipn_protocol_table
[ipnn
->protocol
]->ipn_p_newport(ipn_node
);
1056 if (portno
>= 0 && portno
<ipnn
->maxports
) {
1057 sock
->state
= SS_CONNECTED
;
1058 ipn_node
->portno
=portno
;
1059 ipn_node
->flags
|= ifr
->ifr_flags
& IPN_NODEFLAG_DEVMASK
;
1060 ipnn
->connport
[portno
]=ipn_node
;
1061 err
=ipn_netdev_activate(ipn_node
);
1063 sock
->state
= SS_UNCONNECTED
;
1064 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_delport(ipn_node
);
1065 ipn_node
->netdev
=NULL
;
1066 ipn_node
->portno
= -1;
1067 ipn_node
->flags
&= ~IPN_NODEFLAG_DEVMASK
;
1068 ipnn
->connport
[portno
]=NULL
;
1070 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_postnewport(ipn_node
);
1071 list_del(&ipn_node
->nodelist
);
1072 list_add_tail(&ipn_node
->nodelist
,&ipnn
->connectqueue
);
1075 ipn_netdev_close(ipn_node
);
1077 ipn_node
->netdev
=NULL
;
1081 up(&ipnn
->ipnn_mutex
);
1082 up(&ipn_glob_mutex
);
1086 /* join a netdev, a socket gets connected to a persistent node
1087 * not connected to another socket */
1088 static int ipn_join_netdev(struct socket
*sock
,struct ifreq
*ifr
)
1091 struct net_device
*dev
;
1092 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1093 struct ipn_node
*ipn_joined
;
1094 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1095 if (sock
->state
!= SS_UNCONNECTED
)
1097 if (down_interruptible(&ipn_glob_mutex
))
1098 return -ERESTARTSYS
;
1099 if (down_interruptible(&ipnn
->ipnn_mutex
)) {
1100 up(&ipn_glob_mutex
);
1101 return -ERESTARTSYS
;
1103 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
1104 dev
=__dev_get_by_name(ifr
->ifr_name
);
1106 dev
=__dev_get_by_name(ipn_node
->net
,ifr
->ifr_name
);
1109 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
1110 dev
=__dev_get_by_index(ifr
->ifr_ifindex
);
1112 dev
=__dev_get_by_index(ipn_node
->net
,ifr
->ifr_ifindex
);
1114 if (dev
&& (ipn_joined
=ipn_netdev2node(dev
)) != NULL
) { /* the interface does exist */
1116 for (i
=0;i
<ipnn
->maxports
&& ipn_joined
!= ipnn
->connport
[i
] ;i
++)
1118 if (i
< ipnn
->maxports
) { /* found */
1119 /* ipn_joined is substituted to ipn_node */
1120 ((struct ipn_sock
*)sock
->sk
)->node
=ipn_joined
;
1121 ipn_joined
->flags
|= IPN_NODEFLAG_INUSE
;
1123 kmem_cache_free(ipn_node_cache
,ipn_node
);
1128 up(&ipnn
->ipnn_mutex
);
1129 up(&ipn_glob_mutex
);
1133 /* set persistence of a node looking for it by interface name
1134 * (it is for sysadm, to close network interfaces)*/
1135 static int ipn_setpersist_netdev(struct ifreq
*ifr
, int value
)
1137 struct net_device
*dev
;
1138 struct ipn_node
*ipn_node
;
1140 if (!capable(CAP_NET_ADMIN
))
1142 if (down_interruptible(&ipn_glob_mutex
))
1143 return -ERESTARTSYS
;
1144 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
1145 dev
=__dev_get_by_name(ifr
->ifr_name
);
1147 dev
=__dev_get_by_name(&init_net
,ifr
->ifr_name
);
1150 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
1151 dev
=__dev_get_by_index(ifr
->ifr_ifindex
);
1153 dev
=__dev_get_by_index(&init_net
,ifr
->ifr_ifindex
);
1155 if (dev
&& (ipn_node
=ipn_netdev2node(dev
)) != NULL
)
1156 _ipn_setpersist(ipn_node
,value
);
1159 up(&ipn_glob_mutex
);
1165 int ipn_node_ioctl(struct ipn_node
*ipn_node
, unsigned int cmd
, unsigned long arg
) {
1166 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1168 (ipn_protocol_table
[ipn_node
->protocol
]->ipn_p_ioctl
!= NULL
)) {
1170 if (down_interruptible(&ipnn
->ipnn_mutex
))
1171 return -ERESTARTSYS
;
1172 rv
=ipn_protocol_table
[ipn_node
->protocol
]->ipn_p_ioctl(ipn_node
,cmd
,arg
);
1173 up(&ipnn
->ipnn_mutex
);
1179 static int ipn_ioctl(struct socket
*sock
, unsigned int cmd
, unsigned long arg
) {
1180 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1181 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1182 void __user
* argp
= (void __user
*)arg
;
1184 struct chrdevreq devr
;
1186 if (ipn_node
->shutdown
== SHUTDOWN_XMASK
)
1193 case IPN_SETPERSIST_NETDEV
:
1194 case IPN_CLRPERSIST_NETDEV
:
1195 case IPN_CONN_NETDEV
:
1196 case IPN_JOIN_NETDEV
:
1198 if (copy_from_user(&ifr
, argp
, sizeof(ifr
)))
1200 ifr
.ifr_name
[IFNAMSIZ
-1] = '\0';
1202 case IPN_REGISTER_CHRDEV
:
1203 case IPN_JOIN_CHRDEV
:
1204 if (copy_from_user(&devr
, argp
, sizeof(devr
)))
1206 /*printk("IPN_REGISTER_CHRDEV %p %d %d %d\n",
1207 argp, sizeof(devr),devr.major, devr.minor);*/
1211 /* actions for unconnected and unbound sockets */
1213 case IPN_SETPERSIST_NETDEV
:
1214 return ipn_setpersist_netdev(&ifr
,1);
1215 case IPN_CLRPERSIST_NETDEV
:
1216 return ipn_setpersist_netdev(&ifr
,0);
1217 case IPN_JOIN_CHRDEV
:
1220 if (!capable(CAP_MKNOD
))
1222 if (ipn_node
->ipn
!= NULL
)
1224 if (down_interruptible(&ipn_glob_mutex
))
1225 return -ERESTARTSYS
;
1226 rv
=ipn_node_bind(ipn_node
, ipn_find_chrdev(&devr
));
1227 up(&ipn_glob_mutex
);
1231 if (capable(CAP_NET_ADMIN
))
1233 if (ipn_node
->netdev
&& (ipn_node
->flags
&IPN_NODEFLAG_TAP
))
1234 return dev_set_mac_address(ipn_node
->netdev
, &ifr
.ifr_hwaddr
);
1236 return -EADDRNOTAVAIL
;
1238 if (ipnn
== NULL
|| (ipnn
->flags
& IPN_FLAG_TERMINATED
))
1240 /* actions for connected or bound sockets */
1242 case IPN_CONN_NETDEV
:
1243 return ipn_connect_netdev(sock
,&ifr
);
1244 case IPN_JOIN_NETDEV
:
1245 return ipn_join_netdev(sock
,&ifr
);
1246 case IPN_SETPERSIST
:
1247 return ipn_setpersist(ipn_node
,arg
);
1248 case IPN_CHRDEV_PERSIST
:
1249 return ipn_chrdev_persistence(ipnn
,arg
);
1250 case IPN_REGISTER_CHRDEV
:
1253 unsigned int reqmajor
=devr
.major
;
1254 if (down_interruptible(&ipnn
->ipnn_mutex
))
1255 return -ERESTARTSYS
;
1256 rv
=ipn_register_chrdev(ipnn
,&devr
);
1257 if (reqmajor
==0 && rv
==0) {
1258 if (copy_to_user(argp
, &devr
, sizeof devr
))
1261 up(&ipnn
->ipnn_mutex
);
1264 case IPN_UNREGISTER_CHRDEV
:
1267 if (down_interruptible(&ipnn
->ipnn_mutex
))
1268 return -ERESTARTSYS
;
1269 rv
=ipn_deregister_chrdev(ipnn
);
1270 up(&ipnn
->ipnn_mutex
);
1274 return ipn_node_ioctl(ipn_node
, cmd
, arg
);
1278 /* shutdown: close socket for input or for output.
1279 * shutdown can be called prior to connect and it is not reversible */
1280 static int ipn_shutdown(struct socket
*sock
, int mode
) {
1281 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1282 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1283 int oldshutdown
=ipn_node
->shutdown
;
1284 mode
= (mode
+1)&(RCV_SHUTDOWN
|SEND_SHUTDOWN
);
1286 /* define the new mode first... */
1287 ipn_node
->shutdown
|= mode
;
1290 /* ... and wait for all pending ops to be completed */
1291 if (down_interruptible(&ipnn
->ipnn_mutex
)) {
1292 ipn_node
->shutdown
= oldshutdown
;
1293 return -ERESTARTSYS
;
1295 oldshutdown
=ipn_node
->shutdown
-oldshutdown
;
1296 if (sock
->state
== SS_CONNECTED
&& oldshutdown
) {
1297 ipn_net_update_counters(ipnn
,
1298 (ipn_node
->shutdown
& RCV_SHUTDOWN
)?0:-1,
1299 (ipn_node
->shutdown
& SEND_SHUTDOWN
)?0:-1);
1302 /* if recv channel has been shut down, flush the recv queue */
1303 if ((ipn_node
->shutdown
& RCV_SHUTDOWN
))
1304 ipn_flush_recvqueue(ipn_node
);
1305 up(&ipnn
->ipnn_mutex
);
1310 /* injectmsg: a new message is entering the ipn network.
1311 * injectmsg gets called by send and by the grab/tap node */
1312 /* handlemsg is protected from ipn_network changes: ipnn->ipnn_mutex is locked */
1313 int ipn_proto_injectmsg(struct ipn_node
*from
, struct msgpool_item
*msg
)
1315 struct ipn_network
*ipnn
=from
->ipn
;
1317 if (down_interruptible(&ipnn
->ipnn_mutex
))
1320 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_handlemsg(from
, msg
);
1321 up(&ipnn
->ipnn_mutex
);
1327 int ipn_node_write(struct ipn_node
*ipn_node
, struct iovec
*msg_iov
, int len
) {
1328 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1329 struct msgpool_item
*newmsg
;
1332 if (unlikely(ipn_node
->shutdown
& SEND_SHUTDOWN
)) {
1333 if (ipn_node
->shutdown
== SHUTDOWN_XMASK
)
1338 if (len
> ipnn
->mtu
)
1340 newmsg
=ipn_msgpool_alloc_locking(ipnn
,len
);
1344 err
=memcpy_fromiovec(newmsg
->data
, msg_iov
, len
);
1346 ipn_proto_injectmsg(ipn_node
, newmsg
);
1347 ipn_msgpool_put(newmsg
,ipnn
);
1351 static int ipn_sendmsg(struct kiocb
*kiocb
, struct socket
*sock
,
1352 struct msghdr
*msg
, size_t len
) {
1353 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1355 if (unlikely(sock
->state
!= SS_CONNECTED
))
1358 return ipn_node_write(ipn_node
, msg
->msg_iov
, len
);
1361 /* ipn_proto_sendmsg is called by protocol implementation to enqueue a
1362 * for a destination (to).*/
1363 /* enqueue a msgitem on the receiver's queue, the queue is protected by a
1364 * spinlock (msglock) */
1365 void ipn_proto_sendmsg(struct ipn_node
*to
, struct msgpool_item
*msg
)
1369 ipn_netdev_sendmsg(to
,msg
);
1372 struct msgitem
*msgitem
;
1373 struct ipn_network
*ipnn
=to
->ipn
;
1374 spin_lock(&to
->msglock
);
1375 if (likely((to
->shutdown
& RCV_SHUTDOWN
)==0)) {
1376 /*if (unlikely((ipnn->flags & IPN_FLAG_LOSSLESS) == 0 ||
1377 to->totmsgcount >= ipnn->msgpool_size))
1379 if (likely(ipnn
->flags
& IPN_FLAG_LOSSLESS
||
1380 to
->totmsgcount
< ipnn
->msgpool_size
)) {
1381 if ((msgitem
=kmem_cache_alloc(ipn_msgitem_cache
,GFP_KERNEL
))!=NULL
) {
1384 list_add_tail(&msgitem
->list
, &to
->msgqueue
);
1385 ipn_msgpool_hold(msg
);
1389 spin_unlock(&to
->msglock
);
1390 wake_up_interruptible(&to
->read_wait
);
1395 /* enqueue an oob message. "to" is the destination */
1396 void ipn_proto_oobsendmsg(struct ipn_node
*to
, struct msgpool_item
*msg
)
1399 if (!to
->netdev
) { /* no oob to netdev */
1400 struct msgitem
*msgitem
;
1401 struct ipn_network
*ipnn
=to
->ipn
;
1402 spin_lock(&to
->msglock
);
1403 if ((to
->shutdown
& RCV_SHUTDOWN_NO_OOB
) == 0 &&
1404 (ipnn
->flags
& IPN_FLAG_LOSSLESS
||
1405 to
->oobmsgcount
< ipnn
->msgpool_size
)) {
1406 if ((msgitem
=kmem_cache_alloc(ipn_msgitem_cache
,GFP_KERNEL
))!=NULL
) {
1410 list_add_tail(&msgitem
->list
, &to
->oobmsgqueue
);
1411 ipn_msgpool_hold(msg
);
1414 spin_unlock(&to
->msglock
);
1415 wake_up_interruptible(&to
->read_wait
);
1421 int ipn_node_read(struct ipn_node
*ipn_node
, struct iovec
*msg_iov
, size_t len
, int *msg_flags
, int flags
) {
1422 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1423 struct msgitem
*msgitem
;
1424 struct msgpool_item
*currmsg
;
1426 if (unlikely((ipn_node
->shutdown
& XRCV_SHUTDOWN
) == XRCV_SHUTDOWN
)) {
1427 if (ipn_node
->shutdown
== SHUTDOWN_XMASK
) /*EOF, nothing can be read*/
1430 return -EPIPE
; /*trying to read on a write only node */
1433 /* wait for a message */
1434 spin_lock(&ipn_node
->msglock
);
1435 while (ipn_node
->totmsgcount
== 0) {
1436 spin_unlock(&ipn_node
->msglock
);
1437 if (wait_event_interruptible(ipn_node
->read_wait
,
1438 !(ipn_node
->totmsgcount
== 0)))
1439 return -ERESTARTSYS
;
1440 spin_lock(&ipn_node
->msglock
);
1442 /* oob gets delivered first. oob are rare */
1443 if (likely(list_empty(&ipn_node
->oobmsgqueue
)))
1444 msgitem
=list_first_entry(&ipn_node
->msgqueue
, struct msgitem
, list
);
1446 msgitem
=list_first_entry(&ipn_node
->oobmsgqueue
, struct msgitem
, list
);
1447 *msg_flags
|= MSG_OOB
;
1448 ipn_node
->oobmsgcount
--;
1450 list_del(&msgitem
->list
);
1451 ipn_node
->totmsgcount
--;
1452 spin_unlock(&ipn_node
->msglock
);
1453 currmsg
=msgitem
->msg
;
1454 if (currmsg
->len
< len
)
1456 memcpy_toiovec(msg_iov
, currmsg
->data
, len
);
1457 ipn_msgpool_put(currmsg
,ipnn
);
1458 kmem_cache_free(ipn_msgitem_cache
,msgitem
);
1462 static int ipn_recvmsg(struct kiocb
*kiocb
, struct socket
*sock
,
1463 struct msghdr
*msg
, size_t len
, int flags
) {
1464 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1466 if (unlikely(sock
->state
!= SS_CONNECTED
))
1469 return ipn_node_read(ipn_node
, msg
->msg_iov
, len
, &msg
->msg_flags
, flags
);
1472 /* resize a network: change the # of communication ports (connport) */
1473 static int ipn_netresize(struct ipn_network
*ipnn
,int newsize
)
1476 struct ipn_node
**newconnport
;
1477 struct ipn_node
**oldconnport
;
1479 if (down_interruptible(&ipnn
->ipnn_mutex
))
1480 return -ERESTARTSYS
;
1481 oldsize
=ipnn
->maxports
;
1482 if (newsize
== oldsize
) {
1483 up(&ipnn
->ipnn_mutex
);
1487 /* shrink a network. all the ports we are going to eliminate
1488 * must be unused! */
1489 if (newsize
< oldsize
) {
1491 for (i
=newsize
; i
<oldsize
; i
++)
1492 if (ipnn
->connport
[i
]) {
1493 up(&ipnn
->ipnn_mutex
);
1498 oldconnport
=ipnn
->connport
;
1499 /* allocate the new connport array and copy the old one */
1500 newconnport
=kzalloc(newsize
* sizeof(struct ipn_node
*),GFP_KERNEL
);
1502 up(&ipnn
->ipnn_mutex
);
1505 memcpy(newconnport
,oldconnport
,min
* sizeof(struct ipn_node
*));
1506 ipnn
->connport
=newconnport
;
1507 ipnn
->maxports
=newsize
;
1508 /* notify the protocol that the netowrk has been resized */
1509 err
=ipn_protocol_table
[ipnn
->protocol
]->ipn_p_resizenet(ipnn
,oldsize
,newsize
);
1511 /* roll back if the resize operation failed for the protocol */
1512 ipnn
->connport
=oldconnport
;
1513 ipnn
->maxports
=oldsize
;
1516 /* successful mission, network resized */
1518 up(&ipnn
->ipnn_mutex
);
1522 /* IPN SETSOCKOPT */
1524 static int ipn_setsockopt(struct socket
*sock
, int level
, int optname
,
1525 char __user
*optval
, unsigned int optlen
)
1527 static int ipn_setsockopt(struct socket
*sock
, int level
, int optname
,
1528 char __user
*optval
, int optlen
)
1531 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1532 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1534 if (ipn_node
->shutdown
== SHUTDOWN_XMASK
)
1536 if (level
!= 0 && level
!= ipn_node
->protocol
+1)
1537 return -EPROTONOSUPPORT
;
1539 /* protocol specific sockopt */
1542 if (down_interruptible(&ipnn
->ipnn_mutex
))
1543 return -ERESTARTSYS
;
1544 rv
=ipn_protocol_table
[ipn_node
->protocol
]->ipn_p_setsockopt(ipn_node
,optname
,optval
,optlen
);
1545 up(&ipnn
->ipnn_mutex
);
1550 if (optname
== IPN_SO_DESCR
) {
1551 if (optlen
> IPN_DESCRLEN
)
1554 memset(ipn_node
->descr
,0,IPN_DESCRLEN
);
1555 if (copy_from_user(ipn_node
->descr
,optval
,optlen
))
1556 ipn_node
->descr
[0]=0;
1558 ipn_node
->descr
[optlen
-1]=0;
1562 if (optlen
< sizeof(int))
1564 else if ((optname
& IPN_SO_PREBIND
) && (ipnn
!= NULL
))
1568 get_user(val
, (int __user
*) optval
);
1569 if ((optname
& IPN_SO_PREBIND
) && !ipn_node
->pbp
) {
1570 struct pre_bind_parms std
=STD_BIND_PARMS
;
1571 ipn_node
->pbp
=kzalloc(sizeof(struct pre_bind_parms
),GFP_KERNEL
);
1574 *(ipn_node
->pbp
)=std
;
1578 if (sock
->state
== SS_UNCONNECTED
)
1579 ipn_node
->portno
=val
;
1583 case IPN_SO_CHANGE_NUMNODES
:
1584 if ((ipn_node
->flags
& IPN_NODEFLAG_BOUND
)!=0) {
1588 return ipn_netresize(ipnn
,val
);
1592 case IPN_SO_WANT_OOB_NUMNODES
:
1594 ipn_node
->flags
|= IPN_NODEFLAG_OOB_NUMNODES
;
1596 ipn_node
->flags
&= ~IPN_NODEFLAG_OOB_NUMNODES
;
1598 case IPN_SO_HANDLE_OOB
:
1600 ipn_node
->shutdown
&= ~RCV_SHUTDOWN_NO_OOB
;
1602 ipn_node
->shutdown
|= RCV_SHUTDOWN_NO_OOB
;
1608 ipn_node
->pbp
->mtu
=val
;
1610 case IPN_SO_NUMNODES
:
1614 ipn_node
->pbp
->maxports
=val
;
1616 case IPN_SO_MSGPOOLSIZE
:
1620 ipn_node
->pbp
->msgpoolsize
=val
;
1623 ipn_node
->pbp
->flags
=val
;
1626 ipn_node
->pbp
->mode
=val
;
1635 /* IPN GETSOCKOPT */
1636 static int ipn_getsockopt(struct socket
*sock
, int level
, int optname
,
1637 char __user
*optval
, int __user
*optlen
) {
1638 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1639 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1642 if (ipn_node
->shutdown
== SHUTDOWN_XMASK
)
1644 if (level
!= 0 && level
!= ipn_node
->protocol
+1)
1645 return -EPROTONOSUPPORT
;
1649 /* protocol specific sockopt */
1650 if (down_interruptible(&ipnn
->ipnn_mutex
))
1651 return -ERESTARTSYS
;
1652 rv
=ipn_protocol_table
[ipn_node
->protocol
]->ipn_p_getsockopt(ipn_node
,optname
,optval
,optlen
);
1653 up(&ipnn
->ipnn_mutex
);
1658 if (get_user(len
, optlen
))
1660 if (optname
== IPN_SO_DESCR
) {
1661 if (len
< IPN_DESCRLEN
)
1664 if (len
> IPN_DESCRLEN
)
1666 if(put_user(len
, optlen
))
1668 if(copy_to_user(optval
,ipn_node
->descr
,len
))
1676 val
=ipn_node
->portno
;
1681 else if (ipn_node
->pbp
)
1682 val
=ipn_node
->pbp
->mtu
;
1684 case IPN_SO_NUMNODES
:
1687 else if (ipn_node
->pbp
)
1688 val
=ipn_node
->pbp
->maxports
;
1690 case IPN_SO_MSGPOOLSIZE
:
1692 val
=ipnn
->msgpool_size
;
1693 else if (ipn_node
->pbp
)
1694 val
=ipn_node
->pbp
->msgpoolsize
;
1699 else if (ipn_node
->pbp
)
1700 val
=ipn_node
->pbp
->flags
;
1705 else if (ipn_node
->pbp
)
1706 val
=ipn_node
->pbp
->mode
;
1712 if (len
< sizeof(int))
1716 if(put_user(len
, optlen
))
1718 if(copy_to_user(optval
,&val
,len
))
1727 /* BROADCAST/HUB implementation */
1729 static int ipn_bcast_newport(struct ipn_node
*newport
) {
1730 struct ipn_network
*ipnn
=newport
->ipn
;
1732 for (i
=0;i
<ipnn
->maxports
;i
++) {
1733 if (ipnn
->connport
[i
] == NULL
)
1739 static int ipn_bcast_handlemsg(struct ipn_node
*from
,
1740 struct msgpool_item
*msgitem
){
1741 struct ipn_network
*ipnn
=from
->ipn
;
1743 struct ipn_node
*ipn_node
;
1744 list_for_each_entry(ipn_node
, &ipnn
->connectqueue
, nodelist
) {
1745 if (ipn_node
!= from
)
1746 ipn_proto_sendmsg(ipn_node
,msgitem
);
1751 static void ipn_null_delport(struct ipn_node
*oldport
) {}
1752 static void ipn_null_postnewport(struct ipn_node
*newport
) {}
1753 static void ipn_null_predelport(struct ipn_node
*oldport
) {}
1754 static int ipn_null_newnet(struct ipn_network
*newnet
) {return 0;}
1755 static int ipn_null_resizenet(struct ipn_network
*net
,int oldsize
,int newsize
) {
1757 static void ipn_null_delnet(struct ipn_network
*oldnet
) {}
1758 static int ipn_null_setsockopt(struct ipn_node
*port
,int optname
,
1759 char __user
*optval
, int optlen
) {return -EOPNOTSUPP
;}
1760 static int ipn_null_getsockopt(struct ipn_node
*port
,int optname
,
1761 char __user
*optval
, int *optlen
) {return -EOPNOTSUPP
;}
1762 static int ipn_null_ioctl(struct ipn_node
*port
,unsigned int request
,
1763 unsigned long arg
) {return -EOPNOTSUPP
;}
1765 /* Protocol Registration/deregisteration */
1767 void ipn_init_protocol(struct ipn_protocol
*p
)
1769 if (p
->ipn_p_delport
== NULL
) p
->ipn_p_delport
=ipn_null_delport
;
1770 if (p
->ipn_p_postnewport
== NULL
) p
->ipn_p_postnewport
=ipn_null_postnewport
;
1771 if (p
->ipn_p_predelport
== NULL
) p
->ipn_p_predelport
=ipn_null_predelport
;
1772 if (p
->ipn_p_newnet
== NULL
) p
->ipn_p_newnet
=ipn_null_newnet
;
1773 if (p
->ipn_p_resizenet
== NULL
) p
->ipn_p_resizenet
=ipn_null_resizenet
;
1774 if (p
->ipn_p_delnet
== NULL
) p
->ipn_p_delnet
=ipn_null_delnet
;
1775 if (p
->ipn_p_setsockopt
== NULL
) p
->ipn_p_setsockopt
=ipn_null_setsockopt
;
1776 if (p
->ipn_p_getsockopt
== NULL
) p
->ipn_p_getsockopt
=ipn_null_getsockopt
;
1777 if (p
->ipn_p_ioctl
== NULL
) p
->ipn_p_ioctl
=ipn_null_ioctl
;
1780 int ipn_proto_register(int protocol
,struct ipn_protocol
*ipn_service
)
1783 if (ipn_service
->ipn_p_newport
== NULL
||
1784 ipn_service
->ipn_p_handlemsg
== NULL
)
1786 ipn_init_protocol(ipn_service
);
1787 if (protocol
> 1 && protocol
<= IPN_MAX_PROTO
) {
1789 if (down_interruptible(&ipn_glob_mutex
))
1790 return -ERESTARTSYS
;
1791 if (ipn_protocol_table
[protocol
])
1794 ipn_service
->refcnt
=0;
1795 ipn_protocol_table
[protocol
]=ipn_service
;
1796 printk(KERN_INFO
"IPN: Registered protocol %d\n",protocol
+1);
1798 up(&ipn_glob_mutex
);
1804 int ipn_proto_deregister(int protocol
)
1807 if (protocol
> 1 && protocol
<= IPN_MAX_PROTO
) {
1809 if (down_interruptible(&ipn_glob_mutex
))
1810 return -ERESTARTSYS
;
1811 if (ipn_protocol_table
[protocol
]) {
1812 if (ipn_protocol_table
[protocol
]->refcnt
== 0) {
1813 ipn_protocol_table
[protocol
]=NULL
;
1814 printk(KERN_INFO
"IPN: Unregistered protocol %d\n",protocol
+1);
1819 up(&ipn_glob_mutex
);
1826 /* Module constructor/destructor */
1827 static struct net_proto_family ipn_family_ops
= {
1829 .create
= ipn_create
,
1830 .owner
= THIS_MODULE
,
1833 /* IPN constructor */
1834 static int ipn_init(void)
1838 ipn_init_protocol(&ipn_bcast
);
1839 ipn_network_cache
=kmem_cache_create("ipn_network",sizeof(struct ipn_network
),0,0,NULL
);
1840 if (!ipn_network_cache
) {
1841 printk(KERN_CRIT
"%s: Cannot create ipn_network SLAB cache!\n",
1847 ipn_node_cache
=kmem_cache_create("ipn_node",sizeof(struct ipn_node
),0,0,NULL
);
1848 if (!ipn_node_cache
) {
1849 printk(KERN_CRIT
"%s: Cannot create ipn_node SLAB cache!\n",
1855 ipn_msgitem_cache
=kmem_cache_create("ipn_msgitem",sizeof(struct msgitem
),0,0,NULL
);
1856 if (!ipn_msgitem_cache
) {
1857 printk(KERN_CRIT
"%s: Cannot create ipn_msgitem SLAB cache!\n",
1863 rc
=ipn_msgbuf_init();
1865 printk(KERN_CRIT
"%s: Cannot create ipn_msgbuf SLAB cache\n",
1867 goto out_net_node_msg
;
1870 rc
=proto_register(&ipn_proto
,1);
1872 printk(KERN_CRIT
"%s: Cannot register the protocol!\n",
1874 goto out_net_node_msg_msgbuf
;
1877 sock_register(&ipn_family_ops
);
1879 printk(KERN_INFO
"IPN: Virtual Square Project, University of Bologna 2007-09\n");
1882 out_net_node_msg_msgbuf
:
1885 kmem_cache_destroy(ipn_msgitem_cache
);
1887 kmem_cache_destroy(ipn_node_cache
);
1889 kmem_cache_destroy(ipn_network_cache
);
1894 /* IPN destructor */
1895 static void ipn_exit(void)
1898 if (ipn_msgitem_cache
)
1899 kmem_cache_destroy(ipn_msgitem_cache
);
1901 kmem_cache_destroy(ipn_node_cache
);
1902 if (ipn_network_cache
)
1903 kmem_cache_destroy(ipn_network_cache
);
1905 sock_unregister(PF_IPN
);
1906 proto_unregister(&ipn_proto
);
1907 printk(KERN_INFO
"IPN removed\n");
1910 module_init(ipn_init
);
1911 module_exit(ipn_exit
);
1913 EXPORT_SYMBOL_GPL(ipn_proto_register
);
1914 EXPORT_SYMBOL_GPL(ipn_proto_deregister
);
1915 EXPORT_SYMBOL_GPL(ipn_proto_sendmsg
);
1916 EXPORT_SYMBOL_GPL(ipn_proto_oobsendmsg
);
1917 EXPORT_SYMBOL_GPL(ipn_msgpool_alloc
);
1918 EXPORT_SYMBOL_GPL(ipn_msgpool_put
);