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 void iucv_sock_init_timer(struct sock *sk)
99 init_timer(&sk->sk_timer);
100 sk->sk_timer.function = iucv_sock_timeout;
101 sk->sk_timer.data = (unsigned long)sk;
104 static struct sock *__iucv_get_sock_by_name(char *nm)
107 struct hlist_node *node;
109 sk_for_each(sk, node, &iucv_sk_list.head)
110 if (!memcmp(&iucv_sk(sk)->src_name, nm, 8))
116 static void iucv_sock_destruct(struct sock *sk)
118 skb_queue_purge(&sk->sk_receive_queue);
119 skb_queue_purge(&sk->sk_write_queue);
123 static void iucv_sock_cleanup_listen(struct sock *parent)
127 /* Close non-accepted connections */
128 while ((sk = iucv_accept_dequeue(parent, NULL))) {
133 parent->sk_state = IUCV_CLOSED;
134 sock_set_flag(parent, SOCK_ZAPPED);
138 static void iucv_sock_kill(struct sock *sk)
140 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
143 iucv_sock_unlink(&iucv_sk_list, sk);
144 sock_set_flag(sk, SOCK_DEAD);
148 /* Close an IUCV socket */
149 static void iucv_sock_close(struct sock *sk)
151 unsigned char user_data[16];
152 struct iucv_sock *iucv = iucv_sk(sk);
156 iucv_sock_clear_timer(sk);
159 switch (sk->sk_state) {
161 iucv_sock_cleanup_listen(sk);
168 sk->sk_state = IUCV_CLOSING;
169 sk->sk_state_change(sk);
171 if (!skb_queue_empty(&iucv->send_skb_q)) {
172 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
173 timeo = sk->sk_lingertime;
175 timeo = IUCV_DISCONN_TIMEOUT;
176 err = iucv_sock_wait_state(sk, IUCV_CLOSED, 0, timeo);
179 sk->sk_state = IUCV_CLOSED;
180 sk->sk_state_change(sk);
183 low_nmcpy(user_data, iucv->src_name);
184 high_nmcpy(user_data, iucv->dst_name);
185 ASCEBC(user_data, sizeof(user_data));
186 err = iucv_path_sever(iucv->path, user_data);
187 iucv_path_free(iucv->path);
191 sk->sk_err = ECONNRESET;
192 sk->sk_state_change(sk);
194 skb_queue_purge(&iucv->send_skb_q);
195 skb_queue_purge(&iucv->backlog_skb_q);
197 sock_set_flag(sk, SOCK_ZAPPED);
201 sock_set_flag(sk, SOCK_ZAPPED);
209 static void iucv_sock_init(struct sock *sk, struct sock *parent)
212 sk->sk_type = parent->sk_type;
215 static struct sock *iucv_sock_alloc(struct socket *sock, int proto, gfp_t prio)
219 sk = sk_alloc(&init_net, PF_IUCV, prio, &iucv_proto, 1);
223 sock_init_data(sock, sk);
224 INIT_LIST_HEAD(&iucv_sk(sk)->accept_q);
225 spin_lock_init(&iucv_sk(sk)->accept_q_lock);
226 skb_queue_head_init(&iucv_sk(sk)->send_skb_q);
227 INIT_LIST_HEAD(&iucv_sk(sk)->message_q.list);
228 spin_lock_init(&iucv_sk(sk)->message_q.lock);
229 skb_queue_head_init(&iucv_sk(sk)->backlog_skb_q);
230 iucv_sk(sk)->send_tag = 0;
232 sk->sk_destruct = iucv_sock_destruct;
233 sk->sk_sndtimeo = IUCV_CONN_TIMEOUT;
234 sk->sk_allocation = GFP_DMA;
236 sock_reset_flag(sk, SOCK_ZAPPED);
238 sk->sk_protocol = proto;
239 sk->sk_state = IUCV_OPEN;
241 iucv_sock_init_timer(sk);
243 iucv_sock_link(&iucv_sk_list, sk);
247 /* Create an IUCV socket */
248 static int iucv_sock_create(struct net *net, struct socket *sock, int protocol)
252 if (sock->type != SOCK_STREAM)
253 return -ESOCKTNOSUPPORT;
255 sock->state = SS_UNCONNECTED;
256 sock->ops = &iucv_sock_ops;
258 sk = iucv_sock_alloc(sock, protocol, GFP_KERNEL);
262 iucv_sock_init(sk, NULL);
267 void iucv_sock_link(struct iucv_sock_list *l, struct sock *sk)
269 write_lock_bh(&l->lock);
270 sk_add_node(sk, &l->head);
271 write_unlock_bh(&l->lock);
274 void iucv_sock_unlink(struct iucv_sock_list *l, struct sock *sk)
276 write_lock_bh(&l->lock);
277 sk_del_node_init(sk);
278 write_unlock_bh(&l->lock);
281 void iucv_accept_enqueue(struct sock *parent, struct sock *sk)
284 struct iucv_sock *par = iucv_sk(parent);
287 spin_lock_irqsave(&par->accept_q_lock, flags);
288 list_add_tail(&iucv_sk(sk)->accept_q, &par->accept_q);
289 spin_unlock_irqrestore(&par->accept_q_lock, flags);
290 iucv_sk(sk)->parent = parent;
291 parent->sk_ack_backlog++;
294 void iucv_accept_unlink(struct sock *sk)
297 struct iucv_sock *par = iucv_sk(iucv_sk(sk)->parent);
299 spin_lock_irqsave(&par->accept_q_lock, flags);
300 list_del_init(&iucv_sk(sk)->accept_q);
301 spin_unlock_irqrestore(&par->accept_q_lock, flags);
302 iucv_sk(sk)->parent->sk_ack_backlog--;
303 iucv_sk(sk)->parent = NULL;
307 struct sock *iucv_accept_dequeue(struct sock *parent, struct socket *newsock)
309 struct iucv_sock *isk, *n;
312 list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) {
313 sk = (struct sock *) isk;
316 if (sk->sk_state == IUCV_CLOSED) {
317 iucv_accept_unlink(sk);
322 if (sk->sk_state == IUCV_CONNECTED ||
323 sk->sk_state == IUCV_SEVERED ||
325 iucv_accept_unlink(sk);
327 sock_graft(sk, newsock);
329 if (sk->sk_state == IUCV_SEVERED)
330 sk->sk_state = IUCV_DISCONN;
341 int iucv_sock_wait_state(struct sock *sk, int state, int state2,
344 DECLARE_WAITQUEUE(wait, current);
347 add_wait_queue(sk->sk_sleep, &wait);
348 while (sk->sk_state != state && sk->sk_state != state2) {
349 set_current_state(TASK_INTERRUPTIBLE);
356 if (signal_pending(current)) {
357 err = sock_intr_errno(timeo);
362 timeo = schedule_timeout(timeo);
365 err = sock_error(sk);
369 set_current_state(TASK_RUNNING);
370 remove_wait_queue(sk->sk_sleep, &wait);
374 /* Bind an unbound socket */
375 static int iucv_sock_bind(struct socket *sock, struct sockaddr *addr,
378 struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
379 struct sock *sk = sock->sk;
380 struct iucv_sock *iucv;
383 /* Verify the input sockaddr */
384 if (!addr || addr->sa_family != AF_IUCV)
388 if (sk->sk_state != IUCV_OPEN) {
393 write_lock_bh(&iucv_sk_list.lock);
396 if (__iucv_get_sock_by_name(sa->siucv_name)) {
405 /* Bind the socket */
406 memcpy(iucv->src_name, sa->siucv_name, 8);
408 /* Copy the user id */
409 memcpy(iucv->src_user_id, iucv_userid, 8);
410 sk->sk_state = IUCV_BOUND;
414 /* Release the socket list lock */
415 write_unlock_bh(&iucv_sk_list.lock);
421 /* Automatically bind an unbound socket */
422 static int iucv_sock_autobind(struct sock *sk)
424 struct iucv_sock *iucv = iucv_sk(sk);
425 char query_buffer[80];
429 /* Set the userid and name */
430 cpcmd("QUERY USERID", query_buffer, sizeof(query_buffer), &err);
434 memcpy(iucv->src_user_id, query_buffer, 8);
436 write_lock_bh(&iucv_sk_list.lock);
438 sprintf(name, "%08x", atomic_inc_return(&iucv_sk_list.autobind_name));
439 while (__iucv_get_sock_by_name(name)) {
440 sprintf(name, "%08x",
441 atomic_inc_return(&iucv_sk_list.autobind_name));
444 write_unlock_bh(&iucv_sk_list.lock);
446 memcpy(&iucv->src_name, name, 8);
451 /* Connect an unconnected socket */
452 static int iucv_sock_connect(struct socket *sock, struct sockaddr *addr,
455 struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
456 struct sock *sk = sock->sk;
457 struct iucv_sock *iucv;
458 unsigned char user_data[16];
461 if (addr->sa_family != AF_IUCV || alen < sizeof(struct sockaddr_iucv))
464 if (sk->sk_state != IUCV_OPEN && sk->sk_state != IUCV_BOUND)
467 if (sk->sk_type != SOCK_STREAM)
472 if (sk->sk_state == IUCV_OPEN) {
473 err = iucv_sock_autobind(sk);
480 /* Set the destination information */
481 memcpy(iucv_sk(sk)->dst_user_id, sa->siucv_user_id, 8);
482 memcpy(iucv_sk(sk)->dst_name, sa->siucv_name, 8);
484 high_nmcpy(user_data, sa->siucv_name);
485 low_nmcpy(user_data, iucv_sk(sk)->src_name);
486 ASCEBC(user_data, sizeof(user_data));
490 iucv->path = iucv_path_alloc(IUCV_QUEUELEN_DEFAULT,
491 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);
501 if (sk->sk_state != IUCV_CONNECTED) {
502 err = iucv_sock_wait_state(sk, IUCV_CONNECTED, IUCV_DISCONN,
503 sock_sndtimeo(sk, flags & O_NONBLOCK));
506 if (sk->sk_state == IUCV_DISCONN) {
508 return -ECONNREFUSED;
515 /* Move a socket into listening state. */
516 static int iucv_sock_listen(struct socket *sock, int backlog)
518 struct sock *sk = sock->sk;
524 if (sk->sk_state != IUCV_BOUND || sock->type != SOCK_STREAM)
527 sk->sk_max_ack_backlog = backlog;
528 sk->sk_ack_backlog = 0;
529 sk->sk_state = IUCV_LISTEN;
537 /* Accept a pending connection */
538 static int iucv_sock_accept(struct socket *sock, struct socket *newsock,
541 DECLARE_WAITQUEUE(wait, current);
542 struct sock *sk = sock->sk, *nsk;
546 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
548 if (sk->sk_state != IUCV_LISTEN) {
553 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
555 /* Wait for an incoming connection */
556 add_wait_queue_exclusive(sk->sk_sleep, &wait);
557 while (!(nsk = iucv_accept_dequeue(sk, newsock))) {
558 set_current_state(TASK_INTERRUPTIBLE);
565 timeo = schedule_timeout(timeo);
566 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
568 if (sk->sk_state != IUCV_LISTEN) {
573 if (signal_pending(current)) {
574 err = sock_intr_errno(timeo);
579 set_current_state(TASK_RUNNING);
580 remove_wait_queue(sk->sk_sleep, &wait);
585 newsock->state = SS_CONNECTED;
592 static int iucv_sock_getname(struct socket *sock, struct sockaddr *addr,
595 struct sockaddr_iucv *siucv = (struct sockaddr_iucv *) addr;
596 struct sock *sk = sock->sk;
598 addr->sa_family = AF_IUCV;
599 *len = sizeof(struct sockaddr_iucv);
602 memcpy(siucv->siucv_user_id, iucv_sk(sk)->dst_user_id, 8);
603 memcpy(siucv->siucv_name, &iucv_sk(sk)->dst_name, 8);
605 memcpy(siucv->siucv_user_id, iucv_sk(sk)->src_user_id, 8);
606 memcpy(siucv->siucv_name, iucv_sk(sk)->src_name, 8);
608 memset(&siucv->siucv_port, 0, sizeof(siucv->siucv_port));
609 memset(&siucv->siucv_addr, 0, sizeof(siucv->siucv_addr));
610 memset(siucv->siucv_nodeid, 0, sizeof(siucv->siucv_nodeid));
615 static int iucv_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
616 struct msghdr *msg, size_t len)
618 struct sock *sk = sock->sk;
619 struct iucv_sock *iucv = iucv_sk(sk);
621 struct iucv_message txmsg;
624 err = sock_error(sk);
628 if (msg->msg_flags & MSG_OOB)
633 if (sk->sk_shutdown & SEND_SHUTDOWN) {
638 if (sk->sk_state == IUCV_CONNECTED) {
639 if (!(skb = sock_alloc_send_skb(sk, len,
640 msg->msg_flags & MSG_DONTWAIT,
644 if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
650 txmsg.tag = iucv->send_tag++;
651 memcpy(skb->cb, &txmsg.tag, 4);
652 skb_queue_tail(&iucv->send_skb_q, skb);
653 err = iucv_message_send(iucv->path, &txmsg, 0, 0,
654 (void *) skb->data, skb->len);
657 printk(KERN_ERR "AF_IUCV msg limit exceeded\n");
658 skb_unlink(skb, &iucv->send_skb_q);
678 static int iucv_fragment_skb(struct sock *sk, struct sk_buff *skb, int len)
680 int dataleft, size, copied = 0;
681 struct sk_buff *nskb;
685 if (dataleft >= sk->sk_rcvbuf / 4)
686 size = sk->sk_rcvbuf / 4;
690 nskb = alloc_skb(size, GFP_ATOMIC | GFP_DMA);
694 memcpy(nskb->data, skb->data + copied, size);
698 skb_reset_transport_header(nskb);
699 skb_reset_network_header(nskb);
702 skb_queue_tail(&iucv_sk(sk)->backlog_skb_q, nskb);
708 static void iucv_process_message(struct sock *sk, struct sk_buff *skb,
709 struct iucv_path *path,
710 struct iucv_message *msg)
714 if (msg->flags & IPRMDATA) {
718 rc = iucv_message_receive(path, msg, 0, skb->data,
724 if (skb->truesize >= sk->sk_rcvbuf / 4) {
725 rc = iucv_fragment_skb(sk, skb, msg->length);
729 iucv_path_sever(path, NULL);
732 skb = skb_dequeue(&iucv_sk(sk)->backlog_skb_q);
734 skb_reset_transport_header(skb);
735 skb_reset_network_header(skb);
736 skb->len = msg->length;
740 if (sock_queue_rcv_skb(sk, skb))
741 skb_queue_head(&iucv_sk(sk)->backlog_skb_q, skb);
744 static void iucv_process_message_q(struct sock *sk)
746 struct iucv_sock *iucv = iucv_sk(sk);
748 struct sock_msg_q *p, *n;
750 list_for_each_entry_safe(p, n, &iucv->message_q.list, list) {
751 skb = alloc_skb(p->msg.length, GFP_ATOMIC | GFP_DMA);
754 iucv_process_message(sk, skb, p->path, &p->msg);
757 if (!skb_queue_empty(&iucv->backlog_skb_q))
762 static int iucv_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
763 struct msghdr *msg, size_t len, int flags)
765 int noblock = flags & MSG_DONTWAIT;
766 struct sock *sk = sock->sk;
767 struct iucv_sock *iucv = iucv_sk(sk);
768 int target, copied = 0;
769 struct sk_buff *skb, *rskb, *cskb;
772 if ((sk->sk_state == IUCV_DISCONN || sk->sk_state == IUCV_SEVERED) &&
773 skb_queue_empty(&iucv->backlog_skb_q) &&
774 skb_queue_empty(&sk->sk_receive_queue) &&
775 list_empty(&iucv->message_q.list))
778 if (flags & (MSG_OOB))
781 target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
783 skb = skb_recv_datagram(sk, flags, noblock, &err);
785 if (sk->sk_shutdown & RCV_SHUTDOWN)
790 copied = min_t(unsigned int, skb->len, len);
793 if (memcpy_toiovec(msg->msg_iov, cskb->data, copied)) {
794 skb_queue_head(&sk->sk_receive_queue, skb);
802 /* Mark read part of skb as used */
803 if (!(flags & MSG_PEEK)) {
804 skb_pull(skb, copied);
807 skb_queue_head(&sk->sk_receive_queue, skb);
813 /* Queue backlog skbs */
814 rskb = skb_dequeue(&iucv->backlog_skb_q);
816 if (sock_queue_rcv_skb(sk, rskb)) {
817 skb_queue_head(&iucv->backlog_skb_q,
821 rskb = skb_dequeue(&iucv->backlog_skb_q);
824 if (skb_queue_empty(&iucv->backlog_skb_q)) {
825 spin_lock_bh(&iucv->message_q.lock);
826 if (!list_empty(&iucv->message_q.list))
827 iucv_process_message_q(sk);
828 spin_unlock_bh(&iucv->message_q.lock);
832 skb_queue_head(&sk->sk_receive_queue, skb);
835 return err ? : copied;
838 static inline unsigned int iucv_accept_poll(struct sock *parent)
840 struct iucv_sock *isk, *n;
843 list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) {
844 sk = (struct sock *) isk;
846 if (sk->sk_state == IUCV_CONNECTED)
847 return POLLIN | POLLRDNORM;
853 unsigned int iucv_sock_poll(struct file *file, struct socket *sock,
856 struct sock *sk = sock->sk;
857 unsigned int mask = 0;
859 poll_wait(file, sk->sk_sleep, wait);
861 if (sk->sk_state == IUCV_LISTEN)
862 return iucv_accept_poll(sk);
864 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
867 if (sk->sk_shutdown & RCV_SHUTDOWN)
870 if (sk->sk_shutdown == SHUTDOWN_MASK)
873 if (!skb_queue_empty(&sk->sk_receive_queue) ||
874 (sk->sk_shutdown & RCV_SHUTDOWN))
875 mask |= POLLIN | POLLRDNORM;
877 if (sk->sk_state == IUCV_CLOSED)
880 if (sk->sk_state == IUCV_DISCONN || sk->sk_state == IUCV_SEVERED)
883 if (sock_writeable(sk))
884 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
886 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
891 static int iucv_sock_shutdown(struct socket *sock, int how)
893 struct sock *sk = sock->sk;
894 struct iucv_sock *iucv = iucv_sk(sk);
895 struct iucv_message txmsg;
897 u8 prmmsg[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01};
901 if ((how & ~SHUTDOWN_MASK) || !how)
905 switch (sk->sk_state) {
911 sk->sk_shutdown |= how;
915 if (how == SEND_SHUTDOWN || how == SHUTDOWN_MASK) {
918 err = iucv_message_send(iucv->path, &txmsg, IUCV_IPRMDATA, 0,
935 if (how == RCV_SHUTDOWN || how == SHUTDOWN_MASK) {
936 err = iucv_path_quiesce(iucv_sk(sk)->path, NULL);
940 skb_queue_purge(&sk->sk_receive_queue);
943 /* Wake up anyone sleeping in poll */
944 sk->sk_state_change(sk);
951 static int iucv_sock_release(struct socket *sock)
953 struct sock *sk = sock->sk;
961 /* Unregister with IUCV base support */
962 if (iucv_sk(sk)->path) {
963 iucv_path_sever(iucv_sk(sk)->path, NULL);
964 iucv_path_free(iucv_sk(sk)->path);
965 iucv_sk(sk)->path = NULL;
973 /* Callback wrappers - called from iucv base support */
974 static int iucv_callback_connreq(struct iucv_path *path,
975 u8 ipvmid[8], u8 ipuser[16])
977 unsigned char user_data[16];
978 unsigned char nuser_data[16];
979 unsigned char src_name[8];
980 struct hlist_node *node;
981 struct sock *sk, *nsk;
982 struct iucv_sock *iucv, *niucv;
985 memcpy(src_name, ipuser, 8);
987 /* Find out if this path belongs to af_iucv. */
988 read_lock(&iucv_sk_list.lock);
991 sk_for_each(sk, node, &iucv_sk_list.head)
992 if (sk->sk_state == IUCV_LISTEN &&
993 !memcmp(&iucv_sk(sk)->src_name, src_name, 8)) {
995 * Found a listening socket with
996 * src_name == ipuser[0-7].
1001 read_unlock(&iucv_sk_list.lock);
1003 /* No socket found, not one of our paths. */
1008 /* Check if parent socket is listening */
1009 low_nmcpy(user_data, iucv->src_name);
1010 high_nmcpy(user_data, iucv->dst_name);
1011 ASCEBC(user_data, sizeof(user_data));
1012 if (sk->sk_state != IUCV_LISTEN) {
1013 err = iucv_path_sever(path, user_data);
1017 /* Check for backlog size */
1018 if (sk_acceptq_is_full(sk)) {
1019 err = iucv_path_sever(path, user_data);
1023 /* Create the new socket */
1024 nsk = iucv_sock_alloc(NULL, SOCK_STREAM, GFP_ATOMIC);
1026 err = iucv_path_sever(path, user_data);
1030 niucv = iucv_sk(nsk);
1031 iucv_sock_init(nsk, sk);
1033 /* Set the new iucv_sock */
1034 memcpy(niucv->dst_name, ipuser + 8, 8);
1035 EBCASC(niucv->dst_name, 8);
1036 memcpy(niucv->dst_user_id, ipvmid, 8);
1037 memcpy(niucv->src_name, iucv->src_name, 8);
1038 memcpy(niucv->src_user_id, iucv->src_user_id, 8);
1041 /* Call iucv_accept */
1042 high_nmcpy(nuser_data, ipuser + 8);
1043 memcpy(nuser_data + 8, niucv->src_name, 8);
1044 ASCEBC(nuser_data + 8, 8);
1046 path->msglim = IUCV_QUEUELEN_DEFAULT;
1047 err = iucv_path_accept(path, &af_iucv_handler, nuser_data, nsk);
1049 err = iucv_path_sever(path, user_data);
1053 iucv_accept_enqueue(sk, nsk);
1055 /* Wake up accept */
1056 nsk->sk_state = IUCV_CONNECTED;
1057 sk->sk_data_ready(sk, 1);
1064 static void iucv_callback_connack(struct iucv_path *path, u8 ipuser[16])
1066 struct sock *sk = path->private;
1068 sk->sk_state = IUCV_CONNECTED;
1069 sk->sk_state_change(sk);
1072 static void iucv_callback_rx(struct iucv_path *path, struct iucv_message *msg)
1074 struct sock *sk = path->private;
1075 struct iucv_sock *iucv = iucv_sk(sk);
1076 struct sk_buff *skb;
1077 struct sock_msg_q *save_msg;
1080 if (sk->sk_shutdown & RCV_SHUTDOWN)
1083 if (!list_empty(&iucv->message_q.list) ||
1084 !skb_queue_empty(&iucv->backlog_skb_q))
1087 len = atomic_read(&sk->sk_rmem_alloc);
1088 len += msg->length + sizeof(struct sk_buff);
1089 if (len > sk->sk_rcvbuf)
1092 skb = alloc_skb(msg->length, GFP_ATOMIC | GFP_DMA);
1096 spin_lock(&iucv->message_q.lock);
1097 iucv_process_message(sk, skb, path, msg);
1098 spin_unlock(&iucv->message_q.lock);
1103 save_msg = kzalloc(sizeof(struct sock_msg_q), GFP_ATOMIC | GFP_DMA);
1104 save_msg->path = path;
1105 save_msg->msg = *msg;
1107 spin_lock(&iucv->message_q.lock);
1108 list_add_tail(&save_msg->list, &iucv->message_q.list);
1109 spin_unlock(&iucv->message_q.lock);
1112 static void iucv_callback_txdone(struct iucv_path *path,
1113 struct iucv_message *msg)
1115 struct sock *sk = path->private;
1116 struct sk_buff *this;
1117 struct sk_buff_head *list = &iucv_sk(sk)->send_skb_q;
1118 struct sk_buff *list_skb = list->next;
1119 unsigned long flags;
1122 spin_lock_irqsave(&list->lock, flags);
1126 list_skb = list_skb->next;
1127 } while (memcmp(&msg->tag, this->cb, 4) && list_skb);
1129 spin_unlock_irqrestore(&list->lock, flags);
1131 skb_unlink(this, &iucv_sk(sk)->send_skb_q);
1135 if (sk->sk_state == IUCV_CLOSING) {
1136 if (skb_queue_empty(&iucv_sk(sk)->send_skb_q)) {
1137 sk->sk_state = IUCV_CLOSED;
1138 sk->sk_state_change(sk);
1144 static void iucv_callback_connrej(struct iucv_path *path, u8 ipuser[16])
1146 struct sock *sk = path->private;
1148 if (!list_empty(&iucv_sk(sk)->accept_q))
1149 sk->sk_state = IUCV_SEVERED;
1151 sk->sk_state = IUCV_DISCONN;
1153 sk->sk_state_change(sk);
1156 static struct proto_ops iucv_sock_ops = {
1158 .owner = THIS_MODULE,
1159 .release = iucv_sock_release,
1160 .bind = iucv_sock_bind,
1161 .connect = iucv_sock_connect,
1162 .listen = iucv_sock_listen,
1163 .accept = iucv_sock_accept,
1164 .getname = iucv_sock_getname,
1165 .sendmsg = iucv_sock_sendmsg,
1166 .recvmsg = iucv_sock_recvmsg,
1167 .poll = iucv_sock_poll,
1168 .ioctl = sock_no_ioctl,
1169 .mmap = sock_no_mmap,
1170 .socketpair = sock_no_socketpair,
1171 .shutdown = iucv_sock_shutdown,
1172 .setsockopt = sock_no_setsockopt,
1173 .getsockopt = sock_no_getsockopt
1176 static struct net_proto_family iucv_sock_family_ops = {
1178 .owner = THIS_MODULE,
1179 .create = iucv_sock_create,
1182 static int __init afiucv_init(void)
1186 if (!MACHINE_IS_VM) {
1187 printk(KERN_ERR "AF_IUCV connection needs VM as base\n");
1188 err = -EPROTONOSUPPORT;
1191 cpcmd("QUERY USERID", iucv_userid, sizeof(iucv_userid), &err);
1192 if (unlikely(err)) {
1193 printk(KERN_ERR "AF_IUCV needs the VM userid\n");
1194 err = -EPROTONOSUPPORT;
1198 err = iucv_register(&af_iucv_handler, 0);
1201 err = proto_register(&iucv_proto, 0);
1204 err = sock_register(&iucv_sock_family_ops);
1207 printk(KERN_INFO "AF_IUCV lowlevel driver initialized\n");
1211 proto_unregister(&iucv_proto);
1213 iucv_unregister(&af_iucv_handler, 0);
1218 static void __exit afiucv_exit(void)
1220 sock_unregister(PF_IUCV);
1221 proto_unregister(&iucv_proto);
1222 iucv_unregister(&af_iucv_handler, 0);
1224 printk(KERN_INFO "AF_IUCV lowlevel driver unloaded\n");
1227 module_init(afiucv_init);
1228 module_exit(afiucv_exit);
1230 MODULE_AUTHOR("Jennifer Hunt <jenhunt@us.ibm.com>");
1231 MODULE_DESCRIPTION("IUCV Sockets ver " VERSION);
1232 MODULE_VERSION(VERSION);
1233 MODULE_LICENSE("GPL");
1234 MODULE_ALIAS_NETPROTO(PF_IUCV);