2 * linux/net/iucv/af_iucv.c
4 * IUCV protocol stack for Linux on zSeries
6 * Copyright 2006 IBM Corporation
8 * Author(s): Jennifer Hunt <jenhunt@us.ibm.com>
11 #include <linux/module.h>
12 #include <linux/types.h>
13 #include <linux/list.h>
14 #include <linux/errno.h>
15 #include <linux/kernel.h>
16 #include <linux/sched.h>
17 #include <linux/slab.h>
18 #include <linux/skbuff.h>
19 #include <linux/init.h>
20 #include <linux/poll.h>
22 #include <asm/ebcdic.h>
23 #include <asm/cpcmd.h>
24 #include <linux/kmod.h>
26 #include <net/iucv/iucv.h>
27 #include <net/iucv/af_iucv.h>
29 #define CONFIG_IUCV_SOCK_DEBUG 1
34 static char iucv_userid
[80];
36 static struct proto_ops iucv_sock_ops
;
38 static struct proto iucv_proto
= {
41 .obj_size
= sizeof(struct iucv_sock
),
44 static void iucv_sock_kill(struct sock
*sk
);
45 static void iucv_sock_close(struct sock
*sk
);
47 /* Call Back functions */
48 static void iucv_callback_rx(struct iucv_path
*, struct iucv_message
*);
49 static void iucv_callback_txdone(struct iucv_path
*, struct iucv_message
*);
50 static void iucv_callback_connack(struct iucv_path
*, u8 ipuser
[16]);
51 static int iucv_callback_connreq(struct iucv_path
*, u8 ipvmid
[8],
53 static void iucv_callback_connrej(struct iucv_path
*, u8 ipuser
[16]);
55 static struct iucv_sock_list iucv_sk_list
= {
56 .lock
= RW_LOCK_UNLOCKED
,
57 .autobind_name
= ATOMIC_INIT(0)
60 static struct iucv_handler af_iucv_handler
= {
61 .path_pending
= iucv_callback_connreq
,
62 .path_complete
= iucv_callback_connack
,
63 .path_severed
= iucv_callback_connrej
,
64 .message_pending
= iucv_callback_rx
,
65 .message_complete
= iucv_callback_txdone
68 static inline void high_nmcpy(unsigned char *dst
, char *src
)
73 static inline void low_nmcpy(unsigned char *dst
, char *src
)
75 memcpy(&dst
[8], src
, 8);
79 static void iucv_sock_timeout(unsigned long arg
)
81 struct sock
*sk
= (struct sock
*)arg
;
84 sk
->sk_err
= ETIMEDOUT
;
85 sk
->sk_state_change(sk
);
92 static void iucv_sock_clear_timer(struct sock
*sk
)
94 sk_stop_timer(sk
, &sk
->sk_timer
);
97 static struct sock
*__iucv_get_sock_by_name(char *nm
)
100 struct hlist_node
*node
;
102 sk_for_each(sk
, node
, &iucv_sk_list
.head
)
103 if (!memcmp(&iucv_sk(sk
)->src_name
, nm
, 8))
109 static void iucv_sock_destruct(struct sock
*sk
)
111 skb_queue_purge(&sk
->sk_receive_queue
);
112 skb_queue_purge(&sk
->sk_write_queue
);
116 static void iucv_sock_cleanup_listen(struct sock
*parent
)
120 /* Close non-accepted connections */
121 while ((sk
= iucv_accept_dequeue(parent
, NULL
))) {
126 parent
->sk_state
= IUCV_CLOSED
;
127 sock_set_flag(parent
, SOCK_ZAPPED
);
131 static void iucv_sock_kill(struct sock
*sk
)
133 if (!sock_flag(sk
, SOCK_ZAPPED
) || sk
->sk_socket
)
136 iucv_sock_unlink(&iucv_sk_list
, sk
);
137 sock_set_flag(sk
, SOCK_DEAD
);
141 /* Close an IUCV socket */
142 static void iucv_sock_close(struct sock
*sk
)
144 unsigned char user_data
[16];
145 struct iucv_sock
*iucv
= iucv_sk(sk
);
149 iucv_sock_clear_timer(sk
);
152 switch (sk
->sk_state
) {
154 iucv_sock_cleanup_listen(sk
);
161 sk
->sk_state
= IUCV_CLOSING
;
162 sk
->sk_state_change(sk
);
164 if (!skb_queue_empty(&iucv
->send_skb_q
)) {
165 if (sock_flag(sk
, SOCK_LINGER
) && sk
->sk_lingertime
)
166 timeo
= sk
->sk_lingertime
;
168 timeo
= IUCV_DISCONN_TIMEOUT
;
169 err
= iucv_sock_wait_state(sk
, IUCV_CLOSED
, 0, timeo
);
172 sk
->sk_state
= IUCV_CLOSED
;
173 sk
->sk_state_change(sk
);
176 low_nmcpy(user_data
, iucv
->src_name
);
177 high_nmcpy(user_data
, iucv
->dst_name
);
178 ASCEBC(user_data
, sizeof(user_data
));
179 err
= iucv_path_sever(iucv
->path
, user_data
);
180 iucv_path_free(iucv
->path
);
184 sk
->sk_err
= ECONNRESET
;
185 sk
->sk_state_change(sk
);
187 skb_queue_purge(&iucv
->send_skb_q
);
188 skb_queue_purge(&iucv
->backlog_skb_q
);
190 sock_set_flag(sk
, SOCK_ZAPPED
);
194 sock_set_flag(sk
, SOCK_ZAPPED
);
202 static void iucv_sock_init(struct sock
*sk
, struct sock
*parent
)
205 sk
->sk_type
= parent
->sk_type
;
208 static struct sock
*iucv_sock_alloc(struct socket
*sock
, int proto
, gfp_t prio
)
212 sk
= sk_alloc(&init_net
, PF_IUCV
, prio
, &iucv_proto
);
216 sock_init_data(sock
, sk
);
217 INIT_LIST_HEAD(&iucv_sk(sk
)->accept_q
);
218 spin_lock_init(&iucv_sk(sk
)->accept_q_lock
);
219 skb_queue_head_init(&iucv_sk(sk
)->send_skb_q
);
220 INIT_LIST_HEAD(&iucv_sk(sk
)->message_q
.list
);
221 spin_lock_init(&iucv_sk(sk
)->message_q
.lock
);
222 skb_queue_head_init(&iucv_sk(sk
)->backlog_skb_q
);
223 iucv_sk(sk
)->send_tag
= 0;
225 sk
->sk_destruct
= iucv_sock_destruct
;
226 sk
->sk_sndtimeo
= IUCV_CONN_TIMEOUT
;
227 sk
->sk_allocation
= GFP_DMA
;
229 sock_reset_flag(sk
, SOCK_ZAPPED
);
231 sk
->sk_protocol
= proto
;
232 sk
->sk_state
= IUCV_OPEN
;
234 setup_timer(&sk
->sk_timer
, iucv_sock_timeout
, (unsigned long)sk
);
236 iucv_sock_link(&iucv_sk_list
, sk
);
240 /* Create an IUCV socket */
241 static int iucv_sock_create(struct net
*net
, struct socket
*sock
, int protocol
)
245 if (sock
->type
!= SOCK_STREAM
)
246 return -ESOCKTNOSUPPORT
;
248 sock
->state
= SS_UNCONNECTED
;
249 sock
->ops
= &iucv_sock_ops
;
251 sk
= iucv_sock_alloc(sock
, protocol
, GFP_KERNEL
);
255 iucv_sock_init(sk
, NULL
);
260 void iucv_sock_link(struct iucv_sock_list
*l
, struct sock
*sk
)
262 write_lock_bh(&l
->lock
);
263 sk_add_node(sk
, &l
->head
);
264 write_unlock_bh(&l
->lock
);
267 void iucv_sock_unlink(struct iucv_sock_list
*l
, struct sock
*sk
)
269 write_lock_bh(&l
->lock
);
270 sk_del_node_init(sk
);
271 write_unlock_bh(&l
->lock
);
274 void iucv_accept_enqueue(struct sock
*parent
, struct sock
*sk
)
277 struct iucv_sock
*par
= iucv_sk(parent
);
280 spin_lock_irqsave(&par
->accept_q_lock
, flags
);
281 list_add_tail(&iucv_sk(sk
)->accept_q
, &par
->accept_q
);
282 spin_unlock_irqrestore(&par
->accept_q_lock
, flags
);
283 iucv_sk(sk
)->parent
= parent
;
284 parent
->sk_ack_backlog
++;
287 void iucv_accept_unlink(struct sock
*sk
)
290 struct iucv_sock
*par
= iucv_sk(iucv_sk(sk
)->parent
);
292 spin_lock_irqsave(&par
->accept_q_lock
, flags
);
293 list_del_init(&iucv_sk(sk
)->accept_q
);
294 spin_unlock_irqrestore(&par
->accept_q_lock
, flags
);
295 iucv_sk(sk
)->parent
->sk_ack_backlog
--;
296 iucv_sk(sk
)->parent
= NULL
;
300 struct sock
*iucv_accept_dequeue(struct sock
*parent
, struct socket
*newsock
)
302 struct iucv_sock
*isk
, *n
;
305 list_for_each_entry_safe(isk
, n
, &iucv_sk(parent
)->accept_q
, accept_q
) {
306 sk
= (struct sock
*) isk
;
309 if (sk
->sk_state
== IUCV_CLOSED
) {
310 iucv_accept_unlink(sk
);
315 if (sk
->sk_state
== IUCV_CONNECTED
||
316 sk
->sk_state
== IUCV_SEVERED
||
318 iucv_accept_unlink(sk
);
320 sock_graft(sk
, newsock
);
322 if (sk
->sk_state
== IUCV_SEVERED
)
323 sk
->sk_state
= IUCV_DISCONN
;
334 int iucv_sock_wait_state(struct sock
*sk
, int state
, int state2
,
337 DECLARE_WAITQUEUE(wait
, current
);
340 add_wait_queue(sk
->sk_sleep
, &wait
);
341 while (sk
->sk_state
!= state
&& sk
->sk_state
!= state2
) {
342 set_current_state(TASK_INTERRUPTIBLE
);
349 if (signal_pending(current
)) {
350 err
= sock_intr_errno(timeo
);
355 timeo
= schedule_timeout(timeo
);
358 err
= sock_error(sk
);
362 set_current_state(TASK_RUNNING
);
363 remove_wait_queue(sk
->sk_sleep
, &wait
);
367 /* Bind an unbound socket */
368 static int iucv_sock_bind(struct socket
*sock
, struct sockaddr
*addr
,
371 struct sockaddr_iucv
*sa
= (struct sockaddr_iucv
*) addr
;
372 struct sock
*sk
= sock
->sk
;
373 struct iucv_sock
*iucv
;
376 /* Verify the input sockaddr */
377 if (!addr
|| addr
->sa_family
!= AF_IUCV
)
381 if (sk
->sk_state
!= IUCV_OPEN
) {
386 write_lock_bh(&iucv_sk_list
.lock
);
389 if (__iucv_get_sock_by_name(sa
->siucv_name
)) {
398 /* Bind the socket */
399 memcpy(iucv
->src_name
, sa
->siucv_name
, 8);
401 /* Copy the user id */
402 memcpy(iucv
->src_user_id
, iucv_userid
, 8);
403 sk
->sk_state
= IUCV_BOUND
;
407 /* Release the socket list lock */
408 write_unlock_bh(&iucv_sk_list
.lock
);
414 /* Automatically bind an unbound socket */
415 static int iucv_sock_autobind(struct sock
*sk
)
417 struct iucv_sock
*iucv
= iucv_sk(sk
);
418 char query_buffer
[80];
422 /* Set the userid and name */
423 cpcmd("QUERY USERID", query_buffer
, sizeof(query_buffer
), &err
);
427 memcpy(iucv
->src_user_id
, query_buffer
, 8);
429 write_lock_bh(&iucv_sk_list
.lock
);
431 sprintf(name
, "%08x", atomic_inc_return(&iucv_sk_list
.autobind_name
));
432 while (__iucv_get_sock_by_name(name
)) {
433 sprintf(name
, "%08x",
434 atomic_inc_return(&iucv_sk_list
.autobind_name
));
437 write_unlock_bh(&iucv_sk_list
.lock
);
439 memcpy(&iucv
->src_name
, name
, 8);
444 /* Connect an unconnected socket */
445 static int iucv_sock_connect(struct socket
*sock
, struct sockaddr
*addr
,
448 struct sockaddr_iucv
*sa
= (struct sockaddr_iucv
*) addr
;
449 struct sock
*sk
= sock
->sk
;
450 struct iucv_sock
*iucv
;
451 unsigned char user_data
[16];
454 if (addr
->sa_family
!= AF_IUCV
|| alen
< sizeof(struct sockaddr_iucv
))
457 if (sk
->sk_state
!= IUCV_OPEN
&& sk
->sk_state
!= IUCV_BOUND
)
460 if (sk
->sk_type
!= SOCK_STREAM
)
465 if (sk
->sk_state
== IUCV_OPEN
) {
466 err
= iucv_sock_autobind(sk
);
473 /* Set the destination information */
474 memcpy(iucv_sk(sk
)->dst_user_id
, sa
->siucv_user_id
, 8);
475 memcpy(iucv_sk(sk
)->dst_name
, sa
->siucv_name
, 8);
477 high_nmcpy(user_data
, sa
->siucv_name
);
478 low_nmcpy(user_data
, iucv_sk(sk
)->src_name
);
479 ASCEBC(user_data
, sizeof(user_data
));
483 iucv
->path
= iucv_path_alloc(IUCV_QUEUELEN_DEFAULT
,
484 IPRMDATA
, GFP_KERNEL
);
489 err
= iucv_path_connect(iucv
->path
, &af_iucv_handler
,
490 sa
->siucv_user_id
, NULL
, user_data
, sk
);
492 iucv_path_free(iucv
->path
);
498 if (sk
->sk_state
!= IUCV_CONNECTED
) {
499 err
= iucv_sock_wait_state(sk
, IUCV_CONNECTED
, IUCV_DISCONN
,
500 sock_sndtimeo(sk
, flags
& O_NONBLOCK
));
503 if (sk
->sk_state
== IUCV_DISCONN
) {
505 return -ECONNREFUSED
;
512 /* Move a socket into listening state. */
513 static int iucv_sock_listen(struct socket
*sock
, int backlog
)
515 struct sock
*sk
= sock
->sk
;
521 if (sk
->sk_state
!= IUCV_BOUND
|| sock
->type
!= SOCK_STREAM
)
524 sk
->sk_max_ack_backlog
= backlog
;
525 sk
->sk_ack_backlog
= 0;
526 sk
->sk_state
= IUCV_LISTEN
;
534 /* Accept a pending connection */
535 static int iucv_sock_accept(struct socket
*sock
, struct socket
*newsock
,
538 DECLARE_WAITQUEUE(wait
, current
);
539 struct sock
*sk
= sock
->sk
, *nsk
;
543 lock_sock_nested(sk
, SINGLE_DEPTH_NESTING
);
545 if (sk
->sk_state
!= IUCV_LISTEN
) {
550 timeo
= sock_rcvtimeo(sk
, flags
& O_NONBLOCK
);
552 /* Wait for an incoming connection */
553 add_wait_queue_exclusive(sk
->sk_sleep
, &wait
);
554 while (!(nsk
= iucv_accept_dequeue(sk
, newsock
))) {
555 set_current_state(TASK_INTERRUPTIBLE
);
562 timeo
= schedule_timeout(timeo
);
563 lock_sock_nested(sk
, SINGLE_DEPTH_NESTING
);
565 if (sk
->sk_state
!= IUCV_LISTEN
) {
570 if (signal_pending(current
)) {
571 err
= sock_intr_errno(timeo
);
576 set_current_state(TASK_RUNNING
);
577 remove_wait_queue(sk
->sk_sleep
, &wait
);
582 newsock
->state
= SS_CONNECTED
;
589 static int iucv_sock_getname(struct socket
*sock
, struct sockaddr
*addr
,
592 struct sockaddr_iucv
*siucv
= (struct sockaddr_iucv
*) addr
;
593 struct sock
*sk
= sock
->sk
;
595 addr
->sa_family
= AF_IUCV
;
596 *len
= sizeof(struct sockaddr_iucv
);
599 memcpy(siucv
->siucv_user_id
, iucv_sk(sk
)->dst_user_id
, 8);
600 memcpy(siucv
->siucv_name
, &iucv_sk(sk
)->dst_name
, 8);
602 memcpy(siucv
->siucv_user_id
, iucv_sk(sk
)->src_user_id
, 8);
603 memcpy(siucv
->siucv_name
, iucv_sk(sk
)->src_name
, 8);
605 memset(&siucv
->siucv_port
, 0, sizeof(siucv
->siucv_port
));
606 memset(&siucv
->siucv_addr
, 0, sizeof(siucv
->siucv_addr
));
607 memset(siucv
->siucv_nodeid
, 0, sizeof(siucv
->siucv_nodeid
));
612 static int iucv_sock_sendmsg(struct kiocb
*iocb
, struct socket
*sock
,
613 struct msghdr
*msg
, size_t len
)
615 struct sock
*sk
= sock
->sk
;
616 struct iucv_sock
*iucv
= iucv_sk(sk
);
618 struct iucv_message txmsg
;
621 err
= sock_error(sk
);
625 if (msg
->msg_flags
& MSG_OOB
)
630 if (sk
->sk_shutdown
& SEND_SHUTDOWN
) {
635 if (sk
->sk_state
== IUCV_CONNECTED
) {
636 if (!(skb
= sock_alloc_send_skb(sk
, len
,
637 msg
->msg_flags
& MSG_DONTWAIT
,
641 if (memcpy_fromiovec(skb_put(skb
, len
), msg
->msg_iov
, len
)) {
647 txmsg
.tag
= iucv
->send_tag
++;
648 memcpy(skb
->cb
, &txmsg
.tag
, 4);
649 skb_queue_tail(&iucv
->send_skb_q
, skb
);
650 err
= iucv_message_send(iucv
->path
, &txmsg
, 0, 0,
651 (void *) skb
->data
, skb
->len
);
654 printk(KERN_ERR
"AF_IUCV msg limit exceeded\n");
655 skb_unlink(skb
, &iucv
->send_skb_q
);
675 static int iucv_fragment_skb(struct sock
*sk
, struct sk_buff
*skb
, int len
)
677 int dataleft
, size
, copied
= 0;
678 struct sk_buff
*nskb
;
682 if (dataleft
>= sk
->sk_rcvbuf
/ 4)
683 size
= sk
->sk_rcvbuf
/ 4;
687 nskb
= alloc_skb(size
, GFP_ATOMIC
| GFP_DMA
);
691 memcpy(nskb
->data
, skb
->data
+ copied
, size
);
695 skb_reset_transport_header(nskb
);
696 skb_reset_network_header(nskb
);
699 skb_queue_tail(&iucv_sk(sk
)->backlog_skb_q
, nskb
);
705 static void iucv_process_message(struct sock
*sk
, struct sk_buff
*skb
,
706 struct iucv_path
*path
,
707 struct iucv_message
*msg
)
711 if (msg
->flags
& IPRMDATA
) {
715 rc
= iucv_message_receive(path
, msg
, 0, skb
->data
,
721 if (skb
->truesize
>= sk
->sk_rcvbuf
/ 4) {
722 rc
= iucv_fragment_skb(sk
, skb
, msg
->length
);
726 iucv_path_sever(path
, NULL
);
729 skb
= skb_dequeue(&iucv_sk(sk
)->backlog_skb_q
);
731 skb_reset_transport_header(skb
);
732 skb_reset_network_header(skb
);
733 skb
->len
= msg
->length
;
737 if (sock_queue_rcv_skb(sk
, skb
))
738 skb_queue_head(&iucv_sk(sk
)->backlog_skb_q
, skb
);
741 static void iucv_process_message_q(struct sock
*sk
)
743 struct iucv_sock
*iucv
= iucv_sk(sk
);
745 struct sock_msg_q
*p
, *n
;
747 list_for_each_entry_safe(p
, n
, &iucv
->message_q
.list
, list
) {
748 skb
= alloc_skb(p
->msg
.length
, GFP_ATOMIC
| GFP_DMA
);
751 iucv_process_message(sk
, skb
, p
->path
, &p
->msg
);
754 if (!skb_queue_empty(&iucv
->backlog_skb_q
))
759 static int iucv_sock_recvmsg(struct kiocb
*iocb
, struct socket
*sock
,
760 struct msghdr
*msg
, size_t len
, int flags
)
762 int noblock
= flags
& MSG_DONTWAIT
;
763 struct sock
*sk
= sock
->sk
;
764 struct iucv_sock
*iucv
= iucv_sk(sk
);
765 int target
, copied
= 0;
766 struct sk_buff
*skb
, *rskb
, *cskb
;
769 if ((sk
->sk_state
== IUCV_DISCONN
|| sk
->sk_state
== IUCV_SEVERED
) &&
770 skb_queue_empty(&iucv
->backlog_skb_q
) &&
771 skb_queue_empty(&sk
->sk_receive_queue
) &&
772 list_empty(&iucv
->message_q
.list
))
775 if (flags
& (MSG_OOB
))
778 target
= sock_rcvlowat(sk
, flags
& MSG_WAITALL
, len
);
780 skb
= skb_recv_datagram(sk
, flags
, noblock
, &err
);
782 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
787 copied
= min_t(unsigned int, skb
->len
, len
);
790 if (memcpy_toiovec(msg
->msg_iov
, cskb
->data
, copied
)) {
791 skb_queue_head(&sk
->sk_receive_queue
, skb
);
799 /* Mark read part of skb as used */
800 if (!(flags
& MSG_PEEK
)) {
801 skb_pull(skb
, copied
);
804 skb_queue_head(&sk
->sk_receive_queue
, skb
);
810 /* Queue backlog skbs */
811 rskb
= skb_dequeue(&iucv
->backlog_skb_q
);
813 if (sock_queue_rcv_skb(sk
, rskb
)) {
814 skb_queue_head(&iucv
->backlog_skb_q
,
818 rskb
= skb_dequeue(&iucv
->backlog_skb_q
);
821 if (skb_queue_empty(&iucv
->backlog_skb_q
)) {
822 spin_lock_bh(&iucv
->message_q
.lock
);
823 if (!list_empty(&iucv
->message_q
.list
))
824 iucv_process_message_q(sk
);
825 spin_unlock_bh(&iucv
->message_q
.lock
);
829 skb_queue_head(&sk
->sk_receive_queue
, skb
);
832 return err
? : copied
;
835 static inline unsigned int iucv_accept_poll(struct sock
*parent
)
837 struct iucv_sock
*isk
, *n
;
840 list_for_each_entry_safe(isk
, n
, &iucv_sk(parent
)->accept_q
, accept_q
) {
841 sk
= (struct sock
*) isk
;
843 if (sk
->sk_state
== IUCV_CONNECTED
)
844 return POLLIN
| POLLRDNORM
;
850 unsigned int iucv_sock_poll(struct file
*file
, struct socket
*sock
,
853 struct sock
*sk
= sock
->sk
;
854 unsigned int mask
= 0;
856 poll_wait(file
, sk
->sk_sleep
, wait
);
858 if (sk
->sk_state
== IUCV_LISTEN
)
859 return iucv_accept_poll(sk
);
861 if (sk
->sk_err
|| !skb_queue_empty(&sk
->sk_error_queue
))
864 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
867 if (sk
->sk_shutdown
== SHUTDOWN_MASK
)
870 if (!skb_queue_empty(&sk
->sk_receive_queue
) ||
871 (sk
->sk_shutdown
& RCV_SHUTDOWN
))
872 mask
|= POLLIN
| POLLRDNORM
;
874 if (sk
->sk_state
== IUCV_CLOSED
)
877 if (sk
->sk_state
== IUCV_DISCONN
|| sk
->sk_state
== IUCV_SEVERED
)
880 if (sock_writeable(sk
))
881 mask
|= POLLOUT
| POLLWRNORM
| POLLWRBAND
;
883 set_bit(SOCK_ASYNC_NOSPACE
, &sk
->sk_socket
->flags
);
888 static int iucv_sock_shutdown(struct socket
*sock
, int how
)
890 struct sock
*sk
= sock
->sk
;
891 struct iucv_sock
*iucv
= iucv_sk(sk
);
892 struct iucv_message txmsg
;
894 u8 prmmsg
[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01};
898 if ((how
& ~SHUTDOWN_MASK
) || !how
)
902 switch (sk
->sk_state
) {
908 sk
->sk_shutdown
|= how
;
912 if (how
== SEND_SHUTDOWN
|| how
== SHUTDOWN_MASK
) {
915 err
= iucv_message_send(iucv
->path
, &txmsg
, IUCV_IPRMDATA
, 0,
932 if (how
== RCV_SHUTDOWN
|| how
== SHUTDOWN_MASK
) {
933 err
= iucv_path_quiesce(iucv_sk(sk
)->path
, NULL
);
937 skb_queue_purge(&sk
->sk_receive_queue
);
940 /* Wake up anyone sleeping in poll */
941 sk
->sk_state_change(sk
);
948 static int iucv_sock_release(struct socket
*sock
)
950 struct sock
*sk
= sock
->sk
;
958 /* Unregister with IUCV base support */
959 if (iucv_sk(sk
)->path
) {
960 iucv_path_sever(iucv_sk(sk
)->path
, NULL
);
961 iucv_path_free(iucv_sk(sk
)->path
);
962 iucv_sk(sk
)->path
= NULL
;
970 /* Callback wrappers - called from iucv base support */
971 static int iucv_callback_connreq(struct iucv_path
*path
,
972 u8 ipvmid
[8], u8 ipuser
[16])
974 unsigned char user_data
[16];
975 unsigned char nuser_data
[16];
976 unsigned char src_name
[8];
977 struct hlist_node
*node
;
978 struct sock
*sk
, *nsk
;
979 struct iucv_sock
*iucv
, *niucv
;
982 memcpy(src_name
, ipuser
, 8);
984 /* Find out if this path belongs to af_iucv. */
985 read_lock(&iucv_sk_list
.lock
);
988 sk_for_each(sk
, node
, &iucv_sk_list
.head
)
989 if (sk
->sk_state
== IUCV_LISTEN
&&
990 !memcmp(&iucv_sk(sk
)->src_name
, src_name
, 8)) {
992 * Found a listening socket with
993 * src_name == ipuser[0-7].
998 read_unlock(&iucv_sk_list
.lock
);
1000 /* No socket found, not one of our paths. */
1005 /* Check if parent socket is listening */
1006 low_nmcpy(user_data
, iucv
->src_name
);
1007 high_nmcpy(user_data
, iucv
->dst_name
);
1008 ASCEBC(user_data
, sizeof(user_data
));
1009 if (sk
->sk_state
!= IUCV_LISTEN
) {
1010 err
= iucv_path_sever(path
, user_data
);
1014 /* Check for backlog size */
1015 if (sk_acceptq_is_full(sk
)) {
1016 err
= iucv_path_sever(path
, user_data
);
1020 /* Create the new socket */
1021 nsk
= iucv_sock_alloc(NULL
, SOCK_STREAM
, GFP_ATOMIC
);
1023 err
= iucv_path_sever(path
, user_data
);
1027 niucv
= iucv_sk(nsk
);
1028 iucv_sock_init(nsk
, sk
);
1030 /* Set the new iucv_sock */
1031 memcpy(niucv
->dst_name
, ipuser
+ 8, 8);
1032 EBCASC(niucv
->dst_name
, 8);
1033 memcpy(niucv
->dst_user_id
, ipvmid
, 8);
1034 memcpy(niucv
->src_name
, iucv
->src_name
, 8);
1035 memcpy(niucv
->src_user_id
, iucv
->src_user_id
, 8);
1038 /* Call iucv_accept */
1039 high_nmcpy(nuser_data
, ipuser
+ 8);
1040 memcpy(nuser_data
+ 8, niucv
->src_name
, 8);
1041 ASCEBC(nuser_data
+ 8, 8);
1043 path
->msglim
= IUCV_QUEUELEN_DEFAULT
;
1044 err
= iucv_path_accept(path
, &af_iucv_handler
, nuser_data
, nsk
);
1046 err
= iucv_path_sever(path
, user_data
);
1050 iucv_accept_enqueue(sk
, nsk
);
1052 /* Wake up accept */
1053 nsk
->sk_state
= IUCV_CONNECTED
;
1054 sk
->sk_data_ready(sk
, 1);
1061 static void iucv_callback_connack(struct iucv_path
*path
, u8 ipuser
[16])
1063 struct sock
*sk
= path
->private;
1065 sk
->sk_state
= IUCV_CONNECTED
;
1066 sk
->sk_state_change(sk
);
1069 static void iucv_callback_rx(struct iucv_path
*path
, struct iucv_message
*msg
)
1071 struct sock
*sk
= path
->private;
1072 struct iucv_sock
*iucv
= iucv_sk(sk
);
1073 struct sk_buff
*skb
;
1074 struct sock_msg_q
*save_msg
;
1077 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
1080 if (!list_empty(&iucv
->message_q
.list
) ||
1081 !skb_queue_empty(&iucv
->backlog_skb_q
))
1084 len
= atomic_read(&sk
->sk_rmem_alloc
);
1085 len
+= msg
->length
+ sizeof(struct sk_buff
);
1086 if (len
> sk
->sk_rcvbuf
)
1089 skb
= alloc_skb(msg
->length
, GFP_ATOMIC
| GFP_DMA
);
1093 spin_lock(&iucv
->message_q
.lock
);
1094 iucv_process_message(sk
, skb
, path
, msg
);
1095 spin_unlock(&iucv
->message_q
.lock
);
1100 save_msg
= kzalloc(sizeof(struct sock_msg_q
), GFP_ATOMIC
| GFP_DMA
);
1103 save_msg
->path
= path
;
1104 save_msg
->msg
= *msg
;
1106 spin_lock(&iucv
->message_q
.lock
);
1107 list_add_tail(&save_msg
->list
, &iucv
->message_q
.list
);
1108 spin_unlock(&iucv
->message_q
.lock
);
1111 static void iucv_callback_txdone(struct iucv_path
*path
,
1112 struct iucv_message
*msg
)
1114 struct sock
*sk
= path
->private;
1115 struct sk_buff
*this = NULL
;
1116 struct sk_buff_head
*list
= &iucv_sk(sk
)->send_skb_q
;
1117 struct sk_buff
*list_skb
= list
->next
;
1118 unsigned long flags
;
1120 if (!skb_queue_empty(list
)) {
1121 spin_lock_irqsave(&list
->lock
, flags
);
1123 while (list_skb
!= (struct sk_buff
*)list
) {
1124 if (!memcmp(&msg
->tag
, list_skb
->cb
, 4)) {
1128 list_skb
= list_skb
->next
;
1131 __skb_unlink(this, list
);
1133 spin_unlock_irqrestore(&list
->lock
, flags
);
1139 printk(KERN_ERR
"AF_IUCV msg tag %u not found\n", msg
->tag
);
1141 if (sk
->sk_state
== IUCV_CLOSING
) {
1142 if (skb_queue_empty(&iucv_sk(sk
)->send_skb_q
)) {
1143 sk
->sk_state
= IUCV_CLOSED
;
1144 sk
->sk_state_change(sk
);
1150 static void iucv_callback_connrej(struct iucv_path
*path
, u8 ipuser
[16])
1152 struct sock
*sk
= path
->private;
1154 if (!list_empty(&iucv_sk(sk
)->accept_q
))
1155 sk
->sk_state
= IUCV_SEVERED
;
1157 sk
->sk_state
= IUCV_DISCONN
;
1159 sk
->sk_state_change(sk
);
1162 static struct proto_ops iucv_sock_ops
= {
1164 .owner
= THIS_MODULE
,
1165 .release
= iucv_sock_release
,
1166 .bind
= iucv_sock_bind
,
1167 .connect
= iucv_sock_connect
,
1168 .listen
= iucv_sock_listen
,
1169 .accept
= iucv_sock_accept
,
1170 .getname
= iucv_sock_getname
,
1171 .sendmsg
= iucv_sock_sendmsg
,
1172 .recvmsg
= iucv_sock_recvmsg
,
1173 .poll
= iucv_sock_poll
,
1174 .ioctl
= sock_no_ioctl
,
1175 .mmap
= sock_no_mmap
,
1176 .socketpair
= sock_no_socketpair
,
1177 .shutdown
= iucv_sock_shutdown
,
1178 .setsockopt
= sock_no_setsockopt
,
1179 .getsockopt
= sock_no_getsockopt
1182 static struct net_proto_family iucv_sock_family_ops
= {
1184 .owner
= THIS_MODULE
,
1185 .create
= iucv_sock_create
,
1188 static int __init
afiucv_init(void)
1192 if (!MACHINE_IS_VM
) {
1193 printk(KERN_ERR
"AF_IUCV connection needs VM as base\n");
1194 err
= -EPROTONOSUPPORT
;
1197 cpcmd("QUERY USERID", iucv_userid
, sizeof(iucv_userid
), &err
);
1198 if (unlikely(err
)) {
1199 printk(KERN_ERR
"AF_IUCV needs the VM userid\n");
1200 err
= -EPROTONOSUPPORT
;
1204 err
= iucv_register(&af_iucv_handler
, 0);
1207 err
= proto_register(&iucv_proto
, 0);
1210 err
= sock_register(&iucv_sock_family_ops
);
1213 printk(KERN_INFO
"AF_IUCV lowlevel driver initialized\n");
1217 proto_unregister(&iucv_proto
);
1219 iucv_unregister(&af_iucv_handler
, 0);
1224 static void __exit
afiucv_exit(void)
1226 sock_unregister(PF_IUCV
);
1227 proto_unregister(&iucv_proto
);
1228 iucv_unregister(&af_iucv_handler
, 0);
1230 printk(KERN_INFO
"AF_IUCV lowlevel driver unloaded\n");
1233 module_init(afiucv_init
);
1234 module_exit(afiucv_exit
);
1236 MODULE_AUTHOR("Jennifer Hunt <jenhunt@us.ibm.com>");
1237 MODULE_DESCRIPTION("IUCV Sockets ver " VERSION
);
1238 MODULE_VERSION(VERSION
);
1239 MODULE_LICENSE("GPL");
1240 MODULE_ALIAS_NETPROTO(PF_IUCV
);