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
);
94 static int afiucv_hs_rcv(struct sk_buff
*skb
, struct net_device
*dev
,
95 struct packet_type
*pt
, struct net_device
*orig_dev
);
96 static int afiucv_hs_send(struct iucv_message
*imsg
, struct sock
*sock
,
97 struct sk_buff
*skb
, u8 flags
);
98 static void afiucv_hs_callback_txnotify(struct sk_buff
*, enum iucv_tx_notify
);
100 /* Call Back functions */
101 static void iucv_callback_rx(struct iucv_path
*, struct iucv_message
*);
102 static void iucv_callback_txdone(struct iucv_path
*, struct iucv_message
*);
103 static void iucv_callback_connack(struct iucv_path
*, u8 ipuser
[16]);
104 static int iucv_callback_connreq(struct iucv_path
*, u8 ipvmid
[8],
106 static void iucv_callback_connrej(struct iucv_path
*, u8 ipuser
[16]);
107 static void iucv_callback_shutdown(struct iucv_path
*, u8 ipuser
[16]);
109 static struct iucv_sock_list iucv_sk_list
= {
110 .lock
= __RW_LOCK_UNLOCKED(iucv_sk_list
.lock
),
111 .autobind_name
= ATOMIC_INIT(0)
114 static struct iucv_handler af_iucv_handler
= {
115 .path_pending
= iucv_callback_connreq
,
116 .path_complete
= iucv_callback_connack
,
117 .path_severed
= iucv_callback_connrej
,
118 .message_pending
= iucv_callback_rx
,
119 .message_complete
= iucv_callback_txdone
,
120 .path_quiesced
= iucv_callback_shutdown
,
123 static inline void high_nmcpy(unsigned char *dst
, char *src
)
128 static inline void low_nmcpy(unsigned char *dst
, char *src
)
130 memcpy(&dst
[8], src
, 8);
133 static void iucv_skb_queue_purge(struct sk_buff_head
*list
)
137 while ((skb
= skb_dequeue(list
)) != NULL
) {
144 static int afiucv_pm_prepare(struct device
*dev
)
146 #ifdef CONFIG_PM_DEBUG
147 printk(KERN_WARNING
"afiucv_pm_prepare\n");
152 static void afiucv_pm_complete(struct device
*dev
)
154 #ifdef CONFIG_PM_DEBUG
155 printk(KERN_WARNING
"afiucv_pm_complete\n");
160 * afiucv_pm_freeze() - Freeze PM callback
161 * @dev: AFIUCV dummy device
163 * Sever all established IUCV communication pathes
165 static int afiucv_pm_freeze(struct device
*dev
)
167 struct iucv_sock
*iucv
;
169 struct hlist_node
*node
;
172 #ifdef CONFIG_PM_DEBUG
173 printk(KERN_WARNING
"afiucv_pm_freeze\n");
175 read_lock(&iucv_sk_list
.lock
);
176 sk_for_each(sk
, node
, &iucv_sk_list
.head
) {
178 iucv_skb_queue_purge(&iucv
->send_skb_q
);
179 skb_queue_purge(&iucv
->backlog_skb_q
);
180 switch (sk
->sk_state
) {
185 err
= pr_iucv
->path_sever(iucv
->path
, NULL
);
186 iucv_path_free(iucv
->path
);
198 read_unlock(&iucv_sk_list
.lock
);
203 * afiucv_pm_restore_thaw() - Thaw and restore PM callback
204 * @dev: AFIUCV dummy device
206 * socket clean up after freeze
208 static int afiucv_pm_restore_thaw(struct device
*dev
)
211 struct hlist_node
*node
;
213 #ifdef CONFIG_PM_DEBUG
214 printk(KERN_WARNING
"afiucv_pm_restore_thaw\n");
216 read_lock(&iucv_sk_list
.lock
);
217 sk_for_each(sk
, node
, &iucv_sk_list
.head
) {
218 switch (sk
->sk_state
) {
221 sk
->sk_state
= IUCV_DISCONN
;
222 sk
->sk_state_change(sk
);
233 read_unlock(&iucv_sk_list
.lock
);
237 static const struct dev_pm_ops afiucv_pm_ops
= {
238 .prepare
= afiucv_pm_prepare
,
239 .complete
= afiucv_pm_complete
,
240 .freeze
= afiucv_pm_freeze
,
241 .thaw
= afiucv_pm_restore_thaw
,
242 .restore
= afiucv_pm_restore_thaw
,
245 static struct device_driver af_iucv_driver
= {
246 .owner
= THIS_MODULE
,
249 .pm
= &afiucv_pm_ops
,
252 /* dummy device used as trigger for PM functions */
253 static struct device
*af_iucv_dev
;
256 * iucv_msg_length() - Returns the length of an iucv message.
257 * @msg: Pointer to struct iucv_message, MUST NOT be NULL
259 * The function returns the length of the specified iucv message @msg of data
260 * stored in a buffer and of data stored in the parameter list (PRMDATA).
262 * For IUCV_IPRMDATA, AF_IUCV uses the following convention to transport socket
264 * PRMDATA[0..6] socket data (max 7 bytes);
265 * PRMDATA[7] socket data length value (len is 0xff - PRMDATA[7])
267 * The socket data length is computed by subtracting the socket data length
269 * If the socket data len is greater 7, then PRMDATA can be used for special
270 * notifications (see iucv_sock_shutdown); and further,
271 * if the socket data len is > 7, the function returns 8.
273 * Use this function to allocate socket buffers to store iucv message data.
275 static inline size_t iucv_msg_length(struct iucv_message
*msg
)
279 if (msg
->flags
& IUCV_IPRMDATA
) {
280 datalen
= 0xff - msg
->rmmsg
[7];
281 return (datalen
< 8) ? datalen
: 8;
287 * iucv_sock_in_state() - check for specific states
288 * @sk: sock structure
289 * @state: first iucv sk state
290 * @state: second iucv sk state
292 * Returns true if the socket in either in the first or second state.
294 static int iucv_sock_in_state(struct sock
*sk
, int state
, int state2
)
296 return (sk
->sk_state
== state
|| sk
->sk_state
== state2
);
300 * iucv_below_msglim() - function to check if messages can be sent
301 * @sk: sock structure
303 * Returns true if the send queue length is lower than the message limit.
304 * Always returns true if the socket is not connected (no iucv path for
305 * checking the message limit).
307 static inline int iucv_below_msglim(struct sock
*sk
)
309 struct iucv_sock
*iucv
= iucv_sk(sk
);
311 if (sk
->sk_state
!= IUCV_CONNECTED
)
313 if (iucv
->transport
== AF_IUCV_TRANS_IUCV
)
314 return (skb_queue_len(&iucv
->send_skb_q
) < iucv
->path
->msglim
);
316 return ((atomic_read(&iucv
->msg_sent
) < iucv
->msglimit_peer
) &&
317 (atomic_read(&iucv
->pendings
) <= 0));
321 * iucv_sock_wake_msglim() - Wake up thread waiting on msg limit
323 static void iucv_sock_wake_msglim(struct sock
*sk
)
325 struct socket_wq
*wq
;
328 wq
= rcu_dereference(sk
->sk_wq
);
329 if (wq_has_sleeper(wq
))
330 wake_up_interruptible_all(&wq
->wait
);
331 sk_wake_async(sk
, SOCK_WAKE_SPACE
, POLL_OUT
);
336 * afiucv_hs_send() - send a message through HiperSockets transport
338 static int afiucv_hs_send(struct iucv_message
*imsg
, struct sock
*sock
,
339 struct sk_buff
*skb
, u8 flags
)
341 struct net
*net
= sock_net(sock
);
342 struct iucv_sock
*iucv
= iucv_sk(sock
);
343 struct af_iucv_trans_hdr
*phs_hdr
;
344 struct sk_buff
*nskb
;
345 int err
, confirm_recv
= 0;
347 memset(skb
->head
, 0, ETH_HLEN
);
348 phs_hdr
= (struct af_iucv_trans_hdr
*)skb_push(skb
,
349 sizeof(struct af_iucv_trans_hdr
));
350 skb_reset_mac_header(skb
);
351 skb_reset_network_header(skb
);
352 skb_push(skb
, ETH_HLEN
);
353 skb_reset_mac_header(skb
);
354 memset(phs_hdr
, 0, sizeof(struct af_iucv_trans_hdr
));
356 phs_hdr
->magic
= ETH_P_AF_IUCV
;
357 phs_hdr
->version
= 1;
358 phs_hdr
->flags
= flags
;
359 if (flags
== AF_IUCV_FLAG_SYN
)
360 phs_hdr
->window
= iucv
->msglimit
;
361 else if ((flags
== AF_IUCV_FLAG_WIN
) || !flags
) {
362 confirm_recv
= atomic_read(&iucv
->msg_recv
);
363 phs_hdr
->window
= confirm_recv
;
365 phs_hdr
->flags
= phs_hdr
->flags
| AF_IUCV_FLAG_WIN
;
367 memcpy(phs_hdr
->destUserID
, iucv
->dst_user_id
, 8);
368 memcpy(phs_hdr
->destAppName
, iucv
->dst_name
, 8);
369 memcpy(phs_hdr
->srcUserID
, iucv
->src_user_id
, 8);
370 memcpy(phs_hdr
->srcAppName
, iucv
->src_name
, 8);
371 ASCEBC(phs_hdr
->destUserID
, sizeof(phs_hdr
->destUserID
));
372 ASCEBC(phs_hdr
->destAppName
, sizeof(phs_hdr
->destAppName
));
373 ASCEBC(phs_hdr
->srcUserID
, sizeof(phs_hdr
->srcUserID
));
374 ASCEBC(phs_hdr
->srcAppName
, sizeof(phs_hdr
->srcAppName
));
376 memcpy(&phs_hdr
->iucv_hdr
, imsg
, sizeof(struct iucv_message
));
378 skb
->dev
= dev_get_by_index(net
, sock
->sk_bound_dev_if
);
381 if (!(skb
->dev
->flags
& IFF_UP
))
383 if (skb
->len
> skb
->dev
->mtu
) {
384 if (sock
->sk_type
== SOCK_SEQPACKET
)
387 skb_trim(skb
, skb
->dev
->mtu
);
389 skb
->protocol
= ETH_P_AF_IUCV
;
390 skb_shinfo(skb
)->tx_flags
|= SKBTX_DRV_NEEDS_SK_REF
;
391 nskb
= skb_clone(skb
, GFP_ATOMIC
);
394 skb_queue_tail(&iucv
->send_skb_q
, nskb
);
395 err
= dev_queue_xmit(skb
);
397 skb_unlink(nskb
, &iucv
->send_skb_q
);
401 atomic_sub(confirm_recv
, &iucv
->msg_recv
);
402 WARN_ON(atomic_read(&iucv
->msg_recv
) < 0);
407 static struct sock
*__iucv_get_sock_by_name(char *nm
)
410 struct hlist_node
*node
;
412 sk_for_each(sk
, node
, &iucv_sk_list
.head
)
413 if (!memcmp(&iucv_sk(sk
)->src_name
, nm
, 8))
419 static void iucv_sock_destruct(struct sock
*sk
)
421 skb_queue_purge(&sk
->sk_receive_queue
);
422 skb_queue_purge(&sk
->sk_write_queue
);
426 static void iucv_sock_cleanup_listen(struct sock
*parent
)
430 /* Close non-accepted connections */
431 while ((sk
= iucv_accept_dequeue(parent
, NULL
))) {
436 parent
->sk_state
= IUCV_CLOSED
;
439 /* Kill socket (only if zapped and orphaned) */
440 static void iucv_sock_kill(struct sock
*sk
)
442 if (!sock_flag(sk
, SOCK_ZAPPED
) || sk
->sk_socket
)
445 iucv_sock_unlink(&iucv_sk_list
, sk
);
446 sock_set_flag(sk
, SOCK_DEAD
);
450 /* Close an IUCV socket */
451 static void iucv_sock_close(struct sock
*sk
)
453 unsigned char user_data
[16];
454 struct iucv_sock
*iucv
= iucv_sk(sk
);
461 switch (sk
->sk_state
) {
463 iucv_sock_cleanup_listen(sk
);
467 if (iucv
->transport
== AF_IUCV_TRANS_HIPER
) {
469 blen
= sizeof(struct af_iucv_trans_hdr
) + ETH_HLEN
;
470 skb
= sock_alloc_send_skb(sk
, blen
, 1, &err
);
472 skb_reserve(skb
, blen
);
473 err
= afiucv_hs_send(NULL
, sk
, skb
,
476 sk
->sk_state
= IUCV_DISCONN
;
477 sk
->sk_state_change(sk
);
479 case IUCV_DISCONN
: /* fall through */
480 sk
->sk_state
= IUCV_CLOSING
;
481 sk
->sk_state_change(sk
);
483 if (!skb_queue_empty(&iucv
->send_skb_q
)) {
484 if (sock_flag(sk
, SOCK_LINGER
) && sk
->sk_lingertime
)
485 timeo
= sk
->sk_lingertime
;
487 timeo
= IUCV_DISCONN_TIMEOUT
;
489 iucv_sock_in_state(sk
, IUCV_CLOSED
, 0),
493 case IUCV_CLOSING
: /* fall through */
494 sk
->sk_state
= IUCV_CLOSED
;
495 sk
->sk_state_change(sk
);
498 low_nmcpy(user_data
, iucv
->src_name
);
499 high_nmcpy(user_data
, iucv
->dst_name
);
500 ASCEBC(user_data
, sizeof(user_data
));
501 pr_iucv
->path_sever(iucv
->path
, user_data
);
502 iucv_path_free(iucv
->path
);
506 sk
->sk_err
= ECONNRESET
;
507 sk
->sk_state_change(sk
);
509 iucv_skb_queue_purge(&iucv
->send_skb_q
);
510 skb_queue_purge(&iucv
->backlog_skb_q
);
514 /* nothing to do here */
518 /* mark socket for deletion by iucv_sock_kill() */
519 sock_set_flag(sk
, SOCK_ZAPPED
);
524 static void iucv_sock_init(struct sock
*sk
, struct sock
*parent
)
527 sk
->sk_type
= parent
->sk_type
;
530 static struct sock
*iucv_sock_alloc(struct socket
*sock
, int proto
, gfp_t prio
)
533 struct iucv_sock
*iucv
;
535 sk
= sk_alloc(&init_net
, PF_IUCV
, prio
, &iucv_proto
);
540 sock_init_data(sock
, sk
);
541 INIT_LIST_HEAD(&iucv
->accept_q
);
542 spin_lock_init(&iucv
->accept_q_lock
);
543 skb_queue_head_init(&iucv
->send_skb_q
);
544 INIT_LIST_HEAD(&iucv
->message_q
.list
);
545 spin_lock_init(&iucv
->message_q
.lock
);
546 skb_queue_head_init(&iucv
->backlog_skb_q
);
548 atomic_set(&iucv
->pendings
, 0);
551 atomic_set(&iucv
->msg_sent
, 0);
552 atomic_set(&iucv
->msg_recv
, 0);
554 iucv
->sk_txnotify
= afiucv_hs_callback_txnotify
;
555 memset(&iucv
->src_user_id
, 0, 32);
557 iucv
->transport
= AF_IUCV_TRANS_IUCV
;
559 iucv
->transport
= AF_IUCV_TRANS_HIPER
;
561 sk
->sk_destruct
= iucv_sock_destruct
;
562 sk
->sk_sndtimeo
= IUCV_CONN_TIMEOUT
;
563 sk
->sk_allocation
= GFP_DMA
;
565 sock_reset_flag(sk
, SOCK_ZAPPED
);
567 sk
->sk_protocol
= proto
;
568 sk
->sk_state
= IUCV_OPEN
;
570 iucv_sock_link(&iucv_sk_list
, sk
);
574 /* Create an IUCV socket */
575 static int iucv_sock_create(struct net
*net
, struct socket
*sock
, int protocol
,
580 if (protocol
&& protocol
!= PF_IUCV
)
581 return -EPROTONOSUPPORT
;
583 sock
->state
= SS_UNCONNECTED
;
585 switch (sock
->type
) {
587 sock
->ops
= &iucv_sock_ops
;
590 /* currently, proto ops can handle both sk types */
591 sock
->ops
= &iucv_sock_ops
;
594 return -ESOCKTNOSUPPORT
;
597 sk
= iucv_sock_alloc(sock
, protocol
, GFP_KERNEL
);
601 iucv_sock_init(sk
, NULL
);
606 void iucv_sock_link(struct iucv_sock_list
*l
, struct sock
*sk
)
608 write_lock_bh(&l
->lock
);
609 sk_add_node(sk
, &l
->head
);
610 write_unlock_bh(&l
->lock
);
613 void iucv_sock_unlink(struct iucv_sock_list
*l
, struct sock
*sk
)
615 write_lock_bh(&l
->lock
);
616 sk_del_node_init(sk
);
617 write_unlock_bh(&l
->lock
);
620 void iucv_accept_enqueue(struct sock
*parent
, struct sock
*sk
)
623 struct iucv_sock
*par
= iucv_sk(parent
);
626 spin_lock_irqsave(&par
->accept_q_lock
, flags
);
627 list_add_tail(&iucv_sk(sk
)->accept_q
, &par
->accept_q
);
628 spin_unlock_irqrestore(&par
->accept_q_lock
, flags
);
629 iucv_sk(sk
)->parent
= parent
;
630 sk_acceptq_added(parent
);
633 void iucv_accept_unlink(struct sock
*sk
)
636 struct iucv_sock
*par
= iucv_sk(iucv_sk(sk
)->parent
);
638 spin_lock_irqsave(&par
->accept_q_lock
, flags
);
639 list_del_init(&iucv_sk(sk
)->accept_q
);
640 spin_unlock_irqrestore(&par
->accept_q_lock
, flags
);
641 sk_acceptq_removed(iucv_sk(sk
)->parent
);
642 iucv_sk(sk
)->parent
= NULL
;
646 struct sock
*iucv_accept_dequeue(struct sock
*parent
, struct socket
*newsock
)
648 struct iucv_sock
*isk
, *n
;
651 list_for_each_entry_safe(isk
, n
, &iucv_sk(parent
)->accept_q
, accept_q
) {
652 sk
= (struct sock
*) isk
;
655 if (sk
->sk_state
== IUCV_CLOSED
) {
656 iucv_accept_unlink(sk
);
661 if (sk
->sk_state
== IUCV_CONNECTED
||
662 sk
->sk_state
== IUCV_DISCONN
||
664 iucv_accept_unlink(sk
);
666 sock_graft(sk
, newsock
);
677 /* Bind an unbound socket */
678 static int iucv_sock_bind(struct socket
*sock
, struct sockaddr
*addr
,
681 struct sockaddr_iucv
*sa
= (struct sockaddr_iucv
*) addr
;
682 struct sock
*sk
= sock
->sk
;
683 struct iucv_sock
*iucv
;
685 struct net_device
*dev
;
688 /* Verify the input sockaddr */
689 if (!addr
|| addr
->sa_family
!= AF_IUCV
)
693 if (sk
->sk_state
!= IUCV_OPEN
) {
698 write_lock_bh(&iucv_sk_list
.lock
);
701 if (__iucv_get_sock_by_name(sa
->siucv_name
)) {
708 /* Bind the socket */
711 if (!memcmp(sa
->siucv_user_id
, iucv_userid
, 8))
712 goto vm_bind
; /* VM IUCV transport */
714 /* try hiper transport */
715 memcpy(uid
, sa
->siucv_user_id
, sizeof(uid
));
718 for_each_netdev_rcu(&init_net
, dev
) {
719 if (!memcmp(dev
->perm_addr
, uid
, 8)) {
720 memcpy(iucv
->src_name
, sa
->siucv_name
, 8);
721 memcpy(iucv
->src_user_id
, sa
->siucv_user_id
, 8);
722 sk
->sk_bound_dev_if
= dev
->ifindex
;
723 sk
->sk_state
= IUCV_BOUND
;
724 iucv
->transport
= AF_IUCV_TRANS_HIPER
;
726 iucv
->msglimit
= IUCV_HIPER_MSGLIM_DEFAULT
;
734 /* use local userid for backward compat */
735 memcpy(iucv
->src_name
, sa
->siucv_name
, 8);
736 memcpy(iucv
->src_user_id
, iucv_userid
, 8);
737 sk
->sk_state
= IUCV_BOUND
;
738 iucv
->transport
= AF_IUCV_TRANS_IUCV
;
740 iucv
->msglimit
= IUCV_QUEUELEN_DEFAULT
;
743 /* found no dev to bind */
746 /* Release the socket list lock */
747 write_unlock_bh(&iucv_sk_list
.lock
);
753 /* Automatically bind an unbound socket */
754 static int iucv_sock_autobind(struct sock
*sk
)
756 struct iucv_sock
*iucv
= iucv_sk(sk
);
760 if (unlikely(!pr_iucv
))
763 memcpy(iucv
->src_user_id
, iucv_userid
, 8);
765 write_lock_bh(&iucv_sk_list
.lock
);
767 sprintf(name
, "%08x", atomic_inc_return(&iucv_sk_list
.autobind_name
));
768 while (__iucv_get_sock_by_name(name
)) {
769 sprintf(name
, "%08x",
770 atomic_inc_return(&iucv_sk_list
.autobind_name
));
773 write_unlock_bh(&iucv_sk_list
.lock
);
775 memcpy(&iucv
->src_name
, name
, 8);
778 iucv
->msglimit
= IUCV_QUEUELEN_DEFAULT
;
783 static int afiucv_hs_connect(struct socket
*sock
)
785 struct sock
*sk
= sock
->sk
;
787 int blen
= sizeof(struct af_iucv_trans_hdr
) + ETH_HLEN
;
791 skb
= sock_alloc_send_skb(sk
, blen
, 1, &err
);
797 skb_reserve(skb
, blen
);
798 err
= afiucv_hs_send(NULL
, sk
, skb
, AF_IUCV_FLAG_SYN
);
803 static int afiucv_path_connect(struct socket
*sock
, struct sockaddr
*addr
)
805 struct sockaddr_iucv
*sa
= (struct sockaddr_iucv
*) addr
;
806 struct sock
*sk
= sock
->sk
;
807 struct iucv_sock
*iucv
= iucv_sk(sk
);
808 unsigned char user_data
[16];
811 high_nmcpy(user_data
, sa
->siucv_name
);
812 low_nmcpy(user_data
, iucv
->src_name
);
813 ASCEBC(user_data
, sizeof(user_data
));
816 iucv
->path
= iucv_path_alloc(iucv
->msglimit
,
817 IUCV_IPRMDATA
, GFP_KERNEL
);
822 err
= pr_iucv
->path_connect(iucv
->path
, &af_iucv_handler
,
823 sa
->siucv_user_id
, NULL
, user_data
,
826 iucv_path_free(iucv
->path
);
829 case 0x0b: /* Target communicator is not logged on */
832 case 0x0d: /* Max connections for this guest exceeded */
833 case 0x0e: /* Max connections for target guest exceeded */
836 case 0x0f: /* Missing IUCV authorization */
848 /* Connect an unconnected socket */
849 static int iucv_sock_connect(struct socket
*sock
, struct sockaddr
*addr
,
852 struct sockaddr_iucv
*sa
= (struct sockaddr_iucv
*) addr
;
853 struct sock
*sk
= sock
->sk
;
854 struct iucv_sock
*iucv
= iucv_sk(sk
);
857 if (addr
->sa_family
!= AF_IUCV
|| alen
< sizeof(struct sockaddr_iucv
))
860 if (sk
->sk_state
!= IUCV_OPEN
&& sk
->sk_state
!= IUCV_BOUND
)
863 if (sk
->sk_state
== IUCV_OPEN
&&
864 iucv
->transport
== AF_IUCV_TRANS_HIPER
)
865 return -EBADFD
; /* explicit bind required */
867 if (sk
->sk_type
!= SOCK_STREAM
&& sk
->sk_type
!= SOCK_SEQPACKET
)
870 if (sk
->sk_state
== IUCV_OPEN
) {
871 err
= iucv_sock_autobind(sk
);
878 /* Set the destination information */
879 memcpy(iucv
->dst_user_id
, sa
->siucv_user_id
, 8);
880 memcpy(iucv
->dst_name
, sa
->siucv_name
, 8);
882 if (iucv
->transport
== AF_IUCV_TRANS_HIPER
)
883 err
= afiucv_hs_connect(sock
);
885 err
= afiucv_path_connect(sock
, addr
);
889 if (sk
->sk_state
!= IUCV_CONNECTED
)
890 err
= iucv_sock_wait(sk
, iucv_sock_in_state(sk
, IUCV_CONNECTED
,
892 sock_sndtimeo(sk
, flags
& O_NONBLOCK
));
894 if (sk
->sk_state
== IUCV_DISCONN
|| sk
->sk_state
== IUCV_CLOSED
)
897 if (err
&& iucv
->transport
== AF_IUCV_TRANS_IUCV
) {
898 pr_iucv
->path_sever(iucv
->path
, NULL
);
899 iucv_path_free(iucv
->path
);
908 /* Move a socket into listening state. */
909 static int iucv_sock_listen(struct socket
*sock
, int backlog
)
911 struct sock
*sk
= sock
->sk
;
917 if (sk
->sk_state
!= IUCV_BOUND
)
920 if (sock
->type
!= SOCK_STREAM
&& sock
->type
!= SOCK_SEQPACKET
)
923 sk
->sk_max_ack_backlog
= backlog
;
924 sk
->sk_ack_backlog
= 0;
925 sk
->sk_state
= IUCV_LISTEN
;
933 /* Accept a pending connection */
934 static int iucv_sock_accept(struct socket
*sock
, struct socket
*newsock
,
937 DECLARE_WAITQUEUE(wait
, current
);
938 struct sock
*sk
= sock
->sk
, *nsk
;
942 lock_sock_nested(sk
, SINGLE_DEPTH_NESTING
);
944 if (sk
->sk_state
!= IUCV_LISTEN
) {
949 timeo
= sock_rcvtimeo(sk
, flags
& O_NONBLOCK
);
951 /* Wait for an incoming connection */
952 add_wait_queue_exclusive(sk_sleep(sk
), &wait
);
953 while (!(nsk
= iucv_accept_dequeue(sk
, newsock
))) {
954 set_current_state(TASK_INTERRUPTIBLE
);
961 timeo
= schedule_timeout(timeo
);
962 lock_sock_nested(sk
, SINGLE_DEPTH_NESTING
);
964 if (sk
->sk_state
!= IUCV_LISTEN
) {
969 if (signal_pending(current
)) {
970 err
= sock_intr_errno(timeo
);
975 set_current_state(TASK_RUNNING
);
976 remove_wait_queue(sk_sleep(sk
), &wait
);
981 newsock
->state
= SS_CONNECTED
;
988 static int iucv_sock_getname(struct socket
*sock
, struct sockaddr
*addr
,
991 struct sockaddr_iucv
*siucv
= (struct sockaddr_iucv
*) addr
;
992 struct sock
*sk
= sock
->sk
;
993 struct iucv_sock
*iucv
= iucv_sk(sk
);
995 addr
->sa_family
= AF_IUCV
;
996 *len
= sizeof(struct sockaddr_iucv
);
999 memcpy(siucv
->siucv_user_id
, iucv
->dst_user_id
, 8);
1000 memcpy(siucv
->siucv_name
, iucv
->dst_name
, 8);
1002 memcpy(siucv
->siucv_user_id
, iucv
->src_user_id
, 8);
1003 memcpy(siucv
->siucv_name
, iucv
->src_name
, 8);
1005 memset(&siucv
->siucv_port
, 0, sizeof(siucv
->siucv_port
));
1006 memset(&siucv
->siucv_addr
, 0, sizeof(siucv
->siucv_addr
));
1007 memset(&siucv
->siucv_nodeid
, 0, sizeof(siucv
->siucv_nodeid
));
1013 * iucv_send_iprm() - Send socket data in parameter list of an iucv message.
1015 * @msg: Pointer to a struct iucv_message
1016 * @skb: The socket data to send, skb->len MUST BE <= 7
1018 * Send the socket data in the parameter list in the iucv message
1019 * (IUCV_IPRMDATA). The socket data is stored at index 0 to 6 in the parameter
1020 * list and the socket data len at index 7 (last byte).
1021 * See also iucv_msg_length().
1023 * Returns the error code from the iucv_message_send() call.
1025 static int iucv_send_iprm(struct iucv_path
*path
, struct iucv_message
*msg
,
1026 struct sk_buff
*skb
)
1030 memcpy(prmdata
, (void *) skb
->data
, skb
->len
);
1031 prmdata
[7] = 0xff - (u8
) skb
->len
;
1032 return pr_iucv
->message_send(path
, msg
, IUCV_IPRMDATA
, 0,
1033 (void *) prmdata
, 8);
1036 static int iucv_sock_sendmsg(struct kiocb
*iocb
, struct socket
*sock
,
1037 struct msghdr
*msg
, size_t len
)
1039 struct sock
*sk
= sock
->sk
;
1040 struct iucv_sock
*iucv
= iucv_sk(sk
);
1041 struct sk_buff
*skb
;
1042 struct iucv_message txmsg
;
1043 struct cmsghdr
*cmsg
;
1049 int noblock
= msg
->msg_flags
& MSG_DONTWAIT
;
1051 err
= sock_error(sk
);
1055 if (msg
->msg_flags
& MSG_OOB
)
1058 /* SOCK_SEQPACKET: we do not support segmented records */
1059 if (sk
->sk_type
== SOCK_SEQPACKET
&& !(msg
->msg_flags
& MSG_EOR
))
1064 if (sk
->sk_shutdown
& SEND_SHUTDOWN
) {
1069 /* Return if the socket is not in connected state */
1070 if (sk
->sk_state
!= IUCV_CONNECTED
) {
1075 /* initialize defaults */
1076 cmsg_done
= 0; /* check for duplicate headers */
1079 /* iterate over control messages */
1080 for (cmsg
= CMSG_FIRSTHDR(msg
); cmsg
;
1081 cmsg
= CMSG_NXTHDR(msg
, cmsg
)) {
1083 if (!CMSG_OK(msg
, cmsg
)) {
1088 if (cmsg
->cmsg_level
!= SOL_IUCV
)
1091 if (cmsg
->cmsg_type
& cmsg_done
) {
1095 cmsg_done
|= cmsg
->cmsg_type
;
1097 switch (cmsg
->cmsg_type
) {
1098 case SCM_IUCV_TRGCLS
:
1099 if (cmsg
->cmsg_len
!= CMSG_LEN(TRGCLS_SIZE
)) {
1104 /* set iucv message target class */
1105 memcpy(&txmsg
.class,
1106 (void *) CMSG_DATA(cmsg
), TRGCLS_SIZE
);
1117 /* allocate one skb for each iucv message:
1118 * this is fine for SOCK_SEQPACKET (unless we want to support
1119 * segmented records using the MSG_EOR flag), but
1120 * for SOCK_STREAM we might want to improve it in future */
1121 if (iucv
->transport
== AF_IUCV_TRANS_HIPER
)
1122 skb
= sock_alloc_send_skb(sk
,
1123 len
+ sizeof(struct af_iucv_trans_hdr
) + ETH_HLEN
,
1126 skb
= sock_alloc_send_skb(sk
, len
, noblock
, &err
);
1129 if (iucv
->transport
== AF_IUCV_TRANS_HIPER
)
1130 skb_reserve(skb
, sizeof(struct af_iucv_trans_hdr
) + ETH_HLEN
);
1131 if (memcpy_fromiovec(skb_put(skb
, len
), msg
->msg_iov
, len
)) {
1136 /* wait if outstanding messages for iucv path has reached */
1137 timeo
= sock_sndtimeo(sk
, noblock
);
1138 err
= iucv_sock_wait(sk
, iucv_below_msglim(sk
), timeo
);
1142 /* return -ECONNRESET if the socket is no longer connected */
1143 if (sk
->sk_state
!= IUCV_CONNECTED
) {
1148 /* increment and save iucv message tag for msg_completion cbk */
1149 txmsg
.tag
= iucv
->send_tag
++;
1150 memcpy(CB_TAG(skb
), &txmsg
.tag
, CB_TAG_LEN
);
1151 if (iucv
->transport
== AF_IUCV_TRANS_HIPER
) {
1152 atomic_inc(&iucv
->msg_sent
);
1153 err
= afiucv_hs_send(&txmsg
, sk
, skb
, 0);
1155 atomic_dec(&iucv
->msg_sent
);
1160 skb_queue_tail(&iucv
->send_skb_q
, skb
);
1162 if (((iucv
->path
->flags
& IUCV_IPRMDATA
) & iucv
->flags
)
1164 err
= iucv_send_iprm(iucv
->path
, &txmsg
, skb
);
1166 /* on success: there is no message_complete callback
1167 * for an IPRMDATA msg; remove skb from send queue */
1169 skb_unlink(skb
, &iucv
->send_skb_q
);
1173 /* this error should never happen since the
1174 * IUCV_IPRMDATA path flag is set... sever path */
1176 pr_iucv
->path_sever(iucv
->path
, NULL
);
1177 skb_unlink(skb
, &iucv
->send_skb_q
);
1182 err
= pr_iucv
->message_send(iucv
->path
, &txmsg
, 0, 0,
1183 (void *) skb
->data
, skb
->len
);
1187 memcpy(user_id
, iucv
->dst_user_id
, 8);
1189 memcpy(appl_id
, iucv
->dst_name
, 8);
1190 pr_err("Application %s on z/VM guest %s"
1191 " exceeds message limit\n",
1196 skb_unlink(skb
, &iucv
->send_skb_q
);
1213 /* iucv_fragment_skb() - Fragment a single IUCV message into multiple skb's
1215 * Locking: must be called with message_q.lock held
1217 static int iucv_fragment_skb(struct sock
*sk
, struct sk_buff
*skb
, int len
)
1219 int dataleft
, size
, copied
= 0;
1220 struct sk_buff
*nskb
;
1224 if (dataleft
>= sk
->sk_rcvbuf
/ 4)
1225 size
= sk
->sk_rcvbuf
/ 4;
1229 nskb
= alloc_skb(size
, GFP_ATOMIC
| GFP_DMA
);
1233 /* copy target class to control buffer of new skb */
1234 memcpy(CB_TRGCLS(nskb
), CB_TRGCLS(skb
), CB_TRGCLS_LEN
);
1236 /* copy data fragment */
1237 memcpy(nskb
->data
, skb
->data
+ copied
, size
);
1241 skb_reset_transport_header(nskb
);
1242 skb_reset_network_header(nskb
);
1245 skb_queue_tail(&iucv_sk(sk
)->backlog_skb_q
, nskb
);
1251 /* iucv_process_message() - Receive a single outstanding IUCV message
1253 * Locking: must be called with message_q.lock held
1255 static void iucv_process_message(struct sock
*sk
, struct sk_buff
*skb
,
1256 struct iucv_path
*path
,
1257 struct iucv_message
*msg
)
1262 len
= iucv_msg_length(msg
);
1264 /* store msg target class in the second 4 bytes of skb ctrl buffer */
1265 /* Note: the first 4 bytes are reserved for msg tag */
1266 memcpy(CB_TRGCLS(skb
), &msg
->class, CB_TRGCLS_LEN
);
1268 /* check for special IPRM messages (e.g. iucv_sock_shutdown) */
1269 if ((msg
->flags
& IUCV_IPRMDATA
) && len
> 7) {
1270 if (memcmp(msg
->rmmsg
, iprm_shutdown
, 8) == 0) {
1275 rc
= pr_iucv
->message_receive(path
, msg
,
1276 msg
->flags
& IUCV_IPRMDATA
,
1277 skb
->data
, len
, NULL
);
1282 /* we need to fragment iucv messages for SOCK_STREAM only;
1283 * for SOCK_SEQPACKET, it is only relevant if we support
1284 * record segmentation using MSG_EOR (see also recvmsg()) */
1285 if (sk
->sk_type
== SOCK_STREAM
&&
1286 skb
->truesize
>= sk
->sk_rcvbuf
/ 4) {
1287 rc
= iucv_fragment_skb(sk
, skb
, len
);
1291 pr_iucv
->path_sever(path
, NULL
);
1294 skb
= skb_dequeue(&iucv_sk(sk
)->backlog_skb_q
);
1296 skb_reset_transport_header(skb
);
1297 skb_reset_network_header(skb
);
1302 if (sock_queue_rcv_skb(sk
, skb
))
1303 skb_queue_head(&iucv_sk(sk
)->backlog_skb_q
, skb
);
1306 /* iucv_process_message_q() - Process outstanding IUCV messages
1308 * Locking: must be called with message_q.lock held
1310 static void iucv_process_message_q(struct sock
*sk
)
1312 struct iucv_sock
*iucv
= iucv_sk(sk
);
1313 struct sk_buff
*skb
;
1314 struct sock_msg_q
*p
, *n
;
1316 list_for_each_entry_safe(p
, n
, &iucv
->message_q
.list
, list
) {
1317 skb
= alloc_skb(iucv_msg_length(&p
->msg
), GFP_ATOMIC
| GFP_DMA
);
1320 iucv_process_message(sk
, skb
, p
->path
, &p
->msg
);
1323 if (!skb_queue_empty(&iucv
->backlog_skb_q
))
1328 static int iucv_sock_recvmsg(struct kiocb
*iocb
, struct socket
*sock
,
1329 struct msghdr
*msg
, size_t len
, int flags
)
1331 int noblock
= flags
& MSG_DONTWAIT
;
1332 struct sock
*sk
= sock
->sk
;
1333 struct iucv_sock
*iucv
= iucv_sk(sk
);
1334 unsigned int copied
, rlen
;
1335 struct sk_buff
*skb
, *rskb
, *cskb
, *sskb
;
1339 if ((sk
->sk_state
== IUCV_DISCONN
) &&
1340 skb_queue_empty(&iucv
->backlog_skb_q
) &&
1341 skb_queue_empty(&sk
->sk_receive_queue
) &&
1342 list_empty(&iucv
->message_q
.list
))
1345 if (flags
& (MSG_OOB
))
1348 /* receive/dequeue next skb:
1349 * the function understands MSG_PEEK and, thus, does not dequeue skb */
1350 skb
= skb_recv_datagram(sk
, flags
, noblock
, &err
);
1352 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
1357 rlen
= skb
->len
; /* real length of skb */
1358 copied
= min_t(unsigned int, rlen
, len
);
1361 if (skb_copy_datagram_iovec(cskb
, 0, msg
->msg_iov
, copied
)) {
1362 if (!(flags
& MSG_PEEK
))
1363 skb_queue_head(&sk
->sk_receive_queue
, skb
);
1367 /* SOCK_SEQPACKET: set MSG_TRUNC if recv buf size is too small */
1368 if (sk
->sk_type
== SOCK_SEQPACKET
) {
1370 msg
->msg_flags
|= MSG_TRUNC
;
1371 /* each iucv message contains a complete record */
1372 msg
->msg_flags
|= MSG_EOR
;
1375 /* create control message to store iucv msg target class:
1376 * get the trgcls from the control buffer of the skb due to
1377 * fragmentation of original iucv message. */
1378 err
= put_cmsg(msg
, SOL_IUCV
, SCM_IUCV_TRGCLS
,
1379 CB_TRGCLS_LEN
, CB_TRGCLS(skb
));
1381 if (!(flags
& MSG_PEEK
))
1382 skb_queue_head(&sk
->sk_receive_queue
, skb
);
1386 /* Mark read part of skb as used */
1387 if (!(flags
& MSG_PEEK
)) {
1389 /* SOCK_STREAM: re-queue skb if it contains unreceived data */
1390 if (sk
->sk_type
== SOCK_STREAM
) {
1391 skb_pull(skb
, copied
);
1393 skb_queue_head(&sk
->sk_receive_queue
, skb
);
1399 atomic_inc(&iucv
->msg_recv
);
1401 /* Queue backlog skbs */
1402 spin_lock_bh(&iucv
->message_q
.lock
);
1403 rskb
= skb_dequeue(&iucv
->backlog_skb_q
);
1405 if (sock_queue_rcv_skb(sk
, rskb
)) {
1406 skb_queue_head(&iucv
->backlog_skb_q
,
1410 rskb
= skb_dequeue(&iucv
->backlog_skb_q
);
1413 if (skb_queue_empty(&iucv
->backlog_skb_q
)) {
1414 if (!list_empty(&iucv
->message_q
.list
))
1415 iucv_process_message_q(sk
);
1416 if (atomic_read(&iucv
->msg_recv
) >=
1417 iucv
->msglimit
/ 2) {
1418 /* send WIN to peer */
1419 blen
= sizeof(struct af_iucv_trans_hdr
) +
1421 sskb
= sock_alloc_send_skb(sk
, blen
, 1, &err
);
1423 skb_reserve(sskb
, blen
);
1424 err
= afiucv_hs_send(NULL
, sk
, sskb
,
1428 sk
->sk_state
= IUCV_DISCONN
;
1429 sk
->sk_state_change(sk
);
1433 spin_unlock_bh(&iucv
->message_q
.lock
);
1437 /* SOCK_SEQPACKET: return real length if MSG_TRUNC is set */
1438 if (sk
->sk_type
== SOCK_SEQPACKET
&& (flags
& MSG_TRUNC
))
1444 static inline unsigned int iucv_accept_poll(struct sock
*parent
)
1446 struct iucv_sock
*isk
, *n
;
1449 list_for_each_entry_safe(isk
, n
, &iucv_sk(parent
)->accept_q
, accept_q
) {
1450 sk
= (struct sock
*) isk
;
1452 if (sk
->sk_state
== IUCV_CONNECTED
)
1453 return POLLIN
| POLLRDNORM
;
1459 unsigned int iucv_sock_poll(struct file
*file
, struct socket
*sock
,
1462 struct sock
*sk
= sock
->sk
;
1463 unsigned int mask
= 0;
1465 sock_poll_wait(file
, sk_sleep(sk
), wait
);
1467 if (sk
->sk_state
== IUCV_LISTEN
)
1468 return iucv_accept_poll(sk
);
1470 if (sk
->sk_err
|| !skb_queue_empty(&sk
->sk_error_queue
))
1473 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
1476 if (sk
->sk_shutdown
== SHUTDOWN_MASK
)
1479 if (!skb_queue_empty(&sk
->sk_receive_queue
) ||
1480 (sk
->sk_shutdown
& RCV_SHUTDOWN
))
1481 mask
|= POLLIN
| POLLRDNORM
;
1483 if (sk
->sk_state
== IUCV_CLOSED
)
1486 if (sk
->sk_state
== IUCV_DISCONN
)
1489 if (sock_writeable(sk
))
1490 mask
|= POLLOUT
| POLLWRNORM
| POLLWRBAND
;
1492 set_bit(SOCK_ASYNC_NOSPACE
, &sk
->sk_socket
->flags
);
1497 static int iucv_sock_shutdown(struct socket
*sock
, int how
)
1499 struct sock
*sk
= sock
->sk
;
1500 struct iucv_sock
*iucv
= iucv_sk(sk
);
1501 struct iucv_message txmsg
;
1506 if ((how
& ~SHUTDOWN_MASK
) || !how
)
1510 switch (sk
->sk_state
) {
1518 sk
->sk_shutdown
|= how
;
1522 if (how
== SEND_SHUTDOWN
|| how
== SHUTDOWN_MASK
) {
1525 err
= pr_iucv
->message_send(iucv
->path
, &txmsg
, IUCV_IPRMDATA
,
1526 0, (void *) iprm_shutdown
, 8);
1542 if (how
== RCV_SHUTDOWN
|| how
== SHUTDOWN_MASK
) {
1543 err
= pr_iucv
->path_quiesce(iucv
->path
, NULL
);
1547 skb_queue_purge(&sk
->sk_receive_queue
);
1550 /* Wake up anyone sleeping in poll */
1551 sk
->sk_state_change(sk
);
1558 static int iucv_sock_release(struct socket
*sock
)
1560 struct sock
*sk
= sock
->sk
;
1566 iucv_sock_close(sk
);
1568 /* Unregister with IUCV base support */
1569 if (iucv_sk(sk
)->path
) {
1570 pr_iucv
->path_sever(iucv_sk(sk
)->path
, NULL
);
1571 iucv_path_free(iucv_sk(sk
)->path
);
1572 iucv_sk(sk
)->path
= NULL
;
1580 /* getsockopt and setsockopt */
1581 static int iucv_sock_setsockopt(struct socket
*sock
, int level
, int optname
,
1582 char __user
*optval
, unsigned int optlen
)
1584 struct sock
*sk
= sock
->sk
;
1585 struct iucv_sock
*iucv
= iucv_sk(sk
);
1589 if (level
!= SOL_IUCV
)
1590 return -ENOPROTOOPT
;
1592 if (optlen
< sizeof(int))
1595 if (get_user(val
, (int __user
*) optval
))
1602 case SO_IPRMDATA_MSG
:
1604 iucv
->flags
|= IUCV_IPRMDATA
;
1606 iucv
->flags
&= ~IUCV_IPRMDATA
;
1609 switch (sk
->sk_state
) {
1612 if (val
< 1 || val
> (u16
)(~0))
1615 iucv
->msglimit
= val
;
1631 static int iucv_sock_getsockopt(struct socket
*sock
, int level
, int optname
,
1632 char __user
*optval
, int __user
*optlen
)
1634 struct sock
*sk
= sock
->sk
;
1635 struct iucv_sock
*iucv
= iucv_sk(sk
);
1638 if (level
!= SOL_IUCV
)
1639 return -ENOPROTOOPT
;
1641 if (get_user(len
, optlen
))
1647 len
= min_t(unsigned int, len
, sizeof(int));
1650 case SO_IPRMDATA_MSG
:
1651 val
= (iucv
->flags
& IUCV_IPRMDATA
) ? 1 : 0;
1655 val
= (iucv
->path
!= NULL
) ? iucv
->path
->msglim
/* connected */
1656 : iucv
->msglimit
; /* default */
1660 return -ENOPROTOOPT
;
1663 if (put_user(len
, optlen
))
1665 if (copy_to_user(optval
, &val
, len
))
1672 /* Callback wrappers - called from iucv base support */
1673 static int iucv_callback_connreq(struct iucv_path
*path
,
1674 u8 ipvmid
[8], u8 ipuser
[16])
1676 unsigned char user_data
[16];
1677 unsigned char nuser_data
[16];
1678 unsigned char src_name
[8];
1679 struct hlist_node
*node
;
1680 struct sock
*sk
, *nsk
;
1681 struct iucv_sock
*iucv
, *niucv
;
1684 memcpy(src_name
, ipuser
, 8);
1685 EBCASC(src_name
, 8);
1686 /* Find out if this path belongs to af_iucv. */
1687 read_lock(&iucv_sk_list
.lock
);
1690 sk_for_each(sk
, node
, &iucv_sk_list
.head
)
1691 if (sk
->sk_state
== IUCV_LISTEN
&&
1692 !memcmp(&iucv_sk(sk
)->src_name
, src_name
, 8)) {
1694 * Found a listening socket with
1695 * src_name == ipuser[0-7].
1700 read_unlock(&iucv_sk_list
.lock
);
1702 /* No socket found, not one of our paths. */
1707 /* Check if parent socket is listening */
1708 low_nmcpy(user_data
, iucv
->src_name
);
1709 high_nmcpy(user_data
, iucv
->dst_name
);
1710 ASCEBC(user_data
, sizeof(user_data
));
1711 if (sk
->sk_state
!= IUCV_LISTEN
) {
1712 err
= pr_iucv
->path_sever(path
, user_data
);
1713 iucv_path_free(path
);
1717 /* Check for backlog size */
1718 if (sk_acceptq_is_full(sk
)) {
1719 err
= pr_iucv
->path_sever(path
, user_data
);
1720 iucv_path_free(path
);
1724 /* Create the new socket */
1725 nsk
= iucv_sock_alloc(NULL
, sk
->sk_type
, GFP_ATOMIC
);
1727 err
= pr_iucv
->path_sever(path
, user_data
);
1728 iucv_path_free(path
);
1732 niucv
= iucv_sk(nsk
);
1733 iucv_sock_init(nsk
, sk
);
1735 /* Set the new iucv_sock */
1736 memcpy(niucv
->dst_name
, ipuser
+ 8, 8);
1737 EBCASC(niucv
->dst_name
, 8);
1738 memcpy(niucv
->dst_user_id
, ipvmid
, 8);
1739 memcpy(niucv
->src_name
, iucv
->src_name
, 8);
1740 memcpy(niucv
->src_user_id
, iucv
->src_user_id
, 8);
1743 /* Call iucv_accept */
1744 high_nmcpy(nuser_data
, ipuser
+ 8);
1745 memcpy(nuser_data
+ 8, niucv
->src_name
, 8);
1746 ASCEBC(nuser_data
+ 8, 8);
1748 /* set message limit for path based on msglimit of accepting socket */
1749 niucv
->msglimit
= iucv
->msglimit
;
1750 path
->msglim
= iucv
->msglimit
;
1751 err
= pr_iucv
->path_accept(path
, &af_iucv_handler
, nuser_data
, nsk
);
1753 err
= pr_iucv
->path_sever(path
, user_data
);
1754 iucv_path_free(path
);
1755 iucv_sock_kill(nsk
);
1759 iucv_accept_enqueue(sk
, nsk
);
1761 /* Wake up accept */
1762 nsk
->sk_state
= IUCV_CONNECTED
;
1763 sk
->sk_data_ready(sk
, 1);
1770 static void iucv_callback_connack(struct iucv_path
*path
, u8 ipuser
[16])
1772 struct sock
*sk
= path
->private;
1774 sk
->sk_state
= IUCV_CONNECTED
;
1775 sk
->sk_state_change(sk
);
1778 static void iucv_callback_rx(struct iucv_path
*path
, struct iucv_message
*msg
)
1780 struct sock
*sk
= path
->private;
1781 struct iucv_sock
*iucv
= iucv_sk(sk
);
1782 struct sk_buff
*skb
;
1783 struct sock_msg_q
*save_msg
;
1786 if (sk
->sk_shutdown
& RCV_SHUTDOWN
) {
1787 pr_iucv
->message_reject(path
, msg
);
1791 spin_lock(&iucv
->message_q
.lock
);
1793 if (!list_empty(&iucv
->message_q
.list
) ||
1794 !skb_queue_empty(&iucv
->backlog_skb_q
))
1797 len
= atomic_read(&sk
->sk_rmem_alloc
);
1798 len
+= SKB_TRUESIZE(iucv_msg_length(msg
));
1799 if (len
> sk
->sk_rcvbuf
)
1802 skb
= alloc_skb(iucv_msg_length(msg
), GFP_ATOMIC
| GFP_DMA
);
1806 iucv_process_message(sk
, skb
, path
, msg
);
1810 save_msg
= kzalloc(sizeof(struct sock_msg_q
), GFP_ATOMIC
| GFP_DMA
);
1813 save_msg
->path
= path
;
1814 save_msg
->msg
= *msg
;
1816 list_add_tail(&save_msg
->list
, &iucv
->message_q
.list
);
1819 spin_unlock(&iucv
->message_q
.lock
);
1822 static void iucv_callback_txdone(struct iucv_path
*path
,
1823 struct iucv_message
*msg
)
1825 struct sock
*sk
= path
->private;
1826 struct sk_buff
*this = NULL
;
1827 struct sk_buff_head
*list
= &iucv_sk(sk
)->send_skb_q
;
1828 struct sk_buff
*list_skb
= list
->next
;
1829 unsigned long flags
;
1831 if (!skb_queue_empty(list
)) {
1832 spin_lock_irqsave(&list
->lock
, flags
);
1834 while (list_skb
!= (struct sk_buff
*)list
) {
1835 if (!memcmp(&msg
->tag
, CB_TAG(list_skb
), CB_TAG_LEN
)) {
1839 list_skb
= list_skb
->next
;
1842 __skb_unlink(this, list
);
1844 spin_unlock_irqrestore(&list
->lock
, flags
);
1848 /* wake up any process waiting for sending */
1849 iucv_sock_wake_msglim(sk
);
1854 if (sk
->sk_state
== IUCV_CLOSING
) {
1855 if (skb_queue_empty(&iucv_sk(sk
)->send_skb_q
)) {
1856 sk
->sk_state
= IUCV_CLOSED
;
1857 sk
->sk_state_change(sk
);
1863 static void iucv_callback_connrej(struct iucv_path
*path
, u8 ipuser
[16])
1865 struct sock
*sk
= path
->private;
1867 sk
->sk_state
= IUCV_DISCONN
;
1869 sk
->sk_state_change(sk
);
1872 /* called if the other communication side shuts down its RECV direction;
1873 * in turn, the callback sets SEND_SHUTDOWN to disable sending of data.
1875 static void iucv_callback_shutdown(struct iucv_path
*path
, u8 ipuser
[16])
1877 struct sock
*sk
= path
->private;
1880 if (sk
->sk_state
!= IUCV_CLOSED
) {
1881 sk
->sk_shutdown
|= SEND_SHUTDOWN
;
1882 sk
->sk_state_change(sk
);
1887 /***************** HiperSockets transport callbacks ********************/
1888 static void afiucv_swap_src_dest(struct sk_buff
*skb
)
1890 struct af_iucv_trans_hdr
*trans_hdr
=
1891 (struct af_iucv_trans_hdr
*)skb
->data
;
1895 ASCEBC(trans_hdr
->destUserID
, sizeof(trans_hdr
->destUserID
));
1896 ASCEBC(trans_hdr
->destAppName
, sizeof(trans_hdr
->destAppName
));
1897 ASCEBC(trans_hdr
->srcUserID
, sizeof(trans_hdr
->srcUserID
));
1898 ASCEBC(trans_hdr
->srcAppName
, sizeof(trans_hdr
->srcAppName
));
1899 memcpy(tmpID
, trans_hdr
->srcUserID
, 8);
1900 memcpy(tmpName
, trans_hdr
->srcAppName
, 8);
1901 memcpy(trans_hdr
->srcUserID
, trans_hdr
->destUserID
, 8);
1902 memcpy(trans_hdr
->srcAppName
, trans_hdr
->destAppName
, 8);
1903 memcpy(trans_hdr
->destUserID
, tmpID
, 8);
1904 memcpy(trans_hdr
->destAppName
, tmpName
, 8);
1905 skb_push(skb
, ETH_HLEN
);
1906 memset(skb
->data
, 0, ETH_HLEN
);
1910 * afiucv_hs_callback_syn - react on received SYN
1912 static int afiucv_hs_callback_syn(struct sock
*sk
, struct sk_buff
*skb
)
1915 struct iucv_sock
*iucv
, *niucv
;
1916 struct af_iucv_trans_hdr
*trans_hdr
;
1920 trans_hdr
= (struct af_iucv_trans_hdr
*)skb
->data
;
1922 /* no sock - connection refused */
1923 afiucv_swap_src_dest(skb
);
1924 trans_hdr
->flags
= AF_IUCV_FLAG_SYN
| AF_IUCV_FLAG_FIN
;
1925 err
= dev_queue_xmit(skb
);
1929 nsk
= iucv_sock_alloc(NULL
, sk
->sk_type
, GFP_ATOMIC
);
1931 if ((sk
->sk_state
!= IUCV_LISTEN
) ||
1932 sk_acceptq_is_full(sk
) ||
1934 /* error on server socket - connection refused */
1937 afiucv_swap_src_dest(skb
);
1938 trans_hdr
->flags
= AF_IUCV_FLAG_SYN
| AF_IUCV_FLAG_FIN
;
1939 err
= dev_queue_xmit(skb
);
1944 niucv
= iucv_sk(nsk
);
1945 iucv_sock_init(nsk
, sk
);
1946 niucv
->transport
= AF_IUCV_TRANS_HIPER
;
1947 niucv
->msglimit
= iucv
->msglimit
;
1948 if (!trans_hdr
->window
)
1949 niucv
->msglimit_peer
= IUCV_HIPER_MSGLIM_DEFAULT
;
1951 niucv
->msglimit_peer
= trans_hdr
->window
;
1952 memcpy(niucv
->dst_name
, trans_hdr
->srcAppName
, 8);
1953 memcpy(niucv
->dst_user_id
, trans_hdr
->srcUserID
, 8);
1954 memcpy(niucv
->src_name
, iucv
->src_name
, 8);
1955 memcpy(niucv
->src_user_id
, iucv
->src_user_id
, 8);
1956 nsk
->sk_bound_dev_if
= sk
->sk_bound_dev_if
;
1957 afiucv_swap_src_dest(skb
);
1958 trans_hdr
->flags
= AF_IUCV_FLAG_SYN
| AF_IUCV_FLAG_ACK
;
1959 trans_hdr
->window
= niucv
->msglimit
;
1960 /* if receiver acks the xmit connection is established */
1961 err
= dev_queue_xmit(skb
);
1963 iucv_accept_enqueue(sk
, nsk
);
1964 nsk
->sk_state
= IUCV_CONNECTED
;
1965 sk
->sk_data_ready(sk
, 1);
1967 iucv_sock_kill(nsk
);
1971 return NET_RX_SUCCESS
;
1975 * afiucv_hs_callback_synack() - react on received SYN-ACK
1977 static int afiucv_hs_callback_synack(struct sock
*sk
, struct sk_buff
*skb
)
1979 struct iucv_sock
*iucv
= iucv_sk(sk
);
1980 struct af_iucv_trans_hdr
*trans_hdr
=
1981 (struct af_iucv_trans_hdr
*)skb
->data
;
1985 if (sk
->sk_state
!= IUCV_BOUND
)
1988 iucv
->msglimit_peer
= trans_hdr
->window
;
1989 sk
->sk_state
= IUCV_CONNECTED
;
1990 sk
->sk_state_change(sk
);
1994 return NET_RX_SUCCESS
;
1998 * afiucv_hs_callback_synfin() - react on received SYN_FIN
2000 static int afiucv_hs_callback_synfin(struct sock
*sk
, struct sk_buff
*skb
)
2002 struct iucv_sock
*iucv
= iucv_sk(sk
);
2006 if (sk
->sk_state
!= IUCV_BOUND
)
2009 sk
->sk_state
= IUCV_DISCONN
;
2010 sk
->sk_state_change(sk
);
2014 return NET_RX_SUCCESS
;
2018 * afiucv_hs_callback_fin() - react on received FIN
2020 static int afiucv_hs_callback_fin(struct sock
*sk
, struct sk_buff
*skb
)
2022 struct iucv_sock
*iucv
= iucv_sk(sk
);
2024 /* other end of connection closed */
2027 sk
->sk_state
= IUCV_DISCONN
;
2028 sk
->sk_state_change(sk
);
2032 return NET_RX_SUCCESS
;
2036 * afiucv_hs_callback_win() - react on received WIN
2038 static int afiucv_hs_callback_win(struct sock
*sk
, struct sk_buff
*skb
)
2040 struct iucv_sock
*iucv
= iucv_sk(sk
);
2041 struct af_iucv_trans_hdr
*trans_hdr
=
2042 (struct af_iucv_trans_hdr
*)skb
->data
;
2045 return NET_RX_SUCCESS
;
2047 if (sk
->sk_state
!= IUCV_CONNECTED
)
2048 return NET_RX_SUCCESS
;
2050 atomic_sub(trans_hdr
->window
, &iucv
->msg_sent
);
2051 iucv_sock_wake_msglim(sk
);
2052 return NET_RX_SUCCESS
;
2056 * afiucv_hs_callback_rx() - react on received data
2058 static int afiucv_hs_callback_rx(struct sock
*sk
, struct sk_buff
*skb
)
2060 struct iucv_sock
*iucv
= iucv_sk(sk
);
2064 return NET_RX_SUCCESS
;
2067 if (sk
->sk_state
!= IUCV_CONNECTED
) {
2069 return NET_RX_SUCCESS
;
2072 /* write stuff from iucv_msg to skb cb */
2073 if (skb
->len
<= sizeof(struct af_iucv_trans_hdr
)) {
2075 return NET_RX_SUCCESS
;
2077 skb_pull(skb
, sizeof(struct af_iucv_trans_hdr
));
2078 skb_reset_transport_header(skb
);
2079 skb_reset_network_header(skb
);
2080 spin_lock(&iucv
->message_q
.lock
);
2081 if (skb_queue_empty(&iucv
->backlog_skb_q
)) {
2082 if (sock_queue_rcv_skb(sk
, skb
)) {
2083 /* handle rcv queue full */
2084 skb_queue_tail(&iucv
->backlog_skb_q
, skb
);
2087 skb_queue_tail(&iucv_sk(sk
)->backlog_skb_q
, skb
);
2088 spin_unlock(&iucv
->message_q
.lock
);
2089 return NET_RX_SUCCESS
;
2093 * afiucv_hs_rcv() - base function for arriving data through HiperSockets
2095 * called from netif RX softirq
2097 static int afiucv_hs_rcv(struct sk_buff
*skb
, struct net_device
*dev
,
2098 struct packet_type
*pt
, struct net_device
*orig_dev
)
2100 struct hlist_node
*node
;
2102 struct iucv_sock
*iucv
;
2103 struct af_iucv_trans_hdr
*trans_hdr
;
2107 skb_pull(skb
, ETH_HLEN
);
2108 trans_hdr
= (struct af_iucv_trans_hdr
*)skb
->data
;
2109 EBCASC(trans_hdr
->destAppName
, sizeof(trans_hdr
->destAppName
));
2110 EBCASC(trans_hdr
->destUserID
, sizeof(trans_hdr
->destUserID
));
2111 EBCASC(trans_hdr
->srcAppName
, sizeof(trans_hdr
->srcAppName
));
2112 EBCASC(trans_hdr
->srcUserID
, sizeof(trans_hdr
->srcUserID
));
2113 memset(nullstring
, 0, sizeof(nullstring
));
2116 read_lock(&iucv_sk_list
.lock
);
2117 sk_for_each(sk
, node
, &iucv_sk_list
.head
) {
2118 if (trans_hdr
->flags
== AF_IUCV_FLAG_SYN
) {
2119 if ((!memcmp(&iucv_sk(sk
)->src_name
,
2120 trans_hdr
->destAppName
, 8)) &&
2121 (!memcmp(&iucv_sk(sk
)->src_user_id
,
2122 trans_hdr
->destUserID
, 8)) &&
2123 (!memcmp(&iucv_sk(sk
)->dst_name
, nullstring
, 8)) &&
2124 (!memcmp(&iucv_sk(sk
)->dst_user_id
,
2130 if ((!memcmp(&iucv_sk(sk
)->src_name
,
2131 trans_hdr
->destAppName
, 8)) &&
2132 (!memcmp(&iucv_sk(sk
)->src_user_id
,
2133 trans_hdr
->destUserID
, 8)) &&
2134 (!memcmp(&iucv_sk(sk
)->dst_name
,
2135 trans_hdr
->srcAppName
, 8)) &&
2136 (!memcmp(&iucv_sk(sk
)->dst_user_id
,
2137 trans_hdr
->srcUserID
, 8))) {
2143 read_unlock(&iucv_sk_list
.lock
);
2148 how should we send with no sock
2149 1) send without sock no send rc checking?
2150 2) introduce default sock to handle this cases
2152 SYN -> send SYN|ACK in good case, send SYN|FIN in bad case
2154 SYN|ACK, SYN|FIN, FIN -> no action? */
2156 switch (trans_hdr
->flags
) {
2157 case AF_IUCV_FLAG_SYN
:
2158 /* connect request */
2159 err
= afiucv_hs_callback_syn(sk
, skb
);
2161 case (AF_IUCV_FLAG_SYN
| AF_IUCV_FLAG_ACK
):
2162 /* connect request confirmed */
2163 err
= afiucv_hs_callback_synack(sk
, skb
);
2165 case (AF_IUCV_FLAG_SYN
| AF_IUCV_FLAG_FIN
):
2166 /* connect request refused */
2167 err
= afiucv_hs_callback_synfin(sk
, skb
);
2169 case (AF_IUCV_FLAG_FIN
):
2171 err
= afiucv_hs_callback_fin(sk
, skb
);
2173 case (AF_IUCV_FLAG_WIN
):
2174 err
= afiucv_hs_callback_win(sk
, skb
);
2175 if (skb
->len
> sizeof(struct af_iucv_trans_hdr
))
2176 err
= afiucv_hs_callback_rx(sk
, skb
);
2181 /* plain data frame */
2182 memcpy(CB_TRGCLS(skb
), &trans_hdr
->iucv_hdr
.class,
2184 err
= afiucv_hs_callback_rx(sk
, skb
);
2194 * afiucv_hs_callback_txnotify() - handle send notifcations from HiperSockets
2197 static void afiucv_hs_callback_txnotify(struct sk_buff
*skb
,
2198 enum iucv_tx_notify n
)
2200 struct sock
*isk
= skb
->sk
;
2201 struct sock
*sk
= NULL
;
2202 struct iucv_sock
*iucv
= NULL
;
2203 struct sk_buff_head
*list
;
2204 struct sk_buff
*list_skb
;
2205 struct sk_buff
*this = NULL
;
2206 unsigned long flags
;
2207 struct hlist_node
*node
;
2209 read_lock(&iucv_sk_list
.lock
);
2210 sk_for_each(sk
, node
, &iucv_sk_list
.head
)
2215 read_unlock(&iucv_sk_list
.lock
);
2221 list
= &iucv
->send_skb_q
;
2222 list_skb
= list
->next
;
2223 if (skb_queue_empty(list
))
2226 spin_lock_irqsave(&list
->lock
, flags
);
2227 while (list_skb
!= (struct sk_buff
*)list
) {
2228 if (skb_shinfo(list_skb
) == skb_shinfo(skb
)) {
2232 __skb_unlink(this, list
);
2233 iucv_sock_wake_msglim(sk
);
2237 case TX_NOTIFY_PENDING
:
2238 atomic_inc(&iucv
->pendings
);
2240 case TX_NOTIFY_DELAYED_OK
:
2241 __skb_unlink(this, list
);
2242 atomic_dec(&iucv
->pendings
);
2243 if (atomic_read(&iucv
->pendings
) <= 0)
2244 iucv_sock_wake_msglim(sk
);
2248 case TX_NOTIFY_UNREACHABLE
:
2249 case TX_NOTIFY_DELAYED_UNREACHABLE
:
2250 case TX_NOTIFY_TPQFULL
: /* not yet used */
2251 case TX_NOTIFY_GENERALERROR
:
2252 case TX_NOTIFY_DELAYED_GENERALERROR
:
2253 __skb_unlink(this, list
);
2256 sk
->sk_state
= IUCV_DISCONN
;
2257 sk
->sk_state_change(sk
);
2262 list_skb
= list_skb
->next
;
2264 spin_unlock_irqrestore(&list
->lock
, flags
);
2266 if (sk
->sk_state
== IUCV_CLOSING
) {
2267 if (skb_queue_empty(&iucv_sk(sk
)->send_skb_q
)) {
2268 sk
->sk_state
= IUCV_CLOSED
;
2269 sk
->sk_state_change(sk
);
2276 static const struct proto_ops iucv_sock_ops
= {
2278 .owner
= THIS_MODULE
,
2279 .release
= iucv_sock_release
,
2280 .bind
= iucv_sock_bind
,
2281 .connect
= iucv_sock_connect
,
2282 .listen
= iucv_sock_listen
,
2283 .accept
= iucv_sock_accept
,
2284 .getname
= iucv_sock_getname
,
2285 .sendmsg
= iucv_sock_sendmsg
,
2286 .recvmsg
= iucv_sock_recvmsg
,
2287 .poll
= iucv_sock_poll
,
2288 .ioctl
= sock_no_ioctl
,
2289 .mmap
= sock_no_mmap
,
2290 .socketpair
= sock_no_socketpair
,
2291 .shutdown
= iucv_sock_shutdown
,
2292 .setsockopt
= iucv_sock_setsockopt
,
2293 .getsockopt
= iucv_sock_getsockopt
,
2296 static const struct net_proto_family iucv_sock_family_ops
= {
2298 .owner
= THIS_MODULE
,
2299 .create
= iucv_sock_create
,
2302 static struct packet_type iucv_packet_type
= {
2303 .type
= cpu_to_be16(ETH_P_AF_IUCV
),
2304 .func
= afiucv_hs_rcv
,
2307 static int afiucv_iucv_init(void)
2311 err
= pr_iucv
->iucv_register(&af_iucv_handler
, 0);
2314 /* establish dummy device */
2315 af_iucv_driver
.bus
= pr_iucv
->bus
;
2316 err
= driver_register(&af_iucv_driver
);
2319 af_iucv_dev
= kzalloc(sizeof(struct device
), GFP_KERNEL
);
2324 dev_set_name(af_iucv_dev
, "af_iucv");
2325 af_iucv_dev
->bus
= pr_iucv
->bus
;
2326 af_iucv_dev
->parent
= pr_iucv
->root
;
2327 af_iucv_dev
->release
= (void (*)(struct device
*))kfree
;
2328 af_iucv_dev
->driver
= &af_iucv_driver
;
2329 err
= device_register(af_iucv_dev
);
2335 driver_unregister(&af_iucv_driver
);
2337 pr_iucv
->iucv_unregister(&af_iucv_handler
, 0);
2342 static int __init
afiucv_init(void)
2346 if (MACHINE_IS_VM
) {
2347 cpcmd("QUERY USERID", iucv_userid
, sizeof(iucv_userid
), &err
);
2348 if (unlikely(err
)) {
2350 err
= -EPROTONOSUPPORT
;
2354 pr_iucv
= try_then_request_module(symbol_get(iucv_if
), "iucv");
2356 printk(KERN_WARNING
"iucv_if lookup failed\n");
2357 memset(&iucv_userid
, 0, sizeof(iucv_userid
));
2360 memset(&iucv_userid
, 0, sizeof(iucv_userid
));
2364 err
= proto_register(&iucv_proto
, 0);
2367 err
= sock_register(&iucv_sock_family_ops
);
2372 err
= afiucv_iucv_init();
2376 dev_add_pack(&iucv_packet_type
);
2380 sock_unregister(PF_IUCV
);
2382 proto_unregister(&iucv_proto
);
2385 symbol_put(iucv_if
);
2389 static void __exit
afiucv_exit(void)
2392 device_unregister(af_iucv_dev
);
2393 driver_unregister(&af_iucv_driver
);
2394 pr_iucv
->iucv_unregister(&af_iucv_handler
, 0);
2395 symbol_put(iucv_if
);
2397 dev_remove_pack(&iucv_packet_type
);
2398 sock_unregister(PF_IUCV
);
2399 proto_unregister(&iucv_proto
);
2402 module_init(afiucv_init
);
2403 module_exit(afiucv_exit
);
2405 MODULE_AUTHOR("Jennifer Hunt <jenhunt@us.ibm.com>");
2406 MODULE_DESCRIPTION("IUCV Sockets ver " VERSION
);
2407 MODULE_VERSION(VERSION
);
2408 MODULE_LICENSE("GPL");
2409 MODULE_ALIAS_NETPROTO(PF_IUCV
);