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 sk->sk_state = IUCV_CLOSED;
176 sk->sk_state_change(sk);
179 low_nmcpy(user_data, iucv->src_name);
180 high_nmcpy(user_data, iucv->dst_name);
181 ASCEBC(user_data, sizeof(user_data));
182 err = iucv_path_sever(iucv->path, user_data);
183 iucv_path_free(iucv->path);
187 sk->sk_err = ECONNRESET;
188 sk->sk_state_change(sk);
190 skb_queue_purge(&iucv->send_skb_q);
191 skb_queue_purge(&iucv->backlog_skb_q);
193 sock_set_flag(sk, SOCK_ZAPPED);
197 sock_set_flag(sk, SOCK_ZAPPED);
205 static void iucv_sock_init(struct sock *sk, struct sock *parent)
208 sk->sk_type = parent->sk_type;
211 static struct sock *iucv_sock_alloc(struct socket *sock, int proto, gfp_t prio)
215 sk = sk_alloc(&init_net, PF_IUCV, prio, &iucv_proto);
219 sock_init_data(sock, sk);
220 INIT_LIST_HEAD(&iucv_sk(sk)->accept_q);
221 spin_lock_init(&iucv_sk(sk)->accept_q_lock);
222 skb_queue_head_init(&iucv_sk(sk)->send_skb_q);
223 INIT_LIST_HEAD(&iucv_sk(sk)->message_q.list);
224 spin_lock_init(&iucv_sk(sk)->message_q.lock);
225 skb_queue_head_init(&iucv_sk(sk)->backlog_skb_q);
226 iucv_sk(sk)->send_tag = 0;
228 sk->sk_destruct = iucv_sock_destruct;
229 sk->sk_sndtimeo = IUCV_CONN_TIMEOUT;
230 sk->sk_allocation = GFP_DMA;
232 sock_reset_flag(sk, SOCK_ZAPPED);
234 sk->sk_protocol = proto;
235 sk->sk_state = IUCV_OPEN;
237 setup_timer(&sk->sk_timer, iucv_sock_timeout, (unsigned long)sk);
239 iucv_sock_link(&iucv_sk_list, sk);
243 /* Create an IUCV socket */
244 static int iucv_sock_create(struct net *net, struct socket *sock, int protocol)
248 if (sock->type != SOCK_STREAM)
249 return -ESOCKTNOSUPPORT;
251 sock->state = SS_UNCONNECTED;
252 sock->ops = &iucv_sock_ops;
254 sk = iucv_sock_alloc(sock, protocol, GFP_KERNEL);
258 iucv_sock_init(sk, NULL);
263 void iucv_sock_link(struct iucv_sock_list *l, struct sock *sk)
265 write_lock_bh(&l->lock);
266 sk_add_node(sk, &l->head);
267 write_unlock_bh(&l->lock);
270 void iucv_sock_unlink(struct iucv_sock_list *l, struct sock *sk)
272 write_lock_bh(&l->lock);
273 sk_del_node_init(sk);
274 write_unlock_bh(&l->lock);
277 void iucv_accept_enqueue(struct sock *parent, struct sock *sk)
280 struct iucv_sock *par = iucv_sk(parent);
283 spin_lock_irqsave(&par->accept_q_lock, flags);
284 list_add_tail(&iucv_sk(sk)->accept_q, &par->accept_q);
285 spin_unlock_irqrestore(&par->accept_q_lock, flags);
286 iucv_sk(sk)->parent = parent;
287 parent->sk_ack_backlog++;
290 void iucv_accept_unlink(struct sock *sk)
293 struct iucv_sock *par = iucv_sk(iucv_sk(sk)->parent);
295 spin_lock_irqsave(&par->accept_q_lock, flags);
296 list_del_init(&iucv_sk(sk)->accept_q);
297 spin_unlock_irqrestore(&par->accept_q_lock, flags);
298 iucv_sk(sk)->parent->sk_ack_backlog--;
299 iucv_sk(sk)->parent = NULL;
303 struct sock *iucv_accept_dequeue(struct sock *parent, struct socket *newsock)
305 struct iucv_sock *isk, *n;
308 list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) {
309 sk = (struct sock *) isk;
312 if (sk->sk_state == IUCV_CLOSED) {
313 iucv_accept_unlink(sk);
318 if (sk->sk_state == IUCV_CONNECTED ||
319 sk->sk_state == IUCV_SEVERED ||
321 iucv_accept_unlink(sk);
323 sock_graft(sk, newsock);
325 if (sk->sk_state == IUCV_SEVERED)
326 sk->sk_state = IUCV_DISCONN;
337 int iucv_sock_wait_state(struct sock *sk, int state, int state2,
340 DECLARE_WAITQUEUE(wait, current);
343 add_wait_queue(sk->sk_sleep, &wait);
344 while (sk->sk_state != state && sk->sk_state != state2) {
345 set_current_state(TASK_INTERRUPTIBLE);
352 if (signal_pending(current)) {
353 err = sock_intr_errno(timeo);
358 timeo = schedule_timeout(timeo);
361 err = sock_error(sk);
365 set_current_state(TASK_RUNNING);
366 remove_wait_queue(sk->sk_sleep, &wait);
370 /* Bind an unbound socket */
371 static int iucv_sock_bind(struct socket *sock, struct sockaddr *addr,
374 struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
375 struct sock *sk = sock->sk;
376 struct iucv_sock *iucv;
379 /* Verify the input sockaddr */
380 if (!addr || addr->sa_family != AF_IUCV)
384 if (sk->sk_state != IUCV_OPEN) {
389 write_lock_bh(&iucv_sk_list.lock);
392 if (__iucv_get_sock_by_name(sa->siucv_name)) {
401 /* Bind the socket */
402 memcpy(iucv->src_name, sa->siucv_name, 8);
404 /* Copy the user id */
405 memcpy(iucv->src_user_id, iucv_userid, 8);
406 sk->sk_state = IUCV_BOUND;
410 /* Release the socket list lock */
411 write_unlock_bh(&iucv_sk_list.lock);
417 /* Automatically bind an unbound socket */
418 static int iucv_sock_autobind(struct sock *sk)
420 struct iucv_sock *iucv = iucv_sk(sk);
421 char query_buffer[80];
425 /* Set the userid and name */
426 cpcmd("QUERY USERID", query_buffer, sizeof(query_buffer), &err);
430 memcpy(iucv->src_user_id, query_buffer, 8);
432 write_lock_bh(&iucv_sk_list.lock);
434 sprintf(name, "%08x", atomic_inc_return(&iucv_sk_list.autobind_name));
435 while (__iucv_get_sock_by_name(name)) {
436 sprintf(name, "%08x",
437 atomic_inc_return(&iucv_sk_list.autobind_name));
440 write_unlock_bh(&iucv_sk_list.lock);
442 memcpy(&iucv->src_name, name, 8);
447 /* Connect an unconnected socket */
448 static int iucv_sock_connect(struct socket *sock, struct sockaddr *addr,
451 struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
452 struct sock *sk = sock->sk;
453 struct iucv_sock *iucv;
454 unsigned char user_data[16];
457 if (addr->sa_family != AF_IUCV || alen < sizeof(struct sockaddr_iucv))
460 if (sk->sk_state != IUCV_OPEN && sk->sk_state != IUCV_BOUND)
463 if (sk->sk_type != SOCK_STREAM)
468 if (sk->sk_state == IUCV_OPEN) {
469 err = iucv_sock_autobind(sk);
476 /* Set the destination information */
477 memcpy(iucv_sk(sk)->dst_user_id, sa->siucv_user_id, 8);
478 memcpy(iucv_sk(sk)->dst_name, sa->siucv_name, 8);
480 high_nmcpy(user_data, sa->siucv_name);
481 low_nmcpy(user_data, iucv_sk(sk)->src_name);
482 ASCEBC(user_data, sizeof(user_data));
486 iucv->path = iucv_path_alloc(IUCV_QUEUELEN_DEFAULT,
487 IPRMDATA, GFP_KERNEL);
492 err = iucv_path_connect(iucv->path, &af_iucv_handler,
493 sa->siucv_user_id, NULL, user_data, sk);
495 iucv_path_free(iucv->path);
498 case 0x0b: /* Target communicator is not logged on */
501 case 0x0d: /* Max connections for this guest exceeded */
502 case 0x0e: /* Max connections for target guest exceeded */
505 case 0x0f: /* Missing IUCV authorization */
515 if (sk->sk_state != IUCV_CONNECTED) {
516 err = iucv_sock_wait_state(sk, IUCV_CONNECTED, IUCV_DISCONN,
517 sock_sndtimeo(sk, flags & O_NONBLOCK));
520 if (sk->sk_state == IUCV_DISCONN) {
522 return -ECONNREFUSED;
526 iucv_path_sever(iucv->path, NULL);
527 iucv_path_free(iucv->path);
536 /* Move a socket into listening state. */
537 static int iucv_sock_listen(struct socket *sock, int backlog)
539 struct sock *sk = sock->sk;
545 if (sk->sk_state != IUCV_BOUND || sock->type != SOCK_STREAM)
548 sk->sk_max_ack_backlog = backlog;
549 sk->sk_ack_backlog = 0;
550 sk->sk_state = IUCV_LISTEN;
558 /* Accept a pending connection */
559 static int iucv_sock_accept(struct socket *sock, struct socket *newsock,
562 DECLARE_WAITQUEUE(wait, current);
563 struct sock *sk = sock->sk, *nsk;
567 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
569 if (sk->sk_state != IUCV_LISTEN) {
574 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
576 /* Wait for an incoming connection */
577 add_wait_queue_exclusive(sk->sk_sleep, &wait);
578 while (!(nsk = iucv_accept_dequeue(sk, newsock))) {
579 set_current_state(TASK_INTERRUPTIBLE);
586 timeo = schedule_timeout(timeo);
587 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
589 if (sk->sk_state != IUCV_LISTEN) {
594 if (signal_pending(current)) {
595 err = sock_intr_errno(timeo);
600 set_current_state(TASK_RUNNING);
601 remove_wait_queue(sk->sk_sleep, &wait);
606 newsock->state = SS_CONNECTED;
613 static int iucv_sock_getname(struct socket *sock, struct sockaddr *addr,
616 struct sockaddr_iucv *siucv = (struct sockaddr_iucv *) addr;
617 struct sock *sk = sock->sk;
619 addr->sa_family = AF_IUCV;
620 *len = sizeof(struct sockaddr_iucv);
623 memcpy(siucv->siucv_user_id, iucv_sk(sk)->dst_user_id, 8);
624 memcpy(siucv->siucv_name, &iucv_sk(sk)->dst_name, 8);
626 memcpy(siucv->siucv_user_id, iucv_sk(sk)->src_user_id, 8);
627 memcpy(siucv->siucv_name, iucv_sk(sk)->src_name, 8);
629 memset(&siucv->siucv_port, 0, sizeof(siucv->siucv_port));
630 memset(&siucv->siucv_addr, 0, sizeof(siucv->siucv_addr));
631 memset(siucv->siucv_nodeid, 0, sizeof(siucv->siucv_nodeid));
636 static int iucv_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
637 struct msghdr *msg, size_t len)
639 struct sock *sk = sock->sk;
640 struct iucv_sock *iucv = iucv_sk(sk);
642 struct iucv_message txmsg;
647 err = sock_error(sk);
651 if (msg->msg_flags & MSG_OOB)
656 if (sk->sk_shutdown & SEND_SHUTDOWN) {
661 if (sk->sk_state == IUCV_CONNECTED) {
662 if (!(skb = sock_alloc_send_skb(sk, len,
663 msg->msg_flags & MSG_DONTWAIT,
667 if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
673 memcpy(&txmsg.class, skb->data, skb->len >= 4 ? 4 : skb->len);
674 txmsg.tag = iucv->send_tag++;
675 memcpy(skb->cb, &txmsg.tag, 4);
676 skb_queue_tail(&iucv->send_skb_q, skb);
677 err = iucv_message_send(iucv->path, &txmsg, 0, 0,
678 (void *) skb->data, skb->len);
682 memcpy(user_id, iucv->dst_user_id, 8);
684 memcpy(appl_id, iucv->dst_name, 8);
685 pr_err("Application %s on z/VM guest %s"
686 " exceeds message limit\n",
689 skb_unlink(skb, &iucv->send_skb_q);
709 static int iucv_fragment_skb(struct sock *sk, struct sk_buff *skb, int len)
711 int dataleft, size, copied = 0;
712 struct sk_buff *nskb;
716 if (dataleft >= sk->sk_rcvbuf / 4)
717 size = sk->sk_rcvbuf / 4;
721 nskb = alloc_skb(size, GFP_ATOMIC | GFP_DMA);
725 memcpy(nskb->data, skb->data + copied, size);
729 skb_reset_transport_header(nskb);
730 skb_reset_network_header(nskb);
733 skb_queue_tail(&iucv_sk(sk)->backlog_skb_q, nskb);
739 static void iucv_process_message(struct sock *sk, struct sk_buff *skb,
740 struct iucv_path *path,
741 struct iucv_message *msg)
745 if (msg->flags & IPRMDATA) {
749 rc = iucv_message_receive(path, msg, 0, skb->data,
755 if (skb->truesize >= sk->sk_rcvbuf / 4) {
756 rc = iucv_fragment_skb(sk, skb, msg->length);
760 iucv_path_sever(path, NULL);
763 skb = skb_dequeue(&iucv_sk(sk)->backlog_skb_q);
765 skb_reset_transport_header(skb);
766 skb_reset_network_header(skb);
767 skb->len = msg->length;
771 if (sock_queue_rcv_skb(sk, skb))
772 skb_queue_head(&iucv_sk(sk)->backlog_skb_q, skb);
775 static void iucv_process_message_q(struct sock *sk)
777 struct iucv_sock *iucv = iucv_sk(sk);
779 struct sock_msg_q *p, *n;
781 list_for_each_entry_safe(p, n, &iucv->message_q.list, list) {
782 skb = alloc_skb(p->msg.length, GFP_ATOMIC | GFP_DMA);
785 iucv_process_message(sk, skb, p->path, &p->msg);
788 if (!skb_queue_empty(&iucv->backlog_skb_q))
793 static int iucv_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
794 struct msghdr *msg, size_t len, int flags)
796 int noblock = flags & MSG_DONTWAIT;
797 struct sock *sk = sock->sk;
798 struct iucv_sock *iucv = iucv_sk(sk);
799 int target, copied = 0;
800 struct sk_buff *skb, *rskb, *cskb;
803 if ((sk->sk_state == IUCV_DISCONN || sk->sk_state == IUCV_SEVERED) &&
804 skb_queue_empty(&iucv->backlog_skb_q) &&
805 skb_queue_empty(&sk->sk_receive_queue) &&
806 list_empty(&iucv->message_q.list))
809 if (flags & (MSG_OOB))
812 target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
814 skb = skb_recv_datagram(sk, flags, noblock, &err);
816 if (sk->sk_shutdown & RCV_SHUTDOWN)
821 copied = min_t(unsigned int, skb->len, len);
824 if (memcpy_toiovec(msg->msg_iov, cskb->data, copied)) {
825 skb_queue_head(&sk->sk_receive_queue, skb);
833 /* Mark read part of skb as used */
834 if (!(flags & MSG_PEEK)) {
835 skb_pull(skb, copied);
838 skb_queue_head(&sk->sk_receive_queue, skb);
844 /* Queue backlog skbs */
845 rskb = skb_dequeue(&iucv->backlog_skb_q);
847 if (sock_queue_rcv_skb(sk, rskb)) {
848 skb_queue_head(&iucv->backlog_skb_q,
852 rskb = skb_dequeue(&iucv->backlog_skb_q);
855 if (skb_queue_empty(&iucv->backlog_skb_q)) {
856 spin_lock_bh(&iucv->message_q.lock);
857 if (!list_empty(&iucv->message_q.list))
858 iucv_process_message_q(sk);
859 spin_unlock_bh(&iucv->message_q.lock);
863 skb_queue_head(&sk->sk_receive_queue, skb);
866 return err ? : copied;
869 static inline unsigned int iucv_accept_poll(struct sock *parent)
871 struct iucv_sock *isk, *n;
874 list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) {
875 sk = (struct sock *) isk;
877 if (sk->sk_state == IUCV_CONNECTED)
878 return POLLIN | POLLRDNORM;
884 unsigned int iucv_sock_poll(struct file *file, struct socket *sock,
887 struct sock *sk = sock->sk;
888 unsigned int mask = 0;
890 poll_wait(file, sk->sk_sleep, wait);
892 if (sk->sk_state == IUCV_LISTEN)
893 return iucv_accept_poll(sk);
895 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
898 if (sk->sk_shutdown & RCV_SHUTDOWN)
901 if (sk->sk_shutdown == SHUTDOWN_MASK)
904 if (!skb_queue_empty(&sk->sk_receive_queue) ||
905 (sk->sk_shutdown & RCV_SHUTDOWN))
906 mask |= POLLIN | POLLRDNORM;
908 if (sk->sk_state == IUCV_CLOSED)
911 if (sk->sk_state == IUCV_DISCONN || sk->sk_state == IUCV_SEVERED)
914 if (sock_writeable(sk))
915 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
917 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
922 static int iucv_sock_shutdown(struct socket *sock, int how)
924 struct sock *sk = sock->sk;
925 struct iucv_sock *iucv = iucv_sk(sk);
926 struct iucv_message txmsg;
928 u8 prmmsg[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01};
932 if ((how & ~SHUTDOWN_MASK) || !how)
936 switch (sk->sk_state) {
942 sk->sk_shutdown |= how;
946 if (how == SEND_SHUTDOWN || how == SHUTDOWN_MASK) {
949 err = iucv_message_send(iucv->path, &txmsg, IUCV_IPRMDATA, 0,
966 if (how == RCV_SHUTDOWN || how == SHUTDOWN_MASK) {
967 err = iucv_path_quiesce(iucv_sk(sk)->path, NULL);
971 skb_queue_purge(&sk->sk_receive_queue);
974 /* Wake up anyone sleeping in poll */
975 sk->sk_state_change(sk);
982 static int iucv_sock_release(struct socket *sock)
984 struct sock *sk = sock->sk;
992 /* Unregister with IUCV base support */
993 if (iucv_sk(sk)->path) {
994 iucv_path_sever(iucv_sk(sk)->path, NULL);
995 iucv_path_free(iucv_sk(sk)->path);
996 iucv_sk(sk)->path = NULL;
1004 /* Callback wrappers - called from iucv base support */
1005 static int iucv_callback_connreq(struct iucv_path *path,
1006 u8 ipvmid[8], u8 ipuser[16])
1008 unsigned char user_data[16];
1009 unsigned char nuser_data[16];
1010 unsigned char src_name[8];
1011 struct hlist_node *node;
1012 struct sock *sk, *nsk;
1013 struct iucv_sock *iucv, *niucv;
1016 memcpy(src_name, ipuser, 8);
1017 EBCASC(src_name, 8);
1018 /* Find out if this path belongs to af_iucv. */
1019 read_lock(&iucv_sk_list.lock);
1022 sk_for_each(sk, node, &iucv_sk_list.head)
1023 if (sk->sk_state == IUCV_LISTEN &&
1024 !memcmp(&iucv_sk(sk)->src_name, src_name, 8)) {
1026 * Found a listening socket with
1027 * src_name == ipuser[0-7].
1032 read_unlock(&iucv_sk_list.lock);
1034 /* No socket found, not one of our paths. */
1039 /* Check if parent socket is listening */
1040 low_nmcpy(user_data, iucv->src_name);
1041 high_nmcpy(user_data, iucv->dst_name);
1042 ASCEBC(user_data, sizeof(user_data));
1043 if (sk->sk_state != IUCV_LISTEN) {
1044 err = iucv_path_sever(path, user_data);
1045 iucv_path_free(path);
1049 /* Check for backlog size */
1050 if (sk_acceptq_is_full(sk)) {
1051 err = iucv_path_sever(path, user_data);
1052 iucv_path_free(path);
1056 /* Create the new socket */
1057 nsk = iucv_sock_alloc(NULL, SOCK_STREAM, GFP_ATOMIC);
1059 err = iucv_path_sever(path, user_data);
1060 iucv_path_free(path);
1064 niucv = iucv_sk(nsk);
1065 iucv_sock_init(nsk, sk);
1067 /* Set the new iucv_sock */
1068 memcpy(niucv->dst_name, ipuser + 8, 8);
1069 EBCASC(niucv->dst_name, 8);
1070 memcpy(niucv->dst_user_id, ipvmid, 8);
1071 memcpy(niucv->src_name, iucv->src_name, 8);
1072 memcpy(niucv->src_user_id, iucv->src_user_id, 8);
1075 /* Call iucv_accept */
1076 high_nmcpy(nuser_data, ipuser + 8);
1077 memcpy(nuser_data + 8, niucv->src_name, 8);
1078 ASCEBC(nuser_data + 8, 8);
1080 path->msglim = IUCV_QUEUELEN_DEFAULT;
1081 err = iucv_path_accept(path, &af_iucv_handler, nuser_data, nsk);
1083 err = iucv_path_sever(path, user_data);
1084 iucv_path_free(path);
1085 iucv_sock_kill(nsk);
1089 iucv_accept_enqueue(sk, nsk);
1091 /* Wake up accept */
1092 nsk->sk_state = IUCV_CONNECTED;
1093 sk->sk_data_ready(sk, 1);
1100 static void iucv_callback_connack(struct iucv_path *path, u8 ipuser[16])
1102 struct sock *sk = path->private;
1104 sk->sk_state = IUCV_CONNECTED;
1105 sk->sk_state_change(sk);
1108 static void iucv_callback_rx(struct iucv_path *path, struct iucv_message *msg)
1110 struct sock *sk = path->private;
1111 struct iucv_sock *iucv = iucv_sk(sk);
1112 struct sk_buff *skb;
1113 struct sock_msg_q *save_msg;
1116 if (sk->sk_shutdown & RCV_SHUTDOWN)
1119 if (!list_empty(&iucv->message_q.list) ||
1120 !skb_queue_empty(&iucv->backlog_skb_q))
1123 len = atomic_read(&sk->sk_rmem_alloc);
1124 len += msg->length + sizeof(struct sk_buff);
1125 if (len > sk->sk_rcvbuf)
1128 skb = alloc_skb(msg->length, GFP_ATOMIC | GFP_DMA);
1132 spin_lock(&iucv->message_q.lock);
1133 iucv_process_message(sk, skb, path, msg);
1134 spin_unlock(&iucv->message_q.lock);
1139 save_msg = kzalloc(sizeof(struct sock_msg_q), GFP_ATOMIC | GFP_DMA);
1142 save_msg->path = path;
1143 save_msg->msg = *msg;
1145 spin_lock(&iucv->message_q.lock);
1146 list_add_tail(&save_msg->list, &iucv->message_q.list);
1147 spin_unlock(&iucv->message_q.lock);
1150 static void iucv_callback_txdone(struct iucv_path *path,
1151 struct iucv_message *msg)
1153 struct sock *sk = path->private;
1154 struct sk_buff *this = NULL;
1155 struct sk_buff_head *list = &iucv_sk(sk)->send_skb_q;
1156 struct sk_buff *list_skb = list->next;
1157 unsigned long flags;
1159 if (!skb_queue_empty(list)) {
1160 spin_lock_irqsave(&list->lock, flags);
1162 while (list_skb != (struct sk_buff *)list) {
1163 if (!memcmp(&msg->tag, list_skb->cb, 4)) {
1167 list_skb = list_skb->next;
1170 __skb_unlink(this, list);
1172 spin_unlock_irqrestore(&list->lock, flags);
1178 if (sk->sk_state == IUCV_CLOSING) {
1179 if (skb_queue_empty(&iucv_sk(sk)->send_skb_q)) {
1180 sk->sk_state = IUCV_CLOSED;
1181 sk->sk_state_change(sk);
1187 static void iucv_callback_connrej(struct iucv_path *path, u8 ipuser[16])
1189 struct sock *sk = path->private;
1191 if (!list_empty(&iucv_sk(sk)->accept_q))
1192 sk->sk_state = IUCV_SEVERED;
1194 sk->sk_state = IUCV_DISCONN;
1196 sk->sk_state_change(sk);
1199 static struct proto_ops iucv_sock_ops = {
1201 .owner = THIS_MODULE,
1202 .release = iucv_sock_release,
1203 .bind = iucv_sock_bind,
1204 .connect = iucv_sock_connect,
1205 .listen = iucv_sock_listen,
1206 .accept = iucv_sock_accept,
1207 .getname = iucv_sock_getname,
1208 .sendmsg = iucv_sock_sendmsg,
1209 .recvmsg = iucv_sock_recvmsg,
1210 .poll = iucv_sock_poll,
1211 .ioctl = sock_no_ioctl,
1212 .mmap = sock_no_mmap,
1213 .socketpair = sock_no_socketpair,
1214 .shutdown = iucv_sock_shutdown,
1215 .setsockopt = sock_no_setsockopt,
1216 .getsockopt = sock_no_getsockopt
1219 static struct net_proto_family iucv_sock_family_ops = {
1221 .owner = THIS_MODULE,
1222 .create = iucv_sock_create,
1225 static int __init afiucv_init(void)
1229 if (!MACHINE_IS_VM) {
1230 pr_err("The af_iucv module cannot be loaded"
1232 err = -EPROTONOSUPPORT;
1235 cpcmd("QUERY USERID", iucv_userid, sizeof(iucv_userid), &err);
1236 if (unlikely(err)) {
1238 err = -EPROTONOSUPPORT;
1242 err = iucv_register(&af_iucv_handler, 0);
1245 err = proto_register(&iucv_proto, 0);
1248 err = sock_register(&iucv_sock_family_ops);
1254 proto_unregister(&iucv_proto);
1256 iucv_unregister(&af_iucv_handler, 0);
1261 static void __exit afiucv_exit(void)
1263 sock_unregister(PF_IUCV);
1264 proto_unregister(&iucv_proto);
1265 iucv_unregister(&af_iucv_handler, 0);
1268 module_init(afiucv_init);
1269 module_exit(afiucv_exit);
1271 MODULE_AUTHOR("Jennifer Hunt <jenhunt@us.ibm.com>");
1272 MODULE_DESCRIPTION("IUCV Sockets ver " VERSION);
1273 MODULE_VERSION(VERSION);
1274 MODULE_LICENSE("GPL");
1275 MODULE_ALIAS_NETPROTO(PF_IUCV);