blob: 60f5c20d510ad4880e619adddf619b53fb93c918 [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
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +000052#define __iucv_sock_wait(sk, condition, timeo, ret) \
53do { \
54 DEFINE_WAIT(__wait); \
55 long __timeo = timeo; \
56 ret = 0; \
Eric Dumazetaa395142010-04-20 13:03:51 +000057 prepare_to_wait(sk_sleep(sk), &__wait, TASK_INTERRUPTIBLE); \
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +000058 while (!(condition)) { \
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +000059 if (!__timeo) { \
60 ret = -EAGAIN; \
61 break; \
62 } \
63 if (signal_pending(current)) { \
64 ret = sock_intr_errno(__timeo); \
65 break; \
66 } \
67 release_sock(sk); \
68 __timeo = schedule_timeout(__timeo); \
69 lock_sock(sk); \
70 ret = sock_error(sk); \
71 if (ret) \
72 break; \
73 } \
Eric Dumazetaa395142010-04-20 13:03:51 +000074 finish_wait(sk_sleep(sk), &__wait); \
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +000075} while (0)
76
77#define iucv_sock_wait(sk, condition, timeo) \
78({ \
79 int __ret = 0; \
80 if (!(condition)) \
81 __iucv_sock_wait(sk, condition, timeo, __ret); \
82 __ret; \
83})
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +000084
Heiko Carstens57f20442007-10-08 02:02:52 -070085static void iucv_sock_kill(struct sock *sk);
86static void iucv_sock_close(struct sock *sk);
Ursula Braun7d316b92012-02-08 00:19:45 +000087static void iucv_sever_path(struct sock *, int);
Heiko Carstens57f20442007-10-08 02:02:52 -070088
Ursula Braun3881ac42011-08-08 01:33:54 +000089static int afiucv_hs_rcv(struct sk_buff *skb, struct net_device *dev,
90 struct packet_type *pt, struct net_device *orig_dev);
91static int afiucv_hs_send(struct iucv_message *imsg, struct sock *sock,
92 struct sk_buff *skb, u8 flags);
93static void afiucv_hs_callback_txnotify(struct sk_buff *, enum iucv_tx_notify);
94
Jennifer Hunteac37312007-02-08 13:51:54 -080095/* Call Back functions */
96static void iucv_callback_rx(struct iucv_path *, struct iucv_message *);
97static void iucv_callback_txdone(struct iucv_path *, struct iucv_message *);
98static void iucv_callback_connack(struct iucv_path *, u8 ipuser[16]);
Heiko Carstensda99f052007-05-04 12:23:27 -070099static int iucv_callback_connreq(struct iucv_path *, u8 ipvmid[8],
100 u8 ipuser[16]);
Jennifer Hunteac37312007-02-08 13:51:54 -0800101static void iucv_callback_connrej(struct iucv_path *, u8 ipuser[16]);
Hendrik Brueckneraf88b522009-04-21 23:26:21 +0000102static void iucv_callback_shutdown(struct iucv_path *, u8 ipuser[16]);
Jennifer Hunteac37312007-02-08 13:51:54 -0800103
104static struct iucv_sock_list iucv_sk_list = {
Robert P. J. Day3db8ce32008-04-10 02:11:24 -0700105 .lock = __RW_LOCK_UNLOCKED(iucv_sk_list.lock),
Jennifer Hunteac37312007-02-08 13:51:54 -0800106 .autobind_name = ATOMIC_INIT(0)
107};
108
109static struct iucv_handler af_iucv_handler = {
110 .path_pending = iucv_callback_connreq,
111 .path_complete = iucv_callback_connack,
112 .path_severed = iucv_callback_connrej,
113 .message_pending = iucv_callback_rx,
Hendrik Brueckneraf88b522009-04-21 23:26:21 +0000114 .message_complete = iucv_callback_txdone,
115 .path_quiesced = iucv_callback_shutdown,
Jennifer Hunteac37312007-02-08 13:51:54 -0800116};
117
118static inline void high_nmcpy(unsigned char *dst, char *src)
119{
120 memcpy(dst, src, 8);
121}
122
123static inline void low_nmcpy(unsigned char *dst, char *src)
124{
125 memcpy(&dst[8], src, 8);
126}
127
Ursula Braunc23cad92009-06-16 10:30:44 +0200128static int afiucv_pm_prepare(struct device *dev)
129{
130#ifdef CONFIG_PM_DEBUG
131 printk(KERN_WARNING "afiucv_pm_prepare\n");
132#endif
133 return 0;
134}
135
136static void afiucv_pm_complete(struct device *dev)
137{
138#ifdef CONFIG_PM_DEBUG
139 printk(KERN_WARNING "afiucv_pm_complete\n");
140#endif
Ursula Braunc23cad92009-06-16 10:30:44 +0200141}
142
143/**
144 * afiucv_pm_freeze() - Freeze PM callback
145 * @dev: AFIUCV dummy device
146 *
147 * Sever all established IUCV communication pathes
148 */
149static int afiucv_pm_freeze(struct device *dev)
150{
151 struct iucv_sock *iucv;
152 struct sock *sk;
Ursula Braunc23cad92009-06-16 10:30:44 +0200153 int err = 0;
154
155#ifdef CONFIG_PM_DEBUG
156 printk(KERN_WARNING "afiucv_pm_freeze\n");
157#endif
158 read_lock(&iucv_sk_list.lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800159 sk_for_each(sk, &iucv_sk_list.head) {
Ursula Braunc23cad92009-06-16 10:30:44 +0200160 iucv = iucv_sk(sk);
Ursula Braunc23cad92009-06-16 10:30:44 +0200161 switch (sk->sk_state) {
Ursula Braunc23cad92009-06-16 10:30:44 +0200162 case IUCV_DISCONN:
163 case IUCV_CLOSING:
164 case IUCV_CONNECTED:
Ursula Braun7d316b92012-02-08 00:19:45 +0000165 iucv_sever_path(sk, 0);
Ursula Braunc23cad92009-06-16 10:30:44 +0200166 break;
167 case IUCV_OPEN:
168 case IUCV_BOUND:
169 case IUCV_LISTEN:
170 case IUCV_CLOSED:
171 default:
172 break;
173 }
Ursula Braun7d316b92012-02-08 00:19:45 +0000174 skb_queue_purge(&iucv->send_skb_q);
175 skb_queue_purge(&iucv->backlog_skb_q);
Ursula Braunc23cad92009-06-16 10:30:44 +0200176 }
177 read_unlock(&iucv_sk_list.lock);
178 return err;
179}
180
181/**
182 * afiucv_pm_restore_thaw() - Thaw and restore PM callback
183 * @dev: AFIUCV dummy device
184 *
185 * socket clean up after freeze
186 */
187static int afiucv_pm_restore_thaw(struct device *dev)
188{
Ursula Braunc23cad92009-06-16 10:30:44 +0200189 struct sock *sk;
Ursula Braunc23cad92009-06-16 10:30:44 +0200190
191#ifdef CONFIG_PM_DEBUG
192 printk(KERN_WARNING "afiucv_pm_restore_thaw\n");
193#endif
194 read_lock(&iucv_sk_list.lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800195 sk_for_each(sk, &iucv_sk_list.head) {
Ursula Braunc23cad92009-06-16 10:30:44 +0200196 switch (sk->sk_state) {
197 case IUCV_CONNECTED:
198 sk->sk_err = EPIPE;
199 sk->sk_state = IUCV_DISCONN;
200 sk->sk_state_change(sk);
201 break;
202 case IUCV_DISCONN:
Ursula Braunc23cad92009-06-16 10:30:44 +0200203 case IUCV_CLOSING:
204 case IUCV_LISTEN:
205 case IUCV_BOUND:
206 case IUCV_OPEN:
207 default:
208 break;
209 }
210 }
211 read_unlock(&iucv_sk_list.lock);
212 return 0;
213}
214
Alexey Dobriyan47145212009-12-14 18:00:08 -0800215static const struct dev_pm_ops afiucv_pm_ops = {
Ursula Braunc23cad92009-06-16 10:30:44 +0200216 .prepare = afiucv_pm_prepare,
217 .complete = afiucv_pm_complete,
218 .freeze = afiucv_pm_freeze,
219 .thaw = afiucv_pm_restore_thaw,
220 .restore = afiucv_pm_restore_thaw,
221};
222
223static struct device_driver af_iucv_driver = {
224 .owner = THIS_MODULE,
225 .name = "afiucv",
Frank Blaschka6fcd61f2011-08-08 01:33:51 +0000226 .bus = NULL,
Ursula Braunc23cad92009-06-16 10:30:44 +0200227 .pm = &afiucv_pm_ops,
228};
229
230/* dummy device used as trigger for PM functions */
231static struct device *af_iucv_dev;
232
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000233/**
234 * iucv_msg_length() - Returns the length of an iucv message.
235 * @msg: Pointer to struct iucv_message, MUST NOT be NULL
236 *
237 * The function returns the length of the specified iucv message @msg of data
238 * stored in a buffer and of data stored in the parameter list (PRMDATA).
239 *
240 * For IUCV_IPRMDATA, AF_IUCV uses the following convention to transport socket
241 * data:
242 * PRMDATA[0..6] socket data (max 7 bytes);
243 * PRMDATA[7] socket data length value (len is 0xff - PRMDATA[7])
244 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300245 * The socket data length is computed by subtracting the socket data length
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000246 * value from 0xFF.
247 * If the socket data len is greater 7, then PRMDATA can be used for special
248 * notifications (see iucv_sock_shutdown); and further,
249 * if the socket data len is > 7, the function returns 8.
250 *
251 * Use this function to allocate socket buffers to store iucv message data.
252 */
253static inline size_t iucv_msg_length(struct iucv_message *msg)
254{
255 size_t datalen;
256
257 if (msg->flags & IUCV_IPRMDATA) {
258 datalen = 0xff - msg->rmmsg[7];
259 return (datalen < 8) ? datalen : 8;
260 }
261 return msg->length;
262}
263
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +0000264/**
265 * iucv_sock_in_state() - check for specific states
266 * @sk: sock structure
267 * @state: first iucv sk state
268 * @state: second iucv sk state
269 *
270 * Returns true if the socket in either in the first or second state.
271 */
272static int iucv_sock_in_state(struct sock *sk, int state, int state2)
273{
274 return (sk->sk_state == state || sk->sk_state == state2);
275}
276
277/**
278 * iucv_below_msglim() - function to check if messages can be sent
279 * @sk: sock structure
280 *
281 * Returns true if the send queue length is lower than the message limit.
282 * Always returns true if the socket is not connected (no iucv path for
283 * checking the message limit).
284 */
285static inline int iucv_below_msglim(struct sock *sk)
286{
287 struct iucv_sock *iucv = iucv_sk(sk);
288
289 if (sk->sk_state != IUCV_CONNECTED)
290 return 1;
Ursula Braun3881ac42011-08-08 01:33:54 +0000291 if (iucv->transport == AF_IUCV_TRANS_IUCV)
292 return (skb_queue_len(&iucv->send_skb_q) < iucv->path->msglim);
293 else
294 return ((atomic_read(&iucv->msg_sent) < iucv->msglimit_peer) &&
295 (atomic_read(&iucv->pendings) <= 0));
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +0000296}
297
298/**
299 * iucv_sock_wake_msglim() - Wake up thread waiting on msg limit
300 */
301static void iucv_sock_wake_msglim(struct sock *sk)
302{
Eric Dumazet43815482010-04-29 11:01:49 +0000303 struct socket_wq *wq;
304
305 rcu_read_lock();
306 wq = rcu_dereference(sk->sk_wq);
307 if (wq_has_sleeper(wq))
308 wake_up_interruptible_all(&wq->wait);
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +0000309 sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
Eric Dumazet43815482010-04-29 11:01:49 +0000310 rcu_read_unlock();
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +0000311}
312
Ursula Braun3881ac42011-08-08 01:33:54 +0000313/**
314 * afiucv_hs_send() - send a message through HiperSockets transport
315 */
316static int afiucv_hs_send(struct iucv_message *imsg, struct sock *sock,
317 struct sk_buff *skb, u8 flags)
318{
Ursula Braun3881ac42011-08-08 01:33:54 +0000319 struct iucv_sock *iucv = iucv_sk(sock);
320 struct af_iucv_trans_hdr *phs_hdr;
321 struct sk_buff *nskb;
322 int err, confirm_recv = 0;
323
324 memset(skb->head, 0, ETH_HLEN);
325 phs_hdr = (struct af_iucv_trans_hdr *)skb_push(skb,
326 sizeof(struct af_iucv_trans_hdr));
327 skb_reset_mac_header(skb);
328 skb_reset_network_header(skb);
329 skb_push(skb, ETH_HLEN);
330 skb_reset_mac_header(skb);
331 memset(phs_hdr, 0, sizeof(struct af_iucv_trans_hdr));
332
333 phs_hdr->magic = ETH_P_AF_IUCV;
334 phs_hdr->version = 1;
335 phs_hdr->flags = flags;
336 if (flags == AF_IUCV_FLAG_SYN)
337 phs_hdr->window = iucv->msglimit;
338 else if ((flags == AF_IUCV_FLAG_WIN) || !flags) {
339 confirm_recv = atomic_read(&iucv->msg_recv);
340 phs_hdr->window = confirm_recv;
341 if (confirm_recv)
342 phs_hdr->flags = phs_hdr->flags | AF_IUCV_FLAG_WIN;
343 }
344 memcpy(phs_hdr->destUserID, iucv->dst_user_id, 8);
345 memcpy(phs_hdr->destAppName, iucv->dst_name, 8);
346 memcpy(phs_hdr->srcUserID, iucv->src_user_id, 8);
347 memcpy(phs_hdr->srcAppName, iucv->src_name, 8);
348 ASCEBC(phs_hdr->destUserID, sizeof(phs_hdr->destUserID));
349 ASCEBC(phs_hdr->destAppName, sizeof(phs_hdr->destAppName));
350 ASCEBC(phs_hdr->srcUserID, sizeof(phs_hdr->srcUserID));
351 ASCEBC(phs_hdr->srcAppName, sizeof(phs_hdr->srcAppName));
352 if (imsg)
353 memcpy(&phs_hdr->iucv_hdr, imsg, sizeof(struct iucv_message));
354
Ursula Braun800c5eb2012-02-08 00:19:47 +0000355 skb->dev = iucv->hs_dev;
Ursula Braun3881ac42011-08-08 01:33:54 +0000356 if (!skb->dev)
357 return -ENODEV;
Ursula Braun800c5eb2012-02-08 00:19:47 +0000358 if (!(skb->dev->flags & IFF_UP) || !netif_carrier_ok(skb->dev))
Ursula Braun3881ac42011-08-08 01:33:54 +0000359 return -ENETDOWN;
360 if (skb->len > skb->dev->mtu) {
361 if (sock->sk_type == SOCK_SEQPACKET)
362 return -EMSGSIZE;
363 else
364 skb_trim(skb, skb->dev->mtu);
365 }
366 skb->protocol = ETH_P_AF_IUCV;
Ursula Braun3881ac42011-08-08 01:33:54 +0000367 nskb = skb_clone(skb, GFP_ATOMIC);
368 if (!nskb)
369 return -ENOMEM;
370 skb_queue_tail(&iucv->send_skb_q, nskb);
371 err = dev_queue_xmit(skb);
Ursula Braun800c5eb2012-02-08 00:19:47 +0000372 if (net_xmit_eval(err)) {
Ursula Braun3881ac42011-08-08 01:33:54 +0000373 skb_unlink(nskb, &iucv->send_skb_q);
374 kfree_skb(nskb);
375 } else {
376 atomic_sub(confirm_recv, &iucv->msg_recv);
377 WARN_ON(atomic_read(&iucv->msg_recv) < 0);
378 }
Ursula Braun800c5eb2012-02-08 00:19:47 +0000379 return net_xmit_eval(err);
Ursula Braun3881ac42011-08-08 01:33:54 +0000380}
381
Jennifer Hunteac37312007-02-08 13:51:54 -0800382static struct sock *__iucv_get_sock_by_name(char *nm)
383{
384 struct sock *sk;
Jennifer Hunteac37312007-02-08 13:51:54 -0800385
Sasha Levinb67bfe02013-02-27 17:06:00 -0800386 sk_for_each(sk, &iucv_sk_list.head)
Jennifer Hunteac37312007-02-08 13:51:54 -0800387 if (!memcmp(&iucv_sk(sk)->src_name, nm, 8))
388 return sk;
389
390 return NULL;
391}
392
393static void iucv_sock_destruct(struct sock *sk)
394{
395 skb_queue_purge(&sk->sk_receive_queue);
Ursula Braun82492a32012-03-07 02:06:24 +0000396 skb_queue_purge(&sk->sk_error_queue);
397
398 sk_mem_reclaim(sk);
399
400 if (!sock_flag(sk, SOCK_DEAD)) {
401 pr_err("Attempt to release alive iucv socket %p\n", sk);
402 return;
403 }
404
405 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
406 WARN_ON(atomic_read(&sk->sk_wmem_alloc));
407 WARN_ON(sk->sk_wmem_queued);
408 WARN_ON(sk->sk_forward_alloc);
Jennifer Hunteac37312007-02-08 13:51:54 -0800409}
410
411/* Cleanup Listen */
412static void iucv_sock_cleanup_listen(struct sock *parent)
413{
414 struct sock *sk;
415
416 /* Close non-accepted connections */
417 while ((sk = iucv_accept_dequeue(parent, NULL))) {
418 iucv_sock_close(sk);
419 iucv_sock_kill(sk);
420 }
421
422 parent->sk_state = IUCV_CLOSED;
Jennifer Hunteac37312007-02-08 13:51:54 -0800423}
424
Hendrik Brueckner7514bab2009-09-16 04:37:27 +0000425/* Kill socket (only if zapped and orphaned) */
Jennifer Hunteac37312007-02-08 13:51:54 -0800426static void iucv_sock_kill(struct sock *sk)
427{
428 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
429 return;
430
431 iucv_sock_unlink(&iucv_sk_list, sk);
432 sock_set_flag(sk, SOCK_DEAD);
433 sock_put(sk);
434}
435
Ursula Braun7d316b92012-02-08 00:19:45 +0000436/* Terminate an IUCV path */
437static void iucv_sever_path(struct sock *sk, int with_user_data)
438{
439 unsigned char user_data[16];
440 struct iucv_sock *iucv = iucv_sk(sk);
441 struct iucv_path *path = iucv->path;
442
443 if (iucv->path) {
444 iucv->path = NULL;
445 if (with_user_data) {
446 low_nmcpy(user_data, iucv->src_name);
447 high_nmcpy(user_data, iucv->dst_name);
448 ASCEBC(user_data, sizeof(user_data));
449 pr_iucv->path_sever(path, user_data);
450 } else
451 pr_iucv->path_sever(path, NULL);
452 iucv_path_free(path);
453 }
454}
455
Ursula Braun9fbd87d2012-03-07 02:06:23 +0000456/* Send FIN through an IUCV socket for HIPER transport */
457static int iucv_send_ctrl(struct sock *sk, u8 flags)
458{
459 int err = 0;
460 int blen;
461 struct sk_buff *skb;
462
463 blen = sizeof(struct af_iucv_trans_hdr) + ETH_HLEN;
464 skb = sock_alloc_send_skb(sk, blen, 1, &err);
465 if (skb) {
466 skb_reserve(skb, blen);
467 err = afiucv_hs_send(NULL, sk, skb, flags);
468 }
469 return err;
470}
471
Jennifer Hunteac37312007-02-08 13:51:54 -0800472/* Close an IUCV socket */
473static void iucv_sock_close(struct sock *sk)
474{
Jennifer Hunteac37312007-02-08 13:51:54 -0800475 struct iucv_sock *iucv = iucv_sk(sk);
Jennifer Hunt561e0362007-05-04 12:22:07 -0700476 unsigned long timeo;
Ursula Braun800c5eb2012-02-08 00:19:47 +0000477 int err = 0;
Jennifer Hunteac37312007-02-08 13:51:54 -0800478
Jennifer Hunteac37312007-02-08 13:51:54 -0800479 lock_sock(sk);
480
Heiko Carstensda99f052007-05-04 12:23:27 -0700481 switch (sk->sk_state) {
Jennifer Hunteac37312007-02-08 13:51:54 -0800482 case IUCV_LISTEN:
483 iucv_sock_cleanup_listen(sk);
484 break;
485
486 case IUCV_CONNECTED:
Ursula Braun3881ac42011-08-08 01:33:54 +0000487 if (iucv->transport == AF_IUCV_TRANS_HIPER) {
Ursula Braun9fbd87d2012-03-07 02:06:23 +0000488 err = iucv_send_ctrl(sk, AF_IUCV_FLAG_FIN);
Ursula Braun3881ac42011-08-08 01:33:54 +0000489 sk->sk_state = IUCV_DISCONN;
490 sk->sk_state_change(sk);
491 }
Ursula Braun816abba2011-12-19 22:56:29 +0000492 case IUCV_DISCONN: /* fall through */
Jennifer Hunt561e0362007-05-04 12:22:07 -0700493 sk->sk_state = IUCV_CLOSING;
494 sk->sk_state_change(sk);
495
Ursula Braun800c5eb2012-02-08 00:19:47 +0000496 if (!err && !skb_queue_empty(&iucv->send_skb_q)) {
Jennifer Hunt561e0362007-05-04 12:22:07 -0700497 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
498 timeo = sk->sk_lingertime;
499 else
500 timeo = IUCV_DISCONN_TIMEOUT;
Ursula Braun9f6298a2011-05-12 18:45:08 +0000501 iucv_sock_wait(sk,
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +0000502 iucv_sock_in_state(sk, IUCV_CLOSED, 0),
503 timeo);
Jennifer Hunt561e0362007-05-04 12:22:07 -0700504 }
505
Ursula Braunbbe188c2009-04-21 06:04:20 +0000506 case IUCV_CLOSING: /* fall through */
Jennifer Hunt561e0362007-05-04 12:22:07 -0700507 sk->sk_state = IUCV_CLOSED;
508 sk->sk_state_change(sk);
509
Jennifer Hunteac37312007-02-08 13:51:54 -0800510 sk->sk_err = ECONNRESET;
511 sk->sk_state_change(sk);
512
Ursula Braun800c5eb2012-02-08 00:19:47 +0000513 skb_queue_purge(&iucv->send_skb_q);
Jennifer Hunt561e0362007-05-04 12:22:07 -0700514 skb_queue_purge(&iucv->backlog_skb_q);
Jennifer Hunteac37312007-02-08 13:51:54 -0800515
Ursula Braun7d316b92012-02-08 00:19:45 +0000516 default: /* fall through */
517 iucv_sever_path(sk, 1);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700518 }
Jennifer Hunteac37312007-02-08 13:51:54 -0800519
Ursula Braun800c5eb2012-02-08 00:19:47 +0000520 if (iucv->hs_dev) {
521 dev_put(iucv->hs_dev);
522 iucv->hs_dev = NULL;
523 sk->sk_bound_dev_if = 0;
524 }
525
Hendrik Brueckner7514bab2009-09-16 04:37:27 +0000526 /* mark socket for deletion by iucv_sock_kill() */
527 sock_set_flag(sk, SOCK_ZAPPED);
528
Jennifer Hunteac37312007-02-08 13:51:54 -0800529 release_sock(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800530}
531
532static void iucv_sock_init(struct sock *sk, struct sock *parent)
533{
534 if (parent)
535 sk->sk_type = parent->sk_type;
536}
537
538static struct sock *iucv_sock_alloc(struct socket *sock, int proto, gfp_t prio)
539{
540 struct sock *sk;
Ursula Braun493d3972011-08-08 01:33:52 +0000541 struct iucv_sock *iucv;
Jennifer Hunteac37312007-02-08 13:51:54 -0800542
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700543 sk = sk_alloc(&init_net, PF_IUCV, prio, &iucv_proto);
Jennifer Hunteac37312007-02-08 13:51:54 -0800544 if (!sk)
545 return NULL;
Ursula Braun493d3972011-08-08 01:33:52 +0000546 iucv = iucv_sk(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800547
548 sock_init_data(sock, sk);
Ursula Braun493d3972011-08-08 01:33:52 +0000549 INIT_LIST_HEAD(&iucv->accept_q);
550 spin_lock_init(&iucv->accept_q_lock);
551 skb_queue_head_init(&iucv->send_skb_q);
552 INIT_LIST_HEAD(&iucv->message_q.list);
553 spin_lock_init(&iucv->message_q.lock);
554 skb_queue_head_init(&iucv->backlog_skb_q);
555 iucv->send_tag = 0;
Ursula Braun3881ac42011-08-08 01:33:54 +0000556 atomic_set(&iucv->pendings, 0);
Ursula Braun493d3972011-08-08 01:33:52 +0000557 iucv->flags = 0;
Ursula Braun3881ac42011-08-08 01:33:54 +0000558 iucv->msglimit = 0;
559 atomic_set(&iucv->msg_sent, 0);
560 atomic_set(&iucv->msg_recv, 0);
Ursula Braun493d3972011-08-08 01:33:52 +0000561 iucv->path = NULL;
Ursula Braun3881ac42011-08-08 01:33:54 +0000562 iucv->sk_txnotify = afiucv_hs_callback_txnotify;
Ursula Braun493d3972011-08-08 01:33:52 +0000563 memset(&iucv->src_user_id , 0, 32);
Ursula Braun3881ac42011-08-08 01:33:54 +0000564 if (pr_iucv)
565 iucv->transport = AF_IUCV_TRANS_IUCV;
566 else
567 iucv->transport = AF_IUCV_TRANS_HIPER;
Jennifer Hunteac37312007-02-08 13:51:54 -0800568
569 sk->sk_destruct = iucv_sock_destruct;
570 sk->sk_sndtimeo = IUCV_CONN_TIMEOUT;
571 sk->sk_allocation = GFP_DMA;
572
573 sock_reset_flag(sk, SOCK_ZAPPED);
574
575 sk->sk_protocol = proto;
576 sk->sk_state = IUCV_OPEN;
577
Jennifer Hunteac37312007-02-08 13:51:54 -0800578 iucv_sock_link(&iucv_sk_list, sk);
579 return sk;
580}
581
582/* Create an IUCV socket */
Eric Paris3f378b62009-11-05 22:18:14 -0800583static int iucv_sock_create(struct net *net, struct socket *sock, int protocol,
584 int kern)
Jennifer Hunteac37312007-02-08 13:51:54 -0800585{
586 struct sock *sk;
587
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +0000588 if (protocol && protocol != PF_IUCV)
589 return -EPROTONOSUPPORT;
Jennifer Hunteac37312007-02-08 13:51:54 -0800590
591 sock->state = SS_UNCONNECTED;
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +0000592
593 switch (sock->type) {
594 case SOCK_STREAM:
595 sock->ops = &iucv_sock_ops;
596 break;
597 case SOCK_SEQPACKET:
598 /* currently, proto ops can handle both sk types */
599 sock->ops = &iucv_sock_ops;
600 break;
601 default:
602 return -ESOCKTNOSUPPORT;
603 }
Jennifer Hunteac37312007-02-08 13:51:54 -0800604
605 sk = iucv_sock_alloc(sock, protocol, GFP_KERNEL);
606 if (!sk)
607 return -ENOMEM;
608
609 iucv_sock_init(sk, NULL);
610
611 return 0;
612}
613
614void iucv_sock_link(struct iucv_sock_list *l, struct sock *sk)
615{
616 write_lock_bh(&l->lock);
617 sk_add_node(sk, &l->head);
618 write_unlock_bh(&l->lock);
619}
620
621void iucv_sock_unlink(struct iucv_sock_list *l, struct sock *sk)
622{
623 write_lock_bh(&l->lock);
624 sk_del_node_init(sk);
625 write_unlock_bh(&l->lock);
626}
627
628void iucv_accept_enqueue(struct sock *parent, struct sock *sk)
629{
Ursula Braunfebca282007-07-14 19:04:25 -0700630 unsigned long flags;
631 struct iucv_sock *par = iucv_sk(parent);
632
Jennifer Hunteac37312007-02-08 13:51:54 -0800633 sock_hold(sk);
Ursula Braunfebca282007-07-14 19:04:25 -0700634 spin_lock_irqsave(&par->accept_q_lock, flags);
635 list_add_tail(&iucv_sk(sk)->accept_q, &par->accept_q);
636 spin_unlock_irqrestore(&par->accept_q_lock, flags);
Jennifer Hunteac37312007-02-08 13:51:54 -0800637 iucv_sk(sk)->parent = parent;
Hendrik Brueckner49f5eba2009-10-14 22:54:55 +0000638 sk_acceptq_added(parent);
Jennifer Hunteac37312007-02-08 13:51:54 -0800639}
640
641void iucv_accept_unlink(struct sock *sk)
642{
Ursula Braunfebca282007-07-14 19:04:25 -0700643 unsigned long flags;
644 struct iucv_sock *par = iucv_sk(iucv_sk(sk)->parent);
645
646 spin_lock_irqsave(&par->accept_q_lock, flags);
Jennifer Hunteac37312007-02-08 13:51:54 -0800647 list_del_init(&iucv_sk(sk)->accept_q);
Ursula Braunfebca282007-07-14 19:04:25 -0700648 spin_unlock_irqrestore(&par->accept_q_lock, flags);
Hendrik Brueckner49f5eba2009-10-14 22:54:55 +0000649 sk_acceptq_removed(iucv_sk(sk)->parent);
Jennifer Hunteac37312007-02-08 13:51:54 -0800650 iucv_sk(sk)->parent = NULL;
651 sock_put(sk);
652}
653
654struct sock *iucv_accept_dequeue(struct sock *parent, struct socket *newsock)
655{
656 struct iucv_sock *isk, *n;
657 struct sock *sk;
658
Heiko Carstensda99f052007-05-04 12:23:27 -0700659 list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) {
Jennifer Hunteac37312007-02-08 13:51:54 -0800660 sk = (struct sock *) isk;
661 lock_sock(sk);
662
663 if (sk->sk_state == IUCV_CLOSED) {
Jennifer Hunteac37312007-02-08 13:51:54 -0800664 iucv_accept_unlink(sk);
Ursula Braunfebca282007-07-14 19:04:25 -0700665 release_sock(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800666 continue;
667 }
668
669 if (sk->sk_state == IUCV_CONNECTED ||
Ursula Braunaac63992011-12-19 22:56:31 +0000670 sk->sk_state == IUCV_DISCONN ||
Jennifer Hunteac37312007-02-08 13:51:54 -0800671 !newsock) {
672 iucv_accept_unlink(sk);
673 if (newsock)
674 sock_graft(sk, newsock);
675
Jennifer Hunteac37312007-02-08 13:51:54 -0800676 release_sock(sk);
677 return sk;
678 }
679
680 release_sock(sk);
681 }
682 return NULL;
683}
684
Philipp Hachtmann53a4b492014-05-28 10:22:28 +0200685static void __iucv_auto_name(struct iucv_sock *iucv)
686{
687 char name[12];
688
689 sprintf(name, "%08x", atomic_inc_return(&iucv_sk_list.autobind_name));
690 while (__iucv_get_sock_by_name(name)) {
691 sprintf(name, "%08x",
692 atomic_inc_return(&iucv_sk_list.autobind_name));
693 }
694 memcpy(iucv->src_name, name, 8);
695}
696
Jennifer Hunteac37312007-02-08 13:51:54 -0800697/* Bind an unbound socket */
698static int iucv_sock_bind(struct socket *sock, struct sockaddr *addr,
699 int addr_len)
700{
701 struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
702 struct sock *sk = sock->sk;
703 struct iucv_sock *iucv;
Ursula Braun3881ac42011-08-08 01:33:54 +0000704 int err = 0;
705 struct net_device *dev;
706 char uid[9];
Jennifer Hunteac37312007-02-08 13:51:54 -0800707
708 /* Verify the input sockaddr */
709 if (!addr || addr->sa_family != AF_IUCV)
710 return -EINVAL;
711
712 lock_sock(sk);
713 if (sk->sk_state != IUCV_OPEN) {
714 err = -EBADFD;
715 goto done;
716 }
717
718 write_lock_bh(&iucv_sk_list.lock);
719
720 iucv = iucv_sk(sk);
721 if (__iucv_get_sock_by_name(sa->siucv_name)) {
722 err = -EADDRINUSE;
723 goto done_unlock;
724 }
Ursula Braun3881ac42011-08-08 01:33:54 +0000725 if (iucv->path)
Jennifer Hunteac37312007-02-08 13:51:54 -0800726 goto done_unlock;
Jennifer Hunteac37312007-02-08 13:51:54 -0800727
728 /* Bind the socket */
Ursula Braun3881ac42011-08-08 01:33:54 +0000729 if (pr_iucv)
730 if (!memcmp(sa->siucv_user_id, iucv_userid, 8))
731 goto vm_bind; /* VM IUCV transport */
Jennifer Hunteac37312007-02-08 13:51:54 -0800732
Ursula Braun3881ac42011-08-08 01:33:54 +0000733 /* try hiper transport */
734 memcpy(uid, sa->siucv_user_id, sizeof(uid));
735 ASCEBC(uid, 8);
736 rcu_read_lock();
737 for_each_netdev_rcu(&init_net, dev) {
738 if (!memcmp(dev->perm_addr, uid, 8)) {
Ursula Braun3881ac42011-08-08 01:33:54 +0000739 memcpy(iucv->src_user_id, sa->siucv_user_id, 8);
Philipp Hachtmann53a4b492014-05-28 10:22:28 +0200740 /* Check for unitialized siucv_name */
741 if (strncmp(sa->siucv_name, " ", 8) == 0)
742 __iucv_auto_name(iucv);
743 else
744 memcpy(iucv->src_name, sa->siucv_name, 8);
Ursula Braun816abba2011-12-19 22:56:29 +0000745 sk->sk_bound_dev_if = dev->ifindex;
Ursula Braun800c5eb2012-02-08 00:19:47 +0000746 iucv->hs_dev = dev;
747 dev_hold(dev);
Ursula Braun3881ac42011-08-08 01:33:54 +0000748 sk->sk_state = IUCV_BOUND;
749 iucv->transport = AF_IUCV_TRANS_HIPER;
750 if (!iucv->msglimit)
751 iucv->msglimit = IUCV_HIPER_MSGLIM_DEFAULT;
752 rcu_read_unlock();
753 goto done_unlock;
754 }
755 }
756 rcu_read_unlock();
757vm_bind:
758 if (pr_iucv) {
759 /* use local userid for backward compat */
760 memcpy(iucv->src_name, sa->siucv_name, 8);
761 memcpy(iucv->src_user_id, iucv_userid, 8);
762 sk->sk_state = IUCV_BOUND;
763 iucv->transport = AF_IUCV_TRANS_IUCV;
764 if (!iucv->msglimit)
765 iucv->msglimit = IUCV_QUEUELEN_DEFAULT;
766 goto done_unlock;
767 }
768 /* found no dev to bind */
769 err = -ENODEV;
Jennifer Hunteac37312007-02-08 13:51:54 -0800770done_unlock:
771 /* Release the socket list lock */
772 write_unlock_bh(&iucv_sk_list.lock);
773done:
774 release_sock(sk);
775 return err;
776}
777
778/* Automatically bind an unbound socket */
779static int iucv_sock_autobind(struct sock *sk)
780{
781 struct iucv_sock *iucv = iucv_sk(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800782 int err = 0;
783
Ursula Braunaac63992011-12-19 22:56:31 +0000784 if (unlikely(!pr_iucv))
Jennifer Hunteac37312007-02-08 13:51:54 -0800785 return -EPROTO;
786
Ursula Braunaac63992011-12-19 22:56:31 +0000787 memcpy(iucv->src_user_id, iucv_userid, 8);
Jennifer Hunteac37312007-02-08 13:51:54 -0800788
789 write_lock_bh(&iucv_sk_list.lock);
Philipp Hachtmann53a4b492014-05-28 10:22:28 +0200790 __iucv_auto_name(iucv);
Jennifer Hunteac37312007-02-08 13:51:54 -0800791 write_unlock_bh(&iucv_sk_list.lock);
792
Ursula Braun3881ac42011-08-08 01:33:54 +0000793 if (!iucv->msglimit)
794 iucv->msglimit = IUCV_QUEUELEN_DEFAULT;
795
Jennifer Hunteac37312007-02-08 13:51:54 -0800796 return err;
797}
798
Ursula Braun3881ac42011-08-08 01:33:54 +0000799static int afiucv_path_connect(struct socket *sock, struct sockaddr *addr)
Jennifer Hunteac37312007-02-08 13:51:54 -0800800{
801 struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
802 struct sock *sk = sock->sk;
Ursula Braun493d3972011-08-08 01:33:52 +0000803 struct iucv_sock *iucv = iucv_sk(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800804 unsigned char user_data[16];
805 int err;
806
Jennifer Hunteac37312007-02-08 13:51:54 -0800807 high_nmcpy(user_data, sa->siucv_name);
Ursula Braun493d3972011-08-08 01:33:52 +0000808 low_nmcpy(user_data, iucv->src_name);
Jennifer Hunteac37312007-02-08 13:51:54 -0800809 ASCEBC(user_data, sizeof(user_data));
810
Jennifer Hunteac37312007-02-08 13:51:54 -0800811 /* Create path. */
Hendrik Brueckner09488e22009-04-21 23:26:27 +0000812 iucv->path = iucv_path_alloc(iucv->msglimit,
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000813 IUCV_IPRMDATA, GFP_KERNEL);
Ursula Braund4444722008-02-07 18:07:19 -0800814 if (!iucv->path) {
815 err = -ENOMEM;
816 goto done;
817 }
Frank Blaschka6fcd61f2011-08-08 01:33:51 +0000818 err = pr_iucv->path_connect(iucv->path, &af_iucv_handler,
819 sa->siucv_user_id, NULL, user_data,
820 sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800821 if (err) {
822 iucv_path_free(iucv->path);
823 iucv->path = NULL;
Hendrik Brueckner55cdea92009-01-05 18:07:07 -0800824 switch (err) {
825 case 0x0b: /* Target communicator is not logged on */
826 err = -ENETUNREACH;
827 break;
828 case 0x0d: /* Max connections for this guest exceeded */
829 case 0x0e: /* Max connections for target guest exceeded */
830 err = -EAGAIN;
831 break;
832 case 0x0f: /* Missing IUCV authorization */
833 err = -EACCES;
834 break;
835 default:
836 err = -ECONNREFUSED;
837 break;
838 }
Ursula Braun3881ac42011-08-08 01:33:54 +0000839 }
840done:
841 return err;
842}
843
844/* Connect an unconnected socket */
845static int iucv_sock_connect(struct socket *sock, struct sockaddr *addr,
846 int alen, int flags)
847{
848 struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
849 struct sock *sk = sock->sk;
850 struct iucv_sock *iucv = iucv_sk(sk);
851 int err;
852
853 if (addr->sa_family != AF_IUCV || alen < sizeof(struct sockaddr_iucv))
854 return -EINVAL;
855
856 if (sk->sk_state != IUCV_OPEN && sk->sk_state != IUCV_BOUND)
857 return -EBADFD;
858
859 if (sk->sk_state == IUCV_OPEN &&
860 iucv->transport == AF_IUCV_TRANS_HIPER)
861 return -EBADFD; /* explicit bind required */
862
863 if (sk->sk_type != SOCK_STREAM && sk->sk_type != SOCK_SEQPACKET)
864 return -EINVAL;
865
866 if (sk->sk_state == IUCV_OPEN) {
867 err = iucv_sock_autobind(sk);
868 if (unlikely(err))
869 return err;
Jennifer Hunteac37312007-02-08 13:51:54 -0800870 }
871
Ursula Braun3881ac42011-08-08 01:33:54 +0000872 lock_sock(sk);
873
874 /* Set the destination information */
875 memcpy(iucv->dst_user_id, sa->siucv_user_id, 8);
876 memcpy(iucv->dst_name, sa->siucv_name, 8);
877
878 if (iucv->transport == AF_IUCV_TRANS_HIPER)
Ursula Braun9fbd87d2012-03-07 02:06:23 +0000879 err = iucv_send_ctrl(sock->sk, AF_IUCV_FLAG_SYN);
Ursula Braun3881ac42011-08-08 01:33:54 +0000880 else
881 err = afiucv_path_connect(sock, addr);
882 if (err)
883 goto done;
884
885 if (sk->sk_state != IUCV_CONNECTED)
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +0000886 err = iucv_sock_wait(sk, iucv_sock_in_state(sk, IUCV_CONNECTED,
887 IUCV_DISCONN),
888 sock_sndtimeo(sk, flags & O_NONBLOCK));
Jennifer Hunteac37312007-02-08 13:51:54 -0800889
Ursula Braun3881ac42011-08-08 01:33:54 +0000890 if (sk->sk_state == IUCV_DISCONN || sk->sk_state == IUCV_CLOSED)
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000891 err = -ECONNREFUSED;
Ursula Braun18becbc2009-01-05 18:07:46 -0800892
Ursula Braun7d316b92012-02-08 00:19:45 +0000893 if (err && iucv->transport == AF_IUCV_TRANS_IUCV)
894 iucv_sever_path(sk, 0);
Ursula Braun18becbc2009-01-05 18:07:46 -0800895
Jennifer Hunteac37312007-02-08 13:51:54 -0800896done:
897 release_sock(sk);
898 return err;
899}
900
901/* Move a socket into listening state. */
902static int iucv_sock_listen(struct socket *sock, int backlog)
903{
904 struct sock *sk = sock->sk;
905 int err;
906
907 lock_sock(sk);
908
909 err = -EINVAL;
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +0000910 if (sk->sk_state != IUCV_BOUND)
911 goto done;
912
913 if (sock->type != SOCK_STREAM && sock->type != SOCK_SEQPACKET)
Jennifer Hunteac37312007-02-08 13:51:54 -0800914 goto done;
915
916 sk->sk_max_ack_backlog = backlog;
917 sk->sk_ack_backlog = 0;
918 sk->sk_state = IUCV_LISTEN;
919 err = 0;
920
921done:
922 release_sock(sk);
923 return err;
924}
925
926/* Accept a pending connection */
927static int iucv_sock_accept(struct socket *sock, struct socket *newsock,
928 int flags)
929{
930 DECLARE_WAITQUEUE(wait, current);
931 struct sock *sk = sock->sk, *nsk;
932 long timeo;
933 int err = 0;
934
Jennifer Hunt561e0362007-05-04 12:22:07 -0700935 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
Jennifer Hunteac37312007-02-08 13:51:54 -0800936
937 if (sk->sk_state != IUCV_LISTEN) {
938 err = -EBADFD;
939 goto done;
940 }
941
942 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
943
944 /* Wait for an incoming connection */
Eric Dumazetaa395142010-04-20 13:03:51 +0000945 add_wait_queue_exclusive(sk_sleep(sk), &wait);
Heiko Carstensda99f052007-05-04 12:23:27 -0700946 while (!(nsk = iucv_accept_dequeue(sk, newsock))) {
Jennifer Hunteac37312007-02-08 13:51:54 -0800947 set_current_state(TASK_INTERRUPTIBLE);
948 if (!timeo) {
949 err = -EAGAIN;
950 break;
951 }
952
953 release_sock(sk);
954 timeo = schedule_timeout(timeo);
Jennifer Hunt561e0362007-05-04 12:22:07 -0700955 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
Jennifer Hunteac37312007-02-08 13:51:54 -0800956
957 if (sk->sk_state != IUCV_LISTEN) {
958 err = -EBADFD;
959 break;
960 }
961
962 if (signal_pending(current)) {
963 err = sock_intr_errno(timeo);
964 break;
965 }
966 }
967
968 set_current_state(TASK_RUNNING);
Eric Dumazetaa395142010-04-20 13:03:51 +0000969 remove_wait_queue(sk_sleep(sk), &wait);
Jennifer Hunteac37312007-02-08 13:51:54 -0800970
971 if (err)
972 goto done;
973
974 newsock->state = SS_CONNECTED;
975
976done:
977 release_sock(sk);
978 return err;
979}
980
981static int iucv_sock_getname(struct socket *sock, struct sockaddr *addr,
982 int *len, int peer)
983{
984 struct sockaddr_iucv *siucv = (struct sockaddr_iucv *) addr;
985 struct sock *sk = sock->sk;
Ursula Braun493d3972011-08-08 01:33:52 +0000986 struct iucv_sock *iucv = iucv_sk(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800987
988 addr->sa_family = AF_IUCV;
989 *len = sizeof(struct sockaddr_iucv);
990
991 if (peer) {
Ursula Braun493d3972011-08-08 01:33:52 +0000992 memcpy(siucv->siucv_user_id, iucv->dst_user_id, 8);
993 memcpy(siucv->siucv_name, iucv->dst_name, 8);
Jennifer Hunteac37312007-02-08 13:51:54 -0800994 } else {
Ursula Braun493d3972011-08-08 01:33:52 +0000995 memcpy(siucv->siucv_user_id, iucv->src_user_id, 8);
996 memcpy(siucv->siucv_name, iucv->src_name, 8);
Jennifer Hunteac37312007-02-08 13:51:54 -0800997 }
998 memset(&siucv->siucv_port, 0, sizeof(siucv->siucv_port));
999 memset(&siucv->siucv_addr, 0, sizeof(siucv->siucv_addr));
Ursula Braun493d3972011-08-08 01:33:52 +00001000 memset(&siucv->siucv_nodeid, 0, sizeof(siucv->siucv_nodeid));
Jennifer Hunteac37312007-02-08 13:51:54 -08001001
1002 return 0;
1003}
1004
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001005/**
1006 * iucv_send_iprm() - Send socket data in parameter list of an iucv message.
1007 * @path: IUCV path
1008 * @msg: Pointer to a struct iucv_message
1009 * @skb: The socket data to send, skb->len MUST BE <= 7
1010 *
1011 * Send the socket data in the parameter list in the iucv message
1012 * (IUCV_IPRMDATA). The socket data is stored at index 0 to 6 in the parameter
1013 * list and the socket data len at index 7 (last byte).
1014 * See also iucv_msg_length().
1015 *
1016 * Returns the error code from the iucv_message_send() call.
1017 */
1018static int iucv_send_iprm(struct iucv_path *path, struct iucv_message *msg,
1019 struct sk_buff *skb)
1020{
1021 u8 prmdata[8];
1022
1023 memcpy(prmdata, (void *) skb->data, skb->len);
1024 prmdata[7] = 0xff - (u8) skb->len;
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001025 return pr_iucv->message_send(path, msg, IUCV_IPRMDATA, 0,
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001026 (void *) prmdata, 8);
1027}
1028
Jennifer Hunteac37312007-02-08 13:51:54 -08001029static int iucv_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
1030 struct msghdr *msg, size_t len)
1031{
1032 struct sock *sk = sock->sk;
1033 struct iucv_sock *iucv = iucv_sk(sk);
1034 struct sk_buff *skb;
1035 struct iucv_message txmsg;
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001036 struct cmsghdr *cmsg;
1037 int cmsg_done;
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +00001038 long timeo;
Ursula Braun8f7c5022008-12-25 13:39:47 +01001039 char user_id[9];
1040 char appl_id[9];
Jennifer Hunteac37312007-02-08 13:51:54 -08001041 int err;
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +00001042 int noblock = msg->msg_flags & MSG_DONTWAIT;
Jennifer Hunteac37312007-02-08 13:51:54 -08001043
1044 err = sock_error(sk);
1045 if (err)
1046 return err;
1047
1048 if (msg->msg_flags & MSG_OOB)
1049 return -EOPNOTSUPP;
1050
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001051 /* SOCK_SEQPACKET: we do not support segmented records */
1052 if (sk->sk_type == SOCK_SEQPACKET && !(msg->msg_flags & MSG_EOR))
1053 return -EOPNOTSUPP;
1054
Jennifer Hunteac37312007-02-08 13:51:54 -08001055 lock_sock(sk);
1056
1057 if (sk->sk_shutdown & SEND_SHUTDOWN) {
1058 err = -EPIPE;
1059 goto out;
1060 }
1061
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001062 /* Return if the socket is not in connected state */
1063 if (sk->sk_state != IUCV_CONNECTED) {
1064 err = -ENOTCONN;
1065 goto out;
1066 }
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001067
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001068 /* initialize defaults */
1069 cmsg_done = 0; /* check for duplicate headers */
1070 txmsg.class = 0;
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001071
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001072 /* iterate over control messages */
1073 for (cmsg = CMSG_FIRSTHDR(msg); cmsg;
1074 cmsg = CMSG_NXTHDR(msg, cmsg)) {
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001075
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001076 if (!CMSG_OK(msg, cmsg)) {
1077 err = -EINVAL;
Jennifer Hunt561e0362007-05-04 12:22:07 -07001078 goto out;
Jennifer Hunteac37312007-02-08 13:51:54 -08001079 }
1080
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001081 if (cmsg->cmsg_level != SOL_IUCV)
1082 continue;
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001083
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001084 if (cmsg->cmsg_type & cmsg_done) {
1085 err = -EINVAL;
1086 goto out;
1087 }
1088 cmsg_done |= cmsg->cmsg_type;
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001089
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001090 switch (cmsg->cmsg_type) {
1091 case SCM_IUCV_TRGCLS:
1092 if (cmsg->cmsg_len != CMSG_LEN(TRGCLS_SIZE)) {
1093 err = -EINVAL;
1094 goto out;
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001095 }
1096
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001097 /* set iucv message target class */
1098 memcpy(&txmsg.class,
1099 (void *) CMSG_DATA(cmsg), TRGCLS_SIZE);
1100
1101 break;
1102
1103 default:
1104 err = -EINVAL;
1105 goto out;
1106 break;
1107 }
1108 }
1109
1110 /* allocate one skb for each iucv message:
1111 * this is fine for SOCK_SEQPACKET (unless we want to support
1112 * segmented records using the MSG_EOR flag), but
1113 * for SOCK_STREAM we might want to improve it in future */
Ursula Braun3881ac42011-08-08 01:33:54 +00001114 if (iucv->transport == AF_IUCV_TRANS_HIPER)
1115 skb = sock_alloc_send_skb(sk,
1116 len + sizeof(struct af_iucv_trans_hdr) + ETH_HLEN,
1117 noblock, &err);
1118 else
1119 skb = sock_alloc_send_skb(sk, len, noblock, &err);
Ursula Braun800c5eb2012-02-08 00:19:47 +00001120 if (!skb) {
1121 err = -ENOMEM;
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001122 goto out;
Ursula Braun800c5eb2012-02-08 00:19:47 +00001123 }
Ursula Braun3881ac42011-08-08 01:33:54 +00001124 if (iucv->transport == AF_IUCV_TRANS_HIPER)
1125 skb_reserve(skb, sizeof(struct af_iucv_trans_hdr) + ETH_HLEN);
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001126 if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
1127 err = -EFAULT;
1128 goto fail;
1129 }
1130
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +00001131 /* wait if outstanding messages for iucv path has reached */
1132 timeo = sock_sndtimeo(sk, noblock);
1133 err = iucv_sock_wait(sk, iucv_below_msglim(sk), timeo);
1134 if (err)
1135 goto fail;
1136
1137 /* return -ECONNRESET if the socket is no longer connected */
1138 if (sk->sk_state != IUCV_CONNECTED) {
1139 err = -ECONNRESET;
1140 goto fail;
1141 }
1142
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001143 /* increment and save iucv message tag for msg_completion cbk */
1144 txmsg.tag = iucv->send_tag++;
Ursula Braunf9c41a62013-04-07 22:19:26 +00001145 IUCV_SKB_CB(skb)->tag = txmsg.tag;
Ursula Braun800c5eb2012-02-08 00:19:47 +00001146
Ursula Braun3881ac42011-08-08 01:33:54 +00001147 if (iucv->transport == AF_IUCV_TRANS_HIPER) {
1148 atomic_inc(&iucv->msg_sent);
1149 err = afiucv_hs_send(&txmsg, sk, skb, 0);
1150 if (err) {
1151 atomic_dec(&iucv->msg_sent);
1152 goto fail;
1153 }
1154 goto release;
1155 }
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001156 skb_queue_tail(&iucv->send_skb_q, skb);
1157
1158 if (((iucv->path->flags & IUCV_IPRMDATA) & iucv->flags)
1159 && skb->len <= 7) {
1160 err = iucv_send_iprm(iucv->path, &txmsg, skb);
1161
1162 /* on success: there is no message_complete callback
1163 * for an IPRMDATA msg; remove skb from send queue */
1164 if (err == 0) {
1165 skb_unlink(skb, &iucv->send_skb_q);
1166 kfree_skb(skb);
1167 }
1168
1169 /* this error should never happen since the
1170 * IUCV_IPRMDATA path flag is set... sever path */
1171 if (err == 0x15) {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001172 pr_iucv->path_sever(iucv->path, NULL);
Jennifer Hunteac37312007-02-08 13:51:54 -08001173 skb_unlink(skb, &iucv->send_skb_q);
1174 err = -EPIPE;
1175 goto fail;
1176 }
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001177 } else
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001178 err = pr_iucv->message_send(iucv->path, &txmsg, 0, 0,
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001179 (void *) skb->data, skb->len);
1180 if (err) {
1181 if (err == 3) {
1182 user_id[8] = 0;
1183 memcpy(user_id, iucv->dst_user_id, 8);
1184 appl_id[8] = 0;
1185 memcpy(appl_id, iucv->dst_name, 8);
1186 pr_err("Application %s on z/VM guest %s"
1187 " exceeds message limit\n",
1188 appl_id, user_id);
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +00001189 err = -EAGAIN;
1190 } else
1191 err = -EPIPE;
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001192 skb_unlink(skb, &iucv->send_skb_q);
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001193 goto fail;
Jennifer Hunteac37312007-02-08 13:51:54 -08001194 }
1195
Ursula Braun3881ac42011-08-08 01:33:54 +00001196release:
Jennifer Hunteac37312007-02-08 13:51:54 -08001197 release_sock(sk);
1198 return len;
1199
1200fail:
1201 kfree_skb(skb);
1202out:
1203 release_sock(sk);
1204 return err;
1205}
1206
Hendrik Bruecknerbf95d202009-09-16 04:37:28 +00001207/* iucv_fragment_skb() - Fragment a single IUCV message into multiple skb's
1208 *
1209 * Locking: must be called with message_q.lock held
1210 */
Ursula Braunf0703c82007-10-08 02:03:31 -07001211static int iucv_fragment_skb(struct sock *sk, struct sk_buff *skb, int len)
1212{
1213 int dataleft, size, copied = 0;
1214 struct sk_buff *nskb;
1215
1216 dataleft = len;
1217 while (dataleft) {
1218 if (dataleft >= sk->sk_rcvbuf / 4)
1219 size = sk->sk_rcvbuf / 4;
1220 else
1221 size = dataleft;
1222
1223 nskb = alloc_skb(size, GFP_ATOMIC | GFP_DMA);
1224 if (!nskb)
1225 return -ENOMEM;
1226
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001227 /* copy target class to control buffer of new skb */
Ursula Braunf9c41a62013-04-07 22:19:26 +00001228 IUCV_SKB_CB(nskb)->class = IUCV_SKB_CB(skb)->class;
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001229
1230 /* copy data fragment */
Ursula Braunf0703c82007-10-08 02:03:31 -07001231 memcpy(nskb->data, skb->data + copied, size);
1232 copied += size;
1233 dataleft -= size;
1234
1235 skb_reset_transport_header(nskb);
1236 skb_reset_network_header(nskb);
1237 nskb->len = size;
1238
1239 skb_queue_tail(&iucv_sk(sk)->backlog_skb_q, nskb);
1240 }
1241
1242 return 0;
1243}
1244
Hendrik Bruecknerbf95d202009-09-16 04:37:28 +00001245/* iucv_process_message() - Receive a single outstanding IUCV message
1246 *
1247 * Locking: must be called with message_q.lock held
1248 */
Ursula Braunf0703c82007-10-08 02:03:31 -07001249static void iucv_process_message(struct sock *sk, struct sk_buff *skb,
1250 struct iucv_path *path,
1251 struct iucv_message *msg)
1252{
1253 int rc;
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001254 unsigned int len;
Ursula Braunf0703c82007-10-08 02:03:31 -07001255
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001256 len = iucv_msg_length(msg);
1257
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001258 /* store msg target class in the second 4 bytes of skb ctrl buffer */
1259 /* Note: the first 4 bytes are reserved for msg tag */
Ursula Braunf9c41a62013-04-07 22:19:26 +00001260 IUCV_SKB_CB(skb)->class = msg->class;
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001261
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001262 /* check for special IPRM messages (e.g. iucv_sock_shutdown) */
1263 if ((msg->flags & IUCV_IPRMDATA) && len > 7) {
1264 if (memcmp(msg->rmmsg, iprm_shutdown, 8) == 0) {
1265 skb->data = NULL;
1266 skb->len = 0;
1267 }
Ursula Braunf0703c82007-10-08 02:03:31 -07001268 } else {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001269 rc = pr_iucv->message_receive(path, msg,
1270 msg->flags & IUCV_IPRMDATA,
1271 skb->data, len, NULL);
Ursula Braunf0703c82007-10-08 02:03:31 -07001272 if (rc) {
1273 kfree_skb(skb);
1274 return;
1275 }
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001276 /* we need to fragment iucv messages for SOCK_STREAM only;
1277 * for SOCK_SEQPACKET, it is only relevant if we support
1278 * record segmentation using MSG_EOR (see also recvmsg()) */
1279 if (sk->sk_type == SOCK_STREAM &&
1280 skb->truesize >= sk->sk_rcvbuf / 4) {
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001281 rc = iucv_fragment_skb(sk, skb, len);
Ursula Braunf0703c82007-10-08 02:03:31 -07001282 kfree_skb(skb);
1283 skb = NULL;
1284 if (rc) {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001285 pr_iucv->path_sever(path, NULL);
Ursula Braunf0703c82007-10-08 02:03:31 -07001286 return;
1287 }
1288 skb = skb_dequeue(&iucv_sk(sk)->backlog_skb_q);
1289 } else {
1290 skb_reset_transport_header(skb);
1291 skb_reset_network_header(skb);
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001292 skb->len = len;
Ursula Braunf0703c82007-10-08 02:03:31 -07001293 }
1294 }
1295
Ursula Braunf9c41a62013-04-07 22:19:26 +00001296 IUCV_SKB_CB(skb)->offset = 0;
Ursula Braunf0703c82007-10-08 02:03:31 -07001297 if (sock_queue_rcv_skb(sk, skb))
1298 skb_queue_head(&iucv_sk(sk)->backlog_skb_q, skb);
1299}
1300
Hendrik Bruecknerbf95d202009-09-16 04:37:28 +00001301/* iucv_process_message_q() - Process outstanding IUCV messages
1302 *
1303 * Locking: must be called with message_q.lock held
1304 */
Ursula Braunf0703c82007-10-08 02:03:31 -07001305static void iucv_process_message_q(struct sock *sk)
1306{
1307 struct iucv_sock *iucv = iucv_sk(sk);
1308 struct sk_buff *skb;
1309 struct sock_msg_q *p, *n;
1310
1311 list_for_each_entry_safe(p, n, &iucv->message_q.list, list) {
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001312 skb = alloc_skb(iucv_msg_length(&p->msg), GFP_ATOMIC | GFP_DMA);
Ursula Braunf0703c82007-10-08 02:03:31 -07001313 if (!skb)
1314 break;
1315 iucv_process_message(sk, skb, p->path, &p->msg);
1316 list_del(&p->list);
1317 kfree(p);
1318 if (!skb_queue_empty(&iucv->backlog_skb_q))
1319 break;
1320 }
1321}
1322
Jennifer Hunteac37312007-02-08 13:51:54 -08001323static int iucv_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
1324 struct msghdr *msg, size_t len, int flags)
1325{
1326 int noblock = flags & MSG_DONTWAIT;
1327 struct sock *sk = sock->sk;
Jennifer Hunt561e0362007-05-04 12:22:07 -07001328 struct iucv_sock *iucv = iucv_sk(sk);
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001329 unsigned int copied, rlen;
Ursula Braun9fbd87d2012-03-07 02:06:23 +00001330 struct sk_buff *skb, *rskb, *cskb;
Jennifer Hunteac37312007-02-08 13:51:54 -08001331 int err = 0;
Ursula Braunf9c41a62013-04-07 22:19:26 +00001332 u32 offset;
Jennifer Hunteac37312007-02-08 13:51:54 -08001333
Ursula Braunaac63992011-12-19 22:56:31 +00001334 if ((sk->sk_state == IUCV_DISCONN) &&
Ursula Braunf0703c82007-10-08 02:03:31 -07001335 skb_queue_empty(&iucv->backlog_skb_q) &&
1336 skb_queue_empty(&sk->sk_receive_queue) &&
1337 list_empty(&iucv->message_q.list))
Jennifer Hunt561e0362007-05-04 12:22:07 -07001338 return 0;
1339
Jennifer Hunteac37312007-02-08 13:51:54 -08001340 if (flags & (MSG_OOB))
1341 return -EOPNOTSUPP;
1342
Hendrik Brueckner60d37052009-04-21 06:04:21 +00001343 /* receive/dequeue next skb:
1344 * the function understands MSG_PEEK and, thus, does not dequeue skb */
Jennifer Hunteac37312007-02-08 13:51:54 -08001345 skb = skb_recv_datagram(sk, flags, noblock, &err);
1346 if (!skb) {
1347 if (sk->sk_shutdown & RCV_SHUTDOWN)
1348 return 0;
1349 return err;
1350 }
1351
Ursula Braunf9c41a62013-04-07 22:19:26 +00001352 offset = IUCV_SKB_CB(skb)->offset;
1353 rlen = skb->len - offset; /* real length of skb */
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001354 copied = min_t(unsigned int, rlen, len);
Ursula Braun82492a32012-03-07 02:06:24 +00001355 if (!rlen)
1356 sk->sk_shutdown = sk->sk_shutdown | RCV_SHUTDOWN;
Jennifer Hunteac37312007-02-08 13:51:54 -08001357
Jennifer Hunt561e0362007-05-04 12:22:07 -07001358 cskb = skb;
Ursula Braunf9c41a62013-04-07 22:19:26 +00001359 if (skb_copy_datagram_iovec(cskb, offset, msg->msg_iov, copied)) {
Hendrik Brueckner802788b2009-04-21 23:26:26 +00001360 if (!(flags & MSG_PEEK))
1361 skb_queue_head(&sk->sk_receive_queue, skb);
1362 return -EFAULT;
Jennifer Hunteac37312007-02-08 13:51:54 -08001363 }
1364
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001365 /* SOCK_SEQPACKET: set MSG_TRUNC if recv buf size is too small */
1366 if (sk->sk_type == SOCK_SEQPACKET) {
1367 if (copied < rlen)
1368 msg->msg_flags |= MSG_TRUNC;
1369 /* each iucv message contains a complete record */
1370 msg->msg_flags |= MSG_EOR;
1371 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001372
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001373 /* create control message to store iucv msg target class:
1374 * get the trgcls from the control buffer of the skb due to
1375 * fragmentation of original iucv message. */
1376 err = put_cmsg(msg, SOL_IUCV, SCM_IUCV_TRGCLS,
Ursula Braunf9c41a62013-04-07 22:19:26 +00001377 sizeof(IUCV_SKB_CB(skb)->class),
1378 (void *)&IUCV_SKB_CB(skb)->class);
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001379 if (err) {
1380 if (!(flags & MSG_PEEK))
1381 skb_queue_head(&sk->sk_receive_queue, skb);
1382 return err;
1383 }
1384
Jennifer Hunteac37312007-02-08 13:51:54 -08001385 /* Mark read part of skb as used */
1386 if (!(flags & MSG_PEEK)) {
Jennifer Hunteac37312007-02-08 13:51:54 -08001387
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001388 /* SOCK_STREAM: re-queue skb if it contains unreceived data */
1389 if (sk->sk_type == SOCK_STREAM) {
Ursula Braunf9c41a62013-04-07 22:19:26 +00001390 if (copied < rlen) {
1391 IUCV_SKB_CB(skb)->offset = offset + copied;
Ursula Braun2f139a52014-03-19 07:57:59 +01001392 skb_queue_head(&sk->sk_receive_queue, skb);
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001393 goto done;
1394 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001395 }
1396
1397 kfree_skb(skb);
Ursula Braun800c5eb2012-02-08 00:19:47 +00001398 if (iucv->transport == AF_IUCV_TRANS_HIPER) {
1399 atomic_inc(&iucv->msg_recv);
1400 if (atomic_read(&iucv->msg_recv) > iucv->msglimit) {
1401 WARN_ON(1);
1402 iucv_sock_close(sk);
1403 return -EFAULT;
1404 }
1405 }
Jennifer Hunt561e0362007-05-04 12:22:07 -07001406
1407 /* Queue backlog skbs */
Hendrik Bruecknerbf95d202009-09-16 04:37:28 +00001408 spin_lock_bh(&iucv->message_q.lock);
Ursula Braunf0703c82007-10-08 02:03:31 -07001409 rskb = skb_dequeue(&iucv->backlog_skb_q);
Heiko Carstensda99f052007-05-04 12:23:27 -07001410 while (rskb) {
Ursula Braunf9c41a62013-04-07 22:19:26 +00001411 IUCV_SKB_CB(rskb)->offset = 0;
Jennifer Hunt561e0362007-05-04 12:22:07 -07001412 if (sock_queue_rcv_skb(sk, rskb)) {
Ursula Braunf0703c82007-10-08 02:03:31 -07001413 skb_queue_head(&iucv->backlog_skb_q,
Jennifer Hunt561e0362007-05-04 12:22:07 -07001414 rskb);
1415 break;
1416 } else {
Ursula Braunf0703c82007-10-08 02:03:31 -07001417 rskb = skb_dequeue(&iucv->backlog_skb_q);
Jennifer Hunt561e0362007-05-04 12:22:07 -07001418 }
1419 }
Ursula Braunf0703c82007-10-08 02:03:31 -07001420 if (skb_queue_empty(&iucv->backlog_skb_q)) {
Ursula Braunf0703c82007-10-08 02:03:31 -07001421 if (!list_empty(&iucv->message_q.list))
1422 iucv_process_message_q(sk);
Ursula Braun3881ac42011-08-08 01:33:54 +00001423 if (atomic_read(&iucv->msg_recv) >=
1424 iucv->msglimit / 2) {
Ursula Braun9fbd87d2012-03-07 02:06:23 +00001425 err = iucv_send_ctrl(sk, AF_IUCV_FLAG_WIN);
Ursula Braun3881ac42011-08-08 01:33:54 +00001426 if (err) {
1427 sk->sk_state = IUCV_DISCONN;
1428 sk->sk_state_change(sk);
1429 }
1430 }
Ursula Braunf0703c82007-10-08 02:03:31 -07001431 }
Hendrik Bruecknerbf95d202009-09-16 04:37:28 +00001432 spin_unlock_bh(&iucv->message_q.lock);
Hendrik Brueckner60d37052009-04-21 06:04:21 +00001433 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001434
1435done:
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001436 /* SOCK_SEQPACKET: return real length if MSG_TRUNC is set */
1437 if (sk->sk_type == SOCK_SEQPACKET && (flags & MSG_TRUNC))
1438 copied = rlen;
1439
1440 return copied;
Jennifer Hunteac37312007-02-08 13:51:54 -08001441}
1442
1443static inline unsigned int iucv_accept_poll(struct sock *parent)
1444{
1445 struct iucv_sock *isk, *n;
1446 struct sock *sk;
1447
Heiko Carstensda99f052007-05-04 12:23:27 -07001448 list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) {
Jennifer Hunteac37312007-02-08 13:51:54 -08001449 sk = (struct sock *) isk;
1450
1451 if (sk->sk_state == IUCV_CONNECTED)
1452 return POLLIN | POLLRDNORM;
1453 }
1454
1455 return 0;
1456}
1457
1458unsigned int iucv_sock_poll(struct file *file, struct socket *sock,
1459 poll_table *wait)
1460{
1461 struct sock *sk = sock->sk;
1462 unsigned int mask = 0;
1463
Eric Dumazetaa395142010-04-20 13:03:51 +00001464 sock_poll_wait(file, sk_sleep(sk), wait);
Jennifer Hunteac37312007-02-08 13:51:54 -08001465
1466 if (sk->sk_state == IUCV_LISTEN)
1467 return iucv_accept_poll(sk);
1468
1469 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
Keller, Jacob E7d4c04f2013-03-28 11:19:25 +00001470 mask |= POLLERR |
Jacob Keller8facd5f2013-04-02 13:55:40 -07001471 (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? POLLPRI : 0);
Jennifer Hunteac37312007-02-08 13:51:54 -08001472
1473 if (sk->sk_shutdown & RCV_SHUTDOWN)
1474 mask |= POLLRDHUP;
1475
1476 if (sk->sk_shutdown == SHUTDOWN_MASK)
1477 mask |= POLLHUP;
1478
1479 if (!skb_queue_empty(&sk->sk_receive_queue) ||
Heiko Carstensda99f052007-05-04 12:23:27 -07001480 (sk->sk_shutdown & RCV_SHUTDOWN))
Jennifer Hunteac37312007-02-08 13:51:54 -08001481 mask |= POLLIN | POLLRDNORM;
1482
1483 if (sk->sk_state == IUCV_CLOSED)
1484 mask |= POLLHUP;
1485
Ursula Braunaac63992011-12-19 22:56:31 +00001486 if (sk->sk_state == IUCV_DISCONN)
Jennifer Hunt561e0362007-05-04 12:22:07 -07001487 mask |= POLLIN;
1488
Ursula Braun7f1b0ea2012-02-08 00:19:46 +00001489 if (sock_writeable(sk) && iucv_below_msglim(sk))
Jennifer Hunteac37312007-02-08 13:51:54 -08001490 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
1491 else
1492 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
1493
1494 return mask;
1495}
1496
1497static int iucv_sock_shutdown(struct socket *sock, int how)
1498{
1499 struct sock *sk = sock->sk;
1500 struct iucv_sock *iucv = iucv_sk(sk);
1501 struct iucv_message txmsg;
1502 int err = 0;
Jennifer Hunteac37312007-02-08 13:51:54 -08001503
1504 how++;
1505
1506 if ((how & ~SHUTDOWN_MASK) || !how)
1507 return -EINVAL;
1508
1509 lock_sock(sk);
Heiko Carstensda99f052007-05-04 12:23:27 -07001510 switch (sk->sk_state) {
Ursula Braun82492a32012-03-07 02:06:24 +00001511 case IUCV_LISTEN:
Hendrik Bruecknere14ad5f2009-04-21 06:04:23 +00001512 case IUCV_DISCONN:
1513 case IUCV_CLOSING:
Jennifer Hunteac37312007-02-08 13:51:54 -08001514 case IUCV_CLOSED:
1515 err = -ENOTCONN;
1516 goto fail;
Jennifer Hunteac37312007-02-08 13:51:54 -08001517 default:
Jennifer Hunteac37312007-02-08 13:51:54 -08001518 break;
1519 }
1520
1521 if (how == SEND_SHUTDOWN || how == SHUTDOWN_MASK) {
Ursula Braun82492a32012-03-07 02:06:24 +00001522 if (iucv->transport == AF_IUCV_TRANS_IUCV) {
1523 txmsg.class = 0;
1524 txmsg.tag = 0;
1525 err = pr_iucv->message_send(iucv->path, &txmsg,
1526 IUCV_IPRMDATA, 0, (void *) iprm_shutdown, 8);
1527 if (err) {
1528 switch (err) {
1529 case 1:
1530 err = -ENOTCONN;
1531 break;
1532 case 2:
1533 err = -ECONNRESET;
1534 break;
1535 default:
1536 err = -ENOTCONN;
1537 break;
1538 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001539 }
Ursula Braun82492a32012-03-07 02:06:24 +00001540 } else
1541 iucv_send_ctrl(sk, AF_IUCV_FLAG_SHT);
Jennifer Hunteac37312007-02-08 13:51:54 -08001542 }
1543
Ursula Braun82492a32012-03-07 02:06:24 +00001544 sk->sk_shutdown |= how;
Jennifer Hunteac37312007-02-08 13:51:54 -08001545 if (how == RCV_SHUTDOWN || how == SHUTDOWN_MASK) {
Ursula Braun82492a32012-03-07 02:06:24 +00001546 if (iucv->transport == AF_IUCV_TRANS_IUCV) {
1547 err = pr_iucv->path_quiesce(iucv->path, NULL);
1548 if (err)
1549 err = -ENOTCONN;
1550/* skb_queue_purge(&sk->sk_receive_queue); */
1551 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001552 skb_queue_purge(&sk->sk_receive_queue);
1553 }
1554
1555 /* Wake up anyone sleeping in poll */
1556 sk->sk_state_change(sk);
1557
1558fail:
1559 release_sock(sk);
1560 return err;
1561}
1562
1563static int iucv_sock_release(struct socket *sock)
1564{
1565 struct sock *sk = sock->sk;
1566 int err = 0;
1567
1568 if (!sk)
1569 return 0;
1570
1571 iucv_sock_close(sk);
1572
Jennifer Hunteac37312007-02-08 13:51:54 -08001573 sock_orphan(sk);
1574 iucv_sock_kill(sk);
1575 return err;
1576}
1577
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00001578/* getsockopt and setsockopt */
1579static int iucv_sock_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07001580 char __user *optval, unsigned int optlen)
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00001581{
1582 struct sock *sk = sock->sk;
1583 struct iucv_sock *iucv = iucv_sk(sk);
1584 int val;
1585 int rc;
1586
1587 if (level != SOL_IUCV)
1588 return -ENOPROTOOPT;
1589
1590 if (optlen < sizeof(int))
1591 return -EINVAL;
1592
1593 if (get_user(val, (int __user *) optval))
1594 return -EFAULT;
1595
1596 rc = 0;
1597
1598 lock_sock(sk);
1599 switch (optname) {
1600 case SO_IPRMDATA_MSG:
1601 if (val)
1602 iucv->flags |= IUCV_IPRMDATA;
1603 else
1604 iucv->flags &= ~IUCV_IPRMDATA;
1605 break;
Hendrik Brueckner09488e22009-04-21 23:26:27 +00001606 case SO_MSGLIMIT:
1607 switch (sk->sk_state) {
1608 case IUCV_OPEN:
1609 case IUCV_BOUND:
1610 if (val < 1 || val > (u16)(~0))
1611 rc = -EINVAL;
1612 else
1613 iucv->msglimit = val;
1614 break;
1615 default:
1616 rc = -EINVAL;
1617 break;
1618 }
1619 break;
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00001620 default:
1621 rc = -ENOPROTOOPT;
1622 break;
1623 }
1624 release_sock(sk);
1625
1626 return rc;
1627}
1628
1629static int iucv_sock_getsockopt(struct socket *sock, int level, int optname,
1630 char __user *optval, int __user *optlen)
1631{
1632 struct sock *sk = sock->sk;
1633 struct iucv_sock *iucv = iucv_sk(sk);
Ursula Braun51363b82012-02-08 00:19:48 +00001634 unsigned int val;
1635 int len;
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00001636
1637 if (level != SOL_IUCV)
1638 return -ENOPROTOOPT;
1639
1640 if (get_user(len, optlen))
1641 return -EFAULT;
1642
1643 if (len < 0)
1644 return -EINVAL;
1645
1646 len = min_t(unsigned int, len, sizeof(int));
1647
1648 switch (optname) {
1649 case SO_IPRMDATA_MSG:
1650 val = (iucv->flags & IUCV_IPRMDATA) ? 1 : 0;
1651 break;
Hendrik Brueckner09488e22009-04-21 23:26:27 +00001652 case SO_MSGLIMIT:
1653 lock_sock(sk);
1654 val = (iucv->path != NULL) ? iucv->path->msglim /* connected */
1655 : iucv->msglimit; /* default */
1656 release_sock(sk);
1657 break;
Ursula Braun51363b82012-02-08 00:19:48 +00001658 case SO_MSGSIZE:
1659 if (sk->sk_state == IUCV_OPEN)
1660 return -EBADFD;
1661 val = (iucv->hs_dev) ? iucv->hs_dev->mtu -
1662 sizeof(struct af_iucv_trans_hdr) - ETH_HLEN :
1663 0x7fffffff;
1664 break;
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00001665 default:
1666 return -ENOPROTOOPT;
1667 }
1668
1669 if (put_user(len, optlen))
1670 return -EFAULT;
1671 if (copy_to_user(optval, &val, len))
1672 return -EFAULT;
1673
1674 return 0;
1675}
1676
1677
Jennifer Hunteac37312007-02-08 13:51:54 -08001678/* Callback wrappers - called from iucv base support */
1679static int iucv_callback_connreq(struct iucv_path *path,
1680 u8 ipvmid[8], u8 ipuser[16])
1681{
1682 unsigned char user_data[16];
1683 unsigned char nuser_data[16];
1684 unsigned char src_name[8];
Jennifer Hunteac37312007-02-08 13:51:54 -08001685 struct sock *sk, *nsk;
1686 struct iucv_sock *iucv, *niucv;
1687 int err;
1688
1689 memcpy(src_name, ipuser, 8);
1690 EBCASC(src_name, 8);
1691 /* Find out if this path belongs to af_iucv. */
1692 read_lock(&iucv_sk_list.lock);
1693 iucv = NULL;
Ursula Braunfebca282007-07-14 19:04:25 -07001694 sk = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001695 sk_for_each(sk, &iucv_sk_list.head)
Jennifer Hunteac37312007-02-08 13:51:54 -08001696 if (sk->sk_state == IUCV_LISTEN &&
1697 !memcmp(&iucv_sk(sk)->src_name, src_name, 8)) {
1698 /*
1699 * Found a listening socket with
1700 * src_name == ipuser[0-7].
1701 */
1702 iucv = iucv_sk(sk);
1703 break;
1704 }
1705 read_unlock(&iucv_sk_list.lock);
1706 if (!iucv)
1707 /* No socket found, not one of our paths. */
1708 return -EINVAL;
1709
1710 bh_lock_sock(sk);
1711
1712 /* Check if parent socket is listening */
1713 low_nmcpy(user_data, iucv->src_name);
1714 high_nmcpy(user_data, iucv->dst_name);
1715 ASCEBC(user_data, sizeof(user_data));
1716 if (sk->sk_state != IUCV_LISTEN) {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001717 err = pr_iucv->path_sever(path, user_data);
Hendrik Brueckner65dbd7c2009-01-05 18:08:23 -08001718 iucv_path_free(path);
Jennifer Hunteac37312007-02-08 13:51:54 -08001719 goto fail;
1720 }
1721
1722 /* Check for backlog size */
1723 if (sk_acceptq_is_full(sk)) {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001724 err = pr_iucv->path_sever(path, user_data);
Hendrik Brueckner65dbd7c2009-01-05 18:08:23 -08001725 iucv_path_free(path);
Jennifer Hunteac37312007-02-08 13:51:54 -08001726 goto fail;
1727 }
1728
1729 /* Create the new socket */
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001730 nsk = iucv_sock_alloc(NULL, sk->sk_type, GFP_ATOMIC);
Heiko Carstensda99f052007-05-04 12:23:27 -07001731 if (!nsk) {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001732 err = pr_iucv->path_sever(path, user_data);
Hendrik Brueckner65dbd7c2009-01-05 18:08:23 -08001733 iucv_path_free(path);
Jennifer Hunteac37312007-02-08 13:51:54 -08001734 goto fail;
1735 }
1736
1737 niucv = iucv_sk(nsk);
1738 iucv_sock_init(nsk, sk);
1739
1740 /* Set the new iucv_sock */
1741 memcpy(niucv->dst_name, ipuser + 8, 8);
1742 EBCASC(niucv->dst_name, 8);
1743 memcpy(niucv->dst_user_id, ipvmid, 8);
1744 memcpy(niucv->src_name, iucv->src_name, 8);
1745 memcpy(niucv->src_user_id, iucv->src_user_id, 8);
1746 niucv->path = path;
1747
1748 /* Call iucv_accept */
1749 high_nmcpy(nuser_data, ipuser + 8);
1750 memcpy(nuser_data + 8, niucv->src_name, 8);
1751 ASCEBC(nuser_data + 8, 8);
1752
Hendrik Brueckner09488e22009-04-21 23:26:27 +00001753 /* set message limit for path based on msglimit of accepting socket */
1754 niucv->msglimit = iucv->msglimit;
1755 path->msglim = iucv->msglimit;
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001756 err = pr_iucv->path_accept(path, &af_iucv_handler, nuser_data, nsk);
Heiko Carstensda99f052007-05-04 12:23:27 -07001757 if (err) {
Ursula Braun7d316b92012-02-08 00:19:45 +00001758 iucv_sever_path(nsk, 1);
Hendrik Brueckner65dbd7c2009-01-05 18:08:23 -08001759 iucv_sock_kill(nsk);
Jennifer Hunteac37312007-02-08 13:51:54 -08001760 goto fail;
1761 }
1762
1763 iucv_accept_enqueue(sk, nsk);
1764
1765 /* Wake up accept */
1766 nsk->sk_state = IUCV_CONNECTED;
David S. Miller676d2362014-04-11 16:15:36 -04001767 sk->sk_data_ready(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -08001768 err = 0;
1769fail:
1770 bh_unlock_sock(sk);
1771 return 0;
1772}
1773
1774static void iucv_callback_connack(struct iucv_path *path, u8 ipuser[16])
1775{
1776 struct sock *sk = path->private;
1777
1778 sk->sk_state = IUCV_CONNECTED;
1779 sk->sk_state_change(sk);
1780}
1781
1782static void iucv_callback_rx(struct iucv_path *path, struct iucv_message *msg)
1783{
1784 struct sock *sk = path->private;
Jennifer Hunt561e0362007-05-04 12:22:07 -07001785 struct iucv_sock *iucv = iucv_sk(sk);
Ursula Braunf0703c82007-10-08 02:03:31 -07001786 struct sk_buff *skb;
1787 struct sock_msg_q *save_msg;
1788 int len;
Jennifer Hunt561e0362007-05-04 12:22:07 -07001789
Hendrik Bruecknerfe86e542009-04-21 06:04:22 +00001790 if (sk->sk_shutdown & RCV_SHUTDOWN) {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001791 pr_iucv->message_reject(path, msg);
Jennifer Hunteac37312007-02-08 13:51:54 -08001792 return;
Hendrik Bruecknerfe86e542009-04-21 06:04:22 +00001793 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001794
Hendrik Brueckner3fa6b5a2009-04-21 06:04:24 +00001795 spin_lock(&iucv->message_q.lock);
Jennifer Hunteac37312007-02-08 13:51:54 -08001796
Ursula Braunf0703c82007-10-08 02:03:31 -07001797 if (!list_empty(&iucv->message_q.list) ||
1798 !skb_queue_empty(&iucv->backlog_skb_q))
1799 goto save_message;
1800
1801 len = atomic_read(&sk->sk_rmem_alloc);
Eric Dumazet87fb4b72011-10-13 07:28:54 +00001802 len += SKB_TRUESIZE(iucv_msg_length(msg));
Ursula Braunf0703c82007-10-08 02:03:31 -07001803 if (len > sk->sk_rcvbuf)
1804 goto save_message;
1805
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001806 skb = alloc_skb(iucv_msg_length(msg), GFP_ATOMIC | GFP_DMA);
Ursula Braunf0703c82007-10-08 02:03:31 -07001807 if (!skb)
1808 goto save_message;
Jennifer Hunteac37312007-02-08 13:51:54 -08001809
Ursula Braunf0703c82007-10-08 02:03:31 -07001810 iucv_process_message(sk, skb, path, msg);
Hendrik Brueckner3fa6b5a2009-04-21 06:04:24 +00001811 goto out_unlock;
Jennifer Hunteac37312007-02-08 13:51:54 -08001812
Ursula Braunf0703c82007-10-08 02:03:31 -07001813save_message:
1814 save_msg = kzalloc(sizeof(struct sock_msg_q), GFP_ATOMIC | GFP_DMA);
Ursula Braund4444722008-02-07 18:07:19 -08001815 if (!save_msg)
Julia Lawalla56635a2010-05-26 05:56:48 +00001816 goto out_unlock;
Ursula Braunf0703c82007-10-08 02:03:31 -07001817 save_msg->path = path;
1818 save_msg->msg = *msg;
1819
Ursula Braunf0703c82007-10-08 02:03:31 -07001820 list_add_tail(&save_msg->list, &iucv->message_q.list);
Hendrik Brueckner3fa6b5a2009-04-21 06:04:24 +00001821
1822out_unlock:
Ursula Braunf0703c82007-10-08 02:03:31 -07001823 spin_unlock(&iucv->message_q.lock);
Jennifer Hunteac37312007-02-08 13:51:54 -08001824}
1825
1826static void iucv_callback_txdone(struct iucv_path *path,
1827 struct iucv_message *msg)
1828{
1829 struct sock *sk = path->private;
Ursula Braunf2a77992008-02-07 18:07:44 -08001830 struct sk_buff *this = NULL;
Jennifer Hunteac37312007-02-08 13:51:54 -08001831 struct sk_buff_head *list = &iucv_sk(sk)->send_skb_q;
1832 struct sk_buff *list_skb = list->next;
1833 unsigned long flags;
1834
Ursula Braun7d316b92012-02-08 00:19:45 +00001835 bh_lock_sock(sk);
Ursula Braunf2a77992008-02-07 18:07:44 -08001836 if (!skb_queue_empty(list)) {
Jennifer Hunt561e0362007-05-04 12:22:07 -07001837 spin_lock_irqsave(&list->lock, flags);
Jennifer Hunteac37312007-02-08 13:51:54 -08001838
Ursula Braunf2a77992008-02-07 18:07:44 -08001839 while (list_skb != (struct sk_buff *)list) {
Ursula Braunf5738e22014-05-13 14:38:02 +02001840 if (msg->tag == IUCV_SKB_CB(list_skb)->tag) {
Ursula Braunf2a77992008-02-07 18:07:44 -08001841 this = list_skb;
1842 break;
1843 }
Jennifer Hunt561e0362007-05-04 12:22:07 -07001844 list_skb = list_skb->next;
Ursula Braunf2a77992008-02-07 18:07:44 -08001845 }
1846 if (this)
1847 __skb_unlink(this, list);
Jennifer Hunteac37312007-02-08 13:51:54 -08001848
Jennifer Hunt561e0362007-05-04 12:22:07 -07001849 spin_unlock_irqrestore(&list->lock, flags);
Jennifer Hunteac37312007-02-08 13:51:54 -08001850
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +00001851 if (this) {
1852 kfree_skb(this);
1853 /* wake up any process waiting for sending */
1854 iucv_sock_wake_msglim(sk);
1855 }
Jennifer Hunt561e0362007-05-04 12:22:07 -07001856 }
1857
Heiko Carstensda99f052007-05-04 12:23:27 -07001858 if (sk->sk_state == IUCV_CLOSING) {
Jennifer Hunt561e0362007-05-04 12:22:07 -07001859 if (skb_queue_empty(&iucv_sk(sk)->send_skb_q)) {
1860 sk->sk_state = IUCV_CLOSED;
1861 sk->sk_state_change(sk);
1862 }
1863 }
Ursula Braun7d316b92012-02-08 00:19:45 +00001864 bh_unlock_sock(sk);
Jennifer Hunt561e0362007-05-04 12:22:07 -07001865
Jennifer Hunteac37312007-02-08 13:51:54 -08001866}
1867
1868static void iucv_callback_connrej(struct iucv_path *path, u8 ipuser[16])
1869{
1870 struct sock *sk = path->private;
1871
Ursula Braun7d316b92012-02-08 00:19:45 +00001872 if (sk->sk_state == IUCV_CLOSED)
1873 return;
1874
1875 bh_lock_sock(sk);
1876 iucv_sever_path(sk, 1);
Ursula Braunaac63992011-12-19 22:56:31 +00001877 sk->sk_state = IUCV_DISCONN;
Jennifer Hunteac37312007-02-08 13:51:54 -08001878
1879 sk->sk_state_change(sk);
Ursula Braun7d316b92012-02-08 00:19:45 +00001880 bh_unlock_sock(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -08001881}
1882
Hendrik Brueckneraf88b522009-04-21 23:26:21 +00001883/* called if the other communication side shuts down its RECV direction;
1884 * in turn, the callback sets SEND_SHUTDOWN to disable sending of data.
1885 */
1886static void iucv_callback_shutdown(struct iucv_path *path, u8 ipuser[16])
1887{
1888 struct sock *sk = path->private;
1889
1890 bh_lock_sock(sk);
1891 if (sk->sk_state != IUCV_CLOSED) {
1892 sk->sk_shutdown |= SEND_SHUTDOWN;
1893 sk->sk_state_change(sk);
1894 }
1895 bh_unlock_sock(sk);
1896}
1897
Ursula Braun3881ac42011-08-08 01:33:54 +00001898/***************** HiperSockets transport callbacks ********************/
1899static void afiucv_swap_src_dest(struct sk_buff *skb)
1900{
1901 struct af_iucv_trans_hdr *trans_hdr =
1902 (struct af_iucv_trans_hdr *)skb->data;
1903 char tmpID[8];
1904 char tmpName[8];
1905
1906 ASCEBC(trans_hdr->destUserID, sizeof(trans_hdr->destUserID));
1907 ASCEBC(trans_hdr->destAppName, sizeof(trans_hdr->destAppName));
1908 ASCEBC(trans_hdr->srcUserID, sizeof(trans_hdr->srcUserID));
1909 ASCEBC(trans_hdr->srcAppName, sizeof(trans_hdr->srcAppName));
1910 memcpy(tmpID, trans_hdr->srcUserID, 8);
1911 memcpy(tmpName, trans_hdr->srcAppName, 8);
1912 memcpy(trans_hdr->srcUserID, trans_hdr->destUserID, 8);
1913 memcpy(trans_hdr->srcAppName, trans_hdr->destAppName, 8);
1914 memcpy(trans_hdr->destUserID, tmpID, 8);
1915 memcpy(trans_hdr->destAppName, tmpName, 8);
1916 skb_push(skb, ETH_HLEN);
1917 memset(skb->data, 0, ETH_HLEN);
1918}
1919
1920/**
1921 * afiucv_hs_callback_syn - react on received SYN
1922 **/
1923static int afiucv_hs_callback_syn(struct sock *sk, struct sk_buff *skb)
1924{
1925 struct sock *nsk;
1926 struct iucv_sock *iucv, *niucv;
1927 struct af_iucv_trans_hdr *trans_hdr;
1928 int err;
1929
1930 iucv = iucv_sk(sk);
1931 trans_hdr = (struct af_iucv_trans_hdr *)skb->data;
1932 if (!iucv) {
1933 /* no sock - connection refused */
1934 afiucv_swap_src_dest(skb);
1935 trans_hdr->flags = AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN;
1936 err = dev_queue_xmit(skb);
1937 goto out;
1938 }
1939
1940 nsk = iucv_sock_alloc(NULL, sk->sk_type, GFP_ATOMIC);
1941 bh_lock_sock(sk);
1942 if ((sk->sk_state != IUCV_LISTEN) ||
1943 sk_acceptq_is_full(sk) ||
1944 !nsk) {
1945 /* error on server socket - connection refused */
1946 if (nsk)
1947 sk_free(nsk);
1948 afiucv_swap_src_dest(skb);
1949 trans_hdr->flags = AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN;
1950 err = dev_queue_xmit(skb);
1951 bh_unlock_sock(sk);
1952 goto out;
1953 }
1954
1955 niucv = iucv_sk(nsk);
1956 iucv_sock_init(nsk, sk);
1957 niucv->transport = AF_IUCV_TRANS_HIPER;
1958 niucv->msglimit = iucv->msglimit;
1959 if (!trans_hdr->window)
1960 niucv->msglimit_peer = IUCV_HIPER_MSGLIM_DEFAULT;
1961 else
1962 niucv->msglimit_peer = trans_hdr->window;
1963 memcpy(niucv->dst_name, trans_hdr->srcAppName, 8);
1964 memcpy(niucv->dst_user_id, trans_hdr->srcUserID, 8);
1965 memcpy(niucv->src_name, iucv->src_name, 8);
1966 memcpy(niucv->src_user_id, iucv->src_user_id, 8);
1967 nsk->sk_bound_dev_if = sk->sk_bound_dev_if;
Ursula Braun800c5eb2012-02-08 00:19:47 +00001968 niucv->hs_dev = iucv->hs_dev;
1969 dev_hold(niucv->hs_dev);
Ursula Braun3881ac42011-08-08 01:33:54 +00001970 afiucv_swap_src_dest(skb);
1971 trans_hdr->flags = AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_ACK;
1972 trans_hdr->window = niucv->msglimit;
1973 /* if receiver acks the xmit connection is established */
1974 err = dev_queue_xmit(skb);
1975 if (!err) {
1976 iucv_accept_enqueue(sk, nsk);
1977 nsk->sk_state = IUCV_CONNECTED;
David S. Miller676d2362014-04-11 16:15:36 -04001978 sk->sk_data_ready(sk);
Ursula Braun3881ac42011-08-08 01:33:54 +00001979 } else
1980 iucv_sock_kill(nsk);
1981 bh_unlock_sock(sk);
1982
1983out:
1984 return NET_RX_SUCCESS;
1985}
1986
1987/**
1988 * afiucv_hs_callback_synack() - react on received SYN-ACK
1989 **/
1990static int afiucv_hs_callback_synack(struct sock *sk, struct sk_buff *skb)
1991{
1992 struct iucv_sock *iucv = iucv_sk(sk);
1993 struct af_iucv_trans_hdr *trans_hdr =
1994 (struct af_iucv_trans_hdr *)skb->data;
1995
1996 if (!iucv)
1997 goto out;
1998 if (sk->sk_state != IUCV_BOUND)
1999 goto out;
2000 bh_lock_sock(sk);
2001 iucv->msglimit_peer = trans_hdr->window;
2002 sk->sk_state = IUCV_CONNECTED;
2003 sk->sk_state_change(sk);
2004 bh_unlock_sock(sk);
2005out:
2006 kfree_skb(skb);
2007 return NET_RX_SUCCESS;
2008}
2009
2010/**
2011 * afiucv_hs_callback_synfin() - react on received SYN_FIN
2012 **/
2013static int afiucv_hs_callback_synfin(struct sock *sk, struct sk_buff *skb)
2014{
2015 struct iucv_sock *iucv = iucv_sk(sk);
2016
2017 if (!iucv)
2018 goto out;
2019 if (sk->sk_state != IUCV_BOUND)
2020 goto out;
2021 bh_lock_sock(sk);
2022 sk->sk_state = IUCV_DISCONN;
2023 sk->sk_state_change(sk);
2024 bh_unlock_sock(sk);
2025out:
2026 kfree_skb(skb);
2027 return NET_RX_SUCCESS;
2028}
2029
2030/**
2031 * afiucv_hs_callback_fin() - react on received FIN
2032 **/
2033static int afiucv_hs_callback_fin(struct sock *sk, struct sk_buff *skb)
2034{
2035 struct iucv_sock *iucv = iucv_sk(sk);
2036
2037 /* other end of connection closed */
Ursula Braun800c5eb2012-02-08 00:19:47 +00002038 if (!iucv)
2039 goto out;
2040 bh_lock_sock(sk);
2041 if (sk->sk_state == IUCV_CONNECTED) {
Ursula Braunaac63992011-12-19 22:56:31 +00002042 sk->sk_state = IUCV_DISCONN;
Ursula Braun3881ac42011-08-08 01:33:54 +00002043 sk->sk_state_change(sk);
Ursula Braun3881ac42011-08-08 01:33:54 +00002044 }
Ursula Braun800c5eb2012-02-08 00:19:47 +00002045 bh_unlock_sock(sk);
2046out:
Ursula Braun3881ac42011-08-08 01:33:54 +00002047 kfree_skb(skb);
2048 return NET_RX_SUCCESS;
2049}
2050
2051/**
2052 * afiucv_hs_callback_win() - react on received WIN
2053 **/
2054static int afiucv_hs_callback_win(struct sock *sk, struct sk_buff *skb)
2055{
2056 struct iucv_sock *iucv = iucv_sk(sk);
2057 struct af_iucv_trans_hdr *trans_hdr =
2058 (struct af_iucv_trans_hdr *)skb->data;
2059
2060 if (!iucv)
2061 return NET_RX_SUCCESS;
2062
2063 if (sk->sk_state != IUCV_CONNECTED)
2064 return NET_RX_SUCCESS;
2065
2066 atomic_sub(trans_hdr->window, &iucv->msg_sent);
2067 iucv_sock_wake_msglim(sk);
2068 return NET_RX_SUCCESS;
2069}
2070
2071/**
2072 * afiucv_hs_callback_rx() - react on received data
2073 **/
2074static int afiucv_hs_callback_rx(struct sock *sk, struct sk_buff *skb)
2075{
2076 struct iucv_sock *iucv = iucv_sk(sk);
2077
2078 if (!iucv) {
2079 kfree_skb(skb);
2080 return NET_RX_SUCCESS;
2081 }
2082
2083 if (sk->sk_state != IUCV_CONNECTED) {
2084 kfree_skb(skb);
2085 return NET_RX_SUCCESS;
2086 }
2087
Ursula Braun82492a32012-03-07 02:06:24 +00002088 if (sk->sk_shutdown & RCV_SHUTDOWN) {
2089 kfree_skb(skb);
2090 return NET_RX_SUCCESS;
2091 }
2092
Ursula Braun3881ac42011-08-08 01:33:54 +00002093 /* write stuff from iucv_msg to skb cb */
Ursula Braun82492a32012-03-07 02:06:24 +00002094 if (skb->len < sizeof(struct af_iucv_trans_hdr)) {
Ursula Braun3881ac42011-08-08 01:33:54 +00002095 kfree_skb(skb);
2096 return NET_RX_SUCCESS;
2097 }
2098 skb_pull(skb, sizeof(struct af_iucv_trans_hdr));
2099 skb_reset_transport_header(skb);
2100 skb_reset_network_header(skb);
Ursula Braunf9c41a62013-04-07 22:19:26 +00002101 IUCV_SKB_CB(skb)->offset = 0;
Ursula Braun3881ac42011-08-08 01:33:54 +00002102 spin_lock(&iucv->message_q.lock);
2103 if (skb_queue_empty(&iucv->backlog_skb_q)) {
2104 if (sock_queue_rcv_skb(sk, skb)) {
2105 /* handle rcv queue full */
2106 skb_queue_tail(&iucv->backlog_skb_q, skb);
2107 }
2108 } else
2109 skb_queue_tail(&iucv_sk(sk)->backlog_skb_q, skb);
2110 spin_unlock(&iucv->message_q.lock);
2111 return NET_RX_SUCCESS;
2112}
2113
2114/**
2115 * afiucv_hs_rcv() - base function for arriving data through HiperSockets
2116 * transport
2117 * called from netif RX softirq
2118 **/
2119static int afiucv_hs_rcv(struct sk_buff *skb, struct net_device *dev,
2120 struct packet_type *pt, struct net_device *orig_dev)
2121{
Ursula Braun3881ac42011-08-08 01:33:54 +00002122 struct sock *sk;
2123 struct iucv_sock *iucv;
2124 struct af_iucv_trans_hdr *trans_hdr;
2125 char nullstring[8];
2126 int err = 0;
2127
2128 skb_pull(skb, ETH_HLEN);
2129 trans_hdr = (struct af_iucv_trans_hdr *)skb->data;
2130 EBCASC(trans_hdr->destAppName, sizeof(trans_hdr->destAppName));
2131 EBCASC(trans_hdr->destUserID, sizeof(trans_hdr->destUserID));
2132 EBCASC(trans_hdr->srcAppName, sizeof(trans_hdr->srcAppName));
2133 EBCASC(trans_hdr->srcUserID, sizeof(trans_hdr->srcUserID));
2134 memset(nullstring, 0, sizeof(nullstring));
2135 iucv = NULL;
2136 sk = NULL;
2137 read_lock(&iucv_sk_list.lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08002138 sk_for_each(sk, &iucv_sk_list.head) {
Ursula Braun3881ac42011-08-08 01:33:54 +00002139 if (trans_hdr->flags == AF_IUCV_FLAG_SYN) {
2140 if ((!memcmp(&iucv_sk(sk)->src_name,
2141 trans_hdr->destAppName, 8)) &&
2142 (!memcmp(&iucv_sk(sk)->src_user_id,
2143 trans_hdr->destUserID, 8)) &&
2144 (!memcmp(&iucv_sk(sk)->dst_name, nullstring, 8)) &&
2145 (!memcmp(&iucv_sk(sk)->dst_user_id,
2146 nullstring, 8))) {
2147 iucv = iucv_sk(sk);
2148 break;
2149 }
2150 } else {
2151 if ((!memcmp(&iucv_sk(sk)->src_name,
2152 trans_hdr->destAppName, 8)) &&
2153 (!memcmp(&iucv_sk(sk)->src_user_id,
2154 trans_hdr->destUserID, 8)) &&
2155 (!memcmp(&iucv_sk(sk)->dst_name,
2156 trans_hdr->srcAppName, 8)) &&
2157 (!memcmp(&iucv_sk(sk)->dst_user_id,
2158 trans_hdr->srcUserID, 8))) {
2159 iucv = iucv_sk(sk);
2160 break;
2161 }
2162 }
2163 }
2164 read_unlock(&iucv_sk_list.lock);
2165 if (!iucv)
2166 sk = NULL;
2167
2168 /* no sock
2169 how should we send with no sock
2170 1) send without sock no send rc checking?
2171 2) introduce default sock to handle this cases
2172
2173 SYN -> send SYN|ACK in good case, send SYN|FIN in bad case
2174 data -> send FIN
2175 SYN|ACK, SYN|FIN, FIN -> no action? */
2176
2177 switch (trans_hdr->flags) {
2178 case AF_IUCV_FLAG_SYN:
2179 /* connect request */
2180 err = afiucv_hs_callback_syn(sk, skb);
2181 break;
2182 case (AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_ACK):
2183 /* connect request confirmed */
2184 err = afiucv_hs_callback_synack(sk, skb);
2185 break;
2186 case (AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN):
2187 /* connect request refused */
2188 err = afiucv_hs_callback_synfin(sk, skb);
2189 break;
2190 case (AF_IUCV_FLAG_FIN):
2191 /* close request */
2192 err = afiucv_hs_callback_fin(sk, skb);
2193 break;
2194 case (AF_IUCV_FLAG_WIN):
2195 err = afiucv_hs_callback_win(sk, skb);
Ursula Braun800c5eb2012-02-08 00:19:47 +00002196 if (skb->len == sizeof(struct af_iucv_trans_hdr)) {
2197 kfree_skb(skb);
2198 break;
2199 }
Ursula Braun82492a32012-03-07 02:06:24 +00002200 /* fall through and receive non-zero length data */
2201 case (AF_IUCV_FLAG_SHT):
2202 /* shutdown request */
2203 /* fall through and receive zero length data */
Ursula Braun3881ac42011-08-08 01:33:54 +00002204 case 0:
2205 /* plain data frame */
Ursula Braunf9c41a62013-04-07 22:19:26 +00002206 IUCV_SKB_CB(skb)->class = trans_hdr->iucv_hdr.class;
Ursula Braun3881ac42011-08-08 01:33:54 +00002207 err = afiucv_hs_callback_rx(sk, skb);
2208 break;
2209 default:
2210 ;
2211 }
2212
2213 return err;
2214}
2215
2216/**
2217 * afiucv_hs_callback_txnotify() - handle send notifcations from HiperSockets
2218 * transport
2219 **/
2220static void afiucv_hs_callback_txnotify(struct sk_buff *skb,
2221 enum iucv_tx_notify n)
2222{
2223 struct sock *isk = skb->sk;
2224 struct sock *sk = NULL;
2225 struct iucv_sock *iucv = NULL;
2226 struct sk_buff_head *list;
2227 struct sk_buff *list_skb;
Ursula Braun800c5eb2012-02-08 00:19:47 +00002228 struct sk_buff *nskb;
Ursula Braun3881ac42011-08-08 01:33:54 +00002229 unsigned long flags;
Ursula Braun3881ac42011-08-08 01:33:54 +00002230
Ursula Braun800c5eb2012-02-08 00:19:47 +00002231 read_lock_irqsave(&iucv_sk_list.lock, flags);
Sasha Levinb67bfe02013-02-27 17:06:00 -08002232 sk_for_each(sk, &iucv_sk_list.head)
Ursula Braun3881ac42011-08-08 01:33:54 +00002233 if (sk == isk) {
2234 iucv = iucv_sk(sk);
2235 break;
2236 }
Ursula Braun800c5eb2012-02-08 00:19:47 +00002237 read_unlock_irqrestore(&iucv_sk_list.lock, flags);
Ursula Braun3881ac42011-08-08 01:33:54 +00002238
Ursula Braun800c5eb2012-02-08 00:19:47 +00002239 if (!iucv || sock_flag(sk, SOCK_ZAPPED))
Ursula Braun3881ac42011-08-08 01:33:54 +00002240 return;
2241
Ursula Braun3881ac42011-08-08 01:33:54 +00002242 list = &iucv->send_skb_q;
Ursula Braun800c5eb2012-02-08 00:19:47 +00002243 spin_lock_irqsave(&list->lock, flags);
Ursula Braun3881ac42011-08-08 01:33:54 +00002244 if (skb_queue_empty(list))
2245 goto out_unlock;
Ursula Braun800c5eb2012-02-08 00:19:47 +00002246 list_skb = list->next;
2247 nskb = list_skb->next;
Ursula Braun3881ac42011-08-08 01:33:54 +00002248 while (list_skb != (struct sk_buff *)list) {
2249 if (skb_shinfo(list_skb) == skb_shinfo(skb)) {
Ursula Braun3881ac42011-08-08 01:33:54 +00002250 switch (n) {
2251 case TX_NOTIFY_OK:
Ursula Braun800c5eb2012-02-08 00:19:47 +00002252 __skb_unlink(list_skb, list);
2253 kfree_skb(list_skb);
Ursula Braun3881ac42011-08-08 01:33:54 +00002254 iucv_sock_wake_msglim(sk);
Ursula Braun3881ac42011-08-08 01:33:54 +00002255 break;
2256 case TX_NOTIFY_PENDING:
2257 atomic_inc(&iucv->pendings);
2258 break;
2259 case TX_NOTIFY_DELAYED_OK:
Ursula Braun800c5eb2012-02-08 00:19:47 +00002260 __skb_unlink(list_skb, list);
Ursula Braun3881ac42011-08-08 01:33:54 +00002261 atomic_dec(&iucv->pendings);
2262 if (atomic_read(&iucv->pendings) <= 0)
2263 iucv_sock_wake_msglim(sk);
Ursula Braun800c5eb2012-02-08 00:19:47 +00002264 kfree_skb(list_skb);
Ursula Braun3881ac42011-08-08 01:33:54 +00002265 break;
2266 case TX_NOTIFY_UNREACHABLE:
2267 case TX_NOTIFY_DELAYED_UNREACHABLE:
2268 case TX_NOTIFY_TPQFULL: /* not yet used */
2269 case TX_NOTIFY_GENERALERROR:
2270 case TX_NOTIFY_DELAYED_GENERALERROR:
Ursula Braun800c5eb2012-02-08 00:19:47 +00002271 __skb_unlink(list_skb, list);
2272 kfree_skb(list_skb);
2273 if (sk->sk_state == IUCV_CONNECTED) {
2274 sk->sk_state = IUCV_DISCONN;
2275 sk->sk_state_change(sk);
2276 }
Ursula Braun3881ac42011-08-08 01:33:54 +00002277 break;
2278 }
2279 break;
2280 }
Ursula Braun800c5eb2012-02-08 00:19:47 +00002281 list_skb = nskb;
2282 nskb = nskb->next;
Ursula Braun3881ac42011-08-08 01:33:54 +00002283 }
Ursula Braun800c5eb2012-02-08 00:19:47 +00002284out_unlock:
Ursula Braun3881ac42011-08-08 01:33:54 +00002285 spin_unlock_irqrestore(&list->lock, flags);
2286
Ursula Braun42bd48e2011-12-19 22:56:28 +00002287 if (sk->sk_state == IUCV_CLOSING) {
2288 if (skb_queue_empty(&iucv_sk(sk)->send_skb_q)) {
2289 sk->sk_state = IUCV_CLOSED;
2290 sk->sk_state_change(sk);
2291 }
2292 }
2293
Ursula Braun3881ac42011-08-08 01:33:54 +00002294}
Ursula Braun9fbd87d2012-03-07 02:06:23 +00002295
2296/*
2297 * afiucv_netdev_event: handle netdev notifier chain events
2298 */
2299static int afiucv_netdev_event(struct notifier_block *this,
2300 unsigned long event, void *ptr)
2301{
Jiri Pirko351638e2013-05-28 01:30:21 +00002302 struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
Ursula Braun9fbd87d2012-03-07 02:06:23 +00002303 struct sock *sk;
2304 struct iucv_sock *iucv;
2305
2306 switch (event) {
2307 case NETDEV_REBOOT:
2308 case NETDEV_GOING_DOWN:
Sasha Levinb67bfe02013-02-27 17:06:00 -08002309 sk_for_each(sk, &iucv_sk_list.head) {
Ursula Braun9fbd87d2012-03-07 02:06:23 +00002310 iucv = iucv_sk(sk);
2311 if ((iucv->hs_dev == event_dev) &&
2312 (sk->sk_state == IUCV_CONNECTED)) {
2313 if (event == NETDEV_GOING_DOWN)
2314 iucv_send_ctrl(sk, AF_IUCV_FLAG_FIN);
2315 sk->sk_state = IUCV_DISCONN;
2316 sk->sk_state_change(sk);
2317 }
2318 }
2319 break;
2320 case NETDEV_DOWN:
2321 case NETDEV_UNREGISTER:
2322 default:
2323 break;
2324 }
2325 return NOTIFY_DONE;
2326}
2327
2328static struct notifier_block afiucv_netdev_notifier = {
2329 .notifier_call = afiucv_netdev_event,
2330};
2331
Alexey Dobriyan5708e862009-09-14 12:23:23 +00002332static const struct proto_ops iucv_sock_ops = {
Jennifer Hunteac37312007-02-08 13:51:54 -08002333 .family = PF_IUCV,
2334 .owner = THIS_MODULE,
2335 .release = iucv_sock_release,
2336 .bind = iucv_sock_bind,
2337 .connect = iucv_sock_connect,
2338 .listen = iucv_sock_listen,
2339 .accept = iucv_sock_accept,
2340 .getname = iucv_sock_getname,
2341 .sendmsg = iucv_sock_sendmsg,
2342 .recvmsg = iucv_sock_recvmsg,
2343 .poll = iucv_sock_poll,
2344 .ioctl = sock_no_ioctl,
2345 .mmap = sock_no_mmap,
2346 .socketpair = sock_no_socketpair,
2347 .shutdown = iucv_sock_shutdown,
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00002348 .setsockopt = iucv_sock_setsockopt,
2349 .getsockopt = iucv_sock_getsockopt,
Jennifer Hunteac37312007-02-08 13:51:54 -08002350};
2351
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00002352static const struct net_proto_family iucv_sock_family_ops = {
Jennifer Hunteac37312007-02-08 13:51:54 -08002353 .family = AF_IUCV,
2354 .owner = THIS_MODULE,
2355 .create = iucv_sock_create,
2356};
2357
Ursula Braun3881ac42011-08-08 01:33:54 +00002358static struct packet_type iucv_packet_type = {
2359 .type = cpu_to_be16(ETH_P_AF_IUCV),
2360 .func = afiucv_hs_rcv,
2361};
2362
2363static int afiucv_iucv_init(void)
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00002364{
2365 int err;
2366
2367 err = pr_iucv->iucv_register(&af_iucv_handler, 0);
2368 if (err)
2369 goto out;
2370 /* establish dummy device */
2371 af_iucv_driver.bus = pr_iucv->bus;
2372 err = driver_register(&af_iucv_driver);
2373 if (err)
2374 goto out_iucv;
2375 af_iucv_dev = kzalloc(sizeof(struct device), GFP_KERNEL);
2376 if (!af_iucv_dev) {
2377 err = -ENOMEM;
2378 goto out_driver;
2379 }
2380 dev_set_name(af_iucv_dev, "af_iucv");
2381 af_iucv_dev->bus = pr_iucv->bus;
2382 af_iucv_dev->parent = pr_iucv->root;
2383 af_iucv_dev->release = (void (*)(struct device *))kfree;
2384 af_iucv_dev->driver = &af_iucv_driver;
2385 err = device_register(af_iucv_dev);
2386 if (err)
2387 goto out_driver;
2388 return 0;
2389
2390out_driver:
2391 driver_unregister(&af_iucv_driver);
2392out_iucv:
2393 pr_iucv->iucv_unregister(&af_iucv_handler, 0);
2394out:
2395 return err;
2396}
2397
Heiko Carstensda99f052007-05-04 12:23:27 -07002398static int __init afiucv_init(void)
Jennifer Hunteac37312007-02-08 13:51:54 -08002399{
2400 int err;
2401
Ursula Braun3881ac42011-08-08 01:33:54 +00002402 if (MACHINE_IS_VM) {
2403 cpcmd("QUERY USERID", iucv_userid, sizeof(iucv_userid), &err);
2404 if (unlikely(err)) {
2405 WARN_ON(err);
2406 err = -EPROTONOSUPPORT;
2407 goto out;
2408 }
Jennifer Hunteac37312007-02-08 13:51:54 -08002409
Ursula Braun3881ac42011-08-08 01:33:54 +00002410 pr_iucv = try_then_request_module(symbol_get(iucv_if), "iucv");
2411 if (!pr_iucv) {
2412 printk(KERN_WARNING "iucv_if lookup failed\n");
2413 memset(&iucv_userid, 0, sizeof(iucv_userid));
2414 }
2415 } else {
2416 memset(&iucv_userid, 0, sizeof(iucv_userid));
2417 pr_iucv = NULL;
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00002418 }
2419
Jennifer Hunteac37312007-02-08 13:51:54 -08002420 err = proto_register(&iucv_proto, 0);
2421 if (err)
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00002422 goto out;
Jennifer Hunteac37312007-02-08 13:51:54 -08002423 err = sock_register(&iucv_sock_family_ops);
2424 if (err)
2425 goto out_proto;
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00002426
Ursula Braun3881ac42011-08-08 01:33:54 +00002427 if (pr_iucv) {
2428 err = afiucv_iucv_init();
2429 if (err)
2430 goto out_sock;
Ursula Braun9fbd87d2012-03-07 02:06:23 +00002431 } else
2432 register_netdevice_notifier(&afiucv_netdev_notifier);
Ursula Braun3881ac42011-08-08 01:33:54 +00002433 dev_add_pack(&iucv_packet_type);
Jennifer Hunteac37312007-02-08 13:51:54 -08002434 return 0;
2435
Ursula Braunc23cad92009-06-16 10:30:44 +02002436out_sock:
2437 sock_unregister(PF_IUCV);
Jennifer Hunteac37312007-02-08 13:51:54 -08002438out_proto:
2439 proto_unregister(&iucv_proto);
Jennifer Hunteac37312007-02-08 13:51:54 -08002440out:
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00002441 if (pr_iucv)
2442 symbol_put(iucv_if);
Jennifer Hunteac37312007-02-08 13:51:54 -08002443 return err;
2444}
2445
2446static void __exit afiucv_exit(void)
2447{
Ursula Braun3881ac42011-08-08 01:33:54 +00002448 if (pr_iucv) {
2449 device_unregister(af_iucv_dev);
2450 driver_unregister(&af_iucv_driver);
2451 pr_iucv->iucv_unregister(&af_iucv_handler, 0);
2452 symbol_put(iucv_if);
Ursula Braun9fbd87d2012-03-07 02:06:23 +00002453 } else
2454 unregister_netdevice_notifier(&afiucv_netdev_notifier);
Ursula Braun3881ac42011-08-08 01:33:54 +00002455 dev_remove_pack(&iucv_packet_type);
Jennifer Hunteac37312007-02-08 13:51:54 -08002456 sock_unregister(PF_IUCV);
2457 proto_unregister(&iucv_proto);
Jennifer Hunteac37312007-02-08 13:51:54 -08002458}
2459
2460module_init(afiucv_init);
2461module_exit(afiucv_exit);
2462
2463MODULE_AUTHOR("Jennifer Hunt <jenhunt@us.ibm.com>");
2464MODULE_DESCRIPTION("IUCV Sockets ver " VERSION);
2465MODULE_VERSION(VERSION);
2466MODULE_LICENSE("GPL");
2467MODULE_ALIAS_NETPROTO(PF_IUCV);
Ursula Braun3881ac42011-08-08 01:33:54 +00002468