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.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 (sizeof(((struct iucv_message *)0)->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;
155 #ifdef CONFIG_PM_DEBUG
156 printk(KERN_WARNING "afiucv_pm_freeze\n");
158 read_lock(&iucv_sk_list.lock);
159 sk_for_each(sk, &iucv_sk_list.head) {
161 switch (sk->sk_state) {
165 iucv_sever_path(sk, 0);
174 skb_queue_purge(&iucv->send_skb_q);
175 skb_queue_purge(&iucv->backlog_skb_q);
177 read_unlock(&iucv_sk_list.lock);
182 * afiucv_pm_restore_thaw() - Thaw and restore PM callback
183 * @dev: AFIUCV dummy device
185 * socket clean up after freeze
187 static int afiucv_pm_restore_thaw(struct device *dev)
191 #ifdef CONFIG_PM_DEBUG
192 printk(KERN_WARNING "afiucv_pm_restore_thaw\n");
194 read_lock(&iucv_sk_list.lock);
195 sk_for_each(sk, &iucv_sk_list.head) {
196 switch (sk->sk_state) {
199 sk->sk_state = IUCV_DISCONN;
200 sk->sk_state_change(sk);
211 read_unlock(&iucv_sk_list.lock);
215 static const struct dev_pm_ops afiucv_pm_ops = {
216 .prepare = afiucv_pm_prepare,
217 .complete = afiucv_pm_complete,
218 .freeze = afiucv_pm_freeze,
219 .thaw = afiucv_pm_restore_thaw,
220 .restore = afiucv_pm_restore_thaw,
223 static struct device_driver af_iucv_driver = {
224 .owner = THIS_MODULE,
227 .pm = &afiucv_pm_ops,
230 /* dummy device used as trigger for PM functions */
231 static struct device *af_iucv_dev;
234 * iucv_msg_length() - Returns the length of an iucv message.
235 * @msg: Pointer to struct iucv_message, MUST NOT be NULL
237 * The function returns the length of the specified iucv message @msg of data
238 * stored in a buffer and of data stored in the parameter list (PRMDATA).
240 * For IUCV_IPRMDATA, AF_IUCV uses the following convention to transport socket
242 * PRMDATA[0..6] socket data (max 7 bytes);
243 * PRMDATA[7] socket data length value (len is 0xff - PRMDATA[7])
245 * The socket data length is computed by subtracting the socket data length
247 * If the socket data len is greater 7, then PRMDATA can be used for special
248 * notifications (see iucv_sock_shutdown); and further,
249 * if the socket data len is > 7, the function returns 8.
251 * Use this function to allocate socket buffers to store iucv message data.
253 static inline size_t iucv_msg_length(struct iucv_message *msg)
257 if (msg->flags & IUCV_IPRMDATA) {
258 datalen = 0xff - msg->rmmsg[7];
259 return (datalen < 8) ? datalen : 8;
265 * iucv_sock_in_state() - check for specific states
266 * @sk: sock structure
267 * @state: first iucv sk state
268 * @state: second iucv sk state
270 * Returns true if the socket in either in the first or second state.
272 static int iucv_sock_in_state(struct sock *sk, int state, int state2)
274 return (sk->sk_state == state || sk->sk_state == state2);
278 * iucv_below_msglim() - function to check if messages can be sent
279 * @sk: sock structure
281 * Returns true if the send queue length is lower than the message limit.
282 * Always returns true if the socket is not connected (no iucv path for
283 * checking the message limit).
285 static inline int iucv_below_msglim(struct sock *sk)
287 struct iucv_sock *iucv = iucv_sk(sk);
289 if (sk->sk_state != IUCV_CONNECTED)
291 if (iucv->transport == AF_IUCV_TRANS_IUCV)
292 return (skb_queue_len(&iucv->send_skb_q) < iucv->path->msglim);
294 return ((atomic_read(&iucv->msg_sent) < iucv->msglimit_peer) &&
295 (atomic_read(&iucv->pendings) <= 0));
299 * iucv_sock_wake_msglim() - Wake up thread waiting on msg limit
301 static void iucv_sock_wake_msglim(struct sock *sk)
303 struct socket_wq *wq;
306 wq = rcu_dereference(sk->sk_wq);
307 if (skwq_has_sleeper(wq))
308 wake_up_interruptible_all(&wq->wait);
309 sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
314 * afiucv_hs_send() - send a message through HiperSockets transport
316 static int afiucv_hs_send(struct iucv_message *imsg, struct sock *sock,
317 struct sk_buff *skb, u8 flags)
319 struct iucv_sock *iucv = iucv_sk(sock);
320 struct af_iucv_trans_hdr *phs_hdr;
321 struct sk_buff *nskb;
322 int err, confirm_recv = 0;
324 memset(skb->head, 0, ETH_HLEN);
325 phs_hdr = (struct af_iucv_trans_hdr *)skb_push(skb,
326 sizeof(struct af_iucv_trans_hdr));
327 skb_reset_mac_header(skb);
328 skb_reset_network_header(skb);
329 skb_push(skb, ETH_HLEN);
330 skb_reset_mac_header(skb);
331 memset(phs_hdr, 0, sizeof(struct af_iucv_trans_hdr));
333 phs_hdr->magic = ETH_P_AF_IUCV;
334 phs_hdr->version = 1;
335 phs_hdr->flags = flags;
336 if (flags == AF_IUCV_FLAG_SYN)
337 phs_hdr->window = iucv->msglimit;
338 else if ((flags == AF_IUCV_FLAG_WIN) || !flags) {
339 confirm_recv = atomic_read(&iucv->msg_recv);
340 phs_hdr->window = confirm_recv;
342 phs_hdr->flags = phs_hdr->flags | AF_IUCV_FLAG_WIN;
344 memcpy(phs_hdr->destUserID, iucv->dst_user_id, 8);
345 memcpy(phs_hdr->destAppName, iucv->dst_name, 8);
346 memcpy(phs_hdr->srcUserID, iucv->src_user_id, 8);
347 memcpy(phs_hdr->srcAppName, iucv->src_name, 8);
348 ASCEBC(phs_hdr->destUserID, sizeof(phs_hdr->destUserID));
349 ASCEBC(phs_hdr->destAppName, sizeof(phs_hdr->destAppName));
350 ASCEBC(phs_hdr->srcUserID, sizeof(phs_hdr->srcUserID));
351 ASCEBC(phs_hdr->srcAppName, sizeof(phs_hdr->srcAppName));
353 memcpy(&phs_hdr->iucv_hdr, imsg, sizeof(struct iucv_message));
355 skb->dev = iucv->hs_dev;
358 if (!(skb->dev->flags & IFF_UP) || !netif_carrier_ok(skb->dev))
360 if (skb->len > skb->dev->mtu) {
361 if (sock->sk_type == SOCK_SEQPACKET)
364 skb_trim(skb, skb->dev->mtu);
366 skb->protocol = ETH_P_AF_IUCV;
367 nskb = skb_clone(skb, GFP_ATOMIC);
370 skb_queue_tail(&iucv->send_skb_q, nskb);
371 err = dev_queue_xmit(skb);
372 if (net_xmit_eval(err)) {
373 skb_unlink(nskb, &iucv->send_skb_q);
376 atomic_sub(confirm_recv, &iucv->msg_recv);
377 WARN_ON(atomic_read(&iucv->msg_recv) < 0);
379 return net_xmit_eval(err);
382 static struct sock *__iucv_get_sock_by_name(char *nm)
386 sk_for_each(sk, &iucv_sk_list.head)
387 if (!memcmp(&iucv_sk(sk)->src_name, nm, 8))
393 static void iucv_sock_destruct(struct sock *sk)
395 skb_queue_purge(&sk->sk_receive_queue);
396 skb_queue_purge(&sk->sk_error_queue);
400 if (!sock_flag(sk, SOCK_DEAD)) {
401 pr_err("Attempt to release alive iucv socket %p\n", sk);
405 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
406 WARN_ON(atomic_read(&sk->sk_wmem_alloc));
407 WARN_ON(sk->sk_wmem_queued);
408 WARN_ON(sk->sk_forward_alloc);
412 static void iucv_sock_cleanup_listen(struct sock *parent)
416 /* Close non-accepted connections */
417 while ((sk = iucv_accept_dequeue(parent, NULL))) {
422 parent->sk_state = IUCV_CLOSED;
425 /* Kill socket (only if zapped and orphaned) */
426 static void iucv_sock_kill(struct sock *sk)
428 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
431 iucv_sock_unlink(&iucv_sk_list, sk);
432 sock_set_flag(sk, SOCK_DEAD);
436 /* Terminate an IUCV path */
437 static void iucv_sever_path(struct sock *sk, int with_user_data)
439 unsigned char user_data[16];
440 struct iucv_sock *iucv = iucv_sk(sk);
441 struct iucv_path *path = iucv->path;
445 if (with_user_data) {
446 low_nmcpy(user_data, iucv->src_name);
447 high_nmcpy(user_data, iucv->dst_name);
448 ASCEBC(user_data, sizeof(user_data));
449 pr_iucv->path_sever(path, user_data);
451 pr_iucv->path_sever(path, NULL);
452 iucv_path_free(path);
456 /* Send controlling flags through an IUCV socket for HIPER transport */
457 static int iucv_send_ctrl(struct sock *sk, u8 flags)
464 blen = sizeof(struct af_iucv_trans_hdr) + ETH_HLEN;
465 if (sk->sk_shutdown & SEND_SHUTDOWN) {
466 /* controlling flags should be sent anyway */
467 shutdown = sk->sk_shutdown;
468 sk->sk_shutdown &= RCV_SHUTDOWN;
470 skb = sock_alloc_send_skb(sk, blen, 1, &err);
472 skb_reserve(skb, blen);
473 err = afiucv_hs_send(NULL, sk, skb, flags);
476 sk->sk_shutdown = shutdown;
480 /* Close an IUCV socket */
481 static void iucv_sock_close(struct sock *sk)
483 struct iucv_sock *iucv = iucv_sk(sk);
489 switch (sk->sk_state) {
491 iucv_sock_cleanup_listen(sk);
495 if (iucv->transport == AF_IUCV_TRANS_HIPER) {
496 err = iucv_send_ctrl(sk, AF_IUCV_FLAG_FIN);
497 sk->sk_state = IUCV_DISCONN;
498 sk->sk_state_change(sk);
500 case IUCV_DISCONN: /* fall through */
501 sk->sk_state = IUCV_CLOSING;
502 sk->sk_state_change(sk);
504 if (!err && !skb_queue_empty(&iucv->send_skb_q)) {
505 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
506 timeo = sk->sk_lingertime;
508 timeo = IUCV_DISCONN_TIMEOUT;
510 iucv_sock_in_state(sk, IUCV_CLOSED, 0),
514 case IUCV_CLOSING: /* fall through */
515 sk->sk_state = IUCV_CLOSED;
516 sk->sk_state_change(sk);
518 sk->sk_err = ECONNRESET;
519 sk->sk_state_change(sk);
521 skb_queue_purge(&iucv->send_skb_q);
522 skb_queue_purge(&iucv->backlog_skb_q);
524 default: /* fall through */
525 iucv_sever_path(sk, 1);
529 dev_put(iucv->hs_dev);
531 sk->sk_bound_dev_if = 0;
534 /* mark socket for deletion by iucv_sock_kill() */
535 sock_set_flag(sk, SOCK_ZAPPED);
540 static void iucv_sock_init(struct sock *sk, struct sock *parent)
543 sk->sk_type = parent->sk_type;
544 security_sk_clone(parent, sk);
548 static struct sock *iucv_sock_alloc(struct socket *sock, int proto, gfp_t prio, int kern)
551 struct iucv_sock *iucv;
553 sk = sk_alloc(&init_net, PF_IUCV, prio, &iucv_proto, kern);
558 sock_init_data(sock, sk);
559 INIT_LIST_HEAD(&iucv->accept_q);
560 spin_lock_init(&iucv->accept_q_lock);
561 skb_queue_head_init(&iucv->send_skb_q);
562 INIT_LIST_HEAD(&iucv->message_q.list);
563 spin_lock_init(&iucv->message_q.lock);
564 skb_queue_head_init(&iucv->backlog_skb_q);
566 atomic_set(&iucv->pendings, 0);
569 atomic_set(&iucv->msg_sent, 0);
570 atomic_set(&iucv->msg_recv, 0);
572 iucv->sk_txnotify = afiucv_hs_callback_txnotify;
573 memset(&iucv->src_user_id , 0, 32);
575 iucv->transport = AF_IUCV_TRANS_IUCV;
577 iucv->transport = AF_IUCV_TRANS_HIPER;
579 sk->sk_destruct = iucv_sock_destruct;
580 sk->sk_sndtimeo = IUCV_CONN_TIMEOUT;
581 sk->sk_allocation = GFP_DMA;
583 sock_reset_flag(sk, SOCK_ZAPPED);
585 sk->sk_protocol = proto;
586 sk->sk_state = IUCV_OPEN;
588 iucv_sock_link(&iucv_sk_list, sk);
592 /* Create an IUCV socket */
593 static int iucv_sock_create(struct net *net, struct socket *sock, int protocol,
598 if (protocol && protocol != PF_IUCV)
599 return -EPROTONOSUPPORT;
601 sock->state = SS_UNCONNECTED;
603 switch (sock->type) {
605 sock->ops = &iucv_sock_ops;
608 /* currently, proto ops can handle both sk types */
609 sock->ops = &iucv_sock_ops;
612 return -ESOCKTNOSUPPORT;
615 sk = iucv_sock_alloc(sock, protocol, GFP_KERNEL, kern);
619 iucv_sock_init(sk, NULL);
624 void iucv_sock_link(struct iucv_sock_list *l, struct sock *sk)
626 write_lock_bh(&l->lock);
627 sk_add_node(sk, &l->head);
628 write_unlock_bh(&l->lock);
631 void iucv_sock_unlink(struct iucv_sock_list *l, struct sock *sk)
633 write_lock_bh(&l->lock);
634 sk_del_node_init(sk);
635 write_unlock_bh(&l->lock);
638 void iucv_accept_enqueue(struct sock *parent, struct sock *sk)
641 struct iucv_sock *par = iucv_sk(parent);
644 spin_lock_irqsave(&par->accept_q_lock, flags);
645 list_add_tail(&iucv_sk(sk)->accept_q, &par->accept_q);
646 spin_unlock_irqrestore(&par->accept_q_lock, flags);
647 iucv_sk(sk)->parent = parent;
648 sk_acceptq_added(parent);
651 void iucv_accept_unlink(struct sock *sk)
654 struct iucv_sock *par = iucv_sk(iucv_sk(sk)->parent);
656 spin_lock_irqsave(&par->accept_q_lock, flags);
657 list_del_init(&iucv_sk(sk)->accept_q);
658 spin_unlock_irqrestore(&par->accept_q_lock, flags);
659 sk_acceptq_removed(iucv_sk(sk)->parent);
660 iucv_sk(sk)->parent = NULL;
664 struct sock *iucv_accept_dequeue(struct sock *parent, struct socket *newsock)
666 struct iucv_sock *isk, *n;
669 list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) {
670 sk = (struct sock *) isk;
673 if (sk->sk_state == IUCV_CLOSED) {
674 iucv_accept_unlink(sk);
679 if (sk->sk_state == IUCV_CONNECTED ||
680 sk->sk_state == IUCV_DISCONN ||
682 iucv_accept_unlink(sk);
684 sock_graft(sk, newsock);
695 static void __iucv_auto_name(struct iucv_sock *iucv)
699 sprintf(name, "%08x", atomic_inc_return(&iucv_sk_list.autobind_name));
700 while (__iucv_get_sock_by_name(name)) {
701 sprintf(name, "%08x",
702 atomic_inc_return(&iucv_sk_list.autobind_name));
704 memcpy(iucv->src_name, name, 8);
707 /* Bind an unbound socket */
708 static int iucv_sock_bind(struct socket *sock, struct sockaddr *addr,
711 struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
712 struct sock *sk = sock->sk;
713 struct iucv_sock *iucv;
715 struct net_device *dev;
718 /* Verify the input sockaddr */
719 if (!addr || addr->sa_family != AF_IUCV)
722 if (addr_len < sizeof(struct sockaddr_iucv))
726 if (sk->sk_state != IUCV_OPEN) {
731 write_lock_bh(&iucv_sk_list.lock);
734 if (__iucv_get_sock_by_name(sa->siucv_name)) {
741 /* Bind the socket */
743 if (!memcmp(sa->siucv_user_id, iucv_userid, 8))
744 goto vm_bind; /* VM IUCV transport */
746 /* try hiper transport */
747 memcpy(uid, sa->siucv_user_id, sizeof(uid));
750 for_each_netdev_rcu(&init_net, dev) {
751 if (!memcmp(dev->perm_addr, uid, 8)) {
752 memcpy(iucv->src_user_id, sa->siucv_user_id, 8);
753 /* Check for unitialized siucv_name */
754 if (strncmp(sa->siucv_name, " ", 8) == 0)
755 __iucv_auto_name(iucv);
757 memcpy(iucv->src_name, sa->siucv_name, 8);
758 sk->sk_bound_dev_if = dev->ifindex;
761 sk->sk_state = IUCV_BOUND;
762 iucv->transport = AF_IUCV_TRANS_HIPER;
764 iucv->msglimit = IUCV_HIPER_MSGLIM_DEFAULT;
772 /* use local userid for backward compat */
773 memcpy(iucv->src_name, sa->siucv_name, 8);
774 memcpy(iucv->src_user_id, iucv_userid, 8);
775 sk->sk_state = IUCV_BOUND;
776 iucv->transport = AF_IUCV_TRANS_IUCV;
778 iucv->msglimit = IUCV_QUEUELEN_DEFAULT;
781 /* found no dev to bind */
784 /* Release the socket list lock */
785 write_unlock_bh(&iucv_sk_list.lock);
791 /* Automatically bind an unbound socket */
792 static int iucv_sock_autobind(struct sock *sk)
794 struct iucv_sock *iucv = iucv_sk(sk);
797 if (unlikely(!pr_iucv))
800 memcpy(iucv->src_user_id, iucv_userid, 8);
802 write_lock_bh(&iucv_sk_list.lock);
803 __iucv_auto_name(iucv);
804 write_unlock_bh(&iucv_sk_list.lock);
807 iucv->msglimit = IUCV_QUEUELEN_DEFAULT;
812 static int afiucv_path_connect(struct socket *sock, struct sockaddr *addr)
814 struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
815 struct sock *sk = sock->sk;
816 struct iucv_sock *iucv = iucv_sk(sk);
817 unsigned char user_data[16];
820 high_nmcpy(user_data, sa->siucv_name);
821 low_nmcpy(user_data, iucv->src_name);
822 ASCEBC(user_data, sizeof(user_data));
825 iucv->path = iucv_path_alloc(iucv->msglimit,
826 IUCV_IPRMDATA, GFP_KERNEL);
831 err = pr_iucv->path_connect(iucv->path, &af_iucv_handler,
832 sa->siucv_user_id, NULL, user_data,
835 iucv_path_free(iucv->path);
838 case 0x0b: /* Target communicator is not logged on */
841 case 0x0d: /* Max connections for this guest exceeded */
842 case 0x0e: /* Max connections for target guest exceeded */
845 case 0x0f: /* Missing IUCV authorization */
857 /* Connect an unconnected socket */
858 static int iucv_sock_connect(struct socket *sock, struct sockaddr *addr,
861 struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
862 struct sock *sk = sock->sk;
863 struct iucv_sock *iucv = iucv_sk(sk);
866 if (addr->sa_family != AF_IUCV || alen < sizeof(struct sockaddr_iucv))
869 if (sk->sk_state != IUCV_OPEN && sk->sk_state != IUCV_BOUND)
872 if (sk->sk_state == IUCV_OPEN &&
873 iucv->transport == AF_IUCV_TRANS_HIPER)
874 return -EBADFD; /* explicit bind required */
876 if (sk->sk_type != SOCK_STREAM && sk->sk_type != SOCK_SEQPACKET)
879 if (sk->sk_state == IUCV_OPEN) {
880 err = iucv_sock_autobind(sk);
887 /* Set the destination information */
888 memcpy(iucv->dst_user_id, sa->siucv_user_id, 8);
889 memcpy(iucv->dst_name, sa->siucv_name, 8);
891 if (iucv->transport == AF_IUCV_TRANS_HIPER)
892 err = iucv_send_ctrl(sock->sk, AF_IUCV_FLAG_SYN);
894 err = afiucv_path_connect(sock, addr);
898 if (sk->sk_state != IUCV_CONNECTED)
899 err = iucv_sock_wait(sk, iucv_sock_in_state(sk, IUCV_CONNECTED,
901 sock_sndtimeo(sk, flags & O_NONBLOCK));
903 if (sk->sk_state == IUCV_DISCONN || sk->sk_state == IUCV_CLOSED)
906 if (err && iucv->transport == AF_IUCV_TRANS_IUCV)
907 iucv_sever_path(sk, 0);
914 /* Move a socket into listening state. */
915 static int iucv_sock_listen(struct socket *sock, int backlog)
917 struct sock *sk = sock->sk;
923 if (sk->sk_state != IUCV_BOUND)
926 if (sock->type != SOCK_STREAM && sock->type != SOCK_SEQPACKET)
929 sk->sk_max_ack_backlog = backlog;
930 sk->sk_ack_backlog = 0;
931 sk->sk_state = IUCV_LISTEN;
939 /* Accept a pending connection */
940 static int iucv_sock_accept(struct socket *sock, struct socket *newsock,
943 DECLARE_WAITQUEUE(wait, current);
944 struct sock *sk = sock->sk, *nsk;
948 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
950 if (sk->sk_state != IUCV_LISTEN) {
955 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
957 /* Wait for an incoming connection */
958 add_wait_queue_exclusive(sk_sleep(sk), &wait);
959 while (!(nsk = iucv_accept_dequeue(sk, newsock))) {
960 set_current_state(TASK_INTERRUPTIBLE);
967 timeo = schedule_timeout(timeo);
968 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
970 if (sk->sk_state != IUCV_LISTEN) {
975 if (signal_pending(current)) {
976 err = sock_intr_errno(timeo);
981 set_current_state(TASK_RUNNING);
982 remove_wait_queue(sk_sleep(sk), &wait);
987 newsock->state = SS_CONNECTED;
994 static int iucv_sock_getname(struct socket *sock, struct sockaddr *addr,
997 struct sockaddr_iucv *siucv = (struct sockaddr_iucv *) addr;
998 struct sock *sk = sock->sk;
999 struct iucv_sock *iucv = iucv_sk(sk);
1001 addr->sa_family = AF_IUCV;
1002 *len = sizeof(struct sockaddr_iucv);
1005 memcpy(siucv->siucv_user_id, iucv->dst_user_id, 8);
1006 memcpy(siucv->siucv_name, iucv->dst_name, 8);
1008 memcpy(siucv->siucv_user_id, iucv->src_user_id, 8);
1009 memcpy(siucv->siucv_name, iucv->src_name, 8);
1011 memset(&siucv->siucv_port, 0, sizeof(siucv->siucv_port));
1012 memset(&siucv->siucv_addr, 0, sizeof(siucv->siucv_addr));
1013 memset(&siucv->siucv_nodeid, 0, sizeof(siucv->siucv_nodeid));
1019 * iucv_send_iprm() - Send socket data in parameter list of an iucv message.
1021 * @msg: Pointer to a struct iucv_message
1022 * @skb: The socket data to send, skb->len MUST BE <= 7
1024 * Send the socket data in the parameter list in the iucv message
1025 * (IUCV_IPRMDATA). The socket data is stored at index 0 to 6 in the parameter
1026 * list and the socket data len at index 7 (last byte).
1027 * See also iucv_msg_length().
1029 * Returns the error code from the iucv_message_send() call.
1031 static int iucv_send_iprm(struct iucv_path *path, struct iucv_message *msg,
1032 struct sk_buff *skb)
1036 memcpy(prmdata, (void *) skb->data, skb->len);
1037 prmdata[7] = 0xff - (u8) skb->len;
1038 return pr_iucv->message_send(path, msg, IUCV_IPRMDATA, 0,
1039 (void *) prmdata, 8);
1042 static int iucv_sock_sendmsg(struct socket *sock, struct msghdr *msg,
1045 struct sock *sk = sock->sk;
1046 struct iucv_sock *iucv = iucv_sk(sk);
1047 size_t headroom, linear;
1048 struct sk_buff *skb;
1049 struct iucv_message txmsg = {0};
1050 struct cmsghdr *cmsg;
1056 int noblock = msg->msg_flags & MSG_DONTWAIT;
1058 err = sock_error(sk);
1062 if (msg->msg_flags & MSG_OOB)
1065 /* SOCK_SEQPACKET: we do not support segmented records */
1066 if (sk->sk_type == SOCK_SEQPACKET && !(msg->msg_flags & MSG_EOR))
1071 if (sk->sk_shutdown & SEND_SHUTDOWN) {
1076 /* Return if the socket is not in connected state */
1077 if (sk->sk_state != IUCV_CONNECTED) {
1082 /* initialize defaults */
1083 cmsg_done = 0; /* check for duplicate headers */
1086 /* iterate over control messages */
1087 for_each_cmsghdr(cmsg, msg) {
1088 if (!CMSG_OK(msg, cmsg)) {
1093 if (cmsg->cmsg_level != SOL_IUCV)
1096 if (cmsg->cmsg_type & cmsg_done) {
1100 cmsg_done |= cmsg->cmsg_type;
1102 switch (cmsg->cmsg_type) {
1103 case SCM_IUCV_TRGCLS:
1104 if (cmsg->cmsg_len != CMSG_LEN(TRGCLS_SIZE)) {
1109 /* set iucv message target class */
1110 memcpy(&txmsg.class,
1111 (void *) CMSG_DATA(cmsg), TRGCLS_SIZE);
1121 /* allocate one skb for each iucv message:
1122 * this is fine for SOCK_SEQPACKET (unless we want to support
1123 * segmented records using the MSG_EOR flag), but
1124 * for SOCK_STREAM we might want to improve it in future */
1125 headroom = (iucv->transport == AF_IUCV_TRANS_HIPER)
1126 ? sizeof(struct af_iucv_trans_hdr) + ETH_HLEN : 0;
1127 if (headroom + len < PAGE_SIZE) {
1130 /* In nonlinear "classic" iucv skb,
1131 * reserve space for iucv_array
1133 if (iucv->transport != AF_IUCV_TRANS_HIPER)
1134 headroom += sizeof(struct iucv_array) *
1135 (MAX_SKB_FRAGS + 1);
1136 linear = PAGE_SIZE - headroom;
1138 skb = sock_alloc_send_pskb(sk, headroom + linear, len - linear,
1143 skb_reserve(skb, headroom);
1144 skb_put(skb, linear);
1146 skb->data_len = len - linear;
1147 err = skb_copy_datagram_from_iter(skb, 0, &msg->msg_iter, len);
1151 /* wait if outstanding messages for iucv path has reached */
1152 timeo = sock_sndtimeo(sk, noblock);
1153 err = iucv_sock_wait(sk, iucv_below_msglim(sk), timeo);
1157 /* return -ECONNRESET if the socket is no longer connected */
1158 if (sk->sk_state != IUCV_CONNECTED) {
1163 /* increment and save iucv message tag for msg_completion cbk */
1164 txmsg.tag = iucv->send_tag++;
1165 IUCV_SKB_CB(skb)->tag = txmsg.tag;
1167 if (iucv->transport == AF_IUCV_TRANS_HIPER) {
1168 atomic_inc(&iucv->msg_sent);
1169 err = afiucv_hs_send(&txmsg, sk, skb, 0);
1171 atomic_dec(&iucv->msg_sent);
1174 } else { /* Classic VM IUCV transport */
1175 skb_queue_tail(&iucv->send_skb_q, skb);
1177 if (((iucv->path->flags & IUCV_IPRMDATA) & iucv->flags) &&
1179 err = iucv_send_iprm(iucv->path, &txmsg, skb);
1181 /* on success: there is no message_complete callback */
1182 /* for an IPRMDATA msg; remove skb from send queue */
1184 skb_unlink(skb, &iucv->send_skb_q);
1188 /* this error should never happen since the */
1189 /* IUCV_IPRMDATA path flag is set... sever path */
1191 pr_iucv->path_sever(iucv->path, NULL);
1192 skb_unlink(skb, &iucv->send_skb_q);
1196 } else if (skb_is_nonlinear(skb)) {
1197 struct iucv_array *iba = (struct iucv_array *)skb->head;
1200 /* skip iucv_array lying in the headroom */
1201 iba[0].address = (u32)(addr_t)skb->data;
1202 iba[0].length = (u32)skb_headlen(skb);
1203 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1204 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1206 iba[i + 1].address =
1207 (u32)(addr_t)skb_frag_address(frag);
1208 iba[i + 1].length = (u32)skb_frag_size(frag);
1210 err = pr_iucv->message_send(iucv->path, &txmsg,
1212 (void *)iba, skb->len);
1213 } else { /* non-IPRM Linear skb */
1214 err = pr_iucv->message_send(iucv->path, &txmsg,
1215 0, 0, (void *)skb->data, skb->len);
1220 memcpy(user_id, iucv->dst_user_id, 8);
1222 memcpy(appl_id, iucv->dst_name, 8);
1224 "Application %s on z/VM guest %s exceeds message limit\n",
1230 skb_unlink(skb, &iucv->send_skb_q);
1245 static struct sk_buff *alloc_iucv_recv_skb(unsigned long len)
1247 size_t headroom, linear;
1248 struct sk_buff *skb;
1251 if (len < PAGE_SIZE) {
1255 headroom = sizeof(struct iucv_array) * (MAX_SKB_FRAGS + 1);
1256 linear = PAGE_SIZE - headroom;
1258 skb = alloc_skb_with_frags(headroom + linear, len - linear,
1259 0, &err, GFP_ATOMIC | GFP_DMA);
1261 "alloc of recv iucv skb len=%lu failed with errcode=%d\n",
1265 skb_reserve(skb, headroom);
1266 skb_put(skb, linear);
1268 skb->data_len = len - linear;
1273 /* iucv_process_message() - Receive a single outstanding IUCV message
1275 * Locking: must be called with message_q.lock held
1277 static void iucv_process_message(struct sock *sk, struct sk_buff *skb,
1278 struct iucv_path *path,
1279 struct iucv_message *msg)
1284 len = iucv_msg_length(msg);
1286 /* store msg target class in the second 4 bytes of skb ctrl buffer */
1287 /* Note: the first 4 bytes are reserved for msg tag */
1288 IUCV_SKB_CB(skb)->class = msg->class;
1290 /* check for special IPRM messages (e.g. iucv_sock_shutdown) */
1291 if ((msg->flags & IUCV_IPRMDATA) && len > 7) {
1292 if (memcmp(msg->rmmsg, iprm_shutdown, 8) == 0) {
1297 if (skb_is_nonlinear(skb)) {
1298 struct iucv_array *iba = (struct iucv_array *)skb->head;
1301 iba[0].address = (u32)(addr_t)skb->data;
1302 iba[0].length = (u32)skb_headlen(skb);
1303 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1304 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1306 iba[i + 1].address =
1307 (u32)(addr_t)skb_frag_address(frag);
1308 iba[i + 1].length = (u32)skb_frag_size(frag);
1310 rc = pr_iucv->message_receive(path, msg,
1312 (void *)iba, len, NULL);
1314 rc = pr_iucv->message_receive(path, msg,
1315 msg->flags & IUCV_IPRMDATA,
1316 skb->data, len, NULL);
1322 WARN_ON_ONCE(skb->len != len);
1325 IUCV_SKB_CB(skb)->offset = 0;
1326 if (sk_filter(sk, skb)) {
1327 atomic_inc(&sk->sk_drops); /* skb rejected by filter */
1331 if (__sock_queue_rcv_skb(sk, skb)) /* handle rcv queue full */
1332 skb_queue_tail(&iucv_sk(sk)->backlog_skb_q, skb);
1335 /* iucv_process_message_q() - Process outstanding IUCV messages
1337 * Locking: must be called with message_q.lock held
1339 static void iucv_process_message_q(struct sock *sk)
1341 struct iucv_sock *iucv = iucv_sk(sk);
1342 struct sk_buff *skb;
1343 struct sock_msg_q *p, *n;
1345 list_for_each_entry_safe(p, n, &iucv->message_q.list, list) {
1346 skb = alloc_iucv_recv_skb(iucv_msg_length(&p->msg));
1349 iucv_process_message(sk, skb, p->path, &p->msg);
1352 if (!skb_queue_empty(&iucv->backlog_skb_q))
1357 static int iucv_sock_recvmsg(struct socket *sock, struct msghdr *msg,
1358 size_t len, int flags)
1360 int noblock = flags & MSG_DONTWAIT;
1361 struct sock *sk = sock->sk;
1362 struct iucv_sock *iucv = iucv_sk(sk);
1363 unsigned int copied, rlen;
1364 struct sk_buff *skb, *rskb, *cskb;
1368 if ((sk->sk_state == IUCV_DISCONN) &&
1369 skb_queue_empty(&iucv->backlog_skb_q) &&
1370 skb_queue_empty(&sk->sk_receive_queue) &&
1371 list_empty(&iucv->message_q.list))
1374 if (flags & (MSG_OOB))
1377 /* receive/dequeue next skb:
1378 * the function understands MSG_PEEK and, thus, does not dequeue skb */
1379 skb = skb_recv_datagram(sk, flags, noblock, &err);
1381 if (sk->sk_shutdown & RCV_SHUTDOWN)
1386 offset = IUCV_SKB_CB(skb)->offset;
1387 rlen = skb->len - offset; /* real length of skb */
1388 copied = min_t(unsigned int, rlen, len);
1390 sk->sk_shutdown = sk->sk_shutdown | RCV_SHUTDOWN;
1393 if (skb_copy_datagram_msg(cskb, offset, msg, copied)) {
1394 if (!(flags & MSG_PEEK))
1395 skb_queue_head(&sk->sk_receive_queue, skb);
1399 /* SOCK_SEQPACKET: set MSG_TRUNC if recv buf size is too small */
1400 if (sk->sk_type == SOCK_SEQPACKET) {
1402 msg->msg_flags |= MSG_TRUNC;
1403 /* each iucv message contains a complete record */
1404 msg->msg_flags |= MSG_EOR;
1407 /* create control message to store iucv msg target class:
1408 * get the trgcls from the control buffer of the skb due to
1409 * fragmentation of original iucv message. */
1410 err = put_cmsg(msg, SOL_IUCV, SCM_IUCV_TRGCLS,
1411 sizeof(IUCV_SKB_CB(skb)->class),
1412 (void *)&IUCV_SKB_CB(skb)->class);
1414 if (!(flags & MSG_PEEK))
1415 skb_queue_head(&sk->sk_receive_queue, skb);
1419 /* Mark read part of skb as used */
1420 if (!(flags & MSG_PEEK)) {
1422 /* SOCK_STREAM: re-queue skb if it contains unreceived data */
1423 if (sk->sk_type == SOCK_STREAM) {
1424 if (copied < rlen) {
1425 IUCV_SKB_CB(skb)->offset = offset + copied;
1426 skb_queue_head(&sk->sk_receive_queue, skb);
1432 if (iucv->transport == AF_IUCV_TRANS_HIPER) {
1433 atomic_inc(&iucv->msg_recv);
1434 if (atomic_read(&iucv->msg_recv) > iucv->msglimit) {
1436 iucv_sock_close(sk);
1441 /* Queue backlog skbs */
1442 spin_lock_bh(&iucv->message_q.lock);
1443 rskb = skb_dequeue(&iucv->backlog_skb_q);
1445 IUCV_SKB_CB(rskb)->offset = 0;
1446 if (__sock_queue_rcv_skb(sk, rskb)) {
1447 /* handle rcv queue full */
1448 skb_queue_head(&iucv->backlog_skb_q,
1452 rskb = skb_dequeue(&iucv->backlog_skb_q);
1454 if (skb_queue_empty(&iucv->backlog_skb_q)) {
1455 if (!list_empty(&iucv->message_q.list))
1456 iucv_process_message_q(sk);
1457 if (atomic_read(&iucv->msg_recv) >=
1458 iucv->msglimit / 2) {
1459 err = iucv_send_ctrl(sk, AF_IUCV_FLAG_WIN);
1461 sk->sk_state = IUCV_DISCONN;
1462 sk->sk_state_change(sk);
1466 spin_unlock_bh(&iucv->message_q.lock);
1470 /* SOCK_SEQPACKET: return real length if MSG_TRUNC is set */
1471 if (sk->sk_type == SOCK_SEQPACKET && (flags & MSG_TRUNC))
1477 static inline unsigned int iucv_accept_poll(struct sock *parent)
1479 struct iucv_sock *isk, *n;
1482 list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) {
1483 sk = (struct sock *) isk;
1485 if (sk->sk_state == IUCV_CONNECTED)
1486 return POLLIN | POLLRDNORM;
1492 unsigned int iucv_sock_poll(struct file *file, struct socket *sock,
1495 struct sock *sk = sock->sk;
1496 unsigned int mask = 0;
1498 sock_poll_wait(file, sk_sleep(sk), wait);
1500 if (sk->sk_state == IUCV_LISTEN)
1501 return iucv_accept_poll(sk);
1503 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
1505 (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? POLLPRI : 0);
1507 if (sk->sk_shutdown & RCV_SHUTDOWN)
1510 if (sk->sk_shutdown == SHUTDOWN_MASK)
1513 if (!skb_queue_empty(&sk->sk_receive_queue) ||
1514 (sk->sk_shutdown & RCV_SHUTDOWN))
1515 mask |= POLLIN | POLLRDNORM;
1517 if (sk->sk_state == IUCV_CLOSED)
1520 if (sk->sk_state == IUCV_DISCONN)
1523 if (sock_writeable(sk) && iucv_below_msglim(sk))
1524 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
1526 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
1531 static int iucv_sock_shutdown(struct socket *sock, int how)
1533 struct sock *sk = sock->sk;
1534 struct iucv_sock *iucv = iucv_sk(sk);
1535 struct iucv_message txmsg;
1540 if ((how & ~SHUTDOWN_MASK) || !how)
1544 switch (sk->sk_state) {
1555 if (how == SEND_SHUTDOWN || how == SHUTDOWN_MASK) {
1556 if (iucv->transport == AF_IUCV_TRANS_IUCV) {
1559 err = pr_iucv->message_send(iucv->path, &txmsg,
1560 IUCV_IPRMDATA, 0, (void *) iprm_shutdown, 8);
1575 iucv_send_ctrl(sk, AF_IUCV_FLAG_SHT);
1578 sk->sk_shutdown |= how;
1579 if (how == RCV_SHUTDOWN || how == SHUTDOWN_MASK) {
1580 if ((iucv->transport == AF_IUCV_TRANS_IUCV) &&
1582 err = pr_iucv->path_quiesce(iucv->path, NULL);
1585 /* skb_queue_purge(&sk->sk_receive_queue); */
1587 skb_queue_purge(&sk->sk_receive_queue);
1590 /* Wake up anyone sleeping in poll */
1591 sk->sk_state_change(sk);
1598 static int iucv_sock_release(struct socket *sock)
1600 struct sock *sk = sock->sk;
1606 iucv_sock_close(sk);
1613 /* getsockopt and setsockopt */
1614 static int iucv_sock_setsockopt(struct socket *sock, int level, int optname,
1615 char __user *optval, unsigned int optlen)
1617 struct sock *sk = sock->sk;
1618 struct iucv_sock *iucv = iucv_sk(sk);
1622 if (level != SOL_IUCV)
1623 return -ENOPROTOOPT;
1625 if (optlen < sizeof(int))
1628 if (get_user(val, (int __user *) optval))
1635 case SO_IPRMDATA_MSG:
1637 iucv->flags |= IUCV_IPRMDATA;
1639 iucv->flags &= ~IUCV_IPRMDATA;
1642 switch (sk->sk_state) {
1645 if (val < 1 || val > (u16)(~0))
1648 iucv->msglimit = val;
1664 static int iucv_sock_getsockopt(struct socket *sock, int level, int optname,
1665 char __user *optval, int __user *optlen)
1667 struct sock *sk = sock->sk;
1668 struct iucv_sock *iucv = iucv_sk(sk);
1672 if (level != SOL_IUCV)
1673 return -ENOPROTOOPT;
1675 if (get_user(len, optlen))
1681 len = min_t(unsigned int, len, sizeof(int));
1684 case SO_IPRMDATA_MSG:
1685 val = (iucv->flags & IUCV_IPRMDATA) ? 1 : 0;
1689 val = (iucv->path != NULL) ? iucv->path->msglim /* connected */
1690 : iucv->msglimit; /* default */
1694 if (sk->sk_state == IUCV_OPEN)
1696 val = (iucv->hs_dev) ? iucv->hs_dev->mtu -
1697 sizeof(struct af_iucv_trans_hdr) - ETH_HLEN :
1701 return -ENOPROTOOPT;
1704 if (put_user(len, optlen))
1706 if (copy_to_user(optval, &val, len))
1713 /* Callback wrappers - called from iucv base support */
1714 static int iucv_callback_connreq(struct iucv_path *path,
1715 u8 ipvmid[8], u8 ipuser[16])
1717 unsigned char user_data[16];
1718 unsigned char nuser_data[16];
1719 unsigned char src_name[8];
1720 struct sock *sk, *nsk;
1721 struct iucv_sock *iucv, *niucv;
1724 memcpy(src_name, ipuser, 8);
1725 EBCASC(src_name, 8);
1726 /* Find out if this path belongs to af_iucv. */
1727 read_lock(&iucv_sk_list.lock);
1730 sk_for_each(sk, &iucv_sk_list.head)
1731 if (sk->sk_state == IUCV_LISTEN &&
1732 !memcmp(&iucv_sk(sk)->src_name, src_name, 8)) {
1734 * Found a listening socket with
1735 * src_name == ipuser[0-7].
1740 read_unlock(&iucv_sk_list.lock);
1742 /* No socket found, not one of our paths. */
1747 /* Check if parent socket is listening */
1748 low_nmcpy(user_data, iucv->src_name);
1749 high_nmcpy(user_data, iucv->dst_name);
1750 ASCEBC(user_data, sizeof(user_data));
1751 if (sk->sk_state != IUCV_LISTEN) {
1752 err = pr_iucv->path_sever(path, user_data);
1753 iucv_path_free(path);
1757 /* Check for backlog size */
1758 if (sk_acceptq_is_full(sk)) {
1759 err = pr_iucv->path_sever(path, user_data);
1760 iucv_path_free(path);
1764 /* Create the new socket */
1765 nsk = iucv_sock_alloc(NULL, sk->sk_type, GFP_ATOMIC, 0);
1767 err = pr_iucv->path_sever(path, user_data);
1768 iucv_path_free(path);
1772 niucv = iucv_sk(nsk);
1773 iucv_sock_init(nsk, sk);
1775 /* Set the new iucv_sock */
1776 memcpy(niucv->dst_name, ipuser + 8, 8);
1777 EBCASC(niucv->dst_name, 8);
1778 memcpy(niucv->dst_user_id, ipvmid, 8);
1779 memcpy(niucv->src_name, iucv->src_name, 8);
1780 memcpy(niucv->src_user_id, iucv->src_user_id, 8);
1783 /* Call iucv_accept */
1784 high_nmcpy(nuser_data, ipuser + 8);
1785 memcpy(nuser_data + 8, niucv->src_name, 8);
1786 ASCEBC(nuser_data + 8, 8);
1788 /* set message limit for path based on msglimit of accepting socket */
1789 niucv->msglimit = iucv->msglimit;
1790 path->msglim = iucv->msglimit;
1791 err = pr_iucv->path_accept(path, &af_iucv_handler, nuser_data, nsk);
1793 iucv_sever_path(nsk, 1);
1794 iucv_sock_kill(nsk);
1798 iucv_accept_enqueue(sk, nsk);
1800 /* Wake up accept */
1801 nsk->sk_state = IUCV_CONNECTED;
1802 sk->sk_data_ready(sk);
1809 static void iucv_callback_connack(struct iucv_path *path, u8 ipuser[16])
1811 struct sock *sk = path->private;
1813 sk->sk_state = IUCV_CONNECTED;
1814 sk->sk_state_change(sk);
1817 static void iucv_callback_rx(struct iucv_path *path, struct iucv_message *msg)
1819 struct sock *sk = path->private;
1820 struct iucv_sock *iucv = iucv_sk(sk);
1821 struct sk_buff *skb;
1822 struct sock_msg_q *save_msg;
1825 if (sk->sk_shutdown & RCV_SHUTDOWN) {
1826 pr_iucv->message_reject(path, msg);
1830 spin_lock(&iucv->message_q.lock);
1832 if (!list_empty(&iucv->message_q.list) ||
1833 !skb_queue_empty(&iucv->backlog_skb_q))
1836 len = atomic_read(&sk->sk_rmem_alloc);
1837 len += SKB_TRUESIZE(iucv_msg_length(msg));
1838 if (len > sk->sk_rcvbuf)
1841 skb = alloc_iucv_recv_skb(iucv_msg_length(msg));
1845 iucv_process_message(sk, skb, path, msg);
1849 save_msg = kzalloc(sizeof(struct sock_msg_q), GFP_ATOMIC | GFP_DMA);
1852 save_msg->path = path;
1853 save_msg->msg = *msg;
1855 list_add_tail(&save_msg->list, &iucv->message_q.list);
1858 spin_unlock(&iucv->message_q.lock);
1861 static void iucv_callback_txdone(struct iucv_path *path,
1862 struct iucv_message *msg)
1864 struct sock *sk = path->private;
1865 struct sk_buff *this = NULL;
1866 struct sk_buff_head *list = &iucv_sk(sk)->send_skb_q;
1867 struct sk_buff *list_skb = list->next;
1868 unsigned long flags;
1871 if (!skb_queue_empty(list)) {
1872 spin_lock_irqsave(&list->lock, flags);
1874 while (list_skb != (struct sk_buff *)list) {
1875 if (msg->tag == IUCV_SKB_CB(list_skb)->tag) {
1879 list_skb = list_skb->next;
1882 __skb_unlink(this, list);
1884 spin_unlock_irqrestore(&list->lock, flags);
1888 /* wake up any process waiting for sending */
1889 iucv_sock_wake_msglim(sk);
1893 if (sk->sk_state == IUCV_CLOSING) {
1894 if (skb_queue_empty(&iucv_sk(sk)->send_skb_q)) {
1895 sk->sk_state = IUCV_CLOSED;
1896 sk->sk_state_change(sk);
1903 static void iucv_callback_connrej(struct iucv_path *path, u8 ipuser[16])
1905 struct sock *sk = path->private;
1907 if (sk->sk_state == IUCV_CLOSED)
1911 iucv_sever_path(sk, 1);
1912 sk->sk_state = IUCV_DISCONN;
1914 sk->sk_state_change(sk);
1918 /* called if the other communication side shuts down its RECV direction;
1919 * in turn, the callback sets SEND_SHUTDOWN to disable sending of data.
1921 static void iucv_callback_shutdown(struct iucv_path *path, u8 ipuser[16])
1923 struct sock *sk = path->private;
1926 if (sk->sk_state != IUCV_CLOSED) {
1927 sk->sk_shutdown |= SEND_SHUTDOWN;
1928 sk->sk_state_change(sk);
1933 /***************** HiperSockets transport callbacks ********************/
1934 static void afiucv_swap_src_dest(struct sk_buff *skb)
1936 struct af_iucv_trans_hdr *trans_hdr =
1937 (struct af_iucv_trans_hdr *)skb->data;
1941 ASCEBC(trans_hdr->destUserID, sizeof(trans_hdr->destUserID));
1942 ASCEBC(trans_hdr->destAppName, sizeof(trans_hdr->destAppName));
1943 ASCEBC(trans_hdr->srcUserID, sizeof(trans_hdr->srcUserID));
1944 ASCEBC(trans_hdr->srcAppName, sizeof(trans_hdr->srcAppName));
1945 memcpy(tmpID, trans_hdr->srcUserID, 8);
1946 memcpy(tmpName, trans_hdr->srcAppName, 8);
1947 memcpy(trans_hdr->srcUserID, trans_hdr->destUserID, 8);
1948 memcpy(trans_hdr->srcAppName, trans_hdr->destAppName, 8);
1949 memcpy(trans_hdr->destUserID, tmpID, 8);
1950 memcpy(trans_hdr->destAppName, tmpName, 8);
1951 skb_push(skb, ETH_HLEN);
1952 memset(skb->data, 0, ETH_HLEN);
1956 * afiucv_hs_callback_syn - react on received SYN
1958 static int afiucv_hs_callback_syn(struct sock *sk, struct sk_buff *skb)
1961 struct iucv_sock *iucv, *niucv;
1962 struct af_iucv_trans_hdr *trans_hdr;
1966 trans_hdr = (struct af_iucv_trans_hdr *)skb->data;
1968 /* no sock - connection refused */
1969 afiucv_swap_src_dest(skb);
1970 trans_hdr->flags = AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN;
1971 err = dev_queue_xmit(skb);
1975 nsk = iucv_sock_alloc(NULL, sk->sk_type, GFP_ATOMIC, 0);
1977 if ((sk->sk_state != IUCV_LISTEN) ||
1978 sk_acceptq_is_full(sk) ||
1980 /* error on server socket - connection refused */
1981 afiucv_swap_src_dest(skb);
1982 trans_hdr->flags = AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN;
1983 err = dev_queue_xmit(skb);
1984 iucv_sock_kill(nsk);
1989 niucv = iucv_sk(nsk);
1990 iucv_sock_init(nsk, sk);
1991 niucv->transport = AF_IUCV_TRANS_HIPER;
1992 niucv->msglimit = iucv->msglimit;
1993 if (!trans_hdr->window)
1994 niucv->msglimit_peer = IUCV_HIPER_MSGLIM_DEFAULT;
1996 niucv->msglimit_peer = trans_hdr->window;
1997 memcpy(niucv->dst_name, trans_hdr->srcAppName, 8);
1998 memcpy(niucv->dst_user_id, trans_hdr->srcUserID, 8);
1999 memcpy(niucv->src_name, iucv->src_name, 8);
2000 memcpy(niucv->src_user_id, iucv->src_user_id, 8);
2001 nsk->sk_bound_dev_if = sk->sk_bound_dev_if;
2002 niucv->hs_dev = iucv->hs_dev;
2003 dev_hold(niucv->hs_dev);
2004 afiucv_swap_src_dest(skb);
2005 trans_hdr->flags = AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_ACK;
2006 trans_hdr->window = niucv->msglimit;
2007 /* if receiver acks the xmit connection is established */
2008 err = dev_queue_xmit(skb);
2010 iucv_accept_enqueue(sk, nsk);
2011 nsk->sk_state = IUCV_CONNECTED;
2012 sk->sk_data_ready(sk);
2014 iucv_sock_kill(nsk);
2018 return NET_RX_SUCCESS;
2022 * afiucv_hs_callback_synack() - react on received SYN-ACK
2024 static int afiucv_hs_callback_synack(struct sock *sk, struct sk_buff *skb)
2026 struct iucv_sock *iucv = iucv_sk(sk);
2027 struct af_iucv_trans_hdr *trans_hdr =
2028 (struct af_iucv_trans_hdr *)skb->data;
2032 if (sk->sk_state != IUCV_BOUND)
2035 iucv->msglimit_peer = trans_hdr->window;
2036 sk->sk_state = IUCV_CONNECTED;
2037 sk->sk_state_change(sk);
2041 return NET_RX_SUCCESS;
2045 * afiucv_hs_callback_synfin() - react on received SYN_FIN
2047 static int afiucv_hs_callback_synfin(struct sock *sk, struct sk_buff *skb)
2049 struct iucv_sock *iucv = iucv_sk(sk);
2053 if (sk->sk_state != IUCV_BOUND)
2056 sk->sk_state = IUCV_DISCONN;
2057 sk->sk_state_change(sk);
2061 return NET_RX_SUCCESS;
2065 * afiucv_hs_callback_fin() - react on received FIN
2067 static int afiucv_hs_callback_fin(struct sock *sk, struct sk_buff *skb)
2069 struct iucv_sock *iucv = iucv_sk(sk);
2071 /* other end of connection closed */
2075 if (sk->sk_state == IUCV_CONNECTED) {
2076 sk->sk_state = IUCV_DISCONN;
2077 sk->sk_state_change(sk);
2082 return NET_RX_SUCCESS;
2086 * afiucv_hs_callback_win() - react on received WIN
2088 static int afiucv_hs_callback_win(struct sock *sk, struct sk_buff *skb)
2090 struct iucv_sock *iucv = iucv_sk(sk);
2091 struct af_iucv_trans_hdr *trans_hdr =
2092 (struct af_iucv_trans_hdr *)skb->data;
2095 return NET_RX_SUCCESS;
2097 if (sk->sk_state != IUCV_CONNECTED)
2098 return NET_RX_SUCCESS;
2100 atomic_sub(trans_hdr->window, &iucv->msg_sent);
2101 iucv_sock_wake_msglim(sk);
2102 return NET_RX_SUCCESS;
2106 * afiucv_hs_callback_rx() - react on received data
2108 static int afiucv_hs_callback_rx(struct sock *sk, struct sk_buff *skb)
2110 struct iucv_sock *iucv = iucv_sk(sk);
2114 return NET_RX_SUCCESS;
2117 if (sk->sk_state != IUCV_CONNECTED) {
2119 return NET_RX_SUCCESS;
2122 if (sk->sk_shutdown & RCV_SHUTDOWN) {
2124 return NET_RX_SUCCESS;
2127 /* write stuff from iucv_msg to skb cb */
2128 skb_pull(skb, sizeof(struct af_iucv_trans_hdr));
2129 skb_reset_transport_header(skb);
2130 skb_reset_network_header(skb);
2131 IUCV_SKB_CB(skb)->offset = 0;
2132 if (sk_filter(sk, skb)) {
2133 atomic_inc(&sk->sk_drops); /* skb rejected by filter */
2135 return NET_RX_SUCCESS;
2138 spin_lock(&iucv->message_q.lock);
2139 if (skb_queue_empty(&iucv->backlog_skb_q)) {
2140 if (__sock_queue_rcv_skb(sk, skb))
2141 /* handle rcv queue full */
2142 skb_queue_tail(&iucv->backlog_skb_q, skb);
2144 skb_queue_tail(&iucv_sk(sk)->backlog_skb_q, skb);
2145 spin_unlock(&iucv->message_q.lock);
2146 return NET_RX_SUCCESS;
2150 * afiucv_hs_rcv() - base function for arriving data through HiperSockets
2152 * called from netif RX softirq
2154 static int afiucv_hs_rcv(struct sk_buff *skb, struct net_device *dev,
2155 struct packet_type *pt, struct net_device *orig_dev)
2158 struct iucv_sock *iucv;
2159 struct af_iucv_trans_hdr *trans_hdr;
2163 if (skb->len < (ETH_HLEN + sizeof(struct af_iucv_trans_hdr))) {
2164 WARN_ONCE(1, "AF_IUCV too short skb, len=%d, min=%d",
2166 (int)(ETH_HLEN + sizeof(struct af_iucv_trans_hdr)));
2168 return NET_RX_SUCCESS;
2170 if (skb_headlen(skb) < (ETH_HLEN + sizeof(struct af_iucv_trans_hdr)))
2171 if (skb_linearize(skb)) {
2172 WARN_ONCE(1, "AF_IUCV skb_linearize failed, len=%d",
2175 return NET_RX_SUCCESS;
2177 skb_pull(skb, ETH_HLEN);
2178 trans_hdr = (struct af_iucv_trans_hdr *)skb->data;
2179 EBCASC(trans_hdr->destAppName, sizeof(trans_hdr->destAppName));
2180 EBCASC(trans_hdr->destUserID, sizeof(trans_hdr->destUserID));
2181 EBCASC(trans_hdr->srcAppName, sizeof(trans_hdr->srcAppName));
2182 EBCASC(trans_hdr->srcUserID, sizeof(trans_hdr->srcUserID));
2183 memset(nullstring, 0, sizeof(nullstring));
2186 read_lock(&iucv_sk_list.lock);
2187 sk_for_each(sk, &iucv_sk_list.head) {
2188 if (trans_hdr->flags == AF_IUCV_FLAG_SYN) {
2189 if ((!memcmp(&iucv_sk(sk)->src_name,
2190 trans_hdr->destAppName, 8)) &&
2191 (!memcmp(&iucv_sk(sk)->src_user_id,
2192 trans_hdr->destUserID, 8)) &&
2193 (!memcmp(&iucv_sk(sk)->dst_name, nullstring, 8)) &&
2194 (!memcmp(&iucv_sk(sk)->dst_user_id,
2200 if ((!memcmp(&iucv_sk(sk)->src_name,
2201 trans_hdr->destAppName, 8)) &&
2202 (!memcmp(&iucv_sk(sk)->src_user_id,
2203 trans_hdr->destUserID, 8)) &&
2204 (!memcmp(&iucv_sk(sk)->dst_name,
2205 trans_hdr->srcAppName, 8)) &&
2206 (!memcmp(&iucv_sk(sk)->dst_user_id,
2207 trans_hdr->srcUserID, 8))) {
2213 read_unlock(&iucv_sk_list.lock);
2218 how should we send with no sock
2219 1) send without sock no send rc checking?
2220 2) introduce default sock to handle this cases
2222 SYN -> send SYN|ACK in good case, send SYN|FIN in bad case
2224 SYN|ACK, SYN|FIN, FIN -> no action? */
2226 switch (trans_hdr->flags) {
2227 case AF_IUCV_FLAG_SYN:
2228 /* connect request */
2229 err = afiucv_hs_callback_syn(sk, skb);
2231 case (AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_ACK):
2232 /* connect request confirmed */
2233 err = afiucv_hs_callback_synack(sk, skb);
2235 case (AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN):
2236 /* connect request refused */
2237 err = afiucv_hs_callback_synfin(sk, skb);
2239 case (AF_IUCV_FLAG_FIN):
2241 err = afiucv_hs_callback_fin(sk, skb);
2243 case (AF_IUCV_FLAG_WIN):
2244 err = afiucv_hs_callback_win(sk, skb);
2245 if (skb->len == sizeof(struct af_iucv_trans_hdr)) {
2249 /* fall through and receive non-zero length data */
2250 case (AF_IUCV_FLAG_SHT):
2251 /* shutdown request */
2252 /* fall through and receive zero length data */
2254 /* plain data frame */
2255 IUCV_SKB_CB(skb)->class = trans_hdr->iucv_hdr.class;
2256 err = afiucv_hs_callback_rx(sk, skb);
2266 * afiucv_hs_callback_txnotify() - handle send notifcations from HiperSockets
2269 static void afiucv_hs_callback_txnotify(struct sk_buff *skb,
2270 enum iucv_tx_notify n)
2272 struct sock *isk = skb->sk;
2273 struct sock *sk = NULL;
2274 struct iucv_sock *iucv = NULL;
2275 struct sk_buff_head *list;
2276 struct sk_buff *list_skb;
2277 struct sk_buff *nskb;
2278 unsigned long flags;
2280 read_lock_irqsave(&iucv_sk_list.lock, flags);
2281 sk_for_each(sk, &iucv_sk_list.head)
2286 read_unlock_irqrestore(&iucv_sk_list.lock, flags);
2288 if (!iucv || sock_flag(sk, SOCK_ZAPPED))
2291 list = &iucv->send_skb_q;
2292 spin_lock_irqsave(&list->lock, flags);
2293 if (skb_queue_empty(list))
2295 list_skb = list->next;
2296 nskb = list_skb->next;
2297 while (list_skb != (struct sk_buff *)list) {
2298 if (skb_shinfo(list_skb) == skb_shinfo(skb)) {
2301 __skb_unlink(list_skb, list);
2302 kfree_skb(list_skb);
2303 iucv_sock_wake_msglim(sk);
2305 case TX_NOTIFY_PENDING:
2306 atomic_inc(&iucv->pendings);
2308 case TX_NOTIFY_DELAYED_OK:
2309 __skb_unlink(list_skb, list);
2310 atomic_dec(&iucv->pendings);
2311 if (atomic_read(&iucv->pendings) <= 0)
2312 iucv_sock_wake_msglim(sk);
2313 kfree_skb(list_skb);
2315 case TX_NOTIFY_UNREACHABLE:
2316 case TX_NOTIFY_DELAYED_UNREACHABLE:
2317 case TX_NOTIFY_TPQFULL: /* not yet used */
2318 case TX_NOTIFY_GENERALERROR:
2319 case TX_NOTIFY_DELAYED_GENERALERROR:
2320 __skb_unlink(list_skb, list);
2321 kfree_skb(list_skb);
2322 if (sk->sk_state == IUCV_CONNECTED) {
2323 sk->sk_state = IUCV_DISCONN;
2324 sk->sk_state_change(sk);
2334 spin_unlock_irqrestore(&list->lock, flags);
2336 if (sk->sk_state == IUCV_CLOSING) {
2337 if (skb_queue_empty(&iucv_sk(sk)->send_skb_q)) {
2338 sk->sk_state = IUCV_CLOSED;
2339 sk->sk_state_change(sk);
2346 * afiucv_netdev_event: handle netdev notifier chain events
2348 static int afiucv_netdev_event(struct notifier_block *this,
2349 unsigned long event, void *ptr)
2351 struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
2353 struct iucv_sock *iucv;
2357 case NETDEV_GOING_DOWN:
2358 sk_for_each(sk, &iucv_sk_list.head) {
2360 if ((iucv->hs_dev == event_dev) &&
2361 (sk->sk_state == IUCV_CONNECTED)) {
2362 if (event == NETDEV_GOING_DOWN)
2363 iucv_send_ctrl(sk, AF_IUCV_FLAG_FIN);
2364 sk->sk_state = IUCV_DISCONN;
2365 sk->sk_state_change(sk);
2370 case NETDEV_UNREGISTER:
2377 static struct notifier_block afiucv_netdev_notifier = {
2378 .notifier_call = afiucv_netdev_event,
2381 static const struct proto_ops iucv_sock_ops = {
2383 .owner = THIS_MODULE,
2384 .release = iucv_sock_release,
2385 .bind = iucv_sock_bind,
2386 .connect = iucv_sock_connect,
2387 .listen = iucv_sock_listen,
2388 .accept = iucv_sock_accept,
2389 .getname = iucv_sock_getname,
2390 .sendmsg = iucv_sock_sendmsg,
2391 .recvmsg = iucv_sock_recvmsg,
2392 .poll = iucv_sock_poll,
2393 .ioctl = sock_no_ioctl,
2394 .mmap = sock_no_mmap,
2395 .socketpair = sock_no_socketpair,
2396 .shutdown = iucv_sock_shutdown,
2397 .setsockopt = iucv_sock_setsockopt,
2398 .getsockopt = iucv_sock_getsockopt,
2401 static const struct net_proto_family iucv_sock_family_ops = {
2403 .owner = THIS_MODULE,
2404 .create = iucv_sock_create,
2407 static struct packet_type iucv_packet_type = {
2408 .type = cpu_to_be16(ETH_P_AF_IUCV),
2409 .func = afiucv_hs_rcv,
2412 static int afiucv_iucv_init(void)
2416 err = pr_iucv->iucv_register(&af_iucv_handler, 0);
2419 /* establish dummy device */
2420 af_iucv_driver.bus = pr_iucv->bus;
2421 err = driver_register(&af_iucv_driver);
2424 af_iucv_dev = kzalloc(sizeof(struct device), GFP_KERNEL);
2429 dev_set_name(af_iucv_dev, "af_iucv");
2430 af_iucv_dev->bus = pr_iucv->bus;
2431 af_iucv_dev->parent = pr_iucv->root;
2432 af_iucv_dev->release = (void (*)(struct device *))kfree;
2433 af_iucv_dev->driver = &af_iucv_driver;
2434 err = device_register(af_iucv_dev);
2440 driver_unregister(&af_iucv_driver);
2442 pr_iucv->iucv_unregister(&af_iucv_handler, 0);
2447 static int __init afiucv_init(void)
2451 if (MACHINE_IS_VM) {
2452 cpcmd("QUERY USERID", iucv_userid, sizeof(iucv_userid), &err);
2453 if (unlikely(err)) {
2455 err = -EPROTONOSUPPORT;
2459 pr_iucv = try_then_request_module(symbol_get(iucv_if), "iucv");
2461 printk(KERN_WARNING "iucv_if lookup failed\n");
2462 memset(&iucv_userid, 0, sizeof(iucv_userid));
2465 memset(&iucv_userid, 0, sizeof(iucv_userid));
2469 err = proto_register(&iucv_proto, 0);
2472 err = sock_register(&iucv_sock_family_ops);
2477 err = afiucv_iucv_init();
2481 register_netdevice_notifier(&afiucv_netdev_notifier);
2482 dev_add_pack(&iucv_packet_type);
2486 sock_unregister(PF_IUCV);
2488 proto_unregister(&iucv_proto);
2491 symbol_put(iucv_if);
2495 static void __exit afiucv_exit(void)
2498 device_unregister(af_iucv_dev);
2499 driver_unregister(&af_iucv_driver);
2500 pr_iucv->iucv_unregister(&af_iucv_handler, 0);
2501 symbol_put(iucv_if);
2503 unregister_netdevice_notifier(&afiucv_netdev_notifier);
2504 dev_remove_pack(&iucv_packet_type);
2505 sock_unregister(PF_IUCV);
2506 proto_unregister(&iucv_proto);
2509 module_init(afiucv_init);
2510 module_exit(afiucv_exit);
2512 MODULE_AUTHOR("Jennifer Hunt <jenhunt@us.ibm.com>");
2513 MODULE_DESCRIPTION("IUCV Sockets ver " VERSION);
2514 MODULE_VERSION(VERSION);
2515 MODULE_LICENSE("GPL");
2516 MODULE_ALIAS_NETPROTO(PF_IUCV);