blob: c2dfc32eb9f21a2b15898f166ab13a297e989dea [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>
Paul Moore02f06912016-06-07 10:30:53 -040025#include <linux/security.h>
Jennifer Hunteac37312007-02-08 13:51:54 -080026#include <net/sock.h>
27#include <asm/ebcdic.h>
28#include <asm/cpcmd.h>
29#include <linux/kmod.h>
30
Jennifer Hunteac37312007-02-08 13:51:54 -080031#include <net/iucv/af_iucv.h>
32
Ursula Braun3881ac42011-08-08 01:33:54 +000033#define VERSION "1.2"
Jennifer Hunteac37312007-02-08 13:51:54 -080034
35static char iucv_userid[80];
36
Alexey Dobriyan5708e862009-09-14 12:23:23 +000037static const struct proto_ops iucv_sock_ops;
Jennifer Hunteac37312007-02-08 13:51:54 -080038
39static struct proto iucv_proto = {
40 .name = "AF_IUCV",
41 .owner = THIS_MODULE,
42 .obj_size = sizeof(struct iucv_sock),
43};
44
Frank Blaschka6fcd61f2011-08-08 01:33:51 +000045static struct iucv_interface *pr_iucv;
46
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +000047/* special AF_IUCV IPRM messages */
48static const u8 iprm_shutdown[8] =
49 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01};
50
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +000051#define TRGCLS_SIZE (sizeof(((struct iucv_message *)0)->class))
52
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +000053#define __iucv_sock_wait(sk, condition, timeo, ret) \
54do { \
55 DEFINE_WAIT(__wait); \
56 long __timeo = timeo; \
57 ret = 0; \
Eric Dumazetaa395142010-04-20 13:03:51 +000058 prepare_to_wait(sk_sleep(sk), &__wait, TASK_INTERRUPTIBLE); \
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +000059 while (!(condition)) { \
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +000060 if (!__timeo) { \
61 ret = -EAGAIN; \
62 break; \
63 } \
64 if (signal_pending(current)) { \
65 ret = sock_intr_errno(__timeo); \
66 break; \
67 } \
68 release_sock(sk); \
69 __timeo = schedule_timeout(__timeo); \
70 lock_sock(sk); \
71 ret = sock_error(sk); \
72 if (ret) \
73 break; \
74 } \
Eric Dumazetaa395142010-04-20 13:03:51 +000075 finish_wait(sk_sleep(sk), &__wait); \
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +000076} while (0)
77
78#define iucv_sock_wait(sk, condition, timeo) \
79({ \
80 int __ret = 0; \
81 if (!(condition)) \
82 __iucv_sock_wait(sk, condition, timeo, __ret); \
83 __ret; \
84})
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +000085
Heiko Carstens57f20442007-10-08 02:02:52 -070086static void iucv_sock_kill(struct sock *sk);
87static void iucv_sock_close(struct sock *sk);
Ursula Braun7d316b92012-02-08 00:19:45 +000088static void iucv_sever_path(struct sock *, int);
Heiko Carstens57f20442007-10-08 02:02:52 -070089
Ursula Braun3881ac42011-08-08 01:33:54 +000090static int afiucv_hs_rcv(struct sk_buff *skb, struct net_device *dev,
91 struct packet_type *pt, struct net_device *orig_dev);
92static int afiucv_hs_send(struct iucv_message *imsg, struct sock *sock,
93 struct sk_buff *skb, u8 flags);
94static void afiucv_hs_callback_txnotify(struct sk_buff *, enum iucv_tx_notify);
95
Jennifer Hunteac37312007-02-08 13:51:54 -080096/* Call Back functions */
97static void iucv_callback_rx(struct iucv_path *, struct iucv_message *);
98static void iucv_callback_txdone(struct iucv_path *, struct iucv_message *);
Ursula Braun91e60eb2015-09-18 16:06:52 +020099static void iucv_callback_connack(struct iucv_path *, u8 *);
100static int iucv_callback_connreq(struct iucv_path *, u8 *, u8 *);
101static void iucv_callback_connrej(struct iucv_path *, u8 *);
102static void iucv_callback_shutdown(struct iucv_path *, u8 *);
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);
Herbert Xu1ce0bf52015-11-26 13:55:39 +0800307 if (skwq_has_sleeper(wq))
Eric Dumazet43815482010-04-29 11:01:49 +0000308 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{
Paul Moore02f06912016-06-07 10:30:53 -0400534 if (parent) {
Jennifer Hunteac37312007-02-08 13:51:54 -0800535 sk->sk_type = parent->sk_type;
Paul Moore02f06912016-06-07 10:30:53 -0400536 security_sk_clone(parent, sk);
537 }
Jennifer Hunteac37312007-02-08 13:51:54 -0800538}
539
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500540static struct sock *iucv_sock_alloc(struct socket *sock, int proto, gfp_t prio, int kern)
Jennifer Hunteac37312007-02-08 13:51:54 -0800541{
542 struct sock *sk;
Ursula Braun493d3972011-08-08 01:33:52 +0000543 struct iucv_sock *iucv;
Jennifer Hunteac37312007-02-08 13:51:54 -0800544
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500545 sk = sk_alloc(&init_net, PF_IUCV, prio, &iucv_proto, kern);
Jennifer Hunteac37312007-02-08 13:51:54 -0800546 if (!sk)
547 return NULL;
Ursula Braun493d3972011-08-08 01:33:52 +0000548 iucv = iucv_sk(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800549
550 sock_init_data(sock, sk);
Ursula Braun493d3972011-08-08 01:33:52 +0000551 INIT_LIST_HEAD(&iucv->accept_q);
552 spin_lock_init(&iucv->accept_q_lock);
553 skb_queue_head_init(&iucv->send_skb_q);
554 INIT_LIST_HEAD(&iucv->message_q.list);
555 spin_lock_init(&iucv->message_q.lock);
556 skb_queue_head_init(&iucv->backlog_skb_q);
557 iucv->send_tag = 0;
Ursula Braun3881ac42011-08-08 01:33:54 +0000558 atomic_set(&iucv->pendings, 0);
Ursula Braun493d3972011-08-08 01:33:52 +0000559 iucv->flags = 0;
Ursula Braun3881ac42011-08-08 01:33:54 +0000560 iucv->msglimit = 0;
561 atomic_set(&iucv->msg_sent, 0);
562 atomic_set(&iucv->msg_recv, 0);
Ursula Braun493d3972011-08-08 01:33:52 +0000563 iucv->path = NULL;
Ursula Braun3881ac42011-08-08 01:33:54 +0000564 iucv->sk_txnotify = afiucv_hs_callback_txnotify;
Ursula Braun493d3972011-08-08 01:33:52 +0000565 memset(&iucv->src_user_id , 0, 32);
Ursula Braun3881ac42011-08-08 01:33:54 +0000566 if (pr_iucv)
567 iucv->transport = AF_IUCV_TRANS_IUCV;
568 else
569 iucv->transport = AF_IUCV_TRANS_HIPER;
Jennifer Hunteac37312007-02-08 13:51:54 -0800570
571 sk->sk_destruct = iucv_sock_destruct;
572 sk->sk_sndtimeo = IUCV_CONN_TIMEOUT;
573 sk->sk_allocation = GFP_DMA;
574
575 sock_reset_flag(sk, SOCK_ZAPPED);
576
577 sk->sk_protocol = proto;
578 sk->sk_state = IUCV_OPEN;
579
Jennifer Hunteac37312007-02-08 13:51:54 -0800580 iucv_sock_link(&iucv_sk_list, sk);
581 return sk;
582}
583
584/* Create an IUCV socket */
Eric Paris3f378b62009-11-05 22:18:14 -0800585static int iucv_sock_create(struct net *net, struct socket *sock, int protocol,
586 int kern)
Jennifer Hunteac37312007-02-08 13:51:54 -0800587{
588 struct sock *sk;
589
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +0000590 if (protocol && protocol != PF_IUCV)
591 return -EPROTONOSUPPORT;
Jennifer Hunteac37312007-02-08 13:51:54 -0800592
593 sock->state = SS_UNCONNECTED;
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +0000594
595 switch (sock->type) {
596 case SOCK_STREAM:
597 sock->ops = &iucv_sock_ops;
598 break;
599 case SOCK_SEQPACKET:
600 /* currently, proto ops can handle both sk types */
601 sock->ops = &iucv_sock_ops;
602 break;
603 default:
604 return -ESOCKTNOSUPPORT;
605 }
Jennifer Hunteac37312007-02-08 13:51:54 -0800606
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500607 sk = iucv_sock_alloc(sock, protocol, GFP_KERNEL, kern);
Jennifer Hunteac37312007-02-08 13:51:54 -0800608 if (!sk)
609 return -ENOMEM;
610
611 iucv_sock_init(sk, NULL);
612
613 return 0;
614}
615
616void iucv_sock_link(struct iucv_sock_list *l, struct sock *sk)
617{
618 write_lock_bh(&l->lock);
619 sk_add_node(sk, &l->head);
620 write_unlock_bh(&l->lock);
621}
622
623void iucv_sock_unlink(struct iucv_sock_list *l, struct sock *sk)
624{
625 write_lock_bh(&l->lock);
626 sk_del_node_init(sk);
627 write_unlock_bh(&l->lock);
628}
629
630void iucv_accept_enqueue(struct sock *parent, struct sock *sk)
631{
Ursula Braunfebca282007-07-14 19:04:25 -0700632 unsigned long flags;
633 struct iucv_sock *par = iucv_sk(parent);
634
Jennifer Hunteac37312007-02-08 13:51:54 -0800635 sock_hold(sk);
Ursula Braunfebca282007-07-14 19:04:25 -0700636 spin_lock_irqsave(&par->accept_q_lock, flags);
637 list_add_tail(&iucv_sk(sk)->accept_q, &par->accept_q);
638 spin_unlock_irqrestore(&par->accept_q_lock, flags);
Jennifer Hunteac37312007-02-08 13:51:54 -0800639 iucv_sk(sk)->parent = parent;
Hendrik Brueckner49f5eba2009-10-14 22:54:55 +0000640 sk_acceptq_added(parent);
Jennifer Hunteac37312007-02-08 13:51:54 -0800641}
642
643void iucv_accept_unlink(struct sock *sk)
644{
Ursula Braunfebca282007-07-14 19:04:25 -0700645 unsigned long flags;
646 struct iucv_sock *par = iucv_sk(iucv_sk(sk)->parent);
647
648 spin_lock_irqsave(&par->accept_q_lock, flags);
Jennifer Hunteac37312007-02-08 13:51:54 -0800649 list_del_init(&iucv_sk(sk)->accept_q);
Ursula Braunfebca282007-07-14 19:04:25 -0700650 spin_unlock_irqrestore(&par->accept_q_lock, flags);
Hendrik Brueckner49f5eba2009-10-14 22:54:55 +0000651 sk_acceptq_removed(iucv_sk(sk)->parent);
Jennifer Hunteac37312007-02-08 13:51:54 -0800652 iucv_sk(sk)->parent = NULL;
653 sock_put(sk);
654}
655
656struct sock *iucv_accept_dequeue(struct sock *parent, struct socket *newsock)
657{
658 struct iucv_sock *isk, *n;
659 struct sock *sk;
660
Heiko Carstensda99f052007-05-04 12:23:27 -0700661 list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) {
Jennifer Hunteac37312007-02-08 13:51:54 -0800662 sk = (struct sock *) isk;
663 lock_sock(sk);
664
665 if (sk->sk_state == IUCV_CLOSED) {
Jennifer Hunteac37312007-02-08 13:51:54 -0800666 iucv_accept_unlink(sk);
Ursula Braunfebca282007-07-14 19:04:25 -0700667 release_sock(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800668 continue;
669 }
670
671 if (sk->sk_state == IUCV_CONNECTED ||
Ursula Braunaac63992011-12-19 22:56:31 +0000672 sk->sk_state == IUCV_DISCONN ||
Jennifer Hunteac37312007-02-08 13:51:54 -0800673 !newsock) {
674 iucv_accept_unlink(sk);
675 if (newsock)
676 sock_graft(sk, newsock);
677
Jennifer Hunteac37312007-02-08 13:51:54 -0800678 release_sock(sk);
679 return sk;
680 }
681
682 release_sock(sk);
683 }
684 return NULL;
685}
686
Philipp Hachtmann53a4b492014-05-28 10:22:28 +0200687static void __iucv_auto_name(struct iucv_sock *iucv)
688{
689 char name[12];
690
691 sprintf(name, "%08x", atomic_inc_return(&iucv_sk_list.autobind_name));
692 while (__iucv_get_sock_by_name(name)) {
693 sprintf(name, "%08x",
694 atomic_inc_return(&iucv_sk_list.autobind_name));
695 }
696 memcpy(iucv->src_name, name, 8);
697}
698
Jennifer Hunteac37312007-02-08 13:51:54 -0800699/* Bind an unbound socket */
700static int iucv_sock_bind(struct socket *sock, struct sockaddr *addr,
701 int addr_len)
702{
703 struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
704 struct sock *sk = sock->sk;
705 struct iucv_sock *iucv;
Ursula Braun3881ac42011-08-08 01:33:54 +0000706 int err = 0;
707 struct net_device *dev;
708 char uid[9];
Jennifer Hunteac37312007-02-08 13:51:54 -0800709
710 /* Verify the input sockaddr */
711 if (!addr || addr->sa_family != AF_IUCV)
712 return -EINVAL;
713
Ursula Braun52a82e22016-01-19 10:41:33 +0100714 if (addr_len < sizeof(struct sockaddr_iucv))
715 return -EINVAL;
716
Jennifer Hunteac37312007-02-08 13:51:54 -0800717 lock_sock(sk);
718 if (sk->sk_state != IUCV_OPEN) {
719 err = -EBADFD;
720 goto done;
721 }
722
723 write_lock_bh(&iucv_sk_list.lock);
724
725 iucv = iucv_sk(sk);
726 if (__iucv_get_sock_by_name(sa->siucv_name)) {
727 err = -EADDRINUSE;
728 goto done_unlock;
729 }
Ursula Braun3881ac42011-08-08 01:33:54 +0000730 if (iucv->path)
Jennifer Hunteac37312007-02-08 13:51:54 -0800731 goto done_unlock;
Jennifer Hunteac37312007-02-08 13:51:54 -0800732
733 /* Bind the socket */
Ursula Braun3881ac42011-08-08 01:33:54 +0000734 if (pr_iucv)
735 if (!memcmp(sa->siucv_user_id, iucv_userid, 8))
736 goto vm_bind; /* VM IUCV transport */
Jennifer Hunteac37312007-02-08 13:51:54 -0800737
Ursula Braun3881ac42011-08-08 01:33:54 +0000738 /* try hiper transport */
739 memcpy(uid, sa->siucv_user_id, sizeof(uid));
740 ASCEBC(uid, 8);
741 rcu_read_lock();
742 for_each_netdev_rcu(&init_net, dev) {
743 if (!memcmp(dev->perm_addr, uid, 8)) {
Ursula Braun3881ac42011-08-08 01:33:54 +0000744 memcpy(iucv->src_user_id, sa->siucv_user_id, 8);
Philipp Hachtmann53a4b492014-05-28 10:22:28 +0200745 /* Check for unitialized siucv_name */
746 if (strncmp(sa->siucv_name, " ", 8) == 0)
747 __iucv_auto_name(iucv);
748 else
749 memcpy(iucv->src_name, sa->siucv_name, 8);
Ursula Braun816abba2011-12-19 22:56:29 +0000750 sk->sk_bound_dev_if = dev->ifindex;
Ursula Braun800c5eb2012-02-08 00:19:47 +0000751 iucv->hs_dev = dev;
752 dev_hold(dev);
Ursula Braun3881ac42011-08-08 01:33:54 +0000753 sk->sk_state = IUCV_BOUND;
754 iucv->transport = AF_IUCV_TRANS_HIPER;
755 if (!iucv->msglimit)
756 iucv->msglimit = IUCV_HIPER_MSGLIM_DEFAULT;
757 rcu_read_unlock();
758 goto done_unlock;
759 }
760 }
761 rcu_read_unlock();
762vm_bind:
763 if (pr_iucv) {
764 /* use local userid for backward compat */
765 memcpy(iucv->src_name, sa->siucv_name, 8);
766 memcpy(iucv->src_user_id, iucv_userid, 8);
767 sk->sk_state = IUCV_BOUND;
768 iucv->transport = AF_IUCV_TRANS_IUCV;
769 if (!iucv->msglimit)
770 iucv->msglimit = IUCV_QUEUELEN_DEFAULT;
771 goto done_unlock;
772 }
773 /* found no dev to bind */
774 err = -ENODEV;
Jennifer Hunteac37312007-02-08 13:51:54 -0800775done_unlock:
776 /* Release the socket list lock */
777 write_unlock_bh(&iucv_sk_list.lock);
778done:
779 release_sock(sk);
780 return err;
781}
782
783/* Automatically bind an unbound socket */
784static int iucv_sock_autobind(struct sock *sk)
785{
786 struct iucv_sock *iucv = iucv_sk(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800787 int err = 0;
788
Ursula Braunaac63992011-12-19 22:56:31 +0000789 if (unlikely(!pr_iucv))
Jennifer Hunteac37312007-02-08 13:51:54 -0800790 return -EPROTO;
791
Ursula Braunaac63992011-12-19 22:56:31 +0000792 memcpy(iucv->src_user_id, iucv_userid, 8);
Jennifer Hunteac37312007-02-08 13:51:54 -0800793
794 write_lock_bh(&iucv_sk_list.lock);
Philipp Hachtmann53a4b492014-05-28 10:22:28 +0200795 __iucv_auto_name(iucv);
Jennifer Hunteac37312007-02-08 13:51:54 -0800796 write_unlock_bh(&iucv_sk_list.lock);
797
Ursula Braun3881ac42011-08-08 01:33:54 +0000798 if (!iucv->msglimit)
799 iucv->msglimit = IUCV_QUEUELEN_DEFAULT;
800
Jennifer Hunteac37312007-02-08 13:51:54 -0800801 return err;
802}
803
Ursula Braun3881ac42011-08-08 01:33:54 +0000804static int afiucv_path_connect(struct socket *sock, struct sockaddr *addr)
Jennifer Hunteac37312007-02-08 13:51:54 -0800805{
806 struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
807 struct sock *sk = sock->sk;
Ursula Braun493d3972011-08-08 01:33:52 +0000808 struct iucv_sock *iucv = iucv_sk(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800809 unsigned char user_data[16];
810 int err;
811
Jennifer Hunteac37312007-02-08 13:51:54 -0800812 high_nmcpy(user_data, sa->siucv_name);
Ursula Braun493d3972011-08-08 01:33:52 +0000813 low_nmcpy(user_data, iucv->src_name);
Jennifer Hunteac37312007-02-08 13:51:54 -0800814 ASCEBC(user_data, sizeof(user_data));
815
Jennifer Hunteac37312007-02-08 13:51:54 -0800816 /* Create path. */
Hendrik Brueckner09488e2e2009-04-21 23:26:27 +0000817 iucv->path = iucv_path_alloc(iucv->msglimit,
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000818 IUCV_IPRMDATA, GFP_KERNEL);
Ursula Braund4444722008-02-07 18:07:19 -0800819 if (!iucv->path) {
820 err = -ENOMEM;
821 goto done;
822 }
Frank Blaschka6fcd61f2011-08-08 01:33:51 +0000823 err = pr_iucv->path_connect(iucv->path, &af_iucv_handler,
824 sa->siucv_user_id, NULL, user_data,
825 sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800826 if (err) {
827 iucv_path_free(iucv->path);
828 iucv->path = NULL;
Hendrik Brueckner55cdea92009-01-05 18:07:07 -0800829 switch (err) {
830 case 0x0b: /* Target communicator is not logged on */
831 err = -ENETUNREACH;
832 break;
833 case 0x0d: /* Max connections for this guest exceeded */
834 case 0x0e: /* Max connections for target guest exceeded */
835 err = -EAGAIN;
836 break;
837 case 0x0f: /* Missing IUCV authorization */
838 err = -EACCES;
839 break;
840 default:
841 err = -ECONNREFUSED;
842 break;
843 }
Ursula Braun3881ac42011-08-08 01:33:54 +0000844 }
845done:
846 return err;
847}
848
849/* Connect an unconnected socket */
850static int iucv_sock_connect(struct socket *sock, struct sockaddr *addr,
851 int alen, int flags)
852{
853 struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
854 struct sock *sk = sock->sk;
855 struct iucv_sock *iucv = iucv_sk(sk);
856 int err;
857
858 if (addr->sa_family != AF_IUCV || alen < sizeof(struct sockaddr_iucv))
859 return -EINVAL;
860
861 if (sk->sk_state != IUCV_OPEN && sk->sk_state != IUCV_BOUND)
862 return -EBADFD;
863
864 if (sk->sk_state == IUCV_OPEN &&
865 iucv->transport == AF_IUCV_TRANS_HIPER)
866 return -EBADFD; /* explicit bind required */
867
868 if (sk->sk_type != SOCK_STREAM && sk->sk_type != SOCK_SEQPACKET)
869 return -EINVAL;
870
871 if (sk->sk_state == IUCV_OPEN) {
872 err = iucv_sock_autobind(sk);
873 if (unlikely(err))
874 return err;
Jennifer Hunteac37312007-02-08 13:51:54 -0800875 }
876
Ursula Braun3881ac42011-08-08 01:33:54 +0000877 lock_sock(sk);
878
879 /* Set the destination information */
880 memcpy(iucv->dst_user_id, sa->siucv_user_id, 8);
881 memcpy(iucv->dst_name, sa->siucv_name, 8);
882
883 if (iucv->transport == AF_IUCV_TRANS_HIPER)
Ursula Braun9fbd87d2012-03-07 02:06:23 +0000884 err = iucv_send_ctrl(sock->sk, AF_IUCV_FLAG_SYN);
Ursula Braun3881ac42011-08-08 01:33:54 +0000885 else
886 err = afiucv_path_connect(sock, addr);
887 if (err)
888 goto done;
889
890 if (sk->sk_state != IUCV_CONNECTED)
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +0000891 err = iucv_sock_wait(sk, iucv_sock_in_state(sk, IUCV_CONNECTED,
892 IUCV_DISCONN),
893 sock_sndtimeo(sk, flags & O_NONBLOCK));
Jennifer Hunteac37312007-02-08 13:51:54 -0800894
Ursula Braun3881ac42011-08-08 01:33:54 +0000895 if (sk->sk_state == IUCV_DISCONN || sk->sk_state == IUCV_CLOSED)
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000896 err = -ECONNREFUSED;
Ursula Braun18becbc2009-01-05 18:07:46 -0800897
Ursula Braun7d316b92012-02-08 00:19:45 +0000898 if (err && iucv->transport == AF_IUCV_TRANS_IUCV)
899 iucv_sever_path(sk, 0);
Ursula Braun18becbc2009-01-05 18:07:46 -0800900
Jennifer Hunteac37312007-02-08 13:51:54 -0800901done:
902 release_sock(sk);
903 return err;
904}
905
906/* Move a socket into listening state. */
907static int iucv_sock_listen(struct socket *sock, int backlog)
908{
909 struct sock *sk = sock->sk;
910 int err;
911
912 lock_sock(sk);
913
914 err = -EINVAL;
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +0000915 if (sk->sk_state != IUCV_BOUND)
916 goto done;
917
918 if (sock->type != SOCK_STREAM && sock->type != SOCK_SEQPACKET)
Jennifer Hunteac37312007-02-08 13:51:54 -0800919 goto done;
920
921 sk->sk_max_ack_backlog = backlog;
922 sk->sk_ack_backlog = 0;
923 sk->sk_state = IUCV_LISTEN;
924 err = 0;
925
926done:
927 release_sock(sk);
928 return err;
929}
930
931/* Accept a pending connection */
932static int iucv_sock_accept(struct socket *sock, struct socket *newsock,
933 int flags)
934{
935 DECLARE_WAITQUEUE(wait, current);
936 struct sock *sk = sock->sk, *nsk;
937 long timeo;
938 int err = 0;
939
Jennifer Hunt561e0362007-05-04 12:22:07 -0700940 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
Jennifer Hunteac37312007-02-08 13:51:54 -0800941
942 if (sk->sk_state != IUCV_LISTEN) {
943 err = -EBADFD;
944 goto done;
945 }
946
947 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
948
949 /* Wait for an incoming connection */
Eric Dumazetaa395142010-04-20 13:03:51 +0000950 add_wait_queue_exclusive(sk_sleep(sk), &wait);
Heiko Carstensda99f052007-05-04 12:23:27 -0700951 while (!(nsk = iucv_accept_dequeue(sk, newsock))) {
Jennifer Hunteac37312007-02-08 13:51:54 -0800952 set_current_state(TASK_INTERRUPTIBLE);
953 if (!timeo) {
954 err = -EAGAIN;
955 break;
956 }
957
958 release_sock(sk);
959 timeo = schedule_timeout(timeo);
Jennifer Hunt561e0362007-05-04 12:22:07 -0700960 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
Jennifer Hunteac37312007-02-08 13:51:54 -0800961
962 if (sk->sk_state != IUCV_LISTEN) {
963 err = -EBADFD;
964 break;
965 }
966
967 if (signal_pending(current)) {
968 err = sock_intr_errno(timeo);
969 break;
970 }
971 }
972
973 set_current_state(TASK_RUNNING);
Eric Dumazetaa395142010-04-20 13:03:51 +0000974 remove_wait_queue(sk_sleep(sk), &wait);
Jennifer Hunteac37312007-02-08 13:51:54 -0800975
976 if (err)
977 goto done;
978
979 newsock->state = SS_CONNECTED;
980
981done:
982 release_sock(sk);
983 return err;
984}
985
986static int iucv_sock_getname(struct socket *sock, struct sockaddr *addr,
987 int *len, int peer)
988{
989 struct sockaddr_iucv *siucv = (struct sockaddr_iucv *) addr;
990 struct sock *sk = sock->sk;
Ursula Braun493d3972011-08-08 01:33:52 +0000991 struct iucv_sock *iucv = iucv_sk(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800992
993 addr->sa_family = AF_IUCV;
994 *len = sizeof(struct sockaddr_iucv);
995
996 if (peer) {
Ursula Braun493d3972011-08-08 01:33:52 +0000997 memcpy(siucv->siucv_user_id, iucv->dst_user_id, 8);
998 memcpy(siucv->siucv_name, iucv->dst_name, 8);
Jennifer Hunteac37312007-02-08 13:51:54 -0800999 } else {
Ursula Braun493d3972011-08-08 01:33:52 +00001000 memcpy(siucv->siucv_user_id, iucv->src_user_id, 8);
1001 memcpy(siucv->siucv_name, iucv->src_name, 8);
Jennifer Hunteac37312007-02-08 13:51:54 -08001002 }
1003 memset(&siucv->siucv_port, 0, sizeof(siucv->siucv_port));
1004 memset(&siucv->siucv_addr, 0, sizeof(siucv->siucv_addr));
Ursula Braun493d3972011-08-08 01:33:52 +00001005 memset(&siucv->siucv_nodeid, 0, sizeof(siucv->siucv_nodeid));
Jennifer Hunteac37312007-02-08 13:51:54 -08001006
1007 return 0;
1008}
1009
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001010/**
1011 * iucv_send_iprm() - Send socket data in parameter list of an iucv message.
1012 * @path: IUCV path
1013 * @msg: Pointer to a struct iucv_message
1014 * @skb: The socket data to send, skb->len MUST BE <= 7
1015 *
1016 * Send the socket data in the parameter list in the iucv message
1017 * (IUCV_IPRMDATA). The socket data is stored at index 0 to 6 in the parameter
1018 * list and the socket data len at index 7 (last byte).
1019 * See also iucv_msg_length().
1020 *
1021 * Returns the error code from the iucv_message_send() call.
1022 */
1023static int iucv_send_iprm(struct iucv_path *path, struct iucv_message *msg,
1024 struct sk_buff *skb)
1025{
1026 u8 prmdata[8];
1027
1028 memcpy(prmdata, (void *) skb->data, skb->len);
1029 prmdata[7] = 0xff - (u8) skb->len;
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001030 return pr_iucv->message_send(path, msg, IUCV_IPRMDATA, 0,
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001031 (void *) prmdata, 8);
1032}
1033
Ying Xue1b784142015-03-02 15:37:48 +08001034static int iucv_sock_sendmsg(struct socket *sock, struct msghdr *msg,
1035 size_t len)
Jennifer Hunteac37312007-02-08 13:51:54 -08001036{
1037 struct sock *sk = sock->sk;
1038 struct iucv_sock *iucv = iucv_sk(sk);
Julian Wiedmann9297e0c2017-01-10 17:10:34 +01001039 size_t headroom = 0;
1040 size_t linear;
Jennifer Hunteac37312007-02-08 13:51:54 -08001041 struct sk_buff *skb;
Eugene Crosser0506eb02015-12-11 12:27:50 +01001042 struct iucv_message txmsg = {0};
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001043 struct cmsghdr *cmsg;
1044 int cmsg_done;
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +00001045 long timeo;
Ursula Braun8f7c5022008-12-25 13:39:47 +01001046 char user_id[9];
1047 char appl_id[9];
Jennifer Hunteac37312007-02-08 13:51:54 -08001048 int err;
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +00001049 int noblock = msg->msg_flags & MSG_DONTWAIT;
Jennifer Hunteac37312007-02-08 13:51:54 -08001050
1051 err = sock_error(sk);
1052 if (err)
1053 return err;
1054
1055 if (msg->msg_flags & MSG_OOB)
1056 return -EOPNOTSUPP;
1057
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001058 /* SOCK_SEQPACKET: we do not support segmented records */
1059 if (sk->sk_type == SOCK_SEQPACKET && !(msg->msg_flags & MSG_EOR))
1060 return -EOPNOTSUPP;
1061
Jennifer Hunteac37312007-02-08 13:51:54 -08001062 lock_sock(sk);
1063
1064 if (sk->sk_shutdown & SEND_SHUTDOWN) {
1065 err = -EPIPE;
1066 goto out;
1067 }
1068
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001069 /* Return if the socket is not in connected state */
1070 if (sk->sk_state != IUCV_CONNECTED) {
1071 err = -ENOTCONN;
1072 goto out;
1073 }
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001074
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001075 /* initialize defaults */
1076 cmsg_done = 0; /* check for duplicate headers */
1077 txmsg.class = 0;
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001078
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001079 /* iterate over control messages */
Gu Zhengf95b4142014-12-11 11:22:04 +08001080 for_each_cmsghdr(cmsg, msg) {
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001081 if (!CMSG_OK(msg, cmsg)) {
1082 err = -EINVAL;
Jennifer Hunt561e0362007-05-04 12:22:07 -07001083 goto out;
Jennifer Hunteac37312007-02-08 13:51:54 -08001084 }
1085
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001086 if (cmsg->cmsg_level != SOL_IUCV)
1087 continue;
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001088
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001089 if (cmsg->cmsg_type & cmsg_done) {
1090 err = -EINVAL;
1091 goto out;
1092 }
1093 cmsg_done |= cmsg->cmsg_type;
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001094
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001095 switch (cmsg->cmsg_type) {
1096 case SCM_IUCV_TRGCLS:
1097 if (cmsg->cmsg_len != CMSG_LEN(TRGCLS_SIZE)) {
1098 err = -EINVAL;
1099 goto out;
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001100 }
1101
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001102 /* set iucv message target class */
1103 memcpy(&txmsg.class,
1104 (void *) CMSG_DATA(cmsg), TRGCLS_SIZE);
1105
1106 break;
1107
1108 default:
1109 err = -EINVAL;
1110 goto out;
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001111 }
1112 }
1113
1114 /* allocate one skb for each iucv message:
1115 * this is fine for SOCK_SEQPACKET (unless we want to support
1116 * segmented records using the MSG_EOR flag), but
1117 * for SOCK_STREAM we might want to improve it in future */
Julian Wiedmann9297e0c2017-01-10 17:10:34 +01001118 if (iucv->transport == AF_IUCV_TRANS_HIPER) {
1119 headroom = sizeof(struct af_iucv_trans_hdr) + ETH_HLEN;
Eugene Crossere5374392016-06-13 18:46:14 +02001120 linear = len;
1121 } else {
Julian Wiedmann9297e0c2017-01-10 17:10:34 +01001122 if (len < PAGE_SIZE) {
1123 linear = len;
1124 } else {
1125 /* In nonlinear "classic" iucv skb,
1126 * reserve space for iucv_array
1127 */
1128 headroom = sizeof(struct iucv_array) *
1129 (MAX_SKB_FRAGS + 1);
1130 linear = PAGE_SIZE - headroom;
1131 }
Eugene Crossere5374392016-06-13 18:46:14 +02001132 }
1133 skb = sock_alloc_send_pskb(sk, headroom + linear, len - linear,
1134 noblock, &err, 0);
Eugene Crossered4ac422015-03-30 15:40:42 +02001135 if (!skb)
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001136 goto out;
Eugene Crossere5374392016-06-13 18:46:14 +02001137 if (headroom)
1138 skb_reserve(skb, headroom);
1139 skb_put(skb, linear);
1140 skb->len = len;
1141 skb->data_len = len - linear;
1142 err = skb_copy_datagram_from_iter(skb, 0, &msg->msg_iter, len);
1143 if (err)
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001144 goto fail;
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001145
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +00001146 /* wait if outstanding messages for iucv path has reached */
1147 timeo = sock_sndtimeo(sk, noblock);
1148 err = iucv_sock_wait(sk, iucv_below_msglim(sk), timeo);
1149 if (err)
1150 goto fail;
1151
1152 /* return -ECONNRESET if the socket is no longer connected */
1153 if (sk->sk_state != IUCV_CONNECTED) {
1154 err = -ECONNRESET;
1155 goto fail;
1156 }
1157
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001158 /* increment and save iucv message tag for msg_completion cbk */
1159 txmsg.tag = iucv->send_tag++;
Ursula Braunf9c41a62013-04-07 22:19:26 +00001160 IUCV_SKB_CB(skb)->tag = txmsg.tag;
Ursula Braun800c5eb2012-02-08 00:19:47 +00001161
Ursula Braun3881ac42011-08-08 01:33:54 +00001162 if (iucv->transport == AF_IUCV_TRANS_HIPER) {
1163 atomic_inc(&iucv->msg_sent);
1164 err = afiucv_hs_send(&txmsg, sk, skb, 0);
1165 if (err) {
1166 atomic_dec(&iucv->msg_sent);
1167 goto fail;
1168 }
Eugene Crossere5374392016-06-13 18:46:14 +02001169 } else { /* Classic VM IUCV transport */
1170 skb_queue_tail(&iucv->send_skb_q, skb);
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001171
Eugene Crossere5374392016-06-13 18:46:14 +02001172 if (((iucv->path->flags & IUCV_IPRMDATA) & iucv->flags) &&
1173 skb->len <= 7) {
1174 err = iucv_send_iprm(iucv->path, &txmsg, skb);
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001175
Eugene Crossere5374392016-06-13 18:46:14 +02001176 /* on success: there is no message_complete callback */
1177 /* for an IPRMDATA msg; remove skb from send queue */
1178 if (err == 0) {
1179 skb_unlink(skb, &iucv->send_skb_q);
1180 kfree_skb(skb);
1181 }
1182
1183 /* this error should never happen since the */
1184 /* IUCV_IPRMDATA path flag is set... sever path */
1185 if (err == 0x15) {
1186 pr_iucv->path_sever(iucv->path, NULL);
1187 skb_unlink(skb, &iucv->send_skb_q);
1188 err = -EPIPE;
1189 goto fail;
1190 }
1191 } else if (skb_is_nonlinear(skb)) {
1192 struct iucv_array *iba = (struct iucv_array *)skb->head;
1193 int i;
1194
1195 /* skip iucv_array lying in the headroom */
1196 iba[0].address = (u32)(addr_t)skb->data;
1197 iba[0].length = (u32)skb_headlen(skb);
1198 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1199 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1200
1201 iba[i + 1].address =
1202 (u32)(addr_t)skb_frag_address(frag);
1203 iba[i + 1].length = (u32)skb_frag_size(frag);
1204 }
1205 err = pr_iucv->message_send(iucv->path, &txmsg,
1206 IUCV_IPBUFLST, 0,
1207 (void *)iba, skb->len);
1208 } else { /* non-IPRM Linear skb */
1209 err = pr_iucv->message_send(iucv->path, &txmsg,
1210 0, 0, (void *)skb->data, skb->len);
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001211 }
Eugene Crossere5374392016-06-13 18:46:14 +02001212 if (err) {
1213 if (err == 3) {
1214 user_id[8] = 0;
1215 memcpy(user_id, iucv->dst_user_id, 8);
1216 appl_id[8] = 0;
1217 memcpy(appl_id, iucv->dst_name, 8);
1218 pr_err(
1219 "Application %s on z/VM guest %s exceeds message limit\n",
1220 appl_id, user_id);
1221 err = -EAGAIN;
1222 } else {
1223 err = -EPIPE;
1224 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001225 skb_unlink(skb, &iucv->send_skb_q);
Jennifer Hunteac37312007-02-08 13:51:54 -08001226 goto fail;
1227 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001228 }
1229
1230 release_sock(sk);
1231 return len;
1232
1233fail:
1234 kfree_skb(skb);
1235out:
1236 release_sock(sk);
1237 return err;
1238}
1239
Eugene Crossera006353a92016-06-13 18:46:16 +02001240static struct sk_buff *alloc_iucv_recv_skb(unsigned long len)
1241{
1242 size_t headroom, linear;
1243 struct sk_buff *skb;
1244 int err;
1245
1246 if (len < PAGE_SIZE) {
1247 headroom = 0;
1248 linear = len;
1249 } else {
1250 headroom = sizeof(struct iucv_array) * (MAX_SKB_FRAGS + 1);
1251 linear = PAGE_SIZE - headroom;
1252 }
1253 skb = alloc_skb_with_frags(headroom + linear, len - linear,
1254 0, &err, GFP_ATOMIC | GFP_DMA);
1255 WARN_ONCE(!skb,
1256 "alloc of recv iucv skb len=%lu failed with errcode=%d\n",
1257 len, err);
1258 if (skb) {
1259 if (headroom)
1260 skb_reserve(skb, headroom);
1261 skb_put(skb, linear);
1262 skb->len = len;
1263 skb->data_len = len - linear;
1264 }
1265 return skb;
1266}
1267
Hendrik Bruecknerbf95d202009-09-16 04:37:28 +00001268/* iucv_process_message() - Receive a single outstanding IUCV message
1269 *
1270 * Locking: must be called with message_q.lock held
1271 */
Ursula Braunf0703c82007-10-08 02:03:31 -07001272static void iucv_process_message(struct sock *sk, struct sk_buff *skb,
1273 struct iucv_path *path,
1274 struct iucv_message *msg)
1275{
1276 int rc;
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001277 unsigned int len;
Ursula Braunf0703c82007-10-08 02:03:31 -07001278
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001279 len = iucv_msg_length(msg);
1280
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001281 /* store msg target class in the second 4 bytes of skb ctrl buffer */
1282 /* Note: the first 4 bytes are reserved for msg tag */
Ursula Braunf9c41a62013-04-07 22:19:26 +00001283 IUCV_SKB_CB(skb)->class = msg->class;
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001284
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001285 /* check for special IPRM messages (e.g. iucv_sock_shutdown) */
1286 if ((msg->flags & IUCV_IPRMDATA) && len > 7) {
1287 if (memcmp(msg->rmmsg, iprm_shutdown, 8) == 0) {
1288 skb->data = NULL;
1289 skb->len = 0;
1290 }
Ursula Braunf0703c82007-10-08 02:03:31 -07001291 } else {
Eugene Crossera006353a92016-06-13 18:46:16 +02001292 if (skb_is_nonlinear(skb)) {
1293 struct iucv_array *iba = (struct iucv_array *)skb->head;
1294 int i;
1295
1296 iba[0].address = (u32)(addr_t)skb->data;
1297 iba[0].length = (u32)skb_headlen(skb);
1298 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1299 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1300
1301 iba[i + 1].address =
1302 (u32)(addr_t)skb_frag_address(frag);
1303 iba[i + 1].length = (u32)skb_frag_size(frag);
1304 }
1305 rc = pr_iucv->message_receive(path, msg,
1306 IUCV_IPBUFLST,
1307 (void *)iba, len, NULL);
1308 } else {
1309 rc = pr_iucv->message_receive(path, msg,
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001310 msg->flags & IUCV_IPRMDATA,
1311 skb->data, len, NULL);
Eugene Crossera006353a92016-06-13 18:46:16 +02001312 }
Ursula Braunf0703c82007-10-08 02:03:31 -07001313 if (rc) {
1314 kfree_skb(skb);
1315 return;
1316 }
Eugene Crossera006353a92016-06-13 18:46:16 +02001317 WARN_ON_ONCE(skb->len != len);
Ursula Braunf0703c82007-10-08 02:03:31 -07001318 }
1319
Ursula Braunf9c41a62013-04-07 22:19:26 +00001320 IUCV_SKB_CB(skb)->offset = 0;
Ursula Braunf0703c82007-10-08 02:03:31 -07001321 if (sock_queue_rcv_skb(sk, skb))
1322 skb_queue_head(&iucv_sk(sk)->backlog_skb_q, skb);
1323}
1324
Hendrik Bruecknerbf95d202009-09-16 04:37:28 +00001325/* iucv_process_message_q() - Process outstanding IUCV messages
1326 *
1327 * Locking: must be called with message_q.lock held
1328 */
Ursula Braunf0703c82007-10-08 02:03:31 -07001329static void iucv_process_message_q(struct sock *sk)
1330{
1331 struct iucv_sock *iucv = iucv_sk(sk);
1332 struct sk_buff *skb;
1333 struct sock_msg_q *p, *n;
1334
1335 list_for_each_entry_safe(p, n, &iucv->message_q.list, list) {
Eugene Crossera006353a92016-06-13 18:46:16 +02001336 skb = alloc_iucv_recv_skb(iucv_msg_length(&p->msg));
Ursula Braunf0703c82007-10-08 02:03:31 -07001337 if (!skb)
1338 break;
1339 iucv_process_message(sk, skb, p->path, &p->msg);
1340 list_del(&p->list);
1341 kfree(p);
1342 if (!skb_queue_empty(&iucv->backlog_skb_q))
1343 break;
1344 }
1345}
1346
Ying Xue1b784142015-03-02 15:37:48 +08001347static int iucv_sock_recvmsg(struct socket *sock, struct msghdr *msg,
1348 size_t len, int flags)
Jennifer Hunteac37312007-02-08 13:51:54 -08001349{
1350 int noblock = flags & MSG_DONTWAIT;
1351 struct sock *sk = sock->sk;
Jennifer Hunt561e0362007-05-04 12:22:07 -07001352 struct iucv_sock *iucv = iucv_sk(sk);
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001353 unsigned int copied, rlen;
Ursula Braun9fbd87d2012-03-07 02:06:23 +00001354 struct sk_buff *skb, *rskb, *cskb;
Jennifer Hunteac37312007-02-08 13:51:54 -08001355 int err = 0;
Ursula Braunf9c41a62013-04-07 22:19:26 +00001356 u32 offset;
Jennifer Hunteac37312007-02-08 13:51:54 -08001357
Ursula Braunaac63992011-12-19 22:56:31 +00001358 if ((sk->sk_state == IUCV_DISCONN) &&
Ursula Braunf0703c82007-10-08 02:03:31 -07001359 skb_queue_empty(&iucv->backlog_skb_q) &&
1360 skb_queue_empty(&sk->sk_receive_queue) &&
1361 list_empty(&iucv->message_q.list))
Jennifer Hunt561e0362007-05-04 12:22:07 -07001362 return 0;
1363
Jennifer Hunteac37312007-02-08 13:51:54 -08001364 if (flags & (MSG_OOB))
1365 return -EOPNOTSUPP;
1366
Hendrik Brueckner60d37052009-04-21 06:04:21 +00001367 /* receive/dequeue next skb:
1368 * the function understands MSG_PEEK and, thus, does not dequeue skb */
Jennifer Hunteac37312007-02-08 13:51:54 -08001369 skb = skb_recv_datagram(sk, flags, noblock, &err);
1370 if (!skb) {
1371 if (sk->sk_shutdown & RCV_SHUTDOWN)
1372 return 0;
1373 return err;
1374 }
1375
Ursula Braunf9c41a62013-04-07 22:19:26 +00001376 offset = IUCV_SKB_CB(skb)->offset;
1377 rlen = skb->len - offset; /* real length of skb */
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001378 copied = min_t(unsigned int, rlen, len);
Ursula Braun82492a32012-03-07 02:06:24 +00001379 if (!rlen)
1380 sk->sk_shutdown = sk->sk_shutdown | RCV_SHUTDOWN;
Jennifer Hunteac37312007-02-08 13:51:54 -08001381
Jennifer Hunt561e0362007-05-04 12:22:07 -07001382 cskb = skb;
David S. Miller51f3d022014-11-05 16:46:40 -05001383 if (skb_copy_datagram_msg(cskb, offset, msg, copied)) {
Hendrik Brueckner802788b2009-04-21 23:26:26 +00001384 if (!(flags & MSG_PEEK))
1385 skb_queue_head(&sk->sk_receive_queue, skb);
1386 return -EFAULT;
Jennifer Hunteac37312007-02-08 13:51:54 -08001387 }
1388
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001389 /* SOCK_SEQPACKET: set MSG_TRUNC if recv buf size is too small */
1390 if (sk->sk_type == SOCK_SEQPACKET) {
1391 if (copied < rlen)
1392 msg->msg_flags |= MSG_TRUNC;
1393 /* each iucv message contains a complete record */
1394 msg->msg_flags |= MSG_EOR;
1395 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001396
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001397 /* create control message to store iucv msg target class:
1398 * get the trgcls from the control buffer of the skb due to
1399 * fragmentation of original iucv message. */
1400 err = put_cmsg(msg, SOL_IUCV, SCM_IUCV_TRGCLS,
Ursula Braunf9c41a62013-04-07 22:19:26 +00001401 sizeof(IUCV_SKB_CB(skb)->class),
1402 (void *)&IUCV_SKB_CB(skb)->class);
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001403 if (err) {
1404 if (!(flags & MSG_PEEK))
1405 skb_queue_head(&sk->sk_receive_queue, skb);
1406 return err;
1407 }
1408
Jennifer Hunteac37312007-02-08 13:51:54 -08001409 /* Mark read part of skb as used */
1410 if (!(flags & MSG_PEEK)) {
Jennifer Hunteac37312007-02-08 13:51:54 -08001411
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001412 /* SOCK_STREAM: re-queue skb if it contains unreceived data */
1413 if (sk->sk_type == SOCK_STREAM) {
Ursula Braunf9c41a62013-04-07 22:19:26 +00001414 if (copied < rlen) {
1415 IUCV_SKB_CB(skb)->offset = offset + copied;
Ursula Braun2f139a52014-03-19 07:57:59 +01001416 skb_queue_head(&sk->sk_receive_queue, skb);
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001417 goto done;
1418 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001419 }
1420
1421 kfree_skb(skb);
Ursula Braun800c5eb2012-02-08 00:19:47 +00001422 if (iucv->transport == AF_IUCV_TRANS_HIPER) {
1423 atomic_inc(&iucv->msg_recv);
1424 if (atomic_read(&iucv->msg_recv) > iucv->msglimit) {
1425 WARN_ON(1);
1426 iucv_sock_close(sk);
1427 return -EFAULT;
1428 }
1429 }
Jennifer Hunt561e0362007-05-04 12:22:07 -07001430
1431 /* Queue backlog skbs */
Hendrik Bruecknerbf95d202009-09-16 04:37:28 +00001432 spin_lock_bh(&iucv->message_q.lock);
Ursula Braunf0703c82007-10-08 02:03:31 -07001433 rskb = skb_dequeue(&iucv->backlog_skb_q);
Heiko Carstensda99f052007-05-04 12:23:27 -07001434 while (rskb) {
Ursula Braunf9c41a62013-04-07 22:19:26 +00001435 IUCV_SKB_CB(rskb)->offset = 0;
Jennifer Hunt561e0362007-05-04 12:22:07 -07001436 if (sock_queue_rcv_skb(sk, rskb)) {
Ursula Braunf0703c82007-10-08 02:03:31 -07001437 skb_queue_head(&iucv->backlog_skb_q,
Jennifer Hunt561e0362007-05-04 12:22:07 -07001438 rskb);
1439 break;
1440 } else {
Ursula Braunf0703c82007-10-08 02:03:31 -07001441 rskb = skb_dequeue(&iucv->backlog_skb_q);
Jennifer Hunt561e0362007-05-04 12:22:07 -07001442 }
1443 }
Ursula Braunf0703c82007-10-08 02:03:31 -07001444 if (skb_queue_empty(&iucv->backlog_skb_q)) {
Ursula Braunf0703c82007-10-08 02:03:31 -07001445 if (!list_empty(&iucv->message_q.list))
1446 iucv_process_message_q(sk);
Ursula Braun3881ac42011-08-08 01:33:54 +00001447 if (atomic_read(&iucv->msg_recv) >=
1448 iucv->msglimit / 2) {
Ursula Braun9fbd87d2012-03-07 02:06:23 +00001449 err = iucv_send_ctrl(sk, AF_IUCV_FLAG_WIN);
Ursula Braun3881ac42011-08-08 01:33:54 +00001450 if (err) {
1451 sk->sk_state = IUCV_DISCONN;
1452 sk->sk_state_change(sk);
1453 }
1454 }
Ursula Braunf0703c82007-10-08 02:03:31 -07001455 }
Hendrik Bruecknerbf95d202009-09-16 04:37:28 +00001456 spin_unlock_bh(&iucv->message_q.lock);
Hendrik Brueckner60d37052009-04-21 06:04:21 +00001457 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001458
1459done:
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001460 /* SOCK_SEQPACKET: return real length if MSG_TRUNC is set */
1461 if (sk->sk_type == SOCK_SEQPACKET && (flags & MSG_TRUNC))
1462 copied = rlen;
1463
1464 return copied;
Jennifer Hunteac37312007-02-08 13:51:54 -08001465}
1466
1467static inline unsigned int iucv_accept_poll(struct sock *parent)
1468{
1469 struct iucv_sock *isk, *n;
1470 struct sock *sk;
1471
Heiko Carstensda99f052007-05-04 12:23:27 -07001472 list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) {
Jennifer Hunteac37312007-02-08 13:51:54 -08001473 sk = (struct sock *) isk;
1474
1475 if (sk->sk_state == IUCV_CONNECTED)
1476 return POLLIN | POLLRDNORM;
1477 }
1478
1479 return 0;
1480}
1481
1482unsigned int iucv_sock_poll(struct file *file, struct socket *sock,
1483 poll_table *wait)
1484{
1485 struct sock *sk = sock->sk;
1486 unsigned int mask = 0;
1487
Eric Dumazetaa395142010-04-20 13:03:51 +00001488 sock_poll_wait(file, sk_sleep(sk), wait);
Jennifer Hunteac37312007-02-08 13:51:54 -08001489
1490 if (sk->sk_state == IUCV_LISTEN)
1491 return iucv_accept_poll(sk);
1492
1493 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
Keller, Jacob E7d4c04f2013-03-28 11:19:25 +00001494 mask |= POLLERR |
Jacob Keller8facd5f2013-04-02 13:55:40 -07001495 (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? POLLPRI : 0);
Jennifer Hunteac37312007-02-08 13:51:54 -08001496
1497 if (sk->sk_shutdown & RCV_SHUTDOWN)
1498 mask |= POLLRDHUP;
1499
1500 if (sk->sk_shutdown == SHUTDOWN_MASK)
1501 mask |= POLLHUP;
1502
1503 if (!skb_queue_empty(&sk->sk_receive_queue) ||
Heiko Carstensda99f052007-05-04 12:23:27 -07001504 (sk->sk_shutdown & RCV_SHUTDOWN))
Jennifer Hunteac37312007-02-08 13:51:54 -08001505 mask |= POLLIN | POLLRDNORM;
1506
1507 if (sk->sk_state == IUCV_CLOSED)
1508 mask |= POLLHUP;
1509
Ursula Braunaac63992011-12-19 22:56:31 +00001510 if (sk->sk_state == IUCV_DISCONN)
Jennifer Hunt561e0362007-05-04 12:22:07 -07001511 mask |= POLLIN;
1512
Ursula Braun7f1b0ea2012-02-08 00:19:46 +00001513 if (sock_writeable(sk) && iucv_below_msglim(sk))
Jennifer Hunteac37312007-02-08 13:51:54 -08001514 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
1515 else
Eric Dumazet9cd3e072015-11-29 20:03:10 -08001516 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
Jennifer Hunteac37312007-02-08 13:51:54 -08001517
1518 return mask;
1519}
1520
1521static int iucv_sock_shutdown(struct socket *sock, int how)
1522{
1523 struct sock *sk = sock->sk;
1524 struct iucv_sock *iucv = iucv_sk(sk);
1525 struct iucv_message txmsg;
1526 int err = 0;
Jennifer Hunteac37312007-02-08 13:51:54 -08001527
1528 how++;
1529
1530 if ((how & ~SHUTDOWN_MASK) || !how)
1531 return -EINVAL;
1532
1533 lock_sock(sk);
Heiko Carstensda99f052007-05-04 12:23:27 -07001534 switch (sk->sk_state) {
Ursula Braun82492a32012-03-07 02:06:24 +00001535 case IUCV_LISTEN:
Hendrik Bruecknere14ad5f2009-04-21 06:04:23 +00001536 case IUCV_DISCONN:
1537 case IUCV_CLOSING:
Jennifer Hunteac37312007-02-08 13:51:54 -08001538 case IUCV_CLOSED:
1539 err = -ENOTCONN;
1540 goto fail;
Jennifer Hunteac37312007-02-08 13:51:54 -08001541 default:
Jennifer Hunteac37312007-02-08 13:51:54 -08001542 break;
1543 }
1544
1545 if (how == SEND_SHUTDOWN || how == SHUTDOWN_MASK) {
Ursula Braun82492a32012-03-07 02:06:24 +00001546 if (iucv->transport == AF_IUCV_TRANS_IUCV) {
1547 txmsg.class = 0;
1548 txmsg.tag = 0;
1549 err = pr_iucv->message_send(iucv->path, &txmsg,
1550 IUCV_IPRMDATA, 0, (void *) iprm_shutdown, 8);
1551 if (err) {
1552 switch (err) {
1553 case 1:
1554 err = -ENOTCONN;
1555 break;
1556 case 2:
1557 err = -ECONNRESET;
1558 break;
1559 default:
1560 err = -ENOTCONN;
1561 break;
1562 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001563 }
Ursula Braun82492a32012-03-07 02:06:24 +00001564 } else
1565 iucv_send_ctrl(sk, AF_IUCV_FLAG_SHT);
Jennifer Hunteac37312007-02-08 13:51:54 -08001566 }
1567
Ursula Braun82492a32012-03-07 02:06:24 +00001568 sk->sk_shutdown |= how;
Jennifer Hunteac37312007-02-08 13:51:54 -08001569 if (how == RCV_SHUTDOWN || how == SHUTDOWN_MASK) {
Ursula Braun1042cab2014-07-21 12:54:44 +02001570 if ((iucv->transport == AF_IUCV_TRANS_IUCV) &&
1571 iucv->path) {
Ursula Braun82492a32012-03-07 02:06:24 +00001572 err = pr_iucv->path_quiesce(iucv->path, NULL);
1573 if (err)
1574 err = -ENOTCONN;
1575/* skb_queue_purge(&sk->sk_receive_queue); */
1576 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001577 skb_queue_purge(&sk->sk_receive_queue);
1578 }
1579
1580 /* Wake up anyone sleeping in poll */
1581 sk->sk_state_change(sk);
1582
1583fail:
1584 release_sock(sk);
1585 return err;
1586}
1587
1588static int iucv_sock_release(struct socket *sock)
1589{
1590 struct sock *sk = sock->sk;
1591 int err = 0;
1592
1593 if (!sk)
1594 return 0;
1595
1596 iucv_sock_close(sk);
1597
Jennifer Hunteac37312007-02-08 13:51:54 -08001598 sock_orphan(sk);
1599 iucv_sock_kill(sk);
1600 return err;
1601}
1602
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00001603/* getsockopt and setsockopt */
1604static int iucv_sock_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07001605 char __user *optval, unsigned int optlen)
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00001606{
1607 struct sock *sk = sock->sk;
1608 struct iucv_sock *iucv = iucv_sk(sk);
1609 int val;
1610 int rc;
1611
1612 if (level != SOL_IUCV)
1613 return -ENOPROTOOPT;
1614
1615 if (optlen < sizeof(int))
1616 return -EINVAL;
1617
1618 if (get_user(val, (int __user *) optval))
1619 return -EFAULT;
1620
1621 rc = 0;
1622
1623 lock_sock(sk);
1624 switch (optname) {
1625 case SO_IPRMDATA_MSG:
1626 if (val)
1627 iucv->flags |= IUCV_IPRMDATA;
1628 else
1629 iucv->flags &= ~IUCV_IPRMDATA;
1630 break;
Hendrik Brueckner09488e2e2009-04-21 23:26:27 +00001631 case SO_MSGLIMIT:
1632 switch (sk->sk_state) {
1633 case IUCV_OPEN:
1634 case IUCV_BOUND:
1635 if (val < 1 || val > (u16)(~0))
1636 rc = -EINVAL;
1637 else
1638 iucv->msglimit = val;
1639 break;
1640 default:
1641 rc = -EINVAL;
1642 break;
1643 }
1644 break;
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00001645 default:
1646 rc = -ENOPROTOOPT;
1647 break;
1648 }
1649 release_sock(sk);
1650
1651 return rc;
1652}
1653
1654static int iucv_sock_getsockopt(struct socket *sock, int level, int optname,
1655 char __user *optval, int __user *optlen)
1656{
1657 struct sock *sk = sock->sk;
1658 struct iucv_sock *iucv = iucv_sk(sk);
Ursula Braun51363b82012-02-08 00:19:48 +00001659 unsigned int val;
1660 int len;
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00001661
1662 if (level != SOL_IUCV)
1663 return -ENOPROTOOPT;
1664
1665 if (get_user(len, optlen))
1666 return -EFAULT;
1667
1668 if (len < 0)
1669 return -EINVAL;
1670
1671 len = min_t(unsigned int, len, sizeof(int));
1672
1673 switch (optname) {
1674 case SO_IPRMDATA_MSG:
1675 val = (iucv->flags & IUCV_IPRMDATA) ? 1 : 0;
1676 break;
Hendrik Brueckner09488e2e2009-04-21 23:26:27 +00001677 case SO_MSGLIMIT:
1678 lock_sock(sk);
1679 val = (iucv->path != NULL) ? iucv->path->msglim /* connected */
1680 : iucv->msglimit; /* default */
1681 release_sock(sk);
1682 break;
Ursula Braun51363b82012-02-08 00:19:48 +00001683 case SO_MSGSIZE:
1684 if (sk->sk_state == IUCV_OPEN)
1685 return -EBADFD;
1686 val = (iucv->hs_dev) ? iucv->hs_dev->mtu -
1687 sizeof(struct af_iucv_trans_hdr) - ETH_HLEN :
1688 0x7fffffff;
1689 break;
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00001690 default:
1691 return -ENOPROTOOPT;
1692 }
1693
1694 if (put_user(len, optlen))
1695 return -EFAULT;
1696 if (copy_to_user(optval, &val, len))
1697 return -EFAULT;
1698
1699 return 0;
1700}
1701
1702
Jennifer Hunteac37312007-02-08 13:51:54 -08001703/* Callback wrappers - called from iucv base support */
1704static int iucv_callback_connreq(struct iucv_path *path,
1705 u8 ipvmid[8], u8 ipuser[16])
1706{
1707 unsigned char user_data[16];
1708 unsigned char nuser_data[16];
1709 unsigned char src_name[8];
Jennifer Hunteac37312007-02-08 13:51:54 -08001710 struct sock *sk, *nsk;
1711 struct iucv_sock *iucv, *niucv;
1712 int err;
1713
1714 memcpy(src_name, ipuser, 8);
1715 EBCASC(src_name, 8);
1716 /* Find out if this path belongs to af_iucv. */
1717 read_lock(&iucv_sk_list.lock);
1718 iucv = NULL;
Ursula Braunfebca282007-07-14 19:04:25 -07001719 sk = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001720 sk_for_each(sk, &iucv_sk_list.head)
Jennifer Hunteac37312007-02-08 13:51:54 -08001721 if (sk->sk_state == IUCV_LISTEN &&
1722 !memcmp(&iucv_sk(sk)->src_name, src_name, 8)) {
1723 /*
1724 * Found a listening socket with
1725 * src_name == ipuser[0-7].
1726 */
1727 iucv = iucv_sk(sk);
1728 break;
1729 }
1730 read_unlock(&iucv_sk_list.lock);
1731 if (!iucv)
1732 /* No socket found, not one of our paths. */
1733 return -EINVAL;
1734
1735 bh_lock_sock(sk);
1736
1737 /* Check if parent socket is listening */
1738 low_nmcpy(user_data, iucv->src_name);
1739 high_nmcpy(user_data, iucv->dst_name);
1740 ASCEBC(user_data, sizeof(user_data));
1741 if (sk->sk_state != IUCV_LISTEN) {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001742 err = pr_iucv->path_sever(path, user_data);
Hendrik Brueckner65dbd7c2009-01-05 18:08:23 -08001743 iucv_path_free(path);
Jennifer Hunteac37312007-02-08 13:51:54 -08001744 goto fail;
1745 }
1746
1747 /* Check for backlog size */
1748 if (sk_acceptq_is_full(sk)) {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001749 err = pr_iucv->path_sever(path, user_data);
Hendrik Brueckner65dbd7c2009-01-05 18:08:23 -08001750 iucv_path_free(path);
Jennifer Hunteac37312007-02-08 13:51:54 -08001751 goto fail;
1752 }
1753
1754 /* Create the new socket */
Eric W. Biederman11aa9c22015-05-08 21:09:13 -05001755 nsk = iucv_sock_alloc(NULL, sk->sk_type, GFP_ATOMIC, 0);
Heiko Carstensda99f052007-05-04 12:23:27 -07001756 if (!nsk) {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001757 err = pr_iucv->path_sever(path, user_data);
Hendrik Brueckner65dbd7c2009-01-05 18:08:23 -08001758 iucv_path_free(path);
Jennifer Hunteac37312007-02-08 13:51:54 -08001759 goto fail;
1760 }
1761
1762 niucv = iucv_sk(nsk);
1763 iucv_sock_init(nsk, sk);
1764
1765 /* Set the new iucv_sock */
1766 memcpy(niucv->dst_name, ipuser + 8, 8);
1767 EBCASC(niucv->dst_name, 8);
1768 memcpy(niucv->dst_user_id, ipvmid, 8);
1769 memcpy(niucv->src_name, iucv->src_name, 8);
1770 memcpy(niucv->src_user_id, iucv->src_user_id, 8);
1771 niucv->path = path;
1772
1773 /* Call iucv_accept */
1774 high_nmcpy(nuser_data, ipuser + 8);
1775 memcpy(nuser_data + 8, niucv->src_name, 8);
1776 ASCEBC(nuser_data + 8, 8);
1777
Hendrik Brueckner09488e2e2009-04-21 23:26:27 +00001778 /* set message limit for path based on msglimit of accepting socket */
1779 niucv->msglimit = iucv->msglimit;
1780 path->msglim = iucv->msglimit;
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001781 err = pr_iucv->path_accept(path, &af_iucv_handler, nuser_data, nsk);
Heiko Carstensda99f052007-05-04 12:23:27 -07001782 if (err) {
Ursula Braun7d316b92012-02-08 00:19:45 +00001783 iucv_sever_path(nsk, 1);
Hendrik Brueckner65dbd7c2009-01-05 18:08:23 -08001784 iucv_sock_kill(nsk);
Jennifer Hunteac37312007-02-08 13:51:54 -08001785 goto fail;
1786 }
1787
1788 iucv_accept_enqueue(sk, nsk);
1789
1790 /* Wake up accept */
1791 nsk->sk_state = IUCV_CONNECTED;
David S. Miller676d2362014-04-11 16:15:36 -04001792 sk->sk_data_ready(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -08001793 err = 0;
1794fail:
1795 bh_unlock_sock(sk);
1796 return 0;
1797}
1798
1799static void iucv_callback_connack(struct iucv_path *path, u8 ipuser[16])
1800{
1801 struct sock *sk = path->private;
1802
1803 sk->sk_state = IUCV_CONNECTED;
1804 sk->sk_state_change(sk);
1805}
1806
1807static void iucv_callback_rx(struct iucv_path *path, struct iucv_message *msg)
1808{
1809 struct sock *sk = path->private;
Jennifer Hunt561e0362007-05-04 12:22:07 -07001810 struct iucv_sock *iucv = iucv_sk(sk);
Ursula Braunf0703c82007-10-08 02:03:31 -07001811 struct sk_buff *skb;
1812 struct sock_msg_q *save_msg;
1813 int len;
Jennifer Hunt561e0362007-05-04 12:22:07 -07001814
Hendrik Bruecknerfe86e542009-04-21 06:04:22 +00001815 if (sk->sk_shutdown & RCV_SHUTDOWN) {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001816 pr_iucv->message_reject(path, msg);
Jennifer Hunteac37312007-02-08 13:51:54 -08001817 return;
Hendrik Bruecknerfe86e542009-04-21 06:04:22 +00001818 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001819
Hendrik Brueckner3fa6b5a2009-04-21 06:04:24 +00001820 spin_lock(&iucv->message_q.lock);
Jennifer Hunteac37312007-02-08 13:51:54 -08001821
Ursula Braunf0703c82007-10-08 02:03:31 -07001822 if (!list_empty(&iucv->message_q.list) ||
1823 !skb_queue_empty(&iucv->backlog_skb_q))
1824 goto save_message;
1825
1826 len = atomic_read(&sk->sk_rmem_alloc);
Eric Dumazet87fb4b72011-10-13 07:28:54 +00001827 len += SKB_TRUESIZE(iucv_msg_length(msg));
Ursula Braunf0703c82007-10-08 02:03:31 -07001828 if (len > sk->sk_rcvbuf)
1829 goto save_message;
1830
Eugene Crossera006353a92016-06-13 18:46:16 +02001831 skb = alloc_iucv_recv_skb(iucv_msg_length(msg));
Ursula Braunf0703c82007-10-08 02:03:31 -07001832 if (!skb)
1833 goto save_message;
Jennifer Hunteac37312007-02-08 13:51:54 -08001834
Ursula Braunf0703c82007-10-08 02:03:31 -07001835 iucv_process_message(sk, skb, path, msg);
Hendrik Brueckner3fa6b5a2009-04-21 06:04:24 +00001836 goto out_unlock;
Jennifer Hunteac37312007-02-08 13:51:54 -08001837
Ursula Braunf0703c82007-10-08 02:03:31 -07001838save_message:
1839 save_msg = kzalloc(sizeof(struct sock_msg_q), GFP_ATOMIC | GFP_DMA);
Ursula Braund4444722008-02-07 18:07:19 -08001840 if (!save_msg)
Julia Lawalla56635a2010-05-26 05:56:48 +00001841 goto out_unlock;
Ursula Braunf0703c82007-10-08 02:03:31 -07001842 save_msg->path = path;
1843 save_msg->msg = *msg;
1844
Ursula Braunf0703c82007-10-08 02:03:31 -07001845 list_add_tail(&save_msg->list, &iucv->message_q.list);
Hendrik Brueckner3fa6b5a2009-04-21 06:04:24 +00001846
1847out_unlock:
Ursula Braunf0703c82007-10-08 02:03:31 -07001848 spin_unlock(&iucv->message_q.lock);
Jennifer Hunteac37312007-02-08 13:51:54 -08001849}
1850
1851static void iucv_callback_txdone(struct iucv_path *path,
1852 struct iucv_message *msg)
1853{
1854 struct sock *sk = path->private;
Ursula Braunf2a77992008-02-07 18:07:44 -08001855 struct sk_buff *this = NULL;
Jennifer Hunteac37312007-02-08 13:51:54 -08001856 struct sk_buff_head *list = &iucv_sk(sk)->send_skb_q;
1857 struct sk_buff *list_skb = list->next;
1858 unsigned long flags;
1859
Ursula Braun7d316b92012-02-08 00:19:45 +00001860 bh_lock_sock(sk);
Ursula Braunf2a77992008-02-07 18:07:44 -08001861 if (!skb_queue_empty(list)) {
Jennifer Hunt561e0362007-05-04 12:22:07 -07001862 spin_lock_irqsave(&list->lock, flags);
Jennifer Hunteac37312007-02-08 13:51:54 -08001863
Ursula Braunf2a77992008-02-07 18:07:44 -08001864 while (list_skb != (struct sk_buff *)list) {
Ursula Braunf5738e22014-05-13 14:38:02 +02001865 if (msg->tag == IUCV_SKB_CB(list_skb)->tag) {
Ursula Braunf2a77992008-02-07 18:07:44 -08001866 this = list_skb;
1867 break;
1868 }
Jennifer Hunt561e0362007-05-04 12:22:07 -07001869 list_skb = list_skb->next;
Ursula Braunf2a77992008-02-07 18:07:44 -08001870 }
1871 if (this)
1872 __skb_unlink(this, list);
Jennifer Hunteac37312007-02-08 13:51:54 -08001873
Jennifer Hunt561e0362007-05-04 12:22:07 -07001874 spin_unlock_irqrestore(&list->lock, flags);
Jennifer Hunteac37312007-02-08 13:51:54 -08001875
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +00001876 if (this) {
1877 kfree_skb(this);
1878 /* wake up any process waiting for sending */
1879 iucv_sock_wake_msglim(sk);
1880 }
Jennifer Hunt561e0362007-05-04 12:22:07 -07001881 }
1882
Heiko Carstensda99f052007-05-04 12:23:27 -07001883 if (sk->sk_state == IUCV_CLOSING) {
Jennifer Hunt561e0362007-05-04 12:22:07 -07001884 if (skb_queue_empty(&iucv_sk(sk)->send_skb_q)) {
1885 sk->sk_state = IUCV_CLOSED;
1886 sk->sk_state_change(sk);
1887 }
1888 }
Ursula Braun7d316b92012-02-08 00:19:45 +00001889 bh_unlock_sock(sk);
Jennifer Hunt561e0362007-05-04 12:22:07 -07001890
Jennifer Hunteac37312007-02-08 13:51:54 -08001891}
1892
1893static void iucv_callback_connrej(struct iucv_path *path, u8 ipuser[16])
1894{
1895 struct sock *sk = path->private;
1896
Ursula Braun7d316b92012-02-08 00:19:45 +00001897 if (sk->sk_state == IUCV_CLOSED)
1898 return;
1899
1900 bh_lock_sock(sk);
1901 iucv_sever_path(sk, 1);
Ursula Braunaac63992011-12-19 22:56:31 +00001902 sk->sk_state = IUCV_DISCONN;
Jennifer Hunteac37312007-02-08 13:51:54 -08001903
1904 sk->sk_state_change(sk);
Ursula Braun7d316b92012-02-08 00:19:45 +00001905 bh_unlock_sock(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -08001906}
1907
Hendrik Brueckneraf88b522009-04-21 23:26:21 +00001908/* called if the other communication side shuts down its RECV direction;
1909 * in turn, the callback sets SEND_SHUTDOWN to disable sending of data.
1910 */
1911static void iucv_callback_shutdown(struct iucv_path *path, u8 ipuser[16])
1912{
1913 struct sock *sk = path->private;
1914
1915 bh_lock_sock(sk);
1916 if (sk->sk_state != IUCV_CLOSED) {
1917 sk->sk_shutdown |= SEND_SHUTDOWN;
1918 sk->sk_state_change(sk);
1919 }
1920 bh_unlock_sock(sk);
1921}
1922
Ursula Braun3881ac42011-08-08 01:33:54 +00001923/***************** HiperSockets transport callbacks ********************/
1924static void afiucv_swap_src_dest(struct sk_buff *skb)
1925{
1926 struct af_iucv_trans_hdr *trans_hdr =
1927 (struct af_iucv_trans_hdr *)skb->data;
1928 char tmpID[8];
1929 char tmpName[8];
1930
1931 ASCEBC(trans_hdr->destUserID, sizeof(trans_hdr->destUserID));
1932 ASCEBC(trans_hdr->destAppName, sizeof(trans_hdr->destAppName));
1933 ASCEBC(trans_hdr->srcUserID, sizeof(trans_hdr->srcUserID));
1934 ASCEBC(trans_hdr->srcAppName, sizeof(trans_hdr->srcAppName));
1935 memcpy(tmpID, trans_hdr->srcUserID, 8);
1936 memcpy(tmpName, trans_hdr->srcAppName, 8);
1937 memcpy(trans_hdr->srcUserID, trans_hdr->destUserID, 8);
1938 memcpy(trans_hdr->srcAppName, trans_hdr->destAppName, 8);
1939 memcpy(trans_hdr->destUserID, tmpID, 8);
1940 memcpy(trans_hdr->destAppName, tmpName, 8);
1941 skb_push(skb, ETH_HLEN);
1942 memset(skb->data, 0, ETH_HLEN);
1943}
1944
1945/**
1946 * afiucv_hs_callback_syn - react on received SYN
1947 **/
1948static int afiucv_hs_callback_syn(struct sock *sk, struct sk_buff *skb)
1949{
1950 struct sock *nsk;
1951 struct iucv_sock *iucv, *niucv;
1952 struct af_iucv_trans_hdr *trans_hdr;
1953 int err;
1954
1955 iucv = iucv_sk(sk);
1956 trans_hdr = (struct af_iucv_trans_hdr *)skb->data;
1957 if (!iucv) {
1958 /* no sock - connection refused */
1959 afiucv_swap_src_dest(skb);
1960 trans_hdr->flags = AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN;
1961 err = dev_queue_xmit(skb);
1962 goto out;
1963 }
1964
Eric W. Biederman11aa9c22015-05-08 21:09:13 -05001965 nsk = iucv_sock_alloc(NULL, sk->sk_type, GFP_ATOMIC, 0);
Ursula Braun3881ac42011-08-08 01:33:54 +00001966 bh_lock_sock(sk);
1967 if ((sk->sk_state != IUCV_LISTEN) ||
1968 sk_acceptq_is_full(sk) ||
1969 !nsk) {
1970 /* error on server socket - connection refused */
Ursula Braun3881ac42011-08-08 01:33:54 +00001971 afiucv_swap_src_dest(skb);
1972 trans_hdr->flags = AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN;
1973 err = dev_queue_xmit(skb);
Ursula Braun4d520f62014-05-28 10:22:29 +02001974 iucv_sock_kill(nsk);
Ursula Braun3881ac42011-08-08 01:33:54 +00001975 bh_unlock_sock(sk);
1976 goto out;
1977 }
1978
1979 niucv = iucv_sk(nsk);
1980 iucv_sock_init(nsk, sk);
1981 niucv->transport = AF_IUCV_TRANS_HIPER;
1982 niucv->msglimit = iucv->msglimit;
1983 if (!trans_hdr->window)
1984 niucv->msglimit_peer = IUCV_HIPER_MSGLIM_DEFAULT;
1985 else
1986 niucv->msglimit_peer = trans_hdr->window;
1987 memcpy(niucv->dst_name, trans_hdr->srcAppName, 8);
1988 memcpy(niucv->dst_user_id, trans_hdr->srcUserID, 8);
1989 memcpy(niucv->src_name, iucv->src_name, 8);
1990 memcpy(niucv->src_user_id, iucv->src_user_id, 8);
1991 nsk->sk_bound_dev_if = sk->sk_bound_dev_if;
Ursula Braun800c5eb2012-02-08 00:19:47 +00001992 niucv->hs_dev = iucv->hs_dev;
1993 dev_hold(niucv->hs_dev);
Ursula Braun3881ac42011-08-08 01:33:54 +00001994 afiucv_swap_src_dest(skb);
1995 trans_hdr->flags = AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_ACK;
1996 trans_hdr->window = niucv->msglimit;
1997 /* if receiver acks the xmit connection is established */
1998 err = dev_queue_xmit(skb);
1999 if (!err) {
2000 iucv_accept_enqueue(sk, nsk);
2001 nsk->sk_state = IUCV_CONNECTED;
David S. Miller676d2362014-04-11 16:15:36 -04002002 sk->sk_data_ready(sk);
Ursula Braun3881ac42011-08-08 01:33:54 +00002003 } else
2004 iucv_sock_kill(nsk);
2005 bh_unlock_sock(sk);
2006
2007out:
2008 return NET_RX_SUCCESS;
2009}
2010
2011/**
2012 * afiucv_hs_callback_synack() - react on received SYN-ACK
2013 **/
2014static int afiucv_hs_callback_synack(struct sock *sk, struct sk_buff *skb)
2015{
2016 struct iucv_sock *iucv = iucv_sk(sk);
2017 struct af_iucv_trans_hdr *trans_hdr =
2018 (struct af_iucv_trans_hdr *)skb->data;
2019
2020 if (!iucv)
2021 goto out;
2022 if (sk->sk_state != IUCV_BOUND)
2023 goto out;
2024 bh_lock_sock(sk);
2025 iucv->msglimit_peer = trans_hdr->window;
2026 sk->sk_state = IUCV_CONNECTED;
2027 sk->sk_state_change(sk);
2028 bh_unlock_sock(sk);
2029out:
2030 kfree_skb(skb);
2031 return NET_RX_SUCCESS;
2032}
2033
2034/**
2035 * afiucv_hs_callback_synfin() - react on received SYN_FIN
2036 **/
2037static int afiucv_hs_callback_synfin(struct sock *sk, struct sk_buff *skb)
2038{
2039 struct iucv_sock *iucv = iucv_sk(sk);
2040
2041 if (!iucv)
2042 goto out;
2043 if (sk->sk_state != IUCV_BOUND)
2044 goto out;
2045 bh_lock_sock(sk);
2046 sk->sk_state = IUCV_DISCONN;
2047 sk->sk_state_change(sk);
2048 bh_unlock_sock(sk);
2049out:
2050 kfree_skb(skb);
2051 return NET_RX_SUCCESS;
2052}
2053
2054/**
2055 * afiucv_hs_callback_fin() - react on received FIN
2056 **/
2057static int afiucv_hs_callback_fin(struct sock *sk, struct sk_buff *skb)
2058{
2059 struct iucv_sock *iucv = iucv_sk(sk);
2060
2061 /* other end of connection closed */
Ursula Braun800c5eb2012-02-08 00:19:47 +00002062 if (!iucv)
2063 goto out;
2064 bh_lock_sock(sk);
2065 if (sk->sk_state == IUCV_CONNECTED) {
Ursula Braunaac63992011-12-19 22:56:31 +00002066 sk->sk_state = IUCV_DISCONN;
Ursula Braun3881ac42011-08-08 01:33:54 +00002067 sk->sk_state_change(sk);
Ursula Braun3881ac42011-08-08 01:33:54 +00002068 }
Ursula Braun800c5eb2012-02-08 00:19:47 +00002069 bh_unlock_sock(sk);
2070out:
Ursula Braun3881ac42011-08-08 01:33:54 +00002071 kfree_skb(skb);
2072 return NET_RX_SUCCESS;
2073}
2074
2075/**
2076 * afiucv_hs_callback_win() - react on received WIN
2077 **/
2078static int afiucv_hs_callback_win(struct sock *sk, struct sk_buff *skb)
2079{
2080 struct iucv_sock *iucv = iucv_sk(sk);
2081 struct af_iucv_trans_hdr *trans_hdr =
2082 (struct af_iucv_trans_hdr *)skb->data;
2083
2084 if (!iucv)
2085 return NET_RX_SUCCESS;
2086
2087 if (sk->sk_state != IUCV_CONNECTED)
2088 return NET_RX_SUCCESS;
2089
2090 atomic_sub(trans_hdr->window, &iucv->msg_sent);
2091 iucv_sock_wake_msglim(sk);
2092 return NET_RX_SUCCESS;
2093}
2094
2095/**
2096 * afiucv_hs_callback_rx() - react on received data
2097 **/
2098static int afiucv_hs_callback_rx(struct sock *sk, struct sk_buff *skb)
2099{
2100 struct iucv_sock *iucv = iucv_sk(sk);
2101
2102 if (!iucv) {
2103 kfree_skb(skb);
2104 return NET_RX_SUCCESS;
2105 }
2106
2107 if (sk->sk_state != IUCV_CONNECTED) {
2108 kfree_skb(skb);
2109 return NET_RX_SUCCESS;
2110 }
2111
Ursula Braun82492a32012-03-07 02:06:24 +00002112 if (sk->sk_shutdown & RCV_SHUTDOWN) {
2113 kfree_skb(skb);
2114 return NET_RX_SUCCESS;
2115 }
2116
Eugene Crosser979f66b2015-12-11 12:27:51 +01002117 /* write stuff from iucv_msg to skb cb */
Ursula Braun3881ac42011-08-08 01:33:54 +00002118 skb_pull(skb, sizeof(struct af_iucv_trans_hdr));
2119 skb_reset_transport_header(skb);
2120 skb_reset_network_header(skb);
Ursula Braunf9c41a62013-04-07 22:19:26 +00002121 IUCV_SKB_CB(skb)->offset = 0;
Ursula Braun3881ac42011-08-08 01:33:54 +00002122 spin_lock(&iucv->message_q.lock);
2123 if (skb_queue_empty(&iucv->backlog_skb_q)) {
2124 if (sock_queue_rcv_skb(sk, skb)) {
2125 /* handle rcv queue full */
2126 skb_queue_tail(&iucv->backlog_skb_q, skb);
2127 }
2128 } else
2129 skb_queue_tail(&iucv_sk(sk)->backlog_skb_q, skb);
2130 spin_unlock(&iucv->message_q.lock);
2131 return NET_RX_SUCCESS;
2132}
2133
2134/**
2135 * afiucv_hs_rcv() - base function for arriving data through HiperSockets
2136 * transport
2137 * called from netif RX softirq
2138 **/
2139static int afiucv_hs_rcv(struct sk_buff *skb, struct net_device *dev,
2140 struct packet_type *pt, struct net_device *orig_dev)
2141{
Ursula Braun3881ac42011-08-08 01:33:54 +00002142 struct sock *sk;
2143 struct iucv_sock *iucv;
2144 struct af_iucv_trans_hdr *trans_hdr;
2145 char nullstring[8];
2146 int err = 0;
2147
Eugene Crosser979f66b2015-12-11 12:27:51 +01002148 if (skb->len < (ETH_HLEN + sizeof(struct af_iucv_trans_hdr))) {
2149 WARN_ONCE(1, "AF_IUCV too short skb, len=%d, min=%d",
2150 (int)skb->len,
2151 (int)(ETH_HLEN + sizeof(struct af_iucv_trans_hdr)));
2152 kfree_skb(skb);
2153 return NET_RX_SUCCESS;
2154 }
2155 if (skb_headlen(skb) < (ETH_HLEN + sizeof(struct af_iucv_trans_hdr)))
2156 if (skb_linearize(skb)) {
2157 WARN_ONCE(1, "AF_IUCV skb_linearize failed, len=%d",
2158 (int)skb->len);
2159 kfree_skb(skb);
2160 return NET_RX_SUCCESS;
2161 }
Ursula Braun3881ac42011-08-08 01:33:54 +00002162 skb_pull(skb, ETH_HLEN);
2163 trans_hdr = (struct af_iucv_trans_hdr *)skb->data;
2164 EBCASC(trans_hdr->destAppName, sizeof(trans_hdr->destAppName));
2165 EBCASC(trans_hdr->destUserID, sizeof(trans_hdr->destUserID));
2166 EBCASC(trans_hdr->srcAppName, sizeof(trans_hdr->srcAppName));
2167 EBCASC(trans_hdr->srcUserID, sizeof(trans_hdr->srcUserID));
2168 memset(nullstring, 0, sizeof(nullstring));
2169 iucv = NULL;
2170 sk = NULL;
2171 read_lock(&iucv_sk_list.lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08002172 sk_for_each(sk, &iucv_sk_list.head) {
Ursula Braun3881ac42011-08-08 01:33:54 +00002173 if (trans_hdr->flags == AF_IUCV_FLAG_SYN) {
2174 if ((!memcmp(&iucv_sk(sk)->src_name,
2175 trans_hdr->destAppName, 8)) &&
2176 (!memcmp(&iucv_sk(sk)->src_user_id,
2177 trans_hdr->destUserID, 8)) &&
2178 (!memcmp(&iucv_sk(sk)->dst_name, nullstring, 8)) &&
2179 (!memcmp(&iucv_sk(sk)->dst_user_id,
2180 nullstring, 8))) {
2181 iucv = iucv_sk(sk);
2182 break;
2183 }
2184 } else {
2185 if ((!memcmp(&iucv_sk(sk)->src_name,
2186 trans_hdr->destAppName, 8)) &&
2187 (!memcmp(&iucv_sk(sk)->src_user_id,
2188 trans_hdr->destUserID, 8)) &&
2189 (!memcmp(&iucv_sk(sk)->dst_name,
2190 trans_hdr->srcAppName, 8)) &&
2191 (!memcmp(&iucv_sk(sk)->dst_user_id,
2192 trans_hdr->srcUserID, 8))) {
2193 iucv = iucv_sk(sk);
2194 break;
2195 }
2196 }
2197 }
2198 read_unlock(&iucv_sk_list.lock);
2199 if (!iucv)
2200 sk = NULL;
2201
2202 /* no sock
2203 how should we send with no sock
2204 1) send without sock no send rc checking?
2205 2) introduce default sock to handle this cases
2206
2207 SYN -> send SYN|ACK in good case, send SYN|FIN in bad case
2208 data -> send FIN
2209 SYN|ACK, SYN|FIN, FIN -> no action? */
2210
2211 switch (trans_hdr->flags) {
2212 case AF_IUCV_FLAG_SYN:
2213 /* connect request */
2214 err = afiucv_hs_callback_syn(sk, skb);
2215 break;
2216 case (AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_ACK):
2217 /* connect request confirmed */
2218 err = afiucv_hs_callback_synack(sk, skb);
2219 break;
2220 case (AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN):
2221 /* connect request refused */
2222 err = afiucv_hs_callback_synfin(sk, skb);
2223 break;
2224 case (AF_IUCV_FLAG_FIN):
2225 /* close request */
2226 err = afiucv_hs_callback_fin(sk, skb);
2227 break;
2228 case (AF_IUCV_FLAG_WIN):
2229 err = afiucv_hs_callback_win(sk, skb);
Ursula Braun800c5eb2012-02-08 00:19:47 +00002230 if (skb->len == sizeof(struct af_iucv_trans_hdr)) {
2231 kfree_skb(skb);
2232 break;
2233 }
Ursula Braun82492a32012-03-07 02:06:24 +00002234 /* fall through and receive non-zero length data */
2235 case (AF_IUCV_FLAG_SHT):
2236 /* shutdown request */
2237 /* fall through and receive zero length data */
Ursula Braun3881ac42011-08-08 01:33:54 +00002238 case 0:
2239 /* plain data frame */
Ursula Braunf9c41a62013-04-07 22:19:26 +00002240 IUCV_SKB_CB(skb)->class = trans_hdr->iucv_hdr.class;
Ursula Braun3881ac42011-08-08 01:33:54 +00002241 err = afiucv_hs_callback_rx(sk, skb);
2242 break;
2243 default:
2244 ;
2245 }
2246
2247 return err;
2248}
2249
2250/**
2251 * afiucv_hs_callback_txnotify() - handle send notifcations from HiperSockets
2252 * transport
2253 **/
2254static void afiucv_hs_callback_txnotify(struct sk_buff *skb,
2255 enum iucv_tx_notify n)
2256{
2257 struct sock *isk = skb->sk;
2258 struct sock *sk = NULL;
2259 struct iucv_sock *iucv = NULL;
2260 struct sk_buff_head *list;
2261 struct sk_buff *list_skb;
Ursula Braun800c5eb2012-02-08 00:19:47 +00002262 struct sk_buff *nskb;
Ursula Braun3881ac42011-08-08 01:33:54 +00002263 unsigned long flags;
Ursula Braun3881ac42011-08-08 01:33:54 +00002264
Ursula Braun800c5eb2012-02-08 00:19:47 +00002265 read_lock_irqsave(&iucv_sk_list.lock, flags);
Sasha Levinb67bfe02013-02-27 17:06:00 -08002266 sk_for_each(sk, &iucv_sk_list.head)
Ursula Braun3881ac42011-08-08 01:33:54 +00002267 if (sk == isk) {
2268 iucv = iucv_sk(sk);
2269 break;
2270 }
Ursula Braun800c5eb2012-02-08 00:19:47 +00002271 read_unlock_irqrestore(&iucv_sk_list.lock, flags);
Ursula Braun3881ac42011-08-08 01:33:54 +00002272
Ursula Braun800c5eb2012-02-08 00:19:47 +00002273 if (!iucv || sock_flag(sk, SOCK_ZAPPED))
Ursula Braun3881ac42011-08-08 01:33:54 +00002274 return;
2275
Ursula Braun3881ac42011-08-08 01:33:54 +00002276 list = &iucv->send_skb_q;
Ursula Braun800c5eb2012-02-08 00:19:47 +00002277 spin_lock_irqsave(&list->lock, flags);
Ursula Braun3881ac42011-08-08 01:33:54 +00002278 if (skb_queue_empty(list))
2279 goto out_unlock;
Ursula Braun800c5eb2012-02-08 00:19:47 +00002280 list_skb = list->next;
2281 nskb = list_skb->next;
Ursula Braun3881ac42011-08-08 01:33:54 +00002282 while (list_skb != (struct sk_buff *)list) {
2283 if (skb_shinfo(list_skb) == skb_shinfo(skb)) {
Ursula Braun3881ac42011-08-08 01:33:54 +00002284 switch (n) {
2285 case TX_NOTIFY_OK:
Ursula Braun800c5eb2012-02-08 00:19:47 +00002286 __skb_unlink(list_skb, list);
2287 kfree_skb(list_skb);
Ursula Braun3881ac42011-08-08 01:33:54 +00002288 iucv_sock_wake_msglim(sk);
Ursula Braun3881ac42011-08-08 01:33:54 +00002289 break;
2290 case TX_NOTIFY_PENDING:
2291 atomic_inc(&iucv->pendings);
2292 break;
2293 case TX_NOTIFY_DELAYED_OK:
Ursula Braun800c5eb2012-02-08 00:19:47 +00002294 __skb_unlink(list_skb, list);
Ursula Braun3881ac42011-08-08 01:33:54 +00002295 atomic_dec(&iucv->pendings);
2296 if (atomic_read(&iucv->pendings) <= 0)
2297 iucv_sock_wake_msglim(sk);
Ursula Braun800c5eb2012-02-08 00:19:47 +00002298 kfree_skb(list_skb);
Ursula Braun3881ac42011-08-08 01:33:54 +00002299 break;
2300 case TX_NOTIFY_UNREACHABLE:
2301 case TX_NOTIFY_DELAYED_UNREACHABLE:
2302 case TX_NOTIFY_TPQFULL: /* not yet used */
2303 case TX_NOTIFY_GENERALERROR:
2304 case TX_NOTIFY_DELAYED_GENERALERROR:
Ursula Braun800c5eb2012-02-08 00:19:47 +00002305 __skb_unlink(list_skb, list);
2306 kfree_skb(list_skb);
2307 if (sk->sk_state == IUCV_CONNECTED) {
2308 sk->sk_state = IUCV_DISCONN;
2309 sk->sk_state_change(sk);
2310 }
Ursula Braun3881ac42011-08-08 01:33:54 +00002311 break;
2312 }
2313 break;
2314 }
Ursula Braun800c5eb2012-02-08 00:19:47 +00002315 list_skb = nskb;
2316 nskb = nskb->next;
Ursula Braun3881ac42011-08-08 01:33:54 +00002317 }
Ursula Braun800c5eb2012-02-08 00:19:47 +00002318out_unlock:
Ursula Braun3881ac42011-08-08 01:33:54 +00002319 spin_unlock_irqrestore(&list->lock, flags);
2320
Ursula Braun42bd48e2011-12-19 22:56:28 +00002321 if (sk->sk_state == IUCV_CLOSING) {
2322 if (skb_queue_empty(&iucv_sk(sk)->send_skb_q)) {
2323 sk->sk_state = IUCV_CLOSED;
2324 sk->sk_state_change(sk);
2325 }
2326 }
2327
Ursula Braun3881ac42011-08-08 01:33:54 +00002328}
Ursula Braun9fbd87d2012-03-07 02:06:23 +00002329
2330/*
2331 * afiucv_netdev_event: handle netdev notifier chain events
2332 */
2333static int afiucv_netdev_event(struct notifier_block *this,
2334 unsigned long event, void *ptr)
2335{
Jiri Pirko351638e2013-05-28 01:30:21 +00002336 struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
Ursula Braun9fbd87d2012-03-07 02:06:23 +00002337 struct sock *sk;
2338 struct iucv_sock *iucv;
2339
2340 switch (event) {
2341 case NETDEV_REBOOT:
2342 case NETDEV_GOING_DOWN:
Sasha Levinb67bfe02013-02-27 17:06:00 -08002343 sk_for_each(sk, &iucv_sk_list.head) {
Ursula Braun9fbd87d2012-03-07 02:06:23 +00002344 iucv = iucv_sk(sk);
2345 if ((iucv->hs_dev == event_dev) &&
2346 (sk->sk_state == IUCV_CONNECTED)) {
2347 if (event == NETDEV_GOING_DOWN)
2348 iucv_send_ctrl(sk, AF_IUCV_FLAG_FIN);
2349 sk->sk_state = IUCV_DISCONN;
2350 sk->sk_state_change(sk);
2351 }
2352 }
2353 break;
2354 case NETDEV_DOWN:
2355 case NETDEV_UNREGISTER:
2356 default:
2357 break;
2358 }
2359 return NOTIFY_DONE;
2360}
2361
2362static struct notifier_block afiucv_netdev_notifier = {
2363 .notifier_call = afiucv_netdev_event,
2364};
2365
Alexey Dobriyan5708e862009-09-14 12:23:23 +00002366static const struct proto_ops iucv_sock_ops = {
Jennifer Hunteac37312007-02-08 13:51:54 -08002367 .family = PF_IUCV,
2368 .owner = THIS_MODULE,
2369 .release = iucv_sock_release,
2370 .bind = iucv_sock_bind,
2371 .connect = iucv_sock_connect,
2372 .listen = iucv_sock_listen,
2373 .accept = iucv_sock_accept,
2374 .getname = iucv_sock_getname,
2375 .sendmsg = iucv_sock_sendmsg,
2376 .recvmsg = iucv_sock_recvmsg,
2377 .poll = iucv_sock_poll,
2378 .ioctl = sock_no_ioctl,
2379 .mmap = sock_no_mmap,
2380 .socketpair = sock_no_socketpair,
2381 .shutdown = iucv_sock_shutdown,
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00002382 .setsockopt = iucv_sock_setsockopt,
2383 .getsockopt = iucv_sock_getsockopt,
Jennifer Hunteac37312007-02-08 13:51:54 -08002384};
2385
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00002386static const struct net_proto_family iucv_sock_family_ops = {
Jennifer Hunteac37312007-02-08 13:51:54 -08002387 .family = AF_IUCV,
2388 .owner = THIS_MODULE,
2389 .create = iucv_sock_create,
2390};
2391
Ursula Braun3881ac42011-08-08 01:33:54 +00002392static struct packet_type iucv_packet_type = {
2393 .type = cpu_to_be16(ETH_P_AF_IUCV),
2394 .func = afiucv_hs_rcv,
2395};
2396
2397static int afiucv_iucv_init(void)
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00002398{
2399 int err;
2400
2401 err = pr_iucv->iucv_register(&af_iucv_handler, 0);
2402 if (err)
2403 goto out;
2404 /* establish dummy device */
2405 af_iucv_driver.bus = pr_iucv->bus;
2406 err = driver_register(&af_iucv_driver);
2407 if (err)
2408 goto out_iucv;
2409 af_iucv_dev = kzalloc(sizeof(struct device), GFP_KERNEL);
2410 if (!af_iucv_dev) {
2411 err = -ENOMEM;
2412 goto out_driver;
2413 }
2414 dev_set_name(af_iucv_dev, "af_iucv");
2415 af_iucv_dev->bus = pr_iucv->bus;
2416 af_iucv_dev->parent = pr_iucv->root;
2417 af_iucv_dev->release = (void (*)(struct device *))kfree;
2418 af_iucv_dev->driver = &af_iucv_driver;
2419 err = device_register(af_iucv_dev);
2420 if (err)
Arvind Yadav7d914872018-03-13 16:50:06 +01002421 goto out_iucv_dev;
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00002422 return 0;
2423
Arvind Yadav7d914872018-03-13 16:50:06 +01002424out_iucv_dev:
2425 put_device(af_iucv_dev);
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00002426out_driver:
2427 driver_unregister(&af_iucv_driver);
2428out_iucv:
2429 pr_iucv->iucv_unregister(&af_iucv_handler, 0);
2430out:
2431 return err;
2432}
2433
Heiko Carstensda99f052007-05-04 12:23:27 -07002434static int __init afiucv_init(void)
Jennifer Hunteac37312007-02-08 13:51:54 -08002435{
2436 int err;
2437
Ursula Braun3881ac42011-08-08 01:33:54 +00002438 if (MACHINE_IS_VM) {
2439 cpcmd("QUERY USERID", iucv_userid, sizeof(iucv_userid), &err);
2440 if (unlikely(err)) {
2441 WARN_ON(err);
2442 err = -EPROTONOSUPPORT;
2443 goto out;
2444 }
Jennifer Hunteac37312007-02-08 13:51:54 -08002445
Ursula Braun3881ac42011-08-08 01:33:54 +00002446 pr_iucv = try_then_request_module(symbol_get(iucv_if), "iucv");
2447 if (!pr_iucv) {
2448 printk(KERN_WARNING "iucv_if lookup failed\n");
2449 memset(&iucv_userid, 0, sizeof(iucv_userid));
2450 }
2451 } else {
2452 memset(&iucv_userid, 0, sizeof(iucv_userid));
2453 pr_iucv = NULL;
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00002454 }
2455
Jennifer Hunteac37312007-02-08 13:51:54 -08002456 err = proto_register(&iucv_proto, 0);
2457 if (err)
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00002458 goto out;
Jennifer Hunteac37312007-02-08 13:51:54 -08002459 err = sock_register(&iucv_sock_family_ops);
2460 if (err)
2461 goto out_proto;
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00002462
Ursula Braun3881ac42011-08-08 01:33:54 +00002463 if (pr_iucv) {
2464 err = afiucv_iucv_init();
2465 if (err)
2466 goto out_sock;
Ursula Braun9fbd87d2012-03-07 02:06:23 +00002467 } else
2468 register_netdevice_notifier(&afiucv_netdev_notifier);
Ursula Braun3881ac42011-08-08 01:33:54 +00002469 dev_add_pack(&iucv_packet_type);
Jennifer Hunteac37312007-02-08 13:51:54 -08002470 return 0;
2471
Ursula Braunc23cad92009-06-16 10:30:44 +02002472out_sock:
2473 sock_unregister(PF_IUCV);
Jennifer Hunteac37312007-02-08 13:51:54 -08002474out_proto:
2475 proto_unregister(&iucv_proto);
Jennifer Hunteac37312007-02-08 13:51:54 -08002476out:
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00002477 if (pr_iucv)
2478 symbol_put(iucv_if);
Jennifer Hunteac37312007-02-08 13:51:54 -08002479 return err;
2480}
2481
2482static void __exit afiucv_exit(void)
2483{
Ursula Braun3881ac42011-08-08 01:33:54 +00002484 if (pr_iucv) {
2485 device_unregister(af_iucv_dev);
2486 driver_unregister(&af_iucv_driver);
2487 pr_iucv->iucv_unregister(&af_iucv_handler, 0);
2488 symbol_put(iucv_if);
Ursula Braun9fbd87d2012-03-07 02:06:23 +00002489 } else
2490 unregister_netdevice_notifier(&afiucv_netdev_notifier);
Ursula Braun3881ac42011-08-08 01:33:54 +00002491 dev_remove_pack(&iucv_packet_type);
Jennifer Hunteac37312007-02-08 13:51:54 -08002492 sock_unregister(PF_IUCV);
2493 proto_unregister(&iucv_proto);
Jennifer Hunteac37312007-02-08 13:51:54 -08002494}
2495
2496module_init(afiucv_init);
2497module_exit(afiucv_exit);
2498
2499MODULE_AUTHOR("Jennifer Hunt <jenhunt@us.ibm.com>");
2500MODULE_DESCRIPTION("IUCV Sockets ver " VERSION);
2501MODULE_VERSION(VERSION);
2502MODULE_LICENSE("GPL");
2503MODULE_ALIAS_NETPROTO(PF_IUCV);
Ursula Braun3881ac42011-08-08 01:33:54 +00002504