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>
26 #include <asm/ebcdic.h>
27 #include <asm/cpcmd.h>
28 #include <linux/kmod.h>
30 #include <net/iucv/af_iucv.h>
34 static char iucv_userid[80];
36 static const struct proto_ops iucv_sock_ops;
38 static struct proto iucv_proto = {
41 .obj_size = sizeof(struct iucv_sock),
44 static struct iucv_interface *pr_iucv;
46 /* special AF_IUCV IPRM messages */
47 static const u8 iprm_shutdown[8] =
48 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01};
50 #define TRGCLS_SIZE (sizeof(((struct iucv_message *)0)->class))
52 /* macros to set/get socket control buffer at correct offset */
53 #define CB_TAG(skb) ((skb)->cb) /* iucv message tag */
54 #define CB_TAG_LEN (sizeof(((struct iucv_message *) 0)->tag))
55 #define CB_TRGCLS(skb) ((skb)->cb + CB_TAG_LEN) /* iucv msg target class */
56 #define CB_TRGCLS_LEN (TRGCLS_SIZE)
58 #define __iucv_sock_wait(sk, condition, timeo, ret) \
60 DEFINE_WAIT(__wait); \
61 long __timeo = timeo; \
63 prepare_to_wait(sk_sleep(sk), &__wait, TASK_INTERRUPTIBLE); \
64 while (!(condition)) { \
69 if (signal_pending(current)) { \
70 ret = sock_intr_errno(__timeo); \
74 __timeo = schedule_timeout(__timeo); \
76 ret = sock_error(sk); \
80 finish_wait(sk_sleep(sk), &__wait); \
83 #define iucv_sock_wait(sk, condition, timeo) \
87 __iucv_sock_wait(sk, condition, timeo, __ret); \
91 static void iucv_sock_kill(struct sock *sk);
92 static void iucv_sock_close(struct sock *sk);
93 static void iucv_sever_path(struct sock *, int);
95 static int afiucv_hs_rcv(struct sk_buff *skb, struct net_device *dev,
96 struct packet_type *pt, struct net_device *orig_dev);
97 static int afiucv_hs_send(struct iucv_message *imsg, struct sock *sock,
98 struct sk_buff *skb, u8 flags);
99 static void afiucv_hs_callback_txnotify(struct sk_buff *, enum iucv_tx_notify);
101 /* Call Back functions */
102 static void iucv_callback_rx(struct iucv_path *, struct iucv_message *);
103 static void iucv_callback_txdone(struct iucv_path *, struct iucv_message *);
104 static void iucv_callback_connack(struct iucv_path *, u8 ipuser[16]);
105 static int iucv_callback_connreq(struct iucv_path *, u8 ipvmid[8],
107 static void iucv_callback_connrej(struct iucv_path *, u8 ipuser[16]);
108 static void iucv_callback_shutdown(struct iucv_path *, u8 ipuser[16]);
110 static struct iucv_sock_list iucv_sk_list = {
111 .lock = __RW_LOCK_UNLOCKED(iucv_sk_list.lock),
112 .autobind_name = ATOMIC_INIT(0)
115 static struct iucv_handler af_iucv_handler = {
116 .path_pending = iucv_callback_connreq,
117 .path_complete = iucv_callback_connack,
118 .path_severed = iucv_callback_connrej,
119 .message_pending = iucv_callback_rx,
120 .message_complete = iucv_callback_txdone,
121 .path_quiesced = iucv_callback_shutdown,
124 static inline void high_nmcpy(unsigned char *dst, char *src)
129 static inline void low_nmcpy(unsigned char *dst, char *src)
131 memcpy(&dst[8], src, 8);
134 static int afiucv_pm_prepare(struct device *dev)
136 #ifdef CONFIG_PM_DEBUG
137 printk(KERN_WARNING "afiucv_pm_prepare\n");
142 static void afiucv_pm_complete(struct device *dev)
144 #ifdef CONFIG_PM_DEBUG
145 printk(KERN_WARNING "afiucv_pm_complete\n");
150 * afiucv_pm_freeze() - Freeze PM callback
151 * @dev: AFIUCV dummy device
153 * Sever all established IUCV communication pathes
155 static int afiucv_pm_freeze(struct device *dev)
157 struct iucv_sock *iucv;
159 struct hlist_node *node;
162 #ifdef CONFIG_PM_DEBUG
163 printk(KERN_WARNING "afiucv_pm_freeze\n");
165 read_lock(&iucv_sk_list.lock);
166 sk_for_each(sk, node, &iucv_sk_list.head) {
168 switch (sk->sk_state) {
172 iucv_sever_path(sk, 0);
181 skb_queue_purge(&iucv->send_skb_q);
182 skb_queue_purge(&iucv->backlog_skb_q);
184 read_unlock(&iucv_sk_list.lock);
189 * afiucv_pm_restore_thaw() - Thaw and restore PM callback
190 * @dev: AFIUCV dummy device
192 * socket clean up after freeze
194 static int afiucv_pm_restore_thaw(struct device *dev)
197 struct hlist_node *node;
199 #ifdef CONFIG_PM_DEBUG
200 printk(KERN_WARNING "afiucv_pm_restore_thaw\n");
202 read_lock(&iucv_sk_list.lock);
203 sk_for_each(sk, node, &iucv_sk_list.head) {
204 switch (sk->sk_state) {
207 sk->sk_state = IUCV_DISCONN;
208 sk->sk_state_change(sk);
219 read_unlock(&iucv_sk_list.lock);
223 static const struct dev_pm_ops afiucv_pm_ops = {
224 .prepare = afiucv_pm_prepare,
225 .complete = afiucv_pm_complete,
226 .freeze = afiucv_pm_freeze,
227 .thaw = afiucv_pm_restore_thaw,
228 .restore = afiucv_pm_restore_thaw,
231 static struct device_driver af_iucv_driver = {
232 .owner = THIS_MODULE,
235 .pm = &afiucv_pm_ops,
238 /* dummy device used as trigger for PM functions */
239 static struct device *af_iucv_dev;
242 * iucv_msg_length() - Returns the length of an iucv message.
243 * @msg: Pointer to struct iucv_message, MUST NOT be NULL
245 * The function returns the length of the specified iucv message @msg of data
246 * stored in a buffer and of data stored in the parameter list (PRMDATA).
248 * For IUCV_IPRMDATA, AF_IUCV uses the following convention to transport socket
250 * PRMDATA[0..6] socket data (max 7 bytes);
251 * PRMDATA[7] socket data length value (len is 0xff - PRMDATA[7])
253 * The socket data length is computed by subtracting the socket data length
255 * If the socket data len is greater 7, then PRMDATA can be used for special
256 * notifications (see iucv_sock_shutdown); and further,
257 * if the socket data len is > 7, the function returns 8.
259 * Use this function to allocate socket buffers to store iucv message data.
261 static inline size_t iucv_msg_length(struct iucv_message *msg)
265 if (msg->flags & IUCV_IPRMDATA) {
266 datalen = 0xff - msg->rmmsg[7];
267 return (datalen < 8) ? datalen : 8;
273 * iucv_sock_in_state() - check for specific states
274 * @sk: sock structure
275 * @state: first iucv sk state
276 * @state: second iucv sk state
278 * Returns true if the socket in either in the first or second state.
280 static int iucv_sock_in_state(struct sock *sk, int state, int state2)
282 return (sk->sk_state == state || sk->sk_state == state2);
286 * iucv_below_msglim() - function to check if messages can be sent
287 * @sk: sock structure
289 * Returns true if the send queue length is lower than the message limit.
290 * Always returns true if the socket is not connected (no iucv path for
291 * checking the message limit).
293 static inline int iucv_below_msglim(struct sock *sk)
295 struct iucv_sock *iucv = iucv_sk(sk);
297 if (sk->sk_state != IUCV_CONNECTED)
299 if (iucv->transport == AF_IUCV_TRANS_IUCV)
300 return (skb_queue_len(&iucv->send_skb_q) < iucv->path->msglim);
302 return ((atomic_read(&iucv->msg_sent) < iucv->msglimit_peer) &&
303 (atomic_read(&iucv->pendings) <= 0));
307 * iucv_sock_wake_msglim() - Wake up thread waiting on msg limit
309 static void iucv_sock_wake_msglim(struct sock *sk)
311 struct socket_wq *wq;
314 wq = rcu_dereference(sk->sk_wq);
315 if (wq_has_sleeper(wq))
316 wake_up_interruptible_all(&wq->wait);
317 sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
322 * afiucv_hs_send() - send a message through HiperSockets transport
324 static int afiucv_hs_send(struct iucv_message *imsg, struct sock *sock,
325 struct sk_buff *skb, u8 flags)
327 struct iucv_sock *iucv = iucv_sk(sock);
328 struct af_iucv_trans_hdr *phs_hdr;
329 struct sk_buff *nskb;
330 int err, confirm_recv = 0;
332 memset(skb->head, 0, ETH_HLEN);
333 phs_hdr = (struct af_iucv_trans_hdr *)skb_push(skb,
334 sizeof(struct af_iucv_trans_hdr));
335 skb_reset_mac_header(skb);
336 skb_reset_network_header(skb);
337 skb_push(skb, ETH_HLEN);
338 skb_reset_mac_header(skb);
339 memset(phs_hdr, 0, sizeof(struct af_iucv_trans_hdr));
341 phs_hdr->magic = ETH_P_AF_IUCV;
342 phs_hdr->version = 1;
343 phs_hdr->flags = flags;
344 if (flags == AF_IUCV_FLAG_SYN)
345 phs_hdr->window = iucv->msglimit;
346 else if ((flags == AF_IUCV_FLAG_WIN) || !flags) {
347 confirm_recv = atomic_read(&iucv->msg_recv);
348 phs_hdr->window = confirm_recv;
350 phs_hdr->flags = phs_hdr->flags | AF_IUCV_FLAG_WIN;
352 memcpy(phs_hdr->destUserID, iucv->dst_user_id, 8);
353 memcpy(phs_hdr->destAppName, iucv->dst_name, 8);
354 memcpy(phs_hdr->srcUserID, iucv->src_user_id, 8);
355 memcpy(phs_hdr->srcAppName, iucv->src_name, 8);
356 ASCEBC(phs_hdr->destUserID, sizeof(phs_hdr->destUserID));
357 ASCEBC(phs_hdr->destAppName, sizeof(phs_hdr->destAppName));
358 ASCEBC(phs_hdr->srcUserID, sizeof(phs_hdr->srcUserID));
359 ASCEBC(phs_hdr->srcAppName, sizeof(phs_hdr->srcAppName));
361 memcpy(&phs_hdr->iucv_hdr, imsg, sizeof(struct iucv_message));
363 skb->dev = iucv->hs_dev;
366 if (!(skb->dev->flags & IFF_UP) || !netif_carrier_ok(skb->dev))
368 if (skb->len > skb->dev->mtu) {
369 if (sock->sk_type == SOCK_SEQPACKET)
372 skb_trim(skb, skb->dev->mtu);
374 skb->protocol = ETH_P_AF_IUCV;
375 nskb = skb_clone(skb, GFP_ATOMIC);
378 skb_queue_tail(&iucv->send_skb_q, nskb);
379 err = dev_queue_xmit(skb);
380 if (net_xmit_eval(err)) {
381 skb_unlink(nskb, &iucv->send_skb_q);
384 atomic_sub(confirm_recv, &iucv->msg_recv);
385 WARN_ON(atomic_read(&iucv->msg_recv) < 0);
387 return net_xmit_eval(err);
390 static struct sock *__iucv_get_sock_by_name(char *nm)
393 struct hlist_node *node;
395 sk_for_each(sk, node, &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(atomic_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 FIN through an IUCV socket for HIPER transport */
466 static int iucv_send_ctrl(struct sock *sk, u8 flags)
472 blen = sizeof(struct af_iucv_trans_hdr) + ETH_HLEN;
473 skb = sock_alloc_send_skb(sk, blen, 1, &err);
475 skb_reserve(skb, blen);
476 err = afiucv_hs_send(NULL, sk, skb, flags);
481 /* Close an IUCV socket */
482 static void iucv_sock_close(struct sock *sk)
484 struct iucv_sock *iucv = iucv_sk(sk);
490 switch (sk->sk_state) {
492 iucv_sock_cleanup_listen(sk);
496 if (iucv->transport == AF_IUCV_TRANS_HIPER) {
497 err = iucv_send_ctrl(sk, AF_IUCV_FLAG_FIN);
498 sk->sk_state = IUCV_DISCONN;
499 sk->sk_state_change(sk);
501 case IUCV_DISCONN: /* fall through */
502 sk->sk_state = IUCV_CLOSING;
503 sk->sk_state_change(sk);
505 if (!err && !skb_queue_empty(&iucv->send_skb_q)) {
506 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
507 timeo = sk->sk_lingertime;
509 timeo = IUCV_DISCONN_TIMEOUT;
511 iucv_sock_in_state(sk, IUCV_CLOSED, 0),
515 case IUCV_CLOSING: /* fall through */
516 sk->sk_state = IUCV_CLOSED;
517 sk->sk_state_change(sk);
519 sk->sk_err = ECONNRESET;
520 sk->sk_state_change(sk);
522 skb_queue_purge(&iucv->send_skb_q);
523 skb_queue_purge(&iucv->backlog_skb_q);
525 default: /* fall through */
526 iucv_sever_path(sk, 1);
530 dev_put(iucv->hs_dev);
532 sk->sk_bound_dev_if = 0;
535 /* mark socket for deletion by iucv_sock_kill() */
536 sock_set_flag(sk, SOCK_ZAPPED);
541 static void iucv_sock_init(struct sock *sk, struct sock *parent)
544 sk->sk_type = parent->sk_type;
547 static struct sock *iucv_sock_alloc(struct socket *sock, int proto, gfp_t prio)
550 struct iucv_sock *iucv;
552 sk = sk_alloc(&init_net, PF_IUCV, prio, &iucv_proto);
557 sock_init_data(sock, sk);
558 INIT_LIST_HEAD(&iucv->accept_q);
559 spin_lock_init(&iucv->accept_q_lock);
560 skb_queue_head_init(&iucv->send_skb_q);
561 INIT_LIST_HEAD(&iucv->message_q.list);
562 spin_lock_init(&iucv->message_q.lock);
563 skb_queue_head_init(&iucv->backlog_skb_q);
565 atomic_set(&iucv->pendings, 0);
568 atomic_set(&iucv->msg_sent, 0);
569 atomic_set(&iucv->msg_recv, 0);
571 iucv->sk_txnotify = afiucv_hs_callback_txnotify;
572 memset(&iucv->src_user_id , 0, 32);
574 iucv->transport = AF_IUCV_TRANS_IUCV;
576 iucv->transport = AF_IUCV_TRANS_HIPER;
578 sk->sk_destruct = iucv_sock_destruct;
579 sk->sk_sndtimeo = IUCV_CONN_TIMEOUT;
580 sk->sk_allocation = GFP_DMA;
582 sock_reset_flag(sk, SOCK_ZAPPED);
584 sk->sk_protocol = proto;
585 sk->sk_state = IUCV_OPEN;
587 iucv_sock_link(&iucv_sk_list, sk);
591 /* Create an IUCV socket */
592 static int iucv_sock_create(struct net *net, struct socket *sock, int protocol,
597 if (protocol && protocol != PF_IUCV)
598 return -EPROTONOSUPPORT;
600 sock->state = SS_UNCONNECTED;
602 switch (sock->type) {
604 sock->ops = &iucv_sock_ops;
607 /* currently, proto ops can handle both sk types */
608 sock->ops = &iucv_sock_ops;
611 return -ESOCKTNOSUPPORT;
614 sk = iucv_sock_alloc(sock, protocol, GFP_KERNEL);
618 iucv_sock_init(sk, NULL);
623 void iucv_sock_link(struct iucv_sock_list *l, struct sock *sk)
625 write_lock_bh(&l->lock);
626 sk_add_node(sk, &l->head);
627 write_unlock_bh(&l->lock);
630 void iucv_sock_unlink(struct iucv_sock_list *l, struct sock *sk)
632 write_lock_bh(&l->lock);
633 sk_del_node_init(sk);
634 write_unlock_bh(&l->lock);
637 void iucv_accept_enqueue(struct sock *parent, struct sock *sk)
640 struct iucv_sock *par = iucv_sk(parent);
643 spin_lock_irqsave(&par->accept_q_lock, flags);
644 list_add_tail(&iucv_sk(sk)->accept_q, &par->accept_q);
645 spin_unlock_irqrestore(&par->accept_q_lock, flags);
646 iucv_sk(sk)->parent = parent;
647 sk_acceptq_added(parent);
650 void iucv_accept_unlink(struct sock *sk)
653 struct iucv_sock *par = iucv_sk(iucv_sk(sk)->parent);
655 spin_lock_irqsave(&par->accept_q_lock, flags);
656 list_del_init(&iucv_sk(sk)->accept_q);
657 spin_unlock_irqrestore(&par->accept_q_lock, flags);
658 sk_acceptq_removed(iucv_sk(sk)->parent);
659 iucv_sk(sk)->parent = NULL;
663 struct sock *iucv_accept_dequeue(struct sock *parent, struct socket *newsock)
665 struct iucv_sock *isk, *n;
668 list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) {
669 sk = (struct sock *) isk;
672 if (sk->sk_state == IUCV_CLOSED) {
673 iucv_accept_unlink(sk);
678 if (sk->sk_state == IUCV_CONNECTED ||
679 sk->sk_state == IUCV_DISCONN ||
681 iucv_accept_unlink(sk);
683 sock_graft(sk, newsock);
694 /* Bind an unbound socket */
695 static int iucv_sock_bind(struct socket *sock, struct sockaddr *addr,
698 struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
699 struct sock *sk = sock->sk;
700 struct iucv_sock *iucv;
702 struct net_device *dev;
705 /* Verify the input sockaddr */
706 if (!addr || addr->sa_family != AF_IUCV)
710 if (sk->sk_state != IUCV_OPEN) {
715 write_lock_bh(&iucv_sk_list.lock);
718 if (__iucv_get_sock_by_name(sa->siucv_name)) {
725 /* Bind the socket */
727 if (!memcmp(sa->siucv_user_id, iucv_userid, 8))
728 goto vm_bind; /* VM IUCV transport */
730 /* try hiper transport */
731 memcpy(uid, sa->siucv_user_id, sizeof(uid));
734 for_each_netdev_rcu(&init_net, dev) {
735 if (!memcmp(dev->perm_addr, uid, 8)) {
736 memcpy(iucv->src_name, sa->siucv_name, 8);
737 memcpy(iucv->src_user_id, sa->siucv_user_id, 8);
738 sk->sk_bound_dev_if = dev->ifindex;
741 sk->sk_state = IUCV_BOUND;
742 iucv->transport = AF_IUCV_TRANS_HIPER;
744 iucv->msglimit = IUCV_HIPER_MSGLIM_DEFAULT;
752 /* use local userid for backward compat */
753 memcpy(iucv->src_name, sa->siucv_name, 8);
754 memcpy(iucv->src_user_id, iucv_userid, 8);
755 sk->sk_state = IUCV_BOUND;
756 iucv->transport = AF_IUCV_TRANS_IUCV;
758 iucv->msglimit = IUCV_QUEUELEN_DEFAULT;
761 /* found no dev to bind */
764 /* Release the socket list lock */
765 write_unlock_bh(&iucv_sk_list.lock);
771 /* Automatically bind an unbound socket */
772 static int iucv_sock_autobind(struct sock *sk)
774 struct iucv_sock *iucv = iucv_sk(sk);
778 if (unlikely(!pr_iucv))
781 memcpy(iucv->src_user_id, iucv_userid, 8);
783 write_lock_bh(&iucv_sk_list.lock);
785 sprintf(name, "%08x", atomic_inc_return(&iucv_sk_list.autobind_name));
786 while (__iucv_get_sock_by_name(name)) {
787 sprintf(name, "%08x",
788 atomic_inc_return(&iucv_sk_list.autobind_name));
791 write_unlock_bh(&iucv_sk_list.lock);
793 memcpy(&iucv->src_name, name, 8);
796 iucv->msglimit = IUCV_QUEUELEN_DEFAULT;
801 static int afiucv_path_connect(struct socket *sock, struct sockaddr *addr)
803 struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
804 struct sock *sk = sock->sk;
805 struct iucv_sock *iucv = iucv_sk(sk);
806 unsigned char user_data[16];
809 high_nmcpy(user_data, sa->siucv_name);
810 low_nmcpy(user_data, iucv->src_name);
811 ASCEBC(user_data, sizeof(user_data));
814 iucv->path = iucv_path_alloc(iucv->msglimit,
815 IUCV_IPRMDATA, GFP_KERNEL);
820 err = pr_iucv->path_connect(iucv->path, &af_iucv_handler,
821 sa->siucv_user_id, NULL, user_data,
824 iucv_path_free(iucv->path);
827 case 0x0b: /* Target communicator is not logged on */
830 case 0x0d: /* Max connections for this guest exceeded */
831 case 0x0e: /* Max connections for target guest exceeded */
834 case 0x0f: /* Missing IUCV authorization */
846 /* Connect an unconnected socket */
847 static int iucv_sock_connect(struct socket *sock, struct sockaddr *addr,
850 struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
851 struct sock *sk = sock->sk;
852 struct iucv_sock *iucv = iucv_sk(sk);
855 if (addr->sa_family != AF_IUCV || alen < sizeof(struct sockaddr_iucv))
858 if (sk->sk_state != IUCV_OPEN && sk->sk_state != IUCV_BOUND)
861 if (sk->sk_state == IUCV_OPEN &&
862 iucv->transport == AF_IUCV_TRANS_HIPER)
863 return -EBADFD; /* explicit bind required */
865 if (sk->sk_type != SOCK_STREAM && sk->sk_type != SOCK_SEQPACKET)
868 if (sk->sk_state == IUCV_OPEN) {
869 err = iucv_sock_autobind(sk);
876 /* Set the destination information */
877 memcpy(iucv->dst_user_id, sa->siucv_user_id, 8);
878 memcpy(iucv->dst_name, sa->siucv_name, 8);
880 if (iucv->transport == AF_IUCV_TRANS_HIPER)
881 err = iucv_send_ctrl(sock->sk, AF_IUCV_FLAG_SYN);
883 err = afiucv_path_connect(sock, addr);
887 if (sk->sk_state != IUCV_CONNECTED)
888 err = iucv_sock_wait(sk, iucv_sock_in_state(sk, IUCV_CONNECTED,
890 sock_sndtimeo(sk, flags & O_NONBLOCK));
892 if (sk->sk_state == IUCV_DISCONN || sk->sk_state == IUCV_CLOSED)
895 if (err && iucv->transport == AF_IUCV_TRANS_IUCV)
896 iucv_sever_path(sk, 0);
903 /* Move a socket into listening state. */
904 static int iucv_sock_listen(struct socket *sock, int backlog)
906 struct sock *sk = sock->sk;
912 if (sk->sk_state != IUCV_BOUND)
915 if (sock->type != SOCK_STREAM && sock->type != SOCK_SEQPACKET)
918 sk->sk_max_ack_backlog = backlog;
919 sk->sk_ack_backlog = 0;
920 sk->sk_state = IUCV_LISTEN;
928 /* Accept a pending connection */
929 static int iucv_sock_accept(struct socket *sock, struct socket *newsock,
932 DECLARE_WAITQUEUE(wait, current);
933 struct sock *sk = sock->sk, *nsk;
937 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
939 if (sk->sk_state != IUCV_LISTEN) {
944 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
946 /* Wait for an incoming connection */
947 add_wait_queue_exclusive(sk_sleep(sk), &wait);
948 while (!(nsk = iucv_accept_dequeue(sk, newsock))) {
949 set_current_state(TASK_INTERRUPTIBLE);
956 timeo = schedule_timeout(timeo);
957 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
959 if (sk->sk_state != IUCV_LISTEN) {
964 if (signal_pending(current)) {
965 err = sock_intr_errno(timeo);
970 set_current_state(TASK_RUNNING);
971 remove_wait_queue(sk_sleep(sk), &wait);
976 newsock->state = SS_CONNECTED;
983 static int iucv_sock_getname(struct socket *sock, struct sockaddr *addr,
986 struct sockaddr_iucv *siucv = (struct sockaddr_iucv *) addr;
987 struct sock *sk = sock->sk;
988 struct iucv_sock *iucv = iucv_sk(sk);
990 addr->sa_family = AF_IUCV;
991 *len = sizeof(struct sockaddr_iucv);
994 memcpy(siucv->siucv_user_id, iucv->dst_user_id, 8);
995 memcpy(siucv->siucv_name, iucv->dst_name, 8);
997 memcpy(siucv->siucv_user_id, iucv->src_user_id, 8);
998 memcpy(siucv->siucv_name, iucv->src_name, 8);
1000 memset(&siucv->siucv_port, 0, sizeof(siucv->siucv_port));
1001 memset(&siucv->siucv_addr, 0, sizeof(siucv->siucv_addr));
1002 memset(&siucv->siucv_nodeid, 0, sizeof(siucv->siucv_nodeid));
1008 * iucv_send_iprm() - Send socket data in parameter list of an iucv message.
1010 * @msg: Pointer to a struct iucv_message
1011 * @skb: The socket data to send, skb->len MUST BE <= 7
1013 * Send the socket data in the parameter list in the iucv message
1014 * (IUCV_IPRMDATA). The socket data is stored at index 0 to 6 in the parameter
1015 * list and the socket data len at index 7 (last byte).
1016 * See also iucv_msg_length().
1018 * Returns the error code from the iucv_message_send() call.
1020 static int iucv_send_iprm(struct iucv_path *path, struct iucv_message *msg,
1021 struct sk_buff *skb)
1025 memcpy(prmdata, (void *) skb->data, skb->len);
1026 prmdata[7] = 0xff - (u8) skb->len;
1027 return pr_iucv->message_send(path, msg, IUCV_IPRMDATA, 0,
1028 (void *) prmdata, 8);
1031 static int iucv_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
1032 struct msghdr *msg, size_t len)
1034 struct sock *sk = sock->sk;
1035 struct iucv_sock *iucv = iucv_sk(sk);
1036 struct sk_buff *skb;
1037 struct iucv_message txmsg;
1038 struct cmsghdr *cmsg;
1044 int noblock = msg->msg_flags & MSG_DONTWAIT;
1046 err = sock_error(sk);
1050 if (msg->msg_flags & MSG_OOB)
1053 /* SOCK_SEQPACKET: we do not support segmented records */
1054 if (sk->sk_type == SOCK_SEQPACKET && !(msg->msg_flags & MSG_EOR))
1059 if (sk->sk_shutdown & SEND_SHUTDOWN) {
1064 /* Return if the socket is not in connected state */
1065 if (sk->sk_state != IUCV_CONNECTED) {
1070 /* initialize defaults */
1071 cmsg_done = 0; /* check for duplicate headers */
1074 /* iterate over control messages */
1075 for (cmsg = CMSG_FIRSTHDR(msg); cmsg;
1076 cmsg = CMSG_NXTHDR(msg, cmsg)) {
1078 if (!CMSG_OK(msg, cmsg)) {
1083 if (cmsg->cmsg_level != SOL_IUCV)
1086 if (cmsg->cmsg_type & cmsg_done) {
1090 cmsg_done |= cmsg->cmsg_type;
1092 switch (cmsg->cmsg_type) {
1093 case SCM_IUCV_TRGCLS:
1094 if (cmsg->cmsg_len != CMSG_LEN(TRGCLS_SIZE)) {
1099 /* set iucv message target class */
1100 memcpy(&txmsg.class,
1101 (void *) CMSG_DATA(cmsg), TRGCLS_SIZE);
1112 /* allocate one skb for each iucv message:
1113 * this is fine for SOCK_SEQPACKET (unless we want to support
1114 * segmented records using the MSG_EOR flag), but
1115 * for SOCK_STREAM we might want to improve it in future */
1116 if (iucv->transport == AF_IUCV_TRANS_HIPER)
1117 skb = sock_alloc_send_skb(sk,
1118 len + sizeof(struct af_iucv_trans_hdr) + ETH_HLEN,
1121 skb = sock_alloc_send_skb(sk, len, noblock, &err);
1126 if (iucv->transport == AF_IUCV_TRANS_HIPER)
1127 skb_reserve(skb, sizeof(struct af_iucv_trans_hdr) + ETH_HLEN);
1128 if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
1133 /* wait if outstanding messages for iucv path has reached */
1134 timeo = sock_sndtimeo(sk, noblock);
1135 err = iucv_sock_wait(sk, iucv_below_msglim(sk), timeo);
1139 /* return -ECONNRESET if the socket is no longer connected */
1140 if (sk->sk_state != IUCV_CONNECTED) {
1145 /* increment and save iucv message tag for msg_completion cbk */
1146 txmsg.tag = iucv->send_tag++;
1147 memcpy(CB_TAG(skb), &txmsg.tag, CB_TAG_LEN);
1149 if (iucv->transport == AF_IUCV_TRANS_HIPER) {
1150 atomic_inc(&iucv->msg_sent);
1151 err = afiucv_hs_send(&txmsg, sk, skb, 0);
1153 atomic_dec(&iucv->msg_sent);
1158 skb_queue_tail(&iucv->send_skb_q, skb);
1160 if (((iucv->path->flags & IUCV_IPRMDATA) & iucv->flags)
1162 err = iucv_send_iprm(iucv->path, &txmsg, skb);
1164 /* on success: there is no message_complete callback
1165 * for an IPRMDATA msg; remove skb from send queue */
1167 skb_unlink(skb, &iucv->send_skb_q);
1171 /* this error should never happen since the
1172 * IUCV_IPRMDATA path flag is set... sever path */
1174 pr_iucv->path_sever(iucv->path, NULL);
1175 skb_unlink(skb, &iucv->send_skb_q);
1180 err = pr_iucv->message_send(iucv->path, &txmsg, 0, 0,
1181 (void *) skb->data, skb->len);
1185 memcpy(user_id, iucv->dst_user_id, 8);
1187 memcpy(appl_id, iucv->dst_name, 8);
1188 pr_err("Application %s on z/VM guest %s"
1189 " exceeds message limit\n",
1194 skb_unlink(skb, &iucv->send_skb_q);
1209 /* iucv_fragment_skb() - Fragment a single IUCV message into multiple skb's
1211 * Locking: must be called with message_q.lock held
1213 static int iucv_fragment_skb(struct sock *sk, struct sk_buff *skb, int len)
1215 int dataleft, size, copied = 0;
1216 struct sk_buff *nskb;
1220 if (dataleft >= sk->sk_rcvbuf / 4)
1221 size = sk->sk_rcvbuf / 4;
1225 nskb = alloc_skb(size, GFP_ATOMIC | GFP_DMA);
1229 /* copy target class to control buffer of new skb */
1230 memcpy(CB_TRGCLS(nskb), CB_TRGCLS(skb), CB_TRGCLS_LEN);
1232 /* copy data fragment */
1233 memcpy(nskb->data, skb->data + copied, size);
1237 skb_reset_transport_header(nskb);
1238 skb_reset_network_header(nskb);
1241 skb_queue_tail(&iucv_sk(sk)->backlog_skb_q, nskb);
1247 /* iucv_process_message() - Receive a single outstanding IUCV message
1249 * Locking: must be called with message_q.lock held
1251 static void iucv_process_message(struct sock *sk, struct sk_buff *skb,
1252 struct iucv_path *path,
1253 struct iucv_message *msg)
1258 len = iucv_msg_length(msg);
1260 /* store msg target class in the second 4 bytes of skb ctrl buffer */
1261 /* Note: the first 4 bytes are reserved for msg tag */
1262 memcpy(CB_TRGCLS(skb), &msg->class, CB_TRGCLS_LEN);
1264 /* check for special IPRM messages (e.g. iucv_sock_shutdown) */
1265 if ((msg->flags & IUCV_IPRMDATA) && len > 7) {
1266 if (memcmp(msg->rmmsg, iprm_shutdown, 8) == 0) {
1271 rc = pr_iucv->message_receive(path, msg,
1272 msg->flags & IUCV_IPRMDATA,
1273 skb->data, len, NULL);
1278 /* we need to fragment iucv messages for SOCK_STREAM only;
1279 * for SOCK_SEQPACKET, it is only relevant if we support
1280 * record segmentation using MSG_EOR (see also recvmsg()) */
1281 if (sk->sk_type == SOCK_STREAM &&
1282 skb->truesize >= sk->sk_rcvbuf / 4) {
1283 rc = iucv_fragment_skb(sk, skb, len);
1287 pr_iucv->path_sever(path, NULL);
1290 skb = skb_dequeue(&iucv_sk(sk)->backlog_skb_q);
1292 skb_reset_transport_header(skb);
1293 skb_reset_network_header(skb);
1298 if (sock_queue_rcv_skb(sk, skb))
1299 skb_queue_head(&iucv_sk(sk)->backlog_skb_q, skb);
1302 /* iucv_process_message_q() - Process outstanding IUCV messages
1304 * Locking: must be called with message_q.lock held
1306 static void iucv_process_message_q(struct sock *sk)
1308 struct iucv_sock *iucv = iucv_sk(sk);
1309 struct sk_buff *skb;
1310 struct sock_msg_q *p, *n;
1312 list_for_each_entry_safe(p, n, &iucv->message_q.list, list) {
1313 skb = alloc_skb(iucv_msg_length(&p->msg), GFP_ATOMIC | GFP_DMA);
1316 iucv_process_message(sk, skb, p->path, &p->msg);
1319 if (!skb_queue_empty(&iucv->backlog_skb_q))
1324 static int iucv_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
1325 struct msghdr *msg, size_t len, int flags)
1327 int noblock = flags & MSG_DONTWAIT;
1328 struct sock *sk = sock->sk;
1329 struct iucv_sock *iucv = iucv_sk(sk);
1330 unsigned int copied, rlen;
1331 struct sk_buff *skb, *rskb, *cskb;
1334 if ((sk->sk_state == IUCV_DISCONN) &&
1335 skb_queue_empty(&iucv->backlog_skb_q) &&
1336 skb_queue_empty(&sk->sk_receive_queue) &&
1337 list_empty(&iucv->message_q.list))
1340 if (flags & (MSG_OOB))
1343 /* receive/dequeue next skb:
1344 * the function understands MSG_PEEK and, thus, does not dequeue skb */
1345 skb = skb_recv_datagram(sk, flags, noblock, &err);
1347 if (sk->sk_shutdown & RCV_SHUTDOWN)
1352 rlen = skb->len; /* real length of skb */
1353 copied = min_t(unsigned int, rlen, len);
1355 sk->sk_shutdown = sk->sk_shutdown | RCV_SHUTDOWN;
1358 if (skb_copy_datagram_iovec(cskb, 0, msg->msg_iov, copied)) {
1359 if (!(flags & MSG_PEEK))
1360 skb_queue_head(&sk->sk_receive_queue, skb);
1364 /* SOCK_SEQPACKET: set MSG_TRUNC if recv buf size is too small */
1365 if (sk->sk_type == SOCK_SEQPACKET) {
1367 msg->msg_flags |= MSG_TRUNC;
1368 /* each iucv message contains a complete record */
1369 msg->msg_flags |= MSG_EOR;
1372 /* create control message to store iucv msg target class:
1373 * get the trgcls from the control buffer of the skb due to
1374 * fragmentation of original iucv message. */
1375 err = put_cmsg(msg, SOL_IUCV, SCM_IUCV_TRGCLS,
1376 CB_TRGCLS_LEN, CB_TRGCLS(skb));
1378 if (!(flags & MSG_PEEK))
1379 skb_queue_head(&sk->sk_receive_queue, skb);
1383 /* Mark read part of skb as used */
1384 if (!(flags & MSG_PEEK)) {
1386 /* SOCK_STREAM: re-queue skb if it contains unreceived data */
1387 if (sk->sk_type == SOCK_STREAM) {
1388 skb_pull(skb, copied);
1390 skb_queue_head(&sk->sk_receive_queue, skb);
1396 if (iucv->transport == AF_IUCV_TRANS_HIPER) {
1397 atomic_inc(&iucv->msg_recv);
1398 if (atomic_read(&iucv->msg_recv) > iucv->msglimit) {
1400 iucv_sock_close(sk);
1405 /* Queue backlog skbs */
1406 spin_lock_bh(&iucv->message_q.lock);
1407 rskb = skb_dequeue(&iucv->backlog_skb_q);
1409 if (sock_queue_rcv_skb(sk, rskb)) {
1410 skb_queue_head(&iucv->backlog_skb_q,
1414 rskb = skb_dequeue(&iucv->backlog_skb_q);
1417 if (skb_queue_empty(&iucv->backlog_skb_q)) {
1418 if (!list_empty(&iucv->message_q.list))
1419 iucv_process_message_q(sk);
1420 if (atomic_read(&iucv->msg_recv) >=
1421 iucv->msglimit / 2) {
1422 err = iucv_send_ctrl(sk, AF_IUCV_FLAG_WIN);
1424 sk->sk_state = IUCV_DISCONN;
1425 sk->sk_state_change(sk);
1429 spin_unlock_bh(&iucv->message_q.lock);
1433 /* SOCK_SEQPACKET: return real length if MSG_TRUNC is set */
1434 if (sk->sk_type == SOCK_SEQPACKET && (flags & MSG_TRUNC))
1440 static inline unsigned int iucv_accept_poll(struct sock *parent)
1442 struct iucv_sock *isk, *n;
1445 list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) {
1446 sk = (struct sock *) isk;
1448 if (sk->sk_state == IUCV_CONNECTED)
1449 return POLLIN | POLLRDNORM;
1455 unsigned int iucv_sock_poll(struct file *file, struct socket *sock,
1458 struct sock *sk = sock->sk;
1459 unsigned int mask = 0;
1461 sock_poll_wait(file, sk_sleep(sk), wait);
1463 if (sk->sk_state == IUCV_LISTEN)
1464 return iucv_accept_poll(sk);
1466 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
1469 if (sk->sk_shutdown & RCV_SHUTDOWN)
1472 if (sk->sk_shutdown == SHUTDOWN_MASK)
1475 if (!skb_queue_empty(&sk->sk_receive_queue) ||
1476 (sk->sk_shutdown & RCV_SHUTDOWN))
1477 mask |= POLLIN | POLLRDNORM;
1479 if (sk->sk_state == IUCV_CLOSED)
1482 if (sk->sk_state == IUCV_DISCONN)
1485 if (sock_writeable(sk) && iucv_below_msglim(sk))
1486 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
1488 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
1493 static int iucv_sock_shutdown(struct socket *sock, int how)
1495 struct sock *sk = sock->sk;
1496 struct iucv_sock *iucv = iucv_sk(sk);
1497 struct iucv_message txmsg;
1502 if ((how & ~SHUTDOWN_MASK) || !how)
1506 switch (sk->sk_state) {
1517 if (how == SEND_SHUTDOWN || how == SHUTDOWN_MASK) {
1518 if (iucv->transport == AF_IUCV_TRANS_IUCV) {
1521 err = pr_iucv->message_send(iucv->path, &txmsg,
1522 IUCV_IPRMDATA, 0, (void *) iprm_shutdown, 8);
1537 iucv_send_ctrl(sk, AF_IUCV_FLAG_SHT);
1540 sk->sk_shutdown |= how;
1541 if (how == RCV_SHUTDOWN || how == SHUTDOWN_MASK) {
1542 if (iucv->transport == AF_IUCV_TRANS_IUCV) {
1543 err = pr_iucv->path_quiesce(iucv->path, NULL);
1546 /* skb_queue_purge(&sk->sk_receive_queue); */
1548 skb_queue_purge(&sk->sk_receive_queue);
1551 /* Wake up anyone sleeping in poll */
1552 sk->sk_state_change(sk);
1559 static int iucv_sock_release(struct socket *sock)
1561 struct sock *sk = sock->sk;
1567 iucv_sock_close(sk);
1574 /* getsockopt and setsockopt */
1575 static int iucv_sock_setsockopt(struct socket *sock, int level, int optname,
1576 char __user *optval, unsigned int optlen)
1578 struct sock *sk = sock->sk;
1579 struct iucv_sock *iucv = iucv_sk(sk);
1583 if (level != SOL_IUCV)
1584 return -ENOPROTOOPT;
1586 if (optlen < sizeof(int))
1589 if (get_user(val, (int __user *) optval))
1596 case SO_IPRMDATA_MSG:
1598 iucv->flags |= IUCV_IPRMDATA;
1600 iucv->flags &= ~IUCV_IPRMDATA;
1603 switch (sk->sk_state) {
1606 if (val < 1 || val > (u16)(~0))
1609 iucv->msglimit = val;
1625 static int iucv_sock_getsockopt(struct socket *sock, int level, int optname,
1626 char __user *optval, int __user *optlen)
1628 struct sock *sk = sock->sk;
1629 struct iucv_sock *iucv = iucv_sk(sk);
1633 if (level != SOL_IUCV)
1634 return -ENOPROTOOPT;
1636 if (get_user(len, optlen))
1642 len = min_t(unsigned int, len, sizeof(int));
1645 case SO_IPRMDATA_MSG:
1646 val = (iucv->flags & IUCV_IPRMDATA) ? 1 : 0;
1650 val = (iucv->path != NULL) ? iucv->path->msglim /* connected */
1651 : iucv->msglimit; /* default */
1655 if (sk->sk_state == IUCV_OPEN)
1657 val = (iucv->hs_dev) ? iucv->hs_dev->mtu -
1658 sizeof(struct af_iucv_trans_hdr) - ETH_HLEN :
1662 return -ENOPROTOOPT;
1665 if (put_user(len, optlen))
1667 if (copy_to_user(optval, &val, len))
1674 /* Callback wrappers - called from iucv base support */
1675 static int iucv_callback_connreq(struct iucv_path *path,
1676 u8 ipvmid[8], u8 ipuser[16])
1678 unsigned char user_data[16];
1679 unsigned char nuser_data[16];
1680 unsigned char src_name[8];
1681 struct hlist_node *node;
1682 struct sock *sk, *nsk;
1683 struct iucv_sock *iucv, *niucv;
1686 memcpy(src_name, ipuser, 8);
1687 EBCASC(src_name, 8);
1688 /* Find out if this path belongs to af_iucv. */
1689 read_lock(&iucv_sk_list.lock);
1692 sk_for_each(sk, node, &iucv_sk_list.head)
1693 if (sk->sk_state == IUCV_LISTEN &&
1694 !memcmp(&iucv_sk(sk)->src_name, src_name, 8)) {
1696 * Found a listening socket with
1697 * src_name == ipuser[0-7].
1702 read_unlock(&iucv_sk_list.lock);
1704 /* No socket found, not one of our paths. */
1709 /* Check if parent socket is listening */
1710 low_nmcpy(user_data, iucv->src_name);
1711 high_nmcpy(user_data, iucv->dst_name);
1712 ASCEBC(user_data, sizeof(user_data));
1713 if (sk->sk_state != IUCV_LISTEN) {
1714 err = pr_iucv->path_sever(path, user_data);
1715 iucv_path_free(path);
1719 /* Check for backlog size */
1720 if (sk_acceptq_is_full(sk)) {
1721 err = pr_iucv->path_sever(path, user_data);
1722 iucv_path_free(path);
1726 /* Create the new socket */
1727 nsk = iucv_sock_alloc(NULL, sk->sk_type, GFP_ATOMIC);
1729 err = pr_iucv->path_sever(path, user_data);
1730 iucv_path_free(path);
1734 niucv = iucv_sk(nsk);
1735 iucv_sock_init(nsk, sk);
1737 /* Set the new iucv_sock */
1738 memcpy(niucv->dst_name, ipuser + 8, 8);
1739 EBCASC(niucv->dst_name, 8);
1740 memcpy(niucv->dst_user_id, ipvmid, 8);
1741 memcpy(niucv->src_name, iucv->src_name, 8);
1742 memcpy(niucv->src_user_id, iucv->src_user_id, 8);
1745 /* Call iucv_accept */
1746 high_nmcpy(nuser_data, ipuser + 8);
1747 memcpy(nuser_data + 8, niucv->src_name, 8);
1748 ASCEBC(nuser_data + 8, 8);
1750 /* set message limit for path based on msglimit of accepting socket */
1751 niucv->msglimit = iucv->msglimit;
1752 path->msglim = iucv->msglimit;
1753 err = pr_iucv->path_accept(path, &af_iucv_handler, nuser_data, nsk);
1755 iucv_sever_path(nsk, 1);
1756 iucv_sock_kill(nsk);
1760 iucv_accept_enqueue(sk, nsk);
1762 /* Wake up accept */
1763 nsk->sk_state = IUCV_CONNECTED;
1764 sk->sk_data_ready(sk, 1);
1771 static void iucv_callback_connack(struct iucv_path *path, u8 ipuser[16])
1773 struct sock *sk = path->private;
1775 sk->sk_state = IUCV_CONNECTED;
1776 sk->sk_state_change(sk);
1779 static void iucv_callback_rx(struct iucv_path *path, struct iucv_message *msg)
1781 struct sock *sk = path->private;
1782 struct iucv_sock *iucv = iucv_sk(sk);
1783 struct sk_buff *skb;
1784 struct sock_msg_q *save_msg;
1787 if (sk->sk_shutdown & RCV_SHUTDOWN) {
1788 pr_iucv->message_reject(path, msg);
1792 spin_lock(&iucv->message_q.lock);
1794 if (!list_empty(&iucv->message_q.list) ||
1795 !skb_queue_empty(&iucv->backlog_skb_q))
1798 len = atomic_read(&sk->sk_rmem_alloc);
1799 len += SKB_TRUESIZE(iucv_msg_length(msg));
1800 if (len > sk->sk_rcvbuf)
1803 skb = alloc_skb(iucv_msg_length(msg), GFP_ATOMIC | GFP_DMA);
1807 iucv_process_message(sk, skb, path, msg);
1811 save_msg = kzalloc(sizeof(struct sock_msg_q), GFP_ATOMIC | GFP_DMA);
1814 save_msg->path = path;
1815 save_msg->msg = *msg;
1817 list_add_tail(&save_msg->list, &iucv->message_q.list);
1820 spin_unlock(&iucv->message_q.lock);
1823 static void iucv_callback_txdone(struct iucv_path *path,
1824 struct iucv_message *msg)
1826 struct sock *sk = path->private;
1827 struct sk_buff *this = NULL;
1828 struct sk_buff_head *list = &iucv_sk(sk)->send_skb_q;
1829 struct sk_buff *list_skb = list->next;
1830 unsigned long flags;
1833 if (!skb_queue_empty(list)) {
1834 spin_lock_irqsave(&list->lock, flags);
1836 while (list_skb != (struct sk_buff *)list) {
1837 if (!memcmp(&msg->tag, CB_TAG(list_skb), CB_TAG_LEN)) {
1841 list_skb = list_skb->next;
1844 __skb_unlink(this, list);
1846 spin_unlock_irqrestore(&list->lock, flags);
1850 /* wake up any process waiting for sending */
1851 iucv_sock_wake_msglim(sk);
1855 if (sk->sk_state == IUCV_CLOSING) {
1856 if (skb_queue_empty(&iucv_sk(sk)->send_skb_q)) {
1857 sk->sk_state = IUCV_CLOSED;
1858 sk->sk_state_change(sk);
1865 static void iucv_callback_connrej(struct iucv_path *path, u8 ipuser[16])
1867 struct sock *sk = path->private;
1869 if (sk->sk_state == IUCV_CLOSED)
1873 iucv_sever_path(sk, 1);
1874 sk->sk_state = IUCV_DISCONN;
1876 sk->sk_state_change(sk);
1880 /* called if the other communication side shuts down its RECV direction;
1881 * in turn, the callback sets SEND_SHUTDOWN to disable sending of data.
1883 static void iucv_callback_shutdown(struct iucv_path *path, u8 ipuser[16])
1885 struct sock *sk = path->private;
1888 if (sk->sk_state != IUCV_CLOSED) {
1889 sk->sk_shutdown |= SEND_SHUTDOWN;
1890 sk->sk_state_change(sk);
1895 /***************** HiperSockets transport callbacks ********************/
1896 static void afiucv_swap_src_dest(struct sk_buff *skb)
1898 struct af_iucv_trans_hdr *trans_hdr =
1899 (struct af_iucv_trans_hdr *)skb->data;
1903 ASCEBC(trans_hdr->destUserID, sizeof(trans_hdr->destUserID));
1904 ASCEBC(trans_hdr->destAppName, sizeof(trans_hdr->destAppName));
1905 ASCEBC(trans_hdr->srcUserID, sizeof(trans_hdr->srcUserID));
1906 ASCEBC(trans_hdr->srcAppName, sizeof(trans_hdr->srcAppName));
1907 memcpy(tmpID, trans_hdr->srcUserID, 8);
1908 memcpy(tmpName, trans_hdr->srcAppName, 8);
1909 memcpy(trans_hdr->srcUserID, trans_hdr->destUserID, 8);
1910 memcpy(trans_hdr->srcAppName, trans_hdr->destAppName, 8);
1911 memcpy(trans_hdr->destUserID, tmpID, 8);
1912 memcpy(trans_hdr->destAppName, tmpName, 8);
1913 skb_push(skb, ETH_HLEN);
1914 memset(skb->data, 0, ETH_HLEN);
1918 * afiucv_hs_callback_syn - react on received SYN
1920 static int afiucv_hs_callback_syn(struct sock *sk, struct sk_buff *skb)
1923 struct iucv_sock *iucv, *niucv;
1924 struct af_iucv_trans_hdr *trans_hdr;
1928 trans_hdr = (struct af_iucv_trans_hdr *)skb->data;
1930 /* no sock - connection refused */
1931 afiucv_swap_src_dest(skb);
1932 trans_hdr->flags = AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN;
1933 err = dev_queue_xmit(skb);
1937 nsk = iucv_sock_alloc(NULL, sk->sk_type, GFP_ATOMIC);
1939 if ((sk->sk_state != IUCV_LISTEN) ||
1940 sk_acceptq_is_full(sk) ||
1942 /* error on server socket - connection refused */
1945 afiucv_swap_src_dest(skb);
1946 trans_hdr->flags = AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN;
1947 err = dev_queue_xmit(skb);
1952 niucv = iucv_sk(nsk);
1953 iucv_sock_init(nsk, sk);
1954 niucv->transport = AF_IUCV_TRANS_HIPER;
1955 niucv->msglimit = iucv->msglimit;
1956 if (!trans_hdr->window)
1957 niucv->msglimit_peer = IUCV_HIPER_MSGLIM_DEFAULT;
1959 niucv->msglimit_peer = trans_hdr->window;
1960 memcpy(niucv->dst_name, trans_hdr->srcAppName, 8);
1961 memcpy(niucv->dst_user_id, trans_hdr->srcUserID, 8);
1962 memcpy(niucv->src_name, iucv->src_name, 8);
1963 memcpy(niucv->src_user_id, iucv->src_user_id, 8);
1964 nsk->sk_bound_dev_if = sk->sk_bound_dev_if;
1965 niucv->hs_dev = iucv->hs_dev;
1966 dev_hold(niucv->hs_dev);
1967 afiucv_swap_src_dest(skb);
1968 trans_hdr->flags = AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_ACK;
1969 trans_hdr->window = niucv->msglimit;
1970 /* if receiver acks the xmit connection is established */
1971 err = dev_queue_xmit(skb);
1973 iucv_accept_enqueue(sk, nsk);
1974 nsk->sk_state = IUCV_CONNECTED;
1975 sk->sk_data_ready(sk, 1);
1977 iucv_sock_kill(nsk);
1981 return NET_RX_SUCCESS;
1985 * afiucv_hs_callback_synack() - react on received SYN-ACK
1987 static int afiucv_hs_callback_synack(struct sock *sk, struct sk_buff *skb)
1989 struct iucv_sock *iucv = iucv_sk(sk);
1990 struct af_iucv_trans_hdr *trans_hdr =
1991 (struct af_iucv_trans_hdr *)skb->data;
1995 if (sk->sk_state != IUCV_BOUND)
1998 iucv->msglimit_peer = trans_hdr->window;
1999 sk->sk_state = IUCV_CONNECTED;
2000 sk->sk_state_change(sk);
2004 return NET_RX_SUCCESS;
2008 * afiucv_hs_callback_synfin() - react on received SYN_FIN
2010 static int afiucv_hs_callback_synfin(struct sock *sk, struct sk_buff *skb)
2012 struct iucv_sock *iucv = iucv_sk(sk);
2016 if (sk->sk_state != IUCV_BOUND)
2019 sk->sk_state = IUCV_DISCONN;
2020 sk->sk_state_change(sk);
2024 return NET_RX_SUCCESS;
2028 * afiucv_hs_callback_fin() - react on received FIN
2030 static int afiucv_hs_callback_fin(struct sock *sk, struct sk_buff *skb)
2032 struct iucv_sock *iucv = iucv_sk(sk);
2034 /* other end of connection closed */
2038 if (sk->sk_state == IUCV_CONNECTED) {
2039 sk->sk_state = IUCV_DISCONN;
2040 sk->sk_state_change(sk);
2045 return NET_RX_SUCCESS;
2049 * afiucv_hs_callback_win() - react on received WIN
2051 static int afiucv_hs_callback_win(struct sock *sk, struct sk_buff *skb)
2053 struct iucv_sock *iucv = iucv_sk(sk);
2054 struct af_iucv_trans_hdr *trans_hdr =
2055 (struct af_iucv_trans_hdr *)skb->data;
2058 return NET_RX_SUCCESS;
2060 if (sk->sk_state != IUCV_CONNECTED)
2061 return NET_RX_SUCCESS;
2063 atomic_sub(trans_hdr->window, &iucv->msg_sent);
2064 iucv_sock_wake_msglim(sk);
2065 return NET_RX_SUCCESS;
2069 * afiucv_hs_callback_rx() - react on received data
2071 static int afiucv_hs_callback_rx(struct sock *sk, struct sk_buff *skb)
2073 struct iucv_sock *iucv = iucv_sk(sk);
2077 return NET_RX_SUCCESS;
2080 if (sk->sk_state != IUCV_CONNECTED) {
2082 return NET_RX_SUCCESS;
2085 if (sk->sk_shutdown & RCV_SHUTDOWN) {
2087 return NET_RX_SUCCESS;
2090 /* write stuff from iucv_msg to skb cb */
2091 if (skb->len < sizeof(struct af_iucv_trans_hdr)) {
2093 return NET_RX_SUCCESS;
2095 skb_pull(skb, sizeof(struct af_iucv_trans_hdr));
2096 skb_reset_transport_header(skb);
2097 skb_reset_network_header(skb);
2098 spin_lock(&iucv->message_q.lock);
2099 if (skb_queue_empty(&iucv->backlog_skb_q)) {
2100 if (sock_queue_rcv_skb(sk, skb)) {
2101 /* handle rcv queue full */
2102 skb_queue_tail(&iucv->backlog_skb_q, skb);
2105 skb_queue_tail(&iucv_sk(sk)->backlog_skb_q, skb);
2106 spin_unlock(&iucv->message_q.lock);
2107 return NET_RX_SUCCESS;
2111 * afiucv_hs_rcv() - base function for arriving data through HiperSockets
2113 * called from netif RX softirq
2115 static int afiucv_hs_rcv(struct sk_buff *skb, struct net_device *dev,
2116 struct packet_type *pt, struct net_device *orig_dev)
2118 struct hlist_node *node;
2120 struct iucv_sock *iucv;
2121 struct af_iucv_trans_hdr *trans_hdr;
2125 skb_pull(skb, ETH_HLEN);
2126 trans_hdr = (struct af_iucv_trans_hdr *)skb->data;
2127 EBCASC(trans_hdr->destAppName, sizeof(trans_hdr->destAppName));
2128 EBCASC(trans_hdr->destUserID, sizeof(trans_hdr->destUserID));
2129 EBCASC(trans_hdr->srcAppName, sizeof(trans_hdr->srcAppName));
2130 EBCASC(trans_hdr->srcUserID, sizeof(trans_hdr->srcUserID));
2131 memset(nullstring, 0, sizeof(nullstring));
2134 read_lock(&iucv_sk_list.lock);
2135 sk_for_each(sk, node, &iucv_sk_list.head) {
2136 if (trans_hdr->flags == AF_IUCV_FLAG_SYN) {
2137 if ((!memcmp(&iucv_sk(sk)->src_name,
2138 trans_hdr->destAppName, 8)) &&
2139 (!memcmp(&iucv_sk(sk)->src_user_id,
2140 trans_hdr->destUserID, 8)) &&
2141 (!memcmp(&iucv_sk(sk)->dst_name, nullstring, 8)) &&
2142 (!memcmp(&iucv_sk(sk)->dst_user_id,
2148 if ((!memcmp(&iucv_sk(sk)->src_name,
2149 trans_hdr->destAppName, 8)) &&
2150 (!memcmp(&iucv_sk(sk)->src_user_id,
2151 trans_hdr->destUserID, 8)) &&
2152 (!memcmp(&iucv_sk(sk)->dst_name,
2153 trans_hdr->srcAppName, 8)) &&
2154 (!memcmp(&iucv_sk(sk)->dst_user_id,
2155 trans_hdr->srcUserID, 8))) {
2161 read_unlock(&iucv_sk_list.lock);
2166 how should we send with no sock
2167 1) send without sock no send rc checking?
2168 2) introduce default sock to handle this cases
2170 SYN -> send SYN|ACK in good case, send SYN|FIN in bad case
2172 SYN|ACK, SYN|FIN, FIN -> no action? */
2174 switch (trans_hdr->flags) {
2175 case AF_IUCV_FLAG_SYN:
2176 /* connect request */
2177 err = afiucv_hs_callback_syn(sk, skb);
2179 case (AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_ACK):
2180 /* connect request confirmed */
2181 err = afiucv_hs_callback_synack(sk, skb);
2183 case (AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN):
2184 /* connect request refused */
2185 err = afiucv_hs_callback_synfin(sk, skb);
2187 case (AF_IUCV_FLAG_FIN):
2189 err = afiucv_hs_callback_fin(sk, skb);
2191 case (AF_IUCV_FLAG_WIN):
2192 err = afiucv_hs_callback_win(sk, skb);
2193 if (skb->len == sizeof(struct af_iucv_trans_hdr)) {
2197 /* fall through and receive non-zero length data */
2198 case (AF_IUCV_FLAG_SHT):
2199 /* shutdown request */
2200 /* fall through and receive zero length data */
2202 /* plain data frame */
2203 memcpy(CB_TRGCLS(skb), &trans_hdr->iucv_hdr.class,
2205 err = afiucv_hs_callback_rx(sk, skb);
2215 * afiucv_hs_callback_txnotify() - handle send notifcations from HiperSockets
2218 static void afiucv_hs_callback_txnotify(struct sk_buff *skb,
2219 enum iucv_tx_notify n)
2221 struct sock *isk = skb->sk;
2222 struct sock *sk = NULL;
2223 struct iucv_sock *iucv = NULL;
2224 struct sk_buff_head *list;
2225 struct sk_buff *list_skb;
2226 struct sk_buff *nskb;
2227 unsigned long flags;
2228 struct hlist_node *node;
2230 read_lock_irqsave(&iucv_sk_list.lock, flags);
2231 sk_for_each(sk, node, &iucv_sk_list.head)
2236 read_unlock_irqrestore(&iucv_sk_list.lock, flags);
2238 if (!iucv || sock_flag(sk, SOCK_ZAPPED))
2241 list = &iucv->send_skb_q;
2242 spin_lock_irqsave(&list->lock, flags);
2243 if (skb_queue_empty(list))
2245 list_skb = list->next;
2246 nskb = list_skb->next;
2247 while (list_skb != (struct sk_buff *)list) {
2248 if (skb_shinfo(list_skb) == skb_shinfo(skb)) {
2251 __skb_unlink(list_skb, list);
2252 kfree_skb(list_skb);
2253 iucv_sock_wake_msglim(sk);
2255 case TX_NOTIFY_PENDING:
2256 atomic_inc(&iucv->pendings);
2258 case TX_NOTIFY_DELAYED_OK:
2259 __skb_unlink(list_skb, list);
2260 atomic_dec(&iucv->pendings);
2261 if (atomic_read(&iucv->pendings) <= 0)
2262 iucv_sock_wake_msglim(sk);
2263 kfree_skb(list_skb);
2265 case TX_NOTIFY_UNREACHABLE:
2266 case TX_NOTIFY_DELAYED_UNREACHABLE:
2267 case TX_NOTIFY_TPQFULL: /* not yet used */
2268 case TX_NOTIFY_GENERALERROR:
2269 case TX_NOTIFY_DELAYED_GENERALERROR:
2270 __skb_unlink(list_skb, list);
2271 kfree_skb(list_skb);
2272 if (sk->sk_state == IUCV_CONNECTED) {
2273 sk->sk_state = IUCV_DISCONN;
2274 sk->sk_state_change(sk);
2284 spin_unlock_irqrestore(&list->lock, flags);
2286 if (sk->sk_state == IUCV_CLOSING) {
2287 if (skb_queue_empty(&iucv_sk(sk)->send_skb_q)) {
2288 sk->sk_state = IUCV_CLOSED;
2289 sk->sk_state_change(sk);
2296 * afiucv_netdev_event: handle netdev notifier chain events
2298 static int afiucv_netdev_event(struct notifier_block *this,
2299 unsigned long event, void *ptr)
2301 struct net_device *event_dev = (struct net_device *)ptr;
2302 struct hlist_node *node;
2304 struct iucv_sock *iucv;
2308 case NETDEV_GOING_DOWN:
2309 sk_for_each(sk, node, &iucv_sk_list.head) {
2311 if ((iucv->hs_dev == event_dev) &&
2312 (sk->sk_state == IUCV_CONNECTED)) {
2313 if (event == NETDEV_GOING_DOWN)
2314 iucv_send_ctrl(sk, AF_IUCV_FLAG_FIN);
2315 sk->sk_state = IUCV_DISCONN;
2316 sk->sk_state_change(sk);
2321 case NETDEV_UNREGISTER:
2328 static struct notifier_block afiucv_netdev_notifier = {
2329 .notifier_call = afiucv_netdev_event,
2332 static const struct proto_ops iucv_sock_ops = {
2334 .owner = THIS_MODULE,
2335 .release = iucv_sock_release,
2336 .bind = iucv_sock_bind,
2337 .connect = iucv_sock_connect,
2338 .listen = iucv_sock_listen,
2339 .accept = iucv_sock_accept,
2340 .getname = iucv_sock_getname,
2341 .sendmsg = iucv_sock_sendmsg,
2342 .recvmsg = iucv_sock_recvmsg,
2343 .poll = iucv_sock_poll,
2344 .ioctl = sock_no_ioctl,
2345 .mmap = sock_no_mmap,
2346 .socketpair = sock_no_socketpair,
2347 .shutdown = iucv_sock_shutdown,
2348 .setsockopt = iucv_sock_setsockopt,
2349 .getsockopt = iucv_sock_getsockopt,
2352 static const struct net_proto_family iucv_sock_family_ops = {
2354 .owner = THIS_MODULE,
2355 .create = iucv_sock_create,
2358 static struct packet_type iucv_packet_type = {
2359 .type = cpu_to_be16(ETH_P_AF_IUCV),
2360 .func = afiucv_hs_rcv,
2363 static int afiucv_iucv_init(void)
2367 err = pr_iucv->iucv_register(&af_iucv_handler, 0);
2370 /* establish dummy device */
2371 af_iucv_driver.bus = pr_iucv->bus;
2372 err = driver_register(&af_iucv_driver);
2375 af_iucv_dev = kzalloc(sizeof(struct device), GFP_KERNEL);
2380 dev_set_name(af_iucv_dev, "af_iucv");
2381 af_iucv_dev->bus = pr_iucv->bus;
2382 af_iucv_dev->parent = pr_iucv->root;
2383 af_iucv_dev->release = (void (*)(struct device *))kfree;
2384 af_iucv_dev->driver = &af_iucv_driver;
2385 err = device_register(af_iucv_dev);
2391 driver_unregister(&af_iucv_driver);
2393 pr_iucv->iucv_unregister(&af_iucv_handler, 0);
2398 static int __init afiucv_init(void)
2402 if (MACHINE_IS_VM) {
2403 cpcmd("QUERY USERID", iucv_userid, sizeof(iucv_userid), &err);
2404 if (unlikely(err)) {
2406 err = -EPROTONOSUPPORT;
2410 pr_iucv = try_then_request_module(symbol_get(iucv_if), "iucv");
2412 printk(KERN_WARNING "iucv_if lookup failed\n");
2413 memset(&iucv_userid, 0, sizeof(iucv_userid));
2416 memset(&iucv_userid, 0, sizeof(iucv_userid));
2420 err = proto_register(&iucv_proto, 0);
2423 err = sock_register(&iucv_sock_family_ops);
2428 err = afiucv_iucv_init();
2432 register_netdevice_notifier(&afiucv_netdev_notifier);
2433 dev_add_pack(&iucv_packet_type);
2437 sock_unregister(PF_IUCV);
2439 proto_unregister(&iucv_proto);
2442 symbol_put(iucv_if);
2446 static void __exit afiucv_exit(void)
2449 device_unregister(af_iucv_dev);
2450 driver_unregister(&af_iucv_driver);
2451 pr_iucv->iucv_unregister(&af_iucv_handler, 0);
2452 symbol_put(iucv_if);
2454 unregister_netdevice_notifier(&afiucv_netdev_notifier);
2455 dev_remove_pack(&iucv_packet_type);
2456 sock_unregister(PF_IUCV);
2457 proto_unregister(&iucv_proto);
2460 module_init(afiucv_init);
2461 module_exit(afiucv_exit);
2463 MODULE_AUTHOR("Jennifer Hunt <jenhunt@us.ibm.com>");
2464 MODULE_DESCRIPTION("IUCV Sockets ver " VERSION);
2465 MODULE_VERSION(VERSION);
2466 MODULE_LICENSE("GPL");
2467 MODULE_ALIAS_NETPROTO(PF_IUCV);