[NET]: Initialize the network namespace of network devices.
[linux-2.6] / 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 #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>
25
26 #include <net/iucv/iucv.h>
27 #include <net/iucv/af_iucv.h>
28
29 #define CONFIG_IUCV_SOCK_DEBUG 1
30
31 #define IPRMDATA 0x80
32 #define VERSION "1.0"
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 /* 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],
49                                  u8 ipuser[16]);
50 static void iucv_callback_connrej(struct iucv_path *, u8 ipuser[16]);
51
52 static struct iucv_sock_list iucv_sk_list = {
53         .lock = RW_LOCK_UNLOCKED,
54         .autobind_name = ATOMIC_INIT(0)
55 };
56
57 static struct iucv_handler af_iucv_handler = {
58         .path_pending     = iucv_callback_connreq,
59         .path_complete    = iucv_callback_connack,
60         .path_severed     = iucv_callback_connrej,
61         .message_pending  = iucv_callback_rx,
62         .message_complete = iucv_callback_txdone
63 };
64
65 static inline void high_nmcpy(unsigned char *dst, char *src)
66 {
67        memcpy(dst, src, 8);
68 }
69
70 static inline void low_nmcpy(unsigned char *dst, char *src)
71 {
72        memcpy(&dst[8], src, 8);
73 }
74
75 /* Timers */
76 static void iucv_sock_timeout(unsigned long arg)
77 {
78         struct sock *sk = (struct sock *)arg;
79
80         bh_lock_sock(sk);
81         sk->sk_err = ETIMEDOUT;
82         sk->sk_state_change(sk);
83         bh_unlock_sock(sk);
84
85         iucv_sock_kill(sk);
86         sock_put(sk);
87 }
88
89 static void iucv_sock_clear_timer(struct sock *sk)
90 {
91         sk_stop_timer(sk, &sk->sk_timer);
92 }
93
94 static void iucv_sock_init_timer(struct sock *sk)
95 {
96         init_timer(&sk->sk_timer);
97         sk->sk_timer.function = iucv_sock_timeout;
98         sk->sk_timer.data = (unsigned long)sk;
99 }
100
101 static struct sock *__iucv_get_sock_by_name(char *nm)
102 {
103         struct sock *sk;
104         struct hlist_node *node;
105
106         sk_for_each(sk, node, &iucv_sk_list.head)
107                 if (!memcmp(&iucv_sk(sk)->src_name, nm, 8))
108                         return sk;
109
110         return NULL;
111 }
112
113 static void iucv_sock_destruct(struct sock *sk)
114 {
115         skb_queue_purge(&sk->sk_receive_queue);
116         skb_queue_purge(&sk->sk_write_queue);
117 }
118
119 /* Cleanup Listen */
120 static void iucv_sock_cleanup_listen(struct sock *parent)
121 {
122         struct sock *sk;
123
124         /* Close non-accepted connections */
125         while ((sk = iucv_accept_dequeue(parent, NULL))) {
126                 iucv_sock_close(sk);
127                 iucv_sock_kill(sk);
128         }
129
130         parent->sk_state = IUCV_CLOSED;
131         sock_set_flag(parent, SOCK_ZAPPED);
132 }
133
134 /* Kill socket */
135 static void iucv_sock_kill(struct sock *sk)
136 {
137         if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
138                 return;
139
140         iucv_sock_unlink(&iucv_sk_list, sk);
141         sock_set_flag(sk, SOCK_DEAD);
142         sock_put(sk);
143 }
144
145 /* Close an IUCV socket */
146 static void iucv_sock_close(struct sock *sk)
147 {
148         unsigned char user_data[16];
149         struct iucv_sock *iucv = iucv_sk(sk);
150         int err;
151         unsigned long timeo;
152
153         iucv_sock_clear_timer(sk);
154         lock_sock(sk);
155
156         switch (sk->sk_state) {
157         case IUCV_LISTEN:
158                 iucv_sock_cleanup_listen(sk);
159                 break;
160
161         case IUCV_CONNECTED:
162         case IUCV_DISCONN:
163                 err = 0;
164
165                 sk->sk_state = IUCV_CLOSING;
166                 sk->sk_state_change(sk);
167
168                 if (!skb_queue_empty(&iucv->send_skb_q)) {
169                         if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
170                                 timeo = sk->sk_lingertime;
171                         else
172                                 timeo = IUCV_DISCONN_TIMEOUT;
173                         err = iucv_sock_wait_state(sk, IUCV_CLOSED, 0, timeo);
174                 }
175
176                 sk->sk_state = IUCV_CLOSED;
177                 sk->sk_state_change(sk);
178
179                 if (iucv->path) {
180                         low_nmcpy(user_data, iucv->src_name);
181                         high_nmcpy(user_data, iucv->dst_name);
182                         ASCEBC(user_data, sizeof(user_data));
183                         err = iucv_path_sever(iucv->path, user_data);
184                         iucv_path_free(iucv->path);
185                         iucv->path = NULL;
186                 }
187
188                 sk->sk_err = ECONNRESET;
189                 sk->sk_state_change(sk);
190
191                 skb_queue_purge(&iucv->send_skb_q);
192                 skb_queue_purge(&iucv->backlog_skb_q);
193
194                 sock_set_flag(sk, SOCK_ZAPPED);
195                 break;
196
197         default:
198                 sock_set_flag(sk, SOCK_ZAPPED);
199                 break;
200         }
201
202         release_sock(sk);
203         iucv_sock_kill(sk);
204 }
205
206 static void iucv_sock_init(struct sock *sk, struct sock *parent)
207 {
208         if (parent)
209                 sk->sk_type = parent->sk_type;
210 }
211
212 static struct sock *iucv_sock_alloc(struct socket *sock, int proto, gfp_t prio)
213 {
214         struct sock *sk;
215
216         sk = sk_alloc(&init_net, PF_IUCV, prio, &iucv_proto, 1);
217         if (!sk)
218                 return NULL;
219
220         sock_init_data(sock, sk);
221         INIT_LIST_HEAD(&iucv_sk(sk)->accept_q);
222         spin_lock_init(&iucv_sk(sk)->accept_q_lock);
223         skb_queue_head_init(&iucv_sk(sk)->send_skb_q);
224         skb_queue_head_init(&iucv_sk(sk)->backlog_skb_q);
225         iucv_sk(sk)->send_tag = 0;
226
227         sk->sk_destruct = iucv_sock_destruct;
228         sk->sk_sndtimeo = IUCV_CONN_TIMEOUT;
229         sk->sk_allocation = GFP_DMA;
230
231         sock_reset_flag(sk, SOCK_ZAPPED);
232
233         sk->sk_protocol = proto;
234         sk->sk_state    = IUCV_OPEN;
235
236         iucv_sock_init_timer(sk);
237
238         iucv_sock_link(&iucv_sk_list, sk);
239         return sk;
240 }
241
242 /* Create an IUCV socket */
243 static int iucv_sock_create(struct net *net, struct socket *sock, int protocol)
244 {
245         struct sock *sk;
246
247         if (sock->type != SOCK_STREAM)
248                 return -ESOCKTNOSUPPORT;
249
250         sock->state = SS_UNCONNECTED;
251         sock->ops = &iucv_sock_ops;
252
253         sk = iucv_sock_alloc(sock, protocol, GFP_KERNEL);
254         if (!sk)
255                 return -ENOMEM;
256
257         iucv_sock_init(sk, NULL);
258
259         return 0;
260 }
261
262 void iucv_sock_link(struct iucv_sock_list *l, struct sock *sk)
263 {
264         write_lock_bh(&l->lock);
265         sk_add_node(sk, &l->head);
266         write_unlock_bh(&l->lock);
267 }
268
269 void iucv_sock_unlink(struct iucv_sock_list *l, struct sock *sk)
270 {
271         write_lock_bh(&l->lock);
272         sk_del_node_init(sk);
273         write_unlock_bh(&l->lock);
274 }
275
276 void iucv_accept_enqueue(struct sock *parent, struct sock *sk)
277 {
278         unsigned long flags;
279         struct iucv_sock *par = iucv_sk(parent);
280
281         sock_hold(sk);
282         spin_lock_irqsave(&par->accept_q_lock, flags);
283         list_add_tail(&iucv_sk(sk)->accept_q, &par->accept_q);
284         spin_unlock_irqrestore(&par->accept_q_lock, flags);
285         iucv_sk(sk)->parent = parent;
286         parent->sk_ack_backlog++;
287 }
288
289 void iucv_accept_unlink(struct sock *sk)
290 {
291         unsigned long flags;
292         struct iucv_sock *par = iucv_sk(iucv_sk(sk)->parent);
293
294         spin_lock_irqsave(&par->accept_q_lock, flags);
295         list_del_init(&iucv_sk(sk)->accept_q);
296         spin_unlock_irqrestore(&par->accept_q_lock, flags);
297         iucv_sk(sk)->parent->sk_ack_backlog--;
298         iucv_sk(sk)->parent = NULL;
299         sock_put(sk);
300 }
301
302 struct sock *iucv_accept_dequeue(struct sock *parent, struct socket *newsock)
303 {
304         struct iucv_sock *isk, *n;
305         struct sock *sk;
306
307         list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) {
308                 sk = (struct sock *) isk;
309                 lock_sock(sk);
310
311                 if (sk->sk_state == IUCV_CLOSED) {
312                         iucv_accept_unlink(sk);
313                         release_sock(sk);
314                         continue;
315                 }
316
317                 if (sk->sk_state == IUCV_CONNECTED ||
318                     sk->sk_state == IUCV_SEVERED ||
319                     !newsock) {
320                         iucv_accept_unlink(sk);
321                         if (newsock)
322                                 sock_graft(sk, newsock);
323
324                         if (sk->sk_state == IUCV_SEVERED)
325                                 sk->sk_state = IUCV_DISCONN;
326
327                         release_sock(sk);
328                         return sk;
329                 }
330
331                 release_sock(sk);
332         }
333         return NULL;
334 }
335
336 int iucv_sock_wait_state(struct sock *sk, int state, int state2,
337                          unsigned long timeo)
338 {
339         DECLARE_WAITQUEUE(wait, current);
340         int err = 0;
341
342         add_wait_queue(sk->sk_sleep, &wait);
343         while (sk->sk_state != state && sk->sk_state != state2) {
344                 set_current_state(TASK_INTERRUPTIBLE);
345
346                 if (!timeo) {
347                         err = -EAGAIN;
348                         break;
349                 }
350
351                 if (signal_pending(current)) {
352                         err = sock_intr_errno(timeo);
353                         break;
354                 }
355
356                 release_sock(sk);
357                 timeo = schedule_timeout(timeo);
358                 lock_sock(sk);
359
360                 err = sock_error(sk);
361                 if (err)
362                         break;
363         }
364         set_current_state(TASK_RUNNING);
365         remove_wait_queue(sk->sk_sleep, &wait);
366         return err;
367 }
368
369 /* Bind an unbound socket */
370 static int iucv_sock_bind(struct socket *sock, struct sockaddr *addr,
371                           int addr_len)
372 {
373         struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
374         struct sock *sk = sock->sk;
375         struct iucv_sock *iucv;
376         int err;
377
378         /* Verify the input sockaddr */
379         if (!addr || addr->sa_family != AF_IUCV)
380                 return -EINVAL;
381
382         lock_sock(sk);
383         if (sk->sk_state != IUCV_OPEN) {
384                 err = -EBADFD;
385                 goto done;
386         }
387
388         write_lock_bh(&iucv_sk_list.lock);
389
390         iucv = iucv_sk(sk);
391         if (__iucv_get_sock_by_name(sa->siucv_name)) {
392                 err = -EADDRINUSE;
393                 goto done_unlock;
394         }
395         if (iucv->path) {
396                 err = 0;
397                 goto done_unlock;
398         }
399
400         /* Bind the socket */
401         memcpy(iucv->src_name, sa->siucv_name, 8);
402
403         /* Copy the user id */
404         memcpy(iucv->src_user_id, iucv_userid, 8);
405         sk->sk_state = IUCV_BOUND;
406         err = 0;
407
408 done_unlock:
409         /* Release the socket list lock */
410         write_unlock_bh(&iucv_sk_list.lock);
411 done:
412         release_sock(sk);
413         return err;
414 }
415
416 /* Automatically bind an unbound socket */
417 static int iucv_sock_autobind(struct sock *sk)
418 {
419         struct iucv_sock *iucv = iucv_sk(sk);
420         char query_buffer[80];
421         char name[12];
422         int err = 0;
423
424         /* Set the userid and name */
425         cpcmd("QUERY USERID", query_buffer, sizeof(query_buffer), &err);
426         if (unlikely(err))
427                 return -EPROTO;
428
429         memcpy(iucv->src_user_id, query_buffer, 8);
430
431         write_lock_bh(&iucv_sk_list.lock);
432
433         sprintf(name, "%08x", atomic_inc_return(&iucv_sk_list.autobind_name));
434         while (__iucv_get_sock_by_name(name)) {
435                 sprintf(name, "%08x",
436                         atomic_inc_return(&iucv_sk_list.autobind_name));
437         }
438
439         write_unlock_bh(&iucv_sk_list.lock);
440
441         memcpy(&iucv->src_name, name, 8);
442
443         return err;
444 }
445
446 /* Connect an unconnected socket */
447 static int iucv_sock_connect(struct socket *sock, struct sockaddr *addr,
448                              int alen, int flags)
449 {
450         struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
451         struct sock *sk = sock->sk;
452         struct iucv_sock *iucv;
453         unsigned char user_data[16];
454         int err;
455
456         if (addr->sa_family != AF_IUCV || alen < sizeof(struct sockaddr_iucv))
457                 return -EINVAL;
458
459         if (sk->sk_state != IUCV_OPEN && sk->sk_state != IUCV_BOUND)
460                 return -EBADFD;
461
462         if (sk->sk_type != SOCK_STREAM)
463                 return -EINVAL;
464
465         iucv = iucv_sk(sk);
466
467         if (sk->sk_state == IUCV_OPEN) {
468                 err = iucv_sock_autobind(sk);
469                 if (unlikely(err))
470                         return err;
471         }
472
473         lock_sock(sk);
474
475         /* Set the destination information */
476         memcpy(iucv_sk(sk)->dst_user_id, sa->siucv_user_id, 8);
477         memcpy(iucv_sk(sk)->dst_name, sa->siucv_name, 8);
478
479         high_nmcpy(user_data, sa->siucv_name);
480         low_nmcpy(user_data, iucv_sk(sk)->src_name);
481         ASCEBC(user_data, sizeof(user_data));
482
483         iucv = iucv_sk(sk);
484         /* Create path. */
485         iucv->path = iucv_path_alloc(IUCV_QUEUELEN_DEFAULT,
486                                      IPRMDATA, GFP_KERNEL);
487         err = iucv_path_connect(iucv->path, &af_iucv_handler,
488                                 sa->siucv_user_id, NULL, user_data, sk);
489         if (err) {
490                 iucv_path_free(iucv->path);
491                 iucv->path = NULL;
492                 err = -ECONNREFUSED;
493                 goto done;
494         }
495
496         if (sk->sk_state != IUCV_CONNECTED) {
497                 err = iucv_sock_wait_state(sk, IUCV_CONNECTED, IUCV_DISCONN,
498                                 sock_sndtimeo(sk, flags & O_NONBLOCK));
499         }
500
501         if (sk->sk_state == IUCV_DISCONN) {
502                 release_sock(sk);
503                 return -ECONNREFUSED;
504         }
505 done:
506         release_sock(sk);
507         return err;
508 }
509
510 /* Move a socket into listening state. */
511 static int iucv_sock_listen(struct socket *sock, int backlog)
512 {
513         struct sock *sk = sock->sk;
514         int err;
515
516         lock_sock(sk);
517
518         err = -EINVAL;
519         if (sk->sk_state != IUCV_BOUND || sock->type != SOCK_STREAM)
520                 goto done;
521
522         sk->sk_max_ack_backlog = backlog;
523         sk->sk_ack_backlog = 0;
524         sk->sk_state = IUCV_LISTEN;
525         err = 0;
526
527 done:
528         release_sock(sk);
529         return err;
530 }
531
532 /* Accept a pending connection */
533 static int iucv_sock_accept(struct socket *sock, struct socket *newsock,
534                             int flags)
535 {
536         DECLARE_WAITQUEUE(wait, current);
537         struct sock *sk = sock->sk, *nsk;
538         long timeo;
539         int err = 0;
540
541         lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
542
543         if (sk->sk_state != IUCV_LISTEN) {
544                 err = -EBADFD;
545                 goto done;
546         }
547
548         timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
549
550         /* Wait for an incoming connection */
551         add_wait_queue_exclusive(sk->sk_sleep, &wait);
552         while (!(nsk = iucv_accept_dequeue(sk, newsock))) {
553                 set_current_state(TASK_INTERRUPTIBLE);
554                 if (!timeo) {
555                         err = -EAGAIN;
556                         break;
557                 }
558
559                 release_sock(sk);
560                 timeo = schedule_timeout(timeo);
561                 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
562
563                 if (sk->sk_state != IUCV_LISTEN) {
564                         err = -EBADFD;
565                         break;
566                 }
567
568                 if (signal_pending(current)) {
569                         err = sock_intr_errno(timeo);
570                         break;
571                 }
572         }
573
574         set_current_state(TASK_RUNNING);
575         remove_wait_queue(sk->sk_sleep, &wait);
576
577         if (err)
578                 goto done;
579
580         newsock->state = SS_CONNECTED;
581
582 done:
583         release_sock(sk);
584         return err;
585 }
586
587 static int iucv_sock_getname(struct socket *sock, struct sockaddr *addr,
588                              int *len, int peer)
589 {
590         struct sockaddr_iucv *siucv = (struct sockaddr_iucv *) addr;
591         struct sock *sk = sock->sk;
592
593         addr->sa_family = AF_IUCV;
594         *len = sizeof(struct sockaddr_iucv);
595
596         if (peer) {
597                 memcpy(siucv->siucv_user_id, iucv_sk(sk)->dst_user_id, 8);
598                 memcpy(siucv->siucv_name, &iucv_sk(sk)->dst_name, 8);
599         } else {
600                 memcpy(siucv->siucv_user_id, iucv_sk(sk)->src_user_id, 8);
601                 memcpy(siucv->siucv_name, iucv_sk(sk)->src_name, 8);
602         }
603         memset(&siucv->siucv_port, 0, sizeof(siucv->siucv_port));
604         memset(&siucv->siucv_addr, 0, sizeof(siucv->siucv_addr));
605         memset(siucv->siucv_nodeid, 0, sizeof(siucv->siucv_nodeid));
606
607         return 0;
608 }
609
610 static int iucv_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
611                              struct msghdr *msg, size_t len)
612 {
613         struct sock *sk = sock->sk;
614         struct iucv_sock *iucv = iucv_sk(sk);
615         struct sk_buff *skb;
616         struct iucv_message txmsg;
617         int err;
618
619         err = sock_error(sk);
620         if (err)
621                 return err;
622
623         if (msg->msg_flags & MSG_OOB)
624                 return -EOPNOTSUPP;
625
626         lock_sock(sk);
627
628         if (sk->sk_shutdown & SEND_SHUTDOWN) {
629                 err = -EPIPE;
630                 goto out;
631         }
632
633         if (sk->sk_state == IUCV_CONNECTED) {
634                 if (!(skb = sock_alloc_send_skb(sk, len,
635                                                 msg->msg_flags & MSG_DONTWAIT,
636                                                 &err)))
637                         goto out;
638
639                 if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
640                         err = -EFAULT;
641                         goto fail;
642                 }
643
644                 txmsg.class = 0;
645                 txmsg.tag = iucv->send_tag++;
646                 memcpy(skb->cb, &txmsg.tag, 4);
647                 skb_queue_tail(&iucv->send_skb_q, skb);
648                 err = iucv_message_send(iucv->path, &txmsg, 0, 0,
649                                         (void *) skb->data, skb->len);
650                 if (err) {
651                         if (err == 3)
652                                 printk(KERN_ERR "AF_IUCV msg limit exceeded\n");
653                         skb_unlink(skb, &iucv->send_skb_q);
654                         err = -EPIPE;
655                         goto fail;
656                 }
657
658         } else {
659                 err = -ENOTCONN;
660                 goto out;
661         }
662
663         release_sock(sk);
664         return len;
665
666 fail:
667         kfree_skb(skb);
668 out:
669         release_sock(sk);
670         return err;
671 }
672
673 static int iucv_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
674                              struct msghdr *msg, size_t len, int flags)
675 {
676         int noblock = flags & MSG_DONTWAIT;
677         struct sock *sk = sock->sk;
678         struct iucv_sock *iucv = iucv_sk(sk);
679         int target, copied = 0;
680         struct sk_buff *skb, *rskb, *cskb;
681         int err = 0;
682
683         if ((sk->sk_state == IUCV_DISCONN || sk->sk_state == IUCV_SEVERED) &&
684                 skb_queue_empty(&iucv->backlog_skb_q) &&
685                 skb_queue_empty(&sk->sk_receive_queue))
686                 return 0;
687
688         if (flags & (MSG_OOB))
689                 return -EOPNOTSUPP;
690
691         target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
692
693         skb = skb_recv_datagram(sk, flags, noblock, &err);
694         if (!skb) {
695                 if (sk->sk_shutdown & RCV_SHUTDOWN)
696                         return 0;
697                 return err;
698         }
699
700         copied = min_t(unsigned int, skb->len, len);
701
702         cskb = skb;
703         if (memcpy_toiovec(msg->msg_iov, cskb->data, copied)) {
704                 skb_queue_head(&sk->sk_receive_queue, skb);
705                 if (copied == 0)
706                         return -EFAULT;
707                 goto done;
708         }
709
710         len -= copied;
711
712         /* Mark read part of skb as used */
713         if (!(flags & MSG_PEEK)) {
714                 skb_pull(skb, copied);
715
716                 if (skb->len) {
717                         skb_queue_head(&sk->sk_receive_queue, skb);
718                         goto done;
719                 }
720
721                 kfree_skb(skb);
722
723                 /* Queue backlog skbs */
724                 rskb = skb_dequeue(&iucv_sk(sk)->backlog_skb_q);
725                 while (rskb) {
726                         if (sock_queue_rcv_skb(sk, rskb)) {
727                                 skb_queue_head(&iucv_sk(sk)->backlog_skb_q,
728                                                 rskb);
729                                 break;
730                         } else {
731                                 rskb = skb_dequeue(&iucv_sk(sk)->backlog_skb_q);
732                         }
733                 }
734         } else
735                 skb_queue_head(&sk->sk_receive_queue, skb);
736
737 done:
738         return err ? : copied;
739 }
740
741 static inline unsigned int iucv_accept_poll(struct sock *parent)
742 {
743         struct iucv_sock *isk, *n;
744         struct sock *sk;
745
746         list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) {
747                 sk = (struct sock *) isk;
748
749                 if (sk->sk_state == IUCV_CONNECTED)
750                         return POLLIN | POLLRDNORM;
751         }
752
753         return 0;
754 }
755
756 unsigned int iucv_sock_poll(struct file *file, struct socket *sock,
757                             poll_table *wait)
758 {
759         struct sock *sk = sock->sk;
760         unsigned int mask = 0;
761
762         poll_wait(file, sk->sk_sleep, wait);
763
764         if (sk->sk_state == IUCV_LISTEN)
765                 return iucv_accept_poll(sk);
766
767         if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
768                 mask |= POLLERR;
769
770         if (sk->sk_shutdown & RCV_SHUTDOWN)
771                 mask |= POLLRDHUP;
772
773         if (sk->sk_shutdown == SHUTDOWN_MASK)
774                 mask |= POLLHUP;
775
776         if (!skb_queue_empty(&sk->sk_receive_queue) ||
777             (sk->sk_shutdown & RCV_SHUTDOWN))
778                 mask |= POLLIN | POLLRDNORM;
779
780         if (sk->sk_state == IUCV_CLOSED)
781                 mask |= POLLHUP;
782
783         if (sk->sk_state == IUCV_DISCONN || sk->sk_state == IUCV_SEVERED)
784                 mask |= POLLIN;
785
786         if (sock_writeable(sk))
787                 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
788         else
789                 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
790
791         return mask;
792 }
793
794 static int iucv_sock_shutdown(struct socket *sock, int how)
795 {
796         struct sock *sk = sock->sk;
797         struct iucv_sock *iucv = iucv_sk(sk);
798         struct iucv_message txmsg;
799         int err = 0;
800         u8 prmmsg[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01};
801
802         how++;
803
804         if ((how & ~SHUTDOWN_MASK) || !how)
805                 return -EINVAL;
806
807         lock_sock(sk);
808         switch (sk->sk_state) {
809         case IUCV_CLOSED:
810                 err = -ENOTCONN;
811                 goto fail;
812
813         default:
814                 sk->sk_shutdown |= how;
815                 break;
816         }
817
818         if (how == SEND_SHUTDOWN || how == SHUTDOWN_MASK) {
819                 txmsg.class = 0;
820                 txmsg.tag = 0;
821                 err = iucv_message_send(iucv->path, &txmsg, IUCV_IPRMDATA, 0,
822                                         (void *) prmmsg, 8);
823                 if (err) {
824                         switch (err) {
825                         case 1:
826                                 err = -ENOTCONN;
827                                 break;
828                         case 2:
829                                 err = -ECONNRESET;
830                                 break;
831                         default:
832                                 err = -ENOTCONN;
833                                 break;
834                         }
835                 }
836         }
837
838         if (how == RCV_SHUTDOWN || how == SHUTDOWN_MASK) {
839                 err = iucv_path_quiesce(iucv_sk(sk)->path, NULL);
840                 if (err)
841                         err = -ENOTCONN;
842
843                 skb_queue_purge(&sk->sk_receive_queue);
844         }
845
846         /* Wake up anyone sleeping in poll */
847         sk->sk_state_change(sk);
848
849 fail:
850         release_sock(sk);
851         return err;
852 }
853
854 static int iucv_sock_release(struct socket *sock)
855 {
856         struct sock *sk = sock->sk;
857         int err = 0;
858
859         if (!sk)
860                 return 0;
861
862         iucv_sock_close(sk);
863
864         /* Unregister with IUCV base support */
865         if (iucv_sk(sk)->path) {
866                 iucv_path_sever(iucv_sk(sk)->path, NULL);
867                 iucv_path_free(iucv_sk(sk)->path);
868                 iucv_sk(sk)->path = NULL;
869         }
870
871         sock_orphan(sk);
872         iucv_sock_kill(sk);
873         return err;
874 }
875
876 /* Callback wrappers - called from iucv base support */
877 static int iucv_callback_connreq(struct iucv_path *path,
878                                  u8 ipvmid[8], u8 ipuser[16])
879 {
880         unsigned char user_data[16];
881         unsigned char nuser_data[16];
882         unsigned char src_name[8];
883         struct hlist_node *node;
884         struct sock *sk, *nsk;
885         struct iucv_sock *iucv, *niucv;
886         int err;
887
888         memcpy(src_name, ipuser, 8);
889         EBCASC(src_name, 8);
890         /* Find out if this path belongs to af_iucv. */
891         read_lock(&iucv_sk_list.lock);
892         iucv = NULL;
893         sk = NULL;
894         sk_for_each(sk, node, &iucv_sk_list.head)
895                 if (sk->sk_state == IUCV_LISTEN &&
896                     !memcmp(&iucv_sk(sk)->src_name, src_name, 8)) {
897                         /*
898                          * Found a listening socket with
899                          * src_name == ipuser[0-7].
900                          */
901                         iucv = iucv_sk(sk);
902                         break;
903                 }
904         read_unlock(&iucv_sk_list.lock);
905         if (!iucv)
906                 /* No socket found, not one of our paths. */
907                 return -EINVAL;
908
909         bh_lock_sock(sk);
910
911         /* Check if parent socket is listening */
912         low_nmcpy(user_data, iucv->src_name);
913         high_nmcpy(user_data, iucv->dst_name);
914         ASCEBC(user_data, sizeof(user_data));
915         if (sk->sk_state != IUCV_LISTEN) {
916                 err = iucv_path_sever(path, user_data);
917                 goto fail;
918         }
919
920         /* Check for backlog size */
921         if (sk_acceptq_is_full(sk)) {
922                 err = iucv_path_sever(path, user_data);
923                 goto fail;
924         }
925
926         /* Create the new socket */
927         nsk = iucv_sock_alloc(NULL, SOCK_STREAM, GFP_ATOMIC);
928         if (!nsk) {
929                 err = iucv_path_sever(path, user_data);
930                 goto fail;
931         }
932
933         niucv = iucv_sk(nsk);
934         iucv_sock_init(nsk, sk);
935
936         /* Set the new iucv_sock */
937         memcpy(niucv->dst_name, ipuser + 8, 8);
938         EBCASC(niucv->dst_name, 8);
939         memcpy(niucv->dst_user_id, ipvmid, 8);
940         memcpy(niucv->src_name, iucv->src_name, 8);
941         memcpy(niucv->src_user_id, iucv->src_user_id, 8);
942         niucv->path = path;
943
944         /* Call iucv_accept */
945         high_nmcpy(nuser_data, ipuser + 8);
946         memcpy(nuser_data + 8, niucv->src_name, 8);
947         ASCEBC(nuser_data + 8, 8);
948
949         path->msglim = IUCV_QUEUELEN_DEFAULT;
950         err = iucv_path_accept(path, &af_iucv_handler, nuser_data, nsk);
951         if (err) {
952                 err = iucv_path_sever(path, user_data);
953                 goto fail;
954         }
955
956         iucv_accept_enqueue(sk, nsk);
957
958         /* Wake up accept */
959         nsk->sk_state = IUCV_CONNECTED;
960         sk->sk_data_ready(sk, 1);
961         err = 0;
962 fail:
963         bh_unlock_sock(sk);
964         return 0;
965 }
966
967 static void iucv_callback_connack(struct iucv_path *path, u8 ipuser[16])
968 {
969         struct sock *sk = path->private;
970
971         sk->sk_state = IUCV_CONNECTED;
972         sk->sk_state_change(sk);
973 }
974
975 static int iucv_fragment_skb(struct sock *sk, struct sk_buff *skb, int len,
976                              struct sk_buff_head *fragmented_skb_q)
977 {
978         int dataleft, size, copied = 0;
979         struct sk_buff *nskb;
980
981         dataleft = len;
982         while (dataleft) {
983                 if (dataleft >= sk->sk_rcvbuf / 4)
984                         size = sk->sk_rcvbuf / 4;
985                 else
986                         size = dataleft;
987
988                 nskb = alloc_skb(size, GFP_ATOMIC | GFP_DMA);
989                 if (!nskb)
990                         return -ENOMEM;
991
992                 memcpy(nskb->data, skb->data + copied, size);
993                 copied += size;
994                 dataleft -= size;
995
996                 skb_reset_transport_header(nskb);
997                 skb_reset_network_header(nskb);
998                 nskb->len = size;
999
1000                 skb_queue_tail(fragmented_skb_q, nskb);
1001         }
1002
1003         return 0;
1004 }
1005
1006 static void iucv_callback_rx(struct iucv_path *path, struct iucv_message *msg)
1007 {
1008         struct sock *sk = path->private;
1009         struct iucv_sock *iucv = iucv_sk(sk);
1010         struct sk_buff *skb, *fskb;
1011         struct sk_buff_head fragmented_skb_q;
1012         int rc;
1013
1014         skb_queue_head_init(&fragmented_skb_q);
1015
1016         if (sk->sk_shutdown & RCV_SHUTDOWN)
1017                 return;
1018
1019         skb = alloc_skb(msg->length, GFP_ATOMIC | GFP_DMA);
1020         if (!skb) {
1021                 iucv_path_sever(path, NULL);
1022                 return;
1023         }
1024
1025         if (msg->flags & IPRMDATA) {
1026                 skb->data = NULL;
1027                 skb->len = 0;
1028         } else {
1029                 rc = iucv_message_receive(path, msg, 0, skb->data,
1030                                           msg->length, NULL);
1031                 if (rc) {
1032                         kfree_skb(skb);
1033                         return;
1034                 }
1035                 if (skb->truesize >= sk->sk_rcvbuf / 4) {
1036                         rc = iucv_fragment_skb(sk, skb, msg->length,
1037                                                &fragmented_skb_q);
1038                         kfree_skb(skb);
1039                         skb = NULL;
1040                         if (rc) {
1041                                 iucv_path_sever(path, NULL);
1042                                 return;
1043                         }
1044                 } else {
1045                         skb_reset_transport_header(skb);
1046                         skb_reset_network_header(skb);
1047                         skb->len = msg->length;
1048                 }
1049         }
1050         /* Queue the fragmented skb */
1051         fskb = skb_dequeue(&fragmented_skb_q);
1052         while (fskb) {
1053                 if (!skb_queue_empty(&iucv->backlog_skb_q))
1054                         skb_queue_tail(&iucv->backlog_skb_q, fskb);
1055                 else if (sock_queue_rcv_skb(sk, fskb))
1056                         skb_queue_tail(&iucv_sk(sk)->backlog_skb_q, fskb);
1057                 fskb = skb_dequeue(&fragmented_skb_q);
1058         }
1059
1060         /* Queue the original skb if it exists (was not fragmented) */
1061         if (skb) {
1062                 if (!skb_queue_empty(&iucv->backlog_skb_q))
1063                         skb_queue_tail(&iucv_sk(sk)->backlog_skb_q, skb);
1064                 else if (sock_queue_rcv_skb(sk, skb))
1065                         skb_queue_tail(&iucv_sk(sk)->backlog_skb_q, skb);
1066         }
1067
1068 }
1069
1070 static void iucv_callback_txdone(struct iucv_path *path,
1071                                  struct iucv_message *msg)
1072 {
1073         struct sock *sk = path->private;
1074         struct sk_buff *this;
1075         struct sk_buff_head *list = &iucv_sk(sk)->send_skb_q;
1076         struct sk_buff *list_skb = list->next;
1077         unsigned long flags;
1078
1079         if (list_skb) {
1080                 spin_lock_irqsave(&list->lock, flags);
1081
1082                 do {
1083                         this = list_skb;
1084                         list_skb = list_skb->next;
1085                 } while (memcmp(&msg->tag, this->cb, 4) && list_skb);
1086
1087                 spin_unlock_irqrestore(&list->lock, flags);
1088
1089                 skb_unlink(this, &iucv_sk(sk)->send_skb_q);
1090                 kfree_skb(this);
1091         }
1092
1093         if (sk->sk_state == IUCV_CLOSING) {
1094                 if (skb_queue_empty(&iucv_sk(sk)->send_skb_q)) {
1095                         sk->sk_state = IUCV_CLOSED;
1096                         sk->sk_state_change(sk);
1097                 }
1098         }
1099
1100 }
1101
1102 static void iucv_callback_connrej(struct iucv_path *path, u8 ipuser[16])
1103 {
1104         struct sock *sk = path->private;
1105
1106         if (!list_empty(&iucv_sk(sk)->accept_q))
1107                 sk->sk_state = IUCV_SEVERED;
1108         else
1109                 sk->sk_state = IUCV_DISCONN;
1110
1111         sk->sk_state_change(sk);
1112 }
1113
1114 static struct proto_ops iucv_sock_ops = {
1115         .family         = PF_IUCV,
1116         .owner          = THIS_MODULE,
1117         .release        = iucv_sock_release,
1118         .bind           = iucv_sock_bind,
1119         .connect        = iucv_sock_connect,
1120         .listen         = iucv_sock_listen,
1121         .accept         = iucv_sock_accept,
1122         .getname        = iucv_sock_getname,
1123         .sendmsg        = iucv_sock_sendmsg,
1124         .recvmsg        = iucv_sock_recvmsg,
1125         .poll           = iucv_sock_poll,
1126         .ioctl          = sock_no_ioctl,
1127         .mmap           = sock_no_mmap,
1128         .socketpair     = sock_no_socketpair,
1129         .shutdown       = iucv_sock_shutdown,
1130         .setsockopt     = sock_no_setsockopt,
1131         .getsockopt     = sock_no_getsockopt
1132 };
1133
1134 static struct net_proto_family iucv_sock_family_ops = {
1135         .family = AF_IUCV,
1136         .owner  = THIS_MODULE,
1137         .create = iucv_sock_create,
1138 };
1139
1140 static int __init afiucv_init(void)
1141 {
1142         int err;
1143
1144         if (!MACHINE_IS_VM) {
1145                 printk(KERN_ERR "AF_IUCV connection needs VM as base\n");
1146                 err = -EPROTONOSUPPORT;
1147                 goto out;
1148         }
1149         cpcmd("QUERY USERID", iucv_userid, sizeof(iucv_userid), &err);
1150         if (unlikely(err)) {
1151                 printk(KERN_ERR "AF_IUCV needs the VM userid\n");
1152                 err = -EPROTONOSUPPORT;
1153                 goto out;
1154         }
1155
1156         err = iucv_register(&af_iucv_handler, 0);
1157         if (err)
1158                 goto out;
1159         err = proto_register(&iucv_proto, 0);
1160         if (err)
1161                 goto out_iucv;
1162         err = sock_register(&iucv_sock_family_ops);
1163         if (err)
1164                 goto out_proto;
1165         printk(KERN_INFO "AF_IUCV lowlevel driver initialized\n");
1166         return 0;
1167
1168 out_proto:
1169         proto_unregister(&iucv_proto);
1170 out_iucv:
1171         iucv_unregister(&af_iucv_handler, 0);
1172 out:
1173         return err;
1174 }
1175
1176 static void __exit afiucv_exit(void)
1177 {
1178         sock_unregister(PF_IUCV);
1179         proto_unregister(&iucv_proto);
1180         iucv_unregister(&af_iucv_handler, 0);
1181
1182         printk(KERN_INFO "AF_IUCV lowlevel driver unloaded\n");
1183 }
1184
1185 module_init(afiucv_init);
1186 module_exit(afiucv_exit);
1187
1188 MODULE_AUTHOR("Jennifer Hunt <jenhunt@us.ibm.com>");
1189 MODULE_DESCRIPTION("IUCV Sockets ver " VERSION);
1190 MODULE_VERSION(VERSION);
1191 MODULE_LICENSE("GPL");
1192 MODULE_ALIAS_NETPROTO(PF_IUCV);