[TCP]: Fix tcp_mem[] initialization.
[linux-2.6.22.y-op.git] / net / iucv / af_iucv.c
blobacc94214bde6f75903083bab3adced73930d174c
1 /*
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>
9 */
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>
21 #include <net/sock.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
31 #define IPRMDATA 0x80
32 #define VERSION "1.0"
34 static char iucv_userid[80];
36 static struct proto_ops iucv_sock_ops;
38 static struct proto iucv_proto = {
39 .name = "AF_IUCV",
40 .owner = THIS_MODULE,
41 .obj_size = sizeof(struct iucv_sock),
44 /* Call Back functions */
45 static void iucv_callback_rx(struct iucv_path *, struct iucv_message *);
46 static void iucv_callback_txdone(struct iucv_path *, struct iucv_message *);
47 static void iucv_callback_connack(struct iucv_path *, u8 ipuser[16]);
48 static int iucv_callback_connreq(struct iucv_path *, u8 ipvmid[8], u8 ipuser[16]);
49 static void iucv_callback_connrej(struct iucv_path *, u8 ipuser[16]);
51 static struct iucv_sock_list iucv_sk_list = {
52 .lock = RW_LOCK_UNLOCKED,
53 .autobind_name = ATOMIC_INIT(0)
56 static struct iucv_handler af_iucv_handler = {
57 .path_pending = iucv_callback_connreq,
58 .path_complete = iucv_callback_connack,
59 .path_severed = iucv_callback_connrej,
60 .message_pending = iucv_callback_rx,
61 .message_complete = iucv_callback_txdone
64 static inline void high_nmcpy(unsigned char *dst, char *src)
66 memcpy(dst, src, 8);
69 static inline void low_nmcpy(unsigned char *dst, char *src)
71 memcpy(&dst[8], src, 8);
74 /* Timers */
75 static void iucv_sock_timeout(unsigned long arg)
77 struct sock *sk = (struct sock *)arg;
79 bh_lock_sock(sk);
80 sk->sk_err = ETIMEDOUT;
81 sk->sk_state_change(sk);
82 bh_unlock_sock(sk);
84 iucv_sock_kill(sk);
85 sock_put(sk);
88 static void iucv_sock_clear_timer(struct sock *sk)
90 sk_stop_timer(sk, &sk->sk_timer);
93 static void iucv_sock_init_timer(struct sock *sk)
95 init_timer(&sk->sk_timer);
96 sk->sk_timer.function = iucv_sock_timeout;
97 sk->sk_timer.data = (unsigned long)sk;
100 static struct sock *__iucv_get_sock_by_name(char *nm)
102 struct sock *sk;
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))
107 return sk;
109 return NULL;
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);
118 /* Cleanup Listen */
119 static void iucv_sock_cleanup_listen(struct sock *parent)
121 struct sock *sk;
123 /* Close non-accepted connections */
124 while ((sk = iucv_accept_dequeue(parent, NULL))) {
125 iucv_sock_close(sk);
126 iucv_sock_kill(sk);
129 parent->sk_state = IUCV_CLOSED;
130 sock_set_flag(parent, SOCK_ZAPPED);
133 /* Kill socket */
134 static void iucv_sock_kill(struct sock *sk)
136 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
137 return;
139 iucv_sock_unlink(&iucv_sk_list, sk);
140 sock_set_flag(sk, SOCK_DEAD);
141 sock_put(sk);
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);
149 int err;
151 iucv_sock_clear_timer(sk);
152 lock_sock(sk);
154 switch(sk->sk_state) {
155 case IUCV_LISTEN:
156 iucv_sock_cleanup_listen(sk);
157 break;
159 case IUCV_CONNECTED:
160 case IUCV_DISCONN:
161 err = 0;
162 if (iucv->path) {
163 low_nmcpy(user_data, iucv->src_name);
164 high_nmcpy(user_data, iucv->dst_name);
165 ASCEBC(user_data, sizeof(user_data));
166 err = iucv_path_sever(iucv->path, user_data);
167 iucv_path_free(iucv->path);
168 iucv->path = NULL;
171 sk->sk_state = IUCV_CLOSED;
172 sk->sk_state_change(sk);
173 sk->sk_err = ECONNRESET;
174 sk->sk_state_change(sk);
176 skb_queue_purge(&iucv->send_skb_q);
178 sock_set_flag(sk, SOCK_ZAPPED);
179 break;
181 default:
182 sock_set_flag(sk, SOCK_ZAPPED);
183 break;
186 release_sock(sk);
187 iucv_sock_kill(sk);
190 static void iucv_sock_init(struct sock *sk, struct sock *parent)
192 if (parent)
193 sk->sk_type = parent->sk_type;
196 static struct sock *iucv_sock_alloc(struct socket *sock, int proto, gfp_t prio)
198 struct sock *sk;
200 sk = sk_alloc(PF_IUCV, prio, &iucv_proto, 1);
201 if (!sk)
202 return NULL;
204 sock_init_data(sock, sk);
205 INIT_LIST_HEAD(&iucv_sk(sk)->accept_q);
206 skb_queue_head_init(&iucv_sk(sk)->send_skb_q);
207 iucv_sk(sk)->send_tag = 0;
209 sk->sk_destruct = iucv_sock_destruct;
210 sk->sk_sndtimeo = IUCV_CONN_TIMEOUT;
211 sk->sk_allocation = GFP_DMA;
213 sock_reset_flag(sk, SOCK_ZAPPED);
215 sk->sk_protocol = proto;
216 sk->sk_state = IUCV_OPEN;
218 iucv_sock_init_timer(sk);
220 iucv_sock_link(&iucv_sk_list, sk);
221 return sk;
224 /* Create an IUCV socket */
225 static int iucv_sock_create(struct socket *sock, int protocol)
227 struct sock *sk;
229 if (sock->type != SOCK_STREAM)
230 return -ESOCKTNOSUPPORT;
232 sock->state = SS_UNCONNECTED;
233 sock->ops = &iucv_sock_ops;
235 sk = iucv_sock_alloc(sock, protocol, GFP_KERNEL);
236 if (!sk)
237 return -ENOMEM;
239 iucv_sock_init(sk, NULL);
241 return 0;
244 void iucv_sock_link(struct iucv_sock_list *l, struct sock *sk)
246 write_lock_bh(&l->lock);
247 sk_add_node(sk, &l->head);
248 write_unlock_bh(&l->lock);
251 void iucv_sock_unlink(struct iucv_sock_list *l, struct sock *sk)
253 write_lock_bh(&l->lock);
254 sk_del_node_init(sk);
255 write_unlock_bh(&l->lock);
258 void iucv_accept_enqueue(struct sock *parent, struct sock *sk)
260 sock_hold(sk);
261 list_add_tail(&iucv_sk(sk)->accept_q, &iucv_sk(parent)->accept_q);
262 iucv_sk(sk)->parent = parent;
263 parent->sk_ack_backlog++;
266 void iucv_accept_unlink(struct sock *sk)
268 list_del_init(&iucv_sk(sk)->accept_q);
269 iucv_sk(sk)->parent->sk_ack_backlog--;
270 iucv_sk(sk)->parent = NULL;
271 sock_put(sk);
274 struct sock *iucv_accept_dequeue(struct sock *parent, struct socket *newsock)
276 struct iucv_sock *isk, *n;
277 struct sock *sk;
279 list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q){
280 sk = (struct sock *) isk;
281 lock_sock(sk);
283 if (sk->sk_state == IUCV_CLOSED) {
284 release_sock(sk);
285 iucv_accept_unlink(sk);
286 continue;
289 if (sk->sk_state == IUCV_CONNECTED ||
290 sk->sk_state == IUCV_SEVERED ||
291 !newsock) {
292 iucv_accept_unlink(sk);
293 if (newsock)
294 sock_graft(sk, newsock);
296 if (sk->sk_state == IUCV_SEVERED)
297 sk->sk_state = IUCV_DISCONN;
299 release_sock(sk);
300 return sk;
303 release_sock(sk);
305 return NULL;
308 int iucv_sock_wait_state(struct sock *sk, int state, int state2,
309 unsigned long timeo)
311 DECLARE_WAITQUEUE(wait, current);
312 int err = 0;
314 add_wait_queue(sk->sk_sleep, &wait);
315 while (sk->sk_state != state && sk->sk_state != state2) {
316 set_current_state(TASK_INTERRUPTIBLE);
318 if (!timeo) {
319 err = -EAGAIN;
320 break;
323 if (signal_pending(current)) {
324 err = sock_intr_errno(timeo);
325 break;
328 release_sock(sk);
329 timeo = schedule_timeout(timeo);
330 lock_sock(sk);
332 err = sock_error(sk);
333 if (err)
334 break;
336 set_current_state(TASK_RUNNING);
337 remove_wait_queue(sk->sk_sleep, &wait);
338 return err;
341 /* Bind an unbound socket */
342 static int iucv_sock_bind(struct socket *sock, struct sockaddr *addr,
343 int addr_len)
345 struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
346 struct sock *sk = sock->sk;
347 struct iucv_sock *iucv;
348 int err;
350 /* Verify the input sockaddr */
351 if (!addr || addr->sa_family != AF_IUCV)
352 return -EINVAL;
354 lock_sock(sk);
355 if (sk->sk_state != IUCV_OPEN) {
356 err = -EBADFD;
357 goto done;
360 write_lock_bh(&iucv_sk_list.lock);
362 iucv = iucv_sk(sk);
363 if (__iucv_get_sock_by_name(sa->siucv_name)) {
364 err = -EADDRINUSE;
365 goto done_unlock;
367 if (iucv->path) {
368 err = 0;
369 goto done_unlock;
372 /* Bind the socket */
373 memcpy(iucv->src_name, sa->siucv_name, 8);
375 /* Copy the user id */
376 memcpy(iucv->src_user_id, iucv_userid, 8);
377 sk->sk_state = IUCV_BOUND;
378 err = 0;
380 done_unlock:
381 /* Release the socket list lock */
382 write_unlock_bh(&iucv_sk_list.lock);
383 done:
384 release_sock(sk);
385 return err;
388 /* Automatically bind an unbound socket */
389 static int iucv_sock_autobind(struct sock *sk)
391 struct iucv_sock *iucv = iucv_sk(sk);
392 char query_buffer[80];
393 char name[12];
394 int err = 0;
396 /* Set the userid and name */
397 cpcmd("QUERY USERID", query_buffer, sizeof(query_buffer), &err);
398 if (unlikely(err))
399 return -EPROTO;
401 memcpy(iucv->src_user_id, query_buffer, 8);
403 write_lock_bh(&iucv_sk_list.lock);
405 sprintf(name, "%08x", atomic_inc_return(&iucv_sk_list.autobind_name));
406 while (__iucv_get_sock_by_name(name)) {
407 sprintf(name, "%08x",
408 atomic_inc_return(&iucv_sk_list.autobind_name));
411 write_unlock_bh(&iucv_sk_list.lock);
413 memcpy(&iucv->src_name, name, 8);
415 return err;
418 /* Connect an unconnected socket */
419 static int iucv_sock_connect(struct socket *sock, struct sockaddr *addr,
420 int alen, int flags)
422 struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
423 struct sock *sk = sock->sk;
424 struct iucv_sock *iucv;
425 unsigned char user_data[16];
426 int err;
428 if (addr->sa_family != AF_IUCV || alen < sizeof(struct sockaddr_iucv))
429 return -EINVAL;
431 if (sk->sk_state != IUCV_OPEN && sk->sk_state != IUCV_BOUND)
432 return -EBADFD;
434 if (sk->sk_type != SOCK_STREAM)
435 return -EINVAL;
437 iucv = iucv_sk(sk);
439 if (sk->sk_state == IUCV_OPEN) {
440 err = iucv_sock_autobind(sk);
441 if (unlikely(err))
442 return err;
445 lock_sock(sk);
447 /* Set the destination information */
448 memcpy(iucv_sk(sk)->dst_user_id, sa->siucv_user_id, 8);
449 memcpy(iucv_sk(sk)->dst_name, sa->siucv_name, 8);
451 high_nmcpy(user_data, sa->siucv_name);
452 low_nmcpy(user_data, iucv_sk(sk)->src_name);
453 ASCEBC(user_data, sizeof(user_data));
455 iucv = iucv_sk(sk);
456 /* Create path. */
457 iucv->path = iucv_path_alloc(IUCV_QUEUELEN_DEFAULT,
458 IPRMDATA, GFP_KERNEL);
459 err = iucv_path_connect(iucv->path, &af_iucv_handler,
460 sa->siucv_user_id, NULL, user_data, sk);
461 if (err) {
462 iucv_path_free(iucv->path);
463 iucv->path = NULL;
464 err = -ECONNREFUSED;
465 goto done;
468 if (sk->sk_state != IUCV_CONNECTED) {
469 err = iucv_sock_wait_state(sk, IUCV_CONNECTED, IUCV_DISCONN,
470 sock_sndtimeo(sk, flags & O_NONBLOCK));
473 if (sk->sk_state == IUCV_DISCONN) {
474 release_sock(sk);
475 return -ECONNREFUSED;
477 done:
478 release_sock(sk);
479 return err;
482 /* Move a socket into listening state. */
483 static int iucv_sock_listen(struct socket *sock, int backlog)
485 struct sock *sk = sock->sk;
486 int err;
488 lock_sock(sk);
490 err = -EINVAL;
491 if (sk->sk_state != IUCV_BOUND || sock->type != SOCK_STREAM)
492 goto done;
494 sk->sk_max_ack_backlog = backlog;
495 sk->sk_ack_backlog = 0;
496 sk->sk_state = IUCV_LISTEN;
497 err = 0;
499 done:
500 release_sock(sk);
501 return err;
504 /* Accept a pending connection */
505 static int iucv_sock_accept(struct socket *sock, struct socket *newsock,
506 int flags)
508 DECLARE_WAITQUEUE(wait, current);
509 struct sock *sk = sock->sk, *nsk;
510 long timeo;
511 int err = 0;
513 lock_sock(sk);
515 if (sk->sk_state != IUCV_LISTEN) {
516 err = -EBADFD;
517 goto done;
520 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
522 /* Wait for an incoming connection */
523 add_wait_queue_exclusive(sk->sk_sleep, &wait);
524 while (!(nsk = iucv_accept_dequeue(sk, newsock))){
525 set_current_state(TASK_INTERRUPTIBLE);
526 if (!timeo) {
527 err = -EAGAIN;
528 break;
531 release_sock(sk);
532 timeo = schedule_timeout(timeo);
533 lock_sock(sk);
535 if (sk->sk_state != IUCV_LISTEN) {
536 err = -EBADFD;
537 break;
540 if (signal_pending(current)) {
541 err = sock_intr_errno(timeo);
542 break;
546 set_current_state(TASK_RUNNING);
547 remove_wait_queue(sk->sk_sleep, &wait);
549 if (err)
550 goto done;
552 newsock->state = SS_CONNECTED;
554 done:
555 release_sock(sk);
556 return err;
559 static int iucv_sock_getname(struct socket *sock, struct sockaddr *addr,
560 int *len, int peer)
562 struct sockaddr_iucv *siucv = (struct sockaddr_iucv *) addr;
563 struct sock *sk = sock->sk;
565 addr->sa_family = AF_IUCV;
566 *len = sizeof(struct sockaddr_iucv);
568 if (peer) {
569 memcpy(siucv->siucv_user_id, iucv_sk(sk)->dst_user_id, 8);
570 memcpy(siucv->siucv_name, &iucv_sk(sk)->dst_name, 8);
571 } else {
572 memcpy(siucv->siucv_user_id, iucv_sk(sk)->src_user_id, 8);
573 memcpy(siucv->siucv_name, iucv_sk(sk)->src_name, 8);
575 memset(&siucv->siucv_port, 0, sizeof(siucv->siucv_port));
576 memset(&siucv->siucv_addr, 0, sizeof(siucv->siucv_addr));
577 memset(siucv->siucv_nodeid, 0, sizeof(siucv->siucv_nodeid));
579 return 0;
582 static int iucv_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
583 struct msghdr *msg, size_t len)
585 struct sock *sk = sock->sk;
586 struct iucv_sock *iucv = iucv_sk(sk);
587 struct sk_buff *skb;
588 struct iucv_message txmsg;
589 int err;
591 err = sock_error(sk);
592 if (err)
593 return err;
595 if (msg->msg_flags & MSG_OOB)
596 return -EOPNOTSUPP;
598 lock_sock(sk);
600 if (sk->sk_shutdown & SEND_SHUTDOWN) {
601 err = -EPIPE;
602 goto out;
605 if (sk->sk_state == IUCV_CONNECTED){
606 if(!(skb = sock_alloc_send_skb(sk, len,
607 msg->msg_flags & MSG_DONTWAIT,
608 &err)))
609 return err;
611 if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)){
612 err = -EFAULT;
613 goto fail;
616 txmsg.class = 0;
617 txmsg.tag = iucv->send_tag++;
618 memcpy(skb->cb, &txmsg.tag, 4);
619 skb_queue_tail(&iucv->send_skb_q, skb);
620 err = iucv_message_send(iucv->path, &txmsg, 0, 0,
621 (void *) skb->data, skb->len);
622 if (err) {
623 if (err == 3)
624 printk(KERN_ERR "AF_IUCV msg limit exceeded\n");
625 skb_unlink(skb, &iucv->send_skb_q);
626 err = -EPIPE;
627 goto fail;
630 } else {
631 err = -ENOTCONN;
632 goto out;
635 release_sock(sk);
636 return len;
638 fail:
639 kfree_skb(skb);
640 out:
641 release_sock(sk);
642 return err;
645 static int iucv_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
646 struct msghdr *msg, size_t len, int flags)
648 int noblock = flags & MSG_DONTWAIT;
649 struct sock *sk = sock->sk;
650 int target, copied = 0;
651 struct sk_buff *skb;
652 int err = 0;
654 if (flags & (MSG_OOB))
655 return -EOPNOTSUPP;
657 target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
659 skb = skb_recv_datagram(sk, flags, noblock, &err);
660 if (!skb) {
661 if (sk->sk_shutdown & RCV_SHUTDOWN)
662 return 0;
663 return err;
666 copied = min_t(unsigned int, skb->len, len);
668 if (memcpy_toiovec(msg->msg_iov, skb->data, copied)) {
669 skb_queue_head(&sk->sk_receive_queue, skb);
670 if (copied == 0)
671 return -EFAULT;
674 len -= copied;
676 /* Mark read part of skb as used */
677 if (!(flags & MSG_PEEK)) {
678 skb_pull(skb, copied);
680 if (skb->len) {
681 skb_queue_head(&sk->sk_receive_queue, skb);
682 goto done;
685 kfree_skb(skb);
686 } else
687 skb_queue_head(&sk->sk_receive_queue, skb);
689 done:
690 return err ? : copied;
693 static inline unsigned int iucv_accept_poll(struct sock *parent)
695 struct iucv_sock *isk, *n;
696 struct sock *sk;
698 list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q){
699 sk = (struct sock *) isk;
701 if (sk->sk_state == IUCV_CONNECTED)
702 return POLLIN | POLLRDNORM;
705 return 0;
708 unsigned int iucv_sock_poll(struct file *file, struct socket *sock,
709 poll_table *wait)
711 struct sock *sk = sock->sk;
712 unsigned int mask = 0;
714 poll_wait(file, sk->sk_sleep, wait);
716 if (sk->sk_state == IUCV_LISTEN)
717 return iucv_accept_poll(sk);
719 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
720 mask |= POLLERR;
722 if (sk->sk_shutdown & RCV_SHUTDOWN)
723 mask |= POLLRDHUP;
725 if (sk->sk_shutdown == SHUTDOWN_MASK)
726 mask |= POLLHUP;
728 if (!skb_queue_empty(&sk->sk_receive_queue) ||
729 (sk->sk_shutdown & RCV_SHUTDOWN))
730 mask |= POLLIN | POLLRDNORM;
732 if (sk->sk_state == IUCV_CLOSED)
733 mask |= POLLHUP;
735 if (sock_writeable(sk))
736 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
737 else
738 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
740 return mask;
743 static int iucv_sock_shutdown(struct socket *sock, int how)
745 struct sock *sk = sock->sk;
746 struct iucv_sock *iucv = iucv_sk(sk);
747 struct iucv_message txmsg;
748 int err = 0;
749 u8 prmmsg[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01};
751 how++;
753 if ((how & ~SHUTDOWN_MASK) || !how)
754 return -EINVAL;
756 lock_sock(sk);
757 switch(sk->sk_state) {
758 case IUCV_CLOSED:
759 err = -ENOTCONN;
760 goto fail;
762 default:
763 sk->sk_shutdown |= how;
764 break;
767 if (how == SEND_SHUTDOWN || how == SHUTDOWN_MASK) {
768 txmsg.class = 0;
769 txmsg.tag = 0;
770 err = iucv_message_send(iucv->path, &txmsg, IUCV_IPRMDATA, 0,
771 (void *) prmmsg, 8);
772 if (err) {
773 switch(err) {
774 case 1:
775 err = -ENOTCONN;
776 break;
777 case 2:
778 err = -ECONNRESET;
779 break;
780 default:
781 err = -ENOTCONN;
782 break;
787 if (how == RCV_SHUTDOWN || how == SHUTDOWN_MASK) {
788 err = iucv_path_quiesce(iucv_sk(sk)->path, NULL);
789 if (err)
790 err = -ENOTCONN;
792 skb_queue_purge(&sk->sk_receive_queue);
795 /* Wake up anyone sleeping in poll */
796 sk->sk_state_change(sk);
798 fail:
799 release_sock(sk);
800 return err;
803 static int iucv_sock_release(struct socket *sock)
805 struct sock *sk = sock->sk;
806 int err = 0;
808 if (!sk)
809 return 0;
811 iucv_sock_close(sk);
813 /* Unregister with IUCV base support */
814 if (iucv_sk(sk)->path) {
815 iucv_path_sever(iucv_sk(sk)->path, NULL);
816 iucv_path_free(iucv_sk(sk)->path);
817 iucv_sk(sk)->path = NULL;
820 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime){
821 lock_sock(sk);
822 err = iucv_sock_wait_state(sk, IUCV_CLOSED, 0,
823 sk->sk_lingertime);
824 release_sock(sk);
827 sock_orphan(sk);
828 iucv_sock_kill(sk);
829 return err;
832 /* Callback wrappers - called from iucv base support */
833 static int iucv_callback_connreq(struct iucv_path *path,
834 u8 ipvmid[8], u8 ipuser[16])
836 unsigned char user_data[16];
837 unsigned char nuser_data[16];
838 unsigned char src_name[8];
839 struct hlist_node *node;
840 struct sock *sk, *nsk;
841 struct iucv_sock *iucv, *niucv;
842 int err;
844 memcpy(src_name, ipuser, 8);
845 EBCASC(src_name, 8);
846 /* Find out if this path belongs to af_iucv. */
847 read_lock(&iucv_sk_list.lock);
848 iucv = NULL;
849 sk_for_each(sk, node, &iucv_sk_list.head)
850 if (sk->sk_state == IUCV_LISTEN &&
851 !memcmp(&iucv_sk(sk)->src_name, src_name, 8)) {
853 * Found a listening socket with
854 * src_name == ipuser[0-7].
856 iucv = iucv_sk(sk);
857 break;
859 read_unlock(&iucv_sk_list.lock);
860 if (!iucv)
861 /* No socket found, not one of our paths. */
862 return -EINVAL;
864 bh_lock_sock(sk);
866 /* Check if parent socket is listening */
867 low_nmcpy(user_data, iucv->src_name);
868 high_nmcpy(user_data, iucv->dst_name);
869 ASCEBC(user_data, sizeof(user_data));
870 if (sk->sk_state != IUCV_LISTEN) {
871 err = iucv_path_sever(path, user_data);
872 goto fail;
875 /* Check for backlog size */
876 if (sk_acceptq_is_full(sk)) {
877 err = iucv_path_sever(path, user_data);
878 goto fail;
881 /* Create the new socket */
882 nsk = iucv_sock_alloc(NULL, SOCK_STREAM, GFP_ATOMIC);
883 if (!nsk){
884 err = iucv_path_sever(path, user_data);
885 goto fail;
888 niucv = iucv_sk(nsk);
889 iucv_sock_init(nsk, sk);
891 /* Set the new iucv_sock */
892 memcpy(niucv->dst_name, ipuser + 8, 8);
893 EBCASC(niucv->dst_name, 8);
894 memcpy(niucv->dst_user_id, ipvmid, 8);
895 memcpy(niucv->src_name, iucv->src_name, 8);
896 memcpy(niucv->src_user_id, iucv->src_user_id, 8);
897 niucv->path = path;
899 /* Call iucv_accept */
900 high_nmcpy(nuser_data, ipuser + 8);
901 memcpy(nuser_data + 8, niucv->src_name, 8);
902 ASCEBC(nuser_data + 8, 8);
904 path->msglim = IUCV_QUEUELEN_DEFAULT;
905 err = iucv_path_accept(path, &af_iucv_handler, nuser_data, nsk);
906 if (err){
907 err = iucv_path_sever(path, user_data);
908 goto fail;
911 iucv_accept_enqueue(sk, nsk);
913 /* Wake up accept */
914 nsk->sk_state = IUCV_CONNECTED;
915 sk->sk_data_ready(sk, 1);
916 err = 0;
917 fail:
918 bh_unlock_sock(sk);
919 return 0;
922 static void iucv_callback_connack(struct iucv_path *path, u8 ipuser[16])
924 struct sock *sk = path->private;
926 sk->sk_state = IUCV_CONNECTED;
927 sk->sk_state_change(sk);
930 static void iucv_callback_rx(struct iucv_path *path, struct iucv_message *msg)
932 struct sock *sk = path->private;
933 struct sk_buff *skb;
934 int rc;
936 if (sk->sk_shutdown & RCV_SHUTDOWN)
937 return;
939 skb = alloc_skb(msg->length, GFP_ATOMIC | GFP_DMA);
940 if (!skb) {
941 iucv_message_reject(path, msg);
942 return;
945 if (msg->flags & IPRMDATA) {
946 skb->data = NULL;
947 skb->len = 0;
948 } else {
949 rc = iucv_message_receive(path, msg, 0, skb->data,
950 msg->length, NULL);
951 if (rc) {
952 kfree_skb(skb);
953 return;
956 skb->h.raw = skb->data;
957 skb->nh.raw = skb->data;
958 skb->len = msg->length;
961 if (sock_queue_rcv_skb(sk, skb))
962 kfree_skb(skb);
965 static void iucv_callback_txdone(struct iucv_path *path,
966 struct iucv_message *msg)
968 struct sock *sk = path->private;
969 struct sk_buff *this;
970 struct sk_buff_head *list = &iucv_sk(sk)->send_skb_q;
971 struct sk_buff *list_skb = list->next;
972 unsigned long flags;
974 spin_lock_irqsave(&list->lock, flags);
976 do {
977 this = list_skb;
978 list_skb = list_skb->next;
979 } while (memcmp(&msg->tag, this->cb, 4));
981 spin_unlock_irqrestore(&list->lock, flags);
983 skb_unlink(this, &iucv_sk(sk)->send_skb_q);
984 kfree_skb(this);
987 static void iucv_callback_connrej(struct iucv_path *path, u8 ipuser[16])
989 struct sock *sk = path->private;
991 if (!list_empty(&iucv_sk(sk)->accept_q))
992 sk->sk_state = IUCV_SEVERED;
993 else
994 sk->sk_state = IUCV_DISCONN;
996 sk->sk_state_change(sk);
999 static struct proto_ops iucv_sock_ops = {
1000 .family = PF_IUCV,
1001 .owner = THIS_MODULE,
1002 .release = iucv_sock_release,
1003 .bind = iucv_sock_bind,
1004 .connect = iucv_sock_connect,
1005 .listen = iucv_sock_listen,
1006 .accept = iucv_sock_accept,
1007 .getname = iucv_sock_getname,
1008 .sendmsg = iucv_sock_sendmsg,
1009 .recvmsg = iucv_sock_recvmsg,
1010 .poll = iucv_sock_poll,
1011 .ioctl = sock_no_ioctl,
1012 .mmap = sock_no_mmap,
1013 .socketpair = sock_no_socketpair,
1014 .shutdown = iucv_sock_shutdown,
1015 .setsockopt = sock_no_setsockopt,
1016 .getsockopt = sock_no_getsockopt
1019 static struct net_proto_family iucv_sock_family_ops = {
1020 .family = AF_IUCV,
1021 .owner = THIS_MODULE,
1022 .create = iucv_sock_create,
1025 static int afiucv_init(void)
1027 int err;
1029 if (!MACHINE_IS_VM) {
1030 printk(KERN_ERR "AF_IUCV connection needs VM as base\n");
1031 err = -EPROTONOSUPPORT;
1032 goto out;
1034 cpcmd("QUERY USERID", iucv_userid, sizeof(iucv_userid), &err);
1035 if (unlikely(err)) {
1036 printk(KERN_ERR "AF_IUCV needs the VM userid\n");
1037 err = -EPROTONOSUPPORT;
1038 goto out;
1041 err = iucv_register(&af_iucv_handler, 0);
1042 if (err)
1043 goto out;
1044 err = proto_register(&iucv_proto, 0);
1045 if (err)
1046 goto out_iucv;
1047 err = sock_register(&iucv_sock_family_ops);
1048 if (err)
1049 goto out_proto;
1050 printk(KERN_INFO "AF_IUCV lowlevel driver initialized\n");
1051 return 0;
1053 out_proto:
1054 proto_unregister(&iucv_proto);
1055 out_iucv:
1056 iucv_unregister(&af_iucv_handler, 0);
1057 out:
1058 return err;
1061 static void __exit afiucv_exit(void)
1063 sock_unregister(PF_IUCV);
1064 proto_unregister(&iucv_proto);
1065 iucv_unregister(&af_iucv_handler, 0);
1067 printk(KERN_INFO "AF_IUCV lowlevel driver unloaded\n");
1070 module_init(afiucv_init);
1071 module_exit(afiucv_exit);
1073 MODULE_AUTHOR("Jennifer Hunt <jenhunt@us.ibm.com>");
1074 MODULE_DESCRIPTION("IUCV Sockets ver " VERSION);
1075 MODULE_VERSION(VERSION);
1076 MODULE_LICENSE("GPL");
1077 MODULE_ALIAS_NETPROTO(PF_IUCV);