blob: ad90cf29c96ec9dea8feeec74131db33ac48dd7a [file] [log] [blame]
Jennifer Hunteac37312007-02-08 13:51:54 -08001/*
Jennifer Hunteac37312007-02-08 13:51:54 -08002 * IUCV protocol stack for Linux on zSeries
3 *
Ursula Braunc23cad92009-06-16 10:30:44 +02004 * Copyright IBM Corp. 2006, 2009
Jennifer Hunteac37312007-02-08 13:51:54 -08005 *
6 * Author(s): Jennifer Hunt <jenhunt@us.ibm.com>
Ursula Braunc23cad92009-06-16 10:30:44 +02007 * Hendrik Brueckner <brueckner@linux.vnet.ibm.com>
8 * PM functions:
9 * Ursula Braun <ursula.braun@de.ibm.com>
Jennifer Hunteac37312007-02-08 13:51:54 -080010 */
11
Ursula Braun8f7c5022008-12-25 13:39:47 +010012#define KMSG_COMPONENT "af_iucv"
13#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
14
Jennifer Hunteac37312007-02-08 13:51:54 -080015#include <linux/module.h>
16#include <linux/types.h>
17#include <linux/list.h>
18#include <linux/errno.h>
19#include <linux/kernel.h>
20#include <linux/sched.h>
21#include <linux/slab.h>
22#include <linux/skbuff.h>
23#include <linux/init.h>
24#include <linux/poll.h>
25#include <net/sock.h>
26#include <asm/ebcdic.h>
27#include <asm/cpcmd.h>
28#include <linux/kmod.h>
29
Jennifer Hunteac37312007-02-08 13:51:54 -080030#include <net/iucv/af_iucv.h>
31
Ursula Braun3881ac42011-08-08 01:33:54 +000032#define VERSION "1.2"
Jennifer Hunteac37312007-02-08 13:51:54 -080033
34static char iucv_userid[80];
35
Alexey Dobriyan5708e862009-09-14 12:23:23 +000036static const struct proto_ops iucv_sock_ops;
Jennifer Hunteac37312007-02-08 13:51:54 -080037
38static struct proto iucv_proto = {
39 .name = "AF_IUCV",
40 .owner = THIS_MODULE,
41 .obj_size = sizeof(struct iucv_sock),
42};
43
Frank Blaschka6fcd61f2011-08-08 01:33:51 +000044static struct iucv_interface *pr_iucv;
45
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +000046/* special AF_IUCV IPRM messages */
47static const u8 iprm_shutdown[8] =
48 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01};
49
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +000050#define TRGCLS_SIZE (sizeof(((struct iucv_message *)0)->class))
51
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)
57
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +000058#define __iucv_sock_wait(sk, condition, timeo, ret) \
59do { \
60 DEFINE_WAIT(__wait); \
61 long __timeo = timeo; \
62 ret = 0; \
Eric Dumazetaa395142010-04-20 13:03:51 +000063 prepare_to_wait(sk_sleep(sk), &__wait, TASK_INTERRUPTIBLE); \
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +000064 while (!(condition)) { \
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +000065 if (!__timeo) { \
66 ret = -EAGAIN; \
67 break; \
68 } \
69 if (signal_pending(current)) { \
70 ret = sock_intr_errno(__timeo); \
71 break; \
72 } \
73 release_sock(sk); \
74 __timeo = schedule_timeout(__timeo); \
75 lock_sock(sk); \
76 ret = sock_error(sk); \
77 if (ret) \
78 break; \
79 } \
Eric Dumazetaa395142010-04-20 13:03:51 +000080 finish_wait(sk_sleep(sk), &__wait); \
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +000081} while (0)
82
83#define iucv_sock_wait(sk, condition, timeo) \
84({ \
85 int __ret = 0; \
86 if (!(condition)) \
87 __iucv_sock_wait(sk, condition, timeo, __ret); \
88 __ret; \
89})
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +000090
Heiko Carstens57f20442007-10-08 02:02:52 -070091static void iucv_sock_kill(struct sock *sk);
92static void iucv_sock_close(struct sock *sk);
93
Ursula Braun3881ac42011-08-08 01:33:54 +000094static int afiucv_hs_rcv(struct sk_buff *skb, struct net_device *dev,
95 struct packet_type *pt, struct net_device *orig_dev);
96static int afiucv_hs_send(struct iucv_message *imsg, struct sock *sock,
97 struct sk_buff *skb, u8 flags);
98static void afiucv_hs_callback_txnotify(struct sk_buff *, enum iucv_tx_notify);
99
Jennifer Hunteac37312007-02-08 13:51:54 -0800100/* Call Back functions */
101static void iucv_callback_rx(struct iucv_path *, struct iucv_message *);
102static void iucv_callback_txdone(struct iucv_path *, struct iucv_message *);
103static void iucv_callback_connack(struct iucv_path *, u8 ipuser[16]);
Heiko Carstensda99f052007-05-04 12:23:27 -0700104static int iucv_callback_connreq(struct iucv_path *, u8 ipvmid[8],
105 u8 ipuser[16]);
Jennifer Hunteac37312007-02-08 13:51:54 -0800106static void iucv_callback_connrej(struct iucv_path *, u8 ipuser[16]);
Hendrik Brueckneraf88b522009-04-21 23:26:21 +0000107static void iucv_callback_shutdown(struct iucv_path *, u8 ipuser[16]);
Jennifer Hunteac37312007-02-08 13:51:54 -0800108
109static struct iucv_sock_list iucv_sk_list = {
Robert P. J. Day3db8ce32008-04-10 02:11:24 -0700110 .lock = __RW_LOCK_UNLOCKED(iucv_sk_list.lock),
Jennifer Hunteac37312007-02-08 13:51:54 -0800111 .autobind_name = ATOMIC_INIT(0)
112};
113
114static 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,
Hendrik Brueckneraf88b522009-04-21 23:26:21 +0000119 .message_complete = iucv_callback_txdone,
120 .path_quiesced = iucv_callback_shutdown,
Jennifer Hunteac37312007-02-08 13:51:54 -0800121};
122
123static inline void high_nmcpy(unsigned char *dst, char *src)
124{
125 memcpy(dst, src, 8);
126}
127
128static inline void low_nmcpy(unsigned char *dst, char *src)
129{
130 memcpy(&dst[8], src, 8);
131}
132
Ursula Braun816abba2011-12-19 22:56:29 +0000133static void iucv_skb_queue_purge(struct sk_buff_head *list)
134{
135 struct sk_buff *skb;
136
137 while ((skb = skb_dequeue(list)) != NULL) {
138 if (skb->dev)
139 dev_put(skb->dev);
140 kfree_skb(skb);
141 }
142}
143
Ursula Braunc23cad92009-06-16 10:30:44 +0200144static int afiucv_pm_prepare(struct device *dev)
145{
146#ifdef CONFIG_PM_DEBUG
147 printk(KERN_WARNING "afiucv_pm_prepare\n");
148#endif
149 return 0;
150}
151
152static void afiucv_pm_complete(struct device *dev)
153{
154#ifdef CONFIG_PM_DEBUG
155 printk(KERN_WARNING "afiucv_pm_complete\n");
156#endif
Ursula Braunc23cad92009-06-16 10:30:44 +0200157}
158
159/**
160 * afiucv_pm_freeze() - Freeze PM callback
161 * @dev: AFIUCV dummy device
162 *
163 * Sever all established IUCV communication pathes
164 */
165static int afiucv_pm_freeze(struct device *dev)
166{
167 struct iucv_sock *iucv;
168 struct sock *sk;
169 struct hlist_node *node;
170 int err = 0;
171
172#ifdef CONFIG_PM_DEBUG
173 printk(KERN_WARNING "afiucv_pm_freeze\n");
174#endif
175 read_lock(&iucv_sk_list.lock);
176 sk_for_each(sk, node, &iucv_sk_list.head) {
177 iucv = iucv_sk(sk);
Ursula Braun816abba2011-12-19 22:56:29 +0000178 iucv_skb_queue_purge(&iucv->send_skb_q);
Ursula Braunc23cad92009-06-16 10:30:44 +0200179 skb_queue_purge(&iucv->backlog_skb_q);
180 switch (sk->sk_state) {
181 case IUCV_SEVERED:
182 case IUCV_DISCONN:
183 case IUCV_CLOSING:
184 case IUCV_CONNECTED:
185 if (iucv->path) {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +0000186 err = pr_iucv->path_sever(iucv->path, NULL);
Ursula Braunc23cad92009-06-16 10:30:44 +0200187 iucv_path_free(iucv->path);
188 iucv->path = NULL;
189 }
190 break;
191 case IUCV_OPEN:
192 case IUCV_BOUND:
193 case IUCV_LISTEN:
194 case IUCV_CLOSED:
195 default:
196 break;
197 }
198 }
199 read_unlock(&iucv_sk_list.lock);
200 return err;
201}
202
203/**
204 * afiucv_pm_restore_thaw() - Thaw and restore PM callback
205 * @dev: AFIUCV dummy device
206 *
207 * socket clean up after freeze
208 */
209static int afiucv_pm_restore_thaw(struct device *dev)
210{
Ursula Braunc23cad92009-06-16 10:30:44 +0200211 struct sock *sk;
212 struct hlist_node *node;
213
214#ifdef CONFIG_PM_DEBUG
215 printk(KERN_WARNING "afiucv_pm_restore_thaw\n");
216#endif
217 read_lock(&iucv_sk_list.lock);
218 sk_for_each(sk, node, &iucv_sk_list.head) {
Ursula Braunc23cad92009-06-16 10:30:44 +0200219 switch (sk->sk_state) {
220 case IUCV_CONNECTED:
221 sk->sk_err = EPIPE;
222 sk->sk_state = IUCV_DISCONN;
223 sk->sk_state_change(sk);
224 break;
225 case IUCV_DISCONN:
226 case IUCV_SEVERED:
227 case IUCV_CLOSING:
228 case IUCV_LISTEN:
229 case IUCV_BOUND:
230 case IUCV_OPEN:
231 default:
232 break;
233 }
234 }
235 read_unlock(&iucv_sk_list.lock);
236 return 0;
237}
238
Alexey Dobriyan47145212009-12-14 18:00:08 -0800239static const struct dev_pm_ops afiucv_pm_ops = {
Ursula Braunc23cad92009-06-16 10:30:44 +0200240 .prepare = afiucv_pm_prepare,
241 .complete = afiucv_pm_complete,
242 .freeze = afiucv_pm_freeze,
243 .thaw = afiucv_pm_restore_thaw,
244 .restore = afiucv_pm_restore_thaw,
245};
246
247static struct device_driver af_iucv_driver = {
248 .owner = THIS_MODULE,
249 .name = "afiucv",
Frank Blaschka6fcd61f2011-08-08 01:33:51 +0000250 .bus = NULL,
Ursula Braunc23cad92009-06-16 10:30:44 +0200251 .pm = &afiucv_pm_ops,
252};
253
254/* dummy device used as trigger for PM functions */
255static struct device *af_iucv_dev;
256
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000257/**
258 * iucv_msg_length() - Returns the length of an iucv message.
259 * @msg: Pointer to struct iucv_message, MUST NOT be NULL
260 *
261 * The function returns the length of the specified iucv message @msg of data
262 * stored in a buffer and of data stored in the parameter list (PRMDATA).
263 *
264 * For IUCV_IPRMDATA, AF_IUCV uses the following convention to transport socket
265 * data:
266 * PRMDATA[0..6] socket data (max 7 bytes);
267 * PRMDATA[7] socket data length value (len is 0xff - PRMDATA[7])
268 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300269 * The socket data length is computed by subtracting the socket data length
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000270 * value from 0xFF.
271 * If the socket data len is greater 7, then PRMDATA can be used for special
272 * notifications (see iucv_sock_shutdown); and further,
273 * if the socket data len is > 7, the function returns 8.
274 *
275 * Use this function to allocate socket buffers to store iucv message data.
276 */
277static inline size_t iucv_msg_length(struct iucv_message *msg)
278{
279 size_t datalen;
280
281 if (msg->flags & IUCV_IPRMDATA) {
282 datalen = 0xff - msg->rmmsg[7];
283 return (datalen < 8) ? datalen : 8;
284 }
285 return msg->length;
286}
287
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +0000288/**
289 * iucv_sock_in_state() - check for specific states
290 * @sk: sock structure
291 * @state: first iucv sk state
292 * @state: second iucv sk state
293 *
294 * Returns true if the socket in either in the first or second state.
295 */
296static int iucv_sock_in_state(struct sock *sk, int state, int state2)
297{
298 return (sk->sk_state == state || sk->sk_state == state2);
299}
300
301/**
302 * iucv_below_msglim() - function to check if messages can be sent
303 * @sk: sock structure
304 *
305 * Returns true if the send queue length is lower than the message limit.
306 * Always returns true if the socket is not connected (no iucv path for
307 * checking the message limit).
308 */
309static inline int iucv_below_msglim(struct sock *sk)
310{
311 struct iucv_sock *iucv = iucv_sk(sk);
312
313 if (sk->sk_state != IUCV_CONNECTED)
314 return 1;
Ursula Braun3881ac42011-08-08 01:33:54 +0000315 if (iucv->transport == AF_IUCV_TRANS_IUCV)
316 return (skb_queue_len(&iucv->send_skb_q) < iucv->path->msglim);
317 else
318 return ((atomic_read(&iucv->msg_sent) < iucv->msglimit_peer) &&
319 (atomic_read(&iucv->pendings) <= 0));
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +0000320}
321
322/**
323 * iucv_sock_wake_msglim() - Wake up thread waiting on msg limit
324 */
325static void iucv_sock_wake_msglim(struct sock *sk)
326{
Eric Dumazet43815482010-04-29 11:01:49 +0000327 struct socket_wq *wq;
328
329 rcu_read_lock();
330 wq = rcu_dereference(sk->sk_wq);
331 if (wq_has_sleeper(wq))
332 wake_up_interruptible_all(&wq->wait);
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +0000333 sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
Eric Dumazet43815482010-04-29 11:01:49 +0000334 rcu_read_unlock();
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +0000335}
336
Ursula Braun3881ac42011-08-08 01:33:54 +0000337/**
338 * afiucv_hs_send() - send a message through HiperSockets transport
339 */
340static int afiucv_hs_send(struct iucv_message *imsg, struct sock *sock,
341 struct sk_buff *skb, u8 flags)
342{
343 struct net *net = sock_net(sock);
344 struct iucv_sock *iucv = iucv_sk(sock);
345 struct af_iucv_trans_hdr *phs_hdr;
346 struct sk_buff *nskb;
347 int err, confirm_recv = 0;
348
349 memset(skb->head, 0, ETH_HLEN);
350 phs_hdr = (struct af_iucv_trans_hdr *)skb_push(skb,
351 sizeof(struct af_iucv_trans_hdr));
352 skb_reset_mac_header(skb);
353 skb_reset_network_header(skb);
354 skb_push(skb, ETH_HLEN);
355 skb_reset_mac_header(skb);
356 memset(phs_hdr, 0, sizeof(struct af_iucv_trans_hdr));
357
358 phs_hdr->magic = ETH_P_AF_IUCV;
359 phs_hdr->version = 1;
360 phs_hdr->flags = flags;
361 if (flags == AF_IUCV_FLAG_SYN)
362 phs_hdr->window = iucv->msglimit;
363 else if ((flags == AF_IUCV_FLAG_WIN) || !flags) {
364 confirm_recv = atomic_read(&iucv->msg_recv);
365 phs_hdr->window = confirm_recv;
366 if (confirm_recv)
367 phs_hdr->flags = phs_hdr->flags | AF_IUCV_FLAG_WIN;
368 }
369 memcpy(phs_hdr->destUserID, iucv->dst_user_id, 8);
370 memcpy(phs_hdr->destAppName, iucv->dst_name, 8);
371 memcpy(phs_hdr->srcUserID, iucv->src_user_id, 8);
372 memcpy(phs_hdr->srcAppName, iucv->src_name, 8);
373 ASCEBC(phs_hdr->destUserID, sizeof(phs_hdr->destUserID));
374 ASCEBC(phs_hdr->destAppName, sizeof(phs_hdr->destAppName));
375 ASCEBC(phs_hdr->srcUserID, sizeof(phs_hdr->srcUserID));
376 ASCEBC(phs_hdr->srcAppName, sizeof(phs_hdr->srcAppName));
377 if (imsg)
378 memcpy(&phs_hdr->iucv_hdr, imsg, sizeof(struct iucv_message));
379
Ursula Braun816abba2011-12-19 22:56:29 +0000380 skb->dev = dev_get_by_index(net, sock->sk_bound_dev_if);
Ursula Braun3881ac42011-08-08 01:33:54 +0000381 if (!skb->dev)
382 return -ENODEV;
383 if (!(skb->dev->flags & IFF_UP))
384 return -ENETDOWN;
385 if (skb->len > skb->dev->mtu) {
386 if (sock->sk_type == SOCK_SEQPACKET)
387 return -EMSGSIZE;
388 else
389 skb_trim(skb, skb->dev->mtu);
390 }
391 skb->protocol = ETH_P_AF_IUCV;
392 skb_shinfo(skb)->tx_flags |= SKBTX_DRV_NEEDS_SK_REF;
393 nskb = skb_clone(skb, GFP_ATOMIC);
394 if (!nskb)
395 return -ENOMEM;
396 skb_queue_tail(&iucv->send_skb_q, nskb);
397 err = dev_queue_xmit(skb);
398 if (err) {
399 skb_unlink(nskb, &iucv->send_skb_q);
Ursula Braun816abba2011-12-19 22:56:29 +0000400 dev_put(nskb->dev);
Ursula Braun3881ac42011-08-08 01:33:54 +0000401 kfree_skb(nskb);
402 } else {
403 atomic_sub(confirm_recv, &iucv->msg_recv);
404 WARN_ON(atomic_read(&iucv->msg_recv) < 0);
405 }
406 return err;
407}
408
Jennifer Hunteac37312007-02-08 13:51:54 -0800409/* Timers */
410static void iucv_sock_timeout(unsigned long arg)
411{
412 struct sock *sk = (struct sock *)arg;
413
414 bh_lock_sock(sk);
415 sk->sk_err = ETIMEDOUT;
416 sk->sk_state_change(sk);
417 bh_unlock_sock(sk);
418
419 iucv_sock_kill(sk);
420 sock_put(sk);
421}
422
423static void iucv_sock_clear_timer(struct sock *sk)
424{
425 sk_stop_timer(sk, &sk->sk_timer);
426}
427
Jennifer Hunteac37312007-02-08 13:51:54 -0800428static struct sock *__iucv_get_sock_by_name(char *nm)
429{
430 struct sock *sk;
431 struct hlist_node *node;
432
433 sk_for_each(sk, node, &iucv_sk_list.head)
434 if (!memcmp(&iucv_sk(sk)->src_name, nm, 8))
435 return sk;
436
437 return NULL;
438}
439
440static void iucv_sock_destruct(struct sock *sk)
441{
442 skb_queue_purge(&sk->sk_receive_queue);
443 skb_queue_purge(&sk->sk_write_queue);
444}
445
446/* Cleanup Listen */
447static void iucv_sock_cleanup_listen(struct sock *parent)
448{
449 struct sock *sk;
450
451 /* Close non-accepted connections */
452 while ((sk = iucv_accept_dequeue(parent, NULL))) {
453 iucv_sock_close(sk);
454 iucv_sock_kill(sk);
455 }
456
457 parent->sk_state = IUCV_CLOSED;
Jennifer Hunteac37312007-02-08 13:51:54 -0800458}
459
Hendrik Brueckner7514bab2009-09-16 04:37:27 +0000460/* Kill socket (only if zapped and orphaned) */
Jennifer Hunteac37312007-02-08 13:51:54 -0800461static void iucv_sock_kill(struct sock *sk)
462{
463 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
464 return;
465
466 iucv_sock_unlink(&iucv_sk_list, sk);
467 sock_set_flag(sk, SOCK_DEAD);
468 sock_put(sk);
469}
470
471/* Close an IUCV socket */
472static void iucv_sock_close(struct sock *sk)
473{
474 unsigned char user_data[16];
475 struct iucv_sock *iucv = iucv_sk(sk);
Jennifer Hunt561e0362007-05-04 12:22:07 -0700476 unsigned long timeo;
Ursula Braun3881ac42011-08-08 01:33:54 +0000477 int err, blen;
478 struct sk_buff *skb;
Jennifer Hunteac37312007-02-08 13:51:54 -0800479
480 iucv_sock_clear_timer(sk);
481 lock_sock(sk);
482
Heiko Carstensda99f052007-05-04 12:23:27 -0700483 switch (sk->sk_state) {
Jennifer Hunteac37312007-02-08 13:51:54 -0800484 case IUCV_LISTEN:
485 iucv_sock_cleanup_listen(sk);
486 break;
487
488 case IUCV_CONNECTED:
Ursula Braun3881ac42011-08-08 01:33:54 +0000489 if (iucv->transport == AF_IUCV_TRANS_HIPER) {
490 /* send fin */
491 blen = sizeof(struct af_iucv_trans_hdr) + ETH_HLEN;
492 skb = sock_alloc_send_skb(sk, blen, 1, &err);
493 if (skb) {
Ursula Braun816abba2011-12-19 22:56:29 +0000494 skb_reserve(skb, blen);
Ursula Braun3881ac42011-08-08 01:33:54 +0000495 err = afiucv_hs_send(NULL, sk, skb,
496 AF_IUCV_FLAG_FIN);
497 }
498 sk->sk_state = IUCV_DISCONN;
499 sk->sk_state_change(sk);
500 }
Ursula Braun816abba2011-12-19 22:56:29 +0000501 case IUCV_DISCONN: /* fall through */
Jennifer Hunt561e0362007-05-04 12:22:07 -0700502 sk->sk_state = IUCV_CLOSING;
503 sk->sk_state_change(sk);
504
Heiko Carstensda99f052007-05-04 12:23:27 -0700505 if (!skb_queue_empty(&iucv->send_skb_q)) {
Jennifer Hunt561e0362007-05-04 12:22:07 -0700506 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
507 timeo = sk->sk_lingertime;
508 else
509 timeo = IUCV_DISCONN_TIMEOUT;
Ursula Braun9f6298a2011-05-12 18:45:08 +0000510 iucv_sock_wait(sk,
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +0000511 iucv_sock_in_state(sk, IUCV_CLOSED, 0),
512 timeo);
Jennifer Hunt561e0362007-05-04 12:22:07 -0700513 }
514
Ursula Braunbbe188c2009-04-21 06:04:20 +0000515 case IUCV_CLOSING: /* fall through */
Jennifer Hunt561e0362007-05-04 12:22:07 -0700516 sk->sk_state = IUCV_CLOSED;
517 sk->sk_state_change(sk);
518
Jennifer Hunteac37312007-02-08 13:51:54 -0800519 if (iucv->path) {
520 low_nmcpy(user_data, iucv->src_name);
521 high_nmcpy(user_data, iucv->dst_name);
522 ASCEBC(user_data, sizeof(user_data));
Frank Blaschka6fcd61f2011-08-08 01:33:51 +0000523 pr_iucv->path_sever(iucv->path, user_data);
Jennifer Hunteac37312007-02-08 13:51:54 -0800524 iucv_path_free(iucv->path);
525 iucv->path = NULL;
526 }
527
Jennifer Hunteac37312007-02-08 13:51:54 -0800528 sk->sk_err = ECONNRESET;
529 sk->sk_state_change(sk);
530
Ursula Braun816abba2011-12-19 22:56:29 +0000531 iucv_skb_queue_purge(&iucv->send_skb_q);
Jennifer Hunt561e0362007-05-04 12:22:07 -0700532 skb_queue_purge(&iucv->backlog_skb_q);
Jennifer Hunteac37312007-02-08 13:51:54 -0800533 break;
534
535 default:
Hendrik Brueckner7514bab2009-09-16 04:37:27 +0000536 /* nothing to do here */
Jennifer Hunteac37312007-02-08 13:51:54 -0800537 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700538 }
Jennifer Hunteac37312007-02-08 13:51:54 -0800539
Hendrik Brueckner7514bab2009-09-16 04:37:27 +0000540 /* mark socket for deletion by iucv_sock_kill() */
541 sock_set_flag(sk, SOCK_ZAPPED);
542
Jennifer Hunteac37312007-02-08 13:51:54 -0800543 release_sock(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800544}
545
546static void iucv_sock_init(struct sock *sk, struct sock *parent)
547{
548 if (parent)
549 sk->sk_type = parent->sk_type;
550}
551
552static struct sock *iucv_sock_alloc(struct socket *sock, int proto, gfp_t prio)
553{
554 struct sock *sk;
Ursula Braun493d3972011-08-08 01:33:52 +0000555 struct iucv_sock *iucv;
Jennifer Hunteac37312007-02-08 13:51:54 -0800556
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700557 sk = sk_alloc(&init_net, PF_IUCV, prio, &iucv_proto);
Jennifer Hunteac37312007-02-08 13:51:54 -0800558 if (!sk)
559 return NULL;
Ursula Braun493d3972011-08-08 01:33:52 +0000560 iucv = iucv_sk(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800561
562 sock_init_data(sock, sk);
Ursula Braun493d3972011-08-08 01:33:52 +0000563 INIT_LIST_HEAD(&iucv->accept_q);
564 spin_lock_init(&iucv->accept_q_lock);
565 skb_queue_head_init(&iucv->send_skb_q);
566 INIT_LIST_HEAD(&iucv->message_q.list);
567 spin_lock_init(&iucv->message_q.lock);
568 skb_queue_head_init(&iucv->backlog_skb_q);
569 iucv->send_tag = 0;
Ursula Braun3881ac42011-08-08 01:33:54 +0000570 atomic_set(&iucv->pendings, 0);
Ursula Braun493d3972011-08-08 01:33:52 +0000571 iucv->flags = 0;
Ursula Braun3881ac42011-08-08 01:33:54 +0000572 iucv->msglimit = 0;
573 atomic_set(&iucv->msg_sent, 0);
574 atomic_set(&iucv->msg_recv, 0);
Ursula Braun493d3972011-08-08 01:33:52 +0000575 iucv->path = NULL;
Ursula Braun3881ac42011-08-08 01:33:54 +0000576 iucv->sk_txnotify = afiucv_hs_callback_txnotify;
Ursula Braun493d3972011-08-08 01:33:52 +0000577 memset(&iucv->src_user_id , 0, 32);
Ursula Braun3881ac42011-08-08 01:33:54 +0000578 if (pr_iucv)
579 iucv->transport = AF_IUCV_TRANS_IUCV;
580 else
581 iucv->transport = AF_IUCV_TRANS_HIPER;
Jennifer Hunteac37312007-02-08 13:51:54 -0800582
583 sk->sk_destruct = iucv_sock_destruct;
584 sk->sk_sndtimeo = IUCV_CONN_TIMEOUT;
585 sk->sk_allocation = GFP_DMA;
586
587 sock_reset_flag(sk, SOCK_ZAPPED);
588
589 sk->sk_protocol = proto;
590 sk->sk_state = IUCV_OPEN;
591
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800592 setup_timer(&sk->sk_timer, iucv_sock_timeout, (unsigned long)sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800593
594 iucv_sock_link(&iucv_sk_list, sk);
595 return sk;
596}
597
598/* Create an IUCV socket */
Eric Paris3f378b62009-11-05 22:18:14 -0800599static int iucv_sock_create(struct net *net, struct socket *sock, int protocol,
600 int kern)
Jennifer Hunteac37312007-02-08 13:51:54 -0800601{
602 struct sock *sk;
603
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +0000604 if (protocol && protocol != PF_IUCV)
605 return -EPROTONOSUPPORT;
Jennifer Hunteac37312007-02-08 13:51:54 -0800606
607 sock->state = SS_UNCONNECTED;
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +0000608
609 switch (sock->type) {
610 case SOCK_STREAM:
611 sock->ops = &iucv_sock_ops;
612 break;
613 case SOCK_SEQPACKET:
614 /* currently, proto ops can handle both sk types */
615 sock->ops = &iucv_sock_ops;
616 break;
617 default:
618 return -ESOCKTNOSUPPORT;
619 }
Jennifer Hunteac37312007-02-08 13:51:54 -0800620
621 sk = iucv_sock_alloc(sock, protocol, GFP_KERNEL);
622 if (!sk)
623 return -ENOMEM;
624
625 iucv_sock_init(sk, NULL);
626
627 return 0;
628}
629
630void iucv_sock_link(struct iucv_sock_list *l, struct sock *sk)
631{
632 write_lock_bh(&l->lock);
633 sk_add_node(sk, &l->head);
634 write_unlock_bh(&l->lock);
635}
636
637void iucv_sock_unlink(struct iucv_sock_list *l, struct sock *sk)
638{
639 write_lock_bh(&l->lock);
640 sk_del_node_init(sk);
641 write_unlock_bh(&l->lock);
642}
643
644void iucv_accept_enqueue(struct sock *parent, struct sock *sk)
645{
Ursula Braunfebca282007-07-14 19:04:25 -0700646 unsigned long flags;
647 struct iucv_sock *par = iucv_sk(parent);
648
Jennifer Hunteac37312007-02-08 13:51:54 -0800649 sock_hold(sk);
Ursula Braunfebca282007-07-14 19:04:25 -0700650 spin_lock_irqsave(&par->accept_q_lock, flags);
651 list_add_tail(&iucv_sk(sk)->accept_q, &par->accept_q);
652 spin_unlock_irqrestore(&par->accept_q_lock, flags);
Jennifer Hunteac37312007-02-08 13:51:54 -0800653 iucv_sk(sk)->parent = parent;
Hendrik Brueckner49f5eba2009-10-14 22:54:55 +0000654 sk_acceptq_added(parent);
Jennifer Hunteac37312007-02-08 13:51:54 -0800655}
656
657void iucv_accept_unlink(struct sock *sk)
658{
Ursula Braunfebca282007-07-14 19:04:25 -0700659 unsigned long flags;
660 struct iucv_sock *par = iucv_sk(iucv_sk(sk)->parent);
661
662 spin_lock_irqsave(&par->accept_q_lock, flags);
Jennifer Hunteac37312007-02-08 13:51:54 -0800663 list_del_init(&iucv_sk(sk)->accept_q);
Ursula Braunfebca282007-07-14 19:04:25 -0700664 spin_unlock_irqrestore(&par->accept_q_lock, flags);
Hendrik Brueckner49f5eba2009-10-14 22:54:55 +0000665 sk_acceptq_removed(iucv_sk(sk)->parent);
Jennifer Hunteac37312007-02-08 13:51:54 -0800666 iucv_sk(sk)->parent = NULL;
667 sock_put(sk);
668}
669
670struct sock *iucv_accept_dequeue(struct sock *parent, struct socket *newsock)
671{
672 struct iucv_sock *isk, *n;
673 struct sock *sk;
674
Heiko Carstensda99f052007-05-04 12:23:27 -0700675 list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) {
Jennifer Hunteac37312007-02-08 13:51:54 -0800676 sk = (struct sock *) isk;
677 lock_sock(sk);
678
679 if (sk->sk_state == IUCV_CLOSED) {
Jennifer Hunteac37312007-02-08 13:51:54 -0800680 iucv_accept_unlink(sk);
Ursula Braunfebca282007-07-14 19:04:25 -0700681 release_sock(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800682 continue;
683 }
684
685 if (sk->sk_state == IUCV_CONNECTED ||
686 sk->sk_state == IUCV_SEVERED ||
Hendrik Brueckner56a73de2009-09-16 04:37:26 +0000687 sk->sk_state == IUCV_DISCONN || /* due to PM restore */
Jennifer Hunteac37312007-02-08 13:51:54 -0800688 !newsock) {
689 iucv_accept_unlink(sk);
690 if (newsock)
691 sock_graft(sk, newsock);
692
693 if (sk->sk_state == IUCV_SEVERED)
694 sk->sk_state = IUCV_DISCONN;
695
696 release_sock(sk);
697 return sk;
698 }
699
700 release_sock(sk);
701 }
702 return NULL;
703}
704
Jennifer Hunteac37312007-02-08 13:51:54 -0800705/* Bind an unbound socket */
706static int iucv_sock_bind(struct socket *sock, struct sockaddr *addr,
707 int addr_len)
708{
709 struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
710 struct sock *sk = sock->sk;
711 struct iucv_sock *iucv;
Ursula Braun3881ac42011-08-08 01:33:54 +0000712 int err = 0;
713 struct net_device *dev;
714 char uid[9];
Jennifer Hunteac37312007-02-08 13:51:54 -0800715
716 /* Verify the input sockaddr */
717 if (!addr || addr->sa_family != AF_IUCV)
718 return -EINVAL;
719
720 lock_sock(sk);
721 if (sk->sk_state != IUCV_OPEN) {
722 err = -EBADFD;
723 goto done;
724 }
725
726 write_lock_bh(&iucv_sk_list.lock);
727
728 iucv = iucv_sk(sk);
729 if (__iucv_get_sock_by_name(sa->siucv_name)) {
730 err = -EADDRINUSE;
731 goto done_unlock;
732 }
Ursula Braun3881ac42011-08-08 01:33:54 +0000733 if (iucv->path)
Jennifer Hunteac37312007-02-08 13:51:54 -0800734 goto done_unlock;
Jennifer Hunteac37312007-02-08 13:51:54 -0800735
736 /* Bind the socket */
Jennifer Hunteac37312007-02-08 13:51:54 -0800737
Ursula Braun3881ac42011-08-08 01:33:54 +0000738 if (pr_iucv)
739 if (!memcmp(sa->siucv_user_id, iucv_userid, 8))
740 goto vm_bind; /* VM IUCV transport */
Jennifer Hunteac37312007-02-08 13:51:54 -0800741
Ursula Braun3881ac42011-08-08 01:33:54 +0000742 /* try hiper transport */
743 memcpy(uid, sa->siucv_user_id, sizeof(uid));
744 ASCEBC(uid, 8);
745 rcu_read_lock();
746 for_each_netdev_rcu(&init_net, dev) {
747 if (!memcmp(dev->perm_addr, uid, 8)) {
748 memcpy(iucv->src_name, sa->siucv_name, 8);
749 memcpy(iucv->src_user_id, sa->siucv_user_id, 8);
Ursula Braun816abba2011-12-19 22:56:29 +0000750 sk->sk_bound_dev_if = dev->ifindex;
Ursula Braun3881ac42011-08-08 01:33:54 +0000751 sk->sk_state = IUCV_BOUND;
752 iucv->transport = AF_IUCV_TRANS_HIPER;
753 if (!iucv->msglimit)
754 iucv->msglimit = IUCV_HIPER_MSGLIM_DEFAULT;
755 rcu_read_unlock();
756 goto done_unlock;
757 }
758 }
759 rcu_read_unlock();
760vm_bind:
761 if (pr_iucv) {
762 /* use local userid for backward compat */
763 memcpy(iucv->src_name, sa->siucv_name, 8);
764 memcpy(iucv->src_user_id, iucv_userid, 8);
765 sk->sk_state = IUCV_BOUND;
766 iucv->transport = AF_IUCV_TRANS_IUCV;
767 if (!iucv->msglimit)
768 iucv->msglimit = IUCV_QUEUELEN_DEFAULT;
769 goto done_unlock;
770 }
771 /* found no dev to bind */
772 err = -ENODEV;
Jennifer Hunteac37312007-02-08 13:51:54 -0800773done_unlock:
774 /* Release the socket list lock */
775 write_unlock_bh(&iucv_sk_list.lock);
776done:
777 release_sock(sk);
778 return err;
779}
780
781/* Automatically bind an unbound socket */
782static int iucv_sock_autobind(struct sock *sk)
783{
784 struct iucv_sock *iucv = iucv_sk(sk);
785 char query_buffer[80];
786 char name[12];
787 int err = 0;
788
789 /* Set the userid and name */
790 cpcmd("QUERY USERID", query_buffer, sizeof(query_buffer), &err);
791 if (unlikely(err))
792 return -EPROTO;
793
794 memcpy(iucv->src_user_id, query_buffer, 8);
795
796 write_lock_bh(&iucv_sk_list.lock);
797
798 sprintf(name, "%08x", atomic_inc_return(&iucv_sk_list.autobind_name));
799 while (__iucv_get_sock_by_name(name)) {
800 sprintf(name, "%08x",
801 atomic_inc_return(&iucv_sk_list.autobind_name));
802 }
803
804 write_unlock_bh(&iucv_sk_list.lock);
805
806 memcpy(&iucv->src_name, name, 8);
807
Ursula Braun3881ac42011-08-08 01:33:54 +0000808 if (!iucv->msglimit)
809 iucv->msglimit = IUCV_QUEUELEN_DEFAULT;
810
Jennifer Hunteac37312007-02-08 13:51:54 -0800811 return err;
812}
813
Ursula Braun3881ac42011-08-08 01:33:54 +0000814static int afiucv_hs_connect(struct socket *sock)
815{
816 struct sock *sk = sock->sk;
817 struct sk_buff *skb;
818 int blen = sizeof(struct af_iucv_trans_hdr) + ETH_HLEN;
819 int err = 0;
820
821 /* send syn */
822 skb = sock_alloc_send_skb(sk, blen, 1, &err);
823 if (!skb) {
824 err = -ENOMEM;
825 goto done;
826 }
827 skb->dev = NULL;
828 skb_reserve(skb, blen);
829 err = afiucv_hs_send(NULL, sk, skb, AF_IUCV_FLAG_SYN);
830done:
831 return err;
832}
833
834static int afiucv_path_connect(struct socket *sock, struct sockaddr *addr)
Jennifer Hunteac37312007-02-08 13:51:54 -0800835{
836 struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
837 struct sock *sk = sock->sk;
Ursula Braun493d3972011-08-08 01:33:52 +0000838 struct iucv_sock *iucv = iucv_sk(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800839 unsigned char user_data[16];
840 int err;
841
Jennifer Hunteac37312007-02-08 13:51:54 -0800842 high_nmcpy(user_data, sa->siucv_name);
Ursula Braun493d3972011-08-08 01:33:52 +0000843 low_nmcpy(user_data, iucv->src_name);
Jennifer Hunteac37312007-02-08 13:51:54 -0800844 ASCEBC(user_data, sizeof(user_data));
845
Jennifer Hunteac37312007-02-08 13:51:54 -0800846 /* Create path. */
Hendrik Brueckner09488e2e2009-04-21 23:26:27 +0000847 iucv->path = iucv_path_alloc(iucv->msglimit,
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000848 IUCV_IPRMDATA, GFP_KERNEL);
Ursula Braund4444722008-02-07 18:07:19 -0800849 if (!iucv->path) {
850 err = -ENOMEM;
851 goto done;
852 }
Frank Blaschka6fcd61f2011-08-08 01:33:51 +0000853 err = pr_iucv->path_connect(iucv->path, &af_iucv_handler,
854 sa->siucv_user_id, NULL, user_data,
855 sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800856 if (err) {
857 iucv_path_free(iucv->path);
858 iucv->path = NULL;
Hendrik Brueckner55cdea92009-01-05 18:07:07 -0800859 switch (err) {
860 case 0x0b: /* Target communicator is not logged on */
861 err = -ENETUNREACH;
862 break;
863 case 0x0d: /* Max connections for this guest exceeded */
864 case 0x0e: /* Max connections for target guest exceeded */
865 err = -EAGAIN;
866 break;
867 case 0x0f: /* Missing IUCV authorization */
868 err = -EACCES;
869 break;
870 default:
871 err = -ECONNREFUSED;
872 break;
873 }
Ursula Braun3881ac42011-08-08 01:33:54 +0000874 }
875done:
876 return err;
877}
878
879/* Connect an unconnected socket */
880static int iucv_sock_connect(struct socket *sock, struct sockaddr *addr,
881 int alen, int flags)
882{
883 struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
884 struct sock *sk = sock->sk;
885 struct iucv_sock *iucv = iucv_sk(sk);
886 int err;
887
888 if (addr->sa_family != AF_IUCV || alen < sizeof(struct sockaddr_iucv))
889 return -EINVAL;
890
891 if (sk->sk_state != IUCV_OPEN && sk->sk_state != IUCV_BOUND)
892 return -EBADFD;
893
894 if (sk->sk_state == IUCV_OPEN &&
895 iucv->transport == AF_IUCV_TRANS_HIPER)
896 return -EBADFD; /* explicit bind required */
897
898 if (sk->sk_type != SOCK_STREAM && sk->sk_type != SOCK_SEQPACKET)
899 return -EINVAL;
900
901 if (sk->sk_state == IUCV_OPEN) {
902 err = iucv_sock_autobind(sk);
903 if (unlikely(err))
904 return err;
Jennifer Hunteac37312007-02-08 13:51:54 -0800905 }
906
Ursula Braun3881ac42011-08-08 01:33:54 +0000907 lock_sock(sk);
908
909 /* Set the destination information */
910 memcpy(iucv->dst_user_id, sa->siucv_user_id, 8);
911 memcpy(iucv->dst_name, sa->siucv_name, 8);
912
913 if (iucv->transport == AF_IUCV_TRANS_HIPER)
914 err = afiucv_hs_connect(sock);
915 else
916 err = afiucv_path_connect(sock, addr);
917 if (err)
918 goto done;
919
920 if (sk->sk_state != IUCV_CONNECTED)
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +0000921 err = iucv_sock_wait(sk, iucv_sock_in_state(sk, IUCV_CONNECTED,
922 IUCV_DISCONN),
923 sock_sndtimeo(sk, flags & O_NONBLOCK));
Jennifer Hunteac37312007-02-08 13:51:54 -0800924
Ursula Braun3881ac42011-08-08 01:33:54 +0000925 if (sk->sk_state == IUCV_DISCONN || sk->sk_state == IUCV_CLOSED)
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000926 err = -ECONNREFUSED;
Ursula Braun18becbc2009-01-05 18:07:46 -0800927
Ursula Braun3881ac42011-08-08 01:33:54 +0000928 if (err && iucv->transport == AF_IUCV_TRANS_IUCV) {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +0000929 pr_iucv->path_sever(iucv->path, NULL);
Ursula Braun18becbc2009-01-05 18:07:46 -0800930 iucv_path_free(iucv->path);
931 iucv->path = NULL;
932 }
933
Jennifer Hunteac37312007-02-08 13:51:54 -0800934done:
935 release_sock(sk);
936 return err;
937}
938
939/* Move a socket into listening state. */
940static int iucv_sock_listen(struct socket *sock, int backlog)
941{
942 struct sock *sk = sock->sk;
943 int err;
944
945 lock_sock(sk);
946
947 err = -EINVAL;
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +0000948 if (sk->sk_state != IUCV_BOUND)
949 goto done;
950
951 if (sock->type != SOCK_STREAM && sock->type != SOCK_SEQPACKET)
Jennifer Hunteac37312007-02-08 13:51:54 -0800952 goto done;
953
954 sk->sk_max_ack_backlog = backlog;
955 sk->sk_ack_backlog = 0;
956 sk->sk_state = IUCV_LISTEN;
957 err = 0;
958
959done:
960 release_sock(sk);
961 return err;
962}
963
964/* Accept a pending connection */
965static int iucv_sock_accept(struct socket *sock, struct socket *newsock,
966 int flags)
967{
968 DECLARE_WAITQUEUE(wait, current);
969 struct sock *sk = sock->sk, *nsk;
970 long timeo;
971 int err = 0;
972
Jennifer Hunt561e0362007-05-04 12:22:07 -0700973 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
Jennifer Hunteac37312007-02-08 13:51:54 -0800974
975 if (sk->sk_state != IUCV_LISTEN) {
976 err = -EBADFD;
977 goto done;
978 }
979
980 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
981
982 /* Wait for an incoming connection */
Eric Dumazetaa395142010-04-20 13:03:51 +0000983 add_wait_queue_exclusive(sk_sleep(sk), &wait);
Heiko Carstensda99f052007-05-04 12:23:27 -0700984 while (!(nsk = iucv_accept_dequeue(sk, newsock))) {
Jennifer Hunteac37312007-02-08 13:51:54 -0800985 set_current_state(TASK_INTERRUPTIBLE);
986 if (!timeo) {
987 err = -EAGAIN;
988 break;
989 }
990
991 release_sock(sk);
992 timeo = schedule_timeout(timeo);
Jennifer Hunt561e0362007-05-04 12:22:07 -0700993 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
Jennifer Hunteac37312007-02-08 13:51:54 -0800994
995 if (sk->sk_state != IUCV_LISTEN) {
996 err = -EBADFD;
997 break;
998 }
999
1000 if (signal_pending(current)) {
1001 err = sock_intr_errno(timeo);
1002 break;
1003 }
1004 }
1005
1006 set_current_state(TASK_RUNNING);
Eric Dumazetaa395142010-04-20 13:03:51 +00001007 remove_wait_queue(sk_sleep(sk), &wait);
Jennifer Hunteac37312007-02-08 13:51:54 -08001008
1009 if (err)
1010 goto done;
1011
1012 newsock->state = SS_CONNECTED;
1013
1014done:
1015 release_sock(sk);
1016 return err;
1017}
1018
1019static int iucv_sock_getname(struct socket *sock, struct sockaddr *addr,
1020 int *len, int peer)
1021{
1022 struct sockaddr_iucv *siucv = (struct sockaddr_iucv *) addr;
1023 struct sock *sk = sock->sk;
Ursula Braun493d3972011-08-08 01:33:52 +00001024 struct iucv_sock *iucv = iucv_sk(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -08001025
1026 addr->sa_family = AF_IUCV;
1027 *len = sizeof(struct sockaddr_iucv);
1028
1029 if (peer) {
Ursula Braun493d3972011-08-08 01:33:52 +00001030 memcpy(siucv->siucv_user_id, iucv->dst_user_id, 8);
1031 memcpy(siucv->siucv_name, iucv->dst_name, 8);
Jennifer Hunteac37312007-02-08 13:51:54 -08001032 } else {
Ursula Braun493d3972011-08-08 01:33:52 +00001033 memcpy(siucv->siucv_user_id, iucv->src_user_id, 8);
1034 memcpy(siucv->siucv_name, iucv->src_name, 8);
Jennifer Hunteac37312007-02-08 13:51:54 -08001035 }
1036 memset(&siucv->siucv_port, 0, sizeof(siucv->siucv_port));
1037 memset(&siucv->siucv_addr, 0, sizeof(siucv->siucv_addr));
Ursula Braun493d3972011-08-08 01:33:52 +00001038 memset(&siucv->siucv_nodeid, 0, sizeof(siucv->siucv_nodeid));
Jennifer Hunteac37312007-02-08 13:51:54 -08001039
1040 return 0;
1041}
1042
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001043/**
1044 * iucv_send_iprm() - Send socket data in parameter list of an iucv message.
1045 * @path: IUCV path
1046 * @msg: Pointer to a struct iucv_message
1047 * @skb: The socket data to send, skb->len MUST BE <= 7
1048 *
1049 * Send the socket data in the parameter list in the iucv message
1050 * (IUCV_IPRMDATA). The socket data is stored at index 0 to 6 in the parameter
1051 * list and the socket data len at index 7 (last byte).
1052 * See also iucv_msg_length().
1053 *
1054 * Returns the error code from the iucv_message_send() call.
1055 */
1056static int iucv_send_iprm(struct iucv_path *path, struct iucv_message *msg,
1057 struct sk_buff *skb)
1058{
1059 u8 prmdata[8];
1060
1061 memcpy(prmdata, (void *) skb->data, skb->len);
1062 prmdata[7] = 0xff - (u8) skb->len;
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001063 return pr_iucv->message_send(path, msg, IUCV_IPRMDATA, 0,
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001064 (void *) prmdata, 8);
1065}
1066
Jennifer Hunteac37312007-02-08 13:51:54 -08001067static int iucv_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
1068 struct msghdr *msg, size_t len)
1069{
1070 struct sock *sk = sock->sk;
1071 struct iucv_sock *iucv = iucv_sk(sk);
1072 struct sk_buff *skb;
1073 struct iucv_message txmsg;
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001074 struct cmsghdr *cmsg;
1075 int cmsg_done;
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +00001076 long timeo;
Ursula Braun8f7c5022008-12-25 13:39:47 +01001077 char user_id[9];
1078 char appl_id[9];
Jennifer Hunteac37312007-02-08 13:51:54 -08001079 int err;
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +00001080 int noblock = msg->msg_flags & MSG_DONTWAIT;
Jennifer Hunteac37312007-02-08 13:51:54 -08001081
1082 err = sock_error(sk);
1083 if (err)
1084 return err;
1085
1086 if (msg->msg_flags & MSG_OOB)
1087 return -EOPNOTSUPP;
1088
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001089 /* SOCK_SEQPACKET: we do not support segmented records */
1090 if (sk->sk_type == SOCK_SEQPACKET && !(msg->msg_flags & MSG_EOR))
1091 return -EOPNOTSUPP;
1092
Jennifer Hunteac37312007-02-08 13:51:54 -08001093 lock_sock(sk);
1094
1095 if (sk->sk_shutdown & SEND_SHUTDOWN) {
1096 err = -EPIPE;
1097 goto out;
1098 }
1099
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001100 /* Return if the socket is not in connected state */
1101 if (sk->sk_state != IUCV_CONNECTED) {
1102 err = -ENOTCONN;
1103 goto out;
1104 }
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001105
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001106 /* initialize defaults */
1107 cmsg_done = 0; /* check for duplicate headers */
1108 txmsg.class = 0;
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001109
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001110 /* iterate over control messages */
1111 for (cmsg = CMSG_FIRSTHDR(msg); cmsg;
1112 cmsg = CMSG_NXTHDR(msg, cmsg)) {
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001113
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001114 if (!CMSG_OK(msg, cmsg)) {
1115 err = -EINVAL;
Jennifer Hunt561e0362007-05-04 12:22:07 -07001116 goto out;
Jennifer Hunteac37312007-02-08 13:51:54 -08001117 }
1118
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001119 if (cmsg->cmsg_level != SOL_IUCV)
1120 continue;
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001121
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001122 if (cmsg->cmsg_type & cmsg_done) {
1123 err = -EINVAL;
1124 goto out;
1125 }
1126 cmsg_done |= cmsg->cmsg_type;
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001127
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001128 switch (cmsg->cmsg_type) {
1129 case SCM_IUCV_TRGCLS:
1130 if (cmsg->cmsg_len != CMSG_LEN(TRGCLS_SIZE)) {
1131 err = -EINVAL;
1132 goto out;
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001133 }
1134
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001135 /* set iucv message target class */
1136 memcpy(&txmsg.class,
1137 (void *) CMSG_DATA(cmsg), TRGCLS_SIZE);
1138
1139 break;
1140
1141 default:
1142 err = -EINVAL;
1143 goto out;
1144 break;
1145 }
1146 }
1147
1148 /* allocate one skb for each iucv message:
1149 * this is fine for SOCK_SEQPACKET (unless we want to support
1150 * segmented records using the MSG_EOR flag), but
1151 * for SOCK_STREAM we might want to improve it in future */
Ursula Braun3881ac42011-08-08 01:33:54 +00001152 if (iucv->transport == AF_IUCV_TRANS_HIPER)
1153 skb = sock_alloc_send_skb(sk,
1154 len + sizeof(struct af_iucv_trans_hdr) + ETH_HLEN,
1155 noblock, &err);
1156 else
1157 skb = sock_alloc_send_skb(sk, len, noblock, &err);
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001158 if (!skb)
1159 goto out;
Ursula Braun3881ac42011-08-08 01:33:54 +00001160 if (iucv->transport == AF_IUCV_TRANS_HIPER)
1161 skb_reserve(skb, sizeof(struct af_iucv_trans_hdr) + ETH_HLEN);
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001162 if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
1163 err = -EFAULT;
1164 goto fail;
1165 }
1166
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +00001167 /* wait if outstanding messages for iucv path has reached */
1168 timeo = sock_sndtimeo(sk, noblock);
1169 err = iucv_sock_wait(sk, iucv_below_msglim(sk), timeo);
1170 if (err)
1171 goto fail;
1172
1173 /* return -ECONNRESET if the socket is no longer connected */
1174 if (sk->sk_state != IUCV_CONNECTED) {
1175 err = -ECONNRESET;
1176 goto fail;
1177 }
1178
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001179 /* increment and save iucv message tag for msg_completion cbk */
1180 txmsg.tag = iucv->send_tag++;
1181 memcpy(CB_TAG(skb), &txmsg.tag, CB_TAG_LEN);
Ursula Braun3881ac42011-08-08 01:33:54 +00001182 if (iucv->transport == AF_IUCV_TRANS_HIPER) {
1183 atomic_inc(&iucv->msg_sent);
1184 err = afiucv_hs_send(&txmsg, sk, skb, 0);
1185 if (err) {
1186 atomic_dec(&iucv->msg_sent);
1187 goto fail;
1188 }
1189 goto release;
1190 }
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001191 skb_queue_tail(&iucv->send_skb_q, skb);
1192
1193 if (((iucv->path->flags & IUCV_IPRMDATA) & iucv->flags)
1194 && skb->len <= 7) {
1195 err = iucv_send_iprm(iucv->path, &txmsg, skb);
1196
1197 /* on success: there is no message_complete callback
1198 * for an IPRMDATA msg; remove skb from send queue */
1199 if (err == 0) {
1200 skb_unlink(skb, &iucv->send_skb_q);
1201 kfree_skb(skb);
1202 }
1203
1204 /* this error should never happen since the
1205 * IUCV_IPRMDATA path flag is set... sever path */
1206 if (err == 0x15) {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001207 pr_iucv->path_sever(iucv->path, NULL);
Jennifer Hunteac37312007-02-08 13:51:54 -08001208 skb_unlink(skb, &iucv->send_skb_q);
1209 err = -EPIPE;
1210 goto fail;
1211 }
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001212 } else
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001213 err = pr_iucv->message_send(iucv->path, &txmsg, 0, 0,
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001214 (void *) skb->data, skb->len);
1215 if (err) {
1216 if (err == 3) {
1217 user_id[8] = 0;
1218 memcpy(user_id, iucv->dst_user_id, 8);
1219 appl_id[8] = 0;
1220 memcpy(appl_id, iucv->dst_name, 8);
1221 pr_err("Application %s on z/VM guest %s"
1222 " exceeds message limit\n",
1223 appl_id, user_id);
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +00001224 err = -EAGAIN;
1225 } else
1226 err = -EPIPE;
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001227 skb_unlink(skb, &iucv->send_skb_q);
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001228 goto fail;
Jennifer Hunteac37312007-02-08 13:51:54 -08001229 }
1230
Ursula Braun3881ac42011-08-08 01:33:54 +00001231release:
Jennifer Hunteac37312007-02-08 13:51:54 -08001232 release_sock(sk);
1233 return len;
1234
1235fail:
Ursula Braun816abba2011-12-19 22:56:29 +00001236 if (skb->dev)
1237 dev_put(skb->dev);
Jennifer Hunteac37312007-02-08 13:51:54 -08001238 kfree_skb(skb);
1239out:
1240 release_sock(sk);
1241 return err;
1242}
1243
Hendrik Bruecknerbf95d202009-09-16 04:37:28 +00001244/* iucv_fragment_skb() - Fragment a single IUCV message into multiple skb's
1245 *
1246 * Locking: must be called with message_q.lock held
1247 */
Ursula Braunf0703c82007-10-08 02:03:31 -07001248static int iucv_fragment_skb(struct sock *sk, struct sk_buff *skb, int len)
1249{
1250 int dataleft, size, copied = 0;
1251 struct sk_buff *nskb;
1252
1253 dataleft = len;
1254 while (dataleft) {
1255 if (dataleft >= sk->sk_rcvbuf / 4)
1256 size = sk->sk_rcvbuf / 4;
1257 else
1258 size = dataleft;
1259
1260 nskb = alloc_skb(size, GFP_ATOMIC | GFP_DMA);
1261 if (!nskb)
1262 return -ENOMEM;
1263
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001264 /* copy target class to control buffer of new skb */
1265 memcpy(CB_TRGCLS(nskb), CB_TRGCLS(skb), CB_TRGCLS_LEN);
1266
1267 /* copy data fragment */
Ursula Braunf0703c82007-10-08 02:03:31 -07001268 memcpy(nskb->data, skb->data + copied, size);
1269 copied += size;
1270 dataleft -= size;
1271
1272 skb_reset_transport_header(nskb);
1273 skb_reset_network_header(nskb);
1274 nskb->len = size;
1275
1276 skb_queue_tail(&iucv_sk(sk)->backlog_skb_q, nskb);
1277 }
1278
1279 return 0;
1280}
1281
Hendrik Bruecknerbf95d202009-09-16 04:37:28 +00001282/* iucv_process_message() - Receive a single outstanding IUCV message
1283 *
1284 * Locking: must be called with message_q.lock held
1285 */
Ursula Braunf0703c82007-10-08 02:03:31 -07001286static void iucv_process_message(struct sock *sk, struct sk_buff *skb,
1287 struct iucv_path *path,
1288 struct iucv_message *msg)
1289{
1290 int rc;
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001291 unsigned int len;
Ursula Braunf0703c82007-10-08 02:03:31 -07001292
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001293 len = iucv_msg_length(msg);
1294
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001295 /* store msg target class in the second 4 bytes of skb ctrl buffer */
1296 /* Note: the first 4 bytes are reserved for msg tag */
1297 memcpy(CB_TRGCLS(skb), &msg->class, CB_TRGCLS_LEN);
1298
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001299 /* check for special IPRM messages (e.g. iucv_sock_shutdown) */
1300 if ((msg->flags & IUCV_IPRMDATA) && len > 7) {
1301 if (memcmp(msg->rmmsg, iprm_shutdown, 8) == 0) {
1302 skb->data = NULL;
1303 skb->len = 0;
1304 }
Ursula Braunf0703c82007-10-08 02:03:31 -07001305 } else {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001306 rc = pr_iucv->message_receive(path, msg,
1307 msg->flags & IUCV_IPRMDATA,
1308 skb->data, len, NULL);
Ursula Braunf0703c82007-10-08 02:03:31 -07001309 if (rc) {
1310 kfree_skb(skb);
1311 return;
1312 }
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001313 /* we need to fragment iucv messages for SOCK_STREAM only;
1314 * for SOCK_SEQPACKET, it is only relevant if we support
1315 * record segmentation using MSG_EOR (see also recvmsg()) */
1316 if (sk->sk_type == SOCK_STREAM &&
1317 skb->truesize >= sk->sk_rcvbuf / 4) {
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001318 rc = iucv_fragment_skb(sk, skb, len);
Ursula Braunf0703c82007-10-08 02:03:31 -07001319 kfree_skb(skb);
1320 skb = NULL;
1321 if (rc) {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001322 pr_iucv->path_sever(path, NULL);
Ursula Braunf0703c82007-10-08 02:03:31 -07001323 return;
1324 }
1325 skb = skb_dequeue(&iucv_sk(sk)->backlog_skb_q);
1326 } else {
1327 skb_reset_transport_header(skb);
1328 skb_reset_network_header(skb);
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001329 skb->len = len;
Ursula Braunf0703c82007-10-08 02:03:31 -07001330 }
1331 }
1332
1333 if (sock_queue_rcv_skb(sk, skb))
1334 skb_queue_head(&iucv_sk(sk)->backlog_skb_q, skb);
1335}
1336
Hendrik Bruecknerbf95d202009-09-16 04:37:28 +00001337/* iucv_process_message_q() - Process outstanding IUCV messages
1338 *
1339 * Locking: must be called with message_q.lock held
1340 */
Ursula Braunf0703c82007-10-08 02:03:31 -07001341static void iucv_process_message_q(struct sock *sk)
1342{
1343 struct iucv_sock *iucv = iucv_sk(sk);
1344 struct sk_buff *skb;
1345 struct sock_msg_q *p, *n;
1346
1347 list_for_each_entry_safe(p, n, &iucv->message_q.list, list) {
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001348 skb = alloc_skb(iucv_msg_length(&p->msg), GFP_ATOMIC | GFP_DMA);
Ursula Braunf0703c82007-10-08 02:03:31 -07001349 if (!skb)
1350 break;
1351 iucv_process_message(sk, skb, p->path, &p->msg);
1352 list_del(&p->list);
1353 kfree(p);
1354 if (!skb_queue_empty(&iucv->backlog_skb_q))
1355 break;
1356 }
1357}
1358
Jennifer Hunteac37312007-02-08 13:51:54 -08001359static int iucv_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
1360 struct msghdr *msg, size_t len, int flags)
1361{
1362 int noblock = flags & MSG_DONTWAIT;
1363 struct sock *sk = sock->sk;
Jennifer Hunt561e0362007-05-04 12:22:07 -07001364 struct iucv_sock *iucv = iucv_sk(sk);
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001365 unsigned int copied, rlen;
Ursula Braun3881ac42011-08-08 01:33:54 +00001366 struct sk_buff *skb, *rskb, *cskb, *sskb;
1367 int blen;
Jennifer Hunteac37312007-02-08 13:51:54 -08001368 int err = 0;
1369
Jennifer Hunt561e0362007-05-04 12:22:07 -07001370 if ((sk->sk_state == IUCV_DISCONN || sk->sk_state == IUCV_SEVERED) &&
Ursula Braunf0703c82007-10-08 02:03:31 -07001371 skb_queue_empty(&iucv->backlog_skb_q) &&
1372 skb_queue_empty(&sk->sk_receive_queue) &&
1373 list_empty(&iucv->message_q.list))
Jennifer Hunt561e0362007-05-04 12:22:07 -07001374 return 0;
1375
Jennifer Hunteac37312007-02-08 13:51:54 -08001376 if (flags & (MSG_OOB))
1377 return -EOPNOTSUPP;
1378
Hendrik Brueckner60d37052009-04-21 06:04:21 +00001379 /* receive/dequeue next skb:
1380 * the function understands MSG_PEEK and, thus, does not dequeue skb */
Jennifer Hunteac37312007-02-08 13:51:54 -08001381 skb = skb_recv_datagram(sk, flags, noblock, &err);
1382 if (!skb) {
1383 if (sk->sk_shutdown & RCV_SHUTDOWN)
1384 return 0;
1385 return err;
1386 }
1387
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001388 rlen = skb->len; /* real length of skb */
1389 copied = min_t(unsigned int, rlen, len);
Jennifer Hunteac37312007-02-08 13:51:54 -08001390
Jennifer Hunt561e0362007-05-04 12:22:07 -07001391 cskb = skb;
Ursula Braun3881ac42011-08-08 01:33:54 +00001392 if (skb_copy_datagram_iovec(cskb, 0, msg->msg_iov, copied)) {
Hendrik Brueckner802788b2009-04-21 23:26:26 +00001393 if (!(flags & MSG_PEEK))
1394 skb_queue_head(&sk->sk_receive_queue, skb);
1395 return -EFAULT;
Jennifer Hunteac37312007-02-08 13:51:54 -08001396 }
1397
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001398 /* SOCK_SEQPACKET: set MSG_TRUNC if recv buf size is too small */
1399 if (sk->sk_type == SOCK_SEQPACKET) {
1400 if (copied < rlen)
1401 msg->msg_flags |= MSG_TRUNC;
1402 /* each iucv message contains a complete record */
1403 msg->msg_flags |= MSG_EOR;
1404 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001405
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001406 /* create control message to store iucv msg target class:
1407 * get the trgcls from the control buffer of the skb due to
1408 * fragmentation of original iucv message. */
1409 err = put_cmsg(msg, SOL_IUCV, SCM_IUCV_TRGCLS,
1410 CB_TRGCLS_LEN, CB_TRGCLS(skb));
1411 if (err) {
1412 if (!(flags & MSG_PEEK))
1413 skb_queue_head(&sk->sk_receive_queue, skb);
1414 return err;
1415 }
1416
Jennifer Hunteac37312007-02-08 13:51:54 -08001417 /* Mark read part of skb as used */
1418 if (!(flags & MSG_PEEK)) {
Jennifer Hunteac37312007-02-08 13:51:54 -08001419
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001420 /* SOCK_STREAM: re-queue skb if it contains unreceived data */
1421 if (sk->sk_type == SOCK_STREAM) {
1422 skb_pull(skb, copied);
1423 if (skb->len) {
1424 skb_queue_head(&sk->sk_receive_queue, skb);
1425 goto done;
1426 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001427 }
1428
1429 kfree_skb(skb);
Ursula Braun3881ac42011-08-08 01:33:54 +00001430 atomic_inc(&iucv->msg_recv);
Jennifer Hunt561e0362007-05-04 12:22:07 -07001431
1432 /* Queue backlog skbs */
Hendrik Bruecknerbf95d202009-09-16 04:37:28 +00001433 spin_lock_bh(&iucv->message_q.lock);
Ursula Braunf0703c82007-10-08 02:03:31 -07001434 rskb = skb_dequeue(&iucv->backlog_skb_q);
Heiko Carstensda99f052007-05-04 12:23:27 -07001435 while (rskb) {
Jennifer Hunt561e0362007-05-04 12:22:07 -07001436 if (sock_queue_rcv_skb(sk, rskb)) {
Ursula Braunf0703c82007-10-08 02:03:31 -07001437 skb_queue_head(&iucv->backlog_skb_q,
Jennifer Hunt561e0362007-05-04 12:22:07 -07001438 rskb);
1439 break;
1440 } else {
Ursula Braunf0703c82007-10-08 02:03:31 -07001441 rskb = skb_dequeue(&iucv->backlog_skb_q);
Jennifer Hunt561e0362007-05-04 12:22:07 -07001442 }
1443 }
Ursula Braunf0703c82007-10-08 02:03:31 -07001444 if (skb_queue_empty(&iucv->backlog_skb_q)) {
Ursula Braunf0703c82007-10-08 02:03:31 -07001445 if (!list_empty(&iucv->message_q.list))
1446 iucv_process_message_q(sk);
Ursula Braun3881ac42011-08-08 01:33:54 +00001447 if (atomic_read(&iucv->msg_recv) >=
1448 iucv->msglimit / 2) {
1449 /* send WIN to peer */
1450 blen = sizeof(struct af_iucv_trans_hdr) +
1451 ETH_HLEN;
1452 sskb = sock_alloc_send_skb(sk, blen, 1, &err);
1453 if (sskb) {
Ursula Braun816abba2011-12-19 22:56:29 +00001454 skb_reserve(sskb, blen);
Ursula Braun3881ac42011-08-08 01:33:54 +00001455 err = afiucv_hs_send(NULL, sk, sskb,
1456 AF_IUCV_FLAG_WIN);
1457 }
1458 if (err) {
1459 sk->sk_state = IUCV_DISCONN;
1460 sk->sk_state_change(sk);
1461 }
1462 }
Ursula Braunf0703c82007-10-08 02:03:31 -07001463 }
Hendrik Bruecknerbf95d202009-09-16 04:37:28 +00001464 spin_unlock_bh(&iucv->message_q.lock);
Hendrik Brueckner60d37052009-04-21 06:04:21 +00001465 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001466
1467done:
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001468 /* SOCK_SEQPACKET: return real length if MSG_TRUNC is set */
1469 if (sk->sk_type == SOCK_SEQPACKET && (flags & MSG_TRUNC))
1470 copied = rlen;
1471
1472 return copied;
Jennifer Hunteac37312007-02-08 13:51:54 -08001473}
1474
1475static inline unsigned int iucv_accept_poll(struct sock *parent)
1476{
1477 struct iucv_sock *isk, *n;
1478 struct sock *sk;
1479
Heiko Carstensda99f052007-05-04 12:23:27 -07001480 list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) {
Jennifer Hunteac37312007-02-08 13:51:54 -08001481 sk = (struct sock *) isk;
1482
1483 if (sk->sk_state == IUCV_CONNECTED)
1484 return POLLIN | POLLRDNORM;
1485 }
1486
1487 return 0;
1488}
1489
1490unsigned int iucv_sock_poll(struct file *file, struct socket *sock,
1491 poll_table *wait)
1492{
1493 struct sock *sk = sock->sk;
1494 unsigned int mask = 0;
1495
Eric Dumazetaa395142010-04-20 13:03:51 +00001496 sock_poll_wait(file, sk_sleep(sk), wait);
Jennifer Hunteac37312007-02-08 13:51:54 -08001497
1498 if (sk->sk_state == IUCV_LISTEN)
1499 return iucv_accept_poll(sk);
1500
1501 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
1502 mask |= POLLERR;
1503
1504 if (sk->sk_shutdown & RCV_SHUTDOWN)
1505 mask |= POLLRDHUP;
1506
1507 if (sk->sk_shutdown == SHUTDOWN_MASK)
1508 mask |= POLLHUP;
1509
1510 if (!skb_queue_empty(&sk->sk_receive_queue) ||
Heiko Carstensda99f052007-05-04 12:23:27 -07001511 (sk->sk_shutdown & RCV_SHUTDOWN))
Jennifer Hunteac37312007-02-08 13:51:54 -08001512 mask |= POLLIN | POLLRDNORM;
1513
1514 if (sk->sk_state == IUCV_CLOSED)
1515 mask |= POLLHUP;
1516
Jennifer Hunt561e0362007-05-04 12:22:07 -07001517 if (sk->sk_state == IUCV_DISCONN || sk->sk_state == IUCV_SEVERED)
1518 mask |= POLLIN;
1519
Jennifer Hunteac37312007-02-08 13:51:54 -08001520 if (sock_writeable(sk))
1521 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
1522 else
1523 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
1524
1525 return mask;
1526}
1527
1528static int iucv_sock_shutdown(struct socket *sock, int how)
1529{
1530 struct sock *sk = sock->sk;
1531 struct iucv_sock *iucv = iucv_sk(sk);
1532 struct iucv_message txmsg;
1533 int err = 0;
Jennifer Hunteac37312007-02-08 13:51:54 -08001534
1535 how++;
1536
1537 if ((how & ~SHUTDOWN_MASK) || !how)
1538 return -EINVAL;
1539
1540 lock_sock(sk);
Heiko Carstensda99f052007-05-04 12:23:27 -07001541 switch (sk->sk_state) {
Hendrik Bruecknere14ad5f2009-04-21 06:04:23 +00001542 case IUCV_DISCONN:
1543 case IUCV_CLOSING:
1544 case IUCV_SEVERED:
Jennifer Hunteac37312007-02-08 13:51:54 -08001545 case IUCV_CLOSED:
1546 err = -ENOTCONN;
1547 goto fail;
1548
1549 default:
1550 sk->sk_shutdown |= how;
1551 break;
1552 }
1553
1554 if (how == SEND_SHUTDOWN || how == SHUTDOWN_MASK) {
1555 txmsg.class = 0;
1556 txmsg.tag = 0;
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001557 err = pr_iucv->message_send(iucv->path, &txmsg, IUCV_IPRMDATA,
1558 0, (void *) iprm_shutdown, 8);
Jennifer Hunteac37312007-02-08 13:51:54 -08001559 if (err) {
Heiko Carstensda99f052007-05-04 12:23:27 -07001560 switch (err) {
Jennifer Hunteac37312007-02-08 13:51:54 -08001561 case 1:
1562 err = -ENOTCONN;
1563 break;
1564 case 2:
1565 err = -ECONNRESET;
1566 break;
1567 default:
1568 err = -ENOTCONN;
1569 break;
1570 }
1571 }
1572 }
1573
1574 if (how == RCV_SHUTDOWN || how == SHUTDOWN_MASK) {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001575 err = pr_iucv->path_quiesce(iucv->path, NULL);
Jennifer Hunteac37312007-02-08 13:51:54 -08001576 if (err)
1577 err = -ENOTCONN;
1578
1579 skb_queue_purge(&sk->sk_receive_queue);
1580 }
1581
1582 /* Wake up anyone sleeping in poll */
1583 sk->sk_state_change(sk);
1584
1585fail:
1586 release_sock(sk);
1587 return err;
1588}
1589
1590static int iucv_sock_release(struct socket *sock)
1591{
1592 struct sock *sk = sock->sk;
1593 int err = 0;
1594
1595 if (!sk)
1596 return 0;
1597
1598 iucv_sock_close(sk);
1599
1600 /* Unregister with IUCV base support */
1601 if (iucv_sk(sk)->path) {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001602 pr_iucv->path_sever(iucv_sk(sk)->path, NULL);
Jennifer Hunteac37312007-02-08 13:51:54 -08001603 iucv_path_free(iucv_sk(sk)->path);
1604 iucv_sk(sk)->path = NULL;
1605 }
1606
Jennifer Hunteac37312007-02-08 13:51:54 -08001607 sock_orphan(sk);
1608 iucv_sock_kill(sk);
1609 return err;
1610}
1611
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00001612/* getsockopt and setsockopt */
1613static int iucv_sock_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07001614 char __user *optval, unsigned int optlen)
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00001615{
1616 struct sock *sk = sock->sk;
1617 struct iucv_sock *iucv = iucv_sk(sk);
1618 int val;
1619 int rc;
1620
1621 if (level != SOL_IUCV)
1622 return -ENOPROTOOPT;
1623
1624 if (optlen < sizeof(int))
1625 return -EINVAL;
1626
1627 if (get_user(val, (int __user *) optval))
1628 return -EFAULT;
1629
1630 rc = 0;
1631
1632 lock_sock(sk);
1633 switch (optname) {
1634 case SO_IPRMDATA_MSG:
1635 if (val)
1636 iucv->flags |= IUCV_IPRMDATA;
1637 else
1638 iucv->flags &= ~IUCV_IPRMDATA;
1639 break;
Hendrik Brueckner09488e2e2009-04-21 23:26:27 +00001640 case SO_MSGLIMIT:
1641 switch (sk->sk_state) {
1642 case IUCV_OPEN:
1643 case IUCV_BOUND:
1644 if (val < 1 || val > (u16)(~0))
1645 rc = -EINVAL;
1646 else
1647 iucv->msglimit = val;
1648 break;
1649 default:
1650 rc = -EINVAL;
1651 break;
1652 }
1653 break;
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00001654 default:
1655 rc = -ENOPROTOOPT;
1656 break;
1657 }
1658 release_sock(sk);
1659
1660 return rc;
1661}
1662
1663static int iucv_sock_getsockopt(struct socket *sock, int level, int optname,
1664 char __user *optval, int __user *optlen)
1665{
1666 struct sock *sk = sock->sk;
1667 struct iucv_sock *iucv = iucv_sk(sk);
1668 int val, len;
1669
1670 if (level != SOL_IUCV)
1671 return -ENOPROTOOPT;
1672
1673 if (get_user(len, optlen))
1674 return -EFAULT;
1675
1676 if (len < 0)
1677 return -EINVAL;
1678
1679 len = min_t(unsigned int, len, sizeof(int));
1680
1681 switch (optname) {
1682 case SO_IPRMDATA_MSG:
1683 val = (iucv->flags & IUCV_IPRMDATA) ? 1 : 0;
1684 break;
Hendrik Brueckner09488e2e2009-04-21 23:26:27 +00001685 case SO_MSGLIMIT:
1686 lock_sock(sk);
1687 val = (iucv->path != NULL) ? iucv->path->msglim /* connected */
1688 : iucv->msglimit; /* default */
1689 release_sock(sk);
1690 break;
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00001691 default:
1692 return -ENOPROTOOPT;
1693 }
1694
1695 if (put_user(len, optlen))
1696 return -EFAULT;
1697 if (copy_to_user(optval, &val, len))
1698 return -EFAULT;
1699
1700 return 0;
1701}
1702
1703
Jennifer Hunteac37312007-02-08 13:51:54 -08001704/* Callback wrappers - called from iucv base support */
1705static int iucv_callback_connreq(struct iucv_path *path,
1706 u8 ipvmid[8], u8 ipuser[16])
1707{
1708 unsigned char user_data[16];
1709 unsigned char nuser_data[16];
1710 unsigned char src_name[8];
1711 struct hlist_node *node;
1712 struct sock *sk, *nsk;
1713 struct iucv_sock *iucv, *niucv;
1714 int err;
1715
1716 memcpy(src_name, ipuser, 8);
1717 EBCASC(src_name, 8);
1718 /* Find out if this path belongs to af_iucv. */
1719 read_lock(&iucv_sk_list.lock);
1720 iucv = NULL;
Ursula Braunfebca282007-07-14 19:04:25 -07001721 sk = NULL;
Jennifer Hunteac37312007-02-08 13:51:54 -08001722 sk_for_each(sk, node, &iucv_sk_list.head)
1723 if (sk->sk_state == IUCV_LISTEN &&
1724 !memcmp(&iucv_sk(sk)->src_name, src_name, 8)) {
1725 /*
1726 * Found a listening socket with
1727 * src_name == ipuser[0-7].
1728 */
1729 iucv = iucv_sk(sk);
1730 break;
1731 }
1732 read_unlock(&iucv_sk_list.lock);
1733 if (!iucv)
1734 /* No socket found, not one of our paths. */
1735 return -EINVAL;
1736
1737 bh_lock_sock(sk);
1738
1739 /* Check if parent socket is listening */
1740 low_nmcpy(user_data, iucv->src_name);
1741 high_nmcpy(user_data, iucv->dst_name);
1742 ASCEBC(user_data, sizeof(user_data));
1743 if (sk->sk_state != IUCV_LISTEN) {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001744 err = pr_iucv->path_sever(path, user_data);
Hendrik Brueckner65dbd7c2009-01-05 18:08:23 -08001745 iucv_path_free(path);
Jennifer Hunteac37312007-02-08 13:51:54 -08001746 goto fail;
1747 }
1748
1749 /* Check for backlog size */
1750 if (sk_acceptq_is_full(sk)) {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001751 err = pr_iucv->path_sever(path, user_data);
Hendrik Brueckner65dbd7c2009-01-05 18:08:23 -08001752 iucv_path_free(path);
Jennifer Hunteac37312007-02-08 13:51:54 -08001753 goto fail;
1754 }
1755
1756 /* Create the new socket */
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001757 nsk = iucv_sock_alloc(NULL, sk->sk_type, GFP_ATOMIC);
Heiko Carstensda99f052007-05-04 12:23:27 -07001758 if (!nsk) {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001759 err = pr_iucv->path_sever(path, user_data);
Hendrik Brueckner65dbd7c2009-01-05 18:08:23 -08001760 iucv_path_free(path);
Jennifer Hunteac37312007-02-08 13:51:54 -08001761 goto fail;
1762 }
1763
1764 niucv = iucv_sk(nsk);
1765 iucv_sock_init(nsk, sk);
1766
1767 /* Set the new iucv_sock */
1768 memcpy(niucv->dst_name, ipuser + 8, 8);
1769 EBCASC(niucv->dst_name, 8);
1770 memcpy(niucv->dst_user_id, ipvmid, 8);
1771 memcpy(niucv->src_name, iucv->src_name, 8);
1772 memcpy(niucv->src_user_id, iucv->src_user_id, 8);
1773 niucv->path = path;
1774
1775 /* Call iucv_accept */
1776 high_nmcpy(nuser_data, ipuser + 8);
1777 memcpy(nuser_data + 8, niucv->src_name, 8);
1778 ASCEBC(nuser_data + 8, 8);
1779
Hendrik Brueckner09488e2e2009-04-21 23:26:27 +00001780 /* set message limit for path based on msglimit of accepting socket */
1781 niucv->msglimit = iucv->msglimit;
1782 path->msglim = iucv->msglimit;
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001783 err = pr_iucv->path_accept(path, &af_iucv_handler, nuser_data, nsk);
Heiko Carstensda99f052007-05-04 12:23:27 -07001784 if (err) {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001785 err = pr_iucv->path_sever(path, user_data);
Hendrik Brueckner65dbd7c2009-01-05 18:08:23 -08001786 iucv_path_free(path);
1787 iucv_sock_kill(nsk);
Jennifer Hunteac37312007-02-08 13:51:54 -08001788 goto fail;
1789 }
1790
1791 iucv_accept_enqueue(sk, nsk);
1792
1793 /* Wake up accept */
1794 nsk->sk_state = IUCV_CONNECTED;
1795 sk->sk_data_ready(sk, 1);
1796 err = 0;
1797fail:
1798 bh_unlock_sock(sk);
1799 return 0;
1800}
1801
1802static void iucv_callback_connack(struct iucv_path *path, u8 ipuser[16])
1803{
1804 struct sock *sk = path->private;
1805
1806 sk->sk_state = IUCV_CONNECTED;
1807 sk->sk_state_change(sk);
1808}
1809
1810static void iucv_callback_rx(struct iucv_path *path, struct iucv_message *msg)
1811{
1812 struct sock *sk = path->private;
Jennifer Hunt561e0362007-05-04 12:22:07 -07001813 struct iucv_sock *iucv = iucv_sk(sk);
Ursula Braunf0703c82007-10-08 02:03:31 -07001814 struct sk_buff *skb;
1815 struct sock_msg_q *save_msg;
1816 int len;
Jennifer Hunt561e0362007-05-04 12:22:07 -07001817
Hendrik Bruecknerfe86e542009-04-21 06:04:22 +00001818 if (sk->sk_shutdown & RCV_SHUTDOWN) {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001819 pr_iucv->message_reject(path, msg);
Jennifer Hunteac37312007-02-08 13:51:54 -08001820 return;
Hendrik Bruecknerfe86e542009-04-21 06:04:22 +00001821 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001822
Hendrik Brueckner3fa6b5a2009-04-21 06:04:24 +00001823 spin_lock(&iucv->message_q.lock);
Jennifer Hunteac37312007-02-08 13:51:54 -08001824
Ursula Braunf0703c82007-10-08 02:03:31 -07001825 if (!list_empty(&iucv->message_q.list) ||
1826 !skb_queue_empty(&iucv->backlog_skb_q))
1827 goto save_message;
1828
1829 len = atomic_read(&sk->sk_rmem_alloc);
Eric Dumazet87fb4b72011-10-13 07:28:54 +00001830 len += SKB_TRUESIZE(iucv_msg_length(msg));
Ursula Braunf0703c82007-10-08 02:03:31 -07001831 if (len > sk->sk_rcvbuf)
1832 goto save_message;
1833
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001834 skb = alloc_skb(iucv_msg_length(msg), GFP_ATOMIC | GFP_DMA);
Ursula Braunf0703c82007-10-08 02:03:31 -07001835 if (!skb)
1836 goto save_message;
Jennifer Hunteac37312007-02-08 13:51:54 -08001837
Ursula Braunf0703c82007-10-08 02:03:31 -07001838 iucv_process_message(sk, skb, path, msg);
Hendrik Brueckner3fa6b5a2009-04-21 06:04:24 +00001839 goto out_unlock;
Jennifer Hunteac37312007-02-08 13:51:54 -08001840
Ursula Braunf0703c82007-10-08 02:03:31 -07001841save_message:
1842 save_msg = kzalloc(sizeof(struct sock_msg_q), GFP_ATOMIC | GFP_DMA);
Ursula Braund4444722008-02-07 18:07:19 -08001843 if (!save_msg)
Julia Lawalla56635a2010-05-26 05:56:48 +00001844 goto out_unlock;
Ursula Braunf0703c82007-10-08 02:03:31 -07001845 save_msg->path = path;
1846 save_msg->msg = *msg;
1847
Ursula Braunf0703c82007-10-08 02:03:31 -07001848 list_add_tail(&save_msg->list, &iucv->message_q.list);
Hendrik Brueckner3fa6b5a2009-04-21 06:04:24 +00001849
1850out_unlock:
Ursula Braunf0703c82007-10-08 02:03:31 -07001851 spin_unlock(&iucv->message_q.lock);
Jennifer Hunteac37312007-02-08 13:51:54 -08001852}
1853
1854static void iucv_callback_txdone(struct iucv_path *path,
1855 struct iucv_message *msg)
1856{
1857 struct sock *sk = path->private;
Ursula Braunf2a77992008-02-07 18:07:44 -08001858 struct sk_buff *this = NULL;
Jennifer Hunteac37312007-02-08 13:51:54 -08001859 struct sk_buff_head *list = &iucv_sk(sk)->send_skb_q;
1860 struct sk_buff *list_skb = list->next;
1861 unsigned long flags;
1862
Ursula Braunf2a77992008-02-07 18:07:44 -08001863 if (!skb_queue_empty(list)) {
Jennifer Hunt561e0362007-05-04 12:22:07 -07001864 spin_lock_irqsave(&list->lock, flags);
Jennifer Hunteac37312007-02-08 13:51:54 -08001865
Ursula Braunf2a77992008-02-07 18:07:44 -08001866 while (list_skb != (struct sk_buff *)list) {
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001867 if (!memcmp(&msg->tag, CB_TAG(list_skb), CB_TAG_LEN)) {
Ursula Braunf2a77992008-02-07 18:07:44 -08001868 this = list_skb;
1869 break;
1870 }
Jennifer Hunt561e0362007-05-04 12:22:07 -07001871 list_skb = list_skb->next;
Ursula Braunf2a77992008-02-07 18:07:44 -08001872 }
1873 if (this)
1874 __skb_unlink(this, list);
Jennifer Hunteac37312007-02-08 13:51:54 -08001875
Jennifer Hunt561e0362007-05-04 12:22:07 -07001876 spin_unlock_irqrestore(&list->lock, flags);
Jennifer Hunteac37312007-02-08 13:51:54 -08001877
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +00001878 if (this) {
1879 kfree_skb(this);
1880 /* wake up any process waiting for sending */
1881 iucv_sock_wake_msglim(sk);
1882 }
Jennifer Hunt561e0362007-05-04 12:22:07 -07001883 }
Ursula Braunc2b4afd2008-07-14 09:59:29 +02001884 BUG_ON(!this);
Jennifer Hunt561e0362007-05-04 12:22:07 -07001885
Heiko Carstensda99f052007-05-04 12:23:27 -07001886 if (sk->sk_state == IUCV_CLOSING) {
Jennifer Hunt561e0362007-05-04 12:22:07 -07001887 if (skb_queue_empty(&iucv_sk(sk)->send_skb_q)) {
1888 sk->sk_state = IUCV_CLOSED;
1889 sk->sk_state_change(sk);
1890 }
1891 }
1892
Jennifer Hunteac37312007-02-08 13:51:54 -08001893}
1894
1895static void iucv_callback_connrej(struct iucv_path *path, u8 ipuser[16])
1896{
1897 struct sock *sk = path->private;
1898
1899 if (!list_empty(&iucv_sk(sk)->accept_q))
1900 sk->sk_state = IUCV_SEVERED;
1901 else
1902 sk->sk_state = IUCV_DISCONN;
1903
1904 sk->sk_state_change(sk);
1905}
1906
Hendrik Brueckneraf88b522009-04-21 23:26:21 +00001907/* called if the other communication side shuts down its RECV direction;
1908 * in turn, the callback sets SEND_SHUTDOWN to disable sending of data.
1909 */
1910static void iucv_callback_shutdown(struct iucv_path *path, u8 ipuser[16])
1911{
1912 struct sock *sk = path->private;
1913
1914 bh_lock_sock(sk);
1915 if (sk->sk_state != IUCV_CLOSED) {
1916 sk->sk_shutdown |= SEND_SHUTDOWN;
1917 sk->sk_state_change(sk);
1918 }
1919 bh_unlock_sock(sk);
1920}
1921
Ursula Braun3881ac42011-08-08 01:33:54 +00001922/***************** HiperSockets transport callbacks ********************/
1923static void afiucv_swap_src_dest(struct sk_buff *skb)
1924{
1925 struct af_iucv_trans_hdr *trans_hdr =
1926 (struct af_iucv_trans_hdr *)skb->data;
1927 char tmpID[8];
1928 char tmpName[8];
1929
1930 ASCEBC(trans_hdr->destUserID, sizeof(trans_hdr->destUserID));
1931 ASCEBC(trans_hdr->destAppName, sizeof(trans_hdr->destAppName));
1932 ASCEBC(trans_hdr->srcUserID, sizeof(trans_hdr->srcUserID));
1933 ASCEBC(trans_hdr->srcAppName, sizeof(trans_hdr->srcAppName));
1934 memcpy(tmpID, trans_hdr->srcUserID, 8);
1935 memcpy(tmpName, trans_hdr->srcAppName, 8);
1936 memcpy(trans_hdr->srcUserID, trans_hdr->destUserID, 8);
1937 memcpy(trans_hdr->srcAppName, trans_hdr->destAppName, 8);
1938 memcpy(trans_hdr->destUserID, tmpID, 8);
1939 memcpy(trans_hdr->destAppName, tmpName, 8);
1940 skb_push(skb, ETH_HLEN);
1941 memset(skb->data, 0, ETH_HLEN);
1942}
1943
1944/**
1945 * afiucv_hs_callback_syn - react on received SYN
1946 **/
1947static int afiucv_hs_callback_syn(struct sock *sk, struct sk_buff *skb)
1948{
1949 struct sock *nsk;
1950 struct iucv_sock *iucv, *niucv;
1951 struct af_iucv_trans_hdr *trans_hdr;
1952 int err;
1953
1954 iucv = iucv_sk(sk);
1955 trans_hdr = (struct af_iucv_trans_hdr *)skb->data;
1956 if (!iucv) {
1957 /* no sock - connection refused */
1958 afiucv_swap_src_dest(skb);
1959 trans_hdr->flags = AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN;
1960 err = dev_queue_xmit(skb);
1961 goto out;
1962 }
1963
1964 nsk = iucv_sock_alloc(NULL, sk->sk_type, GFP_ATOMIC);
1965 bh_lock_sock(sk);
1966 if ((sk->sk_state != IUCV_LISTEN) ||
1967 sk_acceptq_is_full(sk) ||
1968 !nsk) {
1969 /* error on server socket - connection refused */
1970 if (nsk)
1971 sk_free(nsk);
1972 afiucv_swap_src_dest(skb);
1973 trans_hdr->flags = AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN;
1974 err = dev_queue_xmit(skb);
1975 bh_unlock_sock(sk);
1976 goto out;
1977 }
1978
1979 niucv = iucv_sk(nsk);
1980 iucv_sock_init(nsk, sk);
1981 niucv->transport = AF_IUCV_TRANS_HIPER;
1982 niucv->msglimit = iucv->msglimit;
1983 if (!trans_hdr->window)
1984 niucv->msglimit_peer = IUCV_HIPER_MSGLIM_DEFAULT;
1985 else
1986 niucv->msglimit_peer = trans_hdr->window;
1987 memcpy(niucv->dst_name, trans_hdr->srcAppName, 8);
1988 memcpy(niucv->dst_user_id, trans_hdr->srcUserID, 8);
1989 memcpy(niucv->src_name, iucv->src_name, 8);
1990 memcpy(niucv->src_user_id, iucv->src_user_id, 8);
1991 nsk->sk_bound_dev_if = sk->sk_bound_dev_if;
1992 afiucv_swap_src_dest(skb);
1993 trans_hdr->flags = AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_ACK;
1994 trans_hdr->window = niucv->msglimit;
1995 /* if receiver acks the xmit connection is established */
1996 err = dev_queue_xmit(skb);
1997 if (!err) {
1998 iucv_accept_enqueue(sk, nsk);
1999 nsk->sk_state = IUCV_CONNECTED;
2000 sk->sk_data_ready(sk, 1);
2001 } else
2002 iucv_sock_kill(nsk);
2003 bh_unlock_sock(sk);
2004
2005out:
2006 return NET_RX_SUCCESS;
2007}
2008
2009/**
2010 * afiucv_hs_callback_synack() - react on received SYN-ACK
2011 **/
2012static int afiucv_hs_callback_synack(struct sock *sk, struct sk_buff *skb)
2013{
2014 struct iucv_sock *iucv = iucv_sk(sk);
2015 struct af_iucv_trans_hdr *trans_hdr =
2016 (struct af_iucv_trans_hdr *)skb->data;
2017
2018 if (!iucv)
2019 goto out;
2020 if (sk->sk_state != IUCV_BOUND)
2021 goto out;
2022 bh_lock_sock(sk);
2023 iucv->msglimit_peer = trans_hdr->window;
2024 sk->sk_state = IUCV_CONNECTED;
2025 sk->sk_state_change(sk);
2026 bh_unlock_sock(sk);
2027out:
2028 kfree_skb(skb);
2029 return NET_RX_SUCCESS;
2030}
2031
2032/**
2033 * afiucv_hs_callback_synfin() - react on received SYN_FIN
2034 **/
2035static int afiucv_hs_callback_synfin(struct sock *sk, struct sk_buff *skb)
2036{
2037 struct iucv_sock *iucv = iucv_sk(sk);
2038
2039 if (!iucv)
2040 goto out;
2041 if (sk->sk_state != IUCV_BOUND)
2042 goto out;
2043 bh_lock_sock(sk);
2044 sk->sk_state = IUCV_DISCONN;
2045 sk->sk_state_change(sk);
2046 bh_unlock_sock(sk);
2047out:
2048 kfree_skb(skb);
2049 return NET_RX_SUCCESS;
2050}
2051
2052/**
2053 * afiucv_hs_callback_fin() - react on received FIN
2054 **/
2055static int afiucv_hs_callback_fin(struct sock *sk, struct sk_buff *skb)
2056{
2057 struct iucv_sock *iucv = iucv_sk(sk);
2058
2059 /* other end of connection closed */
2060 if (iucv) {
2061 bh_lock_sock(sk);
2062 if (!list_empty(&iucv->accept_q))
2063 sk->sk_state = IUCV_SEVERED;
2064 else
2065 sk->sk_state = IUCV_DISCONN;
2066 sk->sk_state_change(sk);
2067 bh_unlock_sock(sk);
2068 }
2069 kfree_skb(skb);
2070 return NET_RX_SUCCESS;
2071}
2072
2073/**
2074 * afiucv_hs_callback_win() - react on received WIN
2075 **/
2076static int afiucv_hs_callback_win(struct sock *sk, struct sk_buff *skb)
2077{
2078 struct iucv_sock *iucv = iucv_sk(sk);
2079 struct af_iucv_trans_hdr *trans_hdr =
2080 (struct af_iucv_trans_hdr *)skb->data;
2081
2082 if (!iucv)
2083 return NET_RX_SUCCESS;
2084
2085 if (sk->sk_state != IUCV_CONNECTED)
2086 return NET_RX_SUCCESS;
2087
2088 atomic_sub(trans_hdr->window, &iucv->msg_sent);
2089 iucv_sock_wake_msglim(sk);
2090 return NET_RX_SUCCESS;
2091}
2092
2093/**
2094 * afiucv_hs_callback_rx() - react on received data
2095 **/
2096static int afiucv_hs_callback_rx(struct sock *sk, struct sk_buff *skb)
2097{
2098 struct iucv_sock *iucv = iucv_sk(sk);
2099
2100 if (!iucv) {
2101 kfree_skb(skb);
2102 return NET_RX_SUCCESS;
2103 }
2104
2105 if (sk->sk_state != IUCV_CONNECTED) {
2106 kfree_skb(skb);
2107 return NET_RX_SUCCESS;
2108 }
2109
2110 /* write stuff from iucv_msg to skb cb */
2111 if (skb->len <= sizeof(struct af_iucv_trans_hdr)) {
2112 kfree_skb(skb);
2113 return NET_RX_SUCCESS;
2114 }
2115 skb_pull(skb, sizeof(struct af_iucv_trans_hdr));
2116 skb_reset_transport_header(skb);
2117 skb_reset_network_header(skb);
2118 spin_lock(&iucv->message_q.lock);
2119 if (skb_queue_empty(&iucv->backlog_skb_q)) {
2120 if (sock_queue_rcv_skb(sk, skb)) {
2121 /* handle rcv queue full */
2122 skb_queue_tail(&iucv->backlog_skb_q, skb);
2123 }
2124 } else
2125 skb_queue_tail(&iucv_sk(sk)->backlog_skb_q, skb);
2126 spin_unlock(&iucv->message_q.lock);
2127 return NET_RX_SUCCESS;
2128}
2129
2130/**
2131 * afiucv_hs_rcv() - base function for arriving data through HiperSockets
2132 * transport
2133 * called from netif RX softirq
2134 **/
2135static int afiucv_hs_rcv(struct sk_buff *skb, struct net_device *dev,
2136 struct packet_type *pt, struct net_device *orig_dev)
2137{
2138 struct hlist_node *node;
2139 struct sock *sk;
2140 struct iucv_sock *iucv;
2141 struct af_iucv_trans_hdr *trans_hdr;
2142 char nullstring[8];
2143 int err = 0;
2144
2145 skb_pull(skb, ETH_HLEN);
2146 trans_hdr = (struct af_iucv_trans_hdr *)skb->data;
2147 EBCASC(trans_hdr->destAppName, sizeof(trans_hdr->destAppName));
2148 EBCASC(trans_hdr->destUserID, sizeof(trans_hdr->destUserID));
2149 EBCASC(trans_hdr->srcAppName, sizeof(trans_hdr->srcAppName));
2150 EBCASC(trans_hdr->srcUserID, sizeof(trans_hdr->srcUserID));
2151 memset(nullstring, 0, sizeof(nullstring));
2152 iucv = NULL;
2153 sk = NULL;
2154 read_lock(&iucv_sk_list.lock);
2155 sk_for_each(sk, node, &iucv_sk_list.head) {
2156 if (trans_hdr->flags == AF_IUCV_FLAG_SYN) {
2157 if ((!memcmp(&iucv_sk(sk)->src_name,
2158 trans_hdr->destAppName, 8)) &&
2159 (!memcmp(&iucv_sk(sk)->src_user_id,
2160 trans_hdr->destUserID, 8)) &&
2161 (!memcmp(&iucv_sk(sk)->dst_name, nullstring, 8)) &&
2162 (!memcmp(&iucv_sk(sk)->dst_user_id,
2163 nullstring, 8))) {
2164 iucv = iucv_sk(sk);
2165 break;
2166 }
2167 } else {
2168 if ((!memcmp(&iucv_sk(sk)->src_name,
2169 trans_hdr->destAppName, 8)) &&
2170 (!memcmp(&iucv_sk(sk)->src_user_id,
2171 trans_hdr->destUserID, 8)) &&
2172 (!memcmp(&iucv_sk(sk)->dst_name,
2173 trans_hdr->srcAppName, 8)) &&
2174 (!memcmp(&iucv_sk(sk)->dst_user_id,
2175 trans_hdr->srcUserID, 8))) {
2176 iucv = iucv_sk(sk);
2177 break;
2178 }
2179 }
2180 }
2181 read_unlock(&iucv_sk_list.lock);
2182 if (!iucv)
2183 sk = NULL;
2184
2185 /* no sock
2186 how should we send with no sock
2187 1) send without sock no send rc checking?
2188 2) introduce default sock to handle this cases
2189
2190 SYN -> send SYN|ACK in good case, send SYN|FIN in bad case
2191 data -> send FIN
2192 SYN|ACK, SYN|FIN, FIN -> no action? */
2193
2194 switch (trans_hdr->flags) {
2195 case AF_IUCV_FLAG_SYN:
2196 /* connect request */
2197 err = afiucv_hs_callback_syn(sk, skb);
2198 break;
2199 case (AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_ACK):
2200 /* connect request confirmed */
2201 err = afiucv_hs_callback_synack(sk, skb);
2202 break;
2203 case (AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN):
2204 /* connect request refused */
2205 err = afiucv_hs_callback_synfin(sk, skb);
2206 break;
2207 case (AF_IUCV_FLAG_FIN):
2208 /* close request */
2209 err = afiucv_hs_callback_fin(sk, skb);
2210 break;
2211 case (AF_IUCV_FLAG_WIN):
2212 err = afiucv_hs_callback_win(sk, skb);
2213 if (skb->len > sizeof(struct af_iucv_trans_hdr))
2214 err = afiucv_hs_callback_rx(sk, skb);
2215 else
2216 kfree(skb);
2217 break;
2218 case 0:
2219 /* plain data frame */
Ursula Braunc64d3f82011-12-19 22:56:27 +00002220 memcpy(CB_TRGCLS(skb), &trans_hdr->iucv_hdr.class,
2221 CB_TRGCLS_LEN);
Ursula Braun3881ac42011-08-08 01:33:54 +00002222 err = afiucv_hs_callback_rx(sk, skb);
2223 break;
2224 default:
2225 ;
2226 }
2227
2228 return err;
2229}
2230
2231/**
2232 * afiucv_hs_callback_txnotify() - handle send notifcations from HiperSockets
2233 * transport
2234 **/
2235static void afiucv_hs_callback_txnotify(struct sk_buff *skb,
2236 enum iucv_tx_notify n)
2237{
2238 struct sock *isk = skb->sk;
2239 struct sock *sk = NULL;
2240 struct iucv_sock *iucv = NULL;
2241 struct sk_buff_head *list;
2242 struct sk_buff *list_skb;
2243 struct sk_buff *this = NULL;
2244 unsigned long flags;
2245 struct hlist_node *node;
2246
2247 read_lock(&iucv_sk_list.lock);
2248 sk_for_each(sk, node, &iucv_sk_list.head)
2249 if (sk == isk) {
2250 iucv = iucv_sk(sk);
2251 break;
2252 }
2253 read_unlock(&iucv_sk_list.lock);
2254
2255 if (!iucv)
2256 return;
2257
2258 bh_lock_sock(sk);
2259 list = &iucv->send_skb_q;
2260 list_skb = list->next;
2261 if (skb_queue_empty(list))
2262 goto out_unlock;
2263
2264 spin_lock_irqsave(&list->lock, flags);
2265 while (list_skb != (struct sk_buff *)list) {
2266 if (skb_shinfo(list_skb) == skb_shinfo(skb)) {
2267 this = list_skb;
2268 switch (n) {
2269 case TX_NOTIFY_OK:
2270 __skb_unlink(this, list);
2271 iucv_sock_wake_msglim(sk);
Ursula Braun816abba2011-12-19 22:56:29 +00002272 dev_put(this->dev);
Ursula Braun3881ac42011-08-08 01:33:54 +00002273 kfree_skb(this);
2274 break;
2275 case TX_NOTIFY_PENDING:
2276 atomic_inc(&iucv->pendings);
2277 break;
2278 case TX_NOTIFY_DELAYED_OK:
2279 __skb_unlink(this, list);
2280 atomic_dec(&iucv->pendings);
2281 if (atomic_read(&iucv->pendings) <= 0)
2282 iucv_sock_wake_msglim(sk);
Ursula Braun816abba2011-12-19 22:56:29 +00002283 dev_put(this->dev);
Ursula Braun3881ac42011-08-08 01:33:54 +00002284 kfree_skb(this);
2285 break;
2286 case TX_NOTIFY_UNREACHABLE:
2287 case TX_NOTIFY_DELAYED_UNREACHABLE:
2288 case TX_NOTIFY_TPQFULL: /* not yet used */
2289 case TX_NOTIFY_GENERALERROR:
2290 case TX_NOTIFY_DELAYED_GENERALERROR:
2291 __skb_unlink(this, list);
Ursula Braun816abba2011-12-19 22:56:29 +00002292 dev_put(this->dev);
Ursula Braun3881ac42011-08-08 01:33:54 +00002293 kfree_skb(this);
2294 if (!list_empty(&iucv->accept_q))
2295 sk->sk_state = IUCV_SEVERED;
2296 else
2297 sk->sk_state = IUCV_DISCONN;
2298 sk->sk_state_change(sk);
2299 break;
2300 }
2301 break;
2302 }
2303 list_skb = list_skb->next;
2304 }
2305 spin_unlock_irqrestore(&list->lock, flags);
2306
Ursula Braun42bd48e2011-12-19 22:56:28 +00002307 if (sk->sk_state == IUCV_CLOSING) {
2308 if (skb_queue_empty(&iucv_sk(sk)->send_skb_q)) {
2309 sk->sk_state = IUCV_CLOSED;
2310 sk->sk_state_change(sk);
2311 }
2312 }
2313
Ursula Braun3881ac42011-08-08 01:33:54 +00002314out_unlock:
2315 bh_unlock_sock(sk);
2316}
Alexey Dobriyan5708e862009-09-14 12:23:23 +00002317static const struct proto_ops iucv_sock_ops = {
Jennifer Hunteac37312007-02-08 13:51:54 -08002318 .family = PF_IUCV,
2319 .owner = THIS_MODULE,
2320 .release = iucv_sock_release,
2321 .bind = iucv_sock_bind,
2322 .connect = iucv_sock_connect,
2323 .listen = iucv_sock_listen,
2324 .accept = iucv_sock_accept,
2325 .getname = iucv_sock_getname,
2326 .sendmsg = iucv_sock_sendmsg,
2327 .recvmsg = iucv_sock_recvmsg,
2328 .poll = iucv_sock_poll,
2329 .ioctl = sock_no_ioctl,
2330 .mmap = sock_no_mmap,
2331 .socketpair = sock_no_socketpair,
2332 .shutdown = iucv_sock_shutdown,
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00002333 .setsockopt = iucv_sock_setsockopt,
2334 .getsockopt = iucv_sock_getsockopt,
Jennifer Hunteac37312007-02-08 13:51:54 -08002335};
2336
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00002337static const struct net_proto_family iucv_sock_family_ops = {
Jennifer Hunteac37312007-02-08 13:51:54 -08002338 .family = AF_IUCV,
2339 .owner = THIS_MODULE,
2340 .create = iucv_sock_create,
2341};
2342
Ursula Braun3881ac42011-08-08 01:33:54 +00002343static struct packet_type iucv_packet_type = {
2344 .type = cpu_to_be16(ETH_P_AF_IUCV),
2345 .func = afiucv_hs_rcv,
2346};
2347
2348static int afiucv_iucv_init(void)
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00002349{
2350 int err;
2351
2352 err = pr_iucv->iucv_register(&af_iucv_handler, 0);
2353 if (err)
2354 goto out;
2355 /* establish dummy device */
2356 af_iucv_driver.bus = pr_iucv->bus;
2357 err = driver_register(&af_iucv_driver);
2358 if (err)
2359 goto out_iucv;
2360 af_iucv_dev = kzalloc(sizeof(struct device), GFP_KERNEL);
2361 if (!af_iucv_dev) {
2362 err = -ENOMEM;
2363 goto out_driver;
2364 }
2365 dev_set_name(af_iucv_dev, "af_iucv");
2366 af_iucv_dev->bus = pr_iucv->bus;
2367 af_iucv_dev->parent = pr_iucv->root;
2368 af_iucv_dev->release = (void (*)(struct device *))kfree;
2369 af_iucv_dev->driver = &af_iucv_driver;
2370 err = device_register(af_iucv_dev);
2371 if (err)
2372 goto out_driver;
2373 return 0;
2374
2375out_driver:
2376 driver_unregister(&af_iucv_driver);
2377out_iucv:
2378 pr_iucv->iucv_unregister(&af_iucv_handler, 0);
2379out:
2380 return err;
2381}
2382
Heiko Carstensda99f052007-05-04 12:23:27 -07002383static int __init afiucv_init(void)
Jennifer Hunteac37312007-02-08 13:51:54 -08002384{
2385 int err;
2386
Ursula Braun3881ac42011-08-08 01:33:54 +00002387 if (MACHINE_IS_VM) {
2388 cpcmd("QUERY USERID", iucv_userid, sizeof(iucv_userid), &err);
2389 if (unlikely(err)) {
2390 WARN_ON(err);
2391 err = -EPROTONOSUPPORT;
2392 goto out;
2393 }
Jennifer Hunteac37312007-02-08 13:51:54 -08002394
Ursula Braun3881ac42011-08-08 01:33:54 +00002395 pr_iucv = try_then_request_module(symbol_get(iucv_if), "iucv");
2396 if (!pr_iucv) {
2397 printk(KERN_WARNING "iucv_if lookup failed\n");
2398 memset(&iucv_userid, 0, sizeof(iucv_userid));
2399 }
2400 } else {
2401 memset(&iucv_userid, 0, sizeof(iucv_userid));
2402 pr_iucv = NULL;
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00002403 }
2404
Jennifer Hunteac37312007-02-08 13:51:54 -08002405 err = proto_register(&iucv_proto, 0);
2406 if (err)
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00002407 goto out;
Jennifer Hunteac37312007-02-08 13:51:54 -08002408 err = sock_register(&iucv_sock_family_ops);
2409 if (err)
2410 goto out_proto;
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00002411
Ursula Braun3881ac42011-08-08 01:33:54 +00002412 if (pr_iucv) {
2413 err = afiucv_iucv_init();
2414 if (err)
2415 goto out_sock;
2416 }
2417 dev_add_pack(&iucv_packet_type);
Jennifer Hunteac37312007-02-08 13:51:54 -08002418 return 0;
2419
Ursula Braunc23cad92009-06-16 10:30:44 +02002420out_sock:
2421 sock_unregister(PF_IUCV);
Jennifer Hunteac37312007-02-08 13:51:54 -08002422out_proto:
2423 proto_unregister(&iucv_proto);
Jennifer Hunteac37312007-02-08 13:51:54 -08002424out:
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00002425 if (pr_iucv)
2426 symbol_put(iucv_if);
Jennifer Hunteac37312007-02-08 13:51:54 -08002427 return err;
2428}
2429
2430static void __exit afiucv_exit(void)
2431{
Ursula Braun3881ac42011-08-08 01:33:54 +00002432 if (pr_iucv) {
2433 device_unregister(af_iucv_dev);
2434 driver_unregister(&af_iucv_driver);
2435 pr_iucv->iucv_unregister(&af_iucv_handler, 0);
2436 symbol_put(iucv_if);
2437 }
2438 dev_remove_pack(&iucv_packet_type);
Jennifer Hunteac37312007-02-08 13:51:54 -08002439 sock_unregister(PF_IUCV);
2440 proto_unregister(&iucv_proto);
Jennifer Hunteac37312007-02-08 13:51:54 -08002441}
2442
2443module_init(afiucv_init);
2444module_exit(afiucv_exit);
2445
2446MODULE_AUTHOR("Jennifer Hunt <jenhunt@us.ibm.com>");
2447MODULE_DESCRIPTION("IUCV Sockets ver " VERSION);
2448MODULE_VERSION(VERSION);
2449MODULE_LICENSE("GPL");
2450MODULE_ALIAS_NETPROTO(PF_IUCV);
Ursula Braun3881ac42011-08-08 01:33:54 +00002451