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 #define KMSG_COMPONENT "af_iucv"
12 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
14 #include <linux/module.h>
15 #include <linux/types.h>
16 #include <linux/list.h>
17 #include <linux/errno.h>
18 #include <linux/kernel.h>
19 #include <linux/sched.h>
20 #include <linux/slab.h>
21 #include <linux/skbuff.h>
22 #include <linux/init.h>
23 #include <linux/poll.h>
25 #include <asm/ebcdic.h>
26 #include <asm/cpcmd.h>
27 #include <linux/kmod.h>
29 #include <net/iucv/iucv.h>
30 #include <net/iucv/af_iucv.h>
32 #define CONFIG_IUCV_SOCK_DEBUG 1
37 static char iucv_userid
[80];
39 static struct proto_ops iucv_sock_ops
;
41 static struct proto iucv_proto
= {
44 .obj_size
= sizeof(struct iucv_sock
),
47 static void iucv_sock_kill(struct sock
*sk
);
48 static void iucv_sock_close(struct sock
*sk
);
50 /* Call Back functions */
51 static void iucv_callback_rx(struct iucv_path
*, struct iucv_message
*);
52 static void iucv_callback_txdone(struct iucv_path
*, struct iucv_message
*);
53 static void iucv_callback_connack(struct iucv_path
*, u8 ipuser
[16]);
54 static int iucv_callback_connreq(struct iucv_path
*, u8 ipvmid
[8],
56 static void iucv_callback_connrej(struct iucv_path
*, u8 ipuser
[16]);
58 static struct iucv_sock_list iucv_sk_list
= {
59 .lock
= __RW_LOCK_UNLOCKED(iucv_sk_list
.lock
),
60 .autobind_name
= ATOMIC_INIT(0)
63 static struct iucv_handler af_iucv_handler
= {
64 .path_pending
= iucv_callback_connreq
,
65 .path_complete
= iucv_callback_connack
,
66 .path_severed
= iucv_callback_connrej
,
67 .message_pending
= iucv_callback_rx
,
68 .message_complete
= iucv_callback_txdone
71 static inline void high_nmcpy(unsigned char *dst
, char *src
)
76 static inline void low_nmcpy(unsigned char *dst
, char *src
)
78 memcpy(&dst
[8], src
, 8);
82 static void iucv_sock_timeout(unsigned long arg
)
84 struct sock
*sk
= (struct sock
*)arg
;
87 sk
->sk_err
= ETIMEDOUT
;
88 sk
->sk_state_change(sk
);
95 static void iucv_sock_clear_timer(struct sock
*sk
)
97 sk_stop_timer(sk
, &sk
->sk_timer
);
100 static struct sock
*__iucv_get_sock_by_name(char *nm
)
103 struct hlist_node
*node
;
105 sk_for_each(sk
, node
, &iucv_sk_list
.head
)
106 if (!memcmp(&iucv_sk(sk
)->src_name
, nm
, 8))
112 static void iucv_sock_destruct(struct sock
*sk
)
114 skb_queue_purge(&sk
->sk_receive_queue
);
115 skb_queue_purge(&sk
->sk_write_queue
);
119 static void iucv_sock_cleanup_listen(struct sock
*parent
)
123 /* Close non-accepted connections */
124 while ((sk
= iucv_accept_dequeue(parent
, NULL
))) {
129 parent
->sk_state
= IUCV_CLOSED
;
130 sock_set_flag(parent
, SOCK_ZAPPED
);
134 static void iucv_sock_kill(struct sock
*sk
)
136 if (!sock_flag(sk
, SOCK_ZAPPED
) || sk
->sk_socket
)
139 iucv_sock_unlink(&iucv_sk_list
, sk
);
140 sock_set_flag(sk
, SOCK_DEAD
);
144 /* Close an IUCV socket */
145 static void iucv_sock_close(struct sock
*sk
)
147 unsigned char user_data
[16];
148 struct iucv_sock
*iucv
= iucv_sk(sk
);
152 iucv_sock_clear_timer(sk
);
155 switch (sk
->sk_state
) {
157 iucv_sock_cleanup_listen(sk
);
164 sk
->sk_state
= IUCV_CLOSING
;
165 sk
->sk_state_change(sk
);
167 if (!skb_queue_empty(&iucv
->send_skb_q
)) {
168 if (sock_flag(sk
, SOCK_LINGER
) && sk
->sk_lingertime
)
169 timeo
= sk
->sk_lingertime
;
171 timeo
= IUCV_DISCONN_TIMEOUT
;
172 err
= iucv_sock_wait_state(sk
, IUCV_CLOSED
, 0, timeo
);
175 case IUCV_CLOSING
: /* fall through */
176 sk
->sk_state
= IUCV_CLOSED
;
177 sk
->sk_state_change(sk
);
180 low_nmcpy(user_data
, iucv
->src_name
);
181 high_nmcpy(user_data
, iucv
->dst_name
);
182 ASCEBC(user_data
, sizeof(user_data
));
183 err
= iucv_path_sever(iucv
->path
, user_data
);
184 iucv_path_free(iucv
->path
);
188 sk
->sk_err
= ECONNRESET
;
189 sk
->sk_state_change(sk
);
191 skb_queue_purge(&iucv
->send_skb_q
);
192 skb_queue_purge(&iucv
->backlog_skb_q
);
194 sock_set_flag(sk
, SOCK_ZAPPED
);
198 sock_set_flag(sk
, SOCK_ZAPPED
);
206 static void iucv_sock_init(struct sock
*sk
, struct sock
*parent
)
209 sk
->sk_type
= parent
->sk_type
;
212 static struct sock
*iucv_sock_alloc(struct socket
*sock
, int proto
, gfp_t prio
)
216 sk
= sk_alloc(&init_net
, PF_IUCV
, prio
, &iucv_proto
);
220 sock_init_data(sock
, sk
);
221 INIT_LIST_HEAD(&iucv_sk(sk
)->accept_q
);
222 spin_lock_init(&iucv_sk(sk
)->accept_q_lock
);
223 skb_queue_head_init(&iucv_sk(sk
)->send_skb_q
);
224 INIT_LIST_HEAD(&iucv_sk(sk
)->message_q
.list
);
225 spin_lock_init(&iucv_sk(sk
)->message_q
.lock
);
226 skb_queue_head_init(&iucv_sk(sk
)->backlog_skb_q
);
227 iucv_sk(sk
)->send_tag
= 0;
228 iucv_sk(sk
)->path
= NULL
;
229 memset(&iucv_sk(sk
)->src_user_id
, 0, 32);
231 sk
->sk_destruct
= iucv_sock_destruct
;
232 sk
->sk_sndtimeo
= IUCV_CONN_TIMEOUT
;
233 sk
->sk_allocation
= GFP_DMA
;
235 sock_reset_flag(sk
, SOCK_ZAPPED
);
237 sk
->sk_protocol
= proto
;
238 sk
->sk_state
= IUCV_OPEN
;
240 setup_timer(&sk
->sk_timer
, iucv_sock_timeout
, (unsigned long)sk
);
242 iucv_sock_link(&iucv_sk_list
, sk
);
246 /* Create an IUCV socket */
247 static int iucv_sock_create(struct net
*net
, struct socket
*sock
, int protocol
)
251 if (sock
->type
!= SOCK_STREAM
)
252 return -ESOCKTNOSUPPORT
;
254 sock
->state
= SS_UNCONNECTED
;
255 sock
->ops
= &iucv_sock_ops
;
257 sk
= iucv_sock_alloc(sock
, protocol
, GFP_KERNEL
);
261 iucv_sock_init(sk
, NULL
);
266 void iucv_sock_link(struct iucv_sock_list
*l
, struct sock
*sk
)
268 write_lock_bh(&l
->lock
);
269 sk_add_node(sk
, &l
->head
);
270 write_unlock_bh(&l
->lock
);
273 void iucv_sock_unlink(struct iucv_sock_list
*l
, struct sock
*sk
)
275 write_lock_bh(&l
->lock
);
276 sk_del_node_init(sk
);
277 write_unlock_bh(&l
->lock
);
280 void iucv_accept_enqueue(struct sock
*parent
, struct sock
*sk
)
283 struct iucv_sock
*par
= iucv_sk(parent
);
286 spin_lock_irqsave(&par
->accept_q_lock
, flags
);
287 list_add_tail(&iucv_sk(sk
)->accept_q
, &par
->accept_q
);
288 spin_unlock_irqrestore(&par
->accept_q_lock
, flags
);
289 iucv_sk(sk
)->parent
= parent
;
290 parent
->sk_ack_backlog
++;
293 void iucv_accept_unlink(struct sock
*sk
)
296 struct iucv_sock
*par
= iucv_sk(iucv_sk(sk
)->parent
);
298 spin_lock_irqsave(&par
->accept_q_lock
, flags
);
299 list_del_init(&iucv_sk(sk
)->accept_q
);
300 spin_unlock_irqrestore(&par
->accept_q_lock
, flags
);
301 iucv_sk(sk
)->parent
->sk_ack_backlog
--;
302 iucv_sk(sk
)->parent
= NULL
;
306 struct sock
*iucv_accept_dequeue(struct sock
*parent
, struct socket
*newsock
)
308 struct iucv_sock
*isk
, *n
;
311 list_for_each_entry_safe(isk
, n
, &iucv_sk(parent
)->accept_q
, accept_q
) {
312 sk
= (struct sock
*) isk
;
315 if (sk
->sk_state
== IUCV_CLOSED
) {
316 iucv_accept_unlink(sk
);
321 if (sk
->sk_state
== IUCV_CONNECTED
||
322 sk
->sk_state
== IUCV_SEVERED
||
324 iucv_accept_unlink(sk
);
326 sock_graft(sk
, newsock
);
328 if (sk
->sk_state
== IUCV_SEVERED
)
329 sk
->sk_state
= IUCV_DISCONN
;
340 int iucv_sock_wait_state(struct sock
*sk
, int state
, int state2
,
343 DECLARE_WAITQUEUE(wait
, current
);
346 add_wait_queue(sk
->sk_sleep
, &wait
);
347 while (sk
->sk_state
!= state
&& sk
->sk_state
!= state2
) {
348 set_current_state(TASK_INTERRUPTIBLE
);
355 if (signal_pending(current
)) {
356 err
= sock_intr_errno(timeo
);
361 timeo
= schedule_timeout(timeo
);
364 err
= sock_error(sk
);
368 set_current_state(TASK_RUNNING
);
369 remove_wait_queue(sk
->sk_sleep
, &wait
);
373 /* Bind an unbound socket */
374 static int iucv_sock_bind(struct socket
*sock
, struct sockaddr
*addr
,
377 struct sockaddr_iucv
*sa
= (struct sockaddr_iucv
*) addr
;
378 struct sock
*sk
= sock
->sk
;
379 struct iucv_sock
*iucv
;
382 /* Verify the input sockaddr */
383 if (!addr
|| addr
->sa_family
!= AF_IUCV
)
387 if (sk
->sk_state
!= IUCV_OPEN
) {
392 write_lock_bh(&iucv_sk_list
.lock
);
395 if (__iucv_get_sock_by_name(sa
->siucv_name
)) {
404 /* Bind the socket */
405 memcpy(iucv
->src_name
, sa
->siucv_name
, 8);
407 /* Copy the user id */
408 memcpy(iucv
->src_user_id
, iucv_userid
, 8);
409 sk
->sk_state
= IUCV_BOUND
;
413 /* Release the socket list lock */
414 write_unlock_bh(&iucv_sk_list
.lock
);
420 /* Automatically bind an unbound socket */
421 static int iucv_sock_autobind(struct sock
*sk
)
423 struct iucv_sock
*iucv
= iucv_sk(sk
);
424 char query_buffer
[80];
428 /* Set the userid and name */
429 cpcmd("QUERY USERID", query_buffer
, sizeof(query_buffer
), &err
);
433 memcpy(iucv
->src_user_id
, query_buffer
, 8);
435 write_lock_bh(&iucv_sk_list
.lock
);
437 sprintf(name
, "%08x", atomic_inc_return(&iucv_sk_list
.autobind_name
));
438 while (__iucv_get_sock_by_name(name
)) {
439 sprintf(name
, "%08x",
440 atomic_inc_return(&iucv_sk_list
.autobind_name
));
443 write_unlock_bh(&iucv_sk_list
.lock
);
445 memcpy(&iucv
->src_name
, name
, 8);
450 /* Connect an unconnected socket */
451 static int iucv_sock_connect(struct socket
*sock
, struct sockaddr
*addr
,
454 struct sockaddr_iucv
*sa
= (struct sockaddr_iucv
*) addr
;
455 struct sock
*sk
= sock
->sk
;
456 struct iucv_sock
*iucv
;
457 unsigned char user_data
[16];
460 if (addr
->sa_family
!= AF_IUCV
|| alen
< sizeof(struct sockaddr_iucv
))
463 if (sk
->sk_state
!= IUCV_OPEN
&& sk
->sk_state
!= IUCV_BOUND
)
466 if (sk
->sk_type
!= SOCK_STREAM
)
471 if (sk
->sk_state
== IUCV_OPEN
) {
472 err
= iucv_sock_autobind(sk
);
479 /* Set the destination information */
480 memcpy(iucv_sk(sk
)->dst_user_id
, sa
->siucv_user_id
, 8);
481 memcpy(iucv_sk(sk
)->dst_name
, sa
->siucv_name
, 8);
483 high_nmcpy(user_data
, sa
->siucv_name
);
484 low_nmcpy(user_data
, iucv_sk(sk
)->src_name
);
485 ASCEBC(user_data
, sizeof(user_data
));
489 iucv
->path
= iucv_path_alloc(IUCV_QUEUELEN_DEFAULT
,
490 IPRMDATA
, GFP_KERNEL
);
495 err
= iucv_path_connect(iucv
->path
, &af_iucv_handler
,
496 sa
->siucv_user_id
, NULL
, user_data
, sk
);
498 iucv_path_free(iucv
->path
);
501 case 0x0b: /* Target communicator is not logged on */
504 case 0x0d: /* Max connections for this guest exceeded */
505 case 0x0e: /* Max connections for target guest exceeded */
508 case 0x0f: /* Missing IUCV authorization */
518 if (sk
->sk_state
!= IUCV_CONNECTED
) {
519 err
= iucv_sock_wait_state(sk
, IUCV_CONNECTED
, IUCV_DISCONN
,
520 sock_sndtimeo(sk
, flags
& O_NONBLOCK
));
523 if (sk
->sk_state
== IUCV_DISCONN
) {
525 return -ECONNREFUSED
;
529 iucv_path_sever(iucv
->path
, NULL
);
530 iucv_path_free(iucv
->path
);
539 /* Move a socket into listening state. */
540 static int iucv_sock_listen(struct socket
*sock
, int backlog
)
542 struct sock
*sk
= sock
->sk
;
548 if (sk
->sk_state
!= IUCV_BOUND
|| sock
->type
!= SOCK_STREAM
)
551 sk
->sk_max_ack_backlog
= backlog
;
552 sk
->sk_ack_backlog
= 0;
553 sk
->sk_state
= IUCV_LISTEN
;
561 /* Accept a pending connection */
562 static int iucv_sock_accept(struct socket
*sock
, struct socket
*newsock
,
565 DECLARE_WAITQUEUE(wait
, current
);
566 struct sock
*sk
= sock
->sk
, *nsk
;
570 lock_sock_nested(sk
, SINGLE_DEPTH_NESTING
);
572 if (sk
->sk_state
!= IUCV_LISTEN
) {
577 timeo
= sock_rcvtimeo(sk
, flags
& O_NONBLOCK
);
579 /* Wait for an incoming connection */
580 add_wait_queue_exclusive(sk
->sk_sleep
, &wait
);
581 while (!(nsk
= iucv_accept_dequeue(sk
, newsock
))) {
582 set_current_state(TASK_INTERRUPTIBLE
);
589 timeo
= schedule_timeout(timeo
);
590 lock_sock_nested(sk
, SINGLE_DEPTH_NESTING
);
592 if (sk
->sk_state
!= IUCV_LISTEN
) {
597 if (signal_pending(current
)) {
598 err
= sock_intr_errno(timeo
);
603 set_current_state(TASK_RUNNING
);
604 remove_wait_queue(sk
->sk_sleep
, &wait
);
609 newsock
->state
= SS_CONNECTED
;
616 static int iucv_sock_getname(struct socket
*sock
, struct sockaddr
*addr
,
619 struct sockaddr_iucv
*siucv
= (struct sockaddr_iucv
*) addr
;
620 struct sock
*sk
= sock
->sk
;
622 addr
->sa_family
= AF_IUCV
;
623 *len
= sizeof(struct sockaddr_iucv
);
626 memcpy(siucv
->siucv_user_id
, iucv_sk(sk
)->dst_user_id
, 8);
627 memcpy(siucv
->siucv_name
, &iucv_sk(sk
)->dst_name
, 8);
629 memcpy(siucv
->siucv_user_id
, iucv_sk(sk
)->src_user_id
, 8);
630 memcpy(siucv
->siucv_name
, iucv_sk(sk
)->src_name
, 8);
632 memset(&siucv
->siucv_port
, 0, sizeof(siucv
->siucv_port
));
633 memset(&siucv
->siucv_addr
, 0, sizeof(siucv
->siucv_addr
));
634 memset(siucv
->siucv_nodeid
, 0, sizeof(siucv
->siucv_nodeid
));
639 static int iucv_sock_sendmsg(struct kiocb
*iocb
, struct socket
*sock
,
640 struct msghdr
*msg
, size_t len
)
642 struct sock
*sk
= sock
->sk
;
643 struct iucv_sock
*iucv
= iucv_sk(sk
);
645 struct iucv_message txmsg
;
650 err
= sock_error(sk
);
654 if (msg
->msg_flags
& MSG_OOB
)
659 if (sk
->sk_shutdown
& SEND_SHUTDOWN
) {
664 if (sk
->sk_state
== IUCV_CONNECTED
) {
665 if (!(skb
= sock_alloc_send_skb(sk
, len
,
666 msg
->msg_flags
& MSG_DONTWAIT
,
670 if (memcpy_fromiovec(skb_put(skb
, len
), msg
->msg_iov
, len
)) {
676 memcpy(&txmsg
.class, skb
->data
, skb
->len
>= 4 ? 4 : skb
->len
);
677 txmsg
.tag
= iucv
->send_tag
++;
678 memcpy(skb
->cb
, &txmsg
.tag
, 4);
679 skb_queue_tail(&iucv
->send_skb_q
, skb
);
680 err
= iucv_message_send(iucv
->path
, &txmsg
, 0, 0,
681 (void *) skb
->data
, skb
->len
);
685 memcpy(user_id
, iucv
->dst_user_id
, 8);
687 memcpy(appl_id
, iucv
->dst_name
, 8);
688 pr_err("Application %s on z/VM guest %s"
689 " exceeds message limit\n",
692 skb_unlink(skb
, &iucv
->send_skb_q
);
712 static int iucv_fragment_skb(struct sock
*sk
, struct sk_buff
*skb
, int len
)
714 int dataleft
, size
, copied
= 0;
715 struct sk_buff
*nskb
;
719 if (dataleft
>= sk
->sk_rcvbuf
/ 4)
720 size
= sk
->sk_rcvbuf
/ 4;
724 nskb
= alloc_skb(size
, GFP_ATOMIC
| GFP_DMA
);
728 memcpy(nskb
->data
, skb
->data
+ copied
, size
);
732 skb_reset_transport_header(nskb
);
733 skb_reset_network_header(nskb
);
736 skb_queue_tail(&iucv_sk(sk
)->backlog_skb_q
, nskb
);
742 static void iucv_process_message(struct sock
*sk
, struct sk_buff
*skb
,
743 struct iucv_path
*path
,
744 struct iucv_message
*msg
)
748 if (msg
->flags
& IPRMDATA
) {
752 rc
= iucv_message_receive(path
, msg
, 0, skb
->data
,
758 if (skb
->truesize
>= sk
->sk_rcvbuf
/ 4) {
759 rc
= iucv_fragment_skb(sk
, skb
, msg
->length
);
763 iucv_path_sever(path
, NULL
);
766 skb
= skb_dequeue(&iucv_sk(sk
)->backlog_skb_q
);
768 skb_reset_transport_header(skb
);
769 skb_reset_network_header(skb
);
770 skb
->len
= msg
->length
;
774 if (sock_queue_rcv_skb(sk
, skb
))
775 skb_queue_head(&iucv_sk(sk
)->backlog_skb_q
, skb
);
778 static void iucv_process_message_q(struct sock
*sk
)
780 struct iucv_sock
*iucv
= iucv_sk(sk
);
782 struct sock_msg_q
*p
, *n
;
784 list_for_each_entry_safe(p
, n
, &iucv
->message_q
.list
, list
) {
785 skb
= alloc_skb(p
->msg
.length
, GFP_ATOMIC
| GFP_DMA
);
788 iucv_process_message(sk
, skb
, p
->path
, &p
->msg
);
791 if (!skb_queue_empty(&iucv
->backlog_skb_q
))
796 static int iucv_sock_recvmsg(struct kiocb
*iocb
, struct socket
*sock
,
797 struct msghdr
*msg
, size_t len
, int flags
)
799 int noblock
= flags
& MSG_DONTWAIT
;
800 struct sock
*sk
= sock
->sk
;
801 struct iucv_sock
*iucv
= iucv_sk(sk
);
802 int target
, copied
= 0;
803 struct sk_buff
*skb
, *rskb
, *cskb
;
806 if ((sk
->sk_state
== IUCV_DISCONN
|| sk
->sk_state
== IUCV_SEVERED
) &&
807 skb_queue_empty(&iucv
->backlog_skb_q
) &&
808 skb_queue_empty(&sk
->sk_receive_queue
) &&
809 list_empty(&iucv
->message_q
.list
))
812 if (flags
& (MSG_OOB
))
815 target
= sock_rcvlowat(sk
, flags
& MSG_WAITALL
, len
);
817 /* receive/dequeue next skb:
818 * the function understands MSG_PEEK and, thus, does not dequeue skb */
819 skb
= skb_recv_datagram(sk
, flags
, noblock
, &err
);
821 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
826 copied
= min_t(unsigned int, skb
->len
, len
);
829 if (memcpy_toiovec(msg
->msg_iov
, cskb
->data
, copied
)) {
830 skb_queue_head(&sk
->sk_receive_queue
, skb
);
838 /* Mark read part of skb as used */
839 if (!(flags
& MSG_PEEK
)) {
840 skb_pull(skb
, copied
);
843 skb_queue_head(&sk
->sk_receive_queue
, skb
);
849 /* Queue backlog skbs */
850 rskb
= skb_dequeue(&iucv
->backlog_skb_q
);
852 if (sock_queue_rcv_skb(sk
, rskb
)) {
853 skb_queue_head(&iucv
->backlog_skb_q
,
857 rskb
= skb_dequeue(&iucv
->backlog_skb_q
);
860 if (skb_queue_empty(&iucv
->backlog_skb_q
)) {
861 spin_lock_bh(&iucv
->message_q
.lock
);
862 if (!list_empty(&iucv
->message_q
.list
))
863 iucv_process_message_q(sk
);
864 spin_unlock_bh(&iucv
->message_q
.lock
);
869 return err
? : copied
;
872 static inline unsigned int iucv_accept_poll(struct sock
*parent
)
874 struct iucv_sock
*isk
, *n
;
877 list_for_each_entry_safe(isk
, n
, &iucv_sk(parent
)->accept_q
, accept_q
) {
878 sk
= (struct sock
*) isk
;
880 if (sk
->sk_state
== IUCV_CONNECTED
)
881 return POLLIN
| POLLRDNORM
;
887 unsigned int iucv_sock_poll(struct file
*file
, struct socket
*sock
,
890 struct sock
*sk
= sock
->sk
;
891 unsigned int mask
= 0;
893 poll_wait(file
, sk
->sk_sleep
, wait
);
895 if (sk
->sk_state
== IUCV_LISTEN
)
896 return iucv_accept_poll(sk
);
898 if (sk
->sk_err
|| !skb_queue_empty(&sk
->sk_error_queue
))
901 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
904 if (sk
->sk_shutdown
== SHUTDOWN_MASK
)
907 if (!skb_queue_empty(&sk
->sk_receive_queue
) ||
908 (sk
->sk_shutdown
& RCV_SHUTDOWN
))
909 mask
|= POLLIN
| POLLRDNORM
;
911 if (sk
->sk_state
== IUCV_CLOSED
)
914 if (sk
->sk_state
== IUCV_DISCONN
|| sk
->sk_state
== IUCV_SEVERED
)
917 if (sock_writeable(sk
))
918 mask
|= POLLOUT
| POLLWRNORM
| POLLWRBAND
;
920 set_bit(SOCK_ASYNC_NOSPACE
, &sk
->sk_socket
->flags
);
925 static int iucv_sock_shutdown(struct socket
*sock
, int how
)
927 struct sock
*sk
= sock
->sk
;
928 struct iucv_sock
*iucv
= iucv_sk(sk
);
929 struct iucv_message txmsg
;
931 u8 prmmsg
[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01};
935 if ((how
& ~SHUTDOWN_MASK
) || !how
)
939 switch (sk
->sk_state
) {
948 sk
->sk_shutdown
|= how
;
952 if (how
== SEND_SHUTDOWN
|| how
== SHUTDOWN_MASK
) {
955 err
= iucv_message_send(iucv
->path
, &txmsg
, IUCV_IPRMDATA
, 0,
972 if (how
== RCV_SHUTDOWN
|| how
== SHUTDOWN_MASK
) {
973 err
= iucv_path_quiesce(iucv_sk(sk
)->path
, NULL
);
977 skb_queue_purge(&sk
->sk_receive_queue
);
980 /* Wake up anyone sleeping in poll */
981 sk
->sk_state_change(sk
);
988 static int iucv_sock_release(struct socket
*sock
)
990 struct sock
*sk
= sock
->sk
;
998 /* Unregister with IUCV base support */
999 if (iucv_sk(sk
)->path
) {
1000 iucv_path_sever(iucv_sk(sk
)->path
, NULL
);
1001 iucv_path_free(iucv_sk(sk
)->path
);
1002 iucv_sk(sk
)->path
= NULL
;
1010 /* Callback wrappers - called from iucv base support */
1011 static int iucv_callback_connreq(struct iucv_path
*path
,
1012 u8 ipvmid
[8], u8 ipuser
[16])
1014 unsigned char user_data
[16];
1015 unsigned char nuser_data
[16];
1016 unsigned char src_name
[8];
1017 struct hlist_node
*node
;
1018 struct sock
*sk
, *nsk
;
1019 struct iucv_sock
*iucv
, *niucv
;
1022 memcpy(src_name
, ipuser
, 8);
1023 EBCASC(src_name
, 8);
1024 /* Find out if this path belongs to af_iucv. */
1025 read_lock(&iucv_sk_list
.lock
);
1028 sk_for_each(sk
, node
, &iucv_sk_list
.head
)
1029 if (sk
->sk_state
== IUCV_LISTEN
&&
1030 !memcmp(&iucv_sk(sk
)->src_name
, src_name
, 8)) {
1032 * Found a listening socket with
1033 * src_name == ipuser[0-7].
1038 read_unlock(&iucv_sk_list
.lock
);
1040 /* No socket found, not one of our paths. */
1045 /* Check if parent socket is listening */
1046 low_nmcpy(user_data
, iucv
->src_name
);
1047 high_nmcpy(user_data
, iucv
->dst_name
);
1048 ASCEBC(user_data
, sizeof(user_data
));
1049 if (sk
->sk_state
!= IUCV_LISTEN
) {
1050 err
= iucv_path_sever(path
, user_data
);
1051 iucv_path_free(path
);
1055 /* Check for backlog size */
1056 if (sk_acceptq_is_full(sk
)) {
1057 err
= iucv_path_sever(path
, user_data
);
1058 iucv_path_free(path
);
1062 /* Create the new socket */
1063 nsk
= iucv_sock_alloc(NULL
, SOCK_STREAM
, GFP_ATOMIC
);
1065 err
= iucv_path_sever(path
, user_data
);
1066 iucv_path_free(path
);
1070 niucv
= iucv_sk(nsk
);
1071 iucv_sock_init(nsk
, sk
);
1073 /* Set the new iucv_sock */
1074 memcpy(niucv
->dst_name
, ipuser
+ 8, 8);
1075 EBCASC(niucv
->dst_name
, 8);
1076 memcpy(niucv
->dst_user_id
, ipvmid
, 8);
1077 memcpy(niucv
->src_name
, iucv
->src_name
, 8);
1078 memcpy(niucv
->src_user_id
, iucv
->src_user_id
, 8);
1081 /* Call iucv_accept */
1082 high_nmcpy(nuser_data
, ipuser
+ 8);
1083 memcpy(nuser_data
+ 8, niucv
->src_name
, 8);
1084 ASCEBC(nuser_data
+ 8, 8);
1086 path
->msglim
= IUCV_QUEUELEN_DEFAULT
;
1087 err
= iucv_path_accept(path
, &af_iucv_handler
, nuser_data
, nsk
);
1089 err
= iucv_path_sever(path
, user_data
);
1090 iucv_path_free(path
);
1091 iucv_sock_kill(nsk
);
1095 iucv_accept_enqueue(sk
, nsk
);
1097 /* Wake up accept */
1098 nsk
->sk_state
= IUCV_CONNECTED
;
1099 sk
->sk_data_ready(sk
, 1);
1106 static void iucv_callback_connack(struct iucv_path
*path
, u8 ipuser
[16])
1108 struct sock
*sk
= path
->private;
1110 sk
->sk_state
= IUCV_CONNECTED
;
1111 sk
->sk_state_change(sk
);
1114 static void iucv_callback_rx(struct iucv_path
*path
, struct iucv_message
*msg
)
1116 struct sock
*sk
= path
->private;
1117 struct iucv_sock
*iucv
= iucv_sk(sk
);
1118 struct sk_buff
*skb
;
1119 struct sock_msg_q
*save_msg
;
1122 if (sk
->sk_shutdown
& RCV_SHUTDOWN
) {
1123 iucv_message_reject(path
, msg
);
1127 spin_lock(&iucv
->message_q
.lock
);
1129 if (!list_empty(&iucv
->message_q
.list
) ||
1130 !skb_queue_empty(&iucv
->backlog_skb_q
))
1133 len
= atomic_read(&sk
->sk_rmem_alloc
);
1134 len
+= msg
->length
+ sizeof(struct sk_buff
);
1135 if (len
> sk
->sk_rcvbuf
)
1138 skb
= alloc_skb(msg
->length
, GFP_ATOMIC
| GFP_DMA
);
1142 iucv_process_message(sk
, skb
, path
, msg
);
1148 save_msg
= kzalloc(sizeof(struct sock_msg_q
), GFP_ATOMIC
| GFP_DMA
);
1151 save_msg
->path
= path
;
1152 save_msg
->msg
= *msg
;
1154 list_add_tail(&save_msg
->list
, &iucv
->message_q
.list
);
1157 spin_unlock(&iucv
->message_q
.lock
);
1160 static void iucv_callback_txdone(struct iucv_path
*path
,
1161 struct iucv_message
*msg
)
1163 struct sock
*sk
= path
->private;
1164 struct sk_buff
*this = NULL
;
1165 struct sk_buff_head
*list
= &iucv_sk(sk
)->send_skb_q
;
1166 struct sk_buff
*list_skb
= list
->next
;
1167 unsigned long flags
;
1169 if (!skb_queue_empty(list
)) {
1170 spin_lock_irqsave(&list
->lock
, flags
);
1172 while (list_skb
!= (struct sk_buff
*)list
) {
1173 if (!memcmp(&msg
->tag
, list_skb
->cb
, 4)) {
1177 list_skb
= list_skb
->next
;
1180 __skb_unlink(this, list
);
1182 spin_unlock_irqrestore(&list
->lock
, flags
);
1188 if (sk
->sk_state
== IUCV_CLOSING
) {
1189 if (skb_queue_empty(&iucv_sk(sk
)->send_skb_q
)) {
1190 sk
->sk_state
= IUCV_CLOSED
;
1191 sk
->sk_state_change(sk
);
1197 static void iucv_callback_connrej(struct iucv_path
*path
, u8 ipuser
[16])
1199 struct sock
*sk
= path
->private;
1201 if (!list_empty(&iucv_sk(sk
)->accept_q
))
1202 sk
->sk_state
= IUCV_SEVERED
;
1204 sk
->sk_state
= IUCV_DISCONN
;
1206 sk
->sk_state_change(sk
);
1209 static struct proto_ops iucv_sock_ops
= {
1211 .owner
= THIS_MODULE
,
1212 .release
= iucv_sock_release
,
1213 .bind
= iucv_sock_bind
,
1214 .connect
= iucv_sock_connect
,
1215 .listen
= iucv_sock_listen
,
1216 .accept
= iucv_sock_accept
,
1217 .getname
= iucv_sock_getname
,
1218 .sendmsg
= iucv_sock_sendmsg
,
1219 .recvmsg
= iucv_sock_recvmsg
,
1220 .poll
= iucv_sock_poll
,
1221 .ioctl
= sock_no_ioctl
,
1222 .mmap
= sock_no_mmap
,
1223 .socketpair
= sock_no_socketpair
,
1224 .shutdown
= iucv_sock_shutdown
,
1225 .setsockopt
= sock_no_setsockopt
,
1226 .getsockopt
= sock_no_getsockopt
1229 static struct net_proto_family iucv_sock_family_ops
= {
1231 .owner
= THIS_MODULE
,
1232 .create
= iucv_sock_create
,
1235 static int __init
afiucv_init(void)
1239 if (!MACHINE_IS_VM
) {
1240 pr_err("The af_iucv module cannot be loaded"
1242 err
= -EPROTONOSUPPORT
;
1245 cpcmd("QUERY USERID", iucv_userid
, sizeof(iucv_userid
), &err
);
1246 if (unlikely(err
)) {
1248 err
= -EPROTONOSUPPORT
;
1252 err
= iucv_register(&af_iucv_handler
, 0);
1255 err
= proto_register(&iucv_proto
, 0);
1258 err
= sock_register(&iucv_sock_family_ops
);
1264 proto_unregister(&iucv_proto
);
1266 iucv_unregister(&af_iucv_handler
, 0);
1271 static void __exit
afiucv_exit(void)
1273 sock_unregister(PF_IUCV
);
1274 proto_unregister(&iucv_proto
);
1275 iucv_unregister(&af_iucv_handler
, 0);
1278 module_init(afiucv_init
);
1279 module_exit(afiucv_exit
);
1281 MODULE_AUTHOR("Jennifer Hunt <jenhunt@us.ibm.com>");
1282 MODULE_DESCRIPTION("IUCV Sockets ver " VERSION
);
1283 MODULE_VERSION(VERSION
);
1284 MODULE_LICENSE("GPL");
1285 MODULE_ALIAS_NETPROTO(PF_IUCV
);