af_iucv: Change if condition in sendmsg() for more readability
[linux-2.6.git] / net / iucv / af_iucv.c
1 /*
2  *  linux/net/iucv/af_iucv.c
3  *
4  *  IUCV protocol stack for Linux on zSeries
5  *
6  *  Copyright 2006 IBM Corporation
7  *
8  *  Author(s):  Jennifer Hunt <jenhunt@us.ibm.com>
9  */
10
11 #define KMSG_COMPONENT "af_iucv"
12 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
13
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>
24 #include <net/sock.h>
25 #include <asm/ebcdic.h>
26 #include <asm/cpcmd.h>
27 #include <linux/kmod.h>
28
29 #include <net/iucv/iucv.h>
30 #include <net/iucv/af_iucv.h>
31
32 #define VERSION "1.1"
33
34 static char iucv_userid[80];
35
36 static struct proto_ops iucv_sock_ops;
37
38 static struct proto iucv_proto = {
39         .name           = "AF_IUCV",
40         .owner          = THIS_MODULE,
41         .obj_size       = sizeof(struct iucv_sock),
42 };
43
44 /* special AF_IUCV IPRM messages */
45 static const u8 iprm_shutdown[8] =
46         {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01};
47
48 #define TRGCLS_SIZE     (sizeof(((struct iucv_message *)0)->class))
49
50 /* macros to set/get socket control buffer at correct offset */
51 #define CB_TAG(skb)     ((skb)->cb)             /* iucv message tag */
52 #define CB_TAG_LEN      (sizeof(((struct iucv_message *) 0)->tag))
53 #define CB_TRGCLS(skb)  ((skb)->cb + CB_TAG_LEN) /* iucv msg target class */
54 #define CB_TRGCLS_LEN   (TRGCLS_SIZE)
55
56
57 static void iucv_sock_kill(struct sock *sk);
58 static void iucv_sock_close(struct sock *sk);
59
60 /* Call Back functions */
61 static void iucv_callback_rx(struct iucv_path *, struct iucv_message *);
62 static void iucv_callback_txdone(struct iucv_path *, struct iucv_message *);
63 static void iucv_callback_connack(struct iucv_path *, u8 ipuser[16]);
64 static int iucv_callback_connreq(struct iucv_path *, u8 ipvmid[8],
65                                  u8 ipuser[16]);
66 static void iucv_callback_connrej(struct iucv_path *, u8 ipuser[16]);
67 static void iucv_callback_shutdown(struct iucv_path *, u8 ipuser[16]);
68
69 static struct iucv_sock_list iucv_sk_list = {
70         .lock = __RW_LOCK_UNLOCKED(iucv_sk_list.lock),
71         .autobind_name = ATOMIC_INIT(0)
72 };
73
74 static struct iucv_handler af_iucv_handler = {
75         .path_pending     = iucv_callback_connreq,
76         .path_complete    = iucv_callback_connack,
77         .path_severed     = iucv_callback_connrej,
78         .message_pending  = iucv_callback_rx,
79         .message_complete = iucv_callback_txdone,
80         .path_quiesced    = iucv_callback_shutdown,
81 };
82
83 static inline void high_nmcpy(unsigned char *dst, char *src)
84 {
85        memcpy(dst, src, 8);
86 }
87
88 static inline void low_nmcpy(unsigned char *dst, char *src)
89 {
90        memcpy(&dst[8], src, 8);
91 }
92
93 /**
94  * iucv_msg_length() - Returns the length of an iucv message.
95  * @msg:        Pointer to struct iucv_message, MUST NOT be NULL
96  *
97  * The function returns the length of the specified iucv message @msg of data
98  * stored in a buffer and of data stored in the parameter list (PRMDATA).
99  *
100  * For IUCV_IPRMDATA, AF_IUCV uses the following convention to transport socket
101  * data:
102  *      PRMDATA[0..6]   socket data (max 7 bytes);
103  *      PRMDATA[7]      socket data length value (len is 0xff - PRMDATA[7])
104  *
105  * The socket data length is computed by substracting the socket data length
106  * value from 0xFF.
107  * If the socket data len is greater 7, then PRMDATA can be used for special
108  * notifications (see iucv_sock_shutdown); and further,
109  * if the socket data len is > 7, the function returns 8.
110  *
111  * Use this function to allocate socket buffers to store iucv message data.
112  */
113 static inline size_t iucv_msg_length(struct iucv_message *msg)
114 {
115         size_t datalen;
116
117         if (msg->flags & IUCV_IPRMDATA) {
118                 datalen = 0xff - msg->rmmsg[7];
119                 return (datalen < 8) ? datalen : 8;
120         }
121         return msg->length;
122 }
123
124 /* Timers */
125 static void iucv_sock_timeout(unsigned long arg)
126 {
127         struct sock *sk = (struct sock *)arg;
128
129         bh_lock_sock(sk);
130         sk->sk_err = ETIMEDOUT;
131         sk->sk_state_change(sk);
132         bh_unlock_sock(sk);
133
134         iucv_sock_kill(sk);
135         sock_put(sk);
136 }
137
138 static void iucv_sock_clear_timer(struct sock *sk)
139 {
140         sk_stop_timer(sk, &sk->sk_timer);
141 }
142
143 static struct sock *__iucv_get_sock_by_name(char *nm)
144 {
145         struct sock *sk;
146         struct hlist_node *node;
147
148         sk_for_each(sk, node, &iucv_sk_list.head)
149                 if (!memcmp(&iucv_sk(sk)->src_name, nm, 8))
150                         return sk;
151
152         return NULL;
153 }
154
155 static void iucv_sock_destruct(struct sock *sk)
156 {
157         skb_queue_purge(&sk->sk_receive_queue);
158         skb_queue_purge(&sk->sk_write_queue);
159 }
160
161 /* Cleanup Listen */
162 static void iucv_sock_cleanup_listen(struct sock *parent)
163 {
164         struct sock *sk;
165
166         /* Close non-accepted connections */
167         while ((sk = iucv_accept_dequeue(parent, NULL))) {
168                 iucv_sock_close(sk);
169                 iucv_sock_kill(sk);
170         }
171
172         parent->sk_state = IUCV_CLOSED;
173         sock_set_flag(parent, SOCK_ZAPPED);
174 }
175
176 /* Kill socket */
177 static void iucv_sock_kill(struct sock *sk)
178 {
179         if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
180                 return;
181
182         iucv_sock_unlink(&iucv_sk_list, sk);
183         sock_set_flag(sk, SOCK_DEAD);
184         sock_put(sk);
185 }
186
187 /* Close an IUCV socket */
188 static void iucv_sock_close(struct sock *sk)
189 {
190         unsigned char user_data[16];
191         struct iucv_sock *iucv = iucv_sk(sk);
192         int err;
193         unsigned long timeo;
194
195         iucv_sock_clear_timer(sk);
196         lock_sock(sk);
197
198         switch (sk->sk_state) {
199         case IUCV_LISTEN:
200                 iucv_sock_cleanup_listen(sk);
201                 break;
202
203         case IUCV_CONNECTED:
204         case IUCV_DISCONN:
205                 err = 0;
206
207                 sk->sk_state = IUCV_CLOSING;
208                 sk->sk_state_change(sk);
209
210                 if (!skb_queue_empty(&iucv->send_skb_q)) {
211                         if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
212                                 timeo = sk->sk_lingertime;
213                         else
214                                 timeo = IUCV_DISCONN_TIMEOUT;
215                         err = iucv_sock_wait_state(sk, IUCV_CLOSED, 0, timeo);
216                 }
217
218         case IUCV_CLOSING:   /* fall through */
219                 sk->sk_state = IUCV_CLOSED;
220                 sk->sk_state_change(sk);
221
222                 if (iucv->path) {
223                         low_nmcpy(user_data, iucv->src_name);
224                         high_nmcpy(user_data, iucv->dst_name);
225                         ASCEBC(user_data, sizeof(user_data));
226                         err = iucv_path_sever(iucv->path, user_data);
227                         iucv_path_free(iucv->path);
228                         iucv->path = NULL;
229                 }
230
231                 sk->sk_err = ECONNRESET;
232                 sk->sk_state_change(sk);
233
234                 skb_queue_purge(&iucv->send_skb_q);
235                 skb_queue_purge(&iucv->backlog_skb_q);
236
237                 sock_set_flag(sk, SOCK_ZAPPED);
238                 break;
239
240         default:
241                 sock_set_flag(sk, SOCK_ZAPPED);
242                 break;
243         }
244
245         release_sock(sk);
246         iucv_sock_kill(sk);
247 }
248
249 static void iucv_sock_init(struct sock *sk, struct sock *parent)
250 {
251         if (parent)
252                 sk->sk_type = parent->sk_type;
253 }
254
255 static struct sock *iucv_sock_alloc(struct socket *sock, int proto, gfp_t prio)
256 {
257         struct sock *sk;
258
259         sk = sk_alloc(&init_net, PF_IUCV, prio, &iucv_proto);
260         if (!sk)
261                 return NULL;
262
263         sock_init_data(sock, sk);
264         INIT_LIST_HEAD(&iucv_sk(sk)->accept_q);
265         spin_lock_init(&iucv_sk(sk)->accept_q_lock);
266         skb_queue_head_init(&iucv_sk(sk)->send_skb_q);
267         INIT_LIST_HEAD(&iucv_sk(sk)->message_q.list);
268         spin_lock_init(&iucv_sk(sk)->message_q.lock);
269         skb_queue_head_init(&iucv_sk(sk)->backlog_skb_q);
270         iucv_sk(sk)->send_tag = 0;
271         iucv_sk(sk)->flags = 0;
272         iucv_sk(sk)->msglimit = IUCV_QUEUELEN_DEFAULT;
273         iucv_sk(sk)->path = NULL;
274         memset(&iucv_sk(sk)->src_user_id , 0, 32);
275
276         sk->sk_destruct = iucv_sock_destruct;
277         sk->sk_sndtimeo = IUCV_CONN_TIMEOUT;
278         sk->sk_allocation = GFP_DMA;
279
280         sock_reset_flag(sk, SOCK_ZAPPED);
281
282         sk->sk_protocol = proto;
283         sk->sk_state    = IUCV_OPEN;
284
285         setup_timer(&sk->sk_timer, iucv_sock_timeout, (unsigned long)sk);
286
287         iucv_sock_link(&iucv_sk_list, sk);
288         return sk;
289 }
290
291 /* Create an IUCV socket */
292 static int iucv_sock_create(struct net *net, struct socket *sock, int protocol)
293 {
294         struct sock *sk;
295
296         if (protocol && protocol != PF_IUCV)
297                 return -EPROTONOSUPPORT;
298
299         sock->state = SS_UNCONNECTED;
300
301         switch (sock->type) {
302         case SOCK_STREAM:
303                 sock->ops = &iucv_sock_ops;
304                 break;
305         case SOCK_SEQPACKET:
306                 /* currently, proto ops can handle both sk types */
307                 sock->ops = &iucv_sock_ops;
308                 break;
309         default:
310                 return -ESOCKTNOSUPPORT;
311         }
312
313         sk = iucv_sock_alloc(sock, protocol, GFP_KERNEL);
314         if (!sk)
315                 return -ENOMEM;
316
317         iucv_sock_init(sk, NULL);
318
319         return 0;
320 }
321
322 void iucv_sock_link(struct iucv_sock_list *l, struct sock *sk)
323 {
324         write_lock_bh(&l->lock);
325         sk_add_node(sk, &l->head);
326         write_unlock_bh(&l->lock);
327 }
328
329 void iucv_sock_unlink(struct iucv_sock_list *l, struct sock *sk)
330 {
331         write_lock_bh(&l->lock);
332         sk_del_node_init(sk);
333         write_unlock_bh(&l->lock);
334 }
335
336 void iucv_accept_enqueue(struct sock *parent, struct sock *sk)
337 {
338         unsigned long flags;
339         struct iucv_sock *par = iucv_sk(parent);
340
341         sock_hold(sk);
342         spin_lock_irqsave(&par->accept_q_lock, flags);
343         list_add_tail(&iucv_sk(sk)->accept_q, &par->accept_q);
344         spin_unlock_irqrestore(&par->accept_q_lock, flags);
345         iucv_sk(sk)->parent = parent;
346         parent->sk_ack_backlog++;
347 }
348
349 void iucv_accept_unlink(struct sock *sk)
350 {
351         unsigned long flags;
352         struct iucv_sock *par = iucv_sk(iucv_sk(sk)->parent);
353
354         spin_lock_irqsave(&par->accept_q_lock, flags);
355         list_del_init(&iucv_sk(sk)->accept_q);
356         spin_unlock_irqrestore(&par->accept_q_lock, flags);
357         iucv_sk(sk)->parent->sk_ack_backlog--;
358         iucv_sk(sk)->parent = NULL;
359         sock_put(sk);
360 }
361
362 struct sock *iucv_accept_dequeue(struct sock *parent, struct socket *newsock)
363 {
364         struct iucv_sock *isk, *n;
365         struct sock *sk;
366
367         list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) {
368                 sk = (struct sock *) isk;
369                 lock_sock(sk);
370
371                 if (sk->sk_state == IUCV_CLOSED) {
372                         iucv_accept_unlink(sk);
373                         release_sock(sk);
374                         continue;
375                 }
376
377                 if (sk->sk_state == IUCV_CONNECTED ||
378                     sk->sk_state == IUCV_SEVERED ||
379                     !newsock) {
380                         iucv_accept_unlink(sk);
381                         if (newsock)
382                                 sock_graft(sk, newsock);
383
384                         if (sk->sk_state == IUCV_SEVERED)
385                                 sk->sk_state = IUCV_DISCONN;
386
387                         release_sock(sk);
388                         return sk;
389                 }
390
391                 release_sock(sk);
392         }
393         return NULL;
394 }
395
396 int iucv_sock_wait_state(struct sock *sk, int state, int state2,
397                          unsigned long timeo)
398 {
399         DECLARE_WAITQUEUE(wait, current);
400         int err = 0;
401
402         add_wait_queue(sk->sk_sleep, &wait);
403         while (sk->sk_state != state && sk->sk_state != state2) {
404                 set_current_state(TASK_INTERRUPTIBLE);
405
406                 if (!timeo) {
407                         err = -EAGAIN;
408                         break;
409                 }
410
411                 if (signal_pending(current)) {
412                         err = sock_intr_errno(timeo);
413                         break;
414                 }
415
416                 release_sock(sk);
417                 timeo = schedule_timeout(timeo);
418                 lock_sock(sk);
419
420                 err = sock_error(sk);
421                 if (err)
422                         break;
423         }
424         set_current_state(TASK_RUNNING);
425         remove_wait_queue(sk->sk_sleep, &wait);
426         return err;
427 }
428
429 /* Bind an unbound socket */
430 static int iucv_sock_bind(struct socket *sock, struct sockaddr *addr,
431                           int addr_len)
432 {
433         struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
434         struct sock *sk = sock->sk;
435         struct iucv_sock *iucv;
436         int err;
437
438         /* Verify the input sockaddr */
439         if (!addr || addr->sa_family != AF_IUCV)
440                 return -EINVAL;
441
442         lock_sock(sk);
443         if (sk->sk_state != IUCV_OPEN) {
444                 err = -EBADFD;
445                 goto done;
446         }
447
448         write_lock_bh(&iucv_sk_list.lock);
449
450         iucv = iucv_sk(sk);
451         if (__iucv_get_sock_by_name(sa->siucv_name)) {
452                 err = -EADDRINUSE;
453                 goto done_unlock;
454         }
455         if (iucv->path) {
456                 err = 0;
457                 goto done_unlock;
458         }
459
460         /* Bind the socket */
461         memcpy(iucv->src_name, sa->siucv_name, 8);
462
463         /* Copy the user id */
464         memcpy(iucv->src_user_id, iucv_userid, 8);
465         sk->sk_state = IUCV_BOUND;
466         err = 0;
467
468 done_unlock:
469         /* Release the socket list lock */
470         write_unlock_bh(&iucv_sk_list.lock);
471 done:
472         release_sock(sk);
473         return err;
474 }
475
476 /* Automatically bind an unbound socket */
477 static int iucv_sock_autobind(struct sock *sk)
478 {
479         struct iucv_sock *iucv = iucv_sk(sk);
480         char query_buffer[80];
481         char name[12];
482         int err = 0;
483
484         /* Set the userid and name */
485         cpcmd("QUERY USERID", query_buffer, sizeof(query_buffer), &err);
486         if (unlikely(err))
487                 return -EPROTO;
488
489         memcpy(iucv->src_user_id, query_buffer, 8);
490
491         write_lock_bh(&iucv_sk_list.lock);
492
493         sprintf(name, "%08x", atomic_inc_return(&iucv_sk_list.autobind_name));
494         while (__iucv_get_sock_by_name(name)) {
495                 sprintf(name, "%08x",
496                         atomic_inc_return(&iucv_sk_list.autobind_name));
497         }
498
499         write_unlock_bh(&iucv_sk_list.lock);
500
501         memcpy(&iucv->src_name, name, 8);
502
503         return err;
504 }
505
506 /* Connect an unconnected socket */
507 static int iucv_sock_connect(struct socket *sock, struct sockaddr *addr,
508                              int alen, int flags)
509 {
510         struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
511         struct sock *sk = sock->sk;
512         struct iucv_sock *iucv;
513         unsigned char user_data[16];
514         int err;
515
516         if (addr->sa_family != AF_IUCV || alen < sizeof(struct sockaddr_iucv))
517                 return -EINVAL;
518
519         if (sk->sk_state != IUCV_OPEN && sk->sk_state != IUCV_BOUND)
520                 return -EBADFD;
521
522         if (sk->sk_type != SOCK_STREAM && sk->sk_type != SOCK_SEQPACKET)
523                 return -EINVAL;
524
525         if (sk->sk_state == IUCV_OPEN) {
526                 err = iucv_sock_autobind(sk);
527                 if (unlikely(err))
528                         return err;
529         }
530
531         lock_sock(sk);
532
533         /* Set the destination information */
534         memcpy(iucv_sk(sk)->dst_user_id, sa->siucv_user_id, 8);
535         memcpy(iucv_sk(sk)->dst_name, sa->siucv_name, 8);
536
537         high_nmcpy(user_data, sa->siucv_name);
538         low_nmcpy(user_data, iucv_sk(sk)->src_name);
539         ASCEBC(user_data, sizeof(user_data));
540
541         iucv = iucv_sk(sk);
542         /* Create path. */
543         iucv->path = iucv_path_alloc(iucv->msglimit,
544                                      IUCV_IPRMDATA, GFP_KERNEL);
545         if (!iucv->path) {
546                 err = -ENOMEM;
547                 goto done;
548         }
549         err = iucv_path_connect(iucv->path, &af_iucv_handler,
550                                 sa->siucv_user_id, NULL, user_data, sk);
551         if (err) {
552                 iucv_path_free(iucv->path);
553                 iucv->path = NULL;
554                 switch (err) {
555                 case 0x0b:      /* Target communicator is not logged on */
556                         err = -ENETUNREACH;
557                         break;
558                 case 0x0d:      /* Max connections for this guest exceeded */
559                 case 0x0e:      /* Max connections for target guest exceeded */
560                         err = -EAGAIN;
561                         break;
562                 case 0x0f:      /* Missing IUCV authorization */
563                         err = -EACCES;
564                         break;
565                 default:
566                         err = -ECONNREFUSED;
567                         break;
568                 }
569                 goto done;
570         }
571
572         if (sk->sk_state != IUCV_CONNECTED) {
573                 err = iucv_sock_wait_state(sk, IUCV_CONNECTED, IUCV_DISCONN,
574                                 sock_sndtimeo(sk, flags & O_NONBLOCK));
575         }
576
577         if (sk->sk_state == IUCV_DISCONN) {
578                 err = -ECONNREFUSED;
579         }
580
581         if (err) {
582                 iucv_path_sever(iucv->path, NULL);
583                 iucv_path_free(iucv->path);
584                 iucv->path = NULL;
585         }
586
587 done:
588         release_sock(sk);
589         return err;
590 }
591
592 /* Move a socket into listening state. */
593 static int iucv_sock_listen(struct socket *sock, int backlog)
594 {
595         struct sock *sk = sock->sk;
596         int err;
597
598         lock_sock(sk);
599
600         err = -EINVAL;
601         if (sk->sk_state != IUCV_BOUND)
602                 goto done;
603
604         if (sock->type != SOCK_STREAM && sock->type != SOCK_SEQPACKET)
605                 goto done;
606
607         sk->sk_max_ack_backlog = backlog;
608         sk->sk_ack_backlog = 0;
609         sk->sk_state = IUCV_LISTEN;
610         err = 0;
611
612 done:
613         release_sock(sk);
614         return err;
615 }
616
617 /* Accept a pending connection */
618 static int iucv_sock_accept(struct socket *sock, struct socket *newsock,
619                             int flags)
620 {
621         DECLARE_WAITQUEUE(wait, current);
622         struct sock *sk = sock->sk, *nsk;
623         long timeo;
624         int err = 0;
625
626         lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
627
628         if (sk->sk_state != IUCV_LISTEN) {
629                 err = -EBADFD;
630                 goto done;
631         }
632
633         timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
634
635         /* Wait for an incoming connection */
636         add_wait_queue_exclusive(sk->sk_sleep, &wait);
637         while (!(nsk = iucv_accept_dequeue(sk, newsock))) {
638                 set_current_state(TASK_INTERRUPTIBLE);
639                 if (!timeo) {
640                         err = -EAGAIN;
641                         break;
642                 }
643
644                 release_sock(sk);
645                 timeo = schedule_timeout(timeo);
646                 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
647
648                 if (sk->sk_state != IUCV_LISTEN) {
649                         err = -EBADFD;
650                         break;
651                 }
652
653                 if (signal_pending(current)) {
654                         err = sock_intr_errno(timeo);
655                         break;
656                 }
657         }
658
659         set_current_state(TASK_RUNNING);
660         remove_wait_queue(sk->sk_sleep, &wait);
661
662         if (err)
663                 goto done;
664
665         newsock->state = SS_CONNECTED;
666
667 done:
668         release_sock(sk);
669         return err;
670 }
671
672 static int iucv_sock_getname(struct socket *sock, struct sockaddr *addr,
673                              int *len, int peer)
674 {
675         struct sockaddr_iucv *siucv = (struct sockaddr_iucv *) addr;
676         struct sock *sk = sock->sk;
677
678         addr->sa_family = AF_IUCV;
679         *len = sizeof(struct sockaddr_iucv);
680
681         if (peer) {
682                 memcpy(siucv->siucv_user_id, iucv_sk(sk)->dst_user_id, 8);
683                 memcpy(siucv->siucv_name, &iucv_sk(sk)->dst_name, 8);
684         } else {
685                 memcpy(siucv->siucv_user_id, iucv_sk(sk)->src_user_id, 8);
686                 memcpy(siucv->siucv_name, iucv_sk(sk)->src_name, 8);
687         }
688         memset(&siucv->siucv_port, 0, sizeof(siucv->siucv_port));
689         memset(&siucv->siucv_addr, 0, sizeof(siucv->siucv_addr));
690         memset(siucv->siucv_nodeid, 0, sizeof(siucv->siucv_nodeid));
691
692         return 0;
693 }
694
695 /**
696  * iucv_send_iprm() - Send socket data in parameter list of an iucv message.
697  * @path:       IUCV path
698  * @msg:        Pointer to a struct iucv_message
699  * @skb:        The socket data to send, skb->len MUST BE <= 7
700  *
701  * Send the socket data in the parameter list in the iucv message
702  * (IUCV_IPRMDATA). The socket data is stored at index 0 to 6 in the parameter
703  * list and the socket data len at index 7 (last byte).
704  * See also iucv_msg_length().
705  *
706  * Returns the error code from the iucv_message_send() call.
707  */
708 static int iucv_send_iprm(struct iucv_path *path, struct iucv_message *msg,
709                           struct sk_buff *skb)
710 {
711         u8 prmdata[8];
712
713         memcpy(prmdata, (void *) skb->data, skb->len);
714         prmdata[7] = 0xff - (u8) skb->len;
715         return iucv_message_send(path, msg, IUCV_IPRMDATA, 0,
716                                  (void *) prmdata, 8);
717 }
718
719 static int iucv_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
720                              struct msghdr *msg, size_t len)
721 {
722         struct sock *sk = sock->sk;
723         struct iucv_sock *iucv = iucv_sk(sk);
724         struct sk_buff *skb;
725         struct iucv_message txmsg;
726         struct cmsghdr *cmsg;
727         int cmsg_done;
728         char user_id[9];
729         char appl_id[9];
730         int err;
731
732         err = sock_error(sk);
733         if (err)
734                 return err;
735
736         if (msg->msg_flags & MSG_OOB)
737                 return -EOPNOTSUPP;
738
739         /* SOCK_SEQPACKET: we do not support segmented records */
740         if (sk->sk_type == SOCK_SEQPACKET && !(msg->msg_flags & MSG_EOR))
741                 return -EOPNOTSUPP;
742
743         lock_sock(sk);
744
745         if (sk->sk_shutdown & SEND_SHUTDOWN) {
746                 err = -EPIPE;
747                 goto out;
748         }
749
750         /* Return if the socket is not in connected state */
751         if (sk->sk_state != IUCV_CONNECTED) {
752                 err = -ENOTCONN;
753                 goto out;
754         }
755
756         /* initialize defaults */
757         cmsg_done   = 0;        /* check for duplicate headers */
758         txmsg.class = 0;
759
760         /* iterate over control messages */
761         for (cmsg = CMSG_FIRSTHDR(msg); cmsg;
762                 cmsg = CMSG_NXTHDR(msg, cmsg)) {
763
764                 if (!CMSG_OK(msg, cmsg)) {
765                         err = -EINVAL;
766                         goto out;
767                 }
768
769                 if (cmsg->cmsg_level != SOL_IUCV)
770                         continue;
771
772                 if (cmsg->cmsg_type & cmsg_done) {
773                         err = -EINVAL;
774                         goto out;
775                 }
776                 cmsg_done |= cmsg->cmsg_type;
777
778                 switch (cmsg->cmsg_type) {
779                 case SCM_IUCV_TRGCLS:
780                         if (cmsg->cmsg_len != CMSG_LEN(TRGCLS_SIZE)) {
781                                 err = -EINVAL;
782                                 goto out;
783                         }
784
785                         /* set iucv message target class */
786                         memcpy(&txmsg.class,
787                                 (void *) CMSG_DATA(cmsg), TRGCLS_SIZE);
788
789                         break;
790
791                 default:
792                         err = -EINVAL;
793                         goto out;
794                         break;
795                 }
796         }
797
798         /* allocate one skb for each iucv message:
799          * this is fine for SOCK_SEQPACKET (unless we want to support
800          * segmented records using the MSG_EOR flag), but
801          * for SOCK_STREAM we might want to improve it in future */
802         skb = sock_alloc_send_skb(sk, len, msg->msg_flags & MSG_DONTWAIT,
803                                   &err);
804         if (!skb)
805                 goto out;
806         if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
807                 err = -EFAULT;
808                 goto fail;
809         }
810
811         /* increment and save iucv message tag for msg_completion cbk */
812         txmsg.tag = iucv->send_tag++;
813         memcpy(CB_TAG(skb), &txmsg.tag, CB_TAG_LEN);
814         skb_queue_tail(&iucv->send_skb_q, skb);
815
816         if (((iucv->path->flags & IUCV_IPRMDATA) & iucv->flags)
817               && skb->len <= 7) {
818                 err = iucv_send_iprm(iucv->path, &txmsg, skb);
819
820                 /* on success: there is no message_complete callback
821                  * for an IPRMDATA msg; remove skb from send queue */
822                 if (err == 0) {
823                         skb_unlink(skb, &iucv->send_skb_q);
824                         kfree_skb(skb);
825                 }
826
827                 /* this error should never happen since the
828                  * IUCV_IPRMDATA path flag is set... sever path */
829                 if (err == 0x15) {
830                         iucv_path_sever(iucv->path, NULL);
831                         skb_unlink(skb, &iucv->send_skb_q);
832                         err = -EPIPE;
833                         goto fail;
834                 }
835         } else
836                 err = iucv_message_send(iucv->path, &txmsg, 0, 0,
837                                         (void *) skb->data, skb->len);
838         if (err) {
839                 if (err == 3) {
840                         user_id[8] = 0;
841                         memcpy(user_id, iucv->dst_user_id, 8);
842                         appl_id[8] = 0;
843                         memcpy(appl_id, iucv->dst_name, 8);
844                         pr_err("Application %s on z/VM guest %s"
845                                 " exceeds message limit\n",
846                                 appl_id, user_id);
847                 }
848                 skb_unlink(skb, &iucv->send_skb_q);
849                 err = -EPIPE;
850                 goto fail;
851         }
852
853         release_sock(sk);
854         return len;
855
856 fail:
857         kfree_skb(skb);
858 out:
859         release_sock(sk);
860         return err;
861 }
862
863 static int iucv_fragment_skb(struct sock *sk, struct sk_buff *skb, int len)
864 {
865         int dataleft, size, copied = 0;
866         struct sk_buff *nskb;
867
868         dataleft = len;
869         while (dataleft) {
870                 if (dataleft >= sk->sk_rcvbuf / 4)
871                         size = sk->sk_rcvbuf / 4;
872                 else
873                         size = dataleft;
874
875                 nskb = alloc_skb(size, GFP_ATOMIC | GFP_DMA);
876                 if (!nskb)
877                         return -ENOMEM;
878
879                 /* copy target class to control buffer of new skb */
880                 memcpy(CB_TRGCLS(nskb), CB_TRGCLS(skb), CB_TRGCLS_LEN);
881
882                 /* copy data fragment */
883                 memcpy(nskb->data, skb->data + copied, size);
884                 copied += size;
885                 dataleft -= size;
886
887                 skb_reset_transport_header(nskb);
888                 skb_reset_network_header(nskb);
889                 nskb->len = size;
890
891                 skb_queue_tail(&iucv_sk(sk)->backlog_skb_q, nskb);
892         }
893
894         return 0;
895 }
896
897 static void iucv_process_message(struct sock *sk, struct sk_buff *skb,
898                                  struct iucv_path *path,
899                                  struct iucv_message *msg)
900 {
901         int rc;
902         unsigned int len;
903
904         len = iucv_msg_length(msg);
905
906         /* store msg target class in the second 4 bytes of skb ctrl buffer */
907         /* Note: the first 4 bytes are reserved for msg tag */
908         memcpy(CB_TRGCLS(skb), &msg->class, CB_TRGCLS_LEN);
909
910         /* check for special IPRM messages (e.g. iucv_sock_shutdown) */
911         if ((msg->flags & IUCV_IPRMDATA) && len > 7) {
912                 if (memcmp(msg->rmmsg, iprm_shutdown, 8) == 0) {
913                         skb->data = NULL;
914                         skb->len = 0;
915                 }
916         } else {
917                 rc = iucv_message_receive(path, msg, msg->flags & IUCV_IPRMDATA,
918                                           skb->data, len, NULL);
919                 if (rc) {
920                         kfree_skb(skb);
921                         return;
922                 }
923                 /* we need to fragment iucv messages for SOCK_STREAM only;
924                  * for SOCK_SEQPACKET, it is only relevant if we support
925                  * record segmentation using MSG_EOR (see also recvmsg()) */
926                 if (sk->sk_type == SOCK_STREAM &&
927                     skb->truesize >= sk->sk_rcvbuf / 4) {
928                         rc = iucv_fragment_skb(sk, skb, len);
929                         kfree_skb(skb);
930                         skb = NULL;
931                         if (rc) {
932                                 iucv_path_sever(path, NULL);
933                                 return;
934                         }
935                         skb = skb_dequeue(&iucv_sk(sk)->backlog_skb_q);
936                 } else {
937                         skb_reset_transport_header(skb);
938                         skb_reset_network_header(skb);
939                         skb->len = len;
940                 }
941         }
942
943         if (sock_queue_rcv_skb(sk, skb))
944                 skb_queue_head(&iucv_sk(sk)->backlog_skb_q, skb);
945 }
946
947 static void iucv_process_message_q(struct sock *sk)
948 {
949         struct iucv_sock *iucv = iucv_sk(sk);
950         struct sk_buff *skb;
951         struct sock_msg_q *p, *n;
952
953         list_for_each_entry_safe(p, n, &iucv->message_q.list, list) {
954                 skb = alloc_skb(iucv_msg_length(&p->msg), GFP_ATOMIC | GFP_DMA);
955                 if (!skb)
956                         break;
957                 iucv_process_message(sk, skb, p->path, &p->msg);
958                 list_del(&p->list);
959                 kfree(p);
960                 if (!skb_queue_empty(&iucv->backlog_skb_q))
961                         break;
962         }
963 }
964
965 static int iucv_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
966                              struct msghdr *msg, size_t len, int flags)
967 {
968         int noblock = flags & MSG_DONTWAIT;
969         struct sock *sk = sock->sk;
970         struct iucv_sock *iucv = iucv_sk(sk);
971         unsigned int copied, rlen;
972         struct sk_buff *skb, *rskb, *cskb;
973         int err = 0;
974
975         if ((sk->sk_state == IUCV_DISCONN || sk->sk_state == IUCV_SEVERED) &&
976             skb_queue_empty(&iucv->backlog_skb_q) &&
977             skb_queue_empty(&sk->sk_receive_queue) &&
978             list_empty(&iucv->message_q.list))
979                 return 0;
980
981         if (flags & (MSG_OOB))
982                 return -EOPNOTSUPP;
983
984         /* receive/dequeue next skb:
985          * the function understands MSG_PEEK and, thus, does not dequeue skb */
986         skb = skb_recv_datagram(sk, flags, noblock, &err);
987         if (!skb) {
988                 if (sk->sk_shutdown & RCV_SHUTDOWN)
989                         return 0;
990                 return err;
991         }
992
993         rlen   = skb->len;              /* real length of skb */
994         copied = min_t(unsigned int, rlen, len);
995
996         cskb = skb;
997         if (memcpy_toiovec(msg->msg_iov, cskb->data, copied)) {
998                 if (!(flags & MSG_PEEK))
999                         skb_queue_head(&sk->sk_receive_queue, skb);
1000                 return -EFAULT;
1001         }
1002
1003         /* SOCK_SEQPACKET: set MSG_TRUNC if recv buf size is too small */
1004         if (sk->sk_type == SOCK_SEQPACKET) {
1005                 if (copied < rlen)
1006                         msg->msg_flags |= MSG_TRUNC;
1007                 /* each iucv message contains a complete record */
1008                 msg->msg_flags |= MSG_EOR;
1009         }
1010
1011         /* create control message to store iucv msg target class:
1012          * get the trgcls from the control buffer of the skb due to
1013          * fragmentation of original iucv message. */
1014         err = put_cmsg(msg, SOL_IUCV, SCM_IUCV_TRGCLS,
1015                         CB_TRGCLS_LEN, CB_TRGCLS(skb));
1016         if (err) {
1017                 if (!(flags & MSG_PEEK))
1018                         skb_queue_head(&sk->sk_receive_queue, skb);
1019                 return err;
1020         }
1021
1022         /* Mark read part of skb as used */
1023         if (!(flags & MSG_PEEK)) {
1024
1025                 /* SOCK_STREAM: re-queue skb if it contains unreceived data */
1026                 if (sk->sk_type == SOCK_STREAM) {
1027                         skb_pull(skb, copied);
1028                         if (skb->len) {
1029                                 skb_queue_head(&sk->sk_receive_queue, skb);
1030                                 goto done;
1031                         }
1032                 }
1033
1034                 kfree_skb(skb);
1035
1036                 /* Queue backlog skbs */
1037                 rskb = skb_dequeue(&iucv->backlog_skb_q);
1038                 while (rskb) {
1039                         if (sock_queue_rcv_skb(sk, rskb)) {
1040                                 skb_queue_head(&iucv->backlog_skb_q,
1041                                                 rskb);
1042                                 break;
1043                         } else {
1044                                 rskb = skb_dequeue(&iucv->backlog_skb_q);
1045                         }
1046                 }
1047                 if (skb_queue_empty(&iucv->backlog_skb_q)) {
1048                         spin_lock_bh(&iucv->message_q.lock);
1049                         if (!list_empty(&iucv->message_q.list))
1050                                 iucv_process_message_q(sk);
1051                         spin_unlock_bh(&iucv->message_q.lock);
1052                 }
1053         }
1054
1055 done:
1056         /* SOCK_SEQPACKET: return real length if MSG_TRUNC is set */
1057         if (sk->sk_type == SOCK_SEQPACKET && (flags & MSG_TRUNC))
1058                 copied = rlen;
1059
1060         return copied;
1061 }
1062
1063 static inline unsigned int iucv_accept_poll(struct sock *parent)
1064 {
1065         struct iucv_sock *isk, *n;
1066         struct sock *sk;
1067
1068         list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) {
1069                 sk = (struct sock *) isk;
1070
1071                 if (sk->sk_state == IUCV_CONNECTED)
1072                         return POLLIN | POLLRDNORM;
1073         }
1074
1075         return 0;
1076 }
1077
1078 unsigned int iucv_sock_poll(struct file *file, struct socket *sock,
1079                             poll_table *wait)
1080 {
1081         struct sock *sk = sock->sk;
1082         unsigned int mask = 0;
1083
1084         poll_wait(file, sk->sk_sleep, wait);
1085
1086         if (sk->sk_state == IUCV_LISTEN)
1087                 return iucv_accept_poll(sk);
1088
1089         if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
1090                 mask |= POLLERR;
1091
1092         if (sk->sk_shutdown & RCV_SHUTDOWN)
1093                 mask |= POLLRDHUP;
1094
1095         if (sk->sk_shutdown == SHUTDOWN_MASK)
1096                 mask |= POLLHUP;
1097
1098         if (!skb_queue_empty(&sk->sk_receive_queue) ||
1099             (sk->sk_shutdown & RCV_SHUTDOWN))
1100                 mask |= POLLIN | POLLRDNORM;
1101
1102         if (sk->sk_state == IUCV_CLOSED)
1103                 mask |= POLLHUP;
1104
1105         if (sk->sk_state == IUCV_DISCONN || sk->sk_state == IUCV_SEVERED)
1106                 mask |= POLLIN;
1107
1108         if (sock_writeable(sk))
1109                 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
1110         else
1111                 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
1112
1113         return mask;
1114 }
1115
1116 static int iucv_sock_shutdown(struct socket *sock, int how)
1117 {
1118         struct sock *sk = sock->sk;
1119         struct iucv_sock *iucv = iucv_sk(sk);
1120         struct iucv_message txmsg;
1121         int err = 0;
1122
1123         how++;
1124
1125         if ((how & ~SHUTDOWN_MASK) || !how)
1126                 return -EINVAL;
1127
1128         lock_sock(sk);
1129         switch (sk->sk_state) {
1130         case IUCV_DISCONN:
1131         case IUCV_CLOSING:
1132         case IUCV_SEVERED:
1133         case IUCV_CLOSED:
1134                 err = -ENOTCONN;
1135                 goto fail;
1136
1137         default:
1138                 sk->sk_shutdown |= how;
1139                 break;
1140         }
1141
1142         if (how == SEND_SHUTDOWN || how == SHUTDOWN_MASK) {
1143                 txmsg.class = 0;
1144                 txmsg.tag = 0;
1145                 err = iucv_message_send(iucv->path, &txmsg, IUCV_IPRMDATA, 0,
1146                                         (void *) iprm_shutdown, 8);
1147                 if (err) {
1148                         switch (err) {
1149                         case 1:
1150                                 err = -ENOTCONN;
1151                                 break;
1152                         case 2:
1153                                 err = -ECONNRESET;
1154                                 break;
1155                         default:
1156                                 err = -ENOTCONN;
1157                                 break;
1158                         }
1159                 }
1160         }
1161
1162         if (how == RCV_SHUTDOWN || how == SHUTDOWN_MASK) {
1163                 err = iucv_path_quiesce(iucv_sk(sk)->path, NULL);
1164                 if (err)
1165                         err = -ENOTCONN;
1166
1167                 skb_queue_purge(&sk->sk_receive_queue);
1168         }
1169
1170         /* Wake up anyone sleeping in poll */
1171         sk->sk_state_change(sk);
1172
1173 fail:
1174         release_sock(sk);
1175         return err;
1176 }
1177
1178 static int iucv_sock_release(struct socket *sock)
1179 {
1180         struct sock *sk = sock->sk;
1181         int err = 0;
1182
1183         if (!sk)
1184                 return 0;
1185
1186         iucv_sock_close(sk);
1187
1188         /* Unregister with IUCV base support */
1189         if (iucv_sk(sk)->path) {
1190                 iucv_path_sever(iucv_sk(sk)->path, NULL);
1191                 iucv_path_free(iucv_sk(sk)->path);
1192                 iucv_sk(sk)->path = NULL;
1193         }
1194
1195         sock_orphan(sk);
1196         iucv_sock_kill(sk);
1197         return err;
1198 }
1199
1200 /* getsockopt and setsockopt */
1201 static int iucv_sock_setsockopt(struct socket *sock, int level, int optname,
1202                                 char __user *optval, int optlen)
1203 {
1204         struct sock *sk = sock->sk;
1205         struct iucv_sock *iucv = iucv_sk(sk);
1206         int val;
1207         int rc;
1208
1209         if (level != SOL_IUCV)
1210                 return -ENOPROTOOPT;
1211
1212         if (optlen < sizeof(int))
1213                 return -EINVAL;
1214
1215         if (get_user(val, (int __user *) optval))
1216                 return -EFAULT;
1217
1218         rc = 0;
1219
1220         lock_sock(sk);
1221         switch (optname) {
1222         case SO_IPRMDATA_MSG:
1223                 if (val)
1224                         iucv->flags |= IUCV_IPRMDATA;
1225                 else
1226                         iucv->flags &= ~IUCV_IPRMDATA;
1227                 break;
1228         case SO_MSGLIMIT:
1229                 switch (sk->sk_state) {
1230                 case IUCV_OPEN:
1231                 case IUCV_BOUND:
1232                         if (val < 1 || val > (u16)(~0))
1233                                 rc = -EINVAL;
1234                         else
1235                                 iucv->msglimit = val;
1236                         break;
1237                 default:
1238                         rc = -EINVAL;
1239                         break;
1240                 }
1241                 break;
1242         default:
1243                 rc = -ENOPROTOOPT;
1244                 break;
1245         }
1246         release_sock(sk);
1247
1248         return rc;
1249 }
1250
1251 static int iucv_sock_getsockopt(struct socket *sock, int level, int optname,
1252                                 char __user *optval, int __user *optlen)
1253 {
1254         struct sock *sk = sock->sk;
1255         struct iucv_sock *iucv = iucv_sk(sk);
1256         int val, len;
1257
1258         if (level != SOL_IUCV)
1259                 return -ENOPROTOOPT;
1260
1261         if (get_user(len, optlen))
1262                 return -EFAULT;
1263
1264         if (len < 0)
1265                 return -EINVAL;
1266
1267         len = min_t(unsigned int, len, sizeof(int));
1268
1269         switch (optname) {
1270         case SO_IPRMDATA_MSG:
1271                 val = (iucv->flags & IUCV_IPRMDATA) ? 1 : 0;
1272                 break;
1273         case SO_MSGLIMIT:
1274                 lock_sock(sk);
1275                 val = (iucv->path != NULL) ? iucv->path->msglim /* connected */
1276                                            : iucv->msglimit;    /* default */
1277                 release_sock(sk);
1278                 break;
1279         default:
1280                 return -ENOPROTOOPT;
1281         }
1282
1283         if (put_user(len, optlen))
1284                 return -EFAULT;
1285         if (copy_to_user(optval, &val, len))
1286                 return -EFAULT;
1287
1288         return 0;
1289 }
1290
1291
1292 /* Callback wrappers - called from iucv base support */
1293 static int iucv_callback_connreq(struct iucv_path *path,
1294                                  u8 ipvmid[8], u8 ipuser[16])
1295 {
1296         unsigned char user_data[16];
1297         unsigned char nuser_data[16];
1298         unsigned char src_name[8];
1299         struct hlist_node *node;
1300         struct sock *sk, *nsk;
1301         struct iucv_sock *iucv, *niucv;
1302         int err;
1303
1304         memcpy(src_name, ipuser, 8);
1305         EBCASC(src_name, 8);
1306         /* Find out if this path belongs to af_iucv. */
1307         read_lock(&iucv_sk_list.lock);
1308         iucv = NULL;
1309         sk = NULL;
1310         sk_for_each(sk, node, &iucv_sk_list.head)
1311                 if (sk->sk_state == IUCV_LISTEN &&
1312                     !memcmp(&iucv_sk(sk)->src_name, src_name, 8)) {
1313                         /*
1314                          * Found a listening socket with
1315                          * src_name == ipuser[0-7].
1316                          */
1317                         iucv = iucv_sk(sk);
1318                         break;
1319                 }
1320         read_unlock(&iucv_sk_list.lock);
1321         if (!iucv)
1322                 /* No socket found, not one of our paths. */
1323                 return -EINVAL;
1324
1325         bh_lock_sock(sk);
1326
1327         /* Check if parent socket is listening */
1328         low_nmcpy(user_data, iucv->src_name);
1329         high_nmcpy(user_data, iucv->dst_name);
1330         ASCEBC(user_data, sizeof(user_data));
1331         if (sk->sk_state != IUCV_LISTEN) {
1332                 err = iucv_path_sever(path, user_data);
1333                 iucv_path_free(path);
1334                 goto fail;
1335         }
1336
1337         /* Check for backlog size */
1338         if (sk_acceptq_is_full(sk)) {
1339                 err = iucv_path_sever(path, user_data);
1340                 iucv_path_free(path);
1341                 goto fail;
1342         }
1343
1344         /* Create the new socket */
1345         nsk = iucv_sock_alloc(NULL, sk->sk_type, GFP_ATOMIC);
1346         if (!nsk) {
1347                 err = iucv_path_sever(path, user_data);
1348                 iucv_path_free(path);
1349                 goto fail;
1350         }
1351
1352         niucv = iucv_sk(nsk);
1353         iucv_sock_init(nsk, sk);
1354
1355         /* Set the new iucv_sock */
1356         memcpy(niucv->dst_name, ipuser + 8, 8);
1357         EBCASC(niucv->dst_name, 8);
1358         memcpy(niucv->dst_user_id, ipvmid, 8);
1359         memcpy(niucv->src_name, iucv->src_name, 8);
1360         memcpy(niucv->src_user_id, iucv->src_user_id, 8);
1361         niucv->path = path;
1362
1363         /* Call iucv_accept */
1364         high_nmcpy(nuser_data, ipuser + 8);
1365         memcpy(nuser_data + 8, niucv->src_name, 8);
1366         ASCEBC(nuser_data + 8, 8);
1367
1368         /* set message limit for path based on msglimit of accepting socket */
1369         niucv->msglimit = iucv->msglimit;
1370         path->msglim = iucv->msglimit;
1371         err = iucv_path_accept(path, &af_iucv_handler, nuser_data, nsk);
1372         if (err) {
1373                 err = iucv_path_sever(path, user_data);
1374                 iucv_path_free(path);
1375                 iucv_sock_kill(nsk);
1376                 goto fail;
1377         }
1378
1379         iucv_accept_enqueue(sk, nsk);
1380
1381         /* Wake up accept */
1382         nsk->sk_state = IUCV_CONNECTED;
1383         sk->sk_data_ready(sk, 1);
1384         err = 0;
1385 fail:
1386         bh_unlock_sock(sk);
1387         return 0;
1388 }
1389
1390 static void iucv_callback_connack(struct iucv_path *path, u8 ipuser[16])
1391 {
1392         struct sock *sk = path->private;
1393
1394         sk->sk_state = IUCV_CONNECTED;
1395         sk->sk_state_change(sk);
1396 }
1397
1398 static void iucv_callback_rx(struct iucv_path *path, struct iucv_message *msg)
1399 {
1400         struct sock *sk = path->private;
1401         struct iucv_sock *iucv = iucv_sk(sk);
1402         struct sk_buff *skb;
1403         struct sock_msg_q *save_msg;
1404         int len;
1405
1406         if (sk->sk_shutdown & RCV_SHUTDOWN) {
1407                 iucv_message_reject(path, msg);
1408                 return;
1409         }
1410
1411         spin_lock(&iucv->message_q.lock);
1412
1413         if (!list_empty(&iucv->message_q.list) ||
1414             !skb_queue_empty(&iucv->backlog_skb_q))
1415                 goto save_message;
1416
1417         len = atomic_read(&sk->sk_rmem_alloc);
1418         len += iucv_msg_length(msg) + sizeof(struct sk_buff);
1419         if (len > sk->sk_rcvbuf)
1420                 goto save_message;
1421
1422         skb = alloc_skb(iucv_msg_length(msg), GFP_ATOMIC | GFP_DMA);
1423         if (!skb)
1424                 goto save_message;
1425
1426         iucv_process_message(sk, skb, path, msg);
1427         goto out_unlock;
1428
1429 save_message:
1430         save_msg = kzalloc(sizeof(struct sock_msg_q), GFP_ATOMIC | GFP_DMA);
1431         if (!save_msg)
1432                 return;
1433         save_msg->path = path;
1434         save_msg->msg = *msg;
1435
1436         list_add_tail(&save_msg->list, &iucv->message_q.list);
1437
1438 out_unlock:
1439         spin_unlock(&iucv->message_q.lock);
1440 }
1441
1442 static void iucv_callback_txdone(struct iucv_path *path,
1443                                  struct iucv_message *msg)
1444 {
1445         struct sock *sk = path->private;
1446         struct sk_buff *this = NULL;
1447         struct sk_buff_head *list = &iucv_sk(sk)->send_skb_q;
1448         struct sk_buff *list_skb = list->next;
1449         unsigned long flags;
1450
1451         if (!skb_queue_empty(list)) {
1452                 spin_lock_irqsave(&list->lock, flags);
1453
1454                 while (list_skb != (struct sk_buff *)list) {
1455                         if (!memcmp(&msg->tag, CB_TAG(list_skb), CB_TAG_LEN)) {
1456                                 this = list_skb;
1457                                 break;
1458                         }
1459                         list_skb = list_skb->next;
1460                 }
1461                 if (this)
1462                         __skb_unlink(this, list);
1463
1464                 spin_unlock_irqrestore(&list->lock, flags);
1465
1466                 kfree_skb(this);
1467         }
1468         BUG_ON(!this);
1469
1470         if (sk->sk_state == IUCV_CLOSING) {
1471                 if (skb_queue_empty(&iucv_sk(sk)->send_skb_q)) {
1472                         sk->sk_state = IUCV_CLOSED;
1473                         sk->sk_state_change(sk);
1474                 }
1475         }
1476
1477 }
1478
1479 static void iucv_callback_connrej(struct iucv_path *path, u8 ipuser[16])
1480 {
1481         struct sock *sk = path->private;
1482
1483         if (!list_empty(&iucv_sk(sk)->accept_q))
1484                 sk->sk_state = IUCV_SEVERED;
1485         else
1486                 sk->sk_state = IUCV_DISCONN;
1487
1488         sk->sk_state_change(sk);
1489 }
1490
1491 /* called if the other communication side shuts down its RECV direction;
1492  * in turn, the callback sets SEND_SHUTDOWN to disable sending of data.
1493  */
1494 static void iucv_callback_shutdown(struct iucv_path *path, u8 ipuser[16])
1495 {
1496         struct sock *sk = path->private;
1497
1498         bh_lock_sock(sk);
1499         if (sk->sk_state != IUCV_CLOSED) {
1500                 sk->sk_shutdown |= SEND_SHUTDOWN;
1501                 sk->sk_state_change(sk);
1502         }
1503         bh_unlock_sock(sk);
1504 }
1505
1506 static struct proto_ops iucv_sock_ops = {
1507         .family         = PF_IUCV,
1508         .owner          = THIS_MODULE,
1509         .release        = iucv_sock_release,
1510         .bind           = iucv_sock_bind,
1511         .connect        = iucv_sock_connect,
1512         .listen         = iucv_sock_listen,
1513         .accept         = iucv_sock_accept,
1514         .getname        = iucv_sock_getname,
1515         .sendmsg        = iucv_sock_sendmsg,
1516         .recvmsg        = iucv_sock_recvmsg,
1517         .poll           = iucv_sock_poll,
1518         .ioctl          = sock_no_ioctl,
1519         .mmap           = sock_no_mmap,
1520         .socketpair     = sock_no_socketpair,
1521         .shutdown       = iucv_sock_shutdown,
1522         .setsockopt     = iucv_sock_setsockopt,
1523         .getsockopt     = iucv_sock_getsockopt,
1524 };
1525
1526 static struct net_proto_family iucv_sock_family_ops = {
1527         .family = AF_IUCV,
1528         .owner  = THIS_MODULE,
1529         .create = iucv_sock_create,
1530 };
1531
1532 static int __init afiucv_init(void)
1533 {
1534         int err;
1535
1536         if (!MACHINE_IS_VM) {
1537                 pr_err("The af_iucv module cannot be loaded"
1538                        " without z/VM\n");
1539                 err = -EPROTONOSUPPORT;
1540                 goto out;
1541         }
1542         cpcmd("QUERY USERID", iucv_userid, sizeof(iucv_userid), &err);
1543         if (unlikely(err)) {
1544                 WARN_ON(err);
1545                 err = -EPROTONOSUPPORT;
1546                 goto out;
1547         }
1548
1549         err = iucv_register(&af_iucv_handler, 0);
1550         if (err)
1551                 goto out;
1552         err = proto_register(&iucv_proto, 0);
1553         if (err)
1554                 goto out_iucv;
1555         err = sock_register(&iucv_sock_family_ops);
1556         if (err)
1557                 goto out_proto;
1558         return 0;
1559
1560 out_proto:
1561         proto_unregister(&iucv_proto);
1562 out_iucv:
1563         iucv_unregister(&af_iucv_handler, 0);
1564 out:
1565         return err;
1566 }
1567
1568 static void __exit afiucv_exit(void)
1569 {
1570         sock_unregister(PF_IUCV);
1571         proto_unregister(&iucv_proto);
1572         iucv_unregister(&af_iucv_handler, 0);
1573 }
1574
1575 module_init(afiucv_init);
1576 module_exit(afiucv_exit);
1577
1578 MODULE_AUTHOR("Jennifer Hunt <jenhunt@us.ibm.com>");
1579 MODULE_DESCRIPTION("IUCV Sockets ver " VERSION);
1580 MODULE_VERSION(VERSION);
1581 MODULE_LICENSE("GPL");
1582 MODULE_ALIAS_NETPROTO(PF_IUCV);