blob: 05112094d76b4b65ac6997ab20ba9d45f374e856 [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>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010020#include <linux/sched/signal.h>
Jennifer Hunteac37312007-02-08 13:51:54 -080021#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);
Johannes Bergd58ff352017-06-16 14:29:23 +0200325 phs_hdr = skb_push(skb, sizeof(struct af_iucv_trans_hdr));
Ursula Braun3881ac42011-08-08 01:33:54 +0000326 skb_reset_mac_header(skb);
327 skb_reset_network_header(skb);
328 skb_push(skb, ETH_HLEN);
329 skb_reset_mac_header(skb);
330 memset(phs_hdr, 0, sizeof(struct af_iucv_trans_hdr));
331
332 phs_hdr->magic = ETH_P_AF_IUCV;
333 phs_hdr->version = 1;
334 phs_hdr->flags = flags;
335 if (flags == AF_IUCV_FLAG_SYN)
336 phs_hdr->window = iucv->msglimit;
337 else if ((flags == AF_IUCV_FLAG_WIN) || !flags) {
338 confirm_recv = atomic_read(&iucv->msg_recv);
339 phs_hdr->window = confirm_recv;
340 if (confirm_recv)
341 phs_hdr->flags = phs_hdr->flags | AF_IUCV_FLAG_WIN;
342 }
343 memcpy(phs_hdr->destUserID, iucv->dst_user_id, 8);
344 memcpy(phs_hdr->destAppName, iucv->dst_name, 8);
345 memcpy(phs_hdr->srcUserID, iucv->src_user_id, 8);
346 memcpy(phs_hdr->srcAppName, iucv->src_name, 8);
347 ASCEBC(phs_hdr->destUserID, sizeof(phs_hdr->destUserID));
348 ASCEBC(phs_hdr->destAppName, sizeof(phs_hdr->destAppName));
349 ASCEBC(phs_hdr->srcUserID, sizeof(phs_hdr->srcUserID));
350 ASCEBC(phs_hdr->srcAppName, sizeof(phs_hdr->srcAppName));
351 if (imsg)
352 memcpy(&phs_hdr->iucv_hdr, imsg, sizeof(struct iucv_message));
353
Ursula Braun800c5eb2012-02-08 00:19:47 +0000354 skb->dev = iucv->hs_dev;
Ursula Braun3881ac42011-08-08 01:33:54 +0000355 if (!skb->dev)
356 return -ENODEV;
Ursula Braun800c5eb2012-02-08 00:19:47 +0000357 if (!(skb->dev->flags & IFF_UP) || !netif_carrier_ok(skb->dev))
Ursula Braun3881ac42011-08-08 01:33:54 +0000358 return -ENETDOWN;
359 if (skb->len > skb->dev->mtu) {
360 if (sock->sk_type == SOCK_SEQPACKET)
361 return -EMSGSIZE;
362 else
363 skb_trim(skb, skb->dev->mtu);
364 }
Hans Wippel2e56c262017-06-23 19:32:27 +0200365 skb->protocol = cpu_to_be16(ETH_P_AF_IUCV);
Ursula Braun3881ac42011-08-08 01:33:54 +0000366 nskb = skb_clone(skb, GFP_ATOMIC);
367 if (!nskb)
368 return -ENOMEM;
369 skb_queue_tail(&iucv->send_skb_q, nskb);
370 err = dev_queue_xmit(skb);
Ursula Braun800c5eb2012-02-08 00:19:47 +0000371 if (net_xmit_eval(err)) {
Ursula Braun3881ac42011-08-08 01:33:54 +0000372 skb_unlink(nskb, &iucv->send_skb_q);
373 kfree_skb(nskb);
374 } else {
375 atomic_sub(confirm_recv, &iucv->msg_recv);
376 WARN_ON(atomic_read(&iucv->msg_recv) < 0);
377 }
Ursula Braun800c5eb2012-02-08 00:19:47 +0000378 return net_xmit_eval(err);
Ursula Braun3881ac42011-08-08 01:33:54 +0000379}
380
Jennifer Hunteac37312007-02-08 13:51:54 -0800381static struct sock *__iucv_get_sock_by_name(char *nm)
382{
383 struct sock *sk;
Jennifer Hunteac37312007-02-08 13:51:54 -0800384
Sasha Levinb67bfe02013-02-27 17:06:00 -0800385 sk_for_each(sk, &iucv_sk_list.head)
Jennifer Hunteac37312007-02-08 13:51:54 -0800386 if (!memcmp(&iucv_sk(sk)->src_name, nm, 8))
387 return sk;
388
389 return NULL;
390}
391
392static void iucv_sock_destruct(struct sock *sk)
393{
394 skb_queue_purge(&sk->sk_receive_queue);
Ursula Braun82492a32012-03-07 02:06:24 +0000395 skb_queue_purge(&sk->sk_error_queue);
396
397 sk_mem_reclaim(sk);
398
399 if (!sock_flag(sk, SOCK_DEAD)) {
400 pr_err("Attempt to release alive iucv socket %p\n", sk);
401 return;
402 }
403
404 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
405 WARN_ON(atomic_read(&sk->sk_wmem_alloc));
406 WARN_ON(sk->sk_wmem_queued);
407 WARN_ON(sk->sk_forward_alloc);
Jennifer Hunteac37312007-02-08 13:51:54 -0800408}
409
410/* Cleanup Listen */
411static void iucv_sock_cleanup_listen(struct sock *parent)
412{
413 struct sock *sk;
414
415 /* Close non-accepted connections */
416 while ((sk = iucv_accept_dequeue(parent, NULL))) {
417 iucv_sock_close(sk);
418 iucv_sock_kill(sk);
419 }
420
421 parent->sk_state = IUCV_CLOSED;
Jennifer Hunteac37312007-02-08 13:51:54 -0800422}
423
Hendrik Brueckner7514bab2009-09-16 04:37:27 +0000424/* Kill socket (only if zapped and orphaned) */
Jennifer Hunteac37312007-02-08 13:51:54 -0800425static void iucv_sock_kill(struct sock *sk)
426{
427 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
428 return;
429
430 iucv_sock_unlink(&iucv_sk_list, sk);
431 sock_set_flag(sk, SOCK_DEAD);
432 sock_put(sk);
433}
434
Ursula Braun7d316b92012-02-08 00:19:45 +0000435/* Terminate an IUCV path */
436static void iucv_sever_path(struct sock *sk, int with_user_data)
437{
438 unsigned char user_data[16];
439 struct iucv_sock *iucv = iucv_sk(sk);
440 struct iucv_path *path = iucv->path;
441
442 if (iucv->path) {
443 iucv->path = NULL;
444 if (with_user_data) {
445 low_nmcpy(user_data, iucv->src_name);
446 high_nmcpy(user_data, iucv->dst_name);
447 ASCEBC(user_data, sizeof(user_data));
448 pr_iucv->path_sever(path, user_data);
449 } else
450 pr_iucv->path_sever(path, NULL);
451 iucv_path_free(path);
452 }
453}
454
Ursula Braun4e0ad322016-10-07 15:51:47 +0200455/* Send controlling flags through an IUCV socket for HIPER transport */
Ursula Braun9fbd87d2012-03-07 02:06:23 +0000456static int iucv_send_ctrl(struct sock *sk, u8 flags)
457{
458 int err = 0;
459 int blen;
460 struct sk_buff *skb;
Ursula Braun4e0ad322016-10-07 15:51:47 +0200461 u8 shutdown = 0;
Ursula Braun9fbd87d2012-03-07 02:06:23 +0000462
463 blen = sizeof(struct af_iucv_trans_hdr) + ETH_HLEN;
Ursula Braun4e0ad322016-10-07 15:51:47 +0200464 if (sk->sk_shutdown & SEND_SHUTDOWN) {
465 /* controlling flags should be sent anyway */
466 shutdown = sk->sk_shutdown;
467 sk->sk_shutdown &= RCV_SHUTDOWN;
468 }
Ursula Braun9fbd87d2012-03-07 02:06:23 +0000469 skb = sock_alloc_send_skb(sk, blen, 1, &err);
470 if (skb) {
471 skb_reserve(skb, blen);
472 err = afiucv_hs_send(NULL, sk, skb, flags);
473 }
Ursula Braun4e0ad322016-10-07 15:51:47 +0200474 if (shutdown)
475 sk->sk_shutdown = shutdown;
Ursula Braun9fbd87d2012-03-07 02:06:23 +0000476 return err;
477}
478
Jennifer Hunteac37312007-02-08 13:51:54 -0800479/* Close an IUCV socket */
480static void iucv_sock_close(struct sock *sk)
481{
Jennifer Hunteac37312007-02-08 13:51:54 -0800482 struct iucv_sock *iucv = iucv_sk(sk);
Jennifer Hunt561e0362007-05-04 12:22:07 -0700483 unsigned long timeo;
Ursula Braun800c5eb2012-02-08 00:19:47 +0000484 int err = 0;
Jennifer Hunteac37312007-02-08 13:51:54 -0800485
Jennifer Hunteac37312007-02-08 13:51:54 -0800486 lock_sock(sk);
487
Heiko Carstensda99f052007-05-04 12:23:27 -0700488 switch (sk->sk_state) {
Jennifer Hunteac37312007-02-08 13:51:54 -0800489 case IUCV_LISTEN:
490 iucv_sock_cleanup_listen(sk);
491 break;
492
493 case IUCV_CONNECTED:
Ursula Braun3881ac42011-08-08 01:33:54 +0000494 if (iucv->transport == AF_IUCV_TRANS_HIPER) {
Ursula Braun9fbd87d2012-03-07 02:06:23 +0000495 err = iucv_send_ctrl(sk, AF_IUCV_FLAG_FIN);
Ursula Braun3881ac42011-08-08 01:33:54 +0000496 sk->sk_state = IUCV_DISCONN;
497 sk->sk_state_change(sk);
498 }
Ursula Braun816abba2011-12-19 22:56:29 +0000499 case IUCV_DISCONN: /* fall through */
Jennifer Hunt561e0362007-05-04 12:22:07 -0700500 sk->sk_state = IUCV_CLOSING;
501 sk->sk_state_change(sk);
502
Ursula Braun800c5eb2012-02-08 00:19:47 +0000503 if (!err && !skb_queue_empty(&iucv->send_skb_q)) {
Jennifer Hunt561e0362007-05-04 12:22:07 -0700504 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
505 timeo = sk->sk_lingertime;
506 else
507 timeo = IUCV_DISCONN_TIMEOUT;
Ursula Braun9f6298a2011-05-12 18:45:08 +0000508 iucv_sock_wait(sk,
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +0000509 iucv_sock_in_state(sk, IUCV_CLOSED, 0),
510 timeo);
Jennifer Hunt561e0362007-05-04 12:22:07 -0700511 }
512
Ursula Braunbbe188c2009-04-21 06:04:20 +0000513 case IUCV_CLOSING: /* fall through */
Jennifer Hunt561e0362007-05-04 12:22:07 -0700514 sk->sk_state = IUCV_CLOSED;
515 sk->sk_state_change(sk);
516
Jennifer Hunteac37312007-02-08 13:51:54 -0800517 sk->sk_err = ECONNRESET;
518 sk->sk_state_change(sk);
519
Ursula Braun800c5eb2012-02-08 00:19:47 +0000520 skb_queue_purge(&iucv->send_skb_q);
Jennifer Hunt561e0362007-05-04 12:22:07 -0700521 skb_queue_purge(&iucv->backlog_skb_q);
Jennifer Hunteac37312007-02-08 13:51:54 -0800522
Ursula Braun7d316b92012-02-08 00:19:45 +0000523 default: /* fall through */
524 iucv_sever_path(sk, 1);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700525 }
Jennifer Hunteac37312007-02-08 13:51:54 -0800526
Ursula Braun800c5eb2012-02-08 00:19:47 +0000527 if (iucv->hs_dev) {
528 dev_put(iucv->hs_dev);
529 iucv->hs_dev = NULL;
530 sk->sk_bound_dev_if = 0;
531 }
532
Hendrik Brueckner7514bab2009-09-16 04:37:27 +0000533 /* mark socket for deletion by iucv_sock_kill() */
534 sock_set_flag(sk, SOCK_ZAPPED);
535
Jennifer Hunteac37312007-02-08 13:51:54 -0800536 release_sock(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800537}
538
539static void iucv_sock_init(struct sock *sk, struct sock *parent)
540{
Paul Moore02f06912016-06-07 10:30:53 -0400541 if (parent) {
Jennifer Hunteac37312007-02-08 13:51:54 -0800542 sk->sk_type = parent->sk_type;
Paul Moore02f06912016-06-07 10:30:53 -0400543 security_sk_clone(parent, sk);
544 }
Jennifer Hunteac37312007-02-08 13:51:54 -0800545}
546
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500547static struct sock *iucv_sock_alloc(struct socket *sock, int proto, gfp_t prio, int kern)
Jennifer Hunteac37312007-02-08 13:51:54 -0800548{
549 struct sock *sk;
Ursula Braun493d3972011-08-08 01:33:52 +0000550 struct iucv_sock *iucv;
Jennifer Hunteac37312007-02-08 13:51:54 -0800551
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500552 sk = sk_alloc(&init_net, PF_IUCV, prio, &iucv_proto, kern);
Jennifer Hunteac37312007-02-08 13:51:54 -0800553 if (!sk)
554 return NULL;
Ursula Braun493d3972011-08-08 01:33:52 +0000555 iucv = iucv_sk(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800556
557 sock_init_data(sock, sk);
Ursula Braun493d3972011-08-08 01:33:52 +0000558 INIT_LIST_HEAD(&iucv->accept_q);
559 spin_lock_init(&iucv->accept_q_lock);
560 skb_queue_head_init(&iucv->send_skb_q);
561 INIT_LIST_HEAD(&iucv->message_q.list);
562 spin_lock_init(&iucv->message_q.lock);
563 skb_queue_head_init(&iucv->backlog_skb_q);
564 iucv->send_tag = 0;
Ursula Braun3881ac42011-08-08 01:33:54 +0000565 atomic_set(&iucv->pendings, 0);
Ursula Braun493d3972011-08-08 01:33:52 +0000566 iucv->flags = 0;
Ursula Braun3881ac42011-08-08 01:33:54 +0000567 iucv->msglimit = 0;
568 atomic_set(&iucv->msg_sent, 0);
569 atomic_set(&iucv->msg_recv, 0);
Ursula Braun493d3972011-08-08 01:33:52 +0000570 iucv->path = NULL;
Ursula Braun3881ac42011-08-08 01:33:54 +0000571 iucv->sk_txnotify = afiucv_hs_callback_txnotify;
Ursula Braun493d3972011-08-08 01:33:52 +0000572 memset(&iucv->src_user_id , 0, 32);
Ursula Braun3881ac42011-08-08 01:33:54 +0000573 if (pr_iucv)
574 iucv->transport = AF_IUCV_TRANS_IUCV;
575 else
576 iucv->transport = AF_IUCV_TRANS_HIPER;
Jennifer Hunteac37312007-02-08 13:51:54 -0800577
578 sk->sk_destruct = iucv_sock_destruct;
579 sk->sk_sndtimeo = IUCV_CONN_TIMEOUT;
580 sk->sk_allocation = GFP_DMA;
581
582 sock_reset_flag(sk, SOCK_ZAPPED);
583
584 sk->sk_protocol = proto;
585 sk->sk_state = IUCV_OPEN;
586
Jennifer Hunteac37312007-02-08 13:51:54 -0800587 iucv_sock_link(&iucv_sk_list, sk);
588 return sk;
589}
590
591/* Create an IUCV socket */
Eric Paris3f378b62009-11-05 22:18:14 -0800592static int iucv_sock_create(struct net *net, struct socket *sock, int protocol,
593 int kern)
Jennifer Hunteac37312007-02-08 13:51:54 -0800594{
595 struct sock *sk;
596
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +0000597 if (protocol && protocol != PF_IUCV)
598 return -EPROTONOSUPPORT;
Jennifer Hunteac37312007-02-08 13:51:54 -0800599
600 sock->state = SS_UNCONNECTED;
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +0000601
602 switch (sock->type) {
603 case SOCK_STREAM:
604 sock->ops = &iucv_sock_ops;
605 break;
606 case SOCK_SEQPACKET:
607 /* currently, proto ops can handle both sk types */
608 sock->ops = &iucv_sock_ops;
609 break;
610 default:
611 return -ESOCKTNOSUPPORT;
612 }
Jennifer Hunteac37312007-02-08 13:51:54 -0800613
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500614 sk = iucv_sock_alloc(sock, protocol, GFP_KERNEL, kern);
Jennifer Hunteac37312007-02-08 13:51:54 -0800615 if (!sk)
616 return -ENOMEM;
617
618 iucv_sock_init(sk, NULL);
619
620 return 0;
621}
622
623void iucv_sock_link(struct iucv_sock_list *l, struct sock *sk)
624{
625 write_lock_bh(&l->lock);
626 sk_add_node(sk, &l->head);
627 write_unlock_bh(&l->lock);
628}
629
630void iucv_sock_unlink(struct iucv_sock_list *l, struct sock *sk)
631{
632 write_lock_bh(&l->lock);
633 sk_del_node_init(sk);
634 write_unlock_bh(&l->lock);
635}
636
637void iucv_accept_enqueue(struct sock *parent, struct sock *sk)
638{
Ursula Braunfebca282007-07-14 19:04:25 -0700639 unsigned long flags;
640 struct iucv_sock *par = iucv_sk(parent);
641
Jennifer Hunteac37312007-02-08 13:51:54 -0800642 sock_hold(sk);
Ursula Braunfebca282007-07-14 19:04:25 -0700643 spin_lock_irqsave(&par->accept_q_lock, flags);
644 list_add_tail(&iucv_sk(sk)->accept_q, &par->accept_q);
645 spin_unlock_irqrestore(&par->accept_q_lock, flags);
Jennifer Hunteac37312007-02-08 13:51:54 -0800646 iucv_sk(sk)->parent = parent;
Hendrik Brueckner49f5eba2009-10-14 22:54:55 +0000647 sk_acceptq_added(parent);
Jennifer Hunteac37312007-02-08 13:51:54 -0800648}
649
650void iucv_accept_unlink(struct sock *sk)
651{
Ursula Braunfebca282007-07-14 19:04:25 -0700652 unsigned long flags;
653 struct iucv_sock *par = iucv_sk(iucv_sk(sk)->parent);
654
655 spin_lock_irqsave(&par->accept_q_lock, flags);
Jennifer Hunteac37312007-02-08 13:51:54 -0800656 list_del_init(&iucv_sk(sk)->accept_q);
Ursula Braunfebca282007-07-14 19:04:25 -0700657 spin_unlock_irqrestore(&par->accept_q_lock, flags);
Hendrik Brueckner49f5eba2009-10-14 22:54:55 +0000658 sk_acceptq_removed(iucv_sk(sk)->parent);
Jennifer Hunteac37312007-02-08 13:51:54 -0800659 iucv_sk(sk)->parent = NULL;
660 sock_put(sk);
661}
662
663struct sock *iucv_accept_dequeue(struct sock *parent, struct socket *newsock)
664{
665 struct iucv_sock *isk, *n;
666 struct sock *sk;
667
Heiko Carstensda99f052007-05-04 12:23:27 -0700668 list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) {
Jennifer Hunteac37312007-02-08 13:51:54 -0800669 sk = (struct sock *) isk;
670 lock_sock(sk);
671
672 if (sk->sk_state == IUCV_CLOSED) {
Jennifer Hunteac37312007-02-08 13:51:54 -0800673 iucv_accept_unlink(sk);
Ursula Braunfebca282007-07-14 19:04:25 -0700674 release_sock(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800675 continue;
676 }
677
678 if (sk->sk_state == IUCV_CONNECTED ||
Ursula Braunaac63992011-12-19 22:56:31 +0000679 sk->sk_state == IUCV_DISCONN ||
Jennifer Hunteac37312007-02-08 13:51:54 -0800680 !newsock) {
681 iucv_accept_unlink(sk);
682 if (newsock)
683 sock_graft(sk, newsock);
684
Jennifer Hunteac37312007-02-08 13:51:54 -0800685 release_sock(sk);
686 return sk;
687 }
688
689 release_sock(sk);
690 }
691 return NULL;
692}
693
Philipp Hachtmann53a4b492014-05-28 10:22:28 +0200694static void __iucv_auto_name(struct iucv_sock *iucv)
695{
696 char name[12];
697
698 sprintf(name, "%08x", atomic_inc_return(&iucv_sk_list.autobind_name));
699 while (__iucv_get_sock_by_name(name)) {
700 sprintf(name, "%08x",
701 atomic_inc_return(&iucv_sk_list.autobind_name));
702 }
703 memcpy(iucv->src_name, name, 8);
704}
705
Jennifer Hunteac37312007-02-08 13:51:54 -0800706/* Bind an unbound socket */
707static int iucv_sock_bind(struct socket *sock, struct sockaddr *addr,
708 int addr_len)
709{
710 struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
711 struct sock *sk = sock->sk;
712 struct iucv_sock *iucv;
Ursula Braun3881ac42011-08-08 01:33:54 +0000713 int err = 0;
714 struct net_device *dev;
715 char uid[9];
Jennifer Hunteac37312007-02-08 13:51:54 -0800716
717 /* Verify the input sockaddr */
718 if (!addr || addr->sa_family != AF_IUCV)
719 return -EINVAL;
720
Ursula Braun52a82e22016-01-19 10:41:33 +0100721 if (addr_len < sizeof(struct sockaddr_iucv))
722 return -EINVAL;
723
Jennifer Hunteac37312007-02-08 13:51:54 -0800724 lock_sock(sk);
725 if (sk->sk_state != IUCV_OPEN) {
726 err = -EBADFD;
727 goto done;
728 }
729
730 write_lock_bh(&iucv_sk_list.lock);
731
732 iucv = iucv_sk(sk);
733 if (__iucv_get_sock_by_name(sa->siucv_name)) {
734 err = -EADDRINUSE;
735 goto done_unlock;
736 }
Ursula Braun3881ac42011-08-08 01:33:54 +0000737 if (iucv->path)
Jennifer Hunteac37312007-02-08 13:51:54 -0800738 goto done_unlock;
Jennifer Hunteac37312007-02-08 13:51:54 -0800739
740 /* Bind the socket */
Ursula Braun3881ac42011-08-08 01:33:54 +0000741 if (pr_iucv)
742 if (!memcmp(sa->siucv_user_id, iucv_userid, 8))
743 goto vm_bind; /* VM IUCV transport */
Jennifer Hunteac37312007-02-08 13:51:54 -0800744
Ursula Braun3881ac42011-08-08 01:33:54 +0000745 /* try hiper transport */
746 memcpy(uid, sa->siucv_user_id, sizeof(uid));
747 ASCEBC(uid, 8);
748 rcu_read_lock();
749 for_each_netdev_rcu(&init_net, dev) {
750 if (!memcmp(dev->perm_addr, uid, 8)) {
Ursula Braun3881ac42011-08-08 01:33:54 +0000751 memcpy(iucv->src_user_id, sa->siucv_user_id, 8);
Philipp Hachtmann53a4b492014-05-28 10:22:28 +0200752 /* Check for unitialized siucv_name */
753 if (strncmp(sa->siucv_name, " ", 8) == 0)
754 __iucv_auto_name(iucv);
755 else
756 memcpy(iucv->src_name, sa->siucv_name, 8);
Ursula Braun816abba2011-12-19 22:56:29 +0000757 sk->sk_bound_dev_if = dev->ifindex;
Ursula Braun800c5eb2012-02-08 00:19:47 +0000758 iucv->hs_dev = dev;
759 dev_hold(dev);
Ursula Braun3881ac42011-08-08 01:33:54 +0000760 sk->sk_state = IUCV_BOUND;
761 iucv->transport = AF_IUCV_TRANS_HIPER;
762 if (!iucv->msglimit)
763 iucv->msglimit = IUCV_HIPER_MSGLIM_DEFAULT;
764 rcu_read_unlock();
765 goto done_unlock;
766 }
767 }
768 rcu_read_unlock();
769vm_bind:
770 if (pr_iucv) {
771 /* use local userid for backward compat */
772 memcpy(iucv->src_name, sa->siucv_name, 8);
773 memcpy(iucv->src_user_id, iucv_userid, 8);
774 sk->sk_state = IUCV_BOUND;
775 iucv->transport = AF_IUCV_TRANS_IUCV;
776 if (!iucv->msglimit)
777 iucv->msglimit = IUCV_QUEUELEN_DEFAULT;
778 goto done_unlock;
779 }
780 /* found no dev to bind */
781 err = -ENODEV;
Jennifer Hunteac37312007-02-08 13:51:54 -0800782done_unlock:
783 /* Release the socket list lock */
784 write_unlock_bh(&iucv_sk_list.lock);
785done:
786 release_sock(sk);
787 return err;
788}
789
790/* Automatically bind an unbound socket */
791static int iucv_sock_autobind(struct sock *sk)
792{
793 struct iucv_sock *iucv = iucv_sk(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800794 int err = 0;
795
Ursula Braunaac63992011-12-19 22:56:31 +0000796 if (unlikely(!pr_iucv))
Jennifer Hunteac37312007-02-08 13:51:54 -0800797 return -EPROTO;
798
Ursula Braunaac63992011-12-19 22:56:31 +0000799 memcpy(iucv->src_user_id, iucv_userid, 8);
Jennifer Hunteac37312007-02-08 13:51:54 -0800800
801 write_lock_bh(&iucv_sk_list.lock);
Philipp Hachtmann53a4b492014-05-28 10:22:28 +0200802 __iucv_auto_name(iucv);
Jennifer Hunteac37312007-02-08 13:51:54 -0800803 write_unlock_bh(&iucv_sk_list.lock);
804
Ursula Braun3881ac42011-08-08 01:33:54 +0000805 if (!iucv->msglimit)
806 iucv->msglimit = IUCV_QUEUELEN_DEFAULT;
807
Jennifer Hunteac37312007-02-08 13:51:54 -0800808 return err;
809}
810
Ursula Braun3881ac42011-08-08 01:33:54 +0000811static int afiucv_path_connect(struct socket *sock, struct sockaddr *addr)
Jennifer Hunteac37312007-02-08 13:51:54 -0800812{
813 struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
814 struct sock *sk = sock->sk;
Ursula Braun493d3972011-08-08 01:33:52 +0000815 struct iucv_sock *iucv = iucv_sk(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800816 unsigned char user_data[16];
817 int err;
818
Jennifer Hunteac37312007-02-08 13:51:54 -0800819 high_nmcpy(user_data, sa->siucv_name);
Ursula Braun493d3972011-08-08 01:33:52 +0000820 low_nmcpy(user_data, iucv->src_name);
Jennifer Hunteac37312007-02-08 13:51:54 -0800821 ASCEBC(user_data, sizeof(user_data));
822
Jennifer Hunteac37312007-02-08 13:51:54 -0800823 /* Create path. */
Hendrik Brueckner09488e22009-04-21 23:26:27 +0000824 iucv->path = iucv_path_alloc(iucv->msglimit,
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000825 IUCV_IPRMDATA, GFP_KERNEL);
Ursula Braund4444722008-02-07 18:07:19 -0800826 if (!iucv->path) {
827 err = -ENOMEM;
828 goto done;
829 }
Frank Blaschka6fcd61f2011-08-08 01:33:51 +0000830 err = pr_iucv->path_connect(iucv->path, &af_iucv_handler,
831 sa->siucv_user_id, NULL, user_data,
832 sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800833 if (err) {
834 iucv_path_free(iucv->path);
835 iucv->path = NULL;
Hendrik Brueckner55cdea92009-01-05 18:07:07 -0800836 switch (err) {
837 case 0x0b: /* Target communicator is not logged on */
838 err = -ENETUNREACH;
839 break;
840 case 0x0d: /* Max connections for this guest exceeded */
841 case 0x0e: /* Max connections for target guest exceeded */
842 err = -EAGAIN;
843 break;
844 case 0x0f: /* Missing IUCV authorization */
845 err = -EACCES;
846 break;
847 default:
848 err = -ECONNREFUSED;
849 break;
850 }
Ursula Braun3881ac42011-08-08 01:33:54 +0000851 }
852done:
853 return err;
854}
855
856/* Connect an unconnected socket */
857static int iucv_sock_connect(struct socket *sock, struct sockaddr *addr,
858 int alen, int flags)
859{
860 struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
861 struct sock *sk = sock->sk;
862 struct iucv_sock *iucv = iucv_sk(sk);
863 int err;
864
865 if (addr->sa_family != AF_IUCV || alen < sizeof(struct sockaddr_iucv))
866 return -EINVAL;
867
868 if (sk->sk_state != IUCV_OPEN && sk->sk_state != IUCV_BOUND)
869 return -EBADFD;
870
871 if (sk->sk_state == IUCV_OPEN &&
872 iucv->transport == AF_IUCV_TRANS_HIPER)
873 return -EBADFD; /* explicit bind required */
874
875 if (sk->sk_type != SOCK_STREAM && sk->sk_type != SOCK_SEQPACKET)
876 return -EINVAL;
877
878 if (sk->sk_state == IUCV_OPEN) {
879 err = iucv_sock_autobind(sk);
880 if (unlikely(err))
881 return err;
Jennifer Hunteac37312007-02-08 13:51:54 -0800882 }
883
Ursula Braun3881ac42011-08-08 01:33:54 +0000884 lock_sock(sk);
885
886 /* Set the destination information */
887 memcpy(iucv->dst_user_id, sa->siucv_user_id, 8);
888 memcpy(iucv->dst_name, sa->siucv_name, 8);
889
890 if (iucv->transport == AF_IUCV_TRANS_HIPER)
Ursula Braun9fbd87d2012-03-07 02:06:23 +0000891 err = iucv_send_ctrl(sock->sk, AF_IUCV_FLAG_SYN);
Ursula Braun3881ac42011-08-08 01:33:54 +0000892 else
893 err = afiucv_path_connect(sock, addr);
894 if (err)
895 goto done;
896
897 if (sk->sk_state != IUCV_CONNECTED)
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +0000898 err = iucv_sock_wait(sk, iucv_sock_in_state(sk, IUCV_CONNECTED,
899 IUCV_DISCONN),
900 sock_sndtimeo(sk, flags & O_NONBLOCK));
Jennifer Hunteac37312007-02-08 13:51:54 -0800901
Ursula Braun3881ac42011-08-08 01:33:54 +0000902 if (sk->sk_state == IUCV_DISCONN || sk->sk_state == IUCV_CLOSED)
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000903 err = -ECONNREFUSED;
Ursula Braun18becbc2009-01-05 18:07:46 -0800904
Ursula Braun7d316b92012-02-08 00:19:45 +0000905 if (err && iucv->transport == AF_IUCV_TRANS_IUCV)
906 iucv_sever_path(sk, 0);
Ursula Braun18becbc2009-01-05 18:07:46 -0800907
Jennifer Hunteac37312007-02-08 13:51:54 -0800908done:
909 release_sock(sk);
910 return err;
911}
912
913/* Move a socket into listening state. */
914static int iucv_sock_listen(struct socket *sock, int backlog)
915{
916 struct sock *sk = sock->sk;
917 int err;
918
919 lock_sock(sk);
920
921 err = -EINVAL;
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +0000922 if (sk->sk_state != IUCV_BOUND)
923 goto done;
924
925 if (sock->type != SOCK_STREAM && sock->type != SOCK_SEQPACKET)
Jennifer Hunteac37312007-02-08 13:51:54 -0800926 goto done;
927
928 sk->sk_max_ack_backlog = backlog;
929 sk->sk_ack_backlog = 0;
930 sk->sk_state = IUCV_LISTEN;
931 err = 0;
932
933done:
934 release_sock(sk);
935 return err;
936}
937
938/* Accept a pending connection */
939static int iucv_sock_accept(struct socket *sock, struct socket *newsock,
David Howellscdfbabf2017-03-09 08:09:05 +0000940 int flags, bool kern)
Jennifer Hunteac37312007-02-08 13:51:54 -0800941{
942 DECLARE_WAITQUEUE(wait, current);
943 struct sock *sk = sock->sk, *nsk;
944 long timeo;
945 int err = 0;
946
Jennifer Hunt561e0362007-05-04 12:22:07 -0700947 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
Jennifer Hunteac37312007-02-08 13:51:54 -0800948
949 if (sk->sk_state != IUCV_LISTEN) {
950 err = -EBADFD;
951 goto done;
952 }
953
954 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
955
956 /* Wait for an incoming connection */
Eric Dumazetaa395142010-04-20 13:03:51 +0000957 add_wait_queue_exclusive(sk_sleep(sk), &wait);
Heiko Carstensda99f052007-05-04 12:23:27 -0700958 while (!(nsk = iucv_accept_dequeue(sk, newsock))) {
Jennifer Hunteac37312007-02-08 13:51:54 -0800959 set_current_state(TASK_INTERRUPTIBLE);
960 if (!timeo) {
961 err = -EAGAIN;
962 break;
963 }
964
965 release_sock(sk);
966 timeo = schedule_timeout(timeo);
Jennifer Hunt561e0362007-05-04 12:22:07 -0700967 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
Jennifer Hunteac37312007-02-08 13:51:54 -0800968
969 if (sk->sk_state != IUCV_LISTEN) {
970 err = -EBADFD;
971 break;
972 }
973
974 if (signal_pending(current)) {
975 err = sock_intr_errno(timeo);
976 break;
977 }
978 }
979
980 set_current_state(TASK_RUNNING);
Eric Dumazetaa395142010-04-20 13:03:51 +0000981 remove_wait_queue(sk_sleep(sk), &wait);
Jennifer Hunteac37312007-02-08 13:51:54 -0800982
983 if (err)
984 goto done;
985
986 newsock->state = SS_CONNECTED;
987
988done:
989 release_sock(sk);
990 return err;
991}
992
993static int iucv_sock_getname(struct socket *sock, struct sockaddr *addr,
994 int *len, int peer)
995{
996 struct sockaddr_iucv *siucv = (struct sockaddr_iucv *) addr;
997 struct sock *sk = sock->sk;
Ursula Braun493d3972011-08-08 01:33:52 +0000998 struct iucv_sock *iucv = iucv_sk(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800999
1000 addr->sa_family = AF_IUCV;
1001 *len = sizeof(struct sockaddr_iucv);
1002
1003 if (peer) {
Ursula Braun493d3972011-08-08 01:33:52 +00001004 memcpy(siucv->siucv_user_id, iucv->dst_user_id, 8);
1005 memcpy(siucv->siucv_name, iucv->dst_name, 8);
Jennifer Hunteac37312007-02-08 13:51:54 -08001006 } else {
Ursula Braun493d3972011-08-08 01:33:52 +00001007 memcpy(siucv->siucv_user_id, iucv->src_user_id, 8);
1008 memcpy(siucv->siucv_name, iucv->src_name, 8);
Jennifer Hunteac37312007-02-08 13:51:54 -08001009 }
1010 memset(&siucv->siucv_port, 0, sizeof(siucv->siucv_port));
1011 memset(&siucv->siucv_addr, 0, sizeof(siucv->siucv_addr));
Ursula Braun493d3972011-08-08 01:33:52 +00001012 memset(&siucv->siucv_nodeid, 0, sizeof(siucv->siucv_nodeid));
Jennifer Hunteac37312007-02-08 13:51:54 -08001013
1014 return 0;
1015}
1016
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001017/**
1018 * iucv_send_iprm() - Send socket data in parameter list of an iucv message.
1019 * @path: IUCV path
1020 * @msg: Pointer to a struct iucv_message
1021 * @skb: The socket data to send, skb->len MUST BE <= 7
1022 *
1023 * Send the socket data in the parameter list in the iucv message
1024 * (IUCV_IPRMDATA). The socket data is stored at index 0 to 6 in the parameter
1025 * list and the socket data len at index 7 (last byte).
1026 * See also iucv_msg_length().
1027 *
1028 * Returns the error code from the iucv_message_send() call.
1029 */
1030static int iucv_send_iprm(struct iucv_path *path, struct iucv_message *msg,
1031 struct sk_buff *skb)
1032{
1033 u8 prmdata[8];
1034
1035 memcpy(prmdata, (void *) skb->data, skb->len);
1036 prmdata[7] = 0xff - (u8) skb->len;
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001037 return pr_iucv->message_send(path, msg, IUCV_IPRMDATA, 0,
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001038 (void *) prmdata, 8);
1039}
1040
Ying Xue1b784142015-03-02 15:37:48 +08001041static int iucv_sock_sendmsg(struct socket *sock, struct msghdr *msg,
1042 size_t len)
Jennifer Hunteac37312007-02-08 13:51:54 -08001043{
1044 struct sock *sk = sock->sk;
1045 struct iucv_sock *iucv = iucv_sk(sk);
Julian Wiedmanndc5367b2017-01-10 17:10:34 +01001046 size_t headroom = 0;
1047 size_t linear;
Jennifer Hunteac37312007-02-08 13:51:54 -08001048 struct sk_buff *skb;
Eugene Crosser0506eb02015-12-11 12:27:50 +01001049 struct iucv_message txmsg = {0};
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001050 struct cmsghdr *cmsg;
1051 int cmsg_done;
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +00001052 long timeo;
Ursula Braun8f7c5022008-12-25 13:39:47 +01001053 char user_id[9];
1054 char appl_id[9];
Jennifer Hunteac37312007-02-08 13:51:54 -08001055 int err;
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +00001056 int noblock = msg->msg_flags & MSG_DONTWAIT;
Jennifer Hunteac37312007-02-08 13:51:54 -08001057
1058 err = sock_error(sk);
1059 if (err)
1060 return err;
1061
1062 if (msg->msg_flags & MSG_OOB)
1063 return -EOPNOTSUPP;
1064
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001065 /* SOCK_SEQPACKET: we do not support segmented records */
1066 if (sk->sk_type == SOCK_SEQPACKET && !(msg->msg_flags & MSG_EOR))
1067 return -EOPNOTSUPP;
1068
Jennifer Hunteac37312007-02-08 13:51:54 -08001069 lock_sock(sk);
1070
1071 if (sk->sk_shutdown & SEND_SHUTDOWN) {
1072 err = -EPIPE;
1073 goto out;
1074 }
1075
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001076 /* Return if the socket is not in connected state */
1077 if (sk->sk_state != IUCV_CONNECTED) {
1078 err = -ENOTCONN;
1079 goto out;
1080 }
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001081
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001082 /* initialize defaults */
1083 cmsg_done = 0; /* check for duplicate headers */
1084 txmsg.class = 0;
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001085
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001086 /* iterate over control messages */
Gu Zhengf95b4142014-12-11 11:22:04 +08001087 for_each_cmsghdr(cmsg, msg) {
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001088 if (!CMSG_OK(msg, cmsg)) {
1089 err = -EINVAL;
Jennifer Hunt561e0362007-05-04 12:22:07 -07001090 goto out;
Jennifer Hunteac37312007-02-08 13:51:54 -08001091 }
1092
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001093 if (cmsg->cmsg_level != SOL_IUCV)
1094 continue;
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001095
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001096 if (cmsg->cmsg_type & cmsg_done) {
1097 err = -EINVAL;
1098 goto out;
1099 }
1100 cmsg_done |= cmsg->cmsg_type;
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001101
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001102 switch (cmsg->cmsg_type) {
1103 case SCM_IUCV_TRGCLS:
1104 if (cmsg->cmsg_len != CMSG_LEN(TRGCLS_SIZE)) {
1105 err = -EINVAL;
1106 goto out;
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001107 }
1108
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001109 /* set iucv message target class */
1110 memcpy(&txmsg.class,
1111 (void *) CMSG_DATA(cmsg), TRGCLS_SIZE);
1112
1113 break;
1114
1115 default:
1116 err = -EINVAL;
1117 goto out;
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001118 }
1119 }
1120
1121 /* allocate one skb for each iucv message:
1122 * this is fine for SOCK_SEQPACKET (unless we want to support
1123 * segmented records using the MSG_EOR flag), but
1124 * for SOCK_STREAM we might want to improve it in future */
Julian Wiedmanndc5367b2017-01-10 17:10:34 +01001125 if (iucv->transport == AF_IUCV_TRANS_HIPER) {
1126 headroom = sizeof(struct af_iucv_trans_hdr) + ETH_HLEN;
Eugene Crossere5374392016-06-13 18:46:14 +02001127 linear = len;
1128 } else {
Julian Wiedmanndc5367b2017-01-10 17:10:34 +01001129 if (len < PAGE_SIZE) {
1130 linear = len;
1131 } else {
1132 /* In nonlinear "classic" iucv skb,
1133 * reserve space for iucv_array
1134 */
1135 headroom = sizeof(struct iucv_array) *
1136 (MAX_SKB_FRAGS + 1);
1137 linear = PAGE_SIZE - headroom;
1138 }
Eugene Crossere5374392016-06-13 18:46:14 +02001139 }
1140 skb = sock_alloc_send_pskb(sk, headroom + linear, len - linear,
1141 noblock, &err, 0);
Eugene Crossered4ac422015-03-30 15:40:42 +02001142 if (!skb)
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001143 goto out;
Eugene Crossere5374392016-06-13 18:46:14 +02001144 if (headroom)
1145 skb_reserve(skb, headroom);
1146 skb_put(skb, linear);
1147 skb->len = len;
1148 skb->data_len = len - linear;
1149 err = skb_copy_datagram_from_iter(skb, 0, &msg->msg_iter, len);
1150 if (err)
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001151 goto fail;
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001152
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +00001153 /* wait if outstanding messages for iucv path has reached */
1154 timeo = sock_sndtimeo(sk, noblock);
1155 err = iucv_sock_wait(sk, iucv_below_msglim(sk), timeo);
1156 if (err)
1157 goto fail;
1158
1159 /* return -ECONNRESET if the socket is no longer connected */
1160 if (sk->sk_state != IUCV_CONNECTED) {
1161 err = -ECONNRESET;
1162 goto fail;
1163 }
1164
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001165 /* increment and save iucv message tag for msg_completion cbk */
1166 txmsg.tag = iucv->send_tag++;
Ursula Braunf9c41a62013-04-07 22:19:26 +00001167 IUCV_SKB_CB(skb)->tag = txmsg.tag;
Ursula Braun800c5eb2012-02-08 00:19:47 +00001168
Ursula Braun3881ac42011-08-08 01:33:54 +00001169 if (iucv->transport == AF_IUCV_TRANS_HIPER) {
1170 atomic_inc(&iucv->msg_sent);
1171 err = afiucv_hs_send(&txmsg, sk, skb, 0);
1172 if (err) {
1173 atomic_dec(&iucv->msg_sent);
1174 goto fail;
1175 }
Eugene Crossere5374392016-06-13 18:46:14 +02001176 } else { /* Classic VM IUCV transport */
1177 skb_queue_tail(&iucv->send_skb_q, skb);
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001178
Eugene Crossere5374392016-06-13 18:46:14 +02001179 if (((iucv->path->flags & IUCV_IPRMDATA) & iucv->flags) &&
1180 skb->len <= 7) {
1181 err = iucv_send_iprm(iucv->path, &txmsg, skb);
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001182
Eugene Crossere5374392016-06-13 18:46:14 +02001183 /* on success: there is no message_complete callback */
1184 /* for an IPRMDATA msg; remove skb from send queue */
1185 if (err == 0) {
1186 skb_unlink(skb, &iucv->send_skb_q);
1187 kfree_skb(skb);
1188 }
1189
1190 /* this error should never happen since the */
1191 /* IUCV_IPRMDATA path flag is set... sever path */
1192 if (err == 0x15) {
1193 pr_iucv->path_sever(iucv->path, NULL);
1194 skb_unlink(skb, &iucv->send_skb_q);
1195 err = -EPIPE;
1196 goto fail;
1197 }
1198 } else if (skb_is_nonlinear(skb)) {
1199 struct iucv_array *iba = (struct iucv_array *)skb->head;
1200 int i;
1201
1202 /* skip iucv_array lying in the headroom */
1203 iba[0].address = (u32)(addr_t)skb->data;
1204 iba[0].length = (u32)skb_headlen(skb);
1205 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1206 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1207
1208 iba[i + 1].address =
1209 (u32)(addr_t)skb_frag_address(frag);
1210 iba[i + 1].length = (u32)skb_frag_size(frag);
1211 }
1212 err = pr_iucv->message_send(iucv->path, &txmsg,
1213 IUCV_IPBUFLST, 0,
1214 (void *)iba, skb->len);
1215 } else { /* non-IPRM Linear skb */
1216 err = pr_iucv->message_send(iucv->path, &txmsg,
1217 0, 0, (void *)skb->data, skb->len);
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001218 }
Eugene Crossere5374392016-06-13 18:46:14 +02001219 if (err) {
1220 if (err == 3) {
1221 user_id[8] = 0;
1222 memcpy(user_id, iucv->dst_user_id, 8);
1223 appl_id[8] = 0;
1224 memcpy(appl_id, iucv->dst_name, 8);
1225 pr_err(
1226 "Application %s on z/VM guest %s exceeds message limit\n",
1227 appl_id, user_id);
1228 err = -EAGAIN;
1229 } else {
1230 err = -EPIPE;
1231 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001232 skb_unlink(skb, &iucv->send_skb_q);
Jennifer Hunteac37312007-02-08 13:51:54 -08001233 goto fail;
1234 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001235 }
1236
1237 release_sock(sk);
1238 return len;
1239
1240fail:
1241 kfree_skb(skb);
1242out:
1243 release_sock(sk);
1244 return err;
1245}
1246
Eugene Crossera006353a92016-06-13 18:46:16 +02001247static struct sk_buff *alloc_iucv_recv_skb(unsigned long len)
1248{
1249 size_t headroom, linear;
1250 struct sk_buff *skb;
1251 int err;
1252
1253 if (len < PAGE_SIZE) {
1254 headroom = 0;
1255 linear = len;
1256 } else {
1257 headroom = sizeof(struct iucv_array) * (MAX_SKB_FRAGS + 1);
1258 linear = PAGE_SIZE - headroom;
1259 }
1260 skb = alloc_skb_with_frags(headroom + linear, len - linear,
1261 0, &err, GFP_ATOMIC | GFP_DMA);
1262 WARN_ONCE(!skb,
1263 "alloc of recv iucv skb len=%lu failed with errcode=%d\n",
1264 len, err);
1265 if (skb) {
1266 if (headroom)
1267 skb_reserve(skb, headroom);
1268 skb_put(skb, linear);
1269 skb->len = len;
1270 skb->data_len = len - linear;
1271 }
1272 return skb;
1273}
1274
Hendrik Bruecknerbf95d202009-09-16 04:37:28 +00001275/* iucv_process_message() - Receive a single outstanding IUCV message
1276 *
1277 * Locking: must be called with message_q.lock held
1278 */
Ursula Braunf0703c82007-10-08 02:03:31 -07001279static void iucv_process_message(struct sock *sk, struct sk_buff *skb,
1280 struct iucv_path *path,
1281 struct iucv_message *msg)
1282{
1283 int rc;
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001284 unsigned int len;
Ursula Braunf0703c82007-10-08 02:03:31 -07001285
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001286 len = iucv_msg_length(msg);
1287
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001288 /* store msg target class in the second 4 bytes of skb ctrl buffer */
1289 /* Note: the first 4 bytes are reserved for msg tag */
Ursula Braunf9c41a62013-04-07 22:19:26 +00001290 IUCV_SKB_CB(skb)->class = msg->class;
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001291
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001292 /* check for special IPRM messages (e.g. iucv_sock_shutdown) */
1293 if ((msg->flags & IUCV_IPRMDATA) && len > 7) {
1294 if (memcmp(msg->rmmsg, iprm_shutdown, 8) == 0) {
1295 skb->data = NULL;
1296 skb->len = 0;
1297 }
Ursula Braunf0703c82007-10-08 02:03:31 -07001298 } else {
Eugene Crossera006353a92016-06-13 18:46:16 +02001299 if (skb_is_nonlinear(skb)) {
1300 struct iucv_array *iba = (struct iucv_array *)skb->head;
1301 int i;
1302
1303 iba[0].address = (u32)(addr_t)skb->data;
1304 iba[0].length = (u32)skb_headlen(skb);
1305 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1306 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1307
1308 iba[i + 1].address =
1309 (u32)(addr_t)skb_frag_address(frag);
1310 iba[i + 1].length = (u32)skb_frag_size(frag);
1311 }
1312 rc = pr_iucv->message_receive(path, msg,
1313 IUCV_IPBUFLST,
1314 (void *)iba, len, NULL);
1315 } else {
1316 rc = pr_iucv->message_receive(path, msg,
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001317 msg->flags & IUCV_IPRMDATA,
1318 skb->data, len, NULL);
Eugene Crossera006353a92016-06-13 18:46:16 +02001319 }
Ursula Braunf0703c82007-10-08 02:03:31 -07001320 if (rc) {
1321 kfree_skb(skb);
1322 return;
1323 }
Eugene Crossera006353a92016-06-13 18:46:16 +02001324 WARN_ON_ONCE(skb->len != len);
Ursula Braunf0703c82007-10-08 02:03:31 -07001325 }
1326
Ursula Braunf9c41a62013-04-07 22:19:26 +00001327 IUCV_SKB_CB(skb)->offset = 0;
Ursula Braun8c68b1a2016-10-07 15:51:48 +02001328 if (sk_filter(sk, skb)) {
1329 atomic_inc(&sk->sk_drops); /* skb rejected by filter */
1330 kfree_skb(skb);
1331 return;
1332 }
1333 if (__sock_queue_rcv_skb(sk, skb)) /* handle rcv queue full */
1334 skb_queue_tail(&iucv_sk(sk)->backlog_skb_q, skb);
Ursula Braunf0703c82007-10-08 02:03:31 -07001335}
1336
Hendrik Bruecknerbf95d202009-09-16 04:37:28 +00001337/* iucv_process_message_q() - Process outstanding IUCV messages
1338 *
1339 * Locking: must be called with message_q.lock held
1340 */
Ursula Braunf0703c82007-10-08 02:03:31 -07001341static void iucv_process_message_q(struct sock *sk)
1342{
1343 struct iucv_sock *iucv = iucv_sk(sk);
1344 struct sk_buff *skb;
1345 struct sock_msg_q *p, *n;
1346
1347 list_for_each_entry_safe(p, n, &iucv->message_q.list, list) {
Eugene Crossera006353a92016-06-13 18:46:16 +02001348 skb = alloc_iucv_recv_skb(iucv_msg_length(&p->msg));
Ursula Braunf0703c82007-10-08 02:03:31 -07001349 if (!skb)
1350 break;
1351 iucv_process_message(sk, skb, p->path, &p->msg);
1352 list_del(&p->list);
1353 kfree(p);
1354 if (!skb_queue_empty(&iucv->backlog_skb_q))
1355 break;
1356 }
1357}
1358
Ying Xue1b784142015-03-02 15:37:48 +08001359static int iucv_sock_recvmsg(struct socket *sock, struct msghdr *msg,
1360 size_t len, int flags)
Jennifer Hunteac37312007-02-08 13:51:54 -08001361{
1362 int noblock = flags & MSG_DONTWAIT;
1363 struct sock *sk = sock->sk;
Jennifer Hunt561e0362007-05-04 12:22:07 -07001364 struct iucv_sock *iucv = iucv_sk(sk);
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001365 unsigned int copied, rlen;
Ursula Braun9fbd87d2012-03-07 02:06:23 +00001366 struct sk_buff *skb, *rskb, *cskb;
Jennifer Hunteac37312007-02-08 13:51:54 -08001367 int err = 0;
Ursula Braunf9c41a62013-04-07 22:19:26 +00001368 u32 offset;
Jennifer Hunteac37312007-02-08 13:51:54 -08001369
Ursula Braunaac63992011-12-19 22:56:31 +00001370 if ((sk->sk_state == IUCV_DISCONN) &&
Ursula Braunf0703c82007-10-08 02:03:31 -07001371 skb_queue_empty(&iucv->backlog_skb_q) &&
1372 skb_queue_empty(&sk->sk_receive_queue) &&
1373 list_empty(&iucv->message_q.list))
Jennifer Hunt561e0362007-05-04 12:22:07 -07001374 return 0;
1375
Jennifer Hunteac37312007-02-08 13:51:54 -08001376 if (flags & (MSG_OOB))
1377 return -EOPNOTSUPP;
1378
Hendrik Brueckner60d37052009-04-21 06:04:21 +00001379 /* receive/dequeue next skb:
1380 * the function understands MSG_PEEK and, thus, does not dequeue skb */
Jennifer Hunteac37312007-02-08 13:51:54 -08001381 skb = skb_recv_datagram(sk, flags, noblock, &err);
1382 if (!skb) {
1383 if (sk->sk_shutdown & RCV_SHUTDOWN)
1384 return 0;
1385 return err;
1386 }
1387
Ursula Braunf9c41a62013-04-07 22:19:26 +00001388 offset = IUCV_SKB_CB(skb)->offset;
1389 rlen = skb->len - offset; /* real length of skb */
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001390 copied = min_t(unsigned int, rlen, len);
Ursula Braun82492a32012-03-07 02:06:24 +00001391 if (!rlen)
1392 sk->sk_shutdown = sk->sk_shutdown | RCV_SHUTDOWN;
Jennifer Hunteac37312007-02-08 13:51:54 -08001393
Jennifer Hunt561e0362007-05-04 12:22:07 -07001394 cskb = skb;
David S. Miller51f3d022014-11-05 16:46:40 -05001395 if (skb_copy_datagram_msg(cskb, offset, msg, copied)) {
Hendrik Brueckner802788b2009-04-21 23:26:26 +00001396 if (!(flags & MSG_PEEK))
1397 skb_queue_head(&sk->sk_receive_queue, skb);
1398 return -EFAULT;
Jennifer Hunteac37312007-02-08 13:51:54 -08001399 }
1400
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001401 /* SOCK_SEQPACKET: set MSG_TRUNC if recv buf size is too small */
1402 if (sk->sk_type == SOCK_SEQPACKET) {
1403 if (copied < rlen)
1404 msg->msg_flags |= MSG_TRUNC;
1405 /* each iucv message contains a complete record */
1406 msg->msg_flags |= MSG_EOR;
1407 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001408
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001409 /* create control message to store iucv msg target class:
1410 * get the trgcls from the control buffer of the skb due to
1411 * fragmentation of original iucv message. */
1412 err = put_cmsg(msg, SOL_IUCV, SCM_IUCV_TRGCLS,
Ursula Braunf9c41a62013-04-07 22:19:26 +00001413 sizeof(IUCV_SKB_CB(skb)->class),
1414 (void *)&IUCV_SKB_CB(skb)->class);
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001415 if (err) {
1416 if (!(flags & MSG_PEEK))
1417 skb_queue_head(&sk->sk_receive_queue, skb);
1418 return err;
1419 }
1420
Jennifer Hunteac37312007-02-08 13:51:54 -08001421 /* Mark read part of skb as used */
1422 if (!(flags & MSG_PEEK)) {
Jennifer Hunteac37312007-02-08 13:51:54 -08001423
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001424 /* SOCK_STREAM: re-queue skb if it contains unreceived data */
1425 if (sk->sk_type == SOCK_STREAM) {
Ursula Braunf9c41a62013-04-07 22:19:26 +00001426 if (copied < rlen) {
1427 IUCV_SKB_CB(skb)->offset = offset + copied;
Ursula Braun2f139a52014-03-19 07:57:59 +01001428 skb_queue_head(&sk->sk_receive_queue, skb);
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001429 goto done;
1430 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001431 }
1432
1433 kfree_skb(skb);
Ursula Braun800c5eb2012-02-08 00:19:47 +00001434 if (iucv->transport == AF_IUCV_TRANS_HIPER) {
1435 atomic_inc(&iucv->msg_recv);
1436 if (atomic_read(&iucv->msg_recv) > iucv->msglimit) {
1437 WARN_ON(1);
1438 iucv_sock_close(sk);
1439 return -EFAULT;
1440 }
1441 }
Jennifer Hunt561e0362007-05-04 12:22:07 -07001442
1443 /* Queue backlog skbs */
Hendrik Bruecknerbf95d202009-09-16 04:37:28 +00001444 spin_lock_bh(&iucv->message_q.lock);
Ursula Braunf0703c82007-10-08 02:03:31 -07001445 rskb = skb_dequeue(&iucv->backlog_skb_q);
Heiko Carstensda99f052007-05-04 12:23:27 -07001446 while (rskb) {
Ursula Braunf9c41a62013-04-07 22:19:26 +00001447 IUCV_SKB_CB(rskb)->offset = 0;
Ursula Braun8c68b1a2016-10-07 15:51:48 +02001448 if (__sock_queue_rcv_skb(sk, rskb)) {
1449 /* handle rcv queue full */
Ursula Braunf0703c82007-10-08 02:03:31 -07001450 skb_queue_head(&iucv->backlog_skb_q,
Jennifer Hunt561e0362007-05-04 12:22:07 -07001451 rskb);
1452 break;
Jennifer Hunt561e0362007-05-04 12:22:07 -07001453 }
Ursula Braun8c68b1a2016-10-07 15:51:48 +02001454 rskb = skb_dequeue(&iucv->backlog_skb_q);
Jennifer Hunt561e0362007-05-04 12:22:07 -07001455 }
Ursula Braunf0703c82007-10-08 02:03:31 -07001456 if (skb_queue_empty(&iucv->backlog_skb_q)) {
Ursula Braunf0703c82007-10-08 02:03:31 -07001457 if (!list_empty(&iucv->message_q.list))
1458 iucv_process_message_q(sk);
Ursula Braun3881ac42011-08-08 01:33:54 +00001459 if (atomic_read(&iucv->msg_recv) >=
1460 iucv->msglimit / 2) {
Ursula Braun9fbd87d2012-03-07 02:06:23 +00001461 err = iucv_send_ctrl(sk, AF_IUCV_FLAG_WIN);
Ursula Braun3881ac42011-08-08 01:33:54 +00001462 if (err) {
1463 sk->sk_state = IUCV_DISCONN;
1464 sk->sk_state_change(sk);
1465 }
1466 }
Ursula Braunf0703c82007-10-08 02:03:31 -07001467 }
Hendrik Bruecknerbf95d202009-09-16 04:37:28 +00001468 spin_unlock_bh(&iucv->message_q.lock);
Hendrik Brueckner60d37052009-04-21 06:04:21 +00001469 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001470
1471done:
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001472 /* SOCK_SEQPACKET: return real length if MSG_TRUNC is set */
1473 if (sk->sk_type == SOCK_SEQPACKET && (flags & MSG_TRUNC))
1474 copied = rlen;
1475
1476 return copied;
Jennifer Hunteac37312007-02-08 13:51:54 -08001477}
1478
1479static inline unsigned int iucv_accept_poll(struct sock *parent)
1480{
1481 struct iucv_sock *isk, *n;
1482 struct sock *sk;
1483
Heiko Carstensda99f052007-05-04 12:23:27 -07001484 list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) {
Jennifer Hunteac37312007-02-08 13:51:54 -08001485 sk = (struct sock *) isk;
1486
1487 if (sk->sk_state == IUCV_CONNECTED)
1488 return POLLIN | POLLRDNORM;
1489 }
1490
1491 return 0;
1492}
1493
1494unsigned int iucv_sock_poll(struct file *file, struct socket *sock,
1495 poll_table *wait)
1496{
1497 struct sock *sk = sock->sk;
1498 unsigned int mask = 0;
1499
Eric Dumazetaa395142010-04-20 13:03:51 +00001500 sock_poll_wait(file, sk_sleep(sk), wait);
Jennifer Hunteac37312007-02-08 13:51:54 -08001501
1502 if (sk->sk_state == IUCV_LISTEN)
1503 return iucv_accept_poll(sk);
1504
1505 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
Keller, Jacob E7d4c04f2013-03-28 11:19:25 +00001506 mask |= POLLERR |
Jacob Keller8facd5f2013-04-02 13:55:40 -07001507 (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? POLLPRI : 0);
Jennifer Hunteac37312007-02-08 13:51:54 -08001508
1509 if (sk->sk_shutdown & RCV_SHUTDOWN)
1510 mask |= POLLRDHUP;
1511
1512 if (sk->sk_shutdown == SHUTDOWN_MASK)
1513 mask |= POLLHUP;
1514
1515 if (!skb_queue_empty(&sk->sk_receive_queue) ||
Heiko Carstensda99f052007-05-04 12:23:27 -07001516 (sk->sk_shutdown & RCV_SHUTDOWN))
Jennifer Hunteac37312007-02-08 13:51:54 -08001517 mask |= POLLIN | POLLRDNORM;
1518
1519 if (sk->sk_state == IUCV_CLOSED)
1520 mask |= POLLHUP;
1521
Ursula Braunaac63992011-12-19 22:56:31 +00001522 if (sk->sk_state == IUCV_DISCONN)
Jennifer Hunt561e0362007-05-04 12:22:07 -07001523 mask |= POLLIN;
1524
Ursula Braun7f1b0ea2012-02-08 00:19:46 +00001525 if (sock_writeable(sk) && iucv_below_msglim(sk))
Jennifer Hunteac37312007-02-08 13:51:54 -08001526 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
1527 else
Eric Dumazet9cd3e072015-11-29 20:03:10 -08001528 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
Jennifer Hunteac37312007-02-08 13:51:54 -08001529
1530 return mask;
1531}
1532
1533static int iucv_sock_shutdown(struct socket *sock, int how)
1534{
1535 struct sock *sk = sock->sk;
1536 struct iucv_sock *iucv = iucv_sk(sk);
1537 struct iucv_message txmsg;
1538 int err = 0;
Jennifer Hunteac37312007-02-08 13:51:54 -08001539
1540 how++;
1541
1542 if ((how & ~SHUTDOWN_MASK) || !how)
1543 return -EINVAL;
1544
1545 lock_sock(sk);
Heiko Carstensda99f052007-05-04 12:23:27 -07001546 switch (sk->sk_state) {
Ursula Braun82492a32012-03-07 02:06:24 +00001547 case IUCV_LISTEN:
Hendrik Bruecknere14ad5f2009-04-21 06:04:23 +00001548 case IUCV_DISCONN:
1549 case IUCV_CLOSING:
Jennifer Hunteac37312007-02-08 13:51:54 -08001550 case IUCV_CLOSED:
1551 err = -ENOTCONN;
1552 goto fail;
Jennifer Hunteac37312007-02-08 13:51:54 -08001553 default:
Jennifer Hunteac37312007-02-08 13:51:54 -08001554 break;
1555 }
1556
1557 if (how == SEND_SHUTDOWN || how == SHUTDOWN_MASK) {
Ursula Braun82492a32012-03-07 02:06:24 +00001558 if (iucv->transport == AF_IUCV_TRANS_IUCV) {
1559 txmsg.class = 0;
1560 txmsg.tag = 0;
1561 err = pr_iucv->message_send(iucv->path, &txmsg,
1562 IUCV_IPRMDATA, 0, (void *) iprm_shutdown, 8);
1563 if (err) {
1564 switch (err) {
1565 case 1:
1566 err = -ENOTCONN;
1567 break;
1568 case 2:
1569 err = -ECONNRESET;
1570 break;
1571 default:
1572 err = -ENOTCONN;
1573 break;
1574 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001575 }
Ursula Braun82492a32012-03-07 02:06:24 +00001576 } else
1577 iucv_send_ctrl(sk, AF_IUCV_FLAG_SHT);
Jennifer Hunteac37312007-02-08 13:51:54 -08001578 }
1579
Ursula Braun82492a32012-03-07 02:06:24 +00001580 sk->sk_shutdown |= how;
Jennifer Hunteac37312007-02-08 13:51:54 -08001581 if (how == RCV_SHUTDOWN || how == SHUTDOWN_MASK) {
Ursula Braun1042cab2014-07-21 12:54:44 +02001582 if ((iucv->transport == AF_IUCV_TRANS_IUCV) &&
1583 iucv->path) {
Ursula Braun82492a32012-03-07 02:06:24 +00001584 err = pr_iucv->path_quiesce(iucv->path, NULL);
1585 if (err)
1586 err = -ENOTCONN;
1587/* skb_queue_purge(&sk->sk_receive_queue); */
1588 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001589 skb_queue_purge(&sk->sk_receive_queue);
1590 }
1591
1592 /* Wake up anyone sleeping in poll */
1593 sk->sk_state_change(sk);
1594
1595fail:
1596 release_sock(sk);
1597 return err;
1598}
1599
1600static int iucv_sock_release(struct socket *sock)
1601{
1602 struct sock *sk = sock->sk;
1603 int err = 0;
1604
1605 if (!sk)
1606 return 0;
1607
1608 iucv_sock_close(sk);
1609
Jennifer Hunteac37312007-02-08 13:51:54 -08001610 sock_orphan(sk);
1611 iucv_sock_kill(sk);
1612 return err;
1613}
1614
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00001615/* getsockopt and setsockopt */
1616static int iucv_sock_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07001617 char __user *optval, unsigned int optlen)
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00001618{
1619 struct sock *sk = sock->sk;
1620 struct iucv_sock *iucv = iucv_sk(sk);
1621 int val;
1622 int rc;
1623
1624 if (level != SOL_IUCV)
1625 return -ENOPROTOOPT;
1626
1627 if (optlen < sizeof(int))
1628 return -EINVAL;
1629
1630 if (get_user(val, (int __user *) optval))
1631 return -EFAULT;
1632
1633 rc = 0;
1634
1635 lock_sock(sk);
1636 switch (optname) {
1637 case SO_IPRMDATA_MSG:
1638 if (val)
1639 iucv->flags |= IUCV_IPRMDATA;
1640 else
1641 iucv->flags &= ~IUCV_IPRMDATA;
1642 break;
Hendrik Brueckner09488e22009-04-21 23:26:27 +00001643 case SO_MSGLIMIT:
1644 switch (sk->sk_state) {
1645 case IUCV_OPEN:
1646 case IUCV_BOUND:
1647 if (val < 1 || val > (u16)(~0))
1648 rc = -EINVAL;
1649 else
1650 iucv->msglimit = val;
1651 break;
1652 default:
1653 rc = -EINVAL;
1654 break;
1655 }
1656 break;
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00001657 default:
1658 rc = -ENOPROTOOPT;
1659 break;
1660 }
1661 release_sock(sk);
1662
1663 return rc;
1664}
1665
1666static int iucv_sock_getsockopt(struct socket *sock, int level, int optname,
1667 char __user *optval, int __user *optlen)
1668{
1669 struct sock *sk = sock->sk;
1670 struct iucv_sock *iucv = iucv_sk(sk);
Ursula Braun51363b82012-02-08 00:19:48 +00001671 unsigned int val;
1672 int len;
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00001673
1674 if (level != SOL_IUCV)
1675 return -ENOPROTOOPT;
1676
1677 if (get_user(len, optlen))
1678 return -EFAULT;
1679
1680 if (len < 0)
1681 return -EINVAL;
1682
1683 len = min_t(unsigned int, len, sizeof(int));
1684
1685 switch (optname) {
1686 case SO_IPRMDATA_MSG:
1687 val = (iucv->flags & IUCV_IPRMDATA) ? 1 : 0;
1688 break;
Hendrik Brueckner09488e22009-04-21 23:26:27 +00001689 case SO_MSGLIMIT:
1690 lock_sock(sk);
1691 val = (iucv->path != NULL) ? iucv->path->msglim /* connected */
1692 : iucv->msglimit; /* default */
1693 release_sock(sk);
1694 break;
Ursula Braun51363b82012-02-08 00:19:48 +00001695 case SO_MSGSIZE:
1696 if (sk->sk_state == IUCV_OPEN)
1697 return -EBADFD;
1698 val = (iucv->hs_dev) ? iucv->hs_dev->mtu -
1699 sizeof(struct af_iucv_trans_hdr) - ETH_HLEN :
1700 0x7fffffff;
1701 break;
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00001702 default:
1703 return -ENOPROTOOPT;
1704 }
1705
1706 if (put_user(len, optlen))
1707 return -EFAULT;
1708 if (copy_to_user(optval, &val, len))
1709 return -EFAULT;
1710
1711 return 0;
1712}
1713
1714
Jennifer Hunteac37312007-02-08 13:51:54 -08001715/* Callback wrappers - called from iucv base support */
1716static int iucv_callback_connreq(struct iucv_path *path,
1717 u8 ipvmid[8], u8 ipuser[16])
1718{
1719 unsigned char user_data[16];
1720 unsigned char nuser_data[16];
1721 unsigned char src_name[8];
Jennifer Hunteac37312007-02-08 13:51:54 -08001722 struct sock *sk, *nsk;
1723 struct iucv_sock *iucv, *niucv;
1724 int err;
1725
1726 memcpy(src_name, ipuser, 8);
1727 EBCASC(src_name, 8);
1728 /* Find out if this path belongs to af_iucv. */
1729 read_lock(&iucv_sk_list.lock);
1730 iucv = NULL;
Ursula Braunfebca282007-07-14 19:04:25 -07001731 sk = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001732 sk_for_each(sk, &iucv_sk_list.head)
Jennifer Hunteac37312007-02-08 13:51:54 -08001733 if (sk->sk_state == IUCV_LISTEN &&
1734 !memcmp(&iucv_sk(sk)->src_name, src_name, 8)) {
1735 /*
1736 * Found a listening socket with
1737 * src_name == ipuser[0-7].
1738 */
1739 iucv = iucv_sk(sk);
1740 break;
1741 }
1742 read_unlock(&iucv_sk_list.lock);
1743 if (!iucv)
1744 /* No socket found, not one of our paths. */
1745 return -EINVAL;
1746
1747 bh_lock_sock(sk);
1748
1749 /* Check if parent socket is listening */
1750 low_nmcpy(user_data, iucv->src_name);
1751 high_nmcpy(user_data, iucv->dst_name);
1752 ASCEBC(user_data, sizeof(user_data));
1753 if (sk->sk_state != IUCV_LISTEN) {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001754 err = pr_iucv->path_sever(path, user_data);
Hendrik Brueckner65dbd7c2009-01-05 18:08:23 -08001755 iucv_path_free(path);
Jennifer Hunteac37312007-02-08 13:51:54 -08001756 goto fail;
1757 }
1758
1759 /* Check for backlog size */
1760 if (sk_acceptq_is_full(sk)) {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001761 err = pr_iucv->path_sever(path, user_data);
Hendrik Brueckner65dbd7c2009-01-05 18:08:23 -08001762 iucv_path_free(path);
Jennifer Hunteac37312007-02-08 13:51:54 -08001763 goto fail;
1764 }
1765
1766 /* Create the new socket */
Eric W. Biederman11aa9c22015-05-08 21:09:13 -05001767 nsk = iucv_sock_alloc(NULL, sk->sk_type, GFP_ATOMIC, 0);
Heiko Carstensda99f052007-05-04 12:23:27 -07001768 if (!nsk) {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001769 err = pr_iucv->path_sever(path, user_data);
Hendrik Brueckner65dbd7c2009-01-05 18:08:23 -08001770 iucv_path_free(path);
Jennifer Hunteac37312007-02-08 13:51:54 -08001771 goto fail;
1772 }
1773
1774 niucv = iucv_sk(nsk);
1775 iucv_sock_init(nsk, sk);
1776
1777 /* Set the new iucv_sock */
1778 memcpy(niucv->dst_name, ipuser + 8, 8);
1779 EBCASC(niucv->dst_name, 8);
1780 memcpy(niucv->dst_user_id, ipvmid, 8);
1781 memcpy(niucv->src_name, iucv->src_name, 8);
1782 memcpy(niucv->src_user_id, iucv->src_user_id, 8);
1783 niucv->path = path;
1784
1785 /* Call iucv_accept */
1786 high_nmcpy(nuser_data, ipuser + 8);
1787 memcpy(nuser_data + 8, niucv->src_name, 8);
1788 ASCEBC(nuser_data + 8, 8);
1789
Hendrik Brueckner09488e22009-04-21 23:26:27 +00001790 /* set message limit for path based on msglimit of accepting socket */
1791 niucv->msglimit = iucv->msglimit;
1792 path->msglim = iucv->msglimit;
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001793 err = pr_iucv->path_accept(path, &af_iucv_handler, nuser_data, nsk);
Heiko Carstensda99f052007-05-04 12:23:27 -07001794 if (err) {
Ursula Braun7d316b92012-02-08 00:19:45 +00001795 iucv_sever_path(nsk, 1);
Hendrik Brueckner65dbd7c2009-01-05 18:08:23 -08001796 iucv_sock_kill(nsk);
Jennifer Hunteac37312007-02-08 13:51:54 -08001797 goto fail;
1798 }
1799
1800 iucv_accept_enqueue(sk, nsk);
1801
1802 /* Wake up accept */
1803 nsk->sk_state = IUCV_CONNECTED;
David S. Miller676d2362014-04-11 16:15:36 -04001804 sk->sk_data_ready(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -08001805 err = 0;
1806fail:
1807 bh_unlock_sock(sk);
1808 return 0;
1809}
1810
1811static void iucv_callback_connack(struct iucv_path *path, u8 ipuser[16])
1812{
1813 struct sock *sk = path->private;
1814
1815 sk->sk_state = IUCV_CONNECTED;
1816 sk->sk_state_change(sk);
1817}
1818
1819static void iucv_callback_rx(struct iucv_path *path, struct iucv_message *msg)
1820{
1821 struct sock *sk = path->private;
Jennifer Hunt561e0362007-05-04 12:22:07 -07001822 struct iucv_sock *iucv = iucv_sk(sk);
Ursula Braunf0703c82007-10-08 02:03:31 -07001823 struct sk_buff *skb;
1824 struct sock_msg_q *save_msg;
1825 int len;
Jennifer Hunt561e0362007-05-04 12:22:07 -07001826
Hendrik Bruecknerfe86e542009-04-21 06:04:22 +00001827 if (sk->sk_shutdown & RCV_SHUTDOWN) {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001828 pr_iucv->message_reject(path, msg);
Jennifer Hunteac37312007-02-08 13:51:54 -08001829 return;
Hendrik Bruecknerfe86e542009-04-21 06:04:22 +00001830 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001831
Hendrik Brueckner3fa6b5a2009-04-21 06:04:24 +00001832 spin_lock(&iucv->message_q.lock);
Jennifer Hunteac37312007-02-08 13:51:54 -08001833
Ursula Braunf0703c82007-10-08 02:03:31 -07001834 if (!list_empty(&iucv->message_q.list) ||
1835 !skb_queue_empty(&iucv->backlog_skb_q))
1836 goto save_message;
1837
1838 len = atomic_read(&sk->sk_rmem_alloc);
Eric Dumazet87fb4b72011-10-13 07:28:54 +00001839 len += SKB_TRUESIZE(iucv_msg_length(msg));
Ursula Braunf0703c82007-10-08 02:03:31 -07001840 if (len > sk->sk_rcvbuf)
1841 goto save_message;
1842
Eugene Crossera006353a92016-06-13 18:46:16 +02001843 skb = alloc_iucv_recv_skb(iucv_msg_length(msg));
Ursula Braunf0703c82007-10-08 02:03:31 -07001844 if (!skb)
1845 goto save_message;
Jennifer Hunteac37312007-02-08 13:51:54 -08001846
Ursula Braunf0703c82007-10-08 02:03:31 -07001847 iucv_process_message(sk, skb, path, msg);
Hendrik Brueckner3fa6b5a2009-04-21 06:04:24 +00001848 goto out_unlock;
Jennifer Hunteac37312007-02-08 13:51:54 -08001849
Ursula Braunf0703c82007-10-08 02:03:31 -07001850save_message:
1851 save_msg = kzalloc(sizeof(struct sock_msg_q), GFP_ATOMIC | GFP_DMA);
Ursula Braund4444722008-02-07 18:07:19 -08001852 if (!save_msg)
Julia Lawalla56635a2010-05-26 05:56:48 +00001853 goto out_unlock;
Ursula Braunf0703c82007-10-08 02:03:31 -07001854 save_msg->path = path;
1855 save_msg->msg = *msg;
1856
Ursula Braunf0703c82007-10-08 02:03:31 -07001857 list_add_tail(&save_msg->list, &iucv->message_q.list);
Hendrik Brueckner3fa6b5a2009-04-21 06:04:24 +00001858
1859out_unlock:
Ursula Braunf0703c82007-10-08 02:03:31 -07001860 spin_unlock(&iucv->message_q.lock);
Jennifer Hunteac37312007-02-08 13:51:54 -08001861}
1862
1863static void iucv_callback_txdone(struct iucv_path *path,
1864 struct iucv_message *msg)
1865{
1866 struct sock *sk = path->private;
Ursula Braunf2a77992008-02-07 18:07:44 -08001867 struct sk_buff *this = NULL;
Jennifer Hunteac37312007-02-08 13:51:54 -08001868 struct sk_buff_head *list = &iucv_sk(sk)->send_skb_q;
1869 struct sk_buff *list_skb = list->next;
1870 unsigned long flags;
1871
Ursula Braun7d316b92012-02-08 00:19:45 +00001872 bh_lock_sock(sk);
Ursula Braunf2a77992008-02-07 18:07:44 -08001873 if (!skb_queue_empty(list)) {
Jennifer Hunt561e0362007-05-04 12:22:07 -07001874 spin_lock_irqsave(&list->lock, flags);
Jennifer Hunteac37312007-02-08 13:51:54 -08001875
Ursula Braunf2a77992008-02-07 18:07:44 -08001876 while (list_skb != (struct sk_buff *)list) {
Ursula Braunf5738e22014-05-13 14:38:02 +02001877 if (msg->tag == IUCV_SKB_CB(list_skb)->tag) {
Ursula Braunf2a77992008-02-07 18:07:44 -08001878 this = list_skb;
1879 break;
1880 }
Jennifer Hunt561e0362007-05-04 12:22:07 -07001881 list_skb = list_skb->next;
Ursula Braunf2a77992008-02-07 18:07:44 -08001882 }
1883 if (this)
1884 __skb_unlink(this, list);
Jennifer Hunteac37312007-02-08 13:51:54 -08001885
Jennifer Hunt561e0362007-05-04 12:22:07 -07001886 spin_unlock_irqrestore(&list->lock, flags);
Jennifer Hunteac37312007-02-08 13:51:54 -08001887
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +00001888 if (this) {
1889 kfree_skb(this);
1890 /* wake up any process waiting for sending */
1891 iucv_sock_wake_msglim(sk);
1892 }
Jennifer Hunt561e0362007-05-04 12:22:07 -07001893 }
1894
Heiko Carstensda99f052007-05-04 12:23:27 -07001895 if (sk->sk_state == IUCV_CLOSING) {
Jennifer Hunt561e0362007-05-04 12:22:07 -07001896 if (skb_queue_empty(&iucv_sk(sk)->send_skb_q)) {
1897 sk->sk_state = IUCV_CLOSED;
1898 sk->sk_state_change(sk);
1899 }
1900 }
Ursula Braun7d316b92012-02-08 00:19:45 +00001901 bh_unlock_sock(sk);
Jennifer Hunt561e0362007-05-04 12:22:07 -07001902
Jennifer Hunteac37312007-02-08 13:51:54 -08001903}
1904
1905static void iucv_callback_connrej(struct iucv_path *path, u8 ipuser[16])
1906{
1907 struct sock *sk = path->private;
1908
Ursula Braun7d316b92012-02-08 00:19:45 +00001909 if (sk->sk_state == IUCV_CLOSED)
1910 return;
1911
1912 bh_lock_sock(sk);
1913 iucv_sever_path(sk, 1);
Ursula Braunaac63992011-12-19 22:56:31 +00001914 sk->sk_state = IUCV_DISCONN;
Jennifer Hunteac37312007-02-08 13:51:54 -08001915
1916 sk->sk_state_change(sk);
Ursula Braun7d316b92012-02-08 00:19:45 +00001917 bh_unlock_sock(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -08001918}
1919
Hendrik Brueckneraf88b522009-04-21 23:26:21 +00001920/* called if the other communication side shuts down its RECV direction;
1921 * in turn, the callback sets SEND_SHUTDOWN to disable sending of data.
1922 */
1923static void iucv_callback_shutdown(struct iucv_path *path, u8 ipuser[16])
1924{
1925 struct sock *sk = path->private;
1926
1927 bh_lock_sock(sk);
1928 if (sk->sk_state != IUCV_CLOSED) {
1929 sk->sk_shutdown |= SEND_SHUTDOWN;
1930 sk->sk_state_change(sk);
1931 }
1932 bh_unlock_sock(sk);
1933}
1934
Ursula Braun3881ac42011-08-08 01:33:54 +00001935/***************** HiperSockets transport callbacks ********************/
1936static void afiucv_swap_src_dest(struct sk_buff *skb)
1937{
1938 struct af_iucv_trans_hdr *trans_hdr =
1939 (struct af_iucv_trans_hdr *)skb->data;
1940 char tmpID[8];
1941 char tmpName[8];
1942
1943 ASCEBC(trans_hdr->destUserID, sizeof(trans_hdr->destUserID));
1944 ASCEBC(trans_hdr->destAppName, sizeof(trans_hdr->destAppName));
1945 ASCEBC(trans_hdr->srcUserID, sizeof(trans_hdr->srcUserID));
1946 ASCEBC(trans_hdr->srcAppName, sizeof(trans_hdr->srcAppName));
1947 memcpy(tmpID, trans_hdr->srcUserID, 8);
1948 memcpy(tmpName, trans_hdr->srcAppName, 8);
1949 memcpy(trans_hdr->srcUserID, trans_hdr->destUserID, 8);
1950 memcpy(trans_hdr->srcAppName, trans_hdr->destAppName, 8);
1951 memcpy(trans_hdr->destUserID, tmpID, 8);
1952 memcpy(trans_hdr->destAppName, tmpName, 8);
1953 skb_push(skb, ETH_HLEN);
1954 memset(skb->data, 0, ETH_HLEN);
1955}
1956
1957/**
1958 * afiucv_hs_callback_syn - react on received SYN
1959 **/
1960static int afiucv_hs_callback_syn(struct sock *sk, struct sk_buff *skb)
1961{
1962 struct sock *nsk;
1963 struct iucv_sock *iucv, *niucv;
1964 struct af_iucv_trans_hdr *trans_hdr;
1965 int err;
1966
1967 iucv = iucv_sk(sk);
1968 trans_hdr = (struct af_iucv_trans_hdr *)skb->data;
1969 if (!iucv) {
1970 /* no sock - connection refused */
1971 afiucv_swap_src_dest(skb);
1972 trans_hdr->flags = AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN;
1973 err = dev_queue_xmit(skb);
1974 goto out;
1975 }
1976
Eric W. Biederman11aa9c22015-05-08 21:09:13 -05001977 nsk = iucv_sock_alloc(NULL, sk->sk_type, GFP_ATOMIC, 0);
Ursula Braun3881ac42011-08-08 01:33:54 +00001978 bh_lock_sock(sk);
1979 if ((sk->sk_state != IUCV_LISTEN) ||
1980 sk_acceptq_is_full(sk) ||
1981 !nsk) {
1982 /* error on server socket - connection refused */
Ursula Braun3881ac42011-08-08 01:33:54 +00001983 afiucv_swap_src_dest(skb);
1984 trans_hdr->flags = AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN;
1985 err = dev_queue_xmit(skb);
Ursula Braun4d520f62014-05-28 10:22:29 +02001986 iucv_sock_kill(nsk);
Ursula Braun3881ac42011-08-08 01:33:54 +00001987 bh_unlock_sock(sk);
1988 goto out;
1989 }
1990
1991 niucv = iucv_sk(nsk);
1992 iucv_sock_init(nsk, sk);
1993 niucv->transport = AF_IUCV_TRANS_HIPER;
1994 niucv->msglimit = iucv->msglimit;
1995 if (!trans_hdr->window)
1996 niucv->msglimit_peer = IUCV_HIPER_MSGLIM_DEFAULT;
1997 else
1998 niucv->msglimit_peer = trans_hdr->window;
1999 memcpy(niucv->dst_name, trans_hdr->srcAppName, 8);
2000 memcpy(niucv->dst_user_id, trans_hdr->srcUserID, 8);
2001 memcpy(niucv->src_name, iucv->src_name, 8);
2002 memcpy(niucv->src_user_id, iucv->src_user_id, 8);
2003 nsk->sk_bound_dev_if = sk->sk_bound_dev_if;
Ursula Braun800c5eb2012-02-08 00:19:47 +00002004 niucv->hs_dev = iucv->hs_dev;
2005 dev_hold(niucv->hs_dev);
Ursula Braun3881ac42011-08-08 01:33:54 +00002006 afiucv_swap_src_dest(skb);
2007 trans_hdr->flags = AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_ACK;
2008 trans_hdr->window = niucv->msglimit;
2009 /* if receiver acks the xmit connection is established */
2010 err = dev_queue_xmit(skb);
2011 if (!err) {
2012 iucv_accept_enqueue(sk, nsk);
2013 nsk->sk_state = IUCV_CONNECTED;
David S. Miller676d2362014-04-11 16:15:36 -04002014 sk->sk_data_ready(sk);
Ursula Braun3881ac42011-08-08 01:33:54 +00002015 } else
2016 iucv_sock_kill(nsk);
2017 bh_unlock_sock(sk);
2018
2019out:
2020 return NET_RX_SUCCESS;
2021}
2022
2023/**
2024 * afiucv_hs_callback_synack() - react on received SYN-ACK
2025 **/
2026static int afiucv_hs_callback_synack(struct sock *sk, struct sk_buff *skb)
2027{
2028 struct iucv_sock *iucv = iucv_sk(sk);
2029 struct af_iucv_trans_hdr *trans_hdr =
2030 (struct af_iucv_trans_hdr *)skb->data;
2031
2032 if (!iucv)
2033 goto out;
2034 if (sk->sk_state != IUCV_BOUND)
2035 goto out;
2036 bh_lock_sock(sk);
2037 iucv->msglimit_peer = trans_hdr->window;
2038 sk->sk_state = IUCV_CONNECTED;
2039 sk->sk_state_change(sk);
2040 bh_unlock_sock(sk);
2041out:
2042 kfree_skb(skb);
2043 return NET_RX_SUCCESS;
2044}
2045
2046/**
2047 * afiucv_hs_callback_synfin() - react on received SYN_FIN
2048 **/
2049static int afiucv_hs_callback_synfin(struct sock *sk, struct sk_buff *skb)
2050{
2051 struct iucv_sock *iucv = iucv_sk(sk);
2052
2053 if (!iucv)
2054 goto out;
2055 if (sk->sk_state != IUCV_BOUND)
2056 goto out;
2057 bh_lock_sock(sk);
2058 sk->sk_state = IUCV_DISCONN;
2059 sk->sk_state_change(sk);
2060 bh_unlock_sock(sk);
2061out:
2062 kfree_skb(skb);
2063 return NET_RX_SUCCESS;
2064}
2065
2066/**
2067 * afiucv_hs_callback_fin() - react on received FIN
2068 **/
2069static int afiucv_hs_callback_fin(struct sock *sk, struct sk_buff *skb)
2070{
2071 struct iucv_sock *iucv = iucv_sk(sk);
2072
2073 /* other end of connection closed */
Ursula Braun800c5eb2012-02-08 00:19:47 +00002074 if (!iucv)
2075 goto out;
2076 bh_lock_sock(sk);
2077 if (sk->sk_state == IUCV_CONNECTED) {
Ursula Braunaac63992011-12-19 22:56:31 +00002078 sk->sk_state = IUCV_DISCONN;
Ursula Braun3881ac42011-08-08 01:33:54 +00002079 sk->sk_state_change(sk);
Ursula Braun3881ac42011-08-08 01:33:54 +00002080 }
Ursula Braun800c5eb2012-02-08 00:19:47 +00002081 bh_unlock_sock(sk);
2082out:
Ursula Braun3881ac42011-08-08 01:33:54 +00002083 kfree_skb(skb);
2084 return NET_RX_SUCCESS;
2085}
2086
2087/**
2088 * afiucv_hs_callback_win() - react on received WIN
2089 **/
2090static int afiucv_hs_callback_win(struct sock *sk, struct sk_buff *skb)
2091{
2092 struct iucv_sock *iucv = iucv_sk(sk);
2093 struct af_iucv_trans_hdr *trans_hdr =
2094 (struct af_iucv_trans_hdr *)skb->data;
2095
2096 if (!iucv)
2097 return NET_RX_SUCCESS;
2098
2099 if (sk->sk_state != IUCV_CONNECTED)
2100 return NET_RX_SUCCESS;
2101
2102 atomic_sub(trans_hdr->window, &iucv->msg_sent);
2103 iucv_sock_wake_msglim(sk);
2104 return NET_RX_SUCCESS;
2105}
2106
2107/**
2108 * afiucv_hs_callback_rx() - react on received data
2109 **/
2110static int afiucv_hs_callback_rx(struct sock *sk, struct sk_buff *skb)
2111{
2112 struct iucv_sock *iucv = iucv_sk(sk);
2113
2114 if (!iucv) {
2115 kfree_skb(skb);
2116 return NET_RX_SUCCESS;
2117 }
2118
2119 if (sk->sk_state != IUCV_CONNECTED) {
2120 kfree_skb(skb);
2121 return NET_RX_SUCCESS;
2122 }
2123
Ursula Braun82492a32012-03-07 02:06:24 +00002124 if (sk->sk_shutdown & RCV_SHUTDOWN) {
2125 kfree_skb(skb);
2126 return NET_RX_SUCCESS;
2127 }
2128
Eugene Crosser979f66b2015-12-11 12:27:51 +01002129 /* write stuff from iucv_msg to skb cb */
Ursula Braun3881ac42011-08-08 01:33:54 +00002130 skb_pull(skb, sizeof(struct af_iucv_trans_hdr));
2131 skb_reset_transport_header(skb);
2132 skb_reset_network_header(skb);
Ursula Braunf9c41a62013-04-07 22:19:26 +00002133 IUCV_SKB_CB(skb)->offset = 0;
Ursula Braun8c68b1a2016-10-07 15:51:48 +02002134 if (sk_filter(sk, skb)) {
2135 atomic_inc(&sk->sk_drops); /* skb rejected by filter */
2136 kfree_skb(skb);
2137 return NET_RX_SUCCESS;
2138 }
2139
Ursula Braun3881ac42011-08-08 01:33:54 +00002140 spin_lock(&iucv->message_q.lock);
2141 if (skb_queue_empty(&iucv->backlog_skb_q)) {
Ursula Braun8c68b1a2016-10-07 15:51:48 +02002142 if (__sock_queue_rcv_skb(sk, skb))
Ursula Braun3881ac42011-08-08 01:33:54 +00002143 /* handle rcv queue full */
2144 skb_queue_tail(&iucv->backlog_skb_q, skb);
Ursula Braun3881ac42011-08-08 01:33:54 +00002145 } else
2146 skb_queue_tail(&iucv_sk(sk)->backlog_skb_q, skb);
2147 spin_unlock(&iucv->message_q.lock);
2148 return NET_RX_SUCCESS;
2149}
2150
2151/**
2152 * afiucv_hs_rcv() - base function for arriving data through HiperSockets
2153 * transport
2154 * called from netif RX softirq
2155 **/
2156static int afiucv_hs_rcv(struct sk_buff *skb, struct net_device *dev,
2157 struct packet_type *pt, struct net_device *orig_dev)
2158{
Ursula Braun3881ac42011-08-08 01:33:54 +00002159 struct sock *sk;
2160 struct iucv_sock *iucv;
2161 struct af_iucv_trans_hdr *trans_hdr;
2162 char nullstring[8];
2163 int err = 0;
2164
Eugene Crosser979f66b2015-12-11 12:27:51 +01002165 if (skb->len < (ETH_HLEN + sizeof(struct af_iucv_trans_hdr))) {
2166 WARN_ONCE(1, "AF_IUCV too short skb, len=%d, min=%d",
2167 (int)skb->len,
2168 (int)(ETH_HLEN + sizeof(struct af_iucv_trans_hdr)));
2169 kfree_skb(skb);
2170 return NET_RX_SUCCESS;
2171 }
2172 if (skb_headlen(skb) < (ETH_HLEN + sizeof(struct af_iucv_trans_hdr)))
2173 if (skb_linearize(skb)) {
2174 WARN_ONCE(1, "AF_IUCV skb_linearize failed, len=%d",
2175 (int)skb->len);
2176 kfree_skb(skb);
2177 return NET_RX_SUCCESS;
2178 }
Ursula Braun3881ac42011-08-08 01:33:54 +00002179 skb_pull(skb, ETH_HLEN);
2180 trans_hdr = (struct af_iucv_trans_hdr *)skb->data;
2181 EBCASC(trans_hdr->destAppName, sizeof(trans_hdr->destAppName));
2182 EBCASC(trans_hdr->destUserID, sizeof(trans_hdr->destUserID));
2183 EBCASC(trans_hdr->srcAppName, sizeof(trans_hdr->srcAppName));
2184 EBCASC(trans_hdr->srcUserID, sizeof(trans_hdr->srcUserID));
2185 memset(nullstring, 0, sizeof(nullstring));
2186 iucv = NULL;
2187 sk = NULL;
2188 read_lock(&iucv_sk_list.lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08002189 sk_for_each(sk, &iucv_sk_list.head) {
Ursula Braun3881ac42011-08-08 01:33:54 +00002190 if (trans_hdr->flags == AF_IUCV_FLAG_SYN) {
2191 if ((!memcmp(&iucv_sk(sk)->src_name,
2192 trans_hdr->destAppName, 8)) &&
2193 (!memcmp(&iucv_sk(sk)->src_user_id,
2194 trans_hdr->destUserID, 8)) &&
2195 (!memcmp(&iucv_sk(sk)->dst_name, nullstring, 8)) &&
2196 (!memcmp(&iucv_sk(sk)->dst_user_id,
2197 nullstring, 8))) {
2198 iucv = iucv_sk(sk);
2199 break;
2200 }
2201 } else {
2202 if ((!memcmp(&iucv_sk(sk)->src_name,
2203 trans_hdr->destAppName, 8)) &&
2204 (!memcmp(&iucv_sk(sk)->src_user_id,
2205 trans_hdr->destUserID, 8)) &&
2206 (!memcmp(&iucv_sk(sk)->dst_name,
2207 trans_hdr->srcAppName, 8)) &&
2208 (!memcmp(&iucv_sk(sk)->dst_user_id,
2209 trans_hdr->srcUserID, 8))) {
2210 iucv = iucv_sk(sk);
2211 break;
2212 }
2213 }
2214 }
2215 read_unlock(&iucv_sk_list.lock);
2216 if (!iucv)
2217 sk = NULL;
2218
2219 /* no sock
2220 how should we send with no sock
2221 1) send without sock no send rc checking?
2222 2) introduce default sock to handle this cases
2223
2224 SYN -> send SYN|ACK in good case, send SYN|FIN in bad case
2225 data -> send FIN
2226 SYN|ACK, SYN|FIN, FIN -> no action? */
2227
2228 switch (trans_hdr->flags) {
2229 case AF_IUCV_FLAG_SYN:
2230 /* connect request */
2231 err = afiucv_hs_callback_syn(sk, skb);
2232 break;
2233 case (AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_ACK):
2234 /* connect request confirmed */
2235 err = afiucv_hs_callback_synack(sk, skb);
2236 break;
2237 case (AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN):
2238 /* connect request refused */
2239 err = afiucv_hs_callback_synfin(sk, skb);
2240 break;
2241 case (AF_IUCV_FLAG_FIN):
2242 /* close request */
2243 err = afiucv_hs_callback_fin(sk, skb);
2244 break;
2245 case (AF_IUCV_FLAG_WIN):
2246 err = afiucv_hs_callback_win(sk, skb);
Ursula Braun800c5eb2012-02-08 00:19:47 +00002247 if (skb->len == sizeof(struct af_iucv_trans_hdr)) {
2248 kfree_skb(skb);
2249 break;
2250 }
Ursula Braun82492a32012-03-07 02:06:24 +00002251 /* fall through and receive non-zero length data */
2252 case (AF_IUCV_FLAG_SHT):
2253 /* shutdown request */
2254 /* fall through and receive zero length data */
Ursula Braun3881ac42011-08-08 01:33:54 +00002255 case 0:
2256 /* plain data frame */
Ursula Braunf9c41a62013-04-07 22:19:26 +00002257 IUCV_SKB_CB(skb)->class = trans_hdr->iucv_hdr.class;
Ursula Braun3881ac42011-08-08 01:33:54 +00002258 err = afiucv_hs_callback_rx(sk, skb);
2259 break;
2260 default:
2261 ;
2262 }
2263
2264 return err;
2265}
2266
2267/**
2268 * afiucv_hs_callback_txnotify() - handle send notifcations from HiperSockets
2269 * transport
2270 **/
2271static void afiucv_hs_callback_txnotify(struct sk_buff *skb,
2272 enum iucv_tx_notify n)
2273{
2274 struct sock *isk = skb->sk;
2275 struct sock *sk = NULL;
2276 struct iucv_sock *iucv = NULL;
2277 struct sk_buff_head *list;
2278 struct sk_buff *list_skb;
Ursula Braun800c5eb2012-02-08 00:19:47 +00002279 struct sk_buff *nskb;
Ursula Braun3881ac42011-08-08 01:33:54 +00002280 unsigned long flags;
Ursula Braun3881ac42011-08-08 01:33:54 +00002281
Ursula Braun800c5eb2012-02-08 00:19:47 +00002282 read_lock_irqsave(&iucv_sk_list.lock, flags);
Sasha Levinb67bfe02013-02-27 17:06:00 -08002283 sk_for_each(sk, &iucv_sk_list.head)
Ursula Braun3881ac42011-08-08 01:33:54 +00002284 if (sk == isk) {
2285 iucv = iucv_sk(sk);
2286 break;
2287 }
Ursula Braun800c5eb2012-02-08 00:19:47 +00002288 read_unlock_irqrestore(&iucv_sk_list.lock, flags);
Ursula Braun3881ac42011-08-08 01:33:54 +00002289
Ursula Braun800c5eb2012-02-08 00:19:47 +00002290 if (!iucv || sock_flag(sk, SOCK_ZAPPED))
Ursula Braun3881ac42011-08-08 01:33:54 +00002291 return;
2292
Ursula Braun3881ac42011-08-08 01:33:54 +00002293 list = &iucv->send_skb_q;
Ursula Braun800c5eb2012-02-08 00:19:47 +00002294 spin_lock_irqsave(&list->lock, flags);
Ursula Braun3881ac42011-08-08 01:33:54 +00002295 if (skb_queue_empty(list))
2296 goto out_unlock;
Ursula Braun800c5eb2012-02-08 00:19:47 +00002297 list_skb = list->next;
2298 nskb = list_skb->next;
Ursula Braun3881ac42011-08-08 01:33:54 +00002299 while (list_skb != (struct sk_buff *)list) {
2300 if (skb_shinfo(list_skb) == skb_shinfo(skb)) {
Ursula Braun3881ac42011-08-08 01:33:54 +00002301 switch (n) {
2302 case TX_NOTIFY_OK:
Ursula Braun800c5eb2012-02-08 00:19:47 +00002303 __skb_unlink(list_skb, list);
2304 kfree_skb(list_skb);
Ursula Braun3881ac42011-08-08 01:33:54 +00002305 iucv_sock_wake_msglim(sk);
Ursula Braun3881ac42011-08-08 01:33:54 +00002306 break;
2307 case TX_NOTIFY_PENDING:
2308 atomic_inc(&iucv->pendings);
2309 break;
2310 case TX_NOTIFY_DELAYED_OK:
Ursula Braun800c5eb2012-02-08 00:19:47 +00002311 __skb_unlink(list_skb, list);
Ursula Braun3881ac42011-08-08 01:33:54 +00002312 atomic_dec(&iucv->pendings);
2313 if (atomic_read(&iucv->pendings) <= 0)
2314 iucv_sock_wake_msglim(sk);
Ursula Braun800c5eb2012-02-08 00:19:47 +00002315 kfree_skb(list_skb);
Ursula Braun3881ac42011-08-08 01:33:54 +00002316 break;
2317 case TX_NOTIFY_UNREACHABLE:
2318 case TX_NOTIFY_DELAYED_UNREACHABLE:
2319 case TX_NOTIFY_TPQFULL: /* not yet used */
2320 case TX_NOTIFY_GENERALERROR:
2321 case TX_NOTIFY_DELAYED_GENERALERROR:
Ursula Braun800c5eb2012-02-08 00:19:47 +00002322 __skb_unlink(list_skb, list);
2323 kfree_skb(list_skb);
2324 if (sk->sk_state == IUCV_CONNECTED) {
2325 sk->sk_state = IUCV_DISCONN;
2326 sk->sk_state_change(sk);
2327 }
Ursula Braun3881ac42011-08-08 01:33:54 +00002328 break;
2329 }
2330 break;
2331 }
Ursula Braun800c5eb2012-02-08 00:19:47 +00002332 list_skb = nskb;
2333 nskb = nskb->next;
Ursula Braun3881ac42011-08-08 01:33:54 +00002334 }
Ursula Braun800c5eb2012-02-08 00:19:47 +00002335out_unlock:
Ursula Braun3881ac42011-08-08 01:33:54 +00002336 spin_unlock_irqrestore(&list->lock, flags);
2337
Ursula Braun42bd48e2011-12-19 22:56:28 +00002338 if (sk->sk_state == IUCV_CLOSING) {
2339 if (skb_queue_empty(&iucv_sk(sk)->send_skb_q)) {
2340 sk->sk_state = IUCV_CLOSED;
2341 sk->sk_state_change(sk);
2342 }
2343 }
2344
Ursula Braun3881ac42011-08-08 01:33:54 +00002345}
Ursula Braun9fbd87d2012-03-07 02:06:23 +00002346
2347/*
2348 * afiucv_netdev_event: handle netdev notifier chain events
2349 */
2350static int afiucv_netdev_event(struct notifier_block *this,
2351 unsigned long event, void *ptr)
2352{
Jiri Pirko351638e2013-05-28 01:30:21 +00002353 struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
Ursula Braun9fbd87d2012-03-07 02:06:23 +00002354 struct sock *sk;
2355 struct iucv_sock *iucv;
2356
2357 switch (event) {
2358 case NETDEV_REBOOT:
2359 case NETDEV_GOING_DOWN:
Sasha Levinb67bfe02013-02-27 17:06:00 -08002360 sk_for_each(sk, &iucv_sk_list.head) {
Ursula Braun9fbd87d2012-03-07 02:06:23 +00002361 iucv = iucv_sk(sk);
2362 if ((iucv->hs_dev == event_dev) &&
2363 (sk->sk_state == IUCV_CONNECTED)) {
2364 if (event == NETDEV_GOING_DOWN)
2365 iucv_send_ctrl(sk, AF_IUCV_FLAG_FIN);
2366 sk->sk_state = IUCV_DISCONN;
2367 sk->sk_state_change(sk);
2368 }
2369 }
2370 break;
2371 case NETDEV_DOWN:
2372 case NETDEV_UNREGISTER:
2373 default:
2374 break;
2375 }
2376 return NOTIFY_DONE;
2377}
2378
2379static struct notifier_block afiucv_netdev_notifier = {
2380 .notifier_call = afiucv_netdev_event,
2381};
2382
Alexey Dobriyan5708e862009-09-14 12:23:23 +00002383static const struct proto_ops iucv_sock_ops = {
Jennifer Hunteac37312007-02-08 13:51:54 -08002384 .family = PF_IUCV,
2385 .owner = THIS_MODULE,
2386 .release = iucv_sock_release,
2387 .bind = iucv_sock_bind,
2388 .connect = iucv_sock_connect,
2389 .listen = iucv_sock_listen,
2390 .accept = iucv_sock_accept,
2391 .getname = iucv_sock_getname,
2392 .sendmsg = iucv_sock_sendmsg,
2393 .recvmsg = iucv_sock_recvmsg,
2394 .poll = iucv_sock_poll,
2395 .ioctl = sock_no_ioctl,
2396 .mmap = sock_no_mmap,
2397 .socketpair = sock_no_socketpair,
2398 .shutdown = iucv_sock_shutdown,
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00002399 .setsockopt = iucv_sock_setsockopt,
2400 .getsockopt = iucv_sock_getsockopt,
Jennifer Hunteac37312007-02-08 13:51:54 -08002401};
2402
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00002403static const struct net_proto_family iucv_sock_family_ops = {
Jennifer Hunteac37312007-02-08 13:51:54 -08002404 .family = AF_IUCV,
2405 .owner = THIS_MODULE,
2406 .create = iucv_sock_create,
2407};
2408
Ursula Braun3881ac42011-08-08 01:33:54 +00002409static struct packet_type iucv_packet_type = {
2410 .type = cpu_to_be16(ETH_P_AF_IUCV),
2411 .func = afiucv_hs_rcv,
2412};
2413
2414static int afiucv_iucv_init(void)
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00002415{
2416 int err;
2417
2418 err = pr_iucv->iucv_register(&af_iucv_handler, 0);
2419 if (err)
2420 goto out;
2421 /* establish dummy device */
2422 af_iucv_driver.bus = pr_iucv->bus;
2423 err = driver_register(&af_iucv_driver);
2424 if (err)
2425 goto out_iucv;
2426 af_iucv_dev = kzalloc(sizeof(struct device), GFP_KERNEL);
2427 if (!af_iucv_dev) {
2428 err = -ENOMEM;
2429 goto out_driver;
2430 }
2431 dev_set_name(af_iucv_dev, "af_iucv");
2432 af_iucv_dev->bus = pr_iucv->bus;
2433 af_iucv_dev->parent = pr_iucv->root;
2434 af_iucv_dev->release = (void (*)(struct device *))kfree;
2435 af_iucv_dev->driver = &af_iucv_driver;
2436 err = device_register(af_iucv_dev);
2437 if (err)
2438 goto out_driver;
2439 return 0;
2440
2441out_driver:
2442 driver_unregister(&af_iucv_driver);
2443out_iucv:
2444 pr_iucv->iucv_unregister(&af_iucv_handler, 0);
2445out:
2446 return err;
2447}
2448
Heiko Carstensda99f052007-05-04 12:23:27 -07002449static int __init afiucv_init(void)
Jennifer Hunteac37312007-02-08 13:51:54 -08002450{
2451 int err;
2452
Ursula Braun3881ac42011-08-08 01:33:54 +00002453 if (MACHINE_IS_VM) {
2454 cpcmd("QUERY USERID", iucv_userid, sizeof(iucv_userid), &err);
2455 if (unlikely(err)) {
2456 WARN_ON(err);
2457 err = -EPROTONOSUPPORT;
2458 goto out;
2459 }
Jennifer Hunteac37312007-02-08 13:51:54 -08002460
Ursula Braun3881ac42011-08-08 01:33:54 +00002461 pr_iucv = try_then_request_module(symbol_get(iucv_if), "iucv");
2462 if (!pr_iucv) {
2463 printk(KERN_WARNING "iucv_if lookup failed\n");
2464 memset(&iucv_userid, 0, sizeof(iucv_userid));
2465 }
2466 } else {
2467 memset(&iucv_userid, 0, sizeof(iucv_userid));
2468 pr_iucv = NULL;
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00002469 }
2470
Jennifer Hunteac37312007-02-08 13:51:54 -08002471 err = proto_register(&iucv_proto, 0);
2472 if (err)
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00002473 goto out;
Jennifer Hunteac37312007-02-08 13:51:54 -08002474 err = sock_register(&iucv_sock_family_ops);
2475 if (err)
2476 goto out_proto;
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00002477
Ursula Braun3881ac42011-08-08 01:33:54 +00002478 if (pr_iucv) {
2479 err = afiucv_iucv_init();
2480 if (err)
2481 goto out_sock;
Ursula Braun9fbd87d2012-03-07 02:06:23 +00002482 } else
2483 register_netdevice_notifier(&afiucv_netdev_notifier);
Ursula Braun3881ac42011-08-08 01:33:54 +00002484 dev_add_pack(&iucv_packet_type);
Jennifer Hunteac37312007-02-08 13:51:54 -08002485 return 0;
2486
Ursula Braunc23cad92009-06-16 10:30:44 +02002487out_sock:
2488 sock_unregister(PF_IUCV);
Jennifer Hunteac37312007-02-08 13:51:54 -08002489out_proto:
2490 proto_unregister(&iucv_proto);
Jennifer Hunteac37312007-02-08 13:51:54 -08002491out:
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00002492 if (pr_iucv)
2493 symbol_put(iucv_if);
Jennifer Hunteac37312007-02-08 13:51:54 -08002494 return err;
2495}
2496
2497static void __exit afiucv_exit(void)
2498{
Ursula Braun3881ac42011-08-08 01:33:54 +00002499 if (pr_iucv) {
2500 device_unregister(af_iucv_dev);
2501 driver_unregister(&af_iucv_driver);
2502 pr_iucv->iucv_unregister(&af_iucv_handler, 0);
2503 symbol_put(iucv_if);
Ursula Braun9fbd87d2012-03-07 02:06:23 +00002504 } else
2505 unregister_netdevice_notifier(&afiucv_netdev_notifier);
Ursula Braun3881ac42011-08-08 01:33:54 +00002506 dev_remove_pack(&iucv_packet_type);
Jennifer Hunteac37312007-02-08 13:51:54 -08002507 sock_unregister(PF_IUCV);
2508 proto_unregister(&iucv_proto);
Jennifer Hunteac37312007-02-08 13:51:54 -08002509}
2510
2511module_init(afiucv_init);
2512module_exit(afiucv_exit);
2513
2514MODULE_AUTHOR("Jennifer Hunt <jenhunt@us.ibm.com>");
2515MODULE_DESCRIPTION("IUCV Sockets ver " VERSION);
2516MODULE_VERSION(VERSION);
2517MODULE_LICENSE("GPL");
2518MODULE_ALIAS_NETPROTO(PF_IUCV);
Ursula Braun3881ac42011-08-08 01:33:54 +00002519