Add linux-next specific files for 20110817
[linux-2.6/next.git] / net / iucv / af_iucv.c
blobc39f3a43cd80992df62f393928020207e6676f8c
1 /*
2 * IUCV protocol stack for Linux on zSeries
4 * Copyright IBM Corp. 2006, 2009
6 * Author(s): Jennifer Hunt <jenhunt@us.ibm.com>
7 * Hendrik Brueckner <brueckner@linux.vnet.ibm.com>
8 * PM functions:
9 * Ursula Braun <ursula.braun@de.ibm.com>
12 #define KMSG_COMPONENT "af_iucv"
13 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
15 #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>
30 #include <net/iucv/af_iucv.h>
32 #define VERSION "1.2"
34 static char iucv_userid[80];
36 static const struct proto_ops iucv_sock_ops;
38 static struct proto iucv_proto = {
39 .name = "AF_IUCV",
40 .owner = THIS_MODULE,
41 .obj_size = sizeof(struct iucv_sock),
44 static struct iucv_interface *pr_iucv;
46 /* special AF_IUCV IPRM messages */
47 static const u8 iprm_shutdown[8] =
48 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01};
50 #define TRGCLS_SIZE (sizeof(((struct iucv_message *)0)->class))
52 /* macros to set/get socket control buffer at correct offset */
53 #define CB_TAG(skb) ((skb)->cb) /* iucv message tag */
54 #define CB_TAG_LEN (sizeof(((struct iucv_message *) 0)->tag))
55 #define CB_TRGCLS(skb) ((skb)->cb + CB_TAG_LEN) /* iucv msg target class */
56 #define CB_TRGCLS_LEN (TRGCLS_SIZE)
58 #define __iucv_sock_wait(sk, condition, timeo, ret) \
59 do { \
60 DEFINE_WAIT(__wait); \
61 long __timeo = timeo; \
62 ret = 0; \
63 prepare_to_wait(sk_sleep(sk), &__wait, TASK_INTERRUPTIBLE); \
64 while (!(condition)) { \
65 if (!__timeo) { \
66 ret = -EAGAIN; \
67 break; \
68 } \
69 if (signal_pending(current)) { \
70 ret = sock_intr_errno(__timeo); \
71 break; \
72 } \
73 release_sock(sk); \
74 __timeo = schedule_timeout(__timeo); \
75 lock_sock(sk); \
76 ret = sock_error(sk); \
77 if (ret) \
78 break; \
79 } \
80 finish_wait(sk_sleep(sk), &__wait); \
81 } while (0)
83 #define iucv_sock_wait(sk, condition, timeo) \
84 ({ \
85 int __ret = 0; \
86 if (!(condition)) \
87 __iucv_sock_wait(sk, condition, timeo, __ret); \
88 __ret; \
91 static void iucv_sock_kill(struct sock *sk);
92 static void iucv_sock_close(struct sock *sk);
94 static int afiucv_hs_rcv(struct sk_buff *skb, struct net_device *dev,
95 struct packet_type *pt, struct net_device *orig_dev);
96 static int afiucv_hs_send(struct iucv_message *imsg, struct sock *sock,
97 struct sk_buff *skb, u8 flags);
98 static void afiucv_hs_callback_txnotify(struct sk_buff *, enum iucv_tx_notify);
100 /* Call Back functions */
101 static void iucv_callback_rx(struct iucv_path *, struct iucv_message *);
102 static void iucv_callback_txdone(struct iucv_path *, struct iucv_message *);
103 static void iucv_callback_connack(struct iucv_path *, u8 ipuser[16]);
104 static int iucv_callback_connreq(struct iucv_path *, u8 ipvmid[8],
105 u8 ipuser[16]);
106 static void iucv_callback_connrej(struct iucv_path *, u8 ipuser[16]);
107 static void iucv_callback_shutdown(struct iucv_path *, u8 ipuser[16]);
109 static struct iucv_sock_list iucv_sk_list = {
110 .lock = __RW_LOCK_UNLOCKED(iucv_sk_list.lock),
111 .autobind_name = ATOMIC_INIT(0)
114 static struct iucv_handler af_iucv_handler = {
115 .path_pending = iucv_callback_connreq,
116 .path_complete = iucv_callback_connack,
117 .path_severed = iucv_callback_connrej,
118 .message_pending = iucv_callback_rx,
119 .message_complete = iucv_callback_txdone,
120 .path_quiesced = iucv_callback_shutdown,
123 static inline void high_nmcpy(unsigned char *dst, char *src)
125 memcpy(dst, src, 8);
128 static inline void low_nmcpy(unsigned char *dst, char *src)
130 memcpy(&dst[8], src, 8);
133 static int afiucv_pm_prepare(struct device *dev)
135 #ifdef CONFIG_PM_DEBUG
136 printk(KERN_WARNING "afiucv_pm_prepare\n");
137 #endif
138 return 0;
141 static void afiucv_pm_complete(struct device *dev)
143 #ifdef CONFIG_PM_DEBUG
144 printk(KERN_WARNING "afiucv_pm_complete\n");
145 #endif
149 * afiucv_pm_freeze() - Freeze PM callback
150 * @dev: AFIUCV dummy device
152 * Sever all established IUCV communication pathes
154 static int afiucv_pm_freeze(struct device *dev)
156 struct iucv_sock *iucv;
157 struct sock *sk;
158 struct hlist_node *node;
159 int err = 0;
161 #ifdef CONFIG_PM_DEBUG
162 printk(KERN_WARNING "afiucv_pm_freeze\n");
163 #endif
164 read_lock(&iucv_sk_list.lock);
165 sk_for_each(sk, node, &iucv_sk_list.head) {
166 iucv = iucv_sk(sk);
167 skb_queue_purge(&iucv->send_skb_q);
168 skb_queue_purge(&iucv->backlog_skb_q);
169 switch (sk->sk_state) {
170 case IUCV_SEVERED:
171 case IUCV_DISCONN:
172 case IUCV_CLOSING:
173 case IUCV_CONNECTED:
174 if (iucv->path) {
175 err = pr_iucv->path_sever(iucv->path, NULL);
176 iucv_path_free(iucv->path);
177 iucv->path = NULL;
179 break;
180 case IUCV_OPEN:
181 case IUCV_BOUND:
182 case IUCV_LISTEN:
183 case IUCV_CLOSED:
184 default:
185 break;
188 read_unlock(&iucv_sk_list.lock);
189 return err;
193 * afiucv_pm_restore_thaw() - Thaw and restore PM callback
194 * @dev: AFIUCV dummy device
196 * socket clean up after freeze
198 static int afiucv_pm_restore_thaw(struct device *dev)
200 struct sock *sk;
201 struct hlist_node *node;
203 #ifdef CONFIG_PM_DEBUG
204 printk(KERN_WARNING "afiucv_pm_restore_thaw\n");
205 #endif
206 read_lock(&iucv_sk_list.lock);
207 sk_for_each(sk, node, &iucv_sk_list.head) {
208 switch (sk->sk_state) {
209 case IUCV_CONNECTED:
210 sk->sk_err = EPIPE;
211 sk->sk_state = IUCV_DISCONN;
212 sk->sk_state_change(sk);
213 break;
214 case IUCV_DISCONN:
215 case IUCV_SEVERED:
216 case IUCV_CLOSING:
217 case IUCV_LISTEN:
218 case IUCV_BOUND:
219 case IUCV_OPEN:
220 default:
221 break;
224 read_unlock(&iucv_sk_list.lock);
225 return 0;
228 static const struct dev_pm_ops afiucv_pm_ops = {
229 .prepare = afiucv_pm_prepare,
230 .complete = afiucv_pm_complete,
231 .freeze = afiucv_pm_freeze,
232 .thaw = afiucv_pm_restore_thaw,
233 .restore = afiucv_pm_restore_thaw,
236 static struct device_driver af_iucv_driver = {
237 .owner = THIS_MODULE,
238 .name = "afiucv",
239 .bus = NULL,
240 .pm = &afiucv_pm_ops,
243 /* dummy device used as trigger for PM functions */
244 static struct device *af_iucv_dev;
247 * iucv_msg_length() - Returns the length of an iucv message.
248 * @msg: Pointer to struct iucv_message, MUST NOT be NULL
250 * The function returns the length of the specified iucv message @msg of data
251 * stored in a buffer and of data stored in the parameter list (PRMDATA).
253 * For IUCV_IPRMDATA, AF_IUCV uses the following convention to transport socket
254 * data:
255 * PRMDATA[0..6] socket data (max 7 bytes);
256 * PRMDATA[7] socket data length value (len is 0xff - PRMDATA[7])
258 * The socket data length is computed by subtracting the socket data length
259 * value from 0xFF.
260 * If the socket data len is greater 7, then PRMDATA can be used for special
261 * notifications (see iucv_sock_shutdown); and further,
262 * if the socket data len is > 7, the function returns 8.
264 * Use this function to allocate socket buffers to store iucv message data.
266 static inline size_t iucv_msg_length(struct iucv_message *msg)
268 size_t datalen;
270 if (msg->flags & IUCV_IPRMDATA) {
271 datalen = 0xff - msg->rmmsg[7];
272 return (datalen < 8) ? datalen : 8;
274 return msg->length;
278 * iucv_sock_in_state() - check for specific states
279 * @sk: sock structure
280 * @state: first iucv sk state
281 * @state: second iucv sk state
283 * Returns true if the socket in either in the first or second state.
285 static int iucv_sock_in_state(struct sock *sk, int state, int state2)
287 return (sk->sk_state == state || sk->sk_state == state2);
291 * iucv_below_msglim() - function to check if messages can be sent
292 * @sk: sock structure
294 * Returns true if the send queue length is lower than the message limit.
295 * Always returns true if the socket is not connected (no iucv path for
296 * checking the message limit).
298 static inline int iucv_below_msglim(struct sock *sk)
300 struct iucv_sock *iucv = iucv_sk(sk);
302 if (sk->sk_state != IUCV_CONNECTED)
303 return 1;
304 if (iucv->transport == AF_IUCV_TRANS_IUCV)
305 return (skb_queue_len(&iucv->send_skb_q) < iucv->path->msglim);
306 else
307 return ((atomic_read(&iucv->msg_sent) < iucv->msglimit_peer) &&
308 (atomic_read(&iucv->pendings) <= 0));
312 * iucv_sock_wake_msglim() - Wake up thread waiting on msg limit
314 static void iucv_sock_wake_msglim(struct sock *sk)
316 struct socket_wq *wq;
318 rcu_read_lock();
319 wq = rcu_dereference(sk->sk_wq);
320 if (wq_has_sleeper(wq))
321 wake_up_interruptible_all(&wq->wait);
322 sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
323 rcu_read_unlock();
327 * afiucv_hs_send() - send a message through HiperSockets transport
329 static int afiucv_hs_send(struct iucv_message *imsg, struct sock *sock,
330 struct sk_buff *skb, u8 flags)
332 struct net *net = sock_net(sock);
333 struct iucv_sock *iucv = iucv_sk(sock);
334 struct af_iucv_trans_hdr *phs_hdr;
335 struct sk_buff *nskb;
336 int err, confirm_recv = 0;
338 memset(skb->head, 0, ETH_HLEN);
339 phs_hdr = (struct af_iucv_trans_hdr *)skb_push(skb,
340 sizeof(struct af_iucv_trans_hdr));
341 skb_reset_mac_header(skb);
342 skb_reset_network_header(skb);
343 skb_push(skb, ETH_HLEN);
344 skb_reset_mac_header(skb);
345 memset(phs_hdr, 0, sizeof(struct af_iucv_trans_hdr));
347 phs_hdr->magic = ETH_P_AF_IUCV;
348 phs_hdr->version = 1;
349 phs_hdr->flags = flags;
350 if (flags == AF_IUCV_FLAG_SYN)
351 phs_hdr->window = iucv->msglimit;
352 else if ((flags == AF_IUCV_FLAG_WIN) || !flags) {
353 confirm_recv = atomic_read(&iucv->msg_recv);
354 phs_hdr->window = confirm_recv;
355 if (confirm_recv)
356 phs_hdr->flags = phs_hdr->flags | AF_IUCV_FLAG_WIN;
358 memcpy(phs_hdr->destUserID, iucv->dst_user_id, 8);
359 memcpy(phs_hdr->destAppName, iucv->dst_name, 8);
360 memcpy(phs_hdr->srcUserID, iucv->src_user_id, 8);
361 memcpy(phs_hdr->srcAppName, iucv->src_name, 8);
362 ASCEBC(phs_hdr->destUserID, sizeof(phs_hdr->destUserID));
363 ASCEBC(phs_hdr->destAppName, sizeof(phs_hdr->destAppName));
364 ASCEBC(phs_hdr->srcUserID, sizeof(phs_hdr->srcUserID));
365 ASCEBC(phs_hdr->srcAppName, sizeof(phs_hdr->srcAppName));
366 if (imsg)
367 memcpy(&phs_hdr->iucv_hdr, imsg, sizeof(struct iucv_message));
369 rcu_read_lock();
370 skb->dev = dev_get_by_index_rcu(net, sock->sk_bound_dev_if);
371 rcu_read_unlock();
372 if (!skb->dev)
373 return -ENODEV;
374 if (!(skb->dev->flags & IFF_UP))
375 return -ENETDOWN;
376 if (skb->len > skb->dev->mtu) {
377 if (sock->sk_type == SOCK_SEQPACKET)
378 return -EMSGSIZE;
379 else
380 skb_trim(skb, skb->dev->mtu);
382 skb->protocol = ETH_P_AF_IUCV;
383 skb_shinfo(skb)->tx_flags |= SKBTX_DRV_NEEDS_SK_REF;
384 nskb = skb_clone(skb, GFP_ATOMIC);
385 if (!nskb)
386 return -ENOMEM;
387 skb_queue_tail(&iucv->send_skb_q, nskb);
388 err = dev_queue_xmit(skb);
389 if (err) {
390 skb_unlink(nskb, &iucv->send_skb_q);
391 kfree_skb(nskb);
392 } else {
393 atomic_sub(confirm_recv, &iucv->msg_recv);
394 WARN_ON(atomic_read(&iucv->msg_recv) < 0);
396 return err;
399 /* Timers */
400 static void iucv_sock_timeout(unsigned long arg)
402 struct sock *sk = (struct sock *)arg;
404 bh_lock_sock(sk);
405 sk->sk_err = ETIMEDOUT;
406 sk->sk_state_change(sk);
407 bh_unlock_sock(sk);
409 iucv_sock_kill(sk);
410 sock_put(sk);
413 static void iucv_sock_clear_timer(struct sock *sk)
415 sk_stop_timer(sk, &sk->sk_timer);
418 static struct sock *__iucv_get_sock_by_name(char *nm)
420 struct sock *sk;
421 struct hlist_node *node;
423 sk_for_each(sk, node, &iucv_sk_list.head)
424 if (!memcmp(&iucv_sk(sk)->src_name, nm, 8))
425 return sk;
427 return NULL;
430 static void iucv_sock_destruct(struct sock *sk)
432 skb_queue_purge(&sk->sk_receive_queue);
433 skb_queue_purge(&sk->sk_write_queue);
436 /* Cleanup Listen */
437 static void iucv_sock_cleanup_listen(struct sock *parent)
439 struct sock *sk;
441 /* Close non-accepted connections */
442 while ((sk = iucv_accept_dequeue(parent, NULL))) {
443 iucv_sock_close(sk);
444 iucv_sock_kill(sk);
447 parent->sk_state = IUCV_CLOSED;
450 /* Kill socket (only if zapped and orphaned) */
451 static void iucv_sock_kill(struct sock *sk)
453 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
454 return;
456 iucv_sock_unlink(&iucv_sk_list, sk);
457 sock_set_flag(sk, SOCK_DEAD);
458 sock_put(sk);
461 /* Close an IUCV socket */
462 static void iucv_sock_close(struct sock *sk)
464 unsigned char user_data[16];
465 struct iucv_sock *iucv = iucv_sk(sk);
466 unsigned long timeo;
467 int err, blen;
468 struct sk_buff *skb;
470 iucv_sock_clear_timer(sk);
471 lock_sock(sk);
473 switch (sk->sk_state) {
474 case IUCV_LISTEN:
475 iucv_sock_cleanup_listen(sk);
476 break;
478 case IUCV_CONNECTED:
479 if (iucv->transport == AF_IUCV_TRANS_HIPER) {
480 /* send fin */
481 blen = sizeof(struct af_iucv_trans_hdr) + ETH_HLEN;
482 skb = sock_alloc_send_skb(sk, blen, 1, &err);
483 if (skb) {
484 skb_reserve(skb,
485 sizeof(struct af_iucv_trans_hdr) +
486 ETH_HLEN);
487 err = afiucv_hs_send(NULL, sk, skb,
488 AF_IUCV_FLAG_FIN);
490 sk->sk_state = IUCV_DISCONN;
491 sk->sk_state_change(sk);
493 case IUCV_DISCONN:
494 sk->sk_state = IUCV_CLOSING;
495 sk->sk_state_change(sk);
497 if (!skb_queue_empty(&iucv->send_skb_q)) {
498 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
499 timeo = sk->sk_lingertime;
500 else
501 timeo = IUCV_DISCONN_TIMEOUT;
502 iucv_sock_wait(sk,
503 iucv_sock_in_state(sk, IUCV_CLOSED, 0),
504 timeo);
507 case IUCV_CLOSING: /* fall through */
508 sk->sk_state = IUCV_CLOSED;
509 sk->sk_state_change(sk);
511 if (iucv->path) {
512 low_nmcpy(user_data, iucv->src_name);
513 high_nmcpy(user_data, iucv->dst_name);
514 ASCEBC(user_data, sizeof(user_data));
515 pr_iucv->path_sever(iucv->path, user_data);
516 iucv_path_free(iucv->path);
517 iucv->path = NULL;
520 sk->sk_err = ECONNRESET;
521 sk->sk_state_change(sk);
523 skb_queue_purge(&iucv->send_skb_q);
524 skb_queue_purge(&iucv->backlog_skb_q);
525 break;
527 default:
528 /* nothing to do here */
529 break;
532 /* mark socket for deletion by iucv_sock_kill() */
533 sock_set_flag(sk, SOCK_ZAPPED);
535 release_sock(sk);
538 static void iucv_sock_init(struct sock *sk, struct sock *parent)
540 if (parent)
541 sk->sk_type = parent->sk_type;
544 static struct sock *iucv_sock_alloc(struct socket *sock, int proto, gfp_t prio)
546 struct sock *sk;
547 struct iucv_sock *iucv;
549 sk = sk_alloc(&init_net, PF_IUCV, prio, &iucv_proto);
550 if (!sk)
551 return NULL;
552 iucv = iucv_sk(sk);
554 sock_init_data(sock, sk);
555 INIT_LIST_HEAD(&iucv->accept_q);
556 spin_lock_init(&iucv->accept_q_lock);
557 skb_queue_head_init(&iucv->send_skb_q);
558 INIT_LIST_HEAD(&iucv->message_q.list);
559 spin_lock_init(&iucv->message_q.lock);
560 skb_queue_head_init(&iucv->backlog_skb_q);
561 iucv->send_tag = 0;
562 atomic_set(&iucv->pendings, 0);
563 iucv->flags = 0;
564 iucv->msglimit = 0;
565 atomic_set(&iucv->msg_sent, 0);
566 atomic_set(&iucv->msg_recv, 0);
567 iucv->path = NULL;
568 iucv->sk_txnotify = afiucv_hs_callback_txnotify;
569 memset(&iucv->src_user_id , 0, 32);
570 if (pr_iucv)
571 iucv->transport = AF_IUCV_TRANS_IUCV;
572 else
573 iucv->transport = AF_IUCV_TRANS_HIPER;
575 sk->sk_destruct = iucv_sock_destruct;
576 sk->sk_sndtimeo = IUCV_CONN_TIMEOUT;
577 sk->sk_allocation = GFP_DMA;
579 sock_reset_flag(sk, SOCK_ZAPPED);
581 sk->sk_protocol = proto;
582 sk->sk_state = IUCV_OPEN;
584 setup_timer(&sk->sk_timer, iucv_sock_timeout, (unsigned long)sk);
586 iucv_sock_link(&iucv_sk_list, sk);
587 return sk;
590 /* Create an IUCV socket */
591 static int iucv_sock_create(struct net *net, struct socket *sock, int protocol,
592 int kern)
594 struct sock *sk;
596 if (protocol && protocol != PF_IUCV)
597 return -EPROTONOSUPPORT;
599 sock->state = SS_UNCONNECTED;
601 switch (sock->type) {
602 case SOCK_STREAM:
603 sock->ops = &iucv_sock_ops;
604 break;
605 case SOCK_SEQPACKET:
606 /* currently, proto ops can handle both sk types */
607 sock->ops = &iucv_sock_ops;
608 break;
609 default:
610 return -ESOCKTNOSUPPORT;
613 sk = iucv_sock_alloc(sock, protocol, GFP_KERNEL);
614 if (!sk)
615 return -ENOMEM;
617 iucv_sock_init(sk, NULL);
619 return 0;
622 void iucv_sock_link(struct iucv_sock_list *l, struct sock *sk)
624 write_lock_bh(&l->lock);
625 sk_add_node(sk, &l->head);
626 write_unlock_bh(&l->lock);
629 void iucv_sock_unlink(struct iucv_sock_list *l, struct sock *sk)
631 write_lock_bh(&l->lock);
632 sk_del_node_init(sk);
633 write_unlock_bh(&l->lock);
636 void iucv_accept_enqueue(struct sock *parent, struct sock *sk)
638 unsigned long flags;
639 struct iucv_sock *par = iucv_sk(parent);
641 sock_hold(sk);
642 spin_lock_irqsave(&par->accept_q_lock, flags);
643 list_add_tail(&iucv_sk(sk)->accept_q, &par->accept_q);
644 spin_unlock_irqrestore(&par->accept_q_lock, flags);
645 iucv_sk(sk)->parent = parent;
646 sk_acceptq_added(parent);
649 void iucv_accept_unlink(struct sock *sk)
651 unsigned long flags;
652 struct iucv_sock *par = iucv_sk(iucv_sk(sk)->parent);
654 spin_lock_irqsave(&par->accept_q_lock, flags);
655 list_del_init(&iucv_sk(sk)->accept_q);
656 spin_unlock_irqrestore(&par->accept_q_lock, flags);
657 sk_acceptq_removed(iucv_sk(sk)->parent);
658 iucv_sk(sk)->parent = NULL;
659 sock_put(sk);
662 struct sock *iucv_accept_dequeue(struct sock *parent, struct socket *newsock)
664 struct iucv_sock *isk, *n;
665 struct sock *sk;
667 list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) {
668 sk = (struct sock *) isk;
669 lock_sock(sk);
671 if (sk->sk_state == IUCV_CLOSED) {
672 iucv_accept_unlink(sk);
673 release_sock(sk);
674 continue;
677 if (sk->sk_state == IUCV_CONNECTED ||
678 sk->sk_state == IUCV_SEVERED ||
679 sk->sk_state == IUCV_DISCONN || /* due to PM restore */
680 !newsock) {
681 iucv_accept_unlink(sk);
682 if (newsock)
683 sock_graft(sk, newsock);
685 if (sk->sk_state == IUCV_SEVERED)
686 sk->sk_state = IUCV_DISCONN;
688 release_sock(sk);
689 return sk;
692 release_sock(sk);
694 return NULL;
697 /* Bind an unbound socket */
698 static int iucv_sock_bind(struct socket *sock, struct sockaddr *addr,
699 int addr_len)
701 struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
702 struct sock *sk = sock->sk;
703 struct iucv_sock *iucv;
704 int err = 0;
705 struct net_device *dev;
706 char uid[9];
708 /* Verify the input sockaddr */
709 if (!addr || addr->sa_family != AF_IUCV)
710 return -EINVAL;
712 lock_sock(sk);
713 if (sk->sk_state != IUCV_OPEN) {
714 err = -EBADFD;
715 goto done;
718 write_lock_bh(&iucv_sk_list.lock);
720 iucv = iucv_sk(sk);
721 if (__iucv_get_sock_by_name(sa->siucv_name)) {
722 err = -EADDRINUSE;
723 goto done_unlock;
725 if (iucv->path)
726 goto done_unlock;
728 /* Bind the socket */
730 if (pr_iucv)
731 if (!memcmp(sa->siucv_user_id, iucv_userid, 8))
732 goto vm_bind; /* VM IUCV transport */
734 /* try hiper transport */
735 memcpy(uid, sa->siucv_user_id, sizeof(uid));
736 ASCEBC(uid, 8);
737 rcu_read_lock();
738 for_each_netdev_rcu(&init_net, dev) {
739 if (!memcmp(dev->perm_addr, uid, 8)) {
740 memcpy(iucv->src_name, sa->siucv_name, 8);
741 memcpy(iucv->src_user_id, sa->siucv_user_id, 8);
742 sock->sk->sk_bound_dev_if = dev->ifindex;
743 sk->sk_state = IUCV_BOUND;
744 iucv->transport = AF_IUCV_TRANS_HIPER;
745 if (!iucv->msglimit)
746 iucv->msglimit = IUCV_HIPER_MSGLIM_DEFAULT;
747 rcu_read_unlock();
748 goto done_unlock;
751 rcu_read_unlock();
752 vm_bind:
753 if (pr_iucv) {
754 /* use local userid for backward compat */
755 memcpy(iucv->src_name, sa->siucv_name, 8);
756 memcpy(iucv->src_user_id, iucv_userid, 8);
757 sk->sk_state = IUCV_BOUND;
758 iucv->transport = AF_IUCV_TRANS_IUCV;
759 if (!iucv->msglimit)
760 iucv->msglimit = IUCV_QUEUELEN_DEFAULT;
761 goto done_unlock;
763 /* found no dev to bind */
764 err = -ENODEV;
765 done_unlock:
766 /* Release the socket list lock */
767 write_unlock_bh(&iucv_sk_list.lock);
768 done:
769 release_sock(sk);
770 return err;
773 /* Automatically bind an unbound socket */
774 static int iucv_sock_autobind(struct sock *sk)
776 struct iucv_sock *iucv = iucv_sk(sk);
777 char query_buffer[80];
778 char name[12];
779 int err = 0;
781 /* Set the userid and name */
782 cpcmd("QUERY USERID", query_buffer, sizeof(query_buffer), &err);
783 if (unlikely(err))
784 return -EPROTO;
786 memcpy(iucv->src_user_id, query_buffer, 8);
788 write_lock_bh(&iucv_sk_list.lock);
790 sprintf(name, "%08x", atomic_inc_return(&iucv_sk_list.autobind_name));
791 while (__iucv_get_sock_by_name(name)) {
792 sprintf(name, "%08x",
793 atomic_inc_return(&iucv_sk_list.autobind_name));
796 write_unlock_bh(&iucv_sk_list.lock);
798 memcpy(&iucv->src_name, name, 8);
800 if (!iucv->msglimit)
801 iucv->msglimit = IUCV_QUEUELEN_DEFAULT;
803 return err;
806 static int afiucv_hs_connect(struct socket *sock)
808 struct sock *sk = sock->sk;
809 struct sk_buff *skb;
810 int blen = sizeof(struct af_iucv_trans_hdr) + ETH_HLEN;
811 int err = 0;
813 /* send syn */
814 skb = sock_alloc_send_skb(sk, blen, 1, &err);
815 if (!skb) {
816 err = -ENOMEM;
817 goto done;
819 skb->dev = NULL;
820 skb_reserve(skb, blen);
821 err = afiucv_hs_send(NULL, sk, skb, AF_IUCV_FLAG_SYN);
822 done:
823 return err;
826 static int afiucv_path_connect(struct socket *sock, struct sockaddr *addr)
828 struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
829 struct sock *sk = sock->sk;
830 struct iucv_sock *iucv = iucv_sk(sk);
831 unsigned char user_data[16];
832 int err;
834 high_nmcpy(user_data, sa->siucv_name);
835 low_nmcpy(user_data, iucv->src_name);
836 ASCEBC(user_data, sizeof(user_data));
838 /* Create path. */
839 iucv->path = iucv_path_alloc(iucv->msglimit,
840 IUCV_IPRMDATA, GFP_KERNEL);
841 if (!iucv->path) {
842 err = -ENOMEM;
843 goto done;
845 err = pr_iucv->path_connect(iucv->path, &af_iucv_handler,
846 sa->siucv_user_id, NULL, user_data,
847 sk);
848 if (err) {
849 iucv_path_free(iucv->path);
850 iucv->path = NULL;
851 switch (err) {
852 case 0x0b: /* Target communicator is not logged on */
853 err = -ENETUNREACH;
854 break;
855 case 0x0d: /* Max connections for this guest exceeded */
856 case 0x0e: /* Max connections for target guest exceeded */
857 err = -EAGAIN;
858 break;
859 case 0x0f: /* Missing IUCV authorization */
860 err = -EACCES;
861 break;
862 default:
863 err = -ECONNREFUSED;
864 break;
867 done:
868 return err;
871 /* Connect an unconnected socket */
872 static int iucv_sock_connect(struct socket *sock, struct sockaddr *addr,
873 int alen, int flags)
875 struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
876 struct sock *sk = sock->sk;
877 struct iucv_sock *iucv = iucv_sk(sk);
878 int err;
880 if (addr->sa_family != AF_IUCV || alen < sizeof(struct sockaddr_iucv))
881 return -EINVAL;
883 if (sk->sk_state != IUCV_OPEN && sk->sk_state != IUCV_BOUND)
884 return -EBADFD;
886 if (sk->sk_state == IUCV_OPEN &&
887 iucv->transport == AF_IUCV_TRANS_HIPER)
888 return -EBADFD; /* explicit bind required */
890 if (sk->sk_type != SOCK_STREAM && sk->sk_type != SOCK_SEQPACKET)
891 return -EINVAL;
893 if (sk->sk_state == IUCV_OPEN) {
894 err = iucv_sock_autobind(sk);
895 if (unlikely(err))
896 return err;
899 lock_sock(sk);
901 /* Set the destination information */
902 memcpy(iucv->dst_user_id, sa->siucv_user_id, 8);
903 memcpy(iucv->dst_name, sa->siucv_name, 8);
905 if (iucv->transport == AF_IUCV_TRANS_HIPER)
906 err = afiucv_hs_connect(sock);
907 else
908 err = afiucv_path_connect(sock, addr);
909 if (err)
910 goto done;
912 if (sk->sk_state != IUCV_CONNECTED)
913 err = iucv_sock_wait(sk, iucv_sock_in_state(sk, IUCV_CONNECTED,
914 IUCV_DISCONN),
915 sock_sndtimeo(sk, flags & O_NONBLOCK));
917 if (sk->sk_state == IUCV_DISCONN || sk->sk_state == IUCV_CLOSED)
918 err = -ECONNREFUSED;
920 if (err && iucv->transport == AF_IUCV_TRANS_IUCV) {
921 pr_iucv->path_sever(iucv->path, NULL);
922 iucv_path_free(iucv->path);
923 iucv->path = NULL;
926 done:
927 release_sock(sk);
928 return err;
931 /* Move a socket into listening state. */
932 static int iucv_sock_listen(struct socket *sock, int backlog)
934 struct sock *sk = sock->sk;
935 int err;
937 lock_sock(sk);
939 err = -EINVAL;
940 if (sk->sk_state != IUCV_BOUND)
941 goto done;
943 if (sock->type != SOCK_STREAM && sock->type != SOCK_SEQPACKET)
944 goto done;
946 sk->sk_max_ack_backlog = backlog;
947 sk->sk_ack_backlog = 0;
948 sk->sk_state = IUCV_LISTEN;
949 err = 0;
951 done:
952 release_sock(sk);
953 return err;
956 /* Accept a pending connection */
957 static int iucv_sock_accept(struct socket *sock, struct socket *newsock,
958 int flags)
960 DECLARE_WAITQUEUE(wait, current);
961 struct sock *sk = sock->sk, *nsk;
962 long timeo;
963 int err = 0;
965 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
967 if (sk->sk_state != IUCV_LISTEN) {
968 err = -EBADFD;
969 goto done;
972 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
974 /* Wait for an incoming connection */
975 add_wait_queue_exclusive(sk_sleep(sk), &wait);
976 while (!(nsk = iucv_accept_dequeue(sk, newsock))) {
977 set_current_state(TASK_INTERRUPTIBLE);
978 if (!timeo) {
979 err = -EAGAIN;
980 break;
983 release_sock(sk);
984 timeo = schedule_timeout(timeo);
985 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
987 if (sk->sk_state != IUCV_LISTEN) {
988 err = -EBADFD;
989 break;
992 if (signal_pending(current)) {
993 err = sock_intr_errno(timeo);
994 break;
998 set_current_state(TASK_RUNNING);
999 remove_wait_queue(sk_sleep(sk), &wait);
1001 if (err)
1002 goto done;
1004 newsock->state = SS_CONNECTED;
1006 done:
1007 release_sock(sk);
1008 return err;
1011 static int iucv_sock_getname(struct socket *sock, struct sockaddr *addr,
1012 int *len, int peer)
1014 struct sockaddr_iucv *siucv = (struct sockaddr_iucv *) addr;
1015 struct sock *sk = sock->sk;
1016 struct iucv_sock *iucv = iucv_sk(sk);
1018 addr->sa_family = AF_IUCV;
1019 *len = sizeof(struct sockaddr_iucv);
1021 if (peer) {
1022 memcpy(siucv->siucv_user_id, iucv->dst_user_id, 8);
1023 memcpy(siucv->siucv_name, iucv->dst_name, 8);
1024 } else {
1025 memcpy(siucv->siucv_user_id, iucv->src_user_id, 8);
1026 memcpy(siucv->siucv_name, iucv->src_name, 8);
1028 memset(&siucv->siucv_port, 0, sizeof(siucv->siucv_port));
1029 memset(&siucv->siucv_addr, 0, sizeof(siucv->siucv_addr));
1030 memset(&siucv->siucv_nodeid, 0, sizeof(siucv->siucv_nodeid));
1032 return 0;
1036 * iucv_send_iprm() - Send socket data in parameter list of an iucv message.
1037 * @path: IUCV path
1038 * @msg: Pointer to a struct iucv_message
1039 * @skb: The socket data to send, skb->len MUST BE <= 7
1041 * Send the socket data in the parameter list in the iucv message
1042 * (IUCV_IPRMDATA). The socket data is stored at index 0 to 6 in the parameter
1043 * list and the socket data len at index 7 (last byte).
1044 * See also iucv_msg_length().
1046 * Returns the error code from the iucv_message_send() call.
1048 static int iucv_send_iprm(struct iucv_path *path, struct iucv_message *msg,
1049 struct sk_buff *skb)
1051 u8 prmdata[8];
1053 memcpy(prmdata, (void *) skb->data, skb->len);
1054 prmdata[7] = 0xff - (u8) skb->len;
1055 return pr_iucv->message_send(path, msg, IUCV_IPRMDATA, 0,
1056 (void *) prmdata, 8);
1059 static int iucv_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
1060 struct msghdr *msg, size_t len)
1062 struct sock *sk = sock->sk;
1063 struct iucv_sock *iucv = iucv_sk(sk);
1064 struct sk_buff *skb;
1065 struct iucv_message txmsg;
1066 struct cmsghdr *cmsg;
1067 int cmsg_done;
1068 long timeo;
1069 char user_id[9];
1070 char appl_id[9];
1071 int err;
1072 int noblock = msg->msg_flags & MSG_DONTWAIT;
1074 err = sock_error(sk);
1075 if (err)
1076 return err;
1078 if (msg->msg_flags & MSG_OOB)
1079 return -EOPNOTSUPP;
1081 /* SOCK_SEQPACKET: we do not support segmented records */
1082 if (sk->sk_type == SOCK_SEQPACKET && !(msg->msg_flags & MSG_EOR))
1083 return -EOPNOTSUPP;
1085 lock_sock(sk);
1087 if (sk->sk_shutdown & SEND_SHUTDOWN) {
1088 err = -EPIPE;
1089 goto out;
1092 /* Return if the socket is not in connected state */
1093 if (sk->sk_state != IUCV_CONNECTED) {
1094 err = -ENOTCONN;
1095 goto out;
1098 /* initialize defaults */
1099 cmsg_done = 0; /* check for duplicate headers */
1100 txmsg.class = 0;
1102 /* iterate over control messages */
1103 for (cmsg = CMSG_FIRSTHDR(msg); cmsg;
1104 cmsg = CMSG_NXTHDR(msg, cmsg)) {
1106 if (!CMSG_OK(msg, cmsg)) {
1107 err = -EINVAL;
1108 goto out;
1111 if (cmsg->cmsg_level != SOL_IUCV)
1112 continue;
1114 if (cmsg->cmsg_type & cmsg_done) {
1115 err = -EINVAL;
1116 goto out;
1118 cmsg_done |= cmsg->cmsg_type;
1120 switch (cmsg->cmsg_type) {
1121 case SCM_IUCV_TRGCLS:
1122 if (cmsg->cmsg_len != CMSG_LEN(TRGCLS_SIZE)) {
1123 err = -EINVAL;
1124 goto out;
1127 /* set iucv message target class */
1128 memcpy(&txmsg.class,
1129 (void *) CMSG_DATA(cmsg), TRGCLS_SIZE);
1131 break;
1133 default:
1134 err = -EINVAL;
1135 goto out;
1136 break;
1140 /* allocate one skb for each iucv message:
1141 * this is fine for SOCK_SEQPACKET (unless we want to support
1142 * segmented records using the MSG_EOR flag), but
1143 * for SOCK_STREAM we might want to improve it in future */
1144 if (iucv->transport == AF_IUCV_TRANS_HIPER)
1145 skb = sock_alloc_send_skb(sk,
1146 len + sizeof(struct af_iucv_trans_hdr) + ETH_HLEN,
1147 noblock, &err);
1148 else
1149 skb = sock_alloc_send_skb(sk, len, noblock, &err);
1150 if (!skb)
1151 goto out;
1152 if (iucv->transport == AF_IUCV_TRANS_HIPER)
1153 skb_reserve(skb, sizeof(struct af_iucv_trans_hdr) + ETH_HLEN);
1154 if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
1155 err = -EFAULT;
1156 goto fail;
1159 /* wait if outstanding messages for iucv path has reached */
1160 timeo = sock_sndtimeo(sk, noblock);
1161 err = iucv_sock_wait(sk, iucv_below_msglim(sk), timeo);
1162 if (err)
1163 goto fail;
1165 /* return -ECONNRESET if the socket is no longer connected */
1166 if (sk->sk_state != IUCV_CONNECTED) {
1167 err = -ECONNRESET;
1168 goto fail;
1171 /* increment and save iucv message tag for msg_completion cbk */
1172 txmsg.tag = iucv->send_tag++;
1173 memcpy(CB_TAG(skb), &txmsg.tag, CB_TAG_LEN);
1174 if (iucv->transport == AF_IUCV_TRANS_HIPER) {
1175 atomic_inc(&iucv->msg_sent);
1176 err = afiucv_hs_send(&txmsg, sk, skb, 0);
1177 if (err) {
1178 atomic_dec(&iucv->msg_sent);
1179 goto fail;
1181 goto release;
1183 skb_queue_tail(&iucv->send_skb_q, skb);
1185 if (((iucv->path->flags & IUCV_IPRMDATA) & iucv->flags)
1186 && skb->len <= 7) {
1187 err = iucv_send_iprm(iucv->path, &txmsg, skb);
1189 /* on success: there is no message_complete callback
1190 * for an IPRMDATA msg; remove skb from send queue */
1191 if (err == 0) {
1192 skb_unlink(skb, &iucv->send_skb_q);
1193 kfree_skb(skb);
1196 /* this error should never happen since the
1197 * IUCV_IPRMDATA path flag is set... sever path */
1198 if (err == 0x15) {
1199 pr_iucv->path_sever(iucv->path, NULL);
1200 skb_unlink(skb, &iucv->send_skb_q);
1201 err = -EPIPE;
1202 goto fail;
1204 } else
1205 err = pr_iucv->message_send(iucv->path, &txmsg, 0, 0,
1206 (void *) skb->data, skb->len);
1207 if (err) {
1208 if (err == 3) {
1209 user_id[8] = 0;
1210 memcpy(user_id, iucv->dst_user_id, 8);
1211 appl_id[8] = 0;
1212 memcpy(appl_id, iucv->dst_name, 8);
1213 pr_err("Application %s on z/VM guest %s"
1214 " exceeds message limit\n",
1215 appl_id, user_id);
1216 err = -EAGAIN;
1217 } else
1218 err = -EPIPE;
1219 skb_unlink(skb, &iucv->send_skb_q);
1220 goto fail;
1223 release:
1224 release_sock(sk);
1225 return len;
1227 fail:
1228 kfree_skb(skb);
1229 out:
1230 release_sock(sk);
1231 return err;
1234 /* iucv_fragment_skb() - Fragment a single IUCV message into multiple skb's
1236 * Locking: must be called with message_q.lock held
1238 static int iucv_fragment_skb(struct sock *sk, struct sk_buff *skb, int len)
1240 int dataleft, size, copied = 0;
1241 struct sk_buff *nskb;
1243 dataleft = len;
1244 while (dataleft) {
1245 if (dataleft >= sk->sk_rcvbuf / 4)
1246 size = sk->sk_rcvbuf / 4;
1247 else
1248 size = dataleft;
1250 nskb = alloc_skb(size, GFP_ATOMIC | GFP_DMA);
1251 if (!nskb)
1252 return -ENOMEM;
1254 /* copy target class to control buffer of new skb */
1255 memcpy(CB_TRGCLS(nskb), CB_TRGCLS(skb), CB_TRGCLS_LEN);
1257 /* copy data fragment */
1258 memcpy(nskb->data, skb->data + copied, size);
1259 copied += size;
1260 dataleft -= size;
1262 skb_reset_transport_header(nskb);
1263 skb_reset_network_header(nskb);
1264 nskb->len = size;
1266 skb_queue_tail(&iucv_sk(sk)->backlog_skb_q, nskb);
1269 return 0;
1272 /* iucv_process_message() - Receive a single outstanding IUCV message
1274 * Locking: must be called with message_q.lock held
1276 static void iucv_process_message(struct sock *sk, struct sk_buff *skb,
1277 struct iucv_path *path,
1278 struct iucv_message *msg)
1280 int rc;
1281 unsigned int len;
1283 len = iucv_msg_length(msg);
1285 /* store msg target class in the second 4 bytes of skb ctrl buffer */
1286 /* Note: the first 4 bytes are reserved for msg tag */
1287 memcpy(CB_TRGCLS(skb), &msg->class, CB_TRGCLS_LEN);
1289 /* check for special IPRM messages (e.g. iucv_sock_shutdown) */
1290 if ((msg->flags & IUCV_IPRMDATA) && len > 7) {
1291 if (memcmp(msg->rmmsg, iprm_shutdown, 8) == 0) {
1292 skb->data = NULL;
1293 skb->len = 0;
1295 } else {
1296 rc = pr_iucv->message_receive(path, msg,
1297 msg->flags & IUCV_IPRMDATA,
1298 skb->data, len, NULL);
1299 if (rc) {
1300 kfree_skb(skb);
1301 return;
1303 /* we need to fragment iucv messages for SOCK_STREAM only;
1304 * for SOCK_SEQPACKET, it is only relevant if we support
1305 * record segmentation using MSG_EOR (see also recvmsg()) */
1306 if (sk->sk_type == SOCK_STREAM &&
1307 skb->truesize >= sk->sk_rcvbuf / 4) {
1308 rc = iucv_fragment_skb(sk, skb, len);
1309 kfree_skb(skb);
1310 skb = NULL;
1311 if (rc) {
1312 pr_iucv->path_sever(path, NULL);
1313 return;
1315 skb = skb_dequeue(&iucv_sk(sk)->backlog_skb_q);
1316 } else {
1317 skb_reset_transport_header(skb);
1318 skb_reset_network_header(skb);
1319 skb->len = len;
1323 if (sock_queue_rcv_skb(sk, skb))
1324 skb_queue_head(&iucv_sk(sk)->backlog_skb_q, skb);
1327 /* iucv_process_message_q() - Process outstanding IUCV messages
1329 * Locking: must be called with message_q.lock held
1331 static void iucv_process_message_q(struct sock *sk)
1333 struct iucv_sock *iucv = iucv_sk(sk);
1334 struct sk_buff *skb;
1335 struct sock_msg_q *p, *n;
1337 list_for_each_entry_safe(p, n, &iucv->message_q.list, list) {
1338 skb = alloc_skb(iucv_msg_length(&p->msg), GFP_ATOMIC | GFP_DMA);
1339 if (!skb)
1340 break;
1341 iucv_process_message(sk, skb, p->path, &p->msg);
1342 list_del(&p->list);
1343 kfree(p);
1344 if (!skb_queue_empty(&iucv->backlog_skb_q))
1345 break;
1349 static int iucv_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
1350 struct msghdr *msg, size_t len, int flags)
1352 int noblock = flags & MSG_DONTWAIT;
1353 struct sock *sk = sock->sk;
1354 struct iucv_sock *iucv = iucv_sk(sk);
1355 unsigned int copied, rlen;
1356 struct sk_buff *skb, *rskb, *cskb, *sskb;
1357 int blen;
1358 int err = 0;
1360 if ((sk->sk_state == IUCV_DISCONN || sk->sk_state == IUCV_SEVERED) &&
1361 skb_queue_empty(&iucv->backlog_skb_q) &&
1362 skb_queue_empty(&sk->sk_receive_queue) &&
1363 list_empty(&iucv->message_q.list))
1364 return 0;
1366 if (flags & (MSG_OOB))
1367 return -EOPNOTSUPP;
1369 /* receive/dequeue next skb:
1370 * the function understands MSG_PEEK and, thus, does not dequeue skb */
1371 skb = skb_recv_datagram(sk, flags, noblock, &err);
1372 if (!skb) {
1373 if (sk->sk_shutdown & RCV_SHUTDOWN)
1374 return 0;
1375 return err;
1378 rlen = skb->len; /* real length of skb */
1379 copied = min_t(unsigned int, rlen, len);
1381 cskb = skb;
1382 if (skb_copy_datagram_iovec(cskb, 0, msg->msg_iov, copied)) {
1383 if (!(flags & MSG_PEEK))
1384 skb_queue_head(&sk->sk_receive_queue, skb);
1385 return -EFAULT;
1388 /* SOCK_SEQPACKET: set MSG_TRUNC if recv buf size is too small */
1389 if (sk->sk_type == SOCK_SEQPACKET) {
1390 if (copied < rlen)
1391 msg->msg_flags |= MSG_TRUNC;
1392 /* each iucv message contains a complete record */
1393 msg->msg_flags |= MSG_EOR;
1396 /* create control message to store iucv msg target class:
1397 * get the trgcls from the control buffer of the skb due to
1398 * fragmentation of original iucv message. */
1399 err = put_cmsg(msg, SOL_IUCV, SCM_IUCV_TRGCLS,
1400 CB_TRGCLS_LEN, CB_TRGCLS(skb));
1401 if (err) {
1402 if (!(flags & MSG_PEEK))
1403 skb_queue_head(&sk->sk_receive_queue, skb);
1404 return err;
1407 /* Mark read part of skb as used */
1408 if (!(flags & MSG_PEEK)) {
1410 /* SOCK_STREAM: re-queue skb if it contains unreceived data */
1411 if (sk->sk_type == SOCK_STREAM) {
1412 skb_pull(skb, copied);
1413 if (skb->len) {
1414 skb_queue_head(&sk->sk_receive_queue, skb);
1415 goto done;
1419 kfree_skb(skb);
1420 atomic_inc(&iucv->msg_recv);
1422 /* Queue backlog skbs */
1423 spin_lock_bh(&iucv->message_q.lock);
1424 rskb = skb_dequeue(&iucv->backlog_skb_q);
1425 while (rskb) {
1426 if (sock_queue_rcv_skb(sk, rskb)) {
1427 skb_queue_head(&iucv->backlog_skb_q,
1428 rskb);
1429 break;
1430 } else {
1431 rskb = skb_dequeue(&iucv->backlog_skb_q);
1434 if (skb_queue_empty(&iucv->backlog_skb_q)) {
1435 if (!list_empty(&iucv->message_q.list))
1436 iucv_process_message_q(sk);
1437 if (atomic_read(&iucv->msg_recv) >=
1438 iucv->msglimit / 2) {
1439 /* send WIN to peer */
1440 blen = sizeof(struct af_iucv_trans_hdr) +
1441 ETH_HLEN;
1442 sskb = sock_alloc_send_skb(sk, blen, 1, &err);
1443 if (sskb) {
1444 skb_reserve(sskb,
1445 sizeof(struct af_iucv_trans_hdr)
1446 + ETH_HLEN);
1447 err = afiucv_hs_send(NULL, sk, sskb,
1448 AF_IUCV_FLAG_WIN);
1450 if (err) {
1451 sk->sk_state = IUCV_DISCONN;
1452 sk->sk_state_change(sk);
1456 spin_unlock_bh(&iucv->message_q.lock);
1459 done:
1460 /* SOCK_SEQPACKET: return real length if MSG_TRUNC is set */
1461 if (sk->sk_type == SOCK_SEQPACKET && (flags & MSG_TRUNC))
1462 copied = rlen;
1464 return copied;
1467 static inline unsigned int iucv_accept_poll(struct sock *parent)
1469 struct iucv_sock *isk, *n;
1470 struct sock *sk;
1472 list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) {
1473 sk = (struct sock *) isk;
1475 if (sk->sk_state == IUCV_CONNECTED)
1476 return POLLIN | POLLRDNORM;
1479 return 0;
1482 unsigned int iucv_sock_poll(struct file *file, struct socket *sock,
1483 poll_table *wait)
1485 struct sock *sk = sock->sk;
1486 unsigned int mask = 0;
1488 sock_poll_wait(file, sk_sleep(sk), wait);
1490 if (sk->sk_state == IUCV_LISTEN)
1491 return iucv_accept_poll(sk);
1493 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
1494 mask |= POLLERR;
1496 if (sk->sk_shutdown & RCV_SHUTDOWN)
1497 mask |= POLLRDHUP;
1499 if (sk->sk_shutdown == SHUTDOWN_MASK)
1500 mask |= POLLHUP;
1502 if (!skb_queue_empty(&sk->sk_receive_queue) ||
1503 (sk->sk_shutdown & RCV_SHUTDOWN))
1504 mask |= POLLIN | POLLRDNORM;
1506 if (sk->sk_state == IUCV_CLOSED)
1507 mask |= POLLHUP;
1509 if (sk->sk_state == IUCV_DISCONN || sk->sk_state == IUCV_SEVERED)
1510 mask |= POLLIN;
1512 if (sock_writeable(sk))
1513 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
1514 else
1515 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
1517 return mask;
1520 static int iucv_sock_shutdown(struct socket *sock, int how)
1522 struct sock *sk = sock->sk;
1523 struct iucv_sock *iucv = iucv_sk(sk);
1524 struct iucv_message txmsg;
1525 int err = 0;
1527 how++;
1529 if ((how & ~SHUTDOWN_MASK) || !how)
1530 return -EINVAL;
1532 lock_sock(sk);
1533 switch (sk->sk_state) {
1534 case IUCV_DISCONN:
1535 case IUCV_CLOSING:
1536 case IUCV_SEVERED:
1537 case IUCV_CLOSED:
1538 err = -ENOTCONN;
1539 goto fail;
1541 default:
1542 sk->sk_shutdown |= how;
1543 break;
1546 if (how == SEND_SHUTDOWN || how == SHUTDOWN_MASK) {
1547 txmsg.class = 0;
1548 txmsg.tag = 0;
1549 err = pr_iucv->message_send(iucv->path, &txmsg, IUCV_IPRMDATA,
1550 0, (void *) iprm_shutdown, 8);
1551 if (err) {
1552 switch (err) {
1553 case 1:
1554 err = -ENOTCONN;
1555 break;
1556 case 2:
1557 err = -ECONNRESET;
1558 break;
1559 default:
1560 err = -ENOTCONN;
1561 break;
1566 if (how == RCV_SHUTDOWN || how == SHUTDOWN_MASK) {
1567 err = pr_iucv->path_quiesce(iucv->path, NULL);
1568 if (err)
1569 err = -ENOTCONN;
1571 skb_queue_purge(&sk->sk_receive_queue);
1574 /* Wake up anyone sleeping in poll */
1575 sk->sk_state_change(sk);
1577 fail:
1578 release_sock(sk);
1579 return err;
1582 static int iucv_sock_release(struct socket *sock)
1584 struct sock *sk = sock->sk;
1585 int err = 0;
1587 if (!sk)
1588 return 0;
1590 iucv_sock_close(sk);
1592 /* Unregister with IUCV base support */
1593 if (iucv_sk(sk)->path) {
1594 pr_iucv->path_sever(iucv_sk(sk)->path, NULL);
1595 iucv_path_free(iucv_sk(sk)->path);
1596 iucv_sk(sk)->path = NULL;
1599 sock_orphan(sk);
1600 iucv_sock_kill(sk);
1601 return err;
1604 /* getsockopt and setsockopt */
1605 static int iucv_sock_setsockopt(struct socket *sock, int level, int optname,
1606 char __user *optval, unsigned int optlen)
1608 struct sock *sk = sock->sk;
1609 struct iucv_sock *iucv = iucv_sk(sk);
1610 int val;
1611 int rc;
1613 if (level != SOL_IUCV)
1614 return -ENOPROTOOPT;
1616 if (optlen < sizeof(int))
1617 return -EINVAL;
1619 if (get_user(val, (int __user *) optval))
1620 return -EFAULT;
1622 rc = 0;
1624 lock_sock(sk);
1625 switch (optname) {
1626 case SO_IPRMDATA_MSG:
1627 if (val)
1628 iucv->flags |= IUCV_IPRMDATA;
1629 else
1630 iucv->flags &= ~IUCV_IPRMDATA;
1631 break;
1632 case SO_MSGLIMIT:
1633 switch (sk->sk_state) {
1634 case IUCV_OPEN:
1635 case IUCV_BOUND:
1636 if (val < 1 || val > (u16)(~0))
1637 rc = -EINVAL;
1638 else
1639 iucv->msglimit = val;
1640 break;
1641 default:
1642 rc = -EINVAL;
1643 break;
1645 break;
1646 default:
1647 rc = -ENOPROTOOPT;
1648 break;
1650 release_sock(sk);
1652 return rc;
1655 static int iucv_sock_getsockopt(struct socket *sock, int level, int optname,
1656 char __user *optval, int __user *optlen)
1658 struct sock *sk = sock->sk;
1659 struct iucv_sock *iucv = iucv_sk(sk);
1660 int val, len;
1662 if (level != SOL_IUCV)
1663 return -ENOPROTOOPT;
1665 if (get_user(len, optlen))
1666 return -EFAULT;
1668 if (len < 0)
1669 return -EINVAL;
1671 len = min_t(unsigned int, len, sizeof(int));
1673 switch (optname) {
1674 case SO_IPRMDATA_MSG:
1675 val = (iucv->flags & IUCV_IPRMDATA) ? 1 : 0;
1676 break;
1677 case SO_MSGLIMIT:
1678 lock_sock(sk);
1679 val = (iucv->path != NULL) ? iucv->path->msglim /* connected */
1680 : iucv->msglimit; /* default */
1681 release_sock(sk);
1682 break;
1683 default:
1684 return -ENOPROTOOPT;
1687 if (put_user(len, optlen))
1688 return -EFAULT;
1689 if (copy_to_user(optval, &val, len))
1690 return -EFAULT;
1692 return 0;
1696 /* Callback wrappers - called from iucv base support */
1697 static int iucv_callback_connreq(struct iucv_path *path,
1698 u8 ipvmid[8], u8 ipuser[16])
1700 unsigned char user_data[16];
1701 unsigned char nuser_data[16];
1702 unsigned char src_name[8];
1703 struct hlist_node *node;
1704 struct sock *sk, *nsk;
1705 struct iucv_sock *iucv, *niucv;
1706 int err;
1708 memcpy(src_name, ipuser, 8);
1709 EBCASC(src_name, 8);
1710 /* Find out if this path belongs to af_iucv. */
1711 read_lock(&iucv_sk_list.lock);
1712 iucv = NULL;
1713 sk = NULL;
1714 sk_for_each(sk, node, &iucv_sk_list.head)
1715 if (sk->sk_state == IUCV_LISTEN &&
1716 !memcmp(&iucv_sk(sk)->src_name, src_name, 8)) {
1718 * Found a listening socket with
1719 * src_name == ipuser[0-7].
1721 iucv = iucv_sk(sk);
1722 break;
1724 read_unlock(&iucv_sk_list.lock);
1725 if (!iucv)
1726 /* No socket found, not one of our paths. */
1727 return -EINVAL;
1729 bh_lock_sock(sk);
1731 /* Check if parent socket is listening */
1732 low_nmcpy(user_data, iucv->src_name);
1733 high_nmcpy(user_data, iucv->dst_name);
1734 ASCEBC(user_data, sizeof(user_data));
1735 if (sk->sk_state != IUCV_LISTEN) {
1736 err = pr_iucv->path_sever(path, user_data);
1737 iucv_path_free(path);
1738 goto fail;
1741 /* Check for backlog size */
1742 if (sk_acceptq_is_full(sk)) {
1743 err = pr_iucv->path_sever(path, user_data);
1744 iucv_path_free(path);
1745 goto fail;
1748 /* Create the new socket */
1749 nsk = iucv_sock_alloc(NULL, sk->sk_type, GFP_ATOMIC);
1750 if (!nsk) {
1751 err = pr_iucv->path_sever(path, user_data);
1752 iucv_path_free(path);
1753 goto fail;
1756 niucv = iucv_sk(nsk);
1757 iucv_sock_init(nsk, sk);
1759 /* Set the new iucv_sock */
1760 memcpy(niucv->dst_name, ipuser + 8, 8);
1761 EBCASC(niucv->dst_name, 8);
1762 memcpy(niucv->dst_user_id, ipvmid, 8);
1763 memcpy(niucv->src_name, iucv->src_name, 8);
1764 memcpy(niucv->src_user_id, iucv->src_user_id, 8);
1765 niucv->path = path;
1767 /* Call iucv_accept */
1768 high_nmcpy(nuser_data, ipuser + 8);
1769 memcpy(nuser_data + 8, niucv->src_name, 8);
1770 ASCEBC(nuser_data + 8, 8);
1772 /* set message limit for path based on msglimit of accepting socket */
1773 niucv->msglimit = iucv->msglimit;
1774 path->msglim = iucv->msglimit;
1775 err = pr_iucv->path_accept(path, &af_iucv_handler, nuser_data, nsk);
1776 if (err) {
1777 err = pr_iucv->path_sever(path, user_data);
1778 iucv_path_free(path);
1779 iucv_sock_kill(nsk);
1780 goto fail;
1783 iucv_accept_enqueue(sk, nsk);
1785 /* Wake up accept */
1786 nsk->sk_state = IUCV_CONNECTED;
1787 sk->sk_data_ready(sk, 1);
1788 err = 0;
1789 fail:
1790 bh_unlock_sock(sk);
1791 return 0;
1794 static void iucv_callback_connack(struct iucv_path *path, u8 ipuser[16])
1796 struct sock *sk = path->private;
1798 sk->sk_state = IUCV_CONNECTED;
1799 sk->sk_state_change(sk);
1802 static void iucv_callback_rx(struct iucv_path *path, struct iucv_message *msg)
1804 struct sock *sk = path->private;
1805 struct iucv_sock *iucv = iucv_sk(sk);
1806 struct sk_buff *skb;
1807 struct sock_msg_q *save_msg;
1808 int len;
1810 if (sk->sk_shutdown & RCV_SHUTDOWN) {
1811 pr_iucv->message_reject(path, msg);
1812 return;
1815 spin_lock(&iucv->message_q.lock);
1817 if (!list_empty(&iucv->message_q.list) ||
1818 !skb_queue_empty(&iucv->backlog_skb_q))
1819 goto save_message;
1821 len = atomic_read(&sk->sk_rmem_alloc);
1822 len += iucv_msg_length(msg) + sizeof(struct sk_buff);
1823 if (len > sk->sk_rcvbuf)
1824 goto save_message;
1826 skb = alloc_skb(iucv_msg_length(msg), GFP_ATOMIC | GFP_DMA);
1827 if (!skb)
1828 goto save_message;
1830 iucv_process_message(sk, skb, path, msg);
1831 goto out_unlock;
1833 save_message:
1834 save_msg = kzalloc(sizeof(struct sock_msg_q), GFP_ATOMIC | GFP_DMA);
1835 if (!save_msg)
1836 goto out_unlock;
1837 save_msg->path = path;
1838 save_msg->msg = *msg;
1840 list_add_tail(&save_msg->list, &iucv->message_q.list);
1842 out_unlock:
1843 spin_unlock(&iucv->message_q.lock);
1846 static void iucv_callback_txdone(struct iucv_path *path,
1847 struct iucv_message *msg)
1849 struct sock *sk = path->private;
1850 struct sk_buff *this = NULL;
1851 struct sk_buff_head *list = &iucv_sk(sk)->send_skb_q;
1852 struct sk_buff *list_skb = list->next;
1853 unsigned long flags;
1855 if (!skb_queue_empty(list)) {
1856 spin_lock_irqsave(&list->lock, flags);
1858 while (list_skb != (struct sk_buff *)list) {
1859 if (!memcmp(&msg->tag, CB_TAG(list_skb), CB_TAG_LEN)) {
1860 this = list_skb;
1861 break;
1863 list_skb = list_skb->next;
1865 if (this)
1866 __skb_unlink(this, list);
1868 spin_unlock_irqrestore(&list->lock, flags);
1870 if (this) {
1871 kfree_skb(this);
1872 /* wake up any process waiting for sending */
1873 iucv_sock_wake_msglim(sk);
1876 BUG_ON(!this);
1878 if (sk->sk_state == IUCV_CLOSING) {
1879 if (skb_queue_empty(&iucv_sk(sk)->send_skb_q)) {
1880 sk->sk_state = IUCV_CLOSED;
1881 sk->sk_state_change(sk);
1887 static void iucv_callback_connrej(struct iucv_path *path, u8 ipuser[16])
1889 struct sock *sk = path->private;
1891 if (!list_empty(&iucv_sk(sk)->accept_q))
1892 sk->sk_state = IUCV_SEVERED;
1893 else
1894 sk->sk_state = IUCV_DISCONN;
1896 sk->sk_state_change(sk);
1899 /* called if the other communication side shuts down its RECV direction;
1900 * in turn, the callback sets SEND_SHUTDOWN to disable sending of data.
1902 static void iucv_callback_shutdown(struct iucv_path *path, u8 ipuser[16])
1904 struct sock *sk = path->private;
1906 bh_lock_sock(sk);
1907 if (sk->sk_state != IUCV_CLOSED) {
1908 sk->sk_shutdown |= SEND_SHUTDOWN;
1909 sk->sk_state_change(sk);
1911 bh_unlock_sock(sk);
1914 /***************** HiperSockets transport callbacks ********************/
1915 static void afiucv_swap_src_dest(struct sk_buff *skb)
1917 struct af_iucv_trans_hdr *trans_hdr =
1918 (struct af_iucv_trans_hdr *)skb->data;
1919 char tmpID[8];
1920 char tmpName[8];
1922 ASCEBC(trans_hdr->destUserID, sizeof(trans_hdr->destUserID));
1923 ASCEBC(trans_hdr->destAppName, sizeof(trans_hdr->destAppName));
1924 ASCEBC(trans_hdr->srcUserID, sizeof(trans_hdr->srcUserID));
1925 ASCEBC(trans_hdr->srcAppName, sizeof(trans_hdr->srcAppName));
1926 memcpy(tmpID, trans_hdr->srcUserID, 8);
1927 memcpy(tmpName, trans_hdr->srcAppName, 8);
1928 memcpy(trans_hdr->srcUserID, trans_hdr->destUserID, 8);
1929 memcpy(trans_hdr->srcAppName, trans_hdr->destAppName, 8);
1930 memcpy(trans_hdr->destUserID, tmpID, 8);
1931 memcpy(trans_hdr->destAppName, tmpName, 8);
1932 skb_push(skb, ETH_HLEN);
1933 memset(skb->data, 0, ETH_HLEN);
1937 * afiucv_hs_callback_syn - react on received SYN
1939 static int afiucv_hs_callback_syn(struct sock *sk, struct sk_buff *skb)
1941 struct sock *nsk;
1942 struct iucv_sock *iucv, *niucv;
1943 struct af_iucv_trans_hdr *trans_hdr;
1944 int err;
1946 iucv = iucv_sk(sk);
1947 trans_hdr = (struct af_iucv_trans_hdr *)skb->data;
1948 if (!iucv) {
1949 /* no sock - connection refused */
1950 afiucv_swap_src_dest(skb);
1951 trans_hdr->flags = AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN;
1952 err = dev_queue_xmit(skb);
1953 goto out;
1956 nsk = iucv_sock_alloc(NULL, sk->sk_type, GFP_ATOMIC);
1957 bh_lock_sock(sk);
1958 if ((sk->sk_state != IUCV_LISTEN) ||
1959 sk_acceptq_is_full(sk) ||
1960 !nsk) {
1961 /* error on server socket - connection refused */
1962 if (nsk)
1963 sk_free(nsk);
1964 afiucv_swap_src_dest(skb);
1965 trans_hdr->flags = AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN;
1966 err = dev_queue_xmit(skb);
1967 bh_unlock_sock(sk);
1968 goto out;
1971 niucv = iucv_sk(nsk);
1972 iucv_sock_init(nsk, sk);
1973 niucv->transport = AF_IUCV_TRANS_HIPER;
1974 niucv->msglimit = iucv->msglimit;
1975 if (!trans_hdr->window)
1976 niucv->msglimit_peer = IUCV_HIPER_MSGLIM_DEFAULT;
1977 else
1978 niucv->msglimit_peer = trans_hdr->window;
1979 memcpy(niucv->dst_name, trans_hdr->srcAppName, 8);
1980 memcpy(niucv->dst_user_id, trans_hdr->srcUserID, 8);
1981 memcpy(niucv->src_name, iucv->src_name, 8);
1982 memcpy(niucv->src_user_id, iucv->src_user_id, 8);
1983 nsk->sk_bound_dev_if = sk->sk_bound_dev_if;
1984 afiucv_swap_src_dest(skb);
1985 trans_hdr->flags = AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_ACK;
1986 trans_hdr->window = niucv->msglimit;
1987 /* if receiver acks the xmit connection is established */
1988 err = dev_queue_xmit(skb);
1989 if (!err) {
1990 iucv_accept_enqueue(sk, nsk);
1991 nsk->sk_state = IUCV_CONNECTED;
1992 sk->sk_data_ready(sk, 1);
1993 } else
1994 iucv_sock_kill(nsk);
1995 bh_unlock_sock(sk);
1997 out:
1998 return NET_RX_SUCCESS;
2002 * afiucv_hs_callback_synack() - react on received SYN-ACK
2004 static int afiucv_hs_callback_synack(struct sock *sk, struct sk_buff *skb)
2006 struct iucv_sock *iucv = iucv_sk(sk);
2007 struct af_iucv_trans_hdr *trans_hdr =
2008 (struct af_iucv_trans_hdr *)skb->data;
2010 if (!iucv)
2011 goto out;
2012 if (sk->sk_state != IUCV_BOUND)
2013 goto out;
2014 bh_lock_sock(sk);
2015 iucv->msglimit_peer = trans_hdr->window;
2016 sk->sk_state = IUCV_CONNECTED;
2017 sk->sk_state_change(sk);
2018 bh_unlock_sock(sk);
2019 out:
2020 kfree_skb(skb);
2021 return NET_RX_SUCCESS;
2025 * afiucv_hs_callback_synfin() - react on received SYN_FIN
2027 static int afiucv_hs_callback_synfin(struct sock *sk, struct sk_buff *skb)
2029 struct iucv_sock *iucv = iucv_sk(sk);
2031 if (!iucv)
2032 goto out;
2033 if (sk->sk_state != IUCV_BOUND)
2034 goto out;
2035 bh_lock_sock(sk);
2036 sk->sk_state = IUCV_DISCONN;
2037 sk->sk_state_change(sk);
2038 bh_unlock_sock(sk);
2039 out:
2040 kfree_skb(skb);
2041 return NET_RX_SUCCESS;
2045 * afiucv_hs_callback_fin() - react on received FIN
2047 static int afiucv_hs_callback_fin(struct sock *sk, struct sk_buff *skb)
2049 struct iucv_sock *iucv = iucv_sk(sk);
2051 /* other end of connection closed */
2052 if (iucv) {
2053 bh_lock_sock(sk);
2054 if (!list_empty(&iucv->accept_q))
2055 sk->sk_state = IUCV_SEVERED;
2056 else
2057 sk->sk_state = IUCV_DISCONN;
2058 sk->sk_state_change(sk);
2059 bh_unlock_sock(sk);
2061 kfree_skb(skb);
2062 return NET_RX_SUCCESS;
2066 * afiucv_hs_callback_win() - react on received WIN
2068 static int afiucv_hs_callback_win(struct sock *sk, struct sk_buff *skb)
2070 struct iucv_sock *iucv = iucv_sk(sk);
2071 struct af_iucv_trans_hdr *trans_hdr =
2072 (struct af_iucv_trans_hdr *)skb->data;
2074 if (!iucv)
2075 return NET_RX_SUCCESS;
2077 if (sk->sk_state != IUCV_CONNECTED)
2078 return NET_RX_SUCCESS;
2080 atomic_sub(trans_hdr->window, &iucv->msg_sent);
2081 iucv_sock_wake_msglim(sk);
2082 return NET_RX_SUCCESS;
2086 * afiucv_hs_callback_rx() - react on received data
2088 static int afiucv_hs_callback_rx(struct sock *sk, struct sk_buff *skb)
2090 struct iucv_sock *iucv = iucv_sk(sk);
2092 if (!iucv) {
2093 kfree_skb(skb);
2094 return NET_RX_SUCCESS;
2097 if (sk->sk_state != IUCV_CONNECTED) {
2098 kfree_skb(skb);
2099 return NET_RX_SUCCESS;
2102 /* write stuff from iucv_msg to skb cb */
2103 if (skb->len <= sizeof(struct af_iucv_trans_hdr)) {
2104 kfree_skb(skb);
2105 return NET_RX_SUCCESS;
2107 skb_pull(skb, sizeof(struct af_iucv_trans_hdr));
2108 skb_reset_transport_header(skb);
2109 skb_reset_network_header(skb);
2110 spin_lock(&iucv->message_q.lock);
2111 if (skb_queue_empty(&iucv->backlog_skb_q)) {
2112 if (sock_queue_rcv_skb(sk, skb)) {
2113 /* handle rcv queue full */
2114 skb_queue_tail(&iucv->backlog_skb_q, skb);
2116 } else
2117 skb_queue_tail(&iucv_sk(sk)->backlog_skb_q, skb);
2118 spin_unlock(&iucv->message_q.lock);
2119 return NET_RX_SUCCESS;
2123 * afiucv_hs_rcv() - base function for arriving data through HiperSockets
2124 * transport
2125 * called from netif RX softirq
2127 static int afiucv_hs_rcv(struct sk_buff *skb, struct net_device *dev,
2128 struct packet_type *pt, struct net_device *orig_dev)
2130 struct hlist_node *node;
2131 struct sock *sk;
2132 struct iucv_sock *iucv;
2133 struct af_iucv_trans_hdr *trans_hdr;
2134 char nullstring[8];
2135 int err = 0;
2137 skb_pull(skb, ETH_HLEN);
2138 trans_hdr = (struct af_iucv_trans_hdr *)skb->data;
2139 EBCASC(trans_hdr->destAppName, sizeof(trans_hdr->destAppName));
2140 EBCASC(trans_hdr->destUserID, sizeof(trans_hdr->destUserID));
2141 EBCASC(trans_hdr->srcAppName, sizeof(trans_hdr->srcAppName));
2142 EBCASC(trans_hdr->srcUserID, sizeof(trans_hdr->srcUserID));
2143 memset(nullstring, 0, sizeof(nullstring));
2144 iucv = NULL;
2145 sk = NULL;
2146 read_lock(&iucv_sk_list.lock);
2147 sk_for_each(sk, node, &iucv_sk_list.head) {
2148 if (trans_hdr->flags == AF_IUCV_FLAG_SYN) {
2149 if ((!memcmp(&iucv_sk(sk)->src_name,
2150 trans_hdr->destAppName, 8)) &&
2151 (!memcmp(&iucv_sk(sk)->src_user_id,
2152 trans_hdr->destUserID, 8)) &&
2153 (!memcmp(&iucv_sk(sk)->dst_name, nullstring, 8)) &&
2154 (!memcmp(&iucv_sk(sk)->dst_user_id,
2155 nullstring, 8))) {
2156 iucv = iucv_sk(sk);
2157 break;
2159 } else {
2160 if ((!memcmp(&iucv_sk(sk)->src_name,
2161 trans_hdr->destAppName, 8)) &&
2162 (!memcmp(&iucv_sk(sk)->src_user_id,
2163 trans_hdr->destUserID, 8)) &&
2164 (!memcmp(&iucv_sk(sk)->dst_name,
2165 trans_hdr->srcAppName, 8)) &&
2166 (!memcmp(&iucv_sk(sk)->dst_user_id,
2167 trans_hdr->srcUserID, 8))) {
2168 iucv = iucv_sk(sk);
2169 break;
2173 read_unlock(&iucv_sk_list.lock);
2174 if (!iucv)
2175 sk = NULL;
2177 /* no sock
2178 how should we send with no sock
2179 1) send without sock no send rc checking?
2180 2) introduce default sock to handle this cases
2182 SYN -> send SYN|ACK in good case, send SYN|FIN in bad case
2183 data -> send FIN
2184 SYN|ACK, SYN|FIN, FIN -> no action? */
2186 switch (trans_hdr->flags) {
2187 case AF_IUCV_FLAG_SYN:
2188 /* connect request */
2189 err = afiucv_hs_callback_syn(sk, skb);
2190 break;
2191 case (AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_ACK):
2192 /* connect request confirmed */
2193 err = afiucv_hs_callback_synack(sk, skb);
2194 break;
2195 case (AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN):
2196 /* connect request refused */
2197 err = afiucv_hs_callback_synfin(sk, skb);
2198 break;
2199 case (AF_IUCV_FLAG_FIN):
2200 /* close request */
2201 err = afiucv_hs_callback_fin(sk, skb);
2202 break;
2203 case (AF_IUCV_FLAG_WIN):
2204 err = afiucv_hs_callback_win(sk, skb);
2205 if (skb->len > sizeof(struct af_iucv_trans_hdr))
2206 err = afiucv_hs_callback_rx(sk, skb);
2207 else
2208 kfree(skb);
2209 break;
2210 case 0:
2211 /* plain data frame */
2212 err = afiucv_hs_callback_rx(sk, skb);
2213 break;
2214 default:
2218 return err;
2222 * afiucv_hs_callback_txnotify() - handle send notifcations from HiperSockets
2223 * transport
2225 static void afiucv_hs_callback_txnotify(struct sk_buff *skb,
2226 enum iucv_tx_notify n)
2228 struct sock *isk = skb->sk;
2229 struct sock *sk = NULL;
2230 struct iucv_sock *iucv = NULL;
2231 struct sk_buff_head *list;
2232 struct sk_buff *list_skb;
2233 struct sk_buff *this = NULL;
2234 unsigned long flags;
2235 struct hlist_node *node;
2237 read_lock(&iucv_sk_list.lock);
2238 sk_for_each(sk, node, &iucv_sk_list.head)
2239 if (sk == isk) {
2240 iucv = iucv_sk(sk);
2241 break;
2243 read_unlock(&iucv_sk_list.lock);
2245 if (!iucv)
2246 return;
2248 bh_lock_sock(sk);
2249 list = &iucv->send_skb_q;
2250 list_skb = list->next;
2251 if (skb_queue_empty(list))
2252 goto out_unlock;
2254 spin_lock_irqsave(&list->lock, flags);
2255 while (list_skb != (struct sk_buff *)list) {
2256 if (skb_shinfo(list_skb) == skb_shinfo(skb)) {
2257 this = list_skb;
2258 switch (n) {
2259 case TX_NOTIFY_OK:
2260 __skb_unlink(this, list);
2261 iucv_sock_wake_msglim(sk);
2262 kfree_skb(this);
2263 break;
2264 case TX_NOTIFY_PENDING:
2265 atomic_inc(&iucv->pendings);
2266 break;
2267 case TX_NOTIFY_DELAYED_OK:
2268 __skb_unlink(this, list);
2269 atomic_dec(&iucv->pendings);
2270 if (atomic_read(&iucv->pendings) <= 0)
2271 iucv_sock_wake_msglim(sk);
2272 kfree_skb(this);
2273 break;
2274 case TX_NOTIFY_UNREACHABLE:
2275 case TX_NOTIFY_DELAYED_UNREACHABLE:
2276 case TX_NOTIFY_TPQFULL: /* not yet used */
2277 case TX_NOTIFY_GENERALERROR:
2278 case TX_NOTIFY_DELAYED_GENERALERROR:
2279 __skb_unlink(this, list);
2280 kfree_skb(this);
2281 if (!list_empty(&iucv->accept_q))
2282 sk->sk_state = IUCV_SEVERED;
2283 else
2284 sk->sk_state = IUCV_DISCONN;
2285 sk->sk_state_change(sk);
2286 break;
2288 break;
2290 list_skb = list_skb->next;
2292 spin_unlock_irqrestore(&list->lock, flags);
2294 out_unlock:
2295 bh_unlock_sock(sk);
2297 static const struct proto_ops iucv_sock_ops = {
2298 .family = PF_IUCV,
2299 .owner = THIS_MODULE,
2300 .release = iucv_sock_release,
2301 .bind = iucv_sock_bind,
2302 .connect = iucv_sock_connect,
2303 .listen = iucv_sock_listen,
2304 .accept = iucv_sock_accept,
2305 .getname = iucv_sock_getname,
2306 .sendmsg = iucv_sock_sendmsg,
2307 .recvmsg = iucv_sock_recvmsg,
2308 .poll = iucv_sock_poll,
2309 .ioctl = sock_no_ioctl,
2310 .mmap = sock_no_mmap,
2311 .socketpair = sock_no_socketpair,
2312 .shutdown = iucv_sock_shutdown,
2313 .setsockopt = iucv_sock_setsockopt,
2314 .getsockopt = iucv_sock_getsockopt,
2317 static const struct net_proto_family iucv_sock_family_ops = {
2318 .family = AF_IUCV,
2319 .owner = THIS_MODULE,
2320 .create = iucv_sock_create,
2323 static struct packet_type iucv_packet_type = {
2324 .type = cpu_to_be16(ETH_P_AF_IUCV),
2325 .func = afiucv_hs_rcv,
2328 static int afiucv_iucv_init(void)
2330 int err;
2332 err = pr_iucv->iucv_register(&af_iucv_handler, 0);
2333 if (err)
2334 goto out;
2335 /* establish dummy device */
2336 af_iucv_driver.bus = pr_iucv->bus;
2337 err = driver_register(&af_iucv_driver);
2338 if (err)
2339 goto out_iucv;
2340 af_iucv_dev = kzalloc(sizeof(struct device), GFP_KERNEL);
2341 if (!af_iucv_dev) {
2342 err = -ENOMEM;
2343 goto out_driver;
2345 dev_set_name(af_iucv_dev, "af_iucv");
2346 af_iucv_dev->bus = pr_iucv->bus;
2347 af_iucv_dev->parent = pr_iucv->root;
2348 af_iucv_dev->release = (void (*)(struct device *))kfree;
2349 af_iucv_dev->driver = &af_iucv_driver;
2350 err = device_register(af_iucv_dev);
2351 if (err)
2352 goto out_driver;
2353 return 0;
2355 out_driver:
2356 driver_unregister(&af_iucv_driver);
2357 out_iucv:
2358 pr_iucv->iucv_unregister(&af_iucv_handler, 0);
2359 out:
2360 return err;
2363 static int __init afiucv_init(void)
2365 int err;
2367 if (MACHINE_IS_VM) {
2368 cpcmd("QUERY USERID", iucv_userid, sizeof(iucv_userid), &err);
2369 if (unlikely(err)) {
2370 WARN_ON(err);
2371 err = -EPROTONOSUPPORT;
2372 goto out;
2375 pr_iucv = try_then_request_module(symbol_get(iucv_if), "iucv");
2376 if (!pr_iucv) {
2377 printk(KERN_WARNING "iucv_if lookup failed\n");
2378 memset(&iucv_userid, 0, sizeof(iucv_userid));
2380 } else {
2381 memset(&iucv_userid, 0, sizeof(iucv_userid));
2382 pr_iucv = NULL;
2385 err = proto_register(&iucv_proto, 0);
2386 if (err)
2387 goto out;
2388 err = sock_register(&iucv_sock_family_ops);
2389 if (err)
2390 goto out_proto;
2392 if (pr_iucv) {
2393 err = afiucv_iucv_init();
2394 if (err)
2395 goto out_sock;
2397 dev_add_pack(&iucv_packet_type);
2398 return 0;
2400 out_sock:
2401 sock_unregister(PF_IUCV);
2402 out_proto:
2403 proto_unregister(&iucv_proto);
2404 out:
2405 if (pr_iucv)
2406 symbol_put(iucv_if);
2407 return err;
2410 static void __exit afiucv_exit(void)
2412 if (pr_iucv) {
2413 device_unregister(af_iucv_dev);
2414 driver_unregister(&af_iucv_driver);
2415 pr_iucv->iucv_unregister(&af_iucv_handler, 0);
2416 symbol_put(iucv_if);
2418 dev_remove_pack(&iucv_packet_type);
2419 sock_unregister(PF_IUCV);
2420 proto_unregister(&iucv_proto);
2423 module_init(afiucv_init);
2424 module_exit(afiucv_exit);
2426 MODULE_AUTHOR("Jennifer Hunt <jenhunt@us.ibm.com>");
2427 MODULE_DESCRIPTION("IUCV Sockets ver " VERSION);
2428 MODULE_VERSION(VERSION);
2429 MODULE_LICENSE("GPL");
2430 MODULE_ALIAS_NETPROTO(PF_IUCV);