blob: 656cbd1958250d54542c2318130dc26a81182a4a [file] [log] [blame]
Jennifer Hunteac37312007-02-08 13:51:54 -08001/*
Jennifer Hunteac37312007-02-08 13:51:54 -08002 * IUCV protocol stack for Linux on zSeries
3 *
Ursula Braunc23cad92009-06-16 10:30:44 +02004 * Copyright IBM Corp. 2006, 2009
Jennifer Hunteac37312007-02-08 13:51:54 -08005 *
6 * Author(s): Jennifer Hunt <jenhunt@us.ibm.com>
Ursula Braunc23cad92009-06-16 10:30:44 +02007 * Hendrik Brueckner <brueckner@linux.vnet.ibm.com>
8 * PM functions:
9 * Ursula Braun <ursula.braun@de.ibm.com>
Jennifer Hunteac37312007-02-08 13:51:54 -080010 */
11
Ursula Braun8f7c5022008-12-25 13:39:47 +010012#define KMSG_COMPONENT "af_iucv"
13#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
14
Jennifer Hunteac37312007-02-08 13:51:54 -080015#include <linux/module.h>
16#include <linux/types.h>
17#include <linux/list.h>
18#include <linux/errno.h>
19#include <linux/kernel.h>
20#include <linux/sched.h>
21#include <linux/slab.h>
22#include <linux/skbuff.h>
23#include <linux/init.h>
24#include <linux/poll.h>
25#include <net/sock.h>
26#include <asm/ebcdic.h>
27#include <asm/cpcmd.h>
28#include <linux/kmod.h>
29
30#include <net/iucv/iucv.h>
31#include <net/iucv/af_iucv.h>
32
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +000033#define VERSION "1.1"
Jennifer Hunteac37312007-02-08 13:51:54 -080034
35static char iucv_userid[80];
36
37static struct proto_ops iucv_sock_ops;
38
39static struct proto iucv_proto = {
40 .name = "AF_IUCV",
41 .owner = THIS_MODULE,
42 .obj_size = sizeof(struct iucv_sock),
43};
44
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +000045/* special AF_IUCV IPRM messages */
46static const u8 iprm_shutdown[8] =
47 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01};
48
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +000049#define TRGCLS_SIZE (sizeof(((struct iucv_message *)0)->class))
50
51/* macros to set/get socket control buffer at correct offset */
52#define CB_TAG(skb) ((skb)->cb) /* iucv message tag */
53#define CB_TAG_LEN (sizeof(((struct iucv_message *) 0)->tag))
54#define CB_TRGCLS(skb) ((skb)->cb + CB_TAG_LEN) /* iucv msg target class */
55#define CB_TRGCLS_LEN (TRGCLS_SIZE)
56
57
Heiko Carstens57f20442007-10-08 02:02:52 -070058static void iucv_sock_kill(struct sock *sk);
59static void iucv_sock_close(struct sock *sk);
60
Jennifer Hunteac37312007-02-08 13:51:54 -080061/* Call Back functions */
62static void iucv_callback_rx(struct iucv_path *, struct iucv_message *);
63static void iucv_callback_txdone(struct iucv_path *, struct iucv_message *);
64static void iucv_callback_connack(struct iucv_path *, u8 ipuser[16]);
Heiko Carstensda99f052007-05-04 12:23:27 -070065static int iucv_callback_connreq(struct iucv_path *, u8 ipvmid[8],
66 u8 ipuser[16]);
Jennifer Hunteac37312007-02-08 13:51:54 -080067static void iucv_callback_connrej(struct iucv_path *, u8 ipuser[16]);
Hendrik Brueckneraf88b522009-04-21 23:26:21 +000068static void iucv_callback_shutdown(struct iucv_path *, u8 ipuser[16]);
Jennifer Hunteac37312007-02-08 13:51:54 -080069
70static struct iucv_sock_list iucv_sk_list = {
Robert P. J. Day3db8ce32008-04-10 02:11:24 -070071 .lock = __RW_LOCK_UNLOCKED(iucv_sk_list.lock),
Jennifer Hunteac37312007-02-08 13:51:54 -080072 .autobind_name = ATOMIC_INIT(0)
73};
74
75static struct iucv_handler af_iucv_handler = {
76 .path_pending = iucv_callback_connreq,
77 .path_complete = iucv_callback_connack,
78 .path_severed = iucv_callback_connrej,
79 .message_pending = iucv_callback_rx,
Hendrik Brueckneraf88b522009-04-21 23:26:21 +000080 .message_complete = iucv_callback_txdone,
81 .path_quiesced = iucv_callback_shutdown,
Jennifer Hunteac37312007-02-08 13:51:54 -080082};
83
84static inline void high_nmcpy(unsigned char *dst, char *src)
85{
86 memcpy(dst, src, 8);
87}
88
89static inline void low_nmcpy(unsigned char *dst, char *src)
90{
91 memcpy(&dst[8], src, 8);
92}
93
Ursula Braunc23cad92009-06-16 10:30:44 +020094static int afiucv_pm_prepare(struct device *dev)
95{
96#ifdef CONFIG_PM_DEBUG
97 printk(KERN_WARNING "afiucv_pm_prepare\n");
98#endif
99 return 0;
100}
101
102static void afiucv_pm_complete(struct device *dev)
103{
104#ifdef CONFIG_PM_DEBUG
105 printk(KERN_WARNING "afiucv_pm_complete\n");
106#endif
107 return;
108}
109
110/**
111 * afiucv_pm_freeze() - Freeze PM callback
112 * @dev: AFIUCV dummy device
113 *
114 * Sever all established IUCV communication pathes
115 */
116static int afiucv_pm_freeze(struct device *dev)
117{
118 struct iucv_sock *iucv;
119 struct sock *sk;
120 struct hlist_node *node;
121 int err = 0;
122
123#ifdef CONFIG_PM_DEBUG
124 printk(KERN_WARNING "afiucv_pm_freeze\n");
125#endif
126 read_lock(&iucv_sk_list.lock);
127 sk_for_each(sk, node, &iucv_sk_list.head) {
128 iucv = iucv_sk(sk);
129 skb_queue_purge(&iucv->send_skb_q);
130 skb_queue_purge(&iucv->backlog_skb_q);
131 switch (sk->sk_state) {
132 case IUCV_SEVERED:
133 case IUCV_DISCONN:
134 case IUCV_CLOSING:
135 case IUCV_CONNECTED:
136 if (iucv->path) {
137 err = iucv_path_sever(iucv->path, NULL);
138 iucv_path_free(iucv->path);
139 iucv->path = NULL;
140 }
141 break;
142 case IUCV_OPEN:
143 case IUCV_BOUND:
144 case IUCV_LISTEN:
145 case IUCV_CLOSED:
146 default:
147 break;
148 }
149 }
150 read_unlock(&iucv_sk_list.lock);
151 return err;
152}
153
154/**
155 * afiucv_pm_restore_thaw() - Thaw and restore PM callback
156 * @dev: AFIUCV dummy device
157 *
158 * socket clean up after freeze
159 */
160static int afiucv_pm_restore_thaw(struct device *dev)
161{
162 struct iucv_sock *iucv;
163 struct sock *sk;
164 struct hlist_node *node;
165
166#ifdef CONFIG_PM_DEBUG
167 printk(KERN_WARNING "afiucv_pm_restore_thaw\n");
168#endif
169 read_lock(&iucv_sk_list.lock);
170 sk_for_each(sk, node, &iucv_sk_list.head) {
171 iucv = iucv_sk(sk);
172 switch (sk->sk_state) {
173 case IUCV_CONNECTED:
174 sk->sk_err = EPIPE;
175 sk->sk_state = IUCV_DISCONN;
176 sk->sk_state_change(sk);
177 break;
178 case IUCV_DISCONN:
179 case IUCV_SEVERED:
180 case IUCV_CLOSING:
181 case IUCV_LISTEN:
182 case IUCV_BOUND:
183 case IUCV_OPEN:
184 default:
185 break;
186 }
187 }
188 read_unlock(&iucv_sk_list.lock);
189 return 0;
190}
191
192static struct dev_pm_ops afiucv_pm_ops = {
193 .prepare = afiucv_pm_prepare,
194 .complete = afiucv_pm_complete,
195 .freeze = afiucv_pm_freeze,
196 .thaw = afiucv_pm_restore_thaw,
197 .restore = afiucv_pm_restore_thaw,
198};
199
200static struct device_driver af_iucv_driver = {
201 .owner = THIS_MODULE,
202 .name = "afiucv",
203 .bus = &iucv_bus,
204 .pm = &afiucv_pm_ops,
205};
206
207/* dummy device used as trigger for PM functions */
208static struct device *af_iucv_dev;
209
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000210/**
211 * iucv_msg_length() - Returns the length of an iucv message.
212 * @msg: Pointer to struct iucv_message, MUST NOT be NULL
213 *
214 * The function returns the length of the specified iucv message @msg of data
215 * stored in a buffer and of data stored in the parameter list (PRMDATA).
216 *
217 * For IUCV_IPRMDATA, AF_IUCV uses the following convention to transport socket
218 * data:
219 * PRMDATA[0..6] socket data (max 7 bytes);
220 * PRMDATA[7] socket data length value (len is 0xff - PRMDATA[7])
221 *
222 * The socket data length is computed by substracting the socket data length
223 * value from 0xFF.
224 * If the socket data len is greater 7, then PRMDATA can be used for special
225 * notifications (see iucv_sock_shutdown); and further,
226 * if the socket data len is > 7, the function returns 8.
227 *
228 * Use this function to allocate socket buffers to store iucv message data.
229 */
230static inline size_t iucv_msg_length(struct iucv_message *msg)
231{
232 size_t datalen;
233
234 if (msg->flags & IUCV_IPRMDATA) {
235 datalen = 0xff - msg->rmmsg[7];
236 return (datalen < 8) ? datalen : 8;
237 }
238 return msg->length;
239}
240
Jennifer Hunteac37312007-02-08 13:51:54 -0800241/* Timers */
242static void iucv_sock_timeout(unsigned long arg)
243{
244 struct sock *sk = (struct sock *)arg;
245
246 bh_lock_sock(sk);
247 sk->sk_err = ETIMEDOUT;
248 sk->sk_state_change(sk);
249 bh_unlock_sock(sk);
250
251 iucv_sock_kill(sk);
252 sock_put(sk);
253}
254
255static void iucv_sock_clear_timer(struct sock *sk)
256{
257 sk_stop_timer(sk, &sk->sk_timer);
258}
259
Jennifer Hunteac37312007-02-08 13:51:54 -0800260static struct sock *__iucv_get_sock_by_name(char *nm)
261{
262 struct sock *sk;
263 struct hlist_node *node;
264
265 sk_for_each(sk, node, &iucv_sk_list.head)
266 if (!memcmp(&iucv_sk(sk)->src_name, nm, 8))
267 return sk;
268
269 return NULL;
270}
271
272static void iucv_sock_destruct(struct sock *sk)
273{
274 skb_queue_purge(&sk->sk_receive_queue);
275 skb_queue_purge(&sk->sk_write_queue);
276}
277
278/* Cleanup Listen */
279static void iucv_sock_cleanup_listen(struct sock *parent)
280{
281 struct sock *sk;
282
283 /* Close non-accepted connections */
284 while ((sk = iucv_accept_dequeue(parent, NULL))) {
285 iucv_sock_close(sk);
286 iucv_sock_kill(sk);
287 }
288
289 parent->sk_state = IUCV_CLOSED;
290 sock_set_flag(parent, SOCK_ZAPPED);
291}
292
293/* Kill socket */
294static void iucv_sock_kill(struct sock *sk)
295{
296 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
297 return;
298
299 iucv_sock_unlink(&iucv_sk_list, sk);
300 sock_set_flag(sk, SOCK_DEAD);
301 sock_put(sk);
302}
303
304/* Close an IUCV socket */
305static void iucv_sock_close(struct sock *sk)
306{
307 unsigned char user_data[16];
308 struct iucv_sock *iucv = iucv_sk(sk);
309 int err;
Jennifer Hunt561e0362007-05-04 12:22:07 -0700310 unsigned long timeo;
Jennifer Hunteac37312007-02-08 13:51:54 -0800311
312 iucv_sock_clear_timer(sk);
313 lock_sock(sk);
314
Heiko Carstensda99f052007-05-04 12:23:27 -0700315 switch (sk->sk_state) {
Jennifer Hunteac37312007-02-08 13:51:54 -0800316 case IUCV_LISTEN:
317 iucv_sock_cleanup_listen(sk);
318 break;
319
320 case IUCV_CONNECTED:
321 case IUCV_DISCONN:
322 err = 0;
Jennifer Hunt561e0362007-05-04 12:22:07 -0700323
324 sk->sk_state = IUCV_CLOSING;
325 sk->sk_state_change(sk);
326
Heiko Carstensda99f052007-05-04 12:23:27 -0700327 if (!skb_queue_empty(&iucv->send_skb_q)) {
Jennifer Hunt561e0362007-05-04 12:22:07 -0700328 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
329 timeo = sk->sk_lingertime;
330 else
331 timeo = IUCV_DISCONN_TIMEOUT;
332 err = iucv_sock_wait_state(sk, IUCV_CLOSED, 0, timeo);
333 }
334
Ursula Braunbbe188c2009-04-21 06:04:20 +0000335 case IUCV_CLOSING: /* fall through */
Jennifer Hunt561e0362007-05-04 12:22:07 -0700336 sk->sk_state = IUCV_CLOSED;
337 sk->sk_state_change(sk);
338
Jennifer Hunteac37312007-02-08 13:51:54 -0800339 if (iucv->path) {
340 low_nmcpy(user_data, iucv->src_name);
341 high_nmcpy(user_data, iucv->dst_name);
342 ASCEBC(user_data, sizeof(user_data));
343 err = iucv_path_sever(iucv->path, user_data);
344 iucv_path_free(iucv->path);
345 iucv->path = NULL;
346 }
347
Jennifer Hunteac37312007-02-08 13:51:54 -0800348 sk->sk_err = ECONNRESET;
349 sk->sk_state_change(sk);
350
351 skb_queue_purge(&iucv->send_skb_q);
Jennifer Hunt561e0362007-05-04 12:22:07 -0700352 skb_queue_purge(&iucv->backlog_skb_q);
Jennifer Hunteac37312007-02-08 13:51:54 -0800353
354 sock_set_flag(sk, SOCK_ZAPPED);
355 break;
356
357 default:
358 sock_set_flag(sk, SOCK_ZAPPED);
359 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700360 }
Jennifer Hunteac37312007-02-08 13:51:54 -0800361
362 release_sock(sk);
363 iucv_sock_kill(sk);
364}
365
366static void iucv_sock_init(struct sock *sk, struct sock *parent)
367{
368 if (parent)
369 sk->sk_type = parent->sk_type;
370}
371
372static struct sock *iucv_sock_alloc(struct socket *sock, int proto, gfp_t prio)
373{
374 struct sock *sk;
375
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700376 sk = sk_alloc(&init_net, PF_IUCV, prio, &iucv_proto);
Jennifer Hunteac37312007-02-08 13:51:54 -0800377 if (!sk)
378 return NULL;
379
380 sock_init_data(sock, sk);
381 INIT_LIST_HEAD(&iucv_sk(sk)->accept_q);
Ursula Braunfebca282007-07-14 19:04:25 -0700382 spin_lock_init(&iucv_sk(sk)->accept_q_lock);
Jennifer Hunteac37312007-02-08 13:51:54 -0800383 skb_queue_head_init(&iucv_sk(sk)->send_skb_q);
Ursula Braunf0703c82007-10-08 02:03:31 -0700384 INIT_LIST_HEAD(&iucv_sk(sk)->message_q.list);
385 spin_lock_init(&iucv_sk(sk)->message_q.lock);
Jennifer Hunt561e0362007-05-04 12:22:07 -0700386 skb_queue_head_init(&iucv_sk(sk)->backlog_skb_q);
Jennifer Hunteac37312007-02-08 13:51:54 -0800387 iucv_sk(sk)->send_tag = 0;
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +0000388 iucv_sk(sk)->flags = 0;
Hendrik Brueckner09488e22009-04-21 23:26:27 +0000389 iucv_sk(sk)->msglimit = IUCV_QUEUELEN_DEFAULT;
Ursula Braunbbe188c2009-04-21 06:04:20 +0000390 iucv_sk(sk)->path = NULL;
391 memset(&iucv_sk(sk)->src_user_id , 0, 32);
Jennifer Hunteac37312007-02-08 13:51:54 -0800392
393 sk->sk_destruct = iucv_sock_destruct;
394 sk->sk_sndtimeo = IUCV_CONN_TIMEOUT;
395 sk->sk_allocation = GFP_DMA;
396
397 sock_reset_flag(sk, SOCK_ZAPPED);
398
399 sk->sk_protocol = proto;
400 sk->sk_state = IUCV_OPEN;
401
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800402 setup_timer(&sk->sk_timer, iucv_sock_timeout, (unsigned long)sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800403
404 iucv_sock_link(&iucv_sk_list, sk);
405 return sk;
406}
407
408/* Create an IUCV socket */
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700409static int iucv_sock_create(struct net *net, struct socket *sock, int protocol)
Jennifer Hunteac37312007-02-08 13:51:54 -0800410{
411 struct sock *sk;
412
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +0000413 if (protocol && protocol != PF_IUCV)
414 return -EPROTONOSUPPORT;
Jennifer Hunteac37312007-02-08 13:51:54 -0800415
416 sock->state = SS_UNCONNECTED;
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +0000417
418 switch (sock->type) {
419 case SOCK_STREAM:
420 sock->ops = &iucv_sock_ops;
421 break;
422 case SOCK_SEQPACKET:
423 /* currently, proto ops can handle both sk types */
424 sock->ops = &iucv_sock_ops;
425 break;
426 default:
427 return -ESOCKTNOSUPPORT;
428 }
Jennifer Hunteac37312007-02-08 13:51:54 -0800429
430 sk = iucv_sock_alloc(sock, protocol, GFP_KERNEL);
431 if (!sk)
432 return -ENOMEM;
433
434 iucv_sock_init(sk, NULL);
435
436 return 0;
437}
438
439void iucv_sock_link(struct iucv_sock_list *l, struct sock *sk)
440{
441 write_lock_bh(&l->lock);
442 sk_add_node(sk, &l->head);
443 write_unlock_bh(&l->lock);
444}
445
446void iucv_sock_unlink(struct iucv_sock_list *l, struct sock *sk)
447{
448 write_lock_bh(&l->lock);
449 sk_del_node_init(sk);
450 write_unlock_bh(&l->lock);
451}
452
453void iucv_accept_enqueue(struct sock *parent, struct sock *sk)
454{
Ursula Braunfebca282007-07-14 19:04:25 -0700455 unsigned long flags;
456 struct iucv_sock *par = iucv_sk(parent);
457
Jennifer Hunteac37312007-02-08 13:51:54 -0800458 sock_hold(sk);
Ursula Braunfebca282007-07-14 19:04:25 -0700459 spin_lock_irqsave(&par->accept_q_lock, flags);
460 list_add_tail(&iucv_sk(sk)->accept_q, &par->accept_q);
461 spin_unlock_irqrestore(&par->accept_q_lock, flags);
Jennifer Hunteac37312007-02-08 13:51:54 -0800462 iucv_sk(sk)->parent = parent;
463 parent->sk_ack_backlog++;
464}
465
466void iucv_accept_unlink(struct sock *sk)
467{
Ursula Braunfebca282007-07-14 19:04:25 -0700468 unsigned long flags;
469 struct iucv_sock *par = iucv_sk(iucv_sk(sk)->parent);
470
471 spin_lock_irqsave(&par->accept_q_lock, flags);
Jennifer Hunteac37312007-02-08 13:51:54 -0800472 list_del_init(&iucv_sk(sk)->accept_q);
Ursula Braunfebca282007-07-14 19:04:25 -0700473 spin_unlock_irqrestore(&par->accept_q_lock, flags);
Jennifer Hunteac37312007-02-08 13:51:54 -0800474 iucv_sk(sk)->parent->sk_ack_backlog--;
475 iucv_sk(sk)->parent = NULL;
476 sock_put(sk);
477}
478
479struct sock *iucv_accept_dequeue(struct sock *parent, struct socket *newsock)
480{
481 struct iucv_sock *isk, *n;
482 struct sock *sk;
483
Heiko Carstensda99f052007-05-04 12:23:27 -0700484 list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) {
Jennifer Hunteac37312007-02-08 13:51:54 -0800485 sk = (struct sock *) isk;
486 lock_sock(sk);
487
488 if (sk->sk_state == IUCV_CLOSED) {
Jennifer Hunteac37312007-02-08 13:51:54 -0800489 iucv_accept_unlink(sk);
Ursula Braunfebca282007-07-14 19:04:25 -0700490 release_sock(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800491 continue;
492 }
493
494 if (sk->sk_state == IUCV_CONNECTED ||
495 sk->sk_state == IUCV_SEVERED ||
496 !newsock) {
497 iucv_accept_unlink(sk);
498 if (newsock)
499 sock_graft(sk, newsock);
500
501 if (sk->sk_state == IUCV_SEVERED)
502 sk->sk_state = IUCV_DISCONN;
503
504 release_sock(sk);
505 return sk;
506 }
507
508 release_sock(sk);
509 }
510 return NULL;
511}
512
513int iucv_sock_wait_state(struct sock *sk, int state, int state2,
514 unsigned long timeo)
515{
516 DECLARE_WAITQUEUE(wait, current);
517 int err = 0;
518
519 add_wait_queue(sk->sk_sleep, &wait);
520 while (sk->sk_state != state && sk->sk_state != state2) {
521 set_current_state(TASK_INTERRUPTIBLE);
522
523 if (!timeo) {
524 err = -EAGAIN;
525 break;
526 }
527
528 if (signal_pending(current)) {
529 err = sock_intr_errno(timeo);
530 break;
531 }
532
533 release_sock(sk);
534 timeo = schedule_timeout(timeo);
535 lock_sock(sk);
536
537 err = sock_error(sk);
538 if (err)
539 break;
540 }
541 set_current_state(TASK_RUNNING);
542 remove_wait_queue(sk->sk_sleep, &wait);
543 return err;
544}
545
546/* Bind an unbound socket */
547static int iucv_sock_bind(struct socket *sock, struct sockaddr *addr,
548 int addr_len)
549{
550 struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
551 struct sock *sk = sock->sk;
552 struct iucv_sock *iucv;
553 int err;
554
555 /* Verify the input sockaddr */
556 if (!addr || addr->sa_family != AF_IUCV)
557 return -EINVAL;
558
559 lock_sock(sk);
560 if (sk->sk_state != IUCV_OPEN) {
561 err = -EBADFD;
562 goto done;
563 }
564
565 write_lock_bh(&iucv_sk_list.lock);
566
567 iucv = iucv_sk(sk);
568 if (__iucv_get_sock_by_name(sa->siucv_name)) {
569 err = -EADDRINUSE;
570 goto done_unlock;
571 }
572 if (iucv->path) {
573 err = 0;
574 goto done_unlock;
575 }
576
577 /* Bind the socket */
578 memcpy(iucv->src_name, sa->siucv_name, 8);
579
580 /* Copy the user id */
581 memcpy(iucv->src_user_id, iucv_userid, 8);
582 sk->sk_state = IUCV_BOUND;
583 err = 0;
584
585done_unlock:
586 /* Release the socket list lock */
587 write_unlock_bh(&iucv_sk_list.lock);
588done:
589 release_sock(sk);
590 return err;
591}
592
593/* Automatically bind an unbound socket */
594static int iucv_sock_autobind(struct sock *sk)
595{
596 struct iucv_sock *iucv = iucv_sk(sk);
597 char query_buffer[80];
598 char name[12];
599 int err = 0;
600
601 /* Set the userid and name */
602 cpcmd("QUERY USERID", query_buffer, sizeof(query_buffer), &err);
603 if (unlikely(err))
604 return -EPROTO;
605
606 memcpy(iucv->src_user_id, query_buffer, 8);
607
608 write_lock_bh(&iucv_sk_list.lock);
609
610 sprintf(name, "%08x", atomic_inc_return(&iucv_sk_list.autobind_name));
611 while (__iucv_get_sock_by_name(name)) {
612 sprintf(name, "%08x",
613 atomic_inc_return(&iucv_sk_list.autobind_name));
614 }
615
616 write_unlock_bh(&iucv_sk_list.lock);
617
618 memcpy(&iucv->src_name, name, 8);
619
620 return err;
621}
622
623/* Connect an unconnected socket */
624static int iucv_sock_connect(struct socket *sock, struct sockaddr *addr,
625 int alen, int flags)
626{
627 struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
628 struct sock *sk = sock->sk;
629 struct iucv_sock *iucv;
630 unsigned char user_data[16];
631 int err;
632
633 if (addr->sa_family != AF_IUCV || alen < sizeof(struct sockaddr_iucv))
634 return -EINVAL;
635
636 if (sk->sk_state != IUCV_OPEN && sk->sk_state != IUCV_BOUND)
637 return -EBADFD;
638
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +0000639 if (sk->sk_type != SOCK_STREAM && sk->sk_type != SOCK_SEQPACKET)
Jennifer Hunteac37312007-02-08 13:51:54 -0800640 return -EINVAL;
641
Jennifer Hunteac37312007-02-08 13:51:54 -0800642 if (sk->sk_state == IUCV_OPEN) {
643 err = iucv_sock_autobind(sk);
644 if (unlikely(err))
645 return err;
646 }
647
648 lock_sock(sk);
649
650 /* Set the destination information */
651 memcpy(iucv_sk(sk)->dst_user_id, sa->siucv_user_id, 8);
652 memcpy(iucv_sk(sk)->dst_name, sa->siucv_name, 8);
653
654 high_nmcpy(user_data, sa->siucv_name);
655 low_nmcpy(user_data, iucv_sk(sk)->src_name);
656 ASCEBC(user_data, sizeof(user_data));
657
658 iucv = iucv_sk(sk);
659 /* Create path. */
Hendrik Brueckner09488e22009-04-21 23:26:27 +0000660 iucv->path = iucv_path_alloc(iucv->msglimit,
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000661 IUCV_IPRMDATA, GFP_KERNEL);
Ursula Braund4444722008-02-07 18:07:19 -0800662 if (!iucv->path) {
663 err = -ENOMEM;
664 goto done;
665 }
Jennifer Hunteac37312007-02-08 13:51:54 -0800666 err = iucv_path_connect(iucv->path, &af_iucv_handler,
667 sa->siucv_user_id, NULL, user_data, sk);
668 if (err) {
669 iucv_path_free(iucv->path);
670 iucv->path = NULL;
Hendrik Brueckner55cdea92009-01-05 18:07:07 -0800671 switch (err) {
672 case 0x0b: /* Target communicator is not logged on */
673 err = -ENETUNREACH;
674 break;
675 case 0x0d: /* Max connections for this guest exceeded */
676 case 0x0e: /* Max connections for target guest exceeded */
677 err = -EAGAIN;
678 break;
679 case 0x0f: /* Missing IUCV authorization */
680 err = -EACCES;
681 break;
682 default:
683 err = -ECONNREFUSED;
684 break;
685 }
Jennifer Hunteac37312007-02-08 13:51:54 -0800686 goto done;
687 }
688
689 if (sk->sk_state != IUCV_CONNECTED) {
690 err = iucv_sock_wait_state(sk, IUCV_CONNECTED, IUCV_DISCONN,
691 sock_sndtimeo(sk, flags & O_NONBLOCK));
692 }
693
694 if (sk->sk_state == IUCV_DISCONN) {
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000695 err = -ECONNREFUSED;
Jennifer Hunteac37312007-02-08 13:51:54 -0800696 }
Ursula Braun18becbc2009-01-05 18:07:46 -0800697
698 if (err) {
699 iucv_path_sever(iucv->path, NULL);
700 iucv_path_free(iucv->path);
701 iucv->path = NULL;
702 }
703
Jennifer Hunteac37312007-02-08 13:51:54 -0800704done:
705 release_sock(sk);
706 return err;
707}
708
709/* Move a socket into listening state. */
710static int iucv_sock_listen(struct socket *sock, int backlog)
711{
712 struct sock *sk = sock->sk;
713 int err;
714
715 lock_sock(sk);
716
717 err = -EINVAL;
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +0000718 if (sk->sk_state != IUCV_BOUND)
719 goto done;
720
721 if (sock->type != SOCK_STREAM && sock->type != SOCK_SEQPACKET)
Jennifer Hunteac37312007-02-08 13:51:54 -0800722 goto done;
723
724 sk->sk_max_ack_backlog = backlog;
725 sk->sk_ack_backlog = 0;
726 sk->sk_state = IUCV_LISTEN;
727 err = 0;
728
729done:
730 release_sock(sk);
731 return err;
732}
733
734/* Accept a pending connection */
735static int iucv_sock_accept(struct socket *sock, struct socket *newsock,
736 int flags)
737{
738 DECLARE_WAITQUEUE(wait, current);
739 struct sock *sk = sock->sk, *nsk;
740 long timeo;
741 int err = 0;
742
Jennifer Hunt561e0362007-05-04 12:22:07 -0700743 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
Jennifer Hunteac37312007-02-08 13:51:54 -0800744
745 if (sk->sk_state != IUCV_LISTEN) {
746 err = -EBADFD;
747 goto done;
748 }
749
750 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
751
752 /* Wait for an incoming connection */
753 add_wait_queue_exclusive(sk->sk_sleep, &wait);
Heiko Carstensda99f052007-05-04 12:23:27 -0700754 while (!(nsk = iucv_accept_dequeue(sk, newsock))) {
Jennifer Hunteac37312007-02-08 13:51:54 -0800755 set_current_state(TASK_INTERRUPTIBLE);
756 if (!timeo) {
757 err = -EAGAIN;
758 break;
759 }
760
761 release_sock(sk);
762 timeo = schedule_timeout(timeo);
Jennifer Hunt561e0362007-05-04 12:22:07 -0700763 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
Jennifer Hunteac37312007-02-08 13:51:54 -0800764
765 if (sk->sk_state != IUCV_LISTEN) {
766 err = -EBADFD;
767 break;
768 }
769
770 if (signal_pending(current)) {
771 err = sock_intr_errno(timeo);
772 break;
773 }
774 }
775
776 set_current_state(TASK_RUNNING);
777 remove_wait_queue(sk->sk_sleep, &wait);
778
779 if (err)
780 goto done;
781
782 newsock->state = SS_CONNECTED;
783
784done:
785 release_sock(sk);
786 return err;
787}
788
789static int iucv_sock_getname(struct socket *sock, struct sockaddr *addr,
790 int *len, int peer)
791{
792 struct sockaddr_iucv *siucv = (struct sockaddr_iucv *) addr;
793 struct sock *sk = sock->sk;
794
795 addr->sa_family = AF_IUCV;
796 *len = sizeof(struct sockaddr_iucv);
797
798 if (peer) {
799 memcpy(siucv->siucv_user_id, iucv_sk(sk)->dst_user_id, 8);
800 memcpy(siucv->siucv_name, &iucv_sk(sk)->dst_name, 8);
801 } else {
802 memcpy(siucv->siucv_user_id, iucv_sk(sk)->src_user_id, 8);
803 memcpy(siucv->siucv_name, iucv_sk(sk)->src_name, 8);
804 }
805 memset(&siucv->siucv_port, 0, sizeof(siucv->siucv_port));
806 memset(&siucv->siucv_addr, 0, sizeof(siucv->siucv_addr));
807 memset(siucv->siucv_nodeid, 0, sizeof(siucv->siucv_nodeid));
808
809 return 0;
810}
811
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000812/**
813 * iucv_send_iprm() - Send socket data in parameter list of an iucv message.
814 * @path: IUCV path
815 * @msg: Pointer to a struct iucv_message
816 * @skb: The socket data to send, skb->len MUST BE <= 7
817 *
818 * Send the socket data in the parameter list in the iucv message
819 * (IUCV_IPRMDATA). The socket data is stored at index 0 to 6 in the parameter
820 * list and the socket data len at index 7 (last byte).
821 * See also iucv_msg_length().
822 *
823 * Returns the error code from the iucv_message_send() call.
824 */
825static int iucv_send_iprm(struct iucv_path *path, struct iucv_message *msg,
826 struct sk_buff *skb)
827{
828 u8 prmdata[8];
829
830 memcpy(prmdata, (void *) skb->data, skb->len);
831 prmdata[7] = 0xff - (u8) skb->len;
832 return iucv_message_send(path, msg, IUCV_IPRMDATA, 0,
833 (void *) prmdata, 8);
834}
835
Jennifer Hunteac37312007-02-08 13:51:54 -0800836static int iucv_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
837 struct msghdr *msg, size_t len)
838{
839 struct sock *sk = sock->sk;
840 struct iucv_sock *iucv = iucv_sk(sk);
841 struct sk_buff *skb;
842 struct iucv_message txmsg;
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +0000843 struct cmsghdr *cmsg;
844 int cmsg_done;
Ursula Braun8f7c5022008-12-25 13:39:47 +0100845 char user_id[9];
846 char appl_id[9];
Jennifer Hunteac37312007-02-08 13:51:54 -0800847 int err;
848
849 err = sock_error(sk);
850 if (err)
851 return err;
852
853 if (msg->msg_flags & MSG_OOB)
854 return -EOPNOTSUPP;
855
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +0000856 /* SOCK_SEQPACKET: we do not support segmented records */
857 if (sk->sk_type == SOCK_SEQPACKET && !(msg->msg_flags & MSG_EOR))
858 return -EOPNOTSUPP;
859
Jennifer Hunteac37312007-02-08 13:51:54 -0800860 lock_sock(sk);
861
862 if (sk->sk_shutdown & SEND_SHUTDOWN) {
863 err = -EPIPE;
864 goto out;
865 }
866
Heiko Carstensda99f052007-05-04 12:23:27 -0700867 if (sk->sk_state == IUCV_CONNECTED) {
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +0000868 /* initialize defaults */
869 cmsg_done = 0; /* check for duplicate headers */
870 txmsg.class = 0;
871
872 /* iterate over control messages */
873 for (cmsg = CMSG_FIRSTHDR(msg); cmsg;
874 cmsg = CMSG_NXTHDR(msg, cmsg)) {
875
876 if (!CMSG_OK(msg, cmsg)) {
877 err = -EINVAL;
878 goto out;
879 }
880
881 if (cmsg->cmsg_level != SOL_IUCV)
882 continue;
883
884 if (cmsg->cmsg_type & cmsg_done) {
885 err = -EINVAL;
886 goto out;
887 }
888 cmsg_done |= cmsg->cmsg_type;
889
890 switch (cmsg->cmsg_type) {
891 case SCM_IUCV_TRGCLS:
892 if (cmsg->cmsg_len != CMSG_LEN(TRGCLS_SIZE)) {
893 err = -EINVAL;
894 goto out;
895 }
896
897 /* set iucv message target class */
898 memcpy(&txmsg.class,
899 (void *) CMSG_DATA(cmsg), TRGCLS_SIZE);
900
901 break;
902
903 default:
904 err = -EINVAL;
905 goto out;
906 break;
907 }
908 }
909
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +0000910 /* allocate one skb for each iucv message:
911 * this is fine for SOCK_SEQPACKET (unless we want to support
912 * segmented records using the MSG_EOR flag), but
913 * for SOCK_STREAM we might want to improve it in future */
Heiko Carstensda99f052007-05-04 12:23:27 -0700914 if (!(skb = sock_alloc_send_skb(sk, len,
915 msg->msg_flags & MSG_DONTWAIT,
916 &err)))
Jennifer Hunt561e0362007-05-04 12:22:07 -0700917 goto out;
Jennifer Hunteac37312007-02-08 13:51:54 -0800918
Heiko Carstensda99f052007-05-04 12:23:27 -0700919 if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
Jennifer Hunteac37312007-02-08 13:51:54 -0800920 err = -EFAULT;
921 goto fail;
922 }
923
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +0000924 /* increment and save iucv message tag for msg_completion cbk */
Jennifer Hunteac37312007-02-08 13:51:54 -0800925 txmsg.tag = iucv->send_tag++;
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +0000926 memcpy(CB_TAG(skb), &txmsg.tag, CB_TAG_LEN);
Jennifer Hunteac37312007-02-08 13:51:54 -0800927 skb_queue_tail(&iucv->send_skb_q, skb);
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000928
929 if (((iucv->path->flags & IUCV_IPRMDATA) & iucv->flags)
930 && skb->len <= 7) {
931 err = iucv_send_iprm(iucv->path, &txmsg, skb);
932
933 /* on success: there is no message_complete callback
934 * for an IPRMDATA msg; remove skb from send queue */
935 if (err == 0) {
936 skb_unlink(skb, &iucv->send_skb_q);
937 kfree_skb(skb);
938 }
939
940 /* this error should never happen since the
941 * IUCV_IPRMDATA path flag is set... sever path */
942 if (err == 0x15) {
943 iucv_path_sever(iucv->path, NULL);
944 skb_unlink(skb, &iucv->send_skb_q);
945 err = -EPIPE;
946 goto fail;
947 }
948 } else
949 err = iucv_message_send(iucv->path, &txmsg, 0, 0,
950 (void *) skb->data, skb->len);
Jennifer Hunteac37312007-02-08 13:51:54 -0800951 if (err) {
Ursula Braun8f7c5022008-12-25 13:39:47 +0100952 if (err == 3) {
953 user_id[8] = 0;
954 memcpy(user_id, iucv->dst_user_id, 8);
955 appl_id[8] = 0;
956 memcpy(appl_id, iucv->dst_name, 8);
957 pr_err("Application %s on z/VM guest %s"
958 " exceeds message limit\n",
959 user_id, appl_id);
960 }
Jennifer Hunteac37312007-02-08 13:51:54 -0800961 skb_unlink(skb, &iucv->send_skb_q);
962 err = -EPIPE;
963 goto fail;
964 }
965
966 } else {
967 err = -ENOTCONN;
968 goto out;
969 }
970
971 release_sock(sk);
972 return len;
973
974fail:
975 kfree_skb(skb);
976out:
977 release_sock(sk);
978 return err;
979}
980
Ursula Braunf0703c82007-10-08 02:03:31 -0700981static int iucv_fragment_skb(struct sock *sk, struct sk_buff *skb, int len)
982{
983 int dataleft, size, copied = 0;
984 struct sk_buff *nskb;
985
986 dataleft = len;
987 while (dataleft) {
988 if (dataleft >= sk->sk_rcvbuf / 4)
989 size = sk->sk_rcvbuf / 4;
990 else
991 size = dataleft;
992
993 nskb = alloc_skb(size, GFP_ATOMIC | GFP_DMA);
994 if (!nskb)
995 return -ENOMEM;
996
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +0000997 /* copy target class to control buffer of new skb */
998 memcpy(CB_TRGCLS(nskb), CB_TRGCLS(skb), CB_TRGCLS_LEN);
999
1000 /* copy data fragment */
Ursula Braunf0703c82007-10-08 02:03:31 -07001001 memcpy(nskb->data, skb->data + copied, size);
1002 copied += size;
1003 dataleft -= size;
1004
1005 skb_reset_transport_header(nskb);
1006 skb_reset_network_header(nskb);
1007 nskb->len = size;
1008
1009 skb_queue_tail(&iucv_sk(sk)->backlog_skb_q, nskb);
1010 }
1011
1012 return 0;
1013}
1014
1015static void iucv_process_message(struct sock *sk, struct sk_buff *skb,
1016 struct iucv_path *path,
1017 struct iucv_message *msg)
1018{
1019 int rc;
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001020 unsigned int len;
Ursula Braunf0703c82007-10-08 02:03:31 -07001021
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001022 len = iucv_msg_length(msg);
1023
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001024 /* store msg target class in the second 4 bytes of skb ctrl buffer */
1025 /* Note: the first 4 bytes are reserved for msg tag */
1026 memcpy(CB_TRGCLS(skb), &msg->class, CB_TRGCLS_LEN);
1027
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001028 /* check for special IPRM messages (e.g. iucv_sock_shutdown) */
1029 if ((msg->flags & IUCV_IPRMDATA) && len > 7) {
1030 if (memcmp(msg->rmmsg, iprm_shutdown, 8) == 0) {
1031 skb->data = NULL;
1032 skb->len = 0;
1033 }
Ursula Braunf0703c82007-10-08 02:03:31 -07001034 } else {
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001035 rc = iucv_message_receive(path, msg, msg->flags & IUCV_IPRMDATA,
1036 skb->data, len, NULL);
Ursula Braunf0703c82007-10-08 02:03:31 -07001037 if (rc) {
1038 kfree_skb(skb);
1039 return;
1040 }
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001041 /* we need to fragment iucv messages for SOCK_STREAM only;
1042 * for SOCK_SEQPACKET, it is only relevant if we support
1043 * record segmentation using MSG_EOR (see also recvmsg()) */
1044 if (sk->sk_type == SOCK_STREAM &&
1045 skb->truesize >= sk->sk_rcvbuf / 4) {
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001046 rc = iucv_fragment_skb(sk, skb, len);
Ursula Braunf0703c82007-10-08 02:03:31 -07001047 kfree_skb(skb);
1048 skb = NULL;
1049 if (rc) {
1050 iucv_path_sever(path, NULL);
1051 return;
1052 }
1053 skb = skb_dequeue(&iucv_sk(sk)->backlog_skb_q);
1054 } else {
1055 skb_reset_transport_header(skb);
1056 skb_reset_network_header(skb);
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001057 skb->len = len;
Ursula Braunf0703c82007-10-08 02:03:31 -07001058 }
1059 }
1060
1061 if (sock_queue_rcv_skb(sk, skb))
1062 skb_queue_head(&iucv_sk(sk)->backlog_skb_q, skb);
1063}
1064
1065static void iucv_process_message_q(struct sock *sk)
1066{
1067 struct iucv_sock *iucv = iucv_sk(sk);
1068 struct sk_buff *skb;
1069 struct sock_msg_q *p, *n;
1070
1071 list_for_each_entry_safe(p, n, &iucv->message_q.list, list) {
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001072 skb = alloc_skb(iucv_msg_length(&p->msg), GFP_ATOMIC | GFP_DMA);
Ursula Braunf0703c82007-10-08 02:03:31 -07001073 if (!skb)
1074 break;
1075 iucv_process_message(sk, skb, p->path, &p->msg);
1076 list_del(&p->list);
1077 kfree(p);
1078 if (!skb_queue_empty(&iucv->backlog_skb_q))
1079 break;
1080 }
1081}
1082
Jennifer Hunteac37312007-02-08 13:51:54 -08001083static int iucv_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
1084 struct msghdr *msg, size_t len, int flags)
1085{
1086 int noblock = flags & MSG_DONTWAIT;
1087 struct sock *sk = sock->sk;
Jennifer Hunt561e0362007-05-04 12:22:07 -07001088 struct iucv_sock *iucv = iucv_sk(sk);
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001089 unsigned int copied, rlen;
Jennifer Hunt561e0362007-05-04 12:22:07 -07001090 struct sk_buff *skb, *rskb, *cskb;
Jennifer Hunteac37312007-02-08 13:51:54 -08001091 int err = 0;
1092
Jennifer Hunt561e0362007-05-04 12:22:07 -07001093 if ((sk->sk_state == IUCV_DISCONN || sk->sk_state == IUCV_SEVERED) &&
Ursula Braunf0703c82007-10-08 02:03:31 -07001094 skb_queue_empty(&iucv->backlog_skb_q) &&
1095 skb_queue_empty(&sk->sk_receive_queue) &&
1096 list_empty(&iucv->message_q.list))
Jennifer Hunt561e0362007-05-04 12:22:07 -07001097 return 0;
1098
Jennifer Hunteac37312007-02-08 13:51:54 -08001099 if (flags & (MSG_OOB))
1100 return -EOPNOTSUPP;
1101
Hendrik Brueckner60d37052009-04-21 06:04:21 +00001102 /* receive/dequeue next skb:
1103 * the function understands MSG_PEEK and, thus, does not dequeue skb */
Jennifer Hunteac37312007-02-08 13:51:54 -08001104 skb = skb_recv_datagram(sk, flags, noblock, &err);
1105 if (!skb) {
1106 if (sk->sk_shutdown & RCV_SHUTDOWN)
1107 return 0;
1108 return err;
1109 }
1110
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001111 rlen = skb->len; /* real length of skb */
1112 copied = min_t(unsigned int, rlen, len);
Jennifer Hunteac37312007-02-08 13:51:54 -08001113
Jennifer Hunt561e0362007-05-04 12:22:07 -07001114 cskb = skb;
1115 if (memcpy_toiovec(msg->msg_iov, cskb->data, copied)) {
Hendrik Brueckner802788b2009-04-21 23:26:26 +00001116 if (!(flags & MSG_PEEK))
1117 skb_queue_head(&sk->sk_receive_queue, skb);
1118 return -EFAULT;
Jennifer Hunteac37312007-02-08 13:51:54 -08001119 }
1120
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001121 /* SOCK_SEQPACKET: set MSG_TRUNC if recv buf size is too small */
1122 if (sk->sk_type == SOCK_SEQPACKET) {
1123 if (copied < rlen)
1124 msg->msg_flags |= MSG_TRUNC;
1125 /* each iucv message contains a complete record */
1126 msg->msg_flags |= MSG_EOR;
1127 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001128
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001129 /* create control message to store iucv msg target class:
1130 * get the trgcls from the control buffer of the skb due to
1131 * fragmentation of original iucv message. */
1132 err = put_cmsg(msg, SOL_IUCV, SCM_IUCV_TRGCLS,
1133 CB_TRGCLS_LEN, CB_TRGCLS(skb));
1134 if (err) {
1135 if (!(flags & MSG_PEEK))
1136 skb_queue_head(&sk->sk_receive_queue, skb);
1137 return err;
1138 }
1139
Jennifer Hunteac37312007-02-08 13:51:54 -08001140 /* Mark read part of skb as used */
1141 if (!(flags & MSG_PEEK)) {
Jennifer Hunteac37312007-02-08 13:51:54 -08001142
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001143 /* SOCK_STREAM: re-queue skb if it contains unreceived data */
1144 if (sk->sk_type == SOCK_STREAM) {
1145 skb_pull(skb, copied);
1146 if (skb->len) {
1147 skb_queue_head(&sk->sk_receive_queue, skb);
1148 goto done;
1149 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001150 }
1151
1152 kfree_skb(skb);
Jennifer Hunt561e0362007-05-04 12:22:07 -07001153
1154 /* Queue backlog skbs */
Ursula Braunf0703c82007-10-08 02:03:31 -07001155 rskb = skb_dequeue(&iucv->backlog_skb_q);
Heiko Carstensda99f052007-05-04 12:23:27 -07001156 while (rskb) {
Jennifer Hunt561e0362007-05-04 12:22:07 -07001157 if (sock_queue_rcv_skb(sk, rskb)) {
Ursula Braunf0703c82007-10-08 02:03:31 -07001158 skb_queue_head(&iucv->backlog_skb_q,
Jennifer Hunt561e0362007-05-04 12:22:07 -07001159 rskb);
1160 break;
1161 } else {
Ursula Braunf0703c82007-10-08 02:03:31 -07001162 rskb = skb_dequeue(&iucv->backlog_skb_q);
Jennifer Hunt561e0362007-05-04 12:22:07 -07001163 }
1164 }
Ursula Braunf0703c82007-10-08 02:03:31 -07001165 if (skb_queue_empty(&iucv->backlog_skb_q)) {
1166 spin_lock_bh(&iucv->message_q.lock);
1167 if (!list_empty(&iucv->message_q.list))
1168 iucv_process_message_q(sk);
1169 spin_unlock_bh(&iucv->message_q.lock);
1170 }
Hendrik Brueckner60d37052009-04-21 06:04:21 +00001171 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001172
1173done:
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001174 /* SOCK_SEQPACKET: return real length if MSG_TRUNC is set */
1175 if (sk->sk_type == SOCK_SEQPACKET && (flags & MSG_TRUNC))
1176 copied = rlen;
1177
1178 return copied;
Jennifer Hunteac37312007-02-08 13:51:54 -08001179}
1180
1181static inline unsigned int iucv_accept_poll(struct sock *parent)
1182{
1183 struct iucv_sock *isk, *n;
1184 struct sock *sk;
1185
Heiko Carstensda99f052007-05-04 12:23:27 -07001186 list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) {
Jennifer Hunteac37312007-02-08 13:51:54 -08001187 sk = (struct sock *) isk;
1188
1189 if (sk->sk_state == IUCV_CONNECTED)
1190 return POLLIN | POLLRDNORM;
1191 }
1192
1193 return 0;
1194}
1195
1196unsigned int iucv_sock_poll(struct file *file, struct socket *sock,
1197 poll_table *wait)
1198{
1199 struct sock *sk = sock->sk;
1200 unsigned int mask = 0;
1201
1202 poll_wait(file, sk->sk_sleep, wait);
1203
1204 if (sk->sk_state == IUCV_LISTEN)
1205 return iucv_accept_poll(sk);
1206
1207 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
1208 mask |= POLLERR;
1209
1210 if (sk->sk_shutdown & RCV_SHUTDOWN)
1211 mask |= POLLRDHUP;
1212
1213 if (sk->sk_shutdown == SHUTDOWN_MASK)
1214 mask |= POLLHUP;
1215
1216 if (!skb_queue_empty(&sk->sk_receive_queue) ||
Heiko Carstensda99f052007-05-04 12:23:27 -07001217 (sk->sk_shutdown & RCV_SHUTDOWN))
Jennifer Hunteac37312007-02-08 13:51:54 -08001218 mask |= POLLIN | POLLRDNORM;
1219
1220 if (sk->sk_state == IUCV_CLOSED)
1221 mask |= POLLHUP;
1222
Jennifer Hunt561e0362007-05-04 12:22:07 -07001223 if (sk->sk_state == IUCV_DISCONN || sk->sk_state == IUCV_SEVERED)
1224 mask |= POLLIN;
1225
Jennifer Hunteac37312007-02-08 13:51:54 -08001226 if (sock_writeable(sk))
1227 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
1228 else
1229 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
1230
1231 return mask;
1232}
1233
1234static int iucv_sock_shutdown(struct socket *sock, int how)
1235{
1236 struct sock *sk = sock->sk;
1237 struct iucv_sock *iucv = iucv_sk(sk);
1238 struct iucv_message txmsg;
1239 int err = 0;
Jennifer Hunteac37312007-02-08 13:51:54 -08001240
1241 how++;
1242
1243 if ((how & ~SHUTDOWN_MASK) || !how)
1244 return -EINVAL;
1245
1246 lock_sock(sk);
Heiko Carstensda99f052007-05-04 12:23:27 -07001247 switch (sk->sk_state) {
Hendrik Bruecknere14ad5f2009-04-21 06:04:23 +00001248 case IUCV_DISCONN:
1249 case IUCV_CLOSING:
1250 case IUCV_SEVERED:
Jennifer Hunteac37312007-02-08 13:51:54 -08001251 case IUCV_CLOSED:
1252 err = -ENOTCONN;
1253 goto fail;
1254
1255 default:
1256 sk->sk_shutdown |= how;
1257 break;
1258 }
1259
1260 if (how == SEND_SHUTDOWN || how == SHUTDOWN_MASK) {
1261 txmsg.class = 0;
1262 txmsg.tag = 0;
1263 err = iucv_message_send(iucv->path, &txmsg, IUCV_IPRMDATA, 0,
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001264 (void *) iprm_shutdown, 8);
Jennifer Hunteac37312007-02-08 13:51:54 -08001265 if (err) {
Heiko Carstensda99f052007-05-04 12:23:27 -07001266 switch (err) {
Jennifer Hunteac37312007-02-08 13:51:54 -08001267 case 1:
1268 err = -ENOTCONN;
1269 break;
1270 case 2:
1271 err = -ECONNRESET;
1272 break;
1273 default:
1274 err = -ENOTCONN;
1275 break;
1276 }
1277 }
1278 }
1279
1280 if (how == RCV_SHUTDOWN || how == SHUTDOWN_MASK) {
1281 err = iucv_path_quiesce(iucv_sk(sk)->path, NULL);
1282 if (err)
1283 err = -ENOTCONN;
1284
1285 skb_queue_purge(&sk->sk_receive_queue);
1286 }
1287
1288 /* Wake up anyone sleeping in poll */
1289 sk->sk_state_change(sk);
1290
1291fail:
1292 release_sock(sk);
1293 return err;
1294}
1295
1296static int iucv_sock_release(struct socket *sock)
1297{
1298 struct sock *sk = sock->sk;
1299 int err = 0;
1300
1301 if (!sk)
1302 return 0;
1303
1304 iucv_sock_close(sk);
1305
1306 /* Unregister with IUCV base support */
1307 if (iucv_sk(sk)->path) {
1308 iucv_path_sever(iucv_sk(sk)->path, NULL);
1309 iucv_path_free(iucv_sk(sk)->path);
1310 iucv_sk(sk)->path = NULL;
1311 }
1312
Jennifer Hunteac37312007-02-08 13:51:54 -08001313 sock_orphan(sk);
1314 iucv_sock_kill(sk);
1315 return err;
1316}
1317
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00001318/* getsockopt and setsockopt */
1319static int iucv_sock_setsockopt(struct socket *sock, int level, int optname,
1320 char __user *optval, int optlen)
1321{
1322 struct sock *sk = sock->sk;
1323 struct iucv_sock *iucv = iucv_sk(sk);
1324 int val;
1325 int rc;
1326
1327 if (level != SOL_IUCV)
1328 return -ENOPROTOOPT;
1329
1330 if (optlen < sizeof(int))
1331 return -EINVAL;
1332
1333 if (get_user(val, (int __user *) optval))
1334 return -EFAULT;
1335
1336 rc = 0;
1337
1338 lock_sock(sk);
1339 switch (optname) {
1340 case SO_IPRMDATA_MSG:
1341 if (val)
1342 iucv->flags |= IUCV_IPRMDATA;
1343 else
1344 iucv->flags &= ~IUCV_IPRMDATA;
1345 break;
Hendrik Brueckner09488e22009-04-21 23:26:27 +00001346 case SO_MSGLIMIT:
1347 switch (sk->sk_state) {
1348 case IUCV_OPEN:
1349 case IUCV_BOUND:
1350 if (val < 1 || val > (u16)(~0))
1351 rc = -EINVAL;
1352 else
1353 iucv->msglimit = val;
1354 break;
1355 default:
1356 rc = -EINVAL;
1357 break;
1358 }
1359 break;
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00001360 default:
1361 rc = -ENOPROTOOPT;
1362 break;
1363 }
1364 release_sock(sk);
1365
1366 return rc;
1367}
1368
1369static int iucv_sock_getsockopt(struct socket *sock, int level, int optname,
1370 char __user *optval, int __user *optlen)
1371{
1372 struct sock *sk = sock->sk;
1373 struct iucv_sock *iucv = iucv_sk(sk);
1374 int val, len;
1375
1376 if (level != SOL_IUCV)
1377 return -ENOPROTOOPT;
1378
1379 if (get_user(len, optlen))
1380 return -EFAULT;
1381
1382 if (len < 0)
1383 return -EINVAL;
1384
1385 len = min_t(unsigned int, len, sizeof(int));
1386
1387 switch (optname) {
1388 case SO_IPRMDATA_MSG:
1389 val = (iucv->flags & IUCV_IPRMDATA) ? 1 : 0;
1390 break;
Hendrik Brueckner09488e22009-04-21 23:26:27 +00001391 case SO_MSGLIMIT:
1392 lock_sock(sk);
1393 val = (iucv->path != NULL) ? iucv->path->msglim /* connected */
1394 : iucv->msglimit; /* default */
1395 release_sock(sk);
1396 break;
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00001397 default:
1398 return -ENOPROTOOPT;
1399 }
1400
1401 if (put_user(len, optlen))
1402 return -EFAULT;
1403 if (copy_to_user(optval, &val, len))
1404 return -EFAULT;
1405
1406 return 0;
1407}
1408
1409
Jennifer Hunteac37312007-02-08 13:51:54 -08001410/* Callback wrappers - called from iucv base support */
1411static int iucv_callback_connreq(struct iucv_path *path,
1412 u8 ipvmid[8], u8 ipuser[16])
1413{
1414 unsigned char user_data[16];
1415 unsigned char nuser_data[16];
1416 unsigned char src_name[8];
1417 struct hlist_node *node;
1418 struct sock *sk, *nsk;
1419 struct iucv_sock *iucv, *niucv;
1420 int err;
1421
1422 memcpy(src_name, ipuser, 8);
1423 EBCASC(src_name, 8);
1424 /* Find out if this path belongs to af_iucv. */
1425 read_lock(&iucv_sk_list.lock);
1426 iucv = NULL;
Ursula Braunfebca282007-07-14 19:04:25 -07001427 sk = NULL;
Jennifer Hunteac37312007-02-08 13:51:54 -08001428 sk_for_each(sk, node, &iucv_sk_list.head)
1429 if (sk->sk_state == IUCV_LISTEN &&
1430 !memcmp(&iucv_sk(sk)->src_name, src_name, 8)) {
1431 /*
1432 * Found a listening socket with
1433 * src_name == ipuser[0-7].
1434 */
1435 iucv = iucv_sk(sk);
1436 break;
1437 }
1438 read_unlock(&iucv_sk_list.lock);
1439 if (!iucv)
1440 /* No socket found, not one of our paths. */
1441 return -EINVAL;
1442
1443 bh_lock_sock(sk);
1444
1445 /* Check if parent socket is listening */
1446 low_nmcpy(user_data, iucv->src_name);
1447 high_nmcpy(user_data, iucv->dst_name);
1448 ASCEBC(user_data, sizeof(user_data));
1449 if (sk->sk_state != IUCV_LISTEN) {
1450 err = iucv_path_sever(path, user_data);
Hendrik Brueckner65dbd7c2009-01-05 18:08:23 -08001451 iucv_path_free(path);
Jennifer Hunteac37312007-02-08 13:51:54 -08001452 goto fail;
1453 }
1454
1455 /* Check for backlog size */
1456 if (sk_acceptq_is_full(sk)) {
1457 err = iucv_path_sever(path, user_data);
Hendrik Brueckner65dbd7c2009-01-05 18:08:23 -08001458 iucv_path_free(path);
Jennifer Hunteac37312007-02-08 13:51:54 -08001459 goto fail;
1460 }
1461
1462 /* Create the new socket */
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001463 nsk = iucv_sock_alloc(NULL, sk->sk_type, GFP_ATOMIC);
Heiko Carstensda99f052007-05-04 12:23:27 -07001464 if (!nsk) {
Jennifer Hunteac37312007-02-08 13:51:54 -08001465 err = iucv_path_sever(path, user_data);
Hendrik Brueckner65dbd7c2009-01-05 18:08:23 -08001466 iucv_path_free(path);
Jennifer Hunteac37312007-02-08 13:51:54 -08001467 goto fail;
1468 }
1469
1470 niucv = iucv_sk(nsk);
1471 iucv_sock_init(nsk, sk);
1472
1473 /* Set the new iucv_sock */
1474 memcpy(niucv->dst_name, ipuser + 8, 8);
1475 EBCASC(niucv->dst_name, 8);
1476 memcpy(niucv->dst_user_id, ipvmid, 8);
1477 memcpy(niucv->src_name, iucv->src_name, 8);
1478 memcpy(niucv->src_user_id, iucv->src_user_id, 8);
1479 niucv->path = path;
1480
1481 /* Call iucv_accept */
1482 high_nmcpy(nuser_data, ipuser + 8);
1483 memcpy(nuser_data + 8, niucv->src_name, 8);
1484 ASCEBC(nuser_data + 8, 8);
1485
Hendrik Brueckner09488e22009-04-21 23:26:27 +00001486 /* set message limit for path based on msglimit of accepting socket */
1487 niucv->msglimit = iucv->msglimit;
1488 path->msglim = iucv->msglimit;
Jennifer Hunteac37312007-02-08 13:51:54 -08001489 err = iucv_path_accept(path, &af_iucv_handler, nuser_data, nsk);
Heiko Carstensda99f052007-05-04 12:23:27 -07001490 if (err) {
Jennifer Hunteac37312007-02-08 13:51:54 -08001491 err = iucv_path_sever(path, user_data);
Hendrik Brueckner65dbd7c2009-01-05 18:08:23 -08001492 iucv_path_free(path);
1493 iucv_sock_kill(nsk);
Jennifer Hunteac37312007-02-08 13:51:54 -08001494 goto fail;
1495 }
1496
1497 iucv_accept_enqueue(sk, nsk);
1498
1499 /* Wake up accept */
1500 nsk->sk_state = IUCV_CONNECTED;
1501 sk->sk_data_ready(sk, 1);
1502 err = 0;
1503fail:
1504 bh_unlock_sock(sk);
1505 return 0;
1506}
1507
1508static void iucv_callback_connack(struct iucv_path *path, u8 ipuser[16])
1509{
1510 struct sock *sk = path->private;
1511
1512 sk->sk_state = IUCV_CONNECTED;
1513 sk->sk_state_change(sk);
1514}
1515
1516static void iucv_callback_rx(struct iucv_path *path, struct iucv_message *msg)
1517{
1518 struct sock *sk = path->private;
Jennifer Hunt561e0362007-05-04 12:22:07 -07001519 struct iucv_sock *iucv = iucv_sk(sk);
Ursula Braunf0703c82007-10-08 02:03:31 -07001520 struct sk_buff *skb;
1521 struct sock_msg_q *save_msg;
1522 int len;
Jennifer Hunt561e0362007-05-04 12:22:07 -07001523
Hendrik Bruecknerfe86e542009-04-21 06:04:22 +00001524 if (sk->sk_shutdown & RCV_SHUTDOWN) {
1525 iucv_message_reject(path, msg);
Jennifer Hunteac37312007-02-08 13:51:54 -08001526 return;
Hendrik Bruecknerfe86e542009-04-21 06:04:22 +00001527 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001528
Hendrik Brueckner3fa6b5a2009-04-21 06:04:24 +00001529 spin_lock(&iucv->message_q.lock);
Jennifer Hunteac37312007-02-08 13:51:54 -08001530
Ursula Braunf0703c82007-10-08 02:03:31 -07001531 if (!list_empty(&iucv->message_q.list) ||
1532 !skb_queue_empty(&iucv->backlog_skb_q))
1533 goto save_message;
1534
1535 len = atomic_read(&sk->sk_rmem_alloc);
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001536 len += iucv_msg_length(msg) + sizeof(struct sk_buff);
Ursula Braunf0703c82007-10-08 02:03:31 -07001537 if (len > sk->sk_rcvbuf)
1538 goto save_message;
1539
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001540 skb = alloc_skb(iucv_msg_length(msg), GFP_ATOMIC | GFP_DMA);
Ursula Braunf0703c82007-10-08 02:03:31 -07001541 if (!skb)
1542 goto save_message;
Jennifer Hunteac37312007-02-08 13:51:54 -08001543
Ursula Braunf0703c82007-10-08 02:03:31 -07001544 iucv_process_message(sk, skb, path, msg);
Hendrik Brueckner3fa6b5a2009-04-21 06:04:24 +00001545 goto out_unlock;
Jennifer Hunteac37312007-02-08 13:51:54 -08001546
Ursula Braunf0703c82007-10-08 02:03:31 -07001547save_message:
1548 save_msg = kzalloc(sizeof(struct sock_msg_q), GFP_ATOMIC | GFP_DMA);
Ursula Braund4444722008-02-07 18:07:19 -08001549 if (!save_msg)
1550 return;
Ursula Braunf0703c82007-10-08 02:03:31 -07001551 save_msg->path = path;
1552 save_msg->msg = *msg;
1553
Ursula Braunf0703c82007-10-08 02:03:31 -07001554 list_add_tail(&save_msg->list, &iucv->message_q.list);
Hendrik Brueckner3fa6b5a2009-04-21 06:04:24 +00001555
1556out_unlock:
Ursula Braunf0703c82007-10-08 02:03:31 -07001557 spin_unlock(&iucv->message_q.lock);
Jennifer Hunteac37312007-02-08 13:51:54 -08001558}
1559
1560static void iucv_callback_txdone(struct iucv_path *path,
1561 struct iucv_message *msg)
1562{
1563 struct sock *sk = path->private;
Ursula Braunf2a77992008-02-07 18:07:44 -08001564 struct sk_buff *this = NULL;
Jennifer Hunteac37312007-02-08 13:51:54 -08001565 struct sk_buff_head *list = &iucv_sk(sk)->send_skb_q;
1566 struct sk_buff *list_skb = list->next;
1567 unsigned long flags;
1568
Ursula Braunf2a77992008-02-07 18:07:44 -08001569 if (!skb_queue_empty(list)) {
Jennifer Hunt561e0362007-05-04 12:22:07 -07001570 spin_lock_irqsave(&list->lock, flags);
Jennifer Hunteac37312007-02-08 13:51:54 -08001571
Ursula Braunf2a77992008-02-07 18:07:44 -08001572 while (list_skb != (struct sk_buff *)list) {
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001573 if (!memcmp(&msg->tag, CB_TAG(list_skb), CB_TAG_LEN)) {
Ursula Braunf2a77992008-02-07 18:07:44 -08001574 this = list_skb;
1575 break;
1576 }
Jennifer Hunt561e0362007-05-04 12:22:07 -07001577 list_skb = list_skb->next;
Ursula Braunf2a77992008-02-07 18:07:44 -08001578 }
1579 if (this)
1580 __skb_unlink(this, list);
Jennifer Hunteac37312007-02-08 13:51:54 -08001581
Jennifer Hunt561e0362007-05-04 12:22:07 -07001582 spin_unlock_irqrestore(&list->lock, flags);
Jennifer Hunteac37312007-02-08 13:51:54 -08001583
Wei Yongjun47a30b22009-02-25 00:41:03 +00001584 kfree_skb(this);
Jennifer Hunt561e0362007-05-04 12:22:07 -07001585 }
Ursula Braunc2b4afd2008-07-14 09:59:29 +02001586 BUG_ON(!this);
Jennifer Hunt561e0362007-05-04 12:22:07 -07001587
Heiko Carstensda99f052007-05-04 12:23:27 -07001588 if (sk->sk_state == IUCV_CLOSING) {
Jennifer Hunt561e0362007-05-04 12:22:07 -07001589 if (skb_queue_empty(&iucv_sk(sk)->send_skb_q)) {
1590 sk->sk_state = IUCV_CLOSED;
1591 sk->sk_state_change(sk);
1592 }
1593 }
1594
Jennifer Hunteac37312007-02-08 13:51:54 -08001595}
1596
1597static void iucv_callback_connrej(struct iucv_path *path, u8 ipuser[16])
1598{
1599 struct sock *sk = path->private;
1600
1601 if (!list_empty(&iucv_sk(sk)->accept_q))
1602 sk->sk_state = IUCV_SEVERED;
1603 else
1604 sk->sk_state = IUCV_DISCONN;
1605
1606 sk->sk_state_change(sk);
1607}
1608
Hendrik Brueckneraf88b522009-04-21 23:26:21 +00001609/* called if the other communication side shuts down its RECV direction;
1610 * in turn, the callback sets SEND_SHUTDOWN to disable sending of data.
1611 */
1612static void iucv_callback_shutdown(struct iucv_path *path, u8 ipuser[16])
1613{
1614 struct sock *sk = path->private;
1615
1616 bh_lock_sock(sk);
1617 if (sk->sk_state != IUCV_CLOSED) {
1618 sk->sk_shutdown |= SEND_SHUTDOWN;
1619 sk->sk_state_change(sk);
1620 }
1621 bh_unlock_sock(sk);
1622}
1623
Jennifer Hunteac37312007-02-08 13:51:54 -08001624static struct proto_ops iucv_sock_ops = {
1625 .family = PF_IUCV,
1626 .owner = THIS_MODULE,
1627 .release = iucv_sock_release,
1628 .bind = iucv_sock_bind,
1629 .connect = iucv_sock_connect,
1630 .listen = iucv_sock_listen,
1631 .accept = iucv_sock_accept,
1632 .getname = iucv_sock_getname,
1633 .sendmsg = iucv_sock_sendmsg,
1634 .recvmsg = iucv_sock_recvmsg,
1635 .poll = iucv_sock_poll,
1636 .ioctl = sock_no_ioctl,
1637 .mmap = sock_no_mmap,
1638 .socketpair = sock_no_socketpair,
1639 .shutdown = iucv_sock_shutdown,
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00001640 .setsockopt = iucv_sock_setsockopt,
1641 .getsockopt = iucv_sock_getsockopt,
Jennifer Hunteac37312007-02-08 13:51:54 -08001642};
1643
1644static struct net_proto_family iucv_sock_family_ops = {
1645 .family = AF_IUCV,
1646 .owner = THIS_MODULE,
1647 .create = iucv_sock_create,
1648};
1649
Heiko Carstensda99f052007-05-04 12:23:27 -07001650static int __init afiucv_init(void)
Jennifer Hunteac37312007-02-08 13:51:54 -08001651{
1652 int err;
1653
1654 if (!MACHINE_IS_VM) {
Ursula Braun8f7c5022008-12-25 13:39:47 +01001655 pr_err("The af_iucv module cannot be loaded"
1656 " without z/VM\n");
Jennifer Hunteac37312007-02-08 13:51:54 -08001657 err = -EPROTONOSUPPORT;
1658 goto out;
1659 }
1660 cpcmd("QUERY USERID", iucv_userid, sizeof(iucv_userid), &err);
1661 if (unlikely(err)) {
Ursula Braunc2b4afd2008-07-14 09:59:29 +02001662 WARN_ON(err);
Jennifer Hunteac37312007-02-08 13:51:54 -08001663 err = -EPROTONOSUPPORT;
1664 goto out;
1665 }
1666
1667 err = iucv_register(&af_iucv_handler, 0);
1668 if (err)
1669 goto out;
1670 err = proto_register(&iucv_proto, 0);
1671 if (err)
1672 goto out_iucv;
1673 err = sock_register(&iucv_sock_family_ops);
1674 if (err)
1675 goto out_proto;
Ursula Braunc23cad92009-06-16 10:30:44 +02001676 /* establish dummy device */
1677 err = driver_register(&af_iucv_driver);
1678 if (err)
1679 goto out_sock;
1680 af_iucv_dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1681 if (!af_iucv_dev) {
1682 err = -ENOMEM;
1683 goto out_driver;
1684 }
1685 dev_set_name(af_iucv_dev, "af_iucv");
1686 af_iucv_dev->bus = &iucv_bus;
1687 af_iucv_dev->parent = iucv_root;
1688 af_iucv_dev->release = (void (*)(struct device *))kfree;
1689 af_iucv_dev->driver = &af_iucv_driver;
1690 err = device_register(af_iucv_dev);
1691 if (err)
1692 goto out_driver;
1693
Jennifer Hunteac37312007-02-08 13:51:54 -08001694 return 0;
1695
Ursula Braunc23cad92009-06-16 10:30:44 +02001696out_driver:
1697 driver_unregister(&af_iucv_driver);
1698out_sock:
1699 sock_unregister(PF_IUCV);
Jennifer Hunteac37312007-02-08 13:51:54 -08001700out_proto:
1701 proto_unregister(&iucv_proto);
1702out_iucv:
1703 iucv_unregister(&af_iucv_handler, 0);
1704out:
1705 return err;
1706}
1707
1708static void __exit afiucv_exit(void)
1709{
Ursula Braunc23cad92009-06-16 10:30:44 +02001710 device_unregister(af_iucv_dev);
1711 driver_unregister(&af_iucv_driver);
Jennifer Hunteac37312007-02-08 13:51:54 -08001712 sock_unregister(PF_IUCV);
1713 proto_unregister(&iucv_proto);
1714 iucv_unregister(&af_iucv_handler, 0);
Jennifer Hunteac37312007-02-08 13:51:54 -08001715}
1716
1717module_init(afiucv_init);
1718module_exit(afiucv_exit);
1719
1720MODULE_AUTHOR("Jennifer Hunt <jenhunt@us.ibm.com>");
1721MODULE_DESCRIPTION("IUCV Sockets ver " VERSION);
1722MODULE_VERSION(VERSION);
1723MODULE_LICENSE("GPL");
1724MODULE_ALIAS_NETPROTO(PF_IUCV);