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"
38 MODULE_LICENSE("GPL");
39 MODULE_AUTHOR("VIEW-OS TEAM");
40 MODULE_DESCRIPTION("IPN Kernel Module");
42 #define IPN_MAX_PROTO 4
43 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25)
47 /*extension of RCV_SHUTDOWN defined in include/net/sock.h
48 * when the bit is set recv fails */
49 /* NO_OOB: do not send OOB */
50 #define RCV_SHUTDOWN_NO_OOB 4
51 /* EXTENDED MASK including OOB */
52 #define SHUTDOWN_XMASK (SHUTDOWN_MASK | RCV_SHUTDOWN_NO_OOB)
53 /* if XRCV_SHUTDOWN is all set recv fails */
54 #define XRCV_SHUTDOWN (RCV_SHUTDOWN | RCV_SHUTDOWN_NO_OOB)
56 /* Network table and hash */
57 struct hlist_head ipn_network_table
[IPN_HASH_SIZE
+ 1];
58 /* not needed. Now protected by ipn_glob_mutex
60 * DEFINE_SPINLOCK(ipn_table_lock);
62 static struct kmem_cache
*ipn_network_cache
;
63 static struct kmem_cache
*ipn_node_cache
;
64 static struct kmem_cache
*ipn_msgitem_cache
;
65 static DECLARE_MUTEX(ipn_glob_mutex
);
67 /* Protocol 1: HUB/Broadcast default protocol. Function Prototypes */
68 static int ipn_bcast_newport(struct ipn_node
*newport
);
69 static int ipn_bcast_handlemsg(struct ipn_node
*from
,
70 struct msgpool_item
*msgitem
);
72 /* default protocol IPN_BROADCAST (0) */
73 static struct ipn_protocol ipn_bcast
= {
75 .ipn_p_newport
=ipn_bcast_newport
,
76 .ipn_p_handlemsg
=ipn_bcast_handlemsg
};
78 static struct ipn_protocol
*ipn_protocol_table
[IPN_MAX_PROTO
]={&ipn_bcast
};
80 /* Socket call function prototypes */
81 static int ipn_release(struct socket
*);
82 static int ipn_bind(struct socket
*, struct sockaddr
*, int);
83 static int ipn_connect(struct socket
*, struct sockaddr
*,
84 int addr_len
, int flags
);
85 static int ipn_getname(struct socket
*, struct sockaddr
*, int *, int);
86 static unsigned int ipn_poll(struct file
*, struct socket
*, poll_table
*);
87 static int ipn_ioctl(struct socket
*, unsigned int, unsigned long);
88 static int ipn_shutdown(struct socket
*, int);
89 static int ipn_sendmsg(struct kiocb
*, struct socket
*,
90 struct msghdr
*, size_t);
91 static int ipn_recvmsg(struct kiocb
*, struct socket
*,
92 struct msghdr
*, size_t, int);
93 static int ipn_setsockopt(struct socket
*sock
, int level
, int optname
,
94 char __user
*optval
, int optlen
);
95 static int ipn_getsockopt(struct socket
*sock
, int level
, int optname
,
96 char __user
*optval
, int __user
*optlen
);
98 /* Network table Management
99 * inode->ipn_network hash table
100 * LOCKING: MUTEX ipn_glob_mutex must be LOCKED*/
101 static inline void ipn_insert_network(struct hlist_head
*list
, struct ipn_network
*ipnn
)
103 /* *IPNTL* spin_lock(&ipn_table_lock); */
104 hlist_add_head(&ipnn
->hnode
, list
);
105 /* *IPNTL* spin_unlock(&ipn_table_lock); */
108 static inline void ipn_remove_network(struct ipn_network
*ipnn
)
110 /* *IPNTL* spin_lock(&ipn_table_lock); */
111 hlist_del(&ipnn
->hnode
);
112 /* *IPNTL* spin_unlock(&ipn_table_lock); */
115 static struct ipn_network
*ipn_find_network_byinode(struct inode
*i
)
117 struct ipn_network
*ipnn
;
118 struct hlist_node
*node
;
120 /* *IPNTL* spin_lock(&ipn_table_lock);*/
121 hlist_for_each_entry(ipnn
, node
,
122 &ipn_network_table
[i
->i_ino
& (IPN_HASH_SIZE
- 1)], hnode
) {
123 struct dentry
*dentry
= ipnn
->dentry
;
125 if(ipnn
->refcnt
> 0 && dentry
&& dentry
->d_inode
== i
)
130 /* *IPNTL* spin_unlock(&ipn_table_lock); */
134 /* msgpool management
135 * msgpool_item are ipn_network dependent (each net has its own MTU)
136 * for each message sent there is one msgpool_item and many struct msgitem
137 * one for each receipient.
138 * msgitem are connected to the node's msgqueue or oobmsgqueue.
139 * when a message is delivered to a process the msgitem is deleted and
140 * the count of the msgpool_item is decreased.
141 * msgpool_item elements gets deleted automatically when count is 0*/
144 struct list_head list
;
145 struct msgpool_item
*msg
;
148 /* alloc a fresh msgpool item. count is set to 1.
151 * for each receipient
152 * enqueue messages to the process (using msgitem), ipn_msgpool_hold
154 * The message can be delivered concurrently. init count to 1 guarantees
155 * that it survives at least until is has been enqueued to all
157 static struct msgpool_item
*_ipn_msgpool_alloc(struct ipn_network
*ipnn
)
159 struct msgpool_item
*new;
160 if ((new=kmem_cache_alloc(ipnn
->msgpool_cache
,GFP_KERNEL
)) != NULL
) {
161 atomic_set(&new->count
,1);
162 atomic_inc(&ipnn
->msgpool_nelem
);
167 struct msgpool_item
*ipn_msgpool_alloc(struct ipn_network
*ipnn
,int leaky
)
169 if (leaky
&& (ipnn
->flags
& IPN_FLAG_LOSSLESS
) &&
170 atomic_read(&ipnn
->msgpool_nelem
) < ipnn
->msgpool_size
)
173 return _ipn_msgpool_alloc(ipnn
);
176 /* If the service il LOSSLESS, this msgpool call waits for an
177 * available msgpool item */
178 static struct msgpool_item
*ipn_msgpool_alloc_locking(struct ipn_network
*ipnn
)
180 if (ipnn
->flags
& IPN_FLAG_LOSSLESS
) {
181 while (atomic_read(&ipnn
->msgpool_nelem
) >= ipnn
->msgpool_size
) {
182 if (wait_event_interruptible_exclusive(ipnn
->send_wait
,
183 atomic_read(&ipnn
->msgpool_nelem
) < ipnn
->msgpool_size
))
187 return _ipn_msgpool_alloc(ipnn
);
190 static inline void ipn_msgpool_hold(struct msgpool_item
*msg
)
192 atomic_inc(&msg
->count
);
195 /* decrease count and delete msgpool_item if count == 0 */
196 void ipn_msgpool_put(struct msgpool_item
*old
,
197 struct ipn_network
*ipnn
)
199 if (atomic_dec_and_test(&old
->count
)) {
200 kmem_cache_free(ipnn
->msgpool_cache
,old
);
201 atomic_dec(&ipnn
->msgpool_nelem
);
202 if (ipnn
->flags
& IPN_FLAG_LOSSLESS
) /* this could be done anyway */
203 wake_up_interruptible(&ipnn
->send_wait
);
208 static const struct proto_ops ipn_ops
= {
210 .owner
= THIS_MODULE
,
211 .release
= ipn_release
,
213 .connect
= ipn_connect
,
214 .socketpair
= sock_no_socketpair
,
215 .accept
= sock_no_accept
,
216 .getname
= ipn_getname
,
219 .listen
= sock_no_listen
,
220 .shutdown
= ipn_shutdown
,
221 .setsockopt
= ipn_setsockopt
,
222 .getsockopt
= ipn_getsockopt
,
223 .sendmsg
= ipn_sendmsg
,
224 .recvmsg
= ipn_recvmsg
,
225 .mmap
= sock_no_mmap
,
226 .sendpage
= sock_no_sendpage
,
229 static struct proto ipn_proto
= {
231 .owner
= THIS_MODULE
,
232 .obj_size
= sizeof(struct ipn_sock
),
236 * ipn_node is a separate structure, pointed by ipn_sock -> node
237 * when a node is "persistent", ipn_node survives while ipn_sock gets released*/
238 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
239 static int ipn_create(struct socket
*sock
, int protocol
)
241 static int ipn_create(struct net
*net
,struct socket
*sock
, int protocol
)
244 struct ipn_sock
*ipn_sk
;
245 struct ipn_node
*ipn_node
;
247 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
250 if (net
!= &init_net
)
251 return -EAFNOSUPPORT
;
254 if (sock
->type
!= SOCK_RAW
)
259 protocol
=IPN_BROADCAST
-1;
260 if (protocol
< 0 || protocol
>= IPN_MAX_PROTO
||
261 ipn_protocol_table
[protocol
] == NULL
)
262 return -EPROTONOSUPPORT
;
263 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
264 ipn_sk
= (struct ipn_sock
*) sk_alloc(PF_IPN
, GFP_KERNEL
, &ipn_proto
, 1);
266 ipn_sk
= (struct ipn_sock
*) sk_alloc(net
, PF_IPN
, GFP_KERNEL
, &ipn_proto
);
271 ipn_sk
->node
=ipn_node
=kmem_cache_alloc(ipn_node_cache
,GFP_KERNEL
);
273 sock_put((struct sock
*) ipn_sk
);
276 sock_init_data(sock
,(struct sock
*) ipn_sk
);
277 sock
->state
= SS_UNCONNECTED
;
278 sock
->ops
= &ipn_ops
;
279 sock
->sk
=(struct sock
*)ipn_sk
;
280 INIT_LIST_HEAD(&ipn_node
->nodelist
);
281 ipn_node
->protocol
=protocol
;
282 ipn_node
->flags
=IPN_NODEFLAG_INUSE
;
283 ipn_node
->shutdown
=RCV_SHUTDOWN_NO_OOB
;
284 ipn_node
->descr
[0]=0;
285 ipn_node
->portno
=IPN_PORTNO_ANY
;
288 ipn_node
->proto_private
=NULL
;
289 ipn_node
->totmsgcount
=0;
290 ipn_node
->oobmsgcount
=0;
291 spin_lock_init(&ipn_node
->msglock
);
292 INIT_LIST_HEAD(&ipn_node
->msgqueue
);
293 INIT_LIST_HEAD(&ipn_node
->oobmsgqueue
);
295 init_waitqueue_head(&ipn_node
->read_wait
);
300 /* update # of readers and # of writers counters for an ipn network.
301 * This function sends oob messages to nodes requesting the service */
302 /* LOCKING ipnn_mutex is locked */
303 static void ipn_net_update_counters(struct ipn_network
*ipnn
,
304 int chg_readers
, int chg_writers
) {
305 ipnn
->numreaders
+= chg_readers
;
306 ipnn
->numwriters
+= chg_writers
;
307 if (ipnn
->mtu
>= sizeof(struct numnode_oob
))
309 struct msgpool_item
*ipn_msg
=_ipn_msgpool_alloc(ipnn
);
311 struct numnode_oob
*oob_msg
=(struct numnode_oob
*)(ipn_msg
->data
);
312 struct ipn_node
*ipn_node
;
313 ipn_msg
->len
=sizeof(struct numnode_oob
);
314 oob_msg
->level
=IPN_ANY
;
315 oob_msg
->tag
=IPN_OOB_NUMNODE_TAG
;
316 oob_msg
->numreaders
=ipnn
->numreaders
;
317 oob_msg
->numwriters
=ipnn
->numwriters
;
318 list_for_each_entry(ipn_node
, &ipnn
->connectqueue
, nodelist
) {
319 if (ipn_node
->flags
& IPN_NODEFLAG_OOB_NUMNODES
)
320 ipn_proto_oobsendmsg(ipn_node
,ipn_msg
);
322 ipn_msgpool_put(ipn_msg
,ipnn
);
327 /* flush pending messages (for close and shutdown RCV) */
328 /* LOCKING: ipnn_mutex is locked */
329 static void ipn_flush_recvqueue(struct ipn_node
*ipn_node
)
331 struct ipn_network
*ipnn
=ipn_node
->ipn
;
332 spin_lock(&ipn_node
->msglock
);
333 while (!list_empty(&ipn_node
->msgqueue
)) {
334 struct msgitem
*msgitem
=
335 list_first_entry(&ipn_node
->msgqueue
, struct msgitem
, list
);
336 list_del(&msgitem
->list
);
337 ipn_node
->totmsgcount
--;
338 ipn_msgpool_put(msgitem
->msg
,ipnn
);
339 kmem_cache_free(ipn_msgitem_cache
,msgitem
);
341 spin_unlock(&ipn_node
->msglock
);
344 /* flush pending oob messages (for socket close) */
345 /* LOCKING: ipnn_mutex is locked */
346 static void ipn_flush_oobrecvqueue(struct ipn_node
*ipn_node
)
348 struct ipn_network
*ipnn
=ipn_node
->ipn
;
349 spin_lock(&ipn_node
->msglock
);
350 while (!list_empty(&ipn_node
->oobmsgqueue
)) {
351 struct msgitem
*msgitem
=
352 list_first_entry(&ipn_node
->oobmsgqueue
, struct msgitem
, list
);
353 list_del(&msgitem
->list
);
354 ipn_node
->totmsgcount
--;
355 ipn_node
->oobmsgcount
--;
356 ipn_msgpool_put(msgitem
->msg
,ipnn
);
357 kmem_cache_free(ipn_msgitem_cache
,msgitem
);
359 spin_unlock(&ipn_node
->msglock
);
362 /* Terminate node. The node is "logically" terminated. */
363 /* LOCKING: ipn_glob_lock must be locked here */
364 static int ipn_terminate_node(struct ipn_node
*ipn_node
)
366 struct ipn_network
*ipnn
=ipn_node
->ipn
;
368 if (down_interruptible(&ipnn
->ipnn_mutex
))
370 if (ipn_node
->portno
>= 0) {
371 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_predelport(ipn_node
);
372 ipnn
->connport
[ipn_node
->portno
]=NULL
;
374 list_del(&ipn_node
->nodelist
);
375 ipn_flush_recvqueue(ipn_node
);
376 ipn_flush_oobrecvqueue(ipn_node
);
377 if (ipn_node
->portno
>= 0)
378 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_delport(ipn_node
);
380 ipn_net_update_counters(ipnn
,
381 (ipn_node
->shutdown
& RCV_SHUTDOWN
)?0:-1,
382 (ipn_node
->shutdown
& SEND_SHUTDOWN
)?0:-1);
383 ipn_node
->shutdown
= SHUTDOWN_XMASK
;
384 up(&ipnn
->ipnn_mutex
);
386 ipn_netdev_close(ipn_node
);
387 /* No more network elements */
389 if (ipnn
->refcnt
== 0)
391 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_delnet(ipnn
);
392 ipn_remove_network(ipnn
);
393 ipn_protocol_table
[ipnn
->protocol
]->refcnt
--;
398 if (ipnn
->msgpool_cache
)
399 ipn_msgbuf_put(ipnn
->msgpool_cache
);
401 kfree(ipnn
->connport
);
402 kmem_cache_free(ipn_network_cache
, ipnn
);
403 module_put(THIS_MODULE
);
407 kfree(ipn_node
->pbp
);
413 /* release of a socket */
414 static int ipn_release (struct socket
*sock
)
416 struct ipn_sock
*ipn_sk
=(struct ipn_sock
*)sock
->sk
;
417 struct ipn_node
*ipn_node
=ipn_sk
->node
;
419 if (down_interruptible(&ipn_glob_mutex
))
421 if (ipn_node
->flags
& IPN_NODEFLAG_PERSIST
) {
422 ipn_node
->flags
&= ~IPN_NODEFLAG_INUSE
;
426 rv
=ipn_terminate_node(ipn_node
);
430 kmem_cache_free(ipn_node_cache
,ipn_node
);
434 sock_put((struct sock
*) ipn_sk
);
438 /* _set persist, change the persistence of a node,
439 * when persistence gets cleared and the node is no longer used
440 * the node is terminated and freed.
441 * ipn_glob_mutex must be locked */
442 static int _ipn_setpersist(struct ipn_node
*ipn_node
, int persist
)
446 ipn_node
->flags
|= IPN_NODEFLAG_PERSIST
;
448 ipn_node
->flags
&= ~IPN_NODEFLAG_PERSIST
;
449 if (!(ipn_node
->flags
& IPN_NODEFLAG_INUSE
)) {
450 rv
=ipn_terminate_node(ipn_node
);
452 kmem_cache_free(ipn_node_cache
,ipn_node
);
459 * lock ipn_glob_mutex and call __ipn_setpersist above */
460 static int ipn_setpersist(struct ipn_node
*ipn_node
, int persist
)
463 if (ipn_node
->dev
== NULL
)
465 if (down_interruptible(&ipn_glob_mutex
))
467 rv
=_ipn_setpersist(ipn_node
,persist
);
472 /* several network parameters can be set by setsockopt prior to bind */
473 /* struct pre_bind_parms is a temporary stucture connected to ipn_node->pbp
474 * to keep the parameter values. */
475 struct pre_bind_parms
{
476 unsigned short maxports
;
477 unsigned short flags
;
478 unsigned short msgpoolsize
;
483 /* STD_PARMS: BITS_PER_LONG nodes, no flags, BITS_PER_BYTE pending msgs,
484 * Ethernet + VLAN MTU*/
485 #define STD_BIND_PARMS {BITS_PER_LONG, 0, BITS_PER_BYTE, 1514, 0x777};
487 static int ipn_mkname(struct sockaddr_un
* sunaddr
, int len
)
489 if (len
<= sizeof(short) || len
> sizeof(*sunaddr
))
491 if (!sunaddr
|| sunaddr
->sun_family
!= AF_IPN
)
494 * This may look like an off by one error but it is a bit more
495 * subtle. 108 is the longest valid AF_IPN path for a binding.
496 * sun_path[108] doesnt as such exist. However in kernel space
497 * we are guaranteed that it is a valid memory location in our
498 * kernel address buffer.
500 ((char *)sunaddr
)[len
]=0;
501 len
= strlen(sunaddr
->sun_path
)+1+sizeof(short);
506 static int ipn_bind(struct socket
*sock
, struct sockaddr
*uaddr
, int addr_len
)
508 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)uaddr
;
509 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
511 struct ipn_network
*ipnn
;
512 struct dentry
* dentry
= NULL
;
514 struct pre_bind_parms parms
=STD_BIND_PARMS
;
516 //printk("IPN bind\n");
518 if (down_interruptible(&ipn_glob_mutex
))
520 if (sock
->state
!= SS_UNCONNECTED
||
521 ipn_node
->ipn
!= NULL
) {
526 if (ipn_node
->protocol
>= 0 &&
527 (ipn_node
->protocol
>= IPN_MAX_PROTO
||
528 ipn_protocol_table
[ipn_node
->protocol
] == NULL
)) {
529 err
= -EPROTONOSUPPORT
;
533 addr_len
= ipn_mkname(sunaddr
, addr_len
);
539 /* check if there is already an ipn-network socket with that name */
540 err
= path_lookup(sunaddr
->sun_path
, LOOKUP_FOLLOW
, &nd
);
541 if (err
) { /* it does not exist, NEW IPN socket! */
543 /* Is it everything okay with the parent? */
544 err
= path_lookup(sunaddr
->sun_path
, LOOKUP_PARENT
, &nd
);
546 goto out_mknod_parent
;
547 /* Do I have the permission to create a file? */
548 dentry
= lookup_create(&nd
, 0);
549 err
= PTR_ERR(dentry
);
551 goto out_mknod_unlock
;
553 * All right, let's create it.
556 mode
= ipn_node
->pbp
->mode
;
558 mode
= SOCK_INODE(sock
)->i_mode
;
559 mode
= S_IFSOCK
| (mode
& ~current
->fs
->umask
);
562 err
= vfs_mknod(nd
.path
.dentry
->d_inode
, dentry
, nd
.path
.mnt
, mode
, 0);
564 err
= vfs_mknod(nd
.path
.dentry
->d_inode
, dentry
, mode
, 0);
568 err
= vfs_mknod(nd
.dentry
->d_inode
, dentry
, nd
.path
.mnt
, mode
, 0);
570 err
= vfs_mknod(nd
.dentry
->d_inode
, dentry
, mode
, 0);
576 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
577 dput(nd
.path
.dentry
);
578 nd
.path
.dentry
= dentry
;
580 mutex_unlock(&nd
.dentry
->d_inode
->i_mutex
);
584 /* create a new ipn_network item */
586 parms
=*ipn_node
->pbp
;
587 ipnn
=kmem_cache_zalloc(ipn_network_cache
,GFP_KERNEL
);
590 goto out_mknod_dput_ipnn
;
592 ipnn
->connport
=kzalloc(parms
.maxports
* sizeof(struct ipn_node
*),GFP_KERNEL
);
593 if (!ipnn
->connport
) {
595 goto out_mknod_dput_ipnn2
;
598 /* module refcnt is incremented for each network, thus
599 * rmmod is forbidden if there are persistent node */
600 if (!try_module_get(THIS_MODULE
)) {
602 goto out_mknod_dput_ipnn2
;
604 memcpy(&ipnn
->sunaddr
,sunaddr
,addr_len
);
606 ipnn
->msgpool_cache
=ipn_msgbuf_get(ipnn
->mtu
);
607 if (!ipnn
->msgpool_cache
) {
609 goto out_mknod_dput_putmodule
;
611 INIT_LIST_HEAD(&ipnn
->unconnectqueue
);
612 INIT_LIST_HEAD(&ipnn
->connectqueue
);
615 ipnn
->dentry
=nd
.path
.dentry
;
616 ipnn
->mnt
=nd
.path
.mnt
;
618 ipnn
->dentry
=nd
.dentry
;
621 init_MUTEX(&ipnn
->ipnn_mutex
);
622 ipnn
->sunaddr_len
=addr_len
;
623 ipnn
->protocol
=ipn_node
->protocol
;
624 if (ipnn
->protocol
< 0) ipnn
->protocol
= 0;
625 ipn_protocol_table
[ipnn
->protocol
]->refcnt
++;
626 ipnn
->flags
=parms
.flags
;
629 ipnn
->maxports
=parms
.maxports
;
630 atomic_set(&ipnn
->msgpool_nelem
,0);
631 ipnn
->msgpool_size
=parms
.msgpoolsize
;
632 ipnn
->proto_private
=NULL
;
633 init_waitqueue_head(&ipnn
->send_wait
);
634 err
=ipn_protocol_table
[ipnn
->protocol
]->ipn_p_newnet(ipnn
);
636 goto out_mknod_dput_putmodule
;
638 ipn_insert_network(&ipn_network_table
[nd
.path
.dentry
->d_inode
->i_ino
& (IPN_HASH_SIZE
-1)],ipnn
);
640 ipn_insert_network(&ipn_network_table
[nd
.dentry
->d_inode
->i_ino
& (IPN_HASH_SIZE
-1)],ipnn
);
643 /* join an existing network */
644 if (parms
.flags
& IPN_FLAG_EXCL
) {
648 err
= inode_permission(nd
.path
.dentry
->d_inode
, MAY_EXEC
);
653 if (!S_ISSOCK(nd
.path
.dentry
->d_inode
->i_mode
))
655 ipnn
=ipn_find_network_byinode(nd
.path
.dentry
->d_inode
);
657 if (!S_ISSOCK(nd
.dentry
->d_inode
->i_mode
))
659 ipnn
=ipn_find_network_byinode(nd
.dentry
->d_inode
);
661 if (!ipnn
|| (ipnn
->flags
& IPN_FLAG_TERMINATED
) ||
662 (ipnn
->flags
& IPN_FLAG_EXCL
))
664 list_add_tail(&ipn_node
->nodelist
,&ipnn
->unconnectqueue
);
668 kfree(ipn_node
->pbp
);
672 ipn_node
->flags
|= IPN_NODEFLAG_BOUND
;
686 out_mknod_dput_putmodule
:
687 module_put(THIS_MODULE
);
688 out_mknod_dput_ipnn2
:
689 kfree(ipnn
->connport
);
691 kmem_cache_free(ipn_network_cache
,ipnn
);
696 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
699 mutex_unlock(&nd
.dentry
->d_inode
->i_mutex
);
710 static int ipn_connect(struct socket
*sock
, struct sockaddr
*addr
,
711 int addr_len
, int flags
){
712 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)addr
;
713 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
715 struct ipn_network
*ipnn
,*previousipnn
;
719 /* the socket cannot be connected twice */
720 if (sock
->state
!= SS_UNCONNECTED
)
723 if (down_interruptible(&ipn_glob_mutex
))
726 if ((previousipnn
=ipn_node
->ipn
) == NULL
) { /* unbound */
727 unsigned char mustshutdown
=0;
728 err
= ipn_mkname(sunaddr
, addr_len
);
732 err
= path_lookup(sunaddr
->sun_path
, LOOKUP_FOLLOW
, &nd
);
735 err
= inode_permission(nd
.path
.dentry
->d_inode
, MAY_READ
);
737 if (err
== -EACCES
|| err
== -EROFS
)
738 mustshutdown
|=RCV_SHUTDOWN
;
742 err
= inode_permission(nd
.path
.dentry
->d_inode
, MAY_WRITE
);
745 mustshutdown
|=SEND_SHUTDOWN
;
749 mustshutdown
|= ipn_node
->shutdown
;
750 /* if the combination of shutdown and permissions leaves
751 * no abilities, connect returns EACCES */
752 if (mustshutdown
== SHUTDOWN_XMASK
) {
757 ipn_node
->shutdown
=mustshutdown
;
760 if (!S_ISSOCK(nd
.path
.dentry
->d_inode
->i_mode
)) {
764 ipnn
=ipn_find_network_byinode(nd
.path
.dentry
->d_inode
);
766 if (!S_ISSOCK(nd
.dentry
->d_inode
->i_mode
)) {
770 ipnn
=ipn_find_network_byinode(nd
.dentry
->d_inode
);
772 if (!ipnn
|| (ipnn
->flags
& IPN_FLAG_TERMINATED
)) {
776 if (ipn_node
->protocol
== IPN_ANY
)
777 ipn_node
->protocol
=ipnn
->protocol
;
778 else if (ipnn
->protocol
!= ipn_node
->protocol
) {
791 if (down_interruptible(&ipnn
->ipnn_mutex
)) {
795 portno
= ipn_protocol_table
[ipnn
->protocol
]->ipn_p_newport(ipn_node
);
796 if (portno
>= 0 && portno
<ipnn
->maxports
) {
797 sock
->state
= SS_CONNECTED
;
798 ipn_node
->portno
=portno
;
799 ipnn
->connport
[portno
]=ipn_node
;
800 if (!(ipn_node
->flags
& IPN_NODEFLAG_BOUND
)) {
802 list_del(&ipn_node
->nodelist
);
804 list_add_tail(&ipn_node
->nodelist
,&ipnn
->connectqueue
);
805 ipn_net_update_counters(ipnn
,
806 (ipn_node
->shutdown
& RCV_SHUTDOWN
)?0:1,
807 (ipn_node
->shutdown
& SEND_SHUTDOWN
)?0:1);
809 ipn_node
->ipn
=previousipnn
; /* undo changes on ipn_node->ipn */
812 up(&ipnn
->ipnn_mutex
);
827 static int ipn_getname(struct socket
*sock
, struct sockaddr
*uaddr
,
828 int *uaddr_len
, int peer
) {
829 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
830 struct ipn_network
*ipnn
=ipn_node
->ipn
;
831 struct sockaddr_un
*sunaddr
=(struct sockaddr_un
*)uaddr
;
834 if (down_interruptible(&ipn_glob_mutex
))
837 *uaddr_len
= ipnn
->sunaddr_len
;
838 memcpy(sunaddr
,&ipnn
->sunaddr
,*uaddr_len
);
846 static unsigned int ipn_poll(struct file
*file
, struct socket
*sock
,
848 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
849 struct ipn_network
*ipnn
=ipn_node
->ipn
;
853 poll_wait(file
,&ipn_node
->read_wait
,wait
);
854 if (ipnn
->flags
& IPN_FLAG_LOSSLESS
)
855 poll_wait(file
,&ipnn
->send_wait
,wait
);
856 /* POLLIN if recv succeeds,
857 * POLL{PRI,RDNORM} if there are {oob,non-oob} messages */
858 if (ipn_node
->totmsgcount
> 0) mask
|= POLLIN
;
859 if (!(list_empty(&ipn_node
->msgqueue
))) mask
|= POLLRDNORM
;
860 if (!(list_empty(&ipn_node
->oobmsgqueue
))) mask
|= POLLPRI
;
861 if ((!(ipnn
->flags
& IPN_FLAG_LOSSLESS
)) |
862 (atomic_read(&ipnn
->msgpool_nelem
) < ipnn
->msgpool_size
))
863 mask
|= POLLOUT
| POLLWRNORM
;
868 /* connect netdev (from ioctl). connect a bound socket to a
869 * network device TAP or GRAB */
870 static int ipn_connect_netdev(struct socket
*sock
,struct ifreq
*ifr
)
873 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
874 struct ipn_network
*ipnn
=ipn_node
->ipn
;
875 if (!capable(CAP_NET_ADMIN
))
877 if (sock
->state
!= SS_UNCONNECTED
)
880 return -ENOTCONN
; /* Maybe we need a different error for "NOT BOUND" */
881 if (down_interruptible(&ipn_glob_mutex
))
883 if (down_interruptible(&ipnn
->ipnn_mutex
)) {
887 ipn_node
->dev
=ipn_netdev_alloc(ipn_node
->net
,ifr
->ifr_flags
,ifr
->ifr_name
,&err
);
890 portno
= ipn_protocol_table
[ipnn
->protocol
]->ipn_p_newport(ipn_node
);
891 if (portno
>= 0 && portno
<ipnn
->maxports
) {
892 sock
->state
= SS_CONNECTED
;
893 ipn_node
->portno
=portno
;
894 ipn_node
->flags
|= ifr
->ifr_flags
& IPN_NODEFLAG_DEVMASK
;
895 ipnn
->connport
[portno
]=ipn_node
;
896 err
=ipn_netdev_activate(ipn_node
);
898 sock
->state
= SS_UNCONNECTED
;
899 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_delport(ipn_node
);
901 ipn_node
->portno
= -1;
902 ipn_node
->flags
&= ~IPN_NODEFLAG_DEVMASK
;
903 ipnn
->connport
[portno
]=NULL
;
905 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_postnewport(ipn_node
);
906 list_del(&ipn_node
->nodelist
);
907 list_add_tail(&ipn_node
->nodelist
,&ipnn
->connectqueue
);
910 ipn_netdev_close(ipn_node
);
916 up(&ipnn
->ipnn_mutex
);
921 /* join a netdev, a socket gets connected to a persistent node
922 * not connected to another socket */
923 static int ipn_join_netdev(struct socket
*sock
,struct ifreq
*ifr
)
926 struct net_device
*dev
;
927 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
928 struct ipn_node
*ipn_joined
;
929 struct ipn_network
*ipnn
=ipn_node
->ipn
;
930 if (sock
->state
!= SS_UNCONNECTED
)
932 if (down_interruptible(&ipn_glob_mutex
))
934 if (down_interruptible(&ipnn
->ipnn_mutex
)) {
938 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
939 dev
=__dev_get_by_name(ifr
->ifr_name
);
941 dev
=__dev_get_by_name(ipn_node
->net
,ifr
->ifr_name
);
944 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
945 dev
=__dev_get_by_index(ifr
->ifr_ifindex
);
947 dev
=__dev_get_by_index(ipn_node
->net
,ifr
->ifr_ifindex
);
949 if (dev
&& (ipn_joined
=ipn_netdev2node(dev
)) != NULL
) { /* the interface does exist */
951 for (i
=0;i
<ipnn
->maxports
&& ipn_joined
!= ipnn
->connport
[i
] ;i
++)
953 if (i
< ipnn
->maxports
) { /* found */
954 /* ipn_joined is substituted to ipn_node */
955 ((struct ipn_sock
*)sock
->sk
)->node
=ipn_joined
;
956 ipn_joined
->flags
|= IPN_NODEFLAG_INUSE
;
958 kmem_cache_free(ipn_node_cache
,ipn_node
);
963 up(&ipnn
->ipnn_mutex
);
968 /* set persistence of a node looking for it by interface name
969 * (it is for sysadm, to close network interfaces)*/
970 static int ipn_setpersist_netdev(struct ifreq
*ifr
, int value
)
972 struct net_device
*dev
;
973 struct ipn_node
*ipn_node
;
975 if (!capable(CAP_NET_ADMIN
))
977 if (down_interruptible(&ipn_glob_mutex
))
979 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
980 dev
=__dev_get_by_name(ifr
->ifr_name
);
982 dev
=__dev_get_by_name(&init_net
,ifr
->ifr_name
);
985 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
986 dev
=__dev_get_by_index(ifr
->ifr_ifindex
);
988 dev
=__dev_get_by_index(&init_net
,ifr
->ifr_ifindex
);
990 if (dev
&& (ipn_node
=ipn_netdev2node(dev
)) != NULL
)
991 _ipn_setpersist(ipn_node
,value
);
999 static int ipn_ioctl(struct socket
*sock
, unsigned int cmd
, unsigned long arg
) {
1000 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1001 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1002 void __user
* argp
= (void __user
*)arg
;
1005 if (ipn_node
->shutdown
== SHUTDOWN_XMASK
)
1012 case IPN_SETPERSIST_NETDEV
:
1013 case IPN_CLRPERSIST_NETDEV
:
1014 case IPN_CONN_NETDEV
:
1015 case IPN_JOIN_NETDEV
:
1017 if (copy_from_user(&ifr
, argp
, sizeof ifr
))
1019 ifr
.ifr_name
[IFNAMSIZ
-1] = '\0';
1022 /* actions for unconnected and unbound sockets */
1024 case IPN_SETPERSIST_NETDEV
:
1025 return ipn_setpersist_netdev(&ifr
,1);
1026 case IPN_CLRPERSIST_NETDEV
:
1027 return ipn_setpersist_netdev(&ifr
,0);
1029 if (capable(CAP_NET_ADMIN
))
1031 if (ipn_node
->dev
&& (ipn_node
->flags
&IPN_NODEFLAG_TAP
))
1032 return dev_set_mac_address(ipn_node
->dev
, &ifr
.ifr_hwaddr
);
1034 return -EADDRNOTAVAIL
;
1036 if (ipnn
== NULL
|| (ipnn
->flags
& IPN_FLAG_TERMINATED
))
1038 /* actions for connected or bound sockets */
1040 case IPN_CONN_NETDEV
:
1041 return ipn_connect_netdev(sock
,&ifr
);
1042 case IPN_JOIN_NETDEV
:
1043 return ipn_join_netdev(sock
,&ifr
);
1044 case IPN_SETPERSIST
:
1045 return ipn_setpersist(ipn_node
,arg
);
1049 if (down_interruptible(&ipnn
->ipnn_mutex
))
1050 return -ERESTARTSYS
;
1051 rv
=ipn_protocol_table
[ipn_node
->protocol
]->ipn_p_ioctl(ipn_node
,cmd
,arg
);
1052 up(&ipnn
->ipnn_mutex
);
1059 /* shutdown: close socket for input or for output.
1060 * shutdown can be called prior to connect and it is not reversible */
1061 static int ipn_shutdown(struct socket
*sock
, int mode
) {
1062 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1063 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1064 int oldshutdown
=ipn_node
->shutdown
;
1065 mode
= (mode
+1)&(RCV_SHUTDOWN
|SEND_SHUTDOWN
);
1067 ipn_node
->shutdown
|= mode
;
1070 if (down_interruptible(&ipnn
->ipnn_mutex
)) {
1071 ipn_node
->shutdown
= oldshutdown
;
1072 return -ERESTARTSYS
;
1074 oldshutdown
=ipn_node
->shutdown
-oldshutdown
;
1075 if (sock
->state
== SS_CONNECTED
&& oldshutdown
) {
1076 ipn_net_update_counters(ipnn
,
1077 (ipn_node
->shutdown
& RCV_SHUTDOWN
)?0:-1,
1078 (ipn_node
->shutdown
& SEND_SHUTDOWN
)?0:-1);
1081 /* if recv channel has been shut down, flush the recv queue */
1082 if ((ipn_node
->shutdown
& RCV_SHUTDOWN
))
1083 ipn_flush_recvqueue(ipn_node
);
1084 up(&ipnn
->ipnn_mutex
);
1089 /* injectmsg: a new message is entering the ipn network.
1090 * injectmsg gets called by send and by the grab/tap node */
1091 int ipn_proto_injectmsg(struct ipn_node
*from
, struct msgpool_item
*msg
)
1093 struct ipn_network
*ipnn
=from
->ipn
;
1095 if (down_interruptible(&ipnn
->ipnn_mutex
))
1098 ipn_protocol_table
[ipnn
->protocol
]->ipn_p_handlemsg(from
, msg
);
1099 up(&ipnn
->ipnn_mutex
);
1105 static int ipn_sendmsg(struct kiocb
*kiocb
, struct socket
*sock
,
1106 struct msghdr
*msg
, size_t len
) {
1107 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1108 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1109 struct msgpool_item
*newmsg
;
1112 if (unlikely(sock
->state
!= SS_CONNECTED
))
1114 if (unlikely(ipn_node
->shutdown
& SEND_SHUTDOWN
)) {
1115 if (ipn_node
->shutdown
== SHUTDOWN_XMASK
)
1120 if (len
> ipnn
->mtu
)
1122 newmsg
=ipn_msgpool_alloc_locking(ipnn
);
1126 err
=memcpy_fromiovec(newmsg
->data
, msg
->msg_iov
, len
);
1128 ipn_proto_injectmsg(ipn_node
, newmsg
);
1129 ipn_msgpool_put(newmsg
,ipnn
);
1133 /* enqueue an oob message. "to" is the destination */
1134 void ipn_proto_oobsendmsg(struct ipn_node
*to
, struct msgpool_item
*msg
)
1137 if (!to
->dev
) { /* no oob to netdev */
1138 struct msgitem
*msgitem
;
1139 struct ipn_network
*ipnn
=to
->ipn
;
1140 spin_lock(&to
->msglock
);
1141 if ((to
->shutdown
& RCV_SHUTDOWN_NO_OOB
) == 0 &&
1142 (ipnn
->flags
& IPN_FLAG_LOSSLESS
||
1143 to
->oobmsgcount
< ipnn
->msgpool_size
)) {
1144 if ((msgitem
=kmem_cache_alloc(ipn_msgitem_cache
,GFP_KERNEL
))!=NULL
) {
1148 list_add_tail(&msgitem
->list
, &to
->oobmsgqueue
);
1149 ipn_msgpool_hold(msg
);
1152 spin_unlock(&to
->msglock
);
1153 wake_up_interruptible(&to
->read_wait
);
1158 /* ipn_proto_sendmsg is called by protocol implementation to enqueue a
1159 * for a destination (to).*/
1160 void ipn_proto_sendmsg(struct ipn_node
*to
, struct msgpool_item
*msg
)
1164 ipn_netdev_sendmsg(to
,msg
);
1167 struct msgitem
*msgitem
;
1168 struct ipn_network
*ipnn
=to
->ipn
;
1169 spin_lock(&to
->msglock
);
1170 if (likely((to
->shutdown
& RCV_SHUTDOWN
)==0)) {
1171 /*if (unlikely((ipnn->flags & IPN_FLAG_LOSSLESS) == 0 ||
1172 to->totmsgcount >= ipnn->msgpool_size))
1174 if (likely(ipnn
->flags
& IPN_FLAG_LOSSLESS
||
1175 to
->totmsgcount
< ipnn
->msgpool_size
)) {
1176 if ((msgitem
=kmem_cache_alloc(ipn_msgitem_cache
,GFP_KERNEL
))!=NULL
) {
1179 list_add_tail(&msgitem
->list
, &to
->msgqueue
);
1180 ipn_msgpool_hold(msg
);
1184 spin_unlock(&to
->msglock
);
1185 wake_up_interruptible(&to
->read_wait
);
1191 static int ipn_recvmsg(struct kiocb
*kiocb
, struct socket
*sock
,
1192 struct msghdr
*msg
, size_t len
, int flags
) {
1193 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1194 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1195 struct msgitem
*msgitem
;
1196 struct msgpool_item
*currmsg
;
1198 if (unlikely(sock
->state
!= SS_CONNECTED
))
1201 if (unlikely((ipn_node
->shutdown
& XRCV_SHUTDOWN
) == XRCV_SHUTDOWN
)) {
1202 if (ipn_node
->shutdown
== SHUTDOWN_XMASK
) /*EOF, nothing can be read*/
1205 return -EPIPE
; /*trying to read on a write only node */
1208 /* wait for a message */
1209 spin_lock(&ipn_node
->msglock
);
1210 while (ipn_node
->totmsgcount
== 0) {
1211 spin_unlock(&ipn_node
->msglock
);
1212 if (wait_event_interruptible(ipn_node
->read_wait
,
1213 !(ipn_node
->totmsgcount
== 0)))
1214 return -ERESTARTSYS
;
1215 spin_lock(&ipn_node
->msglock
);
1217 /* oob gets delivered first. oob are rare */
1218 if (likely(list_empty(&ipn_node
->oobmsgqueue
)))
1219 msgitem
=list_first_entry(&ipn_node
->msgqueue
, struct msgitem
, list
);
1221 msgitem
=list_first_entry(&ipn_node
->oobmsgqueue
, struct msgitem
, list
);
1222 msg
->msg_flags
|= MSG_OOB
;
1223 ipn_node
->oobmsgcount
--;
1225 list_del(&msgitem
->list
);
1226 ipn_node
->totmsgcount
--;
1227 spin_unlock(&ipn_node
->msglock
);
1228 currmsg
=msgitem
->msg
;
1229 if (currmsg
->len
< len
)
1231 memcpy_toiovec(msg
->msg_iov
, currmsg
->data
, len
);
1232 ipn_msgpool_put(currmsg
,ipnn
);
1233 kmem_cache_free(ipn_msgitem_cache
,msgitem
);
1238 /* resize a network: change the # of communication ports (connport) */
1239 static int ipn_netresize(struct ipn_network
*ipnn
,int newsize
)
1242 struct ipn_node
**newconnport
;
1243 struct ipn_node
**oldconnport
;
1245 if (down_interruptible(&ipnn
->ipnn_mutex
))
1246 return -ERESTARTSYS
;
1247 oldsize
=ipnn
->maxports
;
1248 if (newsize
== oldsize
) {
1249 up(&ipnn
->ipnn_mutex
);
1253 /* shrink a network. all the ports we are going to eliminate
1254 * must be unused! */
1255 if (newsize
< oldsize
) {
1257 for (i
=newsize
; i
<oldsize
; i
++)
1258 if (ipnn
->connport
[i
]) {
1259 up(&ipnn
->ipnn_mutex
);
1264 oldconnport
=ipnn
->connport
;
1265 /* allocate the new connport array and copy the old one */
1266 newconnport
=kzalloc(newsize
* sizeof(struct ipn_node
*),GFP_KERNEL
);
1268 up(&ipnn
->ipnn_mutex
);
1271 memcpy(newconnport
,oldconnport
,min
* sizeof(struct ipn_node
*));
1272 ipnn
->connport
=newconnport
;
1273 ipnn
->maxports
=newsize
;
1274 /* notify the protocol that the netowrk has been resized */
1275 err
=ipn_protocol_table
[ipnn
->protocol
]->ipn_p_resizenet(ipnn
,oldsize
,newsize
);
1277 /* roll back if the resize operation failed for the protocol */
1278 ipnn
->connport
=oldconnport
;
1279 ipnn
->maxports
=oldsize
;
1282 /* successful mission, network resized */
1284 up(&ipnn
->ipnn_mutex
);
1288 /* IPN SETSOCKOPT */
1289 static int ipn_setsockopt(struct socket
*sock
, int level
, int optname
,
1290 char __user
*optval
, int optlen
) {
1291 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1292 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1294 if (ipn_node
->shutdown
== SHUTDOWN_XMASK
)
1296 if (level
!= 0 && level
!= ipn_node
->protocol
+1)
1297 return -EPROTONOSUPPORT
;
1299 /* protocol specific sockopt */
1302 if (down_interruptible(&ipnn
->ipnn_mutex
))
1303 return -ERESTARTSYS
;
1304 rv
=ipn_protocol_table
[ipn_node
->protocol
]->ipn_p_setsockopt(ipn_node
,optname
,optval
,optlen
);
1305 up(&ipnn
->ipnn_mutex
);
1310 if (optname
== IPN_SO_DESCR
) {
1311 if (optlen
> IPN_DESCRLEN
)
1314 memset(ipn_node
->descr
,0,IPN_DESCRLEN
);
1315 if (copy_from_user(ipn_node
->descr
,optval
,optlen
))
1316 ipn_node
->descr
[0]=0;
1318 ipn_node
->descr
[optlen
-1]=0;
1322 if (optlen
< sizeof(int))
1324 else if ((optname
& IPN_SO_PREBIND
) && (ipnn
!= NULL
))
1328 get_user(val
, (int __user
*) optval
);
1329 if ((optname
& IPN_SO_PREBIND
) && !ipn_node
->pbp
) {
1330 struct pre_bind_parms std
=STD_BIND_PARMS
;
1331 ipn_node
->pbp
=kzalloc(sizeof(struct pre_bind_parms
),GFP_KERNEL
);
1334 *(ipn_node
->pbp
)=std
;
1338 if (sock
->state
== SS_UNCONNECTED
)
1339 ipn_node
->portno
=val
;
1343 case IPN_SO_CHANGE_NUMNODES
:
1344 if ((ipn_node
->flags
& IPN_NODEFLAG_BOUND
)!=0) {
1348 return ipn_netresize(ipnn
,val
);
1352 case IPN_SO_WANT_OOB_NUMNODES
:
1354 ipn_node
->flags
|= IPN_NODEFLAG_OOB_NUMNODES
;
1356 ipn_node
->flags
&= ~IPN_NODEFLAG_OOB_NUMNODES
;
1358 case IPN_SO_HANDLE_OOB
:
1360 ipn_node
->shutdown
&= ~RCV_SHUTDOWN_NO_OOB
;
1362 ipn_node
->shutdown
|= RCV_SHUTDOWN_NO_OOB
;
1368 ipn_node
->pbp
->mtu
=val
;
1370 case IPN_SO_NUMNODES
:
1374 ipn_node
->pbp
->maxports
=val
;
1376 case IPN_SO_MSGPOOLSIZE
:
1380 ipn_node
->pbp
->msgpoolsize
=val
;
1383 ipn_node
->pbp
->flags
=val
;
1386 ipn_node
->pbp
->mode
=val
;
1395 /* IPN GETSOCKOPT */
1396 static int ipn_getsockopt(struct socket
*sock
, int level
, int optname
,
1397 char __user
*optval
, int __user
*optlen
) {
1398 struct ipn_node
*ipn_node
=((struct ipn_sock
*)sock
->sk
)->node
;
1399 struct ipn_network
*ipnn
=ipn_node
->ipn
;
1402 if (ipn_node
->shutdown
== SHUTDOWN_XMASK
)
1404 if (level
!= 0 && level
!= ipn_node
->protocol
+1)
1405 return -EPROTONOSUPPORT
;
1409 /* protocol specific sockopt */
1410 if (down_interruptible(&ipnn
->ipnn_mutex
))
1411 return -ERESTARTSYS
;
1412 rv
=ipn_protocol_table
[ipn_node
->protocol
]->ipn_p_getsockopt(ipn_node
,optname
,optval
,optlen
);
1413 up(&ipnn
->ipnn_mutex
);
1418 if (get_user(len
, optlen
))
1420 if (optname
== IPN_SO_DESCR
) {
1421 if (len
< IPN_DESCRLEN
)
1424 if (len
> IPN_DESCRLEN
)
1426 if(put_user(len
, optlen
))
1428 if(copy_to_user(optval
,ipn_node
->descr
,len
))
1436 val
=ipn_node
->portno
;
1441 else if (ipn_node
->pbp
)
1442 val
=ipn_node
->pbp
->mtu
;
1444 case IPN_SO_NUMNODES
:
1447 else if (ipn_node
->pbp
)
1448 val
=ipn_node
->pbp
->maxports
;
1450 case IPN_SO_MSGPOOLSIZE
:
1452 val
=ipnn
->msgpool_size
;
1453 else if (ipn_node
->pbp
)
1454 val
=ipn_node
->pbp
->msgpoolsize
;
1459 else if (ipn_node
->pbp
)
1460 val
=ipn_node
->pbp
->flags
;
1465 else if (ipn_node
->pbp
)
1466 val
=ipn_node
->pbp
->mode
;
1472 if (len
< sizeof(int))
1476 if(put_user(len
, optlen
))
1478 if(copy_to_user(optval
,&val
,len
))
1487 /* BROADCAST/HUB implementation */
1489 static int ipn_bcast_newport(struct ipn_node
*newport
) {
1490 struct ipn_network
*ipnn
=newport
->ipn
;
1492 for (i
=0;i
<ipnn
->maxports
;i
++) {
1493 if (ipnn
->connport
[i
] == NULL
)
1499 static int ipn_bcast_handlemsg(struct ipn_node
*from
,
1500 struct msgpool_item
*msgitem
){
1501 struct ipn_network
*ipnn
=from
->ipn
;
1503 struct ipn_node
*ipn_node
;
1504 list_for_each_entry(ipn_node
, &ipnn
->connectqueue
, nodelist
) {
1505 if (ipn_node
!= from
)
1506 ipn_proto_sendmsg(ipn_node
,msgitem
);
1511 static void ipn_null_delport(struct ipn_node
*oldport
) {}
1512 static void ipn_null_postnewport(struct ipn_node
*newport
) {}
1513 static void ipn_null_predelport(struct ipn_node
*oldport
) {}
1514 static int ipn_null_newnet(struct ipn_network
*newnet
) {return 0;}
1515 static int ipn_null_resizenet(struct ipn_network
*net
,int oldsize
,int newsize
) {
1517 static void ipn_null_delnet(struct ipn_network
*oldnet
) {}
1518 static int ipn_null_setsockopt(struct ipn_node
*port
,int optname
,
1519 char __user
*optval
, int optlen
) {return -EOPNOTSUPP
;}
1520 static int ipn_null_getsockopt(struct ipn_node
*port
,int optname
,
1521 char __user
*optval
, int *optlen
) {return -EOPNOTSUPP
;}
1522 static int ipn_null_ioctl(struct ipn_node
*port
,unsigned int request
,
1523 unsigned long arg
) {return -EOPNOTSUPP
;}
1525 /* Protocol Registration/deregisteration */
1527 void ipn_init_protocol(struct ipn_protocol
*p
)
1529 if (p
->ipn_p_delport
== NULL
) p
->ipn_p_delport
=ipn_null_delport
;
1530 if (p
->ipn_p_postnewport
== NULL
) p
->ipn_p_postnewport
=ipn_null_postnewport
;
1531 if (p
->ipn_p_predelport
== NULL
) p
->ipn_p_predelport
=ipn_null_predelport
;
1532 if (p
->ipn_p_newnet
== NULL
) p
->ipn_p_newnet
=ipn_null_newnet
;
1533 if (p
->ipn_p_resizenet
== NULL
) p
->ipn_p_resizenet
=ipn_null_resizenet
;
1534 if (p
->ipn_p_delnet
== NULL
) p
->ipn_p_delnet
=ipn_null_delnet
;
1535 if (p
->ipn_p_setsockopt
== NULL
) p
->ipn_p_setsockopt
=ipn_null_setsockopt
;
1536 if (p
->ipn_p_getsockopt
== NULL
) p
->ipn_p_getsockopt
=ipn_null_getsockopt
;
1537 if (p
->ipn_p_ioctl
== NULL
) p
->ipn_p_ioctl
=ipn_null_ioctl
;
1540 int ipn_proto_register(int protocol
,struct ipn_protocol
*ipn_service
)
1543 if (ipn_service
->ipn_p_newport
== NULL
||
1544 ipn_service
->ipn_p_handlemsg
== NULL
)
1546 ipn_init_protocol(ipn_service
);
1547 if (down_interruptible(&ipn_glob_mutex
))
1548 return -ERESTARTSYS
;
1549 if (protocol
> 1 && protocol
<= IPN_MAX_PROTO
) {
1551 if (ipn_protocol_table
[protocol
])
1554 ipn_service
->refcnt
=0;
1555 ipn_protocol_table
[protocol
]=ipn_service
;
1556 printk(KERN_INFO
"IPN: Registered protocol %d\n",protocol
+1);
1560 up(&ipn_glob_mutex
);
1564 int ipn_proto_deregister(int protocol
)
1567 if (down_interruptible(&ipn_glob_mutex
))
1568 return -ERESTARTSYS
;
1569 if (protocol
> 1 && protocol
<= IPN_MAX_PROTO
) {
1571 if (ipn_protocol_table
[protocol
]) {
1572 if (ipn_protocol_table
[protocol
]->refcnt
== 0) {
1573 ipn_protocol_table
[protocol
]=NULL
;
1574 printk(KERN_INFO
"IPN: Unregistered protocol %d\n",protocol
+1);
1581 up(&ipn_glob_mutex
);
1586 /* Module constructor/destructor */
1587 static struct net_proto_family ipn_family_ops
= {
1589 .create
= ipn_create
,
1590 .owner
= THIS_MODULE
,
1593 /* IPN constructor */
1594 static int ipn_init(void)
1598 ipn_init_protocol(&ipn_bcast
);
1599 ipn_network_cache
=kmem_cache_create("ipn_network",sizeof(struct ipn_network
),0,0,NULL
);
1600 if (!ipn_network_cache
) {
1601 printk(KERN_CRIT
"%s: Cannot create ipn_network SLAB cache!\n",
1607 ipn_node_cache
=kmem_cache_create("ipn_node",sizeof(struct ipn_node
),0,0,NULL
);
1608 if (!ipn_node_cache
) {
1609 printk(KERN_CRIT
"%s: Cannot create ipn_node SLAB cache!\n",
1615 ipn_msgitem_cache
=kmem_cache_create("ipn_msgitem",sizeof(struct msgitem
),0,0,NULL
);
1616 if (!ipn_msgitem_cache
) {
1617 printk(KERN_CRIT
"%s: Cannot create ipn_msgitem SLAB cache!\n",
1623 rc
=ipn_msgbuf_init();
1625 printk(KERN_CRIT
"%s: Cannot create ipn_msgbuf SLAB cache\n",
1627 goto out_net_node_msg
;
1630 rc
=proto_register(&ipn_proto
,1);
1632 printk(KERN_CRIT
"%s: Cannot register the protocol!\n",
1634 goto out_net_node_msg_msgbuf
;
1637 sock_register(&ipn_family_ops
);
1639 printk(KERN_INFO
"IPN: Virtual Square Project, University of Bologna 2007\n");
1642 out_net_node_msg_msgbuf
:
1645 kmem_cache_destroy(ipn_msgitem_cache
);
1647 kmem_cache_destroy(ipn_node_cache
);
1649 kmem_cache_destroy(ipn_network_cache
);
1654 /* IPN destructor */
1655 static void ipn_exit(void)
1658 if (ipn_msgitem_cache
)
1659 kmem_cache_destroy(ipn_msgitem_cache
);
1661 kmem_cache_destroy(ipn_node_cache
);
1662 if (ipn_network_cache
)
1663 kmem_cache_destroy(ipn_network_cache
);
1665 sock_unregister(PF_IPN
);
1666 proto_unregister(&ipn_proto
);
1667 printk(KERN_INFO
"IPN removed\n");
1670 module_init(ipn_init
);
1671 module_exit(ipn_exit
);
1673 EXPORT_SYMBOL_GPL(ipn_proto_register
);
1674 EXPORT_SYMBOL_GPL(ipn_proto_deregister
);
1675 EXPORT_SYMBOL_GPL(ipn_proto_sendmsg
);
1676 EXPORT_SYMBOL_GPL(ipn_proto_oobsendmsg
);
1677 EXPORT_SYMBOL_GPL(ipn_msgpool_alloc
);
1678 EXPORT_SYMBOL_GPL(ipn_msgpool_put
);