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>
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/signal.h>
21 #include <linux/slab.h>
22 #include <linux/skbuff.h>
23 #include <linux/init.h>
24 #include <linux/poll.h>
25 #include <linux/security.h>
27 #include <asm/ebcdic.h>
28 #include <asm/cpcmd.h>
29 #include <linux/kmod.h>
31 #include <net/iucv/af_iucv.h>
35 static char iucv_userid[80];
37 static const struct proto_ops iucv_sock_ops;
39 static struct proto iucv_proto = {
42 .obj_size = sizeof(struct iucv_sock),
45 static struct iucv_interface *pr_iucv;
47 /* special AF_IUCV IPRM messages */
48 static const u8 iprm_shutdown[8] =
49 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01};
51 #define TRGCLS_SIZE FIELD_SIZEOF(struct iucv_message, class)
53 #define __iucv_sock_wait(sk, condition, timeo, ret) \
55 DEFINE_WAIT(__wait); \
56 long __timeo = timeo; \
58 prepare_to_wait(sk_sleep(sk), &__wait, TASK_INTERRUPTIBLE); \
59 while (!(condition)) { \
64 if (signal_pending(current)) { \
65 ret = sock_intr_errno(__timeo); \
69 __timeo = schedule_timeout(__timeo); \
71 ret = sock_error(sk); \
75 finish_wait(sk_sleep(sk), &__wait); \
78 #define iucv_sock_wait(sk, condition, timeo) \
82 __iucv_sock_wait(sk, condition, timeo, __ret); \
86 static void iucv_sock_kill(struct sock *sk);
87 static void iucv_sock_close(struct sock *sk);
88 static void iucv_sever_path(struct sock *, int);
90 static int afiucv_hs_rcv(struct sk_buff *skb, struct net_device *dev,
91 struct packet_type *pt, struct net_device *orig_dev);
92 static int afiucv_hs_send(struct iucv_message *imsg, struct sock *sock,
93 struct sk_buff *skb, u8 flags);
94 static void afiucv_hs_callback_txnotify(struct sk_buff *, enum iucv_tx_notify);
96 /* Call Back functions */
97 static void iucv_callback_rx(struct iucv_path *, struct iucv_message *);
98 static void iucv_callback_txdone(struct iucv_path *, struct iucv_message *);
99 static void iucv_callback_connack(struct iucv_path *, u8 *);
100 static int iucv_callback_connreq(struct iucv_path *, u8 *, u8 *);
101 static void iucv_callback_connrej(struct iucv_path *, u8 *);
102 static void iucv_callback_shutdown(struct iucv_path *, u8 *);
104 static struct iucv_sock_list iucv_sk_list = {
105 .lock = __RW_LOCK_UNLOCKED(iucv_sk_list.lock),
106 .autobind_name = ATOMIC_INIT(0)
109 static struct iucv_handler af_iucv_handler = {
110 .path_pending = iucv_callback_connreq,
111 .path_complete = iucv_callback_connack,
112 .path_severed = iucv_callback_connrej,
113 .message_pending = iucv_callback_rx,
114 .message_complete = iucv_callback_txdone,
115 .path_quiesced = iucv_callback_shutdown,
118 static inline void high_nmcpy(unsigned char *dst, char *src)
123 static inline void low_nmcpy(unsigned char *dst, char *src)
125 memcpy(&dst[8], src, 8);
128 static int afiucv_pm_prepare(struct device *dev)
130 #ifdef CONFIG_PM_DEBUG
131 printk(KERN_WARNING "afiucv_pm_prepare\n");
136 static void afiucv_pm_complete(struct device *dev)
138 #ifdef CONFIG_PM_DEBUG
139 printk(KERN_WARNING "afiucv_pm_complete\n");
144 * afiucv_pm_freeze() - Freeze PM callback
145 * @dev: AFIUCV dummy device
147 * Sever all established IUCV communication pathes
149 static int afiucv_pm_freeze(struct device *dev)
151 struct iucv_sock *iucv;
154 #ifdef CONFIG_PM_DEBUG
155 printk(KERN_WARNING "afiucv_pm_freeze\n");
157 read_lock(&iucv_sk_list.lock);
158 sk_for_each(sk, &iucv_sk_list.head) {
160 switch (sk->sk_state) {
164 iucv_sever_path(sk, 0);
173 skb_queue_purge(&iucv->send_skb_q);
174 skb_queue_purge(&iucv->backlog_skb_q);
176 read_unlock(&iucv_sk_list.lock);
181 * afiucv_pm_restore_thaw() - Thaw and restore PM callback
182 * @dev: AFIUCV dummy device
184 * socket clean up after freeze
186 static int afiucv_pm_restore_thaw(struct device *dev)
190 #ifdef CONFIG_PM_DEBUG
191 printk(KERN_WARNING "afiucv_pm_restore_thaw\n");
193 read_lock(&iucv_sk_list.lock);
194 sk_for_each(sk, &iucv_sk_list.head) {
195 switch (sk->sk_state) {
198 sk->sk_state = IUCV_DISCONN;
199 sk->sk_state_change(sk);
210 read_unlock(&iucv_sk_list.lock);
214 static const struct dev_pm_ops afiucv_pm_ops = {
215 .prepare = afiucv_pm_prepare,
216 .complete = afiucv_pm_complete,
217 .freeze = afiucv_pm_freeze,
218 .thaw = afiucv_pm_restore_thaw,
219 .restore = afiucv_pm_restore_thaw,
222 static struct device_driver af_iucv_driver = {
223 .owner = THIS_MODULE,
226 .pm = &afiucv_pm_ops,
229 /* dummy device used as trigger for PM functions */
230 static struct device *af_iucv_dev;
233 * iucv_msg_length() - Returns the length of an iucv message.
234 * @msg: Pointer to struct iucv_message, MUST NOT be NULL
236 * The function returns the length of the specified iucv message @msg of data
237 * stored in a buffer and of data stored in the parameter list (PRMDATA).
239 * For IUCV_IPRMDATA, AF_IUCV uses the following convention to transport socket
241 * PRMDATA[0..6] socket data (max 7 bytes);
242 * PRMDATA[7] socket data length value (len is 0xff - PRMDATA[7])
244 * The socket data length is computed by subtracting the socket data length
246 * If the socket data len is greater 7, then PRMDATA can be used for special
247 * notifications (see iucv_sock_shutdown); and further,
248 * if the socket data len is > 7, the function returns 8.
250 * Use this function to allocate socket buffers to store iucv message data.
252 static inline size_t iucv_msg_length(struct iucv_message *msg)
256 if (msg->flags & IUCV_IPRMDATA) {
257 datalen = 0xff - msg->rmmsg[7];
258 return (datalen < 8) ? datalen : 8;
264 * iucv_sock_in_state() - check for specific states
265 * @sk: sock structure
266 * @state: first iucv sk state
267 * @state: second iucv sk state
269 * Returns true if the socket in either in the first or second state.
271 static int iucv_sock_in_state(struct sock *sk, int state, int state2)
273 return (sk->sk_state == state || sk->sk_state == state2);
277 * iucv_below_msglim() - function to check if messages can be sent
278 * @sk: sock structure
280 * Returns true if the send queue length is lower than the message limit.
281 * Always returns true if the socket is not connected (no iucv path for
282 * checking the message limit).
284 static inline int iucv_below_msglim(struct sock *sk)
286 struct iucv_sock *iucv = iucv_sk(sk);
288 if (sk->sk_state != IUCV_CONNECTED)
290 if (iucv->transport == AF_IUCV_TRANS_IUCV)
291 return (skb_queue_len(&iucv->send_skb_q) < iucv->path->msglim);
293 return ((atomic_read(&iucv->msg_sent) < iucv->msglimit_peer) &&
294 (atomic_read(&iucv->pendings) <= 0));
298 * iucv_sock_wake_msglim() - Wake up thread waiting on msg limit
300 static void iucv_sock_wake_msglim(struct sock *sk)
302 struct socket_wq *wq;
305 wq = rcu_dereference(sk->sk_wq);
306 if (skwq_has_sleeper(wq))
307 wake_up_interruptible_all(&wq->wait);
308 sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
313 * afiucv_hs_send() - send a message through HiperSockets transport
315 static int afiucv_hs_send(struct iucv_message *imsg, struct sock *sock,
316 struct sk_buff *skb, u8 flags)
318 struct iucv_sock *iucv = iucv_sk(sock);
319 struct af_iucv_trans_hdr *phs_hdr;
320 struct sk_buff *nskb;
321 int err, confirm_recv = 0;
323 phs_hdr = skb_push(skb, sizeof(*phs_hdr));
324 memset(phs_hdr, 0, sizeof(*phs_hdr));
325 skb_reset_network_header(skb);
327 phs_hdr->magic = ETH_P_AF_IUCV;
328 phs_hdr->version = 1;
329 phs_hdr->flags = flags;
330 if (flags == AF_IUCV_FLAG_SYN)
331 phs_hdr->window = iucv->msglimit;
332 else if ((flags == AF_IUCV_FLAG_WIN) || !flags) {
333 confirm_recv = atomic_read(&iucv->msg_recv);
334 phs_hdr->window = confirm_recv;
336 phs_hdr->flags = phs_hdr->flags | AF_IUCV_FLAG_WIN;
338 memcpy(phs_hdr->destUserID, iucv->dst_user_id, 8);
339 memcpy(phs_hdr->destAppName, iucv->dst_name, 8);
340 memcpy(phs_hdr->srcUserID, iucv->src_user_id, 8);
341 memcpy(phs_hdr->srcAppName, iucv->src_name, 8);
342 ASCEBC(phs_hdr->destUserID, sizeof(phs_hdr->destUserID));
343 ASCEBC(phs_hdr->destAppName, sizeof(phs_hdr->destAppName));
344 ASCEBC(phs_hdr->srcUserID, sizeof(phs_hdr->srcUserID));
345 ASCEBC(phs_hdr->srcAppName, sizeof(phs_hdr->srcAppName));
347 memcpy(&phs_hdr->iucv_hdr, imsg, sizeof(struct iucv_message));
349 skb_push(skb, ETH_HLEN);
350 memset(skb->data, 0, ETH_HLEN);
352 skb->dev = iucv->hs_dev;
357 if (!(skb->dev->flags & IFF_UP) || !netif_carrier_ok(skb->dev)) {
361 if (skb->len > skb->dev->mtu) {
362 if (sock->sk_type == SOCK_SEQPACKET) {
366 skb_trim(skb, skb->dev->mtu);
368 skb->protocol = cpu_to_be16(ETH_P_AF_IUCV);
369 nskb = skb_clone(skb, GFP_ATOMIC);
375 skb_queue_tail(&iucv->send_skb_q, nskb);
376 err = dev_queue_xmit(skb);
377 if (net_xmit_eval(err)) {
378 skb_unlink(nskb, &iucv->send_skb_q);
381 atomic_sub(confirm_recv, &iucv->msg_recv);
382 WARN_ON(atomic_read(&iucv->msg_recv) < 0);
384 return net_xmit_eval(err);
391 static struct sock *__iucv_get_sock_by_name(char *nm)
395 sk_for_each(sk, &iucv_sk_list.head)
396 if (!memcmp(&iucv_sk(sk)->src_name, nm, 8))
402 static void iucv_sock_destruct(struct sock *sk)
404 skb_queue_purge(&sk->sk_receive_queue);
405 skb_queue_purge(&sk->sk_error_queue);
409 if (!sock_flag(sk, SOCK_DEAD)) {
410 pr_err("Attempt to release alive iucv socket %p\n", sk);
414 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
415 WARN_ON(refcount_read(&sk->sk_wmem_alloc));
416 WARN_ON(sk->sk_wmem_queued);
417 WARN_ON(sk->sk_forward_alloc);
421 static void iucv_sock_cleanup_listen(struct sock *parent)
425 /* Close non-accepted connections */
426 while ((sk = iucv_accept_dequeue(parent, NULL))) {
431 parent->sk_state = IUCV_CLOSED;
434 /* Kill socket (only if zapped and orphaned) */
435 static void iucv_sock_kill(struct sock *sk)
437 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
440 iucv_sock_unlink(&iucv_sk_list, sk);
441 sock_set_flag(sk, SOCK_DEAD);
445 /* Terminate an IUCV path */
446 static void iucv_sever_path(struct sock *sk, int with_user_data)
448 unsigned char user_data[16];
449 struct iucv_sock *iucv = iucv_sk(sk);
450 struct iucv_path *path = iucv->path;
454 if (with_user_data) {
455 low_nmcpy(user_data, iucv->src_name);
456 high_nmcpy(user_data, iucv->dst_name);
457 ASCEBC(user_data, sizeof(user_data));
458 pr_iucv->path_sever(path, user_data);
460 pr_iucv->path_sever(path, NULL);
461 iucv_path_free(path);
465 /* Send controlling flags through an IUCV socket for HIPER transport */
466 static int iucv_send_ctrl(struct sock *sk, u8 flags)
473 blen = sizeof(struct af_iucv_trans_hdr) + ETH_HLEN;
474 if (sk->sk_shutdown & SEND_SHUTDOWN) {
475 /* controlling flags should be sent anyway */
476 shutdown = sk->sk_shutdown;
477 sk->sk_shutdown &= RCV_SHUTDOWN;
479 skb = sock_alloc_send_skb(sk, blen, 1, &err);
481 skb_reserve(skb, blen);
482 err = afiucv_hs_send(NULL, sk, skb, flags);
485 sk->sk_shutdown = shutdown;
489 /* Close an IUCV socket */
490 static void iucv_sock_close(struct sock *sk)
492 struct iucv_sock *iucv = iucv_sk(sk);
498 switch (sk->sk_state) {
500 iucv_sock_cleanup_listen(sk);
504 if (iucv->transport == AF_IUCV_TRANS_HIPER) {
505 err = iucv_send_ctrl(sk, AF_IUCV_FLAG_FIN);
506 sk->sk_state = IUCV_DISCONN;
507 sk->sk_state_change(sk);
509 case IUCV_DISCONN: /* fall through */
510 sk->sk_state = IUCV_CLOSING;
511 sk->sk_state_change(sk);
513 if (!err && !skb_queue_empty(&iucv->send_skb_q)) {
514 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
515 timeo = sk->sk_lingertime;
517 timeo = IUCV_DISCONN_TIMEOUT;
519 iucv_sock_in_state(sk, IUCV_CLOSED, 0),
523 case IUCV_CLOSING: /* fall through */
524 sk->sk_state = IUCV_CLOSED;
525 sk->sk_state_change(sk);
527 sk->sk_err = ECONNRESET;
528 sk->sk_state_change(sk);
530 skb_queue_purge(&iucv->send_skb_q);
531 skb_queue_purge(&iucv->backlog_skb_q);
533 default: /* fall through */
534 iucv_sever_path(sk, 1);
538 dev_put(iucv->hs_dev);
540 sk->sk_bound_dev_if = 0;
543 /* mark socket for deletion by iucv_sock_kill() */
544 sock_set_flag(sk, SOCK_ZAPPED);
549 static void iucv_sock_init(struct sock *sk, struct sock *parent)
552 sk->sk_type = parent->sk_type;
553 security_sk_clone(parent, sk);
557 static struct sock *iucv_sock_alloc(struct socket *sock, int proto, gfp_t prio, int kern)
560 struct iucv_sock *iucv;
562 sk = sk_alloc(&init_net, PF_IUCV, prio, &iucv_proto, kern);
567 sock_init_data(sock, sk);
568 INIT_LIST_HEAD(&iucv->accept_q);
569 spin_lock_init(&iucv->accept_q_lock);
570 skb_queue_head_init(&iucv->send_skb_q);
571 INIT_LIST_HEAD(&iucv->message_q.list);
572 spin_lock_init(&iucv->message_q.lock);
573 skb_queue_head_init(&iucv->backlog_skb_q);
575 atomic_set(&iucv->pendings, 0);
578 atomic_set(&iucv->msg_sent, 0);
579 atomic_set(&iucv->msg_recv, 0);
581 iucv->sk_txnotify = afiucv_hs_callback_txnotify;
582 memset(&iucv->src_user_id , 0, 32);
584 iucv->transport = AF_IUCV_TRANS_IUCV;
586 iucv->transport = AF_IUCV_TRANS_HIPER;
588 sk->sk_destruct = iucv_sock_destruct;
589 sk->sk_sndtimeo = IUCV_CONN_TIMEOUT;
590 sk->sk_allocation = GFP_DMA;
592 sock_reset_flag(sk, SOCK_ZAPPED);
594 sk->sk_protocol = proto;
595 sk->sk_state = IUCV_OPEN;
597 iucv_sock_link(&iucv_sk_list, sk);
601 /* Create an IUCV socket */
602 static int iucv_sock_create(struct net *net, struct socket *sock, int protocol,
607 if (protocol && protocol != PF_IUCV)
608 return -EPROTONOSUPPORT;
610 sock->state = SS_UNCONNECTED;
612 switch (sock->type) {
614 sock->ops = &iucv_sock_ops;
617 /* currently, proto ops can handle both sk types */
618 sock->ops = &iucv_sock_ops;
621 return -ESOCKTNOSUPPORT;
624 sk = iucv_sock_alloc(sock, protocol, GFP_KERNEL, kern);
628 iucv_sock_init(sk, NULL);
633 void iucv_sock_link(struct iucv_sock_list *l, struct sock *sk)
635 write_lock_bh(&l->lock);
636 sk_add_node(sk, &l->head);
637 write_unlock_bh(&l->lock);
640 void iucv_sock_unlink(struct iucv_sock_list *l, struct sock *sk)
642 write_lock_bh(&l->lock);
643 sk_del_node_init(sk);
644 write_unlock_bh(&l->lock);
647 void iucv_accept_enqueue(struct sock *parent, struct sock *sk)
650 struct iucv_sock *par = iucv_sk(parent);
653 spin_lock_irqsave(&par->accept_q_lock, flags);
654 list_add_tail(&iucv_sk(sk)->accept_q, &par->accept_q);
655 spin_unlock_irqrestore(&par->accept_q_lock, flags);
656 iucv_sk(sk)->parent = parent;
657 sk_acceptq_added(parent);
660 void iucv_accept_unlink(struct sock *sk)
663 struct iucv_sock *par = iucv_sk(iucv_sk(sk)->parent);
665 spin_lock_irqsave(&par->accept_q_lock, flags);
666 list_del_init(&iucv_sk(sk)->accept_q);
667 spin_unlock_irqrestore(&par->accept_q_lock, flags);
668 sk_acceptq_removed(iucv_sk(sk)->parent);
669 iucv_sk(sk)->parent = NULL;
673 struct sock *iucv_accept_dequeue(struct sock *parent, struct socket *newsock)
675 struct iucv_sock *isk, *n;
678 list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) {
679 sk = (struct sock *) isk;
682 if (sk->sk_state == IUCV_CLOSED) {
683 iucv_accept_unlink(sk);
688 if (sk->sk_state == IUCV_CONNECTED ||
689 sk->sk_state == IUCV_DISCONN ||
691 iucv_accept_unlink(sk);
693 sock_graft(sk, newsock);
704 static void __iucv_auto_name(struct iucv_sock *iucv)
708 sprintf(name, "%08x", atomic_inc_return(&iucv_sk_list.autobind_name));
709 while (__iucv_get_sock_by_name(name)) {
710 sprintf(name, "%08x",
711 atomic_inc_return(&iucv_sk_list.autobind_name));
713 memcpy(iucv->src_name, name, 8);
716 /* Bind an unbound socket */
717 static int iucv_sock_bind(struct socket *sock, struct sockaddr *addr,
720 struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
721 struct sock *sk = sock->sk;
722 struct iucv_sock *iucv;
724 struct net_device *dev;
727 /* Verify the input sockaddr */
728 if (addr_len < sizeof(struct sockaddr_iucv) ||
729 addr->sa_family != AF_IUCV)
733 if (sk->sk_state != IUCV_OPEN) {
738 write_lock_bh(&iucv_sk_list.lock);
741 if (__iucv_get_sock_by_name(sa->siucv_name)) {
748 /* Bind the socket */
750 if (!memcmp(sa->siucv_user_id, iucv_userid, 8))
751 goto vm_bind; /* VM IUCV transport */
753 /* try hiper transport */
754 memcpy(uid, sa->siucv_user_id, sizeof(uid));
757 for_each_netdev_rcu(&init_net, dev) {
758 if (!memcmp(dev->perm_addr, uid, 8)) {
759 memcpy(iucv->src_user_id, sa->siucv_user_id, 8);
760 /* Check for unitialized siucv_name */
761 if (strncmp(sa->siucv_name, " ", 8) == 0)
762 __iucv_auto_name(iucv);
764 memcpy(iucv->src_name, sa->siucv_name, 8);
765 sk->sk_bound_dev_if = dev->ifindex;
768 sk->sk_state = IUCV_BOUND;
769 iucv->transport = AF_IUCV_TRANS_HIPER;
771 iucv->msglimit = IUCV_HIPER_MSGLIM_DEFAULT;
779 /* use local userid for backward compat */
780 memcpy(iucv->src_name, sa->siucv_name, 8);
781 memcpy(iucv->src_user_id, iucv_userid, 8);
782 sk->sk_state = IUCV_BOUND;
783 iucv->transport = AF_IUCV_TRANS_IUCV;
785 iucv->msglimit = IUCV_QUEUELEN_DEFAULT;
788 /* found no dev to bind */
791 /* Release the socket list lock */
792 write_unlock_bh(&iucv_sk_list.lock);
798 /* Automatically bind an unbound socket */
799 static int iucv_sock_autobind(struct sock *sk)
801 struct iucv_sock *iucv = iucv_sk(sk);
804 if (unlikely(!pr_iucv))
807 memcpy(iucv->src_user_id, iucv_userid, 8);
809 write_lock_bh(&iucv_sk_list.lock);
810 __iucv_auto_name(iucv);
811 write_unlock_bh(&iucv_sk_list.lock);
814 iucv->msglimit = IUCV_QUEUELEN_DEFAULT;
819 static int afiucv_path_connect(struct socket *sock, struct sockaddr *addr)
821 struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
822 struct sock *sk = sock->sk;
823 struct iucv_sock *iucv = iucv_sk(sk);
824 unsigned char user_data[16];
827 high_nmcpy(user_data, sa->siucv_name);
828 low_nmcpy(user_data, iucv->src_name);
829 ASCEBC(user_data, sizeof(user_data));
832 iucv->path = iucv_path_alloc(iucv->msglimit,
833 IUCV_IPRMDATA, GFP_KERNEL);
838 err = pr_iucv->path_connect(iucv->path, &af_iucv_handler,
839 sa->siucv_user_id, NULL, user_data,
842 iucv_path_free(iucv->path);
845 case 0x0b: /* Target communicator is not logged on */
848 case 0x0d: /* Max connections for this guest exceeded */
849 case 0x0e: /* Max connections for target guest exceeded */
852 case 0x0f: /* Missing IUCV authorization */
864 /* Connect an unconnected socket */
865 static int iucv_sock_connect(struct socket *sock, struct sockaddr *addr,
868 struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
869 struct sock *sk = sock->sk;
870 struct iucv_sock *iucv = iucv_sk(sk);
873 if (alen < sizeof(struct sockaddr_iucv) || addr->sa_family != AF_IUCV)
876 if (sk->sk_state != IUCV_OPEN && sk->sk_state != IUCV_BOUND)
879 if (sk->sk_state == IUCV_OPEN &&
880 iucv->transport == AF_IUCV_TRANS_HIPER)
881 return -EBADFD; /* explicit bind required */
883 if (sk->sk_type != SOCK_STREAM && sk->sk_type != SOCK_SEQPACKET)
886 if (sk->sk_state == IUCV_OPEN) {
887 err = iucv_sock_autobind(sk);
894 /* Set the destination information */
895 memcpy(iucv->dst_user_id, sa->siucv_user_id, 8);
896 memcpy(iucv->dst_name, sa->siucv_name, 8);
898 if (iucv->transport == AF_IUCV_TRANS_HIPER)
899 err = iucv_send_ctrl(sock->sk, AF_IUCV_FLAG_SYN);
901 err = afiucv_path_connect(sock, addr);
905 if (sk->sk_state != IUCV_CONNECTED)
906 err = iucv_sock_wait(sk, iucv_sock_in_state(sk, IUCV_CONNECTED,
908 sock_sndtimeo(sk, flags & O_NONBLOCK));
910 if (sk->sk_state == IUCV_DISCONN || sk->sk_state == IUCV_CLOSED)
913 if (err && iucv->transport == AF_IUCV_TRANS_IUCV)
914 iucv_sever_path(sk, 0);
921 /* Move a socket into listening state. */
922 static int iucv_sock_listen(struct socket *sock, int backlog)
924 struct sock *sk = sock->sk;
930 if (sk->sk_state != IUCV_BOUND)
933 if (sock->type != SOCK_STREAM && sock->type != SOCK_SEQPACKET)
936 sk->sk_max_ack_backlog = backlog;
937 sk->sk_ack_backlog = 0;
938 sk->sk_state = IUCV_LISTEN;
946 /* Accept a pending connection */
947 static int iucv_sock_accept(struct socket *sock, struct socket *newsock,
948 int flags, bool kern)
950 DECLARE_WAITQUEUE(wait, current);
951 struct sock *sk = sock->sk, *nsk;
955 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
957 if (sk->sk_state != IUCV_LISTEN) {
962 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
964 /* Wait for an incoming connection */
965 add_wait_queue_exclusive(sk_sleep(sk), &wait);
966 while (!(nsk = iucv_accept_dequeue(sk, newsock))) {
967 set_current_state(TASK_INTERRUPTIBLE);
974 timeo = schedule_timeout(timeo);
975 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
977 if (sk->sk_state != IUCV_LISTEN) {
982 if (signal_pending(current)) {
983 err = sock_intr_errno(timeo);
988 set_current_state(TASK_RUNNING);
989 remove_wait_queue(sk_sleep(sk), &wait);
994 newsock->state = SS_CONNECTED;
1001 static int iucv_sock_getname(struct socket *sock, struct sockaddr *addr,
1004 struct sockaddr_iucv *siucv = (struct sockaddr_iucv *) addr;
1005 struct sock *sk = sock->sk;
1006 struct iucv_sock *iucv = iucv_sk(sk);
1008 addr->sa_family = AF_IUCV;
1011 memcpy(siucv->siucv_user_id, iucv->dst_user_id, 8);
1012 memcpy(siucv->siucv_name, iucv->dst_name, 8);
1014 memcpy(siucv->siucv_user_id, iucv->src_user_id, 8);
1015 memcpy(siucv->siucv_name, iucv->src_name, 8);
1017 memset(&siucv->siucv_port, 0, sizeof(siucv->siucv_port));
1018 memset(&siucv->siucv_addr, 0, sizeof(siucv->siucv_addr));
1019 memset(&siucv->siucv_nodeid, 0, sizeof(siucv->siucv_nodeid));
1021 return sizeof(struct sockaddr_iucv);
1025 * iucv_send_iprm() - Send socket data in parameter list of an iucv message.
1027 * @msg: Pointer to a struct iucv_message
1028 * @skb: The socket data to send, skb->len MUST BE <= 7
1030 * Send the socket data in the parameter list in the iucv message
1031 * (IUCV_IPRMDATA). The socket data is stored at index 0 to 6 in the parameter
1032 * list and the socket data len at index 7 (last byte).
1033 * See also iucv_msg_length().
1035 * Returns the error code from the iucv_message_send() call.
1037 static int iucv_send_iprm(struct iucv_path *path, struct iucv_message *msg,
1038 struct sk_buff *skb)
1042 memcpy(prmdata, (void *) skb->data, skb->len);
1043 prmdata[7] = 0xff - (u8) skb->len;
1044 return pr_iucv->message_send(path, msg, IUCV_IPRMDATA, 0,
1045 (void *) prmdata, 8);
1048 static int iucv_sock_sendmsg(struct socket *sock, struct msghdr *msg,
1051 struct sock *sk = sock->sk;
1052 struct iucv_sock *iucv = iucv_sk(sk);
1053 size_t headroom = 0;
1055 struct sk_buff *skb;
1056 struct iucv_message txmsg = {0};
1057 struct cmsghdr *cmsg;
1063 int noblock = msg->msg_flags & MSG_DONTWAIT;
1065 err = sock_error(sk);
1069 if (msg->msg_flags & MSG_OOB)
1072 /* SOCK_SEQPACKET: we do not support segmented records */
1073 if (sk->sk_type == SOCK_SEQPACKET && !(msg->msg_flags & MSG_EOR))
1078 if (sk->sk_shutdown & SEND_SHUTDOWN) {
1083 /* Return if the socket is not in connected state */
1084 if (sk->sk_state != IUCV_CONNECTED) {
1089 /* initialize defaults */
1090 cmsg_done = 0; /* check for duplicate headers */
1093 /* iterate over control messages */
1094 for_each_cmsghdr(cmsg, msg) {
1095 if (!CMSG_OK(msg, cmsg)) {
1100 if (cmsg->cmsg_level != SOL_IUCV)
1103 if (cmsg->cmsg_type & cmsg_done) {
1107 cmsg_done |= cmsg->cmsg_type;
1109 switch (cmsg->cmsg_type) {
1110 case SCM_IUCV_TRGCLS:
1111 if (cmsg->cmsg_len != CMSG_LEN(TRGCLS_SIZE)) {
1116 /* set iucv message target class */
1117 memcpy(&txmsg.class,
1118 (void *) CMSG_DATA(cmsg), TRGCLS_SIZE);
1128 /* allocate one skb for each iucv message:
1129 * this is fine for SOCK_SEQPACKET (unless we want to support
1130 * segmented records using the MSG_EOR flag), but
1131 * for SOCK_STREAM we might want to improve it in future */
1132 if (iucv->transport == AF_IUCV_TRANS_HIPER) {
1133 headroom = sizeof(struct af_iucv_trans_hdr) + ETH_HLEN;
1136 if (len < PAGE_SIZE) {
1139 /* In nonlinear "classic" iucv skb,
1140 * reserve space for iucv_array
1142 headroom = sizeof(struct iucv_array) *
1143 (MAX_SKB_FRAGS + 1);
1144 linear = PAGE_SIZE - headroom;
1147 skb = sock_alloc_send_pskb(sk, headroom + linear, len - linear,
1152 skb_reserve(skb, headroom);
1153 skb_put(skb, linear);
1155 skb->data_len = len - linear;
1156 err = skb_copy_datagram_from_iter(skb, 0, &msg->msg_iter, len);
1160 /* wait if outstanding messages for iucv path has reached */
1161 timeo = sock_sndtimeo(sk, noblock);
1162 err = iucv_sock_wait(sk, iucv_below_msglim(sk), timeo);
1166 /* return -ECONNRESET if the socket is no longer connected */
1167 if (sk->sk_state != IUCV_CONNECTED) {
1172 /* increment and save iucv message tag for msg_completion cbk */
1173 txmsg.tag = iucv->send_tag++;
1174 IUCV_SKB_CB(skb)->tag = txmsg.tag;
1176 if (iucv->transport == AF_IUCV_TRANS_HIPER) {
1177 atomic_inc(&iucv->msg_sent);
1178 err = afiucv_hs_send(&txmsg, sk, skb, 0);
1180 atomic_dec(&iucv->msg_sent);
1183 } else { /* Classic VM IUCV transport */
1184 skb_queue_tail(&iucv->send_skb_q, skb);
1186 if (((iucv->path->flags & IUCV_IPRMDATA) & iucv->flags) &&
1188 err = iucv_send_iprm(iucv->path, &txmsg, skb);
1190 /* on success: there is no message_complete callback */
1191 /* for an IPRMDATA msg; remove skb from send queue */
1193 skb_unlink(skb, &iucv->send_skb_q);
1197 /* this error should never happen since the */
1198 /* IUCV_IPRMDATA path flag is set... sever path */
1200 pr_iucv->path_sever(iucv->path, NULL);
1201 skb_unlink(skb, &iucv->send_skb_q);
1205 } else if (skb_is_nonlinear(skb)) {
1206 struct iucv_array *iba = (struct iucv_array *)skb->head;
1209 /* skip iucv_array lying in the headroom */
1210 iba[0].address = (u32)(addr_t)skb->data;
1211 iba[0].length = (u32)skb_headlen(skb);
1212 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1213 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1215 iba[i + 1].address =
1216 (u32)(addr_t)skb_frag_address(frag);
1217 iba[i + 1].length = (u32)skb_frag_size(frag);
1219 err = pr_iucv->message_send(iucv->path, &txmsg,
1221 (void *)iba, skb->len);
1222 } else { /* non-IPRM Linear skb */
1223 err = pr_iucv->message_send(iucv->path, &txmsg,
1224 0, 0, (void *)skb->data, skb->len);
1229 memcpy(user_id, iucv->dst_user_id, 8);
1231 memcpy(appl_id, iucv->dst_name, 8);
1233 "Application %s on z/VM guest %s exceeds message limit\n",
1239 skb_unlink(skb, &iucv->send_skb_q);
1254 static struct sk_buff *alloc_iucv_recv_skb(unsigned long len)
1256 size_t headroom, linear;
1257 struct sk_buff *skb;
1260 if (len < PAGE_SIZE) {
1264 headroom = sizeof(struct iucv_array) * (MAX_SKB_FRAGS + 1);
1265 linear = PAGE_SIZE - headroom;
1267 skb = alloc_skb_with_frags(headroom + linear, len - linear,
1268 0, &err, GFP_ATOMIC | GFP_DMA);
1270 "alloc of recv iucv skb len=%lu failed with errcode=%d\n",
1274 skb_reserve(skb, headroom);
1275 skb_put(skb, linear);
1277 skb->data_len = len - linear;
1282 /* iucv_process_message() - Receive a single outstanding IUCV message
1284 * Locking: must be called with message_q.lock held
1286 static void iucv_process_message(struct sock *sk, struct sk_buff *skb,
1287 struct iucv_path *path,
1288 struct iucv_message *msg)
1293 len = iucv_msg_length(msg);
1295 /* store msg target class in the second 4 bytes of skb ctrl buffer */
1296 /* Note: the first 4 bytes are reserved for msg tag */
1297 IUCV_SKB_CB(skb)->class = msg->class;
1299 /* check for special IPRM messages (e.g. iucv_sock_shutdown) */
1300 if ((msg->flags & IUCV_IPRMDATA) && len > 7) {
1301 if (memcmp(msg->rmmsg, iprm_shutdown, 8) == 0) {
1306 if (skb_is_nonlinear(skb)) {
1307 struct iucv_array *iba = (struct iucv_array *)skb->head;
1310 iba[0].address = (u32)(addr_t)skb->data;
1311 iba[0].length = (u32)skb_headlen(skb);
1312 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1313 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1315 iba[i + 1].address =
1316 (u32)(addr_t)skb_frag_address(frag);
1317 iba[i + 1].length = (u32)skb_frag_size(frag);
1319 rc = pr_iucv->message_receive(path, msg,
1321 (void *)iba, len, NULL);
1323 rc = pr_iucv->message_receive(path, msg,
1324 msg->flags & IUCV_IPRMDATA,
1325 skb->data, len, NULL);
1331 WARN_ON_ONCE(skb->len != len);
1334 IUCV_SKB_CB(skb)->offset = 0;
1335 if (sk_filter(sk, skb)) {
1336 atomic_inc(&sk->sk_drops); /* skb rejected by filter */
1340 if (__sock_queue_rcv_skb(sk, skb)) /* handle rcv queue full */
1341 skb_queue_tail(&iucv_sk(sk)->backlog_skb_q, skb);
1344 /* iucv_process_message_q() - Process outstanding IUCV messages
1346 * Locking: must be called with message_q.lock held
1348 static void iucv_process_message_q(struct sock *sk)
1350 struct iucv_sock *iucv = iucv_sk(sk);
1351 struct sk_buff *skb;
1352 struct sock_msg_q *p, *n;
1354 list_for_each_entry_safe(p, n, &iucv->message_q.list, list) {
1355 skb = alloc_iucv_recv_skb(iucv_msg_length(&p->msg));
1358 iucv_process_message(sk, skb, p->path, &p->msg);
1361 if (!skb_queue_empty(&iucv->backlog_skb_q))
1366 static int iucv_sock_recvmsg(struct socket *sock, struct msghdr *msg,
1367 size_t len, int flags)
1369 int noblock = flags & MSG_DONTWAIT;
1370 struct sock *sk = sock->sk;
1371 struct iucv_sock *iucv = iucv_sk(sk);
1372 unsigned int copied, rlen;
1373 struct sk_buff *skb, *rskb, *cskb;
1377 if ((sk->sk_state == IUCV_DISCONN) &&
1378 skb_queue_empty(&iucv->backlog_skb_q) &&
1379 skb_queue_empty(&sk->sk_receive_queue) &&
1380 list_empty(&iucv->message_q.list))
1383 if (flags & (MSG_OOB))
1386 /* receive/dequeue next skb:
1387 * the function understands MSG_PEEK and, thus, does not dequeue skb */
1388 skb = skb_recv_datagram(sk, flags, noblock, &err);
1390 if (sk->sk_shutdown & RCV_SHUTDOWN)
1395 offset = IUCV_SKB_CB(skb)->offset;
1396 rlen = skb->len - offset; /* real length of skb */
1397 copied = min_t(unsigned int, rlen, len);
1399 sk->sk_shutdown = sk->sk_shutdown | RCV_SHUTDOWN;
1402 if (skb_copy_datagram_msg(cskb, offset, msg, copied)) {
1403 if (!(flags & MSG_PEEK))
1404 skb_queue_head(&sk->sk_receive_queue, skb);
1408 /* SOCK_SEQPACKET: set MSG_TRUNC if recv buf size is too small */
1409 if (sk->sk_type == SOCK_SEQPACKET) {
1411 msg->msg_flags |= MSG_TRUNC;
1412 /* each iucv message contains a complete record */
1413 msg->msg_flags |= MSG_EOR;
1416 /* create control message to store iucv msg target class:
1417 * get the trgcls from the control buffer of the skb due to
1418 * fragmentation of original iucv message. */
1419 err = put_cmsg(msg, SOL_IUCV, SCM_IUCV_TRGCLS,
1420 sizeof(IUCV_SKB_CB(skb)->class),
1421 (void *)&IUCV_SKB_CB(skb)->class);
1423 if (!(flags & MSG_PEEK))
1424 skb_queue_head(&sk->sk_receive_queue, skb);
1428 /* Mark read part of skb as used */
1429 if (!(flags & MSG_PEEK)) {
1431 /* SOCK_STREAM: re-queue skb if it contains unreceived data */
1432 if (sk->sk_type == SOCK_STREAM) {
1433 if (copied < rlen) {
1434 IUCV_SKB_CB(skb)->offset = offset + copied;
1435 skb_queue_head(&sk->sk_receive_queue, skb);
1441 if (iucv->transport == AF_IUCV_TRANS_HIPER) {
1442 atomic_inc(&iucv->msg_recv);
1443 if (atomic_read(&iucv->msg_recv) > iucv->msglimit) {
1445 iucv_sock_close(sk);
1450 /* Queue backlog skbs */
1451 spin_lock_bh(&iucv->message_q.lock);
1452 rskb = skb_dequeue(&iucv->backlog_skb_q);
1454 IUCV_SKB_CB(rskb)->offset = 0;
1455 if (__sock_queue_rcv_skb(sk, rskb)) {
1456 /* handle rcv queue full */
1457 skb_queue_head(&iucv->backlog_skb_q,
1461 rskb = skb_dequeue(&iucv->backlog_skb_q);
1463 if (skb_queue_empty(&iucv->backlog_skb_q)) {
1464 if (!list_empty(&iucv->message_q.list))
1465 iucv_process_message_q(sk);
1466 if (atomic_read(&iucv->msg_recv) >=
1467 iucv->msglimit / 2) {
1468 err = iucv_send_ctrl(sk, AF_IUCV_FLAG_WIN);
1470 sk->sk_state = IUCV_DISCONN;
1471 sk->sk_state_change(sk);
1475 spin_unlock_bh(&iucv->message_q.lock);
1479 /* SOCK_SEQPACKET: return real length if MSG_TRUNC is set */
1480 if (sk->sk_type == SOCK_SEQPACKET && (flags & MSG_TRUNC))
1486 static inline __poll_t iucv_accept_poll(struct sock *parent)
1488 struct iucv_sock *isk, *n;
1491 list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) {
1492 sk = (struct sock *) isk;
1494 if (sk->sk_state == IUCV_CONNECTED)
1495 return EPOLLIN | EPOLLRDNORM;
1501 __poll_t iucv_sock_poll(struct file *file, struct socket *sock,
1504 struct sock *sk = sock->sk;
1507 sock_poll_wait(file, sock, wait);
1509 if (sk->sk_state == IUCV_LISTEN)
1510 return iucv_accept_poll(sk);
1512 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
1514 (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? EPOLLPRI : 0);
1516 if (sk->sk_shutdown & RCV_SHUTDOWN)
1519 if (sk->sk_shutdown == SHUTDOWN_MASK)
1522 if (!skb_queue_empty(&sk->sk_receive_queue) ||
1523 (sk->sk_shutdown & RCV_SHUTDOWN))
1524 mask |= EPOLLIN | EPOLLRDNORM;
1526 if (sk->sk_state == IUCV_CLOSED)
1529 if (sk->sk_state == IUCV_DISCONN)
1532 if (sock_writeable(sk) && iucv_below_msglim(sk))
1533 mask |= EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND;
1535 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
1540 static int iucv_sock_shutdown(struct socket *sock, int how)
1542 struct sock *sk = sock->sk;
1543 struct iucv_sock *iucv = iucv_sk(sk);
1544 struct iucv_message txmsg;
1549 if ((how & ~SHUTDOWN_MASK) || !how)
1553 switch (sk->sk_state) {
1564 if (how == SEND_SHUTDOWN || how == SHUTDOWN_MASK) {
1565 if (iucv->transport == AF_IUCV_TRANS_IUCV) {
1568 err = pr_iucv->message_send(iucv->path, &txmsg,
1569 IUCV_IPRMDATA, 0, (void *) iprm_shutdown, 8);
1584 iucv_send_ctrl(sk, AF_IUCV_FLAG_SHT);
1587 sk->sk_shutdown |= how;
1588 if (how == RCV_SHUTDOWN || how == SHUTDOWN_MASK) {
1589 if ((iucv->transport == AF_IUCV_TRANS_IUCV) &&
1591 err = pr_iucv->path_quiesce(iucv->path, NULL);
1594 /* skb_queue_purge(&sk->sk_receive_queue); */
1596 skb_queue_purge(&sk->sk_receive_queue);
1599 /* Wake up anyone sleeping in poll */
1600 sk->sk_state_change(sk);
1607 static int iucv_sock_release(struct socket *sock)
1609 struct sock *sk = sock->sk;
1615 iucv_sock_close(sk);
1622 /* getsockopt and setsockopt */
1623 static int iucv_sock_setsockopt(struct socket *sock, int level, int optname,
1624 char __user *optval, unsigned int optlen)
1626 struct sock *sk = sock->sk;
1627 struct iucv_sock *iucv = iucv_sk(sk);
1631 if (level != SOL_IUCV)
1632 return -ENOPROTOOPT;
1634 if (optlen < sizeof(int))
1637 if (get_user(val, (int __user *) optval))
1644 case SO_IPRMDATA_MSG:
1646 iucv->flags |= IUCV_IPRMDATA;
1648 iucv->flags &= ~IUCV_IPRMDATA;
1651 switch (sk->sk_state) {
1654 if (val < 1 || val > (u16)(~0))
1657 iucv->msglimit = val;
1673 static int iucv_sock_getsockopt(struct socket *sock, int level, int optname,
1674 char __user *optval, int __user *optlen)
1676 struct sock *sk = sock->sk;
1677 struct iucv_sock *iucv = iucv_sk(sk);
1681 if (level != SOL_IUCV)
1682 return -ENOPROTOOPT;
1684 if (get_user(len, optlen))
1690 len = min_t(unsigned int, len, sizeof(int));
1693 case SO_IPRMDATA_MSG:
1694 val = (iucv->flags & IUCV_IPRMDATA) ? 1 : 0;
1698 val = (iucv->path != NULL) ? iucv->path->msglim /* connected */
1699 : iucv->msglimit; /* default */
1703 if (sk->sk_state == IUCV_OPEN)
1705 val = (iucv->hs_dev) ? iucv->hs_dev->mtu -
1706 sizeof(struct af_iucv_trans_hdr) - ETH_HLEN :
1710 return -ENOPROTOOPT;
1713 if (put_user(len, optlen))
1715 if (copy_to_user(optval, &val, len))
1722 /* Callback wrappers - called from iucv base support */
1723 static int iucv_callback_connreq(struct iucv_path *path,
1724 u8 ipvmid[8], u8 ipuser[16])
1726 unsigned char user_data[16];
1727 unsigned char nuser_data[16];
1728 unsigned char src_name[8];
1729 struct sock *sk, *nsk;
1730 struct iucv_sock *iucv, *niucv;
1733 memcpy(src_name, ipuser, 8);
1734 EBCASC(src_name, 8);
1735 /* Find out if this path belongs to af_iucv. */
1736 read_lock(&iucv_sk_list.lock);
1739 sk_for_each(sk, &iucv_sk_list.head)
1740 if (sk->sk_state == IUCV_LISTEN &&
1741 !memcmp(&iucv_sk(sk)->src_name, src_name, 8)) {
1743 * Found a listening socket with
1744 * src_name == ipuser[0-7].
1749 read_unlock(&iucv_sk_list.lock);
1751 /* No socket found, not one of our paths. */
1756 /* Check if parent socket is listening */
1757 low_nmcpy(user_data, iucv->src_name);
1758 high_nmcpy(user_data, iucv->dst_name);
1759 ASCEBC(user_data, sizeof(user_data));
1760 if (sk->sk_state != IUCV_LISTEN) {
1761 err = pr_iucv->path_sever(path, user_data);
1762 iucv_path_free(path);
1766 /* Check for backlog size */
1767 if (sk_acceptq_is_full(sk)) {
1768 err = pr_iucv->path_sever(path, user_data);
1769 iucv_path_free(path);
1773 /* Create the new socket */
1774 nsk = iucv_sock_alloc(NULL, sk->sk_type, GFP_ATOMIC, 0);
1776 err = pr_iucv->path_sever(path, user_data);
1777 iucv_path_free(path);
1781 niucv = iucv_sk(nsk);
1782 iucv_sock_init(nsk, sk);
1784 /* Set the new iucv_sock */
1785 memcpy(niucv->dst_name, ipuser + 8, 8);
1786 EBCASC(niucv->dst_name, 8);
1787 memcpy(niucv->dst_user_id, ipvmid, 8);
1788 memcpy(niucv->src_name, iucv->src_name, 8);
1789 memcpy(niucv->src_user_id, iucv->src_user_id, 8);
1792 /* Call iucv_accept */
1793 high_nmcpy(nuser_data, ipuser + 8);
1794 memcpy(nuser_data + 8, niucv->src_name, 8);
1795 ASCEBC(nuser_data + 8, 8);
1797 /* set message limit for path based on msglimit of accepting socket */
1798 niucv->msglimit = iucv->msglimit;
1799 path->msglim = iucv->msglimit;
1800 err = pr_iucv->path_accept(path, &af_iucv_handler, nuser_data, nsk);
1802 iucv_sever_path(nsk, 1);
1803 iucv_sock_kill(nsk);
1807 iucv_accept_enqueue(sk, nsk);
1809 /* Wake up accept */
1810 nsk->sk_state = IUCV_CONNECTED;
1811 sk->sk_data_ready(sk);
1818 static void iucv_callback_connack(struct iucv_path *path, u8 ipuser[16])
1820 struct sock *sk = path->private;
1822 sk->sk_state = IUCV_CONNECTED;
1823 sk->sk_state_change(sk);
1826 static void iucv_callback_rx(struct iucv_path *path, struct iucv_message *msg)
1828 struct sock *sk = path->private;
1829 struct iucv_sock *iucv = iucv_sk(sk);
1830 struct sk_buff *skb;
1831 struct sock_msg_q *save_msg;
1834 if (sk->sk_shutdown & RCV_SHUTDOWN) {
1835 pr_iucv->message_reject(path, msg);
1839 spin_lock(&iucv->message_q.lock);
1841 if (!list_empty(&iucv->message_q.list) ||
1842 !skb_queue_empty(&iucv->backlog_skb_q))
1845 len = atomic_read(&sk->sk_rmem_alloc);
1846 len += SKB_TRUESIZE(iucv_msg_length(msg));
1847 if (len > sk->sk_rcvbuf)
1850 skb = alloc_iucv_recv_skb(iucv_msg_length(msg));
1854 iucv_process_message(sk, skb, path, msg);
1858 save_msg = kzalloc(sizeof(struct sock_msg_q), GFP_ATOMIC | GFP_DMA);
1861 save_msg->path = path;
1862 save_msg->msg = *msg;
1864 list_add_tail(&save_msg->list, &iucv->message_q.list);
1867 spin_unlock(&iucv->message_q.lock);
1870 static void iucv_callback_txdone(struct iucv_path *path,
1871 struct iucv_message *msg)
1873 struct sock *sk = path->private;
1874 struct sk_buff *this = NULL;
1875 struct sk_buff_head *list = &iucv_sk(sk)->send_skb_q;
1876 struct sk_buff *list_skb = list->next;
1877 unsigned long flags;
1880 if (!skb_queue_empty(list)) {
1881 spin_lock_irqsave(&list->lock, flags);
1883 while (list_skb != (struct sk_buff *)list) {
1884 if (msg->tag == IUCV_SKB_CB(list_skb)->tag) {
1888 list_skb = list_skb->next;
1891 __skb_unlink(this, list);
1893 spin_unlock_irqrestore(&list->lock, flags);
1897 /* wake up any process waiting for sending */
1898 iucv_sock_wake_msglim(sk);
1902 if (sk->sk_state == IUCV_CLOSING) {
1903 if (skb_queue_empty(&iucv_sk(sk)->send_skb_q)) {
1904 sk->sk_state = IUCV_CLOSED;
1905 sk->sk_state_change(sk);
1912 static void iucv_callback_connrej(struct iucv_path *path, u8 ipuser[16])
1914 struct sock *sk = path->private;
1916 if (sk->sk_state == IUCV_CLOSED)
1920 iucv_sever_path(sk, 1);
1921 sk->sk_state = IUCV_DISCONN;
1923 sk->sk_state_change(sk);
1927 /* called if the other communication side shuts down its RECV direction;
1928 * in turn, the callback sets SEND_SHUTDOWN to disable sending of data.
1930 static void iucv_callback_shutdown(struct iucv_path *path, u8 ipuser[16])
1932 struct sock *sk = path->private;
1935 if (sk->sk_state != IUCV_CLOSED) {
1936 sk->sk_shutdown |= SEND_SHUTDOWN;
1937 sk->sk_state_change(sk);
1942 /***************** HiperSockets transport callbacks ********************/
1943 static void afiucv_swap_src_dest(struct sk_buff *skb)
1945 struct af_iucv_trans_hdr *trans_hdr = iucv_trans_hdr(skb);
1949 ASCEBC(trans_hdr->destUserID, sizeof(trans_hdr->destUserID));
1950 ASCEBC(trans_hdr->destAppName, sizeof(trans_hdr->destAppName));
1951 ASCEBC(trans_hdr->srcUserID, sizeof(trans_hdr->srcUserID));
1952 ASCEBC(trans_hdr->srcAppName, sizeof(trans_hdr->srcAppName));
1953 memcpy(tmpID, trans_hdr->srcUserID, 8);
1954 memcpy(tmpName, trans_hdr->srcAppName, 8);
1955 memcpy(trans_hdr->srcUserID, trans_hdr->destUserID, 8);
1956 memcpy(trans_hdr->srcAppName, trans_hdr->destAppName, 8);
1957 memcpy(trans_hdr->destUserID, tmpID, 8);
1958 memcpy(trans_hdr->destAppName, tmpName, 8);
1959 skb_push(skb, ETH_HLEN);
1960 memset(skb->data, 0, ETH_HLEN);
1964 * afiucv_hs_callback_syn - react on received SYN
1966 static int afiucv_hs_callback_syn(struct sock *sk, struct sk_buff *skb)
1968 struct af_iucv_trans_hdr *trans_hdr = iucv_trans_hdr(skb);
1970 struct iucv_sock *iucv, *niucv;
1975 /* no sock - connection refused */
1976 afiucv_swap_src_dest(skb);
1977 trans_hdr->flags = AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN;
1978 err = dev_queue_xmit(skb);
1982 nsk = iucv_sock_alloc(NULL, sk->sk_type, GFP_ATOMIC, 0);
1984 if ((sk->sk_state != IUCV_LISTEN) ||
1985 sk_acceptq_is_full(sk) ||
1987 /* error on server socket - connection refused */
1988 afiucv_swap_src_dest(skb);
1989 trans_hdr->flags = AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN;
1990 err = dev_queue_xmit(skb);
1991 iucv_sock_kill(nsk);
1996 niucv = iucv_sk(nsk);
1997 iucv_sock_init(nsk, sk);
1998 niucv->transport = AF_IUCV_TRANS_HIPER;
1999 niucv->msglimit = iucv->msglimit;
2000 if (!trans_hdr->window)
2001 niucv->msglimit_peer = IUCV_HIPER_MSGLIM_DEFAULT;
2003 niucv->msglimit_peer = trans_hdr->window;
2004 memcpy(niucv->dst_name, trans_hdr->srcAppName, 8);
2005 memcpy(niucv->dst_user_id, trans_hdr->srcUserID, 8);
2006 memcpy(niucv->src_name, iucv->src_name, 8);
2007 memcpy(niucv->src_user_id, iucv->src_user_id, 8);
2008 nsk->sk_bound_dev_if = sk->sk_bound_dev_if;
2009 niucv->hs_dev = iucv->hs_dev;
2010 dev_hold(niucv->hs_dev);
2011 afiucv_swap_src_dest(skb);
2012 trans_hdr->flags = AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_ACK;
2013 trans_hdr->window = niucv->msglimit;
2014 /* if receiver acks the xmit connection is established */
2015 err = dev_queue_xmit(skb);
2017 iucv_accept_enqueue(sk, nsk);
2018 nsk->sk_state = IUCV_CONNECTED;
2019 sk->sk_data_ready(sk);
2021 iucv_sock_kill(nsk);
2025 return NET_RX_SUCCESS;
2029 * afiucv_hs_callback_synack() - react on received SYN-ACK
2031 static int afiucv_hs_callback_synack(struct sock *sk, struct sk_buff *skb)
2033 struct iucv_sock *iucv = iucv_sk(sk);
2037 if (sk->sk_state != IUCV_BOUND)
2040 iucv->msglimit_peer = iucv_trans_hdr(skb)->window;
2041 sk->sk_state = IUCV_CONNECTED;
2042 sk->sk_state_change(sk);
2046 return NET_RX_SUCCESS;
2050 * afiucv_hs_callback_synfin() - react on received SYN_FIN
2052 static int afiucv_hs_callback_synfin(struct sock *sk, struct sk_buff *skb)
2054 struct iucv_sock *iucv = iucv_sk(sk);
2058 if (sk->sk_state != IUCV_BOUND)
2061 sk->sk_state = IUCV_DISCONN;
2062 sk->sk_state_change(sk);
2066 return NET_RX_SUCCESS;
2070 * afiucv_hs_callback_fin() - react on received FIN
2072 static int afiucv_hs_callback_fin(struct sock *sk, struct sk_buff *skb)
2074 struct iucv_sock *iucv = iucv_sk(sk);
2076 /* other end of connection closed */
2080 if (sk->sk_state == IUCV_CONNECTED) {
2081 sk->sk_state = IUCV_DISCONN;
2082 sk->sk_state_change(sk);
2087 return NET_RX_SUCCESS;
2091 * afiucv_hs_callback_win() - react on received WIN
2093 static int afiucv_hs_callback_win(struct sock *sk, struct sk_buff *skb)
2095 struct iucv_sock *iucv = iucv_sk(sk);
2098 return NET_RX_SUCCESS;
2100 if (sk->sk_state != IUCV_CONNECTED)
2101 return NET_RX_SUCCESS;
2103 atomic_sub(iucv_trans_hdr(skb)->window, &iucv->msg_sent);
2104 iucv_sock_wake_msglim(sk);
2105 return NET_RX_SUCCESS;
2109 * afiucv_hs_callback_rx() - react on received data
2111 static int afiucv_hs_callback_rx(struct sock *sk, struct sk_buff *skb)
2113 struct iucv_sock *iucv = iucv_sk(sk);
2117 return NET_RX_SUCCESS;
2120 if (sk->sk_state != IUCV_CONNECTED) {
2122 return NET_RX_SUCCESS;
2125 if (sk->sk_shutdown & RCV_SHUTDOWN) {
2127 return NET_RX_SUCCESS;
2130 /* write stuff from iucv_msg to skb cb */
2131 skb_pull(skb, sizeof(struct af_iucv_trans_hdr));
2132 skb_reset_transport_header(skb);
2133 skb_reset_network_header(skb);
2134 IUCV_SKB_CB(skb)->offset = 0;
2135 if (sk_filter(sk, skb)) {
2136 atomic_inc(&sk->sk_drops); /* skb rejected by filter */
2138 return NET_RX_SUCCESS;
2141 spin_lock(&iucv->message_q.lock);
2142 if (skb_queue_empty(&iucv->backlog_skb_q)) {
2143 if (__sock_queue_rcv_skb(sk, skb))
2144 /* handle rcv queue full */
2145 skb_queue_tail(&iucv->backlog_skb_q, skb);
2147 skb_queue_tail(&iucv_sk(sk)->backlog_skb_q, skb);
2148 spin_unlock(&iucv->message_q.lock);
2149 return NET_RX_SUCCESS;
2153 * afiucv_hs_rcv() - base function for arriving data through HiperSockets
2155 * called from netif RX softirq
2157 static int afiucv_hs_rcv(struct sk_buff *skb, struct net_device *dev,
2158 struct packet_type *pt, struct net_device *orig_dev)
2161 struct iucv_sock *iucv;
2162 struct af_iucv_trans_hdr *trans_hdr;
2163 int err = NET_RX_SUCCESS;
2166 if (!pskb_may_pull(skb, sizeof(*trans_hdr))) {
2167 WARN_ONCE(1, "AF_IUCV failed to receive skb, len=%u", skb->len);
2169 return NET_RX_SUCCESS;
2172 trans_hdr = iucv_trans_hdr(skb);
2173 EBCASC(trans_hdr->destAppName, sizeof(trans_hdr->destAppName));
2174 EBCASC(trans_hdr->destUserID, sizeof(trans_hdr->destUserID));
2175 EBCASC(trans_hdr->srcAppName, sizeof(trans_hdr->srcAppName));
2176 EBCASC(trans_hdr->srcUserID, sizeof(trans_hdr->srcUserID));
2177 memset(nullstring, 0, sizeof(nullstring));
2180 read_lock(&iucv_sk_list.lock);
2181 sk_for_each(sk, &iucv_sk_list.head) {
2182 if (trans_hdr->flags == AF_IUCV_FLAG_SYN) {
2183 if ((!memcmp(&iucv_sk(sk)->src_name,
2184 trans_hdr->destAppName, 8)) &&
2185 (!memcmp(&iucv_sk(sk)->src_user_id,
2186 trans_hdr->destUserID, 8)) &&
2187 (!memcmp(&iucv_sk(sk)->dst_name, nullstring, 8)) &&
2188 (!memcmp(&iucv_sk(sk)->dst_user_id,
2194 if ((!memcmp(&iucv_sk(sk)->src_name,
2195 trans_hdr->destAppName, 8)) &&
2196 (!memcmp(&iucv_sk(sk)->src_user_id,
2197 trans_hdr->destUserID, 8)) &&
2198 (!memcmp(&iucv_sk(sk)->dst_name,
2199 trans_hdr->srcAppName, 8)) &&
2200 (!memcmp(&iucv_sk(sk)->dst_user_id,
2201 trans_hdr->srcUserID, 8))) {
2207 read_unlock(&iucv_sk_list.lock);
2212 how should we send with no sock
2213 1) send without sock no send rc checking?
2214 2) introduce default sock to handle this cases
2216 SYN -> send SYN|ACK in good case, send SYN|FIN in bad case
2218 SYN|ACK, SYN|FIN, FIN -> no action? */
2220 switch (trans_hdr->flags) {
2221 case AF_IUCV_FLAG_SYN:
2222 /* connect request */
2223 err = afiucv_hs_callback_syn(sk, skb);
2225 case (AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_ACK):
2226 /* connect request confirmed */
2227 err = afiucv_hs_callback_synack(sk, skb);
2229 case (AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN):
2230 /* connect request refused */
2231 err = afiucv_hs_callback_synfin(sk, skb);
2233 case (AF_IUCV_FLAG_FIN):
2235 err = afiucv_hs_callback_fin(sk, skb);
2237 case (AF_IUCV_FLAG_WIN):
2238 err = afiucv_hs_callback_win(sk, skb);
2239 if (skb->len == sizeof(struct af_iucv_trans_hdr)) {
2243 /* fall through and receive non-zero length data */
2244 case (AF_IUCV_FLAG_SHT):
2245 /* shutdown request */
2246 /* fall through and receive zero length data */
2248 /* plain data frame */
2249 IUCV_SKB_CB(skb)->class = trans_hdr->iucv_hdr.class;
2250 err = afiucv_hs_callback_rx(sk, skb);
2260 * afiucv_hs_callback_txnotify() - handle send notifcations from HiperSockets
2263 static void afiucv_hs_callback_txnotify(struct sk_buff *skb,
2264 enum iucv_tx_notify n)
2266 struct sock *isk = skb->sk;
2267 struct sock *sk = NULL;
2268 struct iucv_sock *iucv = NULL;
2269 struct sk_buff_head *list;
2270 struct sk_buff *list_skb;
2271 struct sk_buff *nskb;
2272 unsigned long flags;
2274 read_lock_irqsave(&iucv_sk_list.lock, flags);
2275 sk_for_each(sk, &iucv_sk_list.head)
2280 read_unlock_irqrestore(&iucv_sk_list.lock, flags);
2282 if (!iucv || sock_flag(sk, SOCK_ZAPPED))
2285 list = &iucv->send_skb_q;
2286 spin_lock_irqsave(&list->lock, flags);
2287 if (skb_queue_empty(list))
2289 list_skb = list->next;
2290 nskb = list_skb->next;
2291 while (list_skb != (struct sk_buff *)list) {
2292 if (skb_shinfo(list_skb) == skb_shinfo(skb)) {
2295 __skb_unlink(list_skb, list);
2296 kfree_skb(list_skb);
2297 iucv_sock_wake_msglim(sk);
2299 case TX_NOTIFY_PENDING:
2300 atomic_inc(&iucv->pendings);
2302 case TX_NOTIFY_DELAYED_OK:
2303 __skb_unlink(list_skb, list);
2304 atomic_dec(&iucv->pendings);
2305 if (atomic_read(&iucv->pendings) <= 0)
2306 iucv_sock_wake_msglim(sk);
2307 kfree_skb(list_skb);
2309 case TX_NOTIFY_UNREACHABLE:
2310 case TX_NOTIFY_DELAYED_UNREACHABLE:
2311 case TX_NOTIFY_TPQFULL: /* not yet used */
2312 case TX_NOTIFY_GENERALERROR:
2313 case TX_NOTIFY_DELAYED_GENERALERROR:
2314 __skb_unlink(list_skb, list);
2315 kfree_skb(list_skb);
2316 if (sk->sk_state == IUCV_CONNECTED) {
2317 sk->sk_state = IUCV_DISCONN;
2318 sk->sk_state_change(sk);
2328 spin_unlock_irqrestore(&list->lock, flags);
2330 if (sk->sk_state == IUCV_CLOSING) {
2331 if (skb_queue_empty(&iucv_sk(sk)->send_skb_q)) {
2332 sk->sk_state = IUCV_CLOSED;
2333 sk->sk_state_change(sk);
2340 * afiucv_netdev_event: handle netdev notifier chain events
2342 static int afiucv_netdev_event(struct notifier_block *this,
2343 unsigned long event, void *ptr)
2345 struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
2347 struct iucv_sock *iucv;
2351 case NETDEV_GOING_DOWN:
2352 sk_for_each(sk, &iucv_sk_list.head) {
2354 if ((iucv->hs_dev == event_dev) &&
2355 (sk->sk_state == IUCV_CONNECTED)) {
2356 if (event == NETDEV_GOING_DOWN)
2357 iucv_send_ctrl(sk, AF_IUCV_FLAG_FIN);
2358 sk->sk_state = IUCV_DISCONN;
2359 sk->sk_state_change(sk);
2364 case NETDEV_UNREGISTER:
2371 static struct notifier_block afiucv_netdev_notifier = {
2372 .notifier_call = afiucv_netdev_event,
2375 static const struct proto_ops iucv_sock_ops = {
2377 .owner = THIS_MODULE,
2378 .release = iucv_sock_release,
2379 .bind = iucv_sock_bind,
2380 .connect = iucv_sock_connect,
2381 .listen = iucv_sock_listen,
2382 .accept = iucv_sock_accept,
2383 .getname = iucv_sock_getname,
2384 .sendmsg = iucv_sock_sendmsg,
2385 .recvmsg = iucv_sock_recvmsg,
2386 .poll = iucv_sock_poll,
2387 .ioctl = sock_no_ioctl,
2388 .mmap = sock_no_mmap,
2389 .socketpair = sock_no_socketpair,
2390 .shutdown = iucv_sock_shutdown,
2391 .setsockopt = iucv_sock_setsockopt,
2392 .getsockopt = iucv_sock_getsockopt,
2395 static const struct net_proto_family iucv_sock_family_ops = {
2397 .owner = THIS_MODULE,
2398 .create = iucv_sock_create,
2401 static struct packet_type iucv_packet_type = {
2402 .type = cpu_to_be16(ETH_P_AF_IUCV),
2403 .func = afiucv_hs_rcv,
2406 static int afiucv_iucv_init(void)
2410 err = pr_iucv->iucv_register(&af_iucv_handler, 0);
2413 /* establish dummy device */
2414 af_iucv_driver.bus = pr_iucv->bus;
2415 err = driver_register(&af_iucv_driver);
2418 af_iucv_dev = kzalloc(sizeof(struct device), GFP_KERNEL);
2423 dev_set_name(af_iucv_dev, "af_iucv");
2424 af_iucv_dev->bus = pr_iucv->bus;
2425 af_iucv_dev->parent = pr_iucv->root;
2426 af_iucv_dev->release = (void (*)(struct device *))kfree;
2427 af_iucv_dev->driver = &af_iucv_driver;
2428 err = device_register(af_iucv_dev);
2434 put_device(af_iucv_dev);
2436 driver_unregister(&af_iucv_driver);
2438 pr_iucv->iucv_unregister(&af_iucv_handler, 0);
2443 static int __init afiucv_init(void)
2447 if (MACHINE_IS_VM) {
2448 cpcmd("QUERY USERID", iucv_userid, sizeof(iucv_userid), &err);
2449 if (unlikely(err)) {
2451 err = -EPROTONOSUPPORT;
2455 pr_iucv = try_then_request_module(symbol_get(iucv_if), "iucv");
2457 printk(KERN_WARNING "iucv_if lookup failed\n");
2458 memset(&iucv_userid, 0, sizeof(iucv_userid));
2461 memset(&iucv_userid, 0, sizeof(iucv_userid));
2465 err = proto_register(&iucv_proto, 0);
2468 err = sock_register(&iucv_sock_family_ops);
2473 err = afiucv_iucv_init();
2477 register_netdevice_notifier(&afiucv_netdev_notifier);
2478 dev_add_pack(&iucv_packet_type);
2482 sock_unregister(PF_IUCV);
2484 proto_unregister(&iucv_proto);
2487 symbol_put(iucv_if);
2491 static void __exit afiucv_exit(void)
2494 device_unregister(af_iucv_dev);
2495 driver_unregister(&af_iucv_driver);
2496 pr_iucv->iucv_unregister(&af_iucv_handler, 0);
2497 symbol_put(iucv_if);
2499 unregister_netdevice_notifier(&afiucv_netdev_notifier);
2500 dev_remove_pack(&iucv_packet_type);
2501 sock_unregister(PF_IUCV);
2502 proto_unregister(&iucv_proto);
2505 module_init(afiucv_init);
2506 module_exit(afiucv_exit);
2508 MODULE_AUTHOR("Jennifer Hunt <jenhunt@us.ibm.com>");
2509 MODULE_DESCRIPTION("IUCV Sockets ver " VERSION);
2510 MODULE_VERSION(VERSION);
2511 MODULE_LICENSE("GPL");
2512 MODULE_ALIAS_NETPROTO(PF_IUCV);