blob: 88d0eaa18749213b7152ca71dc6b6a18a5a83dfc [file] [log] [blame]
Jennifer Hunteac37312007-02-08 13:51:54 -08001/*
Jennifer Hunteac37312007-02-08 13:51:54 -08002 * IUCV protocol stack for Linux on zSeries
3 *
Ursula Braunc23cad92009-06-16 10:30:44 +02004 * Copyright IBM Corp. 2006, 2009
Jennifer Hunteac37312007-02-08 13:51:54 -08005 *
6 * Author(s): Jennifer Hunt <jenhunt@us.ibm.com>
Ursula Braunc23cad92009-06-16 10:30:44 +02007 * Hendrik Brueckner <brueckner@linux.vnet.ibm.com>
8 * PM functions:
9 * Ursula Braun <ursula.braun@de.ibm.com>
Jennifer Hunteac37312007-02-08 13:51:54 -080010 */
11
Ursula Braun8f7c5022008-12-25 13:39:47 +010012#define KMSG_COMPONENT "af_iucv"
13#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
14
Jennifer Hunteac37312007-02-08 13:51:54 -080015#include <linux/module.h>
16#include <linux/types.h>
17#include <linux/list.h>
18#include <linux/errno.h>
19#include <linux/kernel.h>
20#include <linux/sched.h>
21#include <linux/slab.h>
22#include <linux/skbuff.h>
23#include <linux/init.h>
24#include <linux/poll.h>
Paul Moore02f06912016-06-07 10:30:53 -040025#include <linux/security.h>
Jennifer Hunteac37312007-02-08 13:51:54 -080026#include <net/sock.h>
27#include <asm/ebcdic.h>
28#include <asm/cpcmd.h>
29#include <linux/kmod.h>
30
Jennifer Hunteac37312007-02-08 13:51:54 -080031#include <net/iucv/af_iucv.h>
32
Ursula Braun3881ac42011-08-08 01:33:54 +000033#define VERSION "1.2"
Jennifer Hunteac37312007-02-08 13:51:54 -080034
35static char iucv_userid[80];
36
Alexey Dobriyan5708e862009-09-14 12:23:23 +000037static const struct proto_ops iucv_sock_ops;
Jennifer Hunteac37312007-02-08 13:51:54 -080038
39static struct proto iucv_proto = {
40 .name = "AF_IUCV",
41 .owner = THIS_MODULE,
42 .obj_size = sizeof(struct iucv_sock),
43};
44
Frank Blaschka6fcd61f2011-08-08 01:33:51 +000045static struct iucv_interface *pr_iucv;
46
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +000047/* special AF_IUCV IPRM messages */
48static const u8 iprm_shutdown[8] =
49 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01};
50
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +000051#define TRGCLS_SIZE (sizeof(((struct iucv_message *)0)->class))
52
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +000053#define __iucv_sock_wait(sk, condition, timeo, ret) \
54do { \
55 DEFINE_WAIT(__wait); \
56 long __timeo = timeo; \
57 ret = 0; \
Eric Dumazetaa395142010-04-20 13:03:51 +000058 prepare_to_wait(sk_sleep(sk), &__wait, TASK_INTERRUPTIBLE); \
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +000059 while (!(condition)) { \
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +000060 if (!__timeo) { \
61 ret = -EAGAIN; \
62 break; \
63 } \
64 if (signal_pending(current)) { \
65 ret = sock_intr_errno(__timeo); \
66 break; \
67 } \
68 release_sock(sk); \
69 __timeo = schedule_timeout(__timeo); \
70 lock_sock(sk); \
71 ret = sock_error(sk); \
72 if (ret) \
73 break; \
74 } \
Eric Dumazetaa395142010-04-20 13:03:51 +000075 finish_wait(sk_sleep(sk), &__wait); \
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +000076} while (0)
77
78#define iucv_sock_wait(sk, condition, timeo) \
79({ \
80 int __ret = 0; \
81 if (!(condition)) \
82 __iucv_sock_wait(sk, condition, timeo, __ret); \
83 __ret; \
84})
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +000085
Heiko Carstens57f20442007-10-08 02:02:52 -070086static void iucv_sock_kill(struct sock *sk);
87static void iucv_sock_close(struct sock *sk);
Ursula Braun7d316b92012-02-08 00:19:45 +000088static void iucv_sever_path(struct sock *, int);
Heiko Carstens57f20442007-10-08 02:02:52 -070089
Ursula Braun3881ac42011-08-08 01:33:54 +000090static int afiucv_hs_rcv(struct sk_buff *skb, struct net_device *dev,
91 struct packet_type *pt, struct net_device *orig_dev);
92static int afiucv_hs_send(struct iucv_message *imsg, struct sock *sock,
93 struct sk_buff *skb, u8 flags);
94static void afiucv_hs_callback_txnotify(struct sk_buff *, enum iucv_tx_notify);
95
Jennifer Hunteac37312007-02-08 13:51:54 -080096/* Call Back functions */
97static void iucv_callback_rx(struct iucv_path *, struct iucv_message *);
98static void iucv_callback_txdone(struct iucv_path *, struct iucv_message *);
Ursula Braun91e60eb2015-09-18 16:06:52 +020099static void iucv_callback_connack(struct iucv_path *, u8 *);
100static int iucv_callback_connreq(struct iucv_path *, u8 *, u8 *);
101static void iucv_callback_connrej(struct iucv_path *, u8 *);
102static void iucv_callback_shutdown(struct iucv_path *, u8 *);
Jennifer Hunteac37312007-02-08 13:51:54 -0800103
104static struct iucv_sock_list iucv_sk_list = {
Robert P. J. Day3db8ce32008-04-10 02:11:24 -0700105 .lock = __RW_LOCK_UNLOCKED(iucv_sk_list.lock),
Jennifer Hunteac37312007-02-08 13:51:54 -0800106 .autobind_name = ATOMIC_INIT(0)
107};
108
109static struct iucv_handler af_iucv_handler = {
110 .path_pending = iucv_callback_connreq,
111 .path_complete = iucv_callback_connack,
112 .path_severed = iucv_callback_connrej,
113 .message_pending = iucv_callback_rx,
Hendrik Brueckneraf88b522009-04-21 23:26:21 +0000114 .message_complete = iucv_callback_txdone,
115 .path_quiesced = iucv_callback_shutdown,
Jennifer Hunteac37312007-02-08 13:51:54 -0800116};
117
118static inline void high_nmcpy(unsigned char *dst, char *src)
119{
120 memcpy(dst, src, 8);
121}
122
123static inline void low_nmcpy(unsigned char *dst, char *src)
124{
125 memcpy(&dst[8], src, 8);
126}
127
Ursula Braunc23cad92009-06-16 10:30:44 +0200128static int afiucv_pm_prepare(struct device *dev)
129{
130#ifdef CONFIG_PM_DEBUG
131 printk(KERN_WARNING "afiucv_pm_prepare\n");
132#endif
133 return 0;
134}
135
136static void afiucv_pm_complete(struct device *dev)
137{
138#ifdef CONFIG_PM_DEBUG
139 printk(KERN_WARNING "afiucv_pm_complete\n");
140#endif
Ursula Braunc23cad92009-06-16 10:30:44 +0200141}
142
143/**
144 * afiucv_pm_freeze() - Freeze PM callback
145 * @dev: AFIUCV dummy device
146 *
147 * Sever all established IUCV communication pathes
148 */
149static int afiucv_pm_freeze(struct device *dev)
150{
151 struct iucv_sock *iucv;
152 struct sock *sk;
Ursula Braunc23cad92009-06-16 10:30:44 +0200153 int err = 0;
154
155#ifdef CONFIG_PM_DEBUG
156 printk(KERN_WARNING "afiucv_pm_freeze\n");
157#endif
158 read_lock(&iucv_sk_list.lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800159 sk_for_each(sk, &iucv_sk_list.head) {
Ursula Braunc23cad92009-06-16 10:30:44 +0200160 iucv = iucv_sk(sk);
Ursula Braunc23cad92009-06-16 10:30:44 +0200161 switch (sk->sk_state) {
Ursula Braunc23cad92009-06-16 10:30:44 +0200162 case IUCV_DISCONN:
163 case IUCV_CLOSING:
164 case IUCV_CONNECTED:
Ursula Braun7d316b92012-02-08 00:19:45 +0000165 iucv_sever_path(sk, 0);
Ursula Braunc23cad92009-06-16 10:30:44 +0200166 break;
167 case IUCV_OPEN:
168 case IUCV_BOUND:
169 case IUCV_LISTEN:
170 case IUCV_CLOSED:
171 default:
172 break;
173 }
Ursula Braun7d316b92012-02-08 00:19:45 +0000174 skb_queue_purge(&iucv->send_skb_q);
175 skb_queue_purge(&iucv->backlog_skb_q);
Ursula Braunc23cad92009-06-16 10:30:44 +0200176 }
177 read_unlock(&iucv_sk_list.lock);
178 return err;
179}
180
181/**
182 * afiucv_pm_restore_thaw() - Thaw and restore PM callback
183 * @dev: AFIUCV dummy device
184 *
185 * socket clean up after freeze
186 */
187static int afiucv_pm_restore_thaw(struct device *dev)
188{
Ursula Braunc23cad92009-06-16 10:30:44 +0200189 struct sock *sk;
Ursula Braunc23cad92009-06-16 10:30:44 +0200190
191#ifdef CONFIG_PM_DEBUG
192 printk(KERN_WARNING "afiucv_pm_restore_thaw\n");
193#endif
194 read_lock(&iucv_sk_list.lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800195 sk_for_each(sk, &iucv_sk_list.head) {
Ursula Braunc23cad92009-06-16 10:30:44 +0200196 switch (sk->sk_state) {
197 case IUCV_CONNECTED:
198 sk->sk_err = EPIPE;
199 sk->sk_state = IUCV_DISCONN;
200 sk->sk_state_change(sk);
201 break;
202 case IUCV_DISCONN:
Ursula Braunc23cad92009-06-16 10:30:44 +0200203 case IUCV_CLOSING:
204 case IUCV_LISTEN:
205 case IUCV_BOUND:
206 case IUCV_OPEN:
207 default:
208 break;
209 }
210 }
211 read_unlock(&iucv_sk_list.lock);
212 return 0;
213}
214
Alexey Dobriyan47145212009-12-14 18:00:08 -0800215static const struct dev_pm_ops afiucv_pm_ops = {
Ursula Braunc23cad92009-06-16 10:30:44 +0200216 .prepare = afiucv_pm_prepare,
217 .complete = afiucv_pm_complete,
218 .freeze = afiucv_pm_freeze,
219 .thaw = afiucv_pm_restore_thaw,
220 .restore = afiucv_pm_restore_thaw,
221};
222
223static struct device_driver af_iucv_driver = {
224 .owner = THIS_MODULE,
225 .name = "afiucv",
Frank Blaschka6fcd61f2011-08-08 01:33:51 +0000226 .bus = NULL,
Ursula Braunc23cad92009-06-16 10:30:44 +0200227 .pm = &afiucv_pm_ops,
228};
229
230/* dummy device used as trigger for PM functions */
231static struct device *af_iucv_dev;
232
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000233/**
234 * iucv_msg_length() - Returns the length of an iucv message.
235 * @msg: Pointer to struct iucv_message, MUST NOT be NULL
236 *
237 * The function returns the length of the specified iucv message @msg of data
238 * stored in a buffer and of data stored in the parameter list (PRMDATA).
239 *
240 * For IUCV_IPRMDATA, AF_IUCV uses the following convention to transport socket
241 * data:
242 * PRMDATA[0..6] socket data (max 7 bytes);
243 * PRMDATA[7] socket data length value (len is 0xff - PRMDATA[7])
244 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300245 * The socket data length is computed by subtracting the socket data length
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000246 * value from 0xFF.
247 * If the socket data len is greater 7, then PRMDATA can be used for special
248 * notifications (see iucv_sock_shutdown); and further,
249 * if the socket data len is > 7, the function returns 8.
250 *
251 * Use this function to allocate socket buffers to store iucv message data.
252 */
253static inline size_t iucv_msg_length(struct iucv_message *msg)
254{
255 size_t datalen;
256
257 if (msg->flags & IUCV_IPRMDATA) {
258 datalen = 0xff - msg->rmmsg[7];
259 return (datalen < 8) ? datalen : 8;
260 }
261 return msg->length;
262}
263
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +0000264/**
265 * iucv_sock_in_state() - check for specific states
266 * @sk: sock structure
267 * @state: first iucv sk state
268 * @state: second iucv sk state
269 *
270 * Returns true if the socket in either in the first or second state.
271 */
272static int iucv_sock_in_state(struct sock *sk, int state, int state2)
273{
274 return (sk->sk_state == state || sk->sk_state == state2);
275}
276
277/**
278 * iucv_below_msglim() - function to check if messages can be sent
279 * @sk: sock structure
280 *
281 * Returns true if the send queue length is lower than the message limit.
282 * Always returns true if the socket is not connected (no iucv path for
283 * checking the message limit).
284 */
285static inline int iucv_below_msglim(struct sock *sk)
286{
287 struct iucv_sock *iucv = iucv_sk(sk);
288
289 if (sk->sk_state != IUCV_CONNECTED)
290 return 1;
Ursula Braun3881ac42011-08-08 01:33:54 +0000291 if (iucv->transport == AF_IUCV_TRANS_IUCV)
292 return (skb_queue_len(&iucv->send_skb_q) < iucv->path->msglim);
293 else
294 return ((atomic_read(&iucv->msg_sent) < iucv->msglimit_peer) &&
295 (atomic_read(&iucv->pendings) <= 0));
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +0000296}
297
298/**
299 * iucv_sock_wake_msglim() - Wake up thread waiting on msg limit
300 */
301static void iucv_sock_wake_msglim(struct sock *sk)
302{
Eric Dumazet43815482010-04-29 11:01:49 +0000303 struct socket_wq *wq;
304
305 rcu_read_lock();
306 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +0800307 if (skwq_has_sleeper(wq))
Eric Dumazet43815482010-04-29 11:01:49 +0000308 wake_up_interruptible_all(&wq->wait);
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +0000309 sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
Eric Dumazet43815482010-04-29 11:01:49 +0000310 rcu_read_unlock();
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +0000311}
312
Ursula Braun3881ac42011-08-08 01:33:54 +0000313/**
314 * afiucv_hs_send() - send a message through HiperSockets transport
315 */
316static int afiucv_hs_send(struct iucv_message *imsg, struct sock *sock,
317 struct sk_buff *skb, u8 flags)
318{
Ursula Braun3881ac42011-08-08 01:33:54 +0000319 struct iucv_sock *iucv = iucv_sk(sock);
320 struct af_iucv_trans_hdr *phs_hdr;
321 struct sk_buff *nskb;
322 int err, confirm_recv = 0;
323
324 memset(skb->head, 0, ETH_HLEN);
325 phs_hdr = (struct af_iucv_trans_hdr *)skb_push(skb,
326 sizeof(struct af_iucv_trans_hdr));
327 skb_reset_mac_header(skb);
328 skb_reset_network_header(skb);
329 skb_push(skb, ETH_HLEN);
330 skb_reset_mac_header(skb);
331 memset(phs_hdr, 0, sizeof(struct af_iucv_trans_hdr));
332
333 phs_hdr->magic = ETH_P_AF_IUCV;
334 phs_hdr->version = 1;
335 phs_hdr->flags = flags;
336 if (flags == AF_IUCV_FLAG_SYN)
337 phs_hdr->window = iucv->msglimit;
338 else if ((flags == AF_IUCV_FLAG_WIN) || !flags) {
339 confirm_recv = atomic_read(&iucv->msg_recv);
340 phs_hdr->window = confirm_recv;
341 if (confirm_recv)
342 phs_hdr->flags = phs_hdr->flags | AF_IUCV_FLAG_WIN;
343 }
344 memcpy(phs_hdr->destUserID, iucv->dst_user_id, 8);
345 memcpy(phs_hdr->destAppName, iucv->dst_name, 8);
346 memcpy(phs_hdr->srcUserID, iucv->src_user_id, 8);
347 memcpy(phs_hdr->srcAppName, iucv->src_name, 8);
348 ASCEBC(phs_hdr->destUserID, sizeof(phs_hdr->destUserID));
349 ASCEBC(phs_hdr->destAppName, sizeof(phs_hdr->destAppName));
350 ASCEBC(phs_hdr->srcUserID, sizeof(phs_hdr->srcUserID));
351 ASCEBC(phs_hdr->srcAppName, sizeof(phs_hdr->srcAppName));
352 if (imsg)
353 memcpy(&phs_hdr->iucv_hdr, imsg, sizeof(struct iucv_message));
354
Ursula Braun800c5eb2012-02-08 00:19:47 +0000355 skb->dev = iucv->hs_dev;
Ursula Braun3881ac42011-08-08 01:33:54 +0000356 if (!skb->dev)
357 return -ENODEV;
Ursula Braun800c5eb2012-02-08 00:19:47 +0000358 if (!(skb->dev->flags & IFF_UP) || !netif_carrier_ok(skb->dev))
Ursula Braun3881ac42011-08-08 01:33:54 +0000359 return -ENETDOWN;
360 if (skb->len > skb->dev->mtu) {
361 if (sock->sk_type == SOCK_SEQPACKET)
362 return -EMSGSIZE;
363 else
364 skb_trim(skb, skb->dev->mtu);
365 }
366 skb->protocol = ETH_P_AF_IUCV;
Ursula Braun3881ac42011-08-08 01:33:54 +0000367 nskb = skb_clone(skb, GFP_ATOMIC);
368 if (!nskb)
369 return -ENOMEM;
370 skb_queue_tail(&iucv->send_skb_q, nskb);
371 err = dev_queue_xmit(skb);
Ursula Braun800c5eb2012-02-08 00:19:47 +0000372 if (net_xmit_eval(err)) {
Ursula Braun3881ac42011-08-08 01:33:54 +0000373 skb_unlink(nskb, &iucv->send_skb_q);
374 kfree_skb(nskb);
375 } else {
376 atomic_sub(confirm_recv, &iucv->msg_recv);
377 WARN_ON(atomic_read(&iucv->msg_recv) < 0);
378 }
Ursula Braun800c5eb2012-02-08 00:19:47 +0000379 return net_xmit_eval(err);
Ursula Braun3881ac42011-08-08 01:33:54 +0000380}
381
Jennifer Hunteac37312007-02-08 13:51:54 -0800382static struct sock *__iucv_get_sock_by_name(char *nm)
383{
384 struct sock *sk;
Jennifer Hunteac37312007-02-08 13:51:54 -0800385
Sasha Levinb67bfe02013-02-27 17:06:00 -0800386 sk_for_each(sk, &iucv_sk_list.head)
Jennifer Hunteac37312007-02-08 13:51:54 -0800387 if (!memcmp(&iucv_sk(sk)->src_name, nm, 8))
388 return sk;
389
390 return NULL;
391}
392
393static void iucv_sock_destruct(struct sock *sk)
394{
395 skb_queue_purge(&sk->sk_receive_queue);
Ursula Braun82492a32012-03-07 02:06:24 +0000396 skb_queue_purge(&sk->sk_error_queue);
397
398 sk_mem_reclaim(sk);
399
400 if (!sock_flag(sk, SOCK_DEAD)) {
401 pr_err("Attempt to release alive iucv socket %p\n", sk);
402 return;
403 }
404
405 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
406 WARN_ON(atomic_read(&sk->sk_wmem_alloc));
407 WARN_ON(sk->sk_wmem_queued);
408 WARN_ON(sk->sk_forward_alloc);
Jennifer Hunteac37312007-02-08 13:51:54 -0800409}
410
411/* Cleanup Listen */
412static void iucv_sock_cleanup_listen(struct sock *parent)
413{
414 struct sock *sk;
415
416 /* Close non-accepted connections */
417 while ((sk = iucv_accept_dequeue(parent, NULL))) {
418 iucv_sock_close(sk);
419 iucv_sock_kill(sk);
420 }
421
422 parent->sk_state = IUCV_CLOSED;
Jennifer Hunteac37312007-02-08 13:51:54 -0800423}
424
Hendrik Brueckner7514bab2009-09-16 04:37:27 +0000425/* Kill socket (only if zapped and orphaned) */
Jennifer Hunteac37312007-02-08 13:51:54 -0800426static void iucv_sock_kill(struct sock *sk)
427{
428 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
429 return;
430
431 iucv_sock_unlink(&iucv_sk_list, sk);
432 sock_set_flag(sk, SOCK_DEAD);
433 sock_put(sk);
434}
435
Ursula Braun7d316b92012-02-08 00:19:45 +0000436/* Terminate an IUCV path */
437static void iucv_sever_path(struct sock *sk, int with_user_data)
438{
439 unsigned char user_data[16];
440 struct iucv_sock *iucv = iucv_sk(sk);
441 struct iucv_path *path = iucv->path;
442
443 if (iucv->path) {
444 iucv->path = NULL;
445 if (with_user_data) {
446 low_nmcpy(user_data, iucv->src_name);
447 high_nmcpy(user_data, iucv->dst_name);
448 ASCEBC(user_data, sizeof(user_data));
449 pr_iucv->path_sever(path, user_data);
450 } else
451 pr_iucv->path_sever(path, NULL);
452 iucv_path_free(path);
453 }
454}
455
Ursula Braun4e0ad322016-10-07 15:51:47 +0200456/* Send controlling flags through an IUCV socket for HIPER transport */
Ursula Braun9fbd87d2012-03-07 02:06:23 +0000457static int iucv_send_ctrl(struct sock *sk, u8 flags)
458{
459 int err = 0;
460 int blen;
461 struct sk_buff *skb;
Ursula Braun4e0ad322016-10-07 15:51:47 +0200462 u8 shutdown = 0;
Ursula Braun9fbd87d2012-03-07 02:06:23 +0000463
464 blen = sizeof(struct af_iucv_trans_hdr) + ETH_HLEN;
Ursula Braun4e0ad322016-10-07 15:51:47 +0200465 if (sk->sk_shutdown & SEND_SHUTDOWN) {
466 /* controlling flags should be sent anyway */
467 shutdown = sk->sk_shutdown;
468 sk->sk_shutdown &= RCV_SHUTDOWN;
469 }
Ursula Braun9fbd87d2012-03-07 02:06:23 +0000470 skb = sock_alloc_send_skb(sk, blen, 1, &err);
471 if (skb) {
472 skb_reserve(skb, blen);
473 err = afiucv_hs_send(NULL, sk, skb, flags);
474 }
Ursula Braun4e0ad322016-10-07 15:51:47 +0200475 if (shutdown)
476 sk->sk_shutdown = shutdown;
Ursula Braun9fbd87d2012-03-07 02:06:23 +0000477 return err;
478}
479
Jennifer Hunteac37312007-02-08 13:51:54 -0800480/* Close an IUCV socket */
481static void iucv_sock_close(struct sock *sk)
482{
Jennifer Hunteac37312007-02-08 13:51:54 -0800483 struct iucv_sock *iucv = iucv_sk(sk);
Jennifer Hunt561e0362007-05-04 12:22:07 -0700484 unsigned long timeo;
Ursula Braun800c5eb2012-02-08 00:19:47 +0000485 int err = 0;
Jennifer Hunteac37312007-02-08 13:51:54 -0800486
Jennifer Hunteac37312007-02-08 13:51:54 -0800487 lock_sock(sk);
488
Heiko Carstensda99f052007-05-04 12:23:27 -0700489 switch (sk->sk_state) {
Jennifer Hunteac37312007-02-08 13:51:54 -0800490 case IUCV_LISTEN:
491 iucv_sock_cleanup_listen(sk);
492 break;
493
494 case IUCV_CONNECTED:
Ursula Braun3881ac42011-08-08 01:33:54 +0000495 if (iucv->transport == AF_IUCV_TRANS_HIPER) {
Ursula Braun9fbd87d2012-03-07 02:06:23 +0000496 err = iucv_send_ctrl(sk, AF_IUCV_FLAG_FIN);
Ursula Braun3881ac42011-08-08 01:33:54 +0000497 sk->sk_state = IUCV_DISCONN;
498 sk->sk_state_change(sk);
499 }
Ursula Braun816abba2011-12-19 22:56:29 +0000500 case IUCV_DISCONN: /* fall through */
Jennifer Hunt561e0362007-05-04 12:22:07 -0700501 sk->sk_state = IUCV_CLOSING;
502 sk->sk_state_change(sk);
503
Ursula Braun800c5eb2012-02-08 00:19:47 +0000504 if (!err && !skb_queue_empty(&iucv->send_skb_q)) {
Jennifer Hunt561e0362007-05-04 12:22:07 -0700505 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
506 timeo = sk->sk_lingertime;
507 else
508 timeo = IUCV_DISCONN_TIMEOUT;
Ursula Braun9f6298a2011-05-12 18:45:08 +0000509 iucv_sock_wait(sk,
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +0000510 iucv_sock_in_state(sk, IUCV_CLOSED, 0),
511 timeo);
Jennifer Hunt561e0362007-05-04 12:22:07 -0700512 }
513
Ursula Braunbbe188c2009-04-21 06:04:20 +0000514 case IUCV_CLOSING: /* fall through */
Jennifer Hunt561e0362007-05-04 12:22:07 -0700515 sk->sk_state = IUCV_CLOSED;
516 sk->sk_state_change(sk);
517
Jennifer Hunteac37312007-02-08 13:51:54 -0800518 sk->sk_err = ECONNRESET;
519 sk->sk_state_change(sk);
520
Ursula Braun800c5eb2012-02-08 00:19:47 +0000521 skb_queue_purge(&iucv->send_skb_q);
Jennifer Hunt561e0362007-05-04 12:22:07 -0700522 skb_queue_purge(&iucv->backlog_skb_q);
Jennifer Hunteac37312007-02-08 13:51:54 -0800523
Ursula Braun7d316b92012-02-08 00:19:45 +0000524 default: /* fall through */
525 iucv_sever_path(sk, 1);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700526 }
Jennifer Hunteac37312007-02-08 13:51:54 -0800527
Ursula Braun800c5eb2012-02-08 00:19:47 +0000528 if (iucv->hs_dev) {
529 dev_put(iucv->hs_dev);
530 iucv->hs_dev = NULL;
531 sk->sk_bound_dev_if = 0;
532 }
533
Hendrik Brueckner7514bab2009-09-16 04:37:27 +0000534 /* mark socket for deletion by iucv_sock_kill() */
535 sock_set_flag(sk, SOCK_ZAPPED);
536
Jennifer Hunteac37312007-02-08 13:51:54 -0800537 release_sock(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800538}
539
540static void iucv_sock_init(struct sock *sk, struct sock *parent)
541{
Paul Moore02f06912016-06-07 10:30:53 -0400542 if (parent) {
Jennifer Hunteac37312007-02-08 13:51:54 -0800543 sk->sk_type = parent->sk_type;
Paul Moore02f06912016-06-07 10:30:53 -0400544 security_sk_clone(parent, sk);
545 }
Jennifer Hunteac37312007-02-08 13:51:54 -0800546}
547
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500548static struct sock *iucv_sock_alloc(struct socket *sock, int proto, gfp_t prio, int kern)
Jennifer Hunteac37312007-02-08 13:51:54 -0800549{
550 struct sock *sk;
Ursula Braun493d3972011-08-08 01:33:52 +0000551 struct iucv_sock *iucv;
Jennifer Hunteac37312007-02-08 13:51:54 -0800552
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500553 sk = sk_alloc(&init_net, PF_IUCV, prio, &iucv_proto, kern);
Jennifer Hunteac37312007-02-08 13:51:54 -0800554 if (!sk)
555 return NULL;
Ursula Braun493d3972011-08-08 01:33:52 +0000556 iucv = iucv_sk(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800557
558 sock_init_data(sock, sk);
Ursula Braun493d3972011-08-08 01:33:52 +0000559 INIT_LIST_HEAD(&iucv->accept_q);
560 spin_lock_init(&iucv->accept_q_lock);
561 skb_queue_head_init(&iucv->send_skb_q);
562 INIT_LIST_HEAD(&iucv->message_q.list);
563 spin_lock_init(&iucv->message_q.lock);
564 skb_queue_head_init(&iucv->backlog_skb_q);
565 iucv->send_tag = 0;
Ursula Braun3881ac42011-08-08 01:33:54 +0000566 atomic_set(&iucv->pendings, 0);
Ursula Braun493d3972011-08-08 01:33:52 +0000567 iucv->flags = 0;
Ursula Braun3881ac42011-08-08 01:33:54 +0000568 iucv->msglimit = 0;
569 atomic_set(&iucv->msg_sent, 0);
570 atomic_set(&iucv->msg_recv, 0);
Ursula Braun493d3972011-08-08 01:33:52 +0000571 iucv->path = NULL;
Ursula Braun3881ac42011-08-08 01:33:54 +0000572 iucv->sk_txnotify = afiucv_hs_callback_txnotify;
Ursula Braun493d3972011-08-08 01:33:52 +0000573 memset(&iucv->src_user_id , 0, 32);
Ursula Braun3881ac42011-08-08 01:33:54 +0000574 if (pr_iucv)
575 iucv->transport = AF_IUCV_TRANS_IUCV;
576 else
577 iucv->transport = AF_IUCV_TRANS_HIPER;
Jennifer Hunteac37312007-02-08 13:51:54 -0800578
579 sk->sk_destruct = iucv_sock_destruct;
580 sk->sk_sndtimeo = IUCV_CONN_TIMEOUT;
581 sk->sk_allocation = GFP_DMA;
582
583 sock_reset_flag(sk, SOCK_ZAPPED);
584
585 sk->sk_protocol = proto;
586 sk->sk_state = IUCV_OPEN;
587
Jennifer Hunteac37312007-02-08 13:51:54 -0800588 iucv_sock_link(&iucv_sk_list, sk);
589 return sk;
590}
591
592/* Create an IUCV socket */
Eric Paris3f378b62009-11-05 22:18:14 -0800593static int iucv_sock_create(struct net *net, struct socket *sock, int protocol,
594 int kern)
Jennifer Hunteac37312007-02-08 13:51:54 -0800595{
596 struct sock *sk;
597
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +0000598 if (protocol && protocol != PF_IUCV)
599 return -EPROTONOSUPPORT;
Jennifer Hunteac37312007-02-08 13:51:54 -0800600
601 sock->state = SS_UNCONNECTED;
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +0000602
603 switch (sock->type) {
604 case SOCK_STREAM:
605 sock->ops = &iucv_sock_ops;
606 break;
607 case SOCK_SEQPACKET:
608 /* currently, proto ops can handle both sk types */
609 sock->ops = &iucv_sock_ops;
610 break;
611 default:
612 return -ESOCKTNOSUPPORT;
613 }
Jennifer Hunteac37312007-02-08 13:51:54 -0800614
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500615 sk = iucv_sock_alloc(sock, protocol, GFP_KERNEL, kern);
Jennifer Hunteac37312007-02-08 13:51:54 -0800616 if (!sk)
617 return -ENOMEM;
618
619 iucv_sock_init(sk, NULL);
620
621 return 0;
622}
623
624void iucv_sock_link(struct iucv_sock_list *l, struct sock *sk)
625{
626 write_lock_bh(&l->lock);
627 sk_add_node(sk, &l->head);
628 write_unlock_bh(&l->lock);
629}
630
631void iucv_sock_unlink(struct iucv_sock_list *l, struct sock *sk)
632{
633 write_lock_bh(&l->lock);
634 sk_del_node_init(sk);
635 write_unlock_bh(&l->lock);
636}
637
638void iucv_accept_enqueue(struct sock *parent, struct sock *sk)
639{
Ursula Braunfebca282007-07-14 19:04:25 -0700640 unsigned long flags;
641 struct iucv_sock *par = iucv_sk(parent);
642
Jennifer Hunteac37312007-02-08 13:51:54 -0800643 sock_hold(sk);
Ursula Braunfebca282007-07-14 19:04:25 -0700644 spin_lock_irqsave(&par->accept_q_lock, flags);
645 list_add_tail(&iucv_sk(sk)->accept_q, &par->accept_q);
646 spin_unlock_irqrestore(&par->accept_q_lock, flags);
Jennifer Hunteac37312007-02-08 13:51:54 -0800647 iucv_sk(sk)->parent = parent;
Hendrik Brueckner49f5eba2009-10-14 22:54:55 +0000648 sk_acceptq_added(parent);
Jennifer Hunteac37312007-02-08 13:51:54 -0800649}
650
651void iucv_accept_unlink(struct sock *sk)
652{
Ursula Braunfebca282007-07-14 19:04:25 -0700653 unsigned long flags;
654 struct iucv_sock *par = iucv_sk(iucv_sk(sk)->parent);
655
656 spin_lock_irqsave(&par->accept_q_lock, flags);
Jennifer Hunteac37312007-02-08 13:51:54 -0800657 list_del_init(&iucv_sk(sk)->accept_q);
Ursula Braunfebca282007-07-14 19:04:25 -0700658 spin_unlock_irqrestore(&par->accept_q_lock, flags);
Hendrik Brueckner49f5eba2009-10-14 22:54:55 +0000659 sk_acceptq_removed(iucv_sk(sk)->parent);
Jennifer Hunteac37312007-02-08 13:51:54 -0800660 iucv_sk(sk)->parent = NULL;
661 sock_put(sk);
662}
663
664struct sock *iucv_accept_dequeue(struct sock *parent, struct socket *newsock)
665{
666 struct iucv_sock *isk, *n;
667 struct sock *sk;
668
Heiko Carstensda99f052007-05-04 12:23:27 -0700669 list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) {
Jennifer Hunteac37312007-02-08 13:51:54 -0800670 sk = (struct sock *) isk;
671 lock_sock(sk);
672
673 if (sk->sk_state == IUCV_CLOSED) {
Jennifer Hunteac37312007-02-08 13:51:54 -0800674 iucv_accept_unlink(sk);
Ursula Braunfebca282007-07-14 19:04:25 -0700675 release_sock(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800676 continue;
677 }
678
679 if (sk->sk_state == IUCV_CONNECTED ||
Ursula Braunaac63992011-12-19 22:56:31 +0000680 sk->sk_state == IUCV_DISCONN ||
Jennifer Hunteac37312007-02-08 13:51:54 -0800681 !newsock) {
682 iucv_accept_unlink(sk);
683 if (newsock)
684 sock_graft(sk, newsock);
685
Jennifer Hunteac37312007-02-08 13:51:54 -0800686 release_sock(sk);
687 return sk;
688 }
689
690 release_sock(sk);
691 }
692 return NULL;
693}
694
Philipp Hachtmann53a4b492014-05-28 10:22:28 +0200695static void __iucv_auto_name(struct iucv_sock *iucv)
696{
697 char name[12];
698
699 sprintf(name, "%08x", atomic_inc_return(&iucv_sk_list.autobind_name));
700 while (__iucv_get_sock_by_name(name)) {
701 sprintf(name, "%08x",
702 atomic_inc_return(&iucv_sk_list.autobind_name));
703 }
704 memcpy(iucv->src_name, name, 8);
705}
706
Jennifer Hunteac37312007-02-08 13:51:54 -0800707/* Bind an unbound socket */
708static int iucv_sock_bind(struct socket *sock, struct sockaddr *addr,
709 int addr_len)
710{
711 struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
712 struct sock *sk = sock->sk;
713 struct iucv_sock *iucv;
Ursula Braun3881ac42011-08-08 01:33:54 +0000714 int err = 0;
715 struct net_device *dev;
716 char uid[9];
Jennifer Hunteac37312007-02-08 13:51:54 -0800717
718 /* Verify the input sockaddr */
719 if (!addr || addr->sa_family != AF_IUCV)
720 return -EINVAL;
721
Ursula Braun52a82e22016-01-19 10:41:33 +0100722 if (addr_len < sizeof(struct sockaddr_iucv))
723 return -EINVAL;
724
Jennifer Hunteac37312007-02-08 13:51:54 -0800725 lock_sock(sk);
726 if (sk->sk_state != IUCV_OPEN) {
727 err = -EBADFD;
728 goto done;
729 }
730
731 write_lock_bh(&iucv_sk_list.lock);
732
733 iucv = iucv_sk(sk);
734 if (__iucv_get_sock_by_name(sa->siucv_name)) {
735 err = -EADDRINUSE;
736 goto done_unlock;
737 }
Ursula Braun3881ac42011-08-08 01:33:54 +0000738 if (iucv->path)
Jennifer Hunteac37312007-02-08 13:51:54 -0800739 goto done_unlock;
Jennifer Hunteac37312007-02-08 13:51:54 -0800740
741 /* Bind the socket */
Ursula Braun3881ac42011-08-08 01:33:54 +0000742 if (pr_iucv)
743 if (!memcmp(sa->siucv_user_id, iucv_userid, 8))
744 goto vm_bind; /* VM IUCV transport */
Jennifer Hunteac37312007-02-08 13:51:54 -0800745
Ursula Braun3881ac42011-08-08 01:33:54 +0000746 /* try hiper transport */
747 memcpy(uid, sa->siucv_user_id, sizeof(uid));
748 ASCEBC(uid, 8);
749 rcu_read_lock();
750 for_each_netdev_rcu(&init_net, dev) {
751 if (!memcmp(dev->perm_addr, uid, 8)) {
Ursula Braun3881ac42011-08-08 01:33:54 +0000752 memcpy(iucv->src_user_id, sa->siucv_user_id, 8);
Philipp Hachtmann53a4b492014-05-28 10:22:28 +0200753 /* Check for unitialized siucv_name */
754 if (strncmp(sa->siucv_name, " ", 8) == 0)
755 __iucv_auto_name(iucv);
756 else
757 memcpy(iucv->src_name, sa->siucv_name, 8);
Ursula Braun816abba2011-12-19 22:56:29 +0000758 sk->sk_bound_dev_if = dev->ifindex;
Ursula Braun800c5eb2012-02-08 00:19:47 +0000759 iucv->hs_dev = dev;
760 dev_hold(dev);
Ursula Braun3881ac42011-08-08 01:33:54 +0000761 sk->sk_state = IUCV_BOUND;
762 iucv->transport = AF_IUCV_TRANS_HIPER;
763 if (!iucv->msglimit)
764 iucv->msglimit = IUCV_HIPER_MSGLIM_DEFAULT;
765 rcu_read_unlock();
766 goto done_unlock;
767 }
768 }
769 rcu_read_unlock();
770vm_bind:
771 if (pr_iucv) {
772 /* use local userid for backward compat */
773 memcpy(iucv->src_name, sa->siucv_name, 8);
774 memcpy(iucv->src_user_id, iucv_userid, 8);
775 sk->sk_state = IUCV_BOUND;
776 iucv->transport = AF_IUCV_TRANS_IUCV;
777 if (!iucv->msglimit)
778 iucv->msglimit = IUCV_QUEUELEN_DEFAULT;
779 goto done_unlock;
780 }
781 /* found no dev to bind */
782 err = -ENODEV;
Jennifer Hunteac37312007-02-08 13:51:54 -0800783done_unlock:
784 /* Release the socket list lock */
785 write_unlock_bh(&iucv_sk_list.lock);
786done:
787 release_sock(sk);
788 return err;
789}
790
791/* Automatically bind an unbound socket */
792static int iucv_sock_autobind(struct sock *sk)
793{
794 struct iucv_sock *iucv = iucv_sk(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800795 int err = 0;
796
Ursula Braunaac63992011-12-19 22:56:31 +0000797 if (unlikely(!pr_iucv))
Jennifer Hunteac37312007-02-08 13:51:54 -0800798 return -EPROTO;
799
Ursula Braunaac63992011-12-19 22:56:31 +0000800 memcpy(iucv->src_user_id, iucv_userid, 8);
Jennifer Hunteac37312007-02-08 13:51:54 -0800801
802 write_lock_bh(&iucv_sk_list.lock);
Philipp Hachtmann53a4b492014-05-28 10:22:28 +0200803 __iucv_auto_name(iucv);
Jennifer Hunteac37312007-02-08 13:51:54 -0800804 write_unlock_bh(&iucv_sk_list.lock);
805
Ursula Braun3881ac42011-08-08 01:33:54 +0000806 if (!iucv->msglimit)
807 iucv->msglimit = IUCV_QUEUELEN_DEFAULT;
808
Jennifer Hunteac37312007-02-08 13:51:54 -0800809 return err;
810}
811
Ursula Braun3881ac42011-08-08 01:33:54 +0000812static int afiucv_path_connect(struct socket *sock, struct sockaddr *addr)
Jennifer Hunteac37312007-02-08 13:51:54 -0800813{
814 struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
815 struct sock *sk = sock->sk;
Ursula Braun493d3972011-08-08 01:33:52 +0000816 struct iucv_sock *iucv = iucv_sk(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800817 unsigned char user_data[16];
818 int err;
819
Jennifer Hunteac37312007-02-08 13:51:54 -0800820 high_nmcpy(user_data, sa->siucv_name);
Ursula Braun493d3972011-08-08 01:33:52 +0000821 low_nmcpy(user_data, iucv->src_name);
Jennifer Hunteac37312007-02-08 13:51:54 -0800822 ASCEBC(user_data, sizeof(user_data));
823
Jennifer Hunteac37312007-02-08 13:51:54 -0800824 /* Create path. */
Hendrik Brueckner09488e22009-04-21 23:26:27 +0000825 iucv->path = iucv_path_alloc(iucv->msglimit,
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000826 IUCV_IPRMDATA, GFP_KERNEL);
Ursula Braund4444722008-02-07 18:07:19 -0800827 if (!iucv->path) {
828 err = -ENOMEM;
829 goto done;
830 }
Frank Blaschka6fcd61f2011-08-08 01:33:51 +0000831 err = pr_iucv->path_connect(iucv->path, &af_iucv_handler,
832 sa->siucv_user_id, NULL, user_data,
833 sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800834 if (err) {
835 iucv_path_free(iucv->path);
836 iucv->path = NULL;
Hendrik Brueckner55cdea92009-01-05 18:07:07 -0800837 switch (err) {
838 case 0x0b: /* Target communicator is not logged on */
839 err = -ENETUNREACH;
840 break;
841 case 0x0d: /* Max connections for this guest exceeded */
842 case 0x0e: /* Max connections for target guest exceeded */
843 err = -EAGAIN;
844 break;
845 case 0x0f: /* Missing IUCV authorization */
846 err = -EACCES;
847 break;
848 default:
849 err = -ECONNREFUSED;
850 break;
851 }
Ursula Braun3881ac42011-08-08 01:33:54 +0000852 }
853done:
854 return err;
855}
856
857/* Connect an unconnected socket */
858static int iucv_sock_connect(struct socket *sock, struct sockaddr *addr,
859 int alen, int flags)
860{
861 struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
862 struct sock *sk = sock->sk;
863 struct iucv_sock *iucv = iucv_sk(sk);
864 int err;
865
866 if (addr->sa_family != AF_IUCV || alen < sizeof(struct sockaddr_iucv))
867 return -EINVAL;
868
869 if (sk->sk_state != IUCV_OPEN && sk->sk_state != IUCV_BOUND)
870 return -EBADFD;
871
872 if (sk->sk_state == IUCV_OPEN &&
873 iucv->transport == AF_IUCV_TRANS_HIPER)
874 return -EBADFD; /* explicit bind required */
875
876 if (sk->sk_type != SOCK_STREAM && sk->sk_type != SOCK_SEQPACKET)
877 return -EINVAL;
878
879 if (sk->sk_state == IUCV_OPEN) {
880 err = iucv_sock_autobind(sk);
881 if (unlikely(err))
882 return err;
Jennifer Hunteac37312007-02-08 13:51:54 -0800883 }
884
Ursula Braun3881ac42011-08-08 01:33:54 +0000885 lock_sock(sk);
886
887 /* Set the destination information */
888 memcpy(iucv->dst_user_id, sa->siucv_user_id, 8);
889 memcpy(iucv->dst_name, sa->siucv_name, 8);
890
891 if (iucv->transport == AF_IUCV_TRANS_HIPER)
Ursula Braun9fbd87d2012-03-07 02:06:23 +0000892 err = iucv_send_ctrl(sock->sk, AF_IUCV_FLAG_SYN);
Ursula Braun3881ac42011-08-08 01:33:54 +0000893 else
894 err = afiucv_path_connect(sock, addr);
895 if (err)
896 goto done;
897
898 if (sk->sk_state != IUCV_CONNECTED)
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +0000899 err = iucv_sock_wait(sk, iucv_sock_in_state(sk, IUCV_CONNECTED,
900 IUCV_DISCONN),
901 sock_sndtimeo(sk, flags & O_NONBLOCK));
Jennifer Hunteac37312007-02-08 13:51:54 -0800902
Ursula Braun3881ac42011-08-08 01:33:54 +0000903 if (sk->sk_state == IUCV_DISCONN || sk->sk_state == IUCV_CLOSED)
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000904 err = -ECONNREFUSED;
Ursula Braun18becbc2009-01-05 18:07:46 -0800905
Ursula Braun7d316b92012-02-08 00:19:45 +0000906 if (err && iucv->transport == AF_IUCV_TRANS_IUCV)
907 iucv_sever_path(sk, 0);
Ursula Braun18becbc2009-01-05 18:07:46 -0800908
Jennifer Hunteac37312007-02-08 13:51:54 -0800909done:
910 release_sock(sk);
911 return err;
912}
913
914/* Move a socket into listening state. */
915static int iucv_sock_listen(struct socket *sock, int backlog)
916{
917 struct sock *sk = sock->sk;
918 int err;
919
920 lock_sock(sk);
921
922 err = -EINVAL;
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +0000923 if (sk->sk_state != IUCV_BOUND)
924 goto done;
925
926 if (sock->type != SOCK_STREAM && sock->type != SOCK_SEQPACKET)
Jennifer Hunteac37312007-02-08 13:51:54 -0800927 goto done;
928
929 sk->sk_max_ack_backlog = backlog;
930 sk->sk_ack_backlog = 0;
931 sk->sk_state = IUCV_LISTEN;
932 err = 0;
933
934done:
935 release_sock(sk);
936 return err;
937}
938
939/* Accept a pending connection */
940static int iucv_sock_accept(struct socket *sock, struct socket *newsock,
941 int flags)
942{
943 DECLARE_WAITQUEUE(wait, current);
944 struct sock *sk = sock->sk, *nsk;
945 long timeo;
946 int err = 0;
947
Jennifer Hunt561e0362007-05-04 12:22:07 -0700948 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
Jennifer Hunteac37312007-02-08 13:51:54 -0800949
950 if (sk->sk_state != IUCV_LISTEN) {
951 err = -EBADFD;
952 goto done;
953 }
954
955 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
956
957 /* Wait for an incoming connection */
Eric Dumazetaa395142010-04-20 13:03:51 +0000958 add_wait_queue_exclusive(sk_sleep(sk), &wait);
Heiko Carstensda99f052007-05-04 12:23:27 -0700959 while (!(nsk = iucv_accept_dequeue(sk, newsock))) {
Jennifer Hunteac37312007-02-08 13:51:54 -0800960 set_current_state(TASK_INTERRUPTIBLE);
961 if (!timeo) {
962 err = -EAGAIN;
963 break;
964 }
965
966 release_sock(sk);
967 timeo = schedule_timeout(timeo);
Jennifer Hunt561e0362007-05-04 12:22:07 -0700968 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
Jennifer Hunteac37312007-02-08 13:51:54 -0800969
970 if (sk->sk_state != IUCV_LISTEN) {
971 err = -EBADFD;
972 break;
973 }
974
975 if (signal_pending(current)) {
976 err = sock_intr_errno(timeo);
977 break;
978 }
979 }
980
981 set_current_state(TASK_RUNNING);
Eric Dumazetaa395142010-04-20 13:03:51 +0000982 remove_wait_queue(sk_sleep(sk), &wait);
Jennifer Hunteac37312007-02-08 13:51:54 -0800983
984 if (err)
985 goto done;
986
987 newsock->state = SS_CONNECTED;
988
989done:
990 release_sock(sk);
991 return err;
992}
993
994static int iucv_sock_getname(struct socket *sock, struct sockaddr *addr,
995 int *len, int peer)
996{
997 struct sockaddr_iucv *siucv = (struct sockaddr_iucv *) addr;
998 struct sock *sk = sock->sk;
Ursula Braun493d3972011-08-08 01:33:52 +0000999 struct iucv_sock *iucv = iucv_sk(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -08001000
1001 addr->sa_family = AF_IUCV;
1002 *len = sizeof(struct sockaddr_iucv);
1003
1004 if (peer) {
Ursula Braun493d3972011-08-08 01:33:52 +00001005 memcpy(siucv->siucv_user_id, iucv->dst_user_id, 8);
1006 memcpy(siucv->siucv_name, iucv->dst_name, 8);
Jennifer Hunteac37312007-02-08 13:51:54 -08001007 } else {
Ursula Braun493d3972011-08-08 01:33:52 +00001008 memcpy(siucv->siucv_user_id, iucv->src_user_id, 8);
1009 memcpy(siucv->siucv_name, iucv->src_name, 8);
Jennifer Hunteac37312007-02-08 13:51:54 -08001010 }
1011 memset(&siucv->siucv_port, 0, sizeof(siucv->siucv_port));
1012 memset(&siucv->siucv_addr, 0, sizeof(siucv->siucv_addr));
Ursula Braun493d3972011-08-08 01:33:52 +00001013 memset(&siucv->siucv_nodeid, 0, sizeof(siucv->siucv_nodeid));
Jennifer Hunteac37312007-02-08 13:51:54 -08001014
1015 return 0;
1016}
1017
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001018/**
1019 * iucv_send_iprm() - Send socket data in parameter list of an iucv message.
1020 * @path: IUCV path
1021 * @msg: Pointer to a struct iucv_message
1022 * @skb: The socket data to send, skb->len MUST BE <= 7
1023 *
1024 * Send the socket data in the parameter list in the iucv message
1025 * (IUCV_IPRMDATA). The socket data is stored at index 0 to 6 in the parameter
1026 * list and the socket data len at index 7 (last byte).
1027 * See also iucv_msg_length().
1028 *
1029 * Returns the error code from the iucv_message_send() call.
1030 */
1031static int iucv_send_iprm(struct iucv_path *path, struct iucv_message *msg,
1032 struct sk_buff *skb)
1033{
1034 u8 prmdata[8];
1035
1036 memcpy(prmdata, (void *) skb->data, skb->len);
1037 prmdata[7] = 0xff - (u8) skb->len;
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001038 return pr_iucv->message_send(path, msg, IUCV_IPRMDATA, 0,
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001039 (void *) prmdata, 8);
1040}
1041
Ying Xue1b784142015-03-02 15:37:48 +08001042static int iucv_sock_sendmsg(struct socket *sock, struct msghdr *msg,
1043 size_t len)
Jennifer Hunteac37312007-02-08 13:51:54 -08001044{
1045 struct sock *sk = sock->sk;
1046 struct iucv_sock *iucv = iucv_sk(sk);
Eugene Crossere5374392016-06-13 18:46:14 +02001047 size_t headroom, 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 */
Eugene Crossere5374392016-06-13 18:46:14 +02001125 headroom = (iucv->transport == AF_IUCV_TRANS_HIPER)
1126 ? sizeof(struct af_iucv_trans_hdr) + ETH_HLEN : 0;
1127 if (headroom + len < PAGE_SIZE) {
1128 linear = len;
1129 } else {
1130 /* In nonlinear "classic" iucv skb,
1131 * reserve space for iucv_array
1132 */
1133 if (iucv->transport != AF_IUCV_TRANS_HIPER)
1134 headroom += sizeof(struct iucv_array) *
1135 (MAX_SKB_FRAGS + 1);
1136 linear = PAGE_SIZE - headroom;
1137 }
1138 skb = sock_alloc_send_pskb(sk, headroom + linear, len - linear,
1139 noblock, &err, 0);
Eugene Crossered4ac422015-03-30 15:40:42 +02001140 if (!skb)
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001141 goto out;
Eugene Crossere5374392016-06-13 18:46:14 +02001142 if (headroom)
1143 skb_reserve(skb, headroom);
1144 skb_put(skb, linear);
1145 skb->len = len;
1146 skb->data_len = len - linear;
1147 err = skb_copy_datagram_from_iter(skb, 0, &msg->msg_iter, len);
1148 if (err)
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001149 goto fail;
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001150
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +00001151 /* wait if outstanding messages for iucv path has reached */
1152 timeo = sock_sndtimeo(sk, noblock);
1153 err = iucv_sock_wait(sk, iucv_below_msglim(sk), timeo);
1154 if (err)
1155 goto fail;
1156
1157 /* return -ECONNRESET if the socket is no longer connected */
1158 if (sk->sk_state != IUCV_CONNECTED) {
1159 err = -ECONNRESET;
1160 goto fail;
1161 }
1162
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001163 /* increment and save iucv message tag for msg_completion cbk */
1164 txmsg.tag = iucv->send_tag++;
Ursula Braunf9c41a62013-04-07 22:19:26 +00001165 IUCV_SKB_CB(skb)->tag = txmsg.tag;
Ursula Braun800c5eb2012-02-08 00:19:47 +00001166
Ursula Braun3881ac42011-08-08 01:33:54 +00001167 if (iucv->transport == AF_IUCV_TRANS_HIPER) {
1168 atomic_inc(&iucv->msg_sent);
1169 err = afiucv_hs_send(&txmsg, sk, skb, 0);
1170 if (err) {
1171 atomic_dec(&iucv->msg_sent);
1172 goto fail;
1173 }
Eugene Crossere5374392016-06-13 18:46:14 +02001174 } else { /* Classic VM IUCV transport */
1175 skb_queue_tail(&iucv->send_skb_q, skb);
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001176
Eugene Crossere5374392016-06-13 18:46:14 +02001177 if (((iucv->path->flags & IUCV_IPRMDATA) & iucv->flags) &&
1178 skb->len <= 7) {
1179 err = iucv_send_iprm(iucv->path, &txmsg, skb);
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001180
Eugene Crossere5374392016-06-13 18:46:14 +02001181 /* on success: there is no message_complete callback */
1182 /* for an IPRMDATA msg; remove skb from send queue */
1183 if (err == 0) {
1184 skb_unlink(skb, &iucv->send_skb_q);
1185 kfree_skb(skb);
1186 }
1187
1188 /* this error should never happen since the */
1189 /* IUCV_IPRMDATA path flag is set... sever path */
1190 if (err == 0x15) {
1191 pr_iucv->path_sever(iucv->path, NULL);
1192 skb_unlink(skb, &iucv->send_skb_q);
1193 err = -EPIPE;
1194 goto fail;
1195 }
1196 } else if (skb_is_nonlinear(skb)) {
1197 struct iucv_array *iba = (struct iucv_array *)skb->head;
1198 int i;
1199
1200 /* skip iucv_array lying in the headroom */
1201 iba[0].address = (u32)(addr_t)skb->data;
1202 iba[0].length = (u32)skb_headlen(skb);
1203 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1204 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1205
1206 iba[i + 1].address =
1207 (u32)(addr_t)skb_frag_address(frag);
1208 iba[i + 1].length = (u32)skb_frag_size(frag);
1209 }
1210 err = pr_iucv->message_send(iucv->path, &txmsg,
1211 IUCV_IPBUFLST, 0,
1212 (void *)iba, skb->len);
1213 } else { /* non-IPRM Linear skb */
1214 err = pr_iucv->message_send(iucv->path, &txmsg,
1215 0, 0, (void *)skb->data, skb->len);
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001216 }
Eugene Crossere5374392016-06-13 18:46:14 +02001217 if (err) {
1218 if (err == 3) {
1219 user_id[8] = 0;
1220 memcpy(user_id, iucv->dst_user_id, 8);
1221 appl_id[8] = 0;
1222 memcpy(appl_id, iucv->dst_name, 8);
1223 pr_err(
1224 "Application %s on z/VM guest %s exceeds message limit\n",
1225 appl_id, user_id);
1226 err = -EAGAIN;
1227 } else {
1228 err = -EPIPE;
1229 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001230 skb_unlink(skb, &iucv->send_skb_q);
Jennifer Hunteac37312007-02-08 13:51:54 -08001231 goto fail;
1232 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001233 }
1234
1235 release_sock(sk);
1236 return len;
1237
1238fail:
1239 kfree_skb(skb);
1240out:
1241 release_sock(sk);
1242 return err;
1243}
1244
Eugene Crossera006353a92016-06-13 18:46:16 +02001245static struct sk_buff *alloc_iucv_recv_skb(unsigned long len)
1246{
1247 size_t headroom, linear;
1248 struct sk_buff *skb;
1249 int err;
1250
1251 if (len < PAGE_SIZE) {
1252 headroom = 0;
1253 linear = len;
1254 } else {
1255 headroom = sizeof(struct iucv_array) * (MAX_SKB_FRAGS + 1);
1256 linear = PAGE_SIZE - headroom;
1257 }
1258 skb = alloc_skb_with_frags(headroom + linear, len - linear,
1259 0, &err, GFP_ATOMIC | GFP_DMA);
1260 WARN_ONCE(!skb,
1261 "alloc of recv iucv skb len=%lu failed with errcode=%d\n",
1262 len, err);
1263 if (skb) {
1264 if (headroom)
1265 skb_reserve(skb, headroom);
1266 skb_put(skb, linear);
1267 skb->len = len;
1268 skb->data_len = len - linear;
1269 }
1270 return skb;
1271}
1272
Hendrik Bruecknerbf95d202009-09-16 04:37:28 +00001273/* iucv_process_message() - Receive a single outstanding IUCV message
1274 *
1275 * Locking: must be called with message_q.lock held
1276 */
Ursula Braunf0703c82007-10-08 02:03:31 -07001277static void iucv_process_message(struct sock *sk, struct sk_buff *skb,
1278 struct iucv_path *path,
1279 struct iucv_message *msg)
1280{
1281 int rc;
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001282 unsigned int len;
Ursula Braunf0703c82007-10-08 02:03:31 -07001283
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001284 len = iucv_msg_length(msg);
1285
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001286 /* store msg target class in the second 4 bytes of skb ctrl buffer */
1287 /* Note: the first 4 bytes are reserved for msg tag */
Ursula Braunf9c41a62013-04-07 22:19:26 +00001288 IUCV_SKB_CB(skb)->class = msg->class;
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001289
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001290 /* check for special IPRM messages (e.g. iucv_sock_shutdown) */
1291 if ((msg->flags & IUCV_IPRMDATA) && len > 7) {
1292 if (memcmp(msg->rmmsg, iprm_shutdown, 8) == 0) {
1293 skb->data = NULL;
1294 skb->len = 0;
1295 }
Ursula Braunf0703c82007-10-08 02:03:31 -07001296 } else {
Eugene Crossera006353a92016-06-13 18:46:16 +02001297 if (skb_is_nonlinear(skb)) {
1298 struct iucv_array *iba = (struct iucv_array *)skb->head;
1299 int i;
1300
1301 iba[0].address = (u32)(addr_t)skb->data;
1302 iba[0].length = (u32)skb_headlen(skb);
1303 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1304 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1305
1306 iba[i + 1].address =
1307 (u32)(addr_t)skb_frag_address(frag);
1308 iba[i + 1].length = (u32)skb_frag_size(frag);
1309 }
1310 rc = pr_iucv->message_receive(path, msg,
1311 IUCV_IPBUFLST,
1312 (void *)iba, len, NULL);
1313 } else {
1314 rc = pr_iucv->message_receive(path, msg,
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001315 msg->flags & IUCV_IPRMDATA,
1316 skb->data, len, NULL);
Eugene Crossera006353a92016-06-13 18:46:16 +02001317 }
Ursula Braunf0703c82007-10-08 02:03:31 -07001318 if (rc) {
1319 kfree_skb(skb);
1320 return;
1321 }
Eugene Crossera006353a92016-06-13 18:46:16 +02001322 WARN_ON_ONCE(skb->len != len);
Ursula Braunf0703c82007-10-08 02:03:31 -07001323 }
1324
Ursula Braunf9c41a62013-04-07 22:19:26 +00001325 IUCV_SKB_CB(skb)->offset = 0;
Ursula Braunf0703c82007-10-08 02:03:31 -07001326 if (sock_queue_rcv_skb(sk, skb))
1327 skb_queue_head(&iucv_sk(sk)->backlog_skb_q, skb);
1328}
1329
Hendrik Bruecknerbf95d202009-09-16 04:37:28 +00001330/* iucv_process_message_q() - Process outstanding IUCV messages
1331 *
1332 * Locking: must be called with message_q.lock held
1333 */
Ursula Braunf0703c82007-10-08 02:03:31 -07001334static void iucv_process_message_q(struct sock *sk)
1335{
1336 struct iucv_sock *iucv = iucv_sk(sk);
1337 struct sk_buff *skb;
1338 struct sock_msg_q *p, *n;
1339
1340 list_for_each_entry_safe(p, n, &iucv->message_q.list, list) {
Eugene Crossera006353a92016-06-13 18:46:16 +02001341 skb = alloc_iucv_recv_skb(iucv_msg_length(&p->msg));
Ursula Braunf0703c82007-10-08 02:03:31 -07001342 if (!skb)
1343 break;
1344 iucv_process_message(sk, skb, p->path, &p->msg);
1345 list_del(&p->list);
1346 kfree(p);
1347 if (!skb_queue_empty(&iucv->backlog_skb_q))
1348 break;
1349 }
1350}
1351
Ying Xue1b784142015-03-02 15:37:48 +08001352static int iucv_sock_recvmsg(struct socket *sock, struct msghdr *msg,
1353 size_t len, int flags)
Jennifer Hunteac37312007-02-08 13:51:54 -08001354{
1355 int noblock = flags & MSG_DONTWAIT;
1356 struct sock *sk = sock->sk;
Jennifer Hunt561e0362007-05-04 12:22:07 -07001357 struct iucv_sock *iucv = iucv_sk(sk);
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001358 unsigned int copied, rlen;
Ursula Braun9fbd87d2012-03-07 02:06:23 +00001359 struct sk_buff *skb, *rskb, *cskb;
Jennifer Hunteac37312007-02-08 13:51:54 -08001360 int err = 0;
Ursula Braunf9c41a62013-04-07 22:19:26 +00001361 u32 offset;
Jennifer Hunteac37312007-02-08 13:51:54 -08001362
Ursula Braunaac63992011-12-19 22:56:31 +00001363 if ((sk->sk_state == IUCV_DISCONN) &&
Ursula Braunf0703c82007-10-08 02:03:31 -07001364 skb_queue_empty(&iucv->backlog_skb_q) &&
1365 skb_queue_empty(&sk->sk_receive_queue) &&
1366 list_empty(&iucv->message_q.list))
Jennifer Hunt561e0362007-05-04 12:22:07 -07001367 return 0;
1368
Jennifer Hunteac37312007-02-08 13:51:54 -08001369 if (flags & (MSG_OOB))
1370 return -EOPNOTSUPP;
1371
Hendrik Brueckner60d37052009-04-21 06:04:21 +00001372 /* receive/dequeue next skb:
1373 * the function understands MSG_PEEK and, thus, does not dequeue skb */
Jennifer Hunteac37312007-02-08 13:51:54 -08001374 skb = skb_recv_datagram(sk, flags, noblock, &err);
1375 if (!skb) {
1376 if (sk->sk_shutdown & RCV_SHUTDOWN)
1377 return 0;
1378 return err;
1379 }
1380
Ursula Braunf9c41a62013-04-07 22:19:26 +00001381 offset = IUCV_SKB_CB(skb)->offset;
1382 rlen = skb->len - offset; /* real length of skb */
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001383 copied = min_t(unsigned int, rlen, len);
Ursula Braun82492a32012-03-07 02:06:24 +00001384 if (!rlen)
1385 sk->sk_shutdown = sk->sk_shutdown | RCV_SHUTDOWN;
Jennifer Hunteac37312007-02-08 13:51:54 -08001386
Jennifer Hunt561e0362007-05-04 12:22:07 -07001387 cskb = skb;
David S. Miller51f3d022014-11-05 16:46:40 -05001388 if (skb_copy_datagram_msg(cskb, offset, msg, copied)) {
Hendrik Brueckner802788b2009-04-21 23:26:26 +00001389 if (!(flags & MSG_PEEK))
1390 skb_queue_head(&sk->sk_receive_queue, skb);
1391 return -EFAULT;
Jennifer Hunteac37312007-02-08 13:51:54 -08001392 }
1393
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001394 /* SOCK_SEQPACKET: set MSG_TRUNC if recv buf size is too small */
1395 if (sk->sk_type == SOCK_SEQPACKET) {
1396 if (copied < rlen)
1397 msg->msg_flags |= MSG_TRUNC;
1398 /* each iucv message contains a complete record */
1399 msg->msg_flags |= MSG_EOR;
1400 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001401
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001402 /* create control message to store iucv msg target class:
1403 * get the trgcls from the control buffer of the skb due to
1404 * fragmentation of original iucv message. */
1405 err = put_cmsg(msg, SOL_IUCV, SCM_IUCV_TRGCLS,
Ursula Braunf9c41a62013-04-07 22:19:26 +00001406 sizeof(IUCV_SKB_CB(skb)->class),
1407 (void *)&IUCV_SKB_CB(skb)->class);
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001408 if (err) {
1409 if (!(flags & MSG_PEEK))
1410 skb_queue_head(&sk->sk_receive_queue, skb);
1411 return err;
1412 }
1413
Jennifer Hunteac37312007-02-08 13:51:54 -08001414 /* Mark read part of skb as used */
1415 if (!(flags & MSG_PEEK)) {
Jennifer Hunteac37312007-02-08 13:51:54 -08001416
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001417 /* SOCK_STREAM: re-queue skb if it contains unreceived data */
1418 if (sk->sk_type == SOCK_STREAM) {
Ursula Braunf9c41a62013-04-07 22:19:26 +00001419 if (copied < rlen) {
1420 IUCV_SKB_CB(skb)->offset = offset + copied;
Ursula Braun2f139a52014-03-19 07:57:59 +01001421 skb_queue_head(&sk->sk_receive_queue, skb);
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001422 goto done;
1423 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001424 }
1425
1426 kfree_skb(skb);
Ursula Braun800c5eb2012-02-08 00:19:47 +00001427 if (iucv->transport == AF_IUCV_TRANS_HIPER) {
1428 atomic_inc(&iucv->msg_recv);
1429 if (atomic_read(&iucv->msg_recv) > iucv->msglimit) {
1430 WARN_ON(1);
1431 iucv_sock_close(sk);
1432 return -EFAULT;
1433 }
1434 }
Jennifer Hunt561e0362007-05-04 12:22:07 -07001435
1436 /* Queue backlog skbs */
Hendrik Bruecknerbf95d202009-09-16 04:37:28 +00001437 spin_lock_bh(&iucv->message_q.lock);
Ursula Braunf0703c82007-10-08 02:03:31 -07001438 rskb = skb_dequeue(&iucv->backlog_skb_q);
Heiko Carstensda99f052007-05-04 12:23:27 -07001439 while (rskb) {
Ursula Braunf9c41a62013-04-07 22:19:26 +00001440 IUCV_SKB_CB(rskb)->offset = 0;
Jennifer Hunt561e0362007-05-04 12:22:07 -07001441 if (sock_queue_rcv_skb(sk, rskb)) {
Ursula Braunf0703c82007-10-08 02:03:31 -07001442 skb_queue_head(&iucv->backlog_skb_q,
Jennifer Hunt561e0362007-05-04 12:22:07 -07001443 rskb);
1444 break;
1445 } else {
Ursula Braunf0703c82007-10-08 02:03:31 -07001446 rskb = skb_dequeue(&iucv->backlog_skb_q);
Jennifer Hunt561e0362007-05-04 12:22:07 -07001447 }
1448 }
Ursula Braunf0703c82007-10-08 02:03:31 -07001449 if (skb_queue_empty(&iucv->backlog_skb_q)) {
Ursula Braunf0703c82007-10-08 02:03:31 -07001450 if (!list_empty(&iucv->message_q.list))
1451 iucv_process_message_q(sk);
Ursula Braun3881ac42011-08-08 01:33:54 +00001452 if (atomic_read(&iucv->msg_recv) >=
1453 iucv->msglimit / 2) {
Ursula Braun9fbd87d2012-03-07 02:06:23 +00001454 err = iucv_send_ctrl(sk, AF_IUCV_FLAG_WIN);
Ursula Braun3881ac42011-08-08 01:33:54 +00001455 if (err) {
1456 sk->sk_state = IUCV_DISCONN;
1457 sk->sk_state_change(sk);
1458 }
1459 }
Ursula Braunf0703c82007-10-08 02:03:31 -07001460 }
Hendrik Bruecknerbf95d202009-09-16 04:37:28 +00001461 spin_unlock_bh(&iucv->message_q.lock);
Hendrik Brueckner60d37052009-04-21 06:04:21 +00001462 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001463
1464done:
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001465 /* SOCK_SEQPACKET: return real length if MSG_TRUNC is set */
1466 if (sk->sk_type == SOCK_SEQPACKET && (flags & MSG_TRUNC))
1467 copied = rlen;
1468
1469 return copied;
Jennifer Hunteac37312007-02-08 13:51:54 -08001470}
1471
1472static inline unsigned int iucv_accept_poll(struct sock *parent)
1473{
1474 struct iucv_sock *isk, *n;
1475 struct sock *sk;
1476
Heiko Carstensda99f052007-05-04 12:23:27 -07001477 list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) {
Jennifer Hunteac37312007-02-08 13:51:54 -08001478 sk = (struct sock *) isk;
1479
1480 if (sk->sk_state == IUCV_CONNECTED)
1481 return POLLIN | POLLRDNORM;
1482 }
1483
1484 return 0;
1485}
1486
1487unsigned int iucv_sock_poll(struct file *file, struct socket *sock,
1488 poll_table *wait)
1489{
1490 struct sock *sk = sock->sk;
1491 unsigned int mask = 0;
1492
Eric Dumazetaa395142010-04-20 13:03:51 +00001493 sock_poll_wait(file, sk_sleep(sk), wait);
Jennifer Hunteac37312007-02-08 13:51:54 -08001494
1495 if (sk->sk_state == IUCV_LISTEN)
1496 return iucv_accept_poll(sk);
1497
1498 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
Keller, Jacob E7d4c04f2013-03-28 11:19:25 +00001499 mask |= POLLERR |
Jacob Keller8facd5f2013-04-02 13:55:40 -07001500 (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? POLLPRI : 0);
Jennifer Hunteac37312007-02-08 13:51:54 -08001501
1502 if (sk->sk_shutdown & RCV_SHUTDOWN)
1503 mask |= POLLRDHUP;
1504
1505 if (sk->sk_shutdown == SHUTDOWN_MASK)
1506 mask |= POLLHUP;
1507
1508 if (!skb_queue_empty(&sk->sk_receive_queue) ||
Heiko Carstensda99f052007-05-04 12:23:27 -07001509 (sk->sk_shutdown & RCV_SHUTDOWN))
Jennifer Hunteac37312007-02-08 13:51:54 -08001510 mask |= POLLIN | POLLRDNORM;
1511
1512 if (sk->sk_state == IUCV_CLOSED)
1513 mask |= POLLHUP;
1514
Ursula Braunaac63992011-12-19 22:56:31 +00001515 if (sk->sk_state == IUCV_DISCONN)
Jennifer Hunt561e0362007-05-04 12:22:07 -07001516 mask |= POLLIN;
1517
Ursula Braun7f1b0ea2012-02-08 00:19:46 +00001518 if (sock_writeable(sk) && iucv_below_msglim(sk))
Jennifer Hunteac37312007-02-08 13:51:54 -08001519 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
1520 else
Eric Dumazet9cd3e072015-11-29 20:03:10 -08001521 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
Jennifer Hunteac37312007-02-08 13:51:54 -08001522
1523 return mask;
1524}
1525
1526static int iucv_sock_shutdown(struct socket *sock, int how)
1527{
1528 struct sock *sk = sock->sk;
1529 struct iucv_sock *iucv = iucv_sk(sk);
1530 struct iucv_message txmsg;
1531 int err = 0;
Jennifer Hunteac37312007-02-08 13:51:54 -08001532
1533 how++;
1534
1535 if ((how & ~SHUTDOWN_MASK) || !how)
1536 return -EINVAL;
1537
1538 lock_sock(sk);
Heiko Carstensda99f052007-05-04 12:23:27 -07001539 switch (sk->sk_state) {
Ursula Braun82492a32012-03-07 02:06:24 +00001540 case IUCV_LISTEN:
Hendrik Bruecknere14ad5f2009-04-21 06:04:23 +00001541 case IUCV_DISCONN:
1542 case IUCV_CLOSING:
Jennifer Hunteac37312007-02-08 13:51:54 -08001543 case IUCV_CLOSED:
1544 err = -ENOTCONN;
1545 goto fail;
Jennifer Hunteac37312007-02-08 13:51:54 -08001546 default:
Jennifer Hunteac37312007-02-08 13:51:54 -08001547 break;
1548 }
1549
1550 if (how == SEND_SHUTDOWN || how == SHUTDOWN_MASK) {
Ursula Braun82492a32012-03-07 02:06:24 +00001551 if (iucv->transport == AF_IUCV_TRANS_IUCV) {
1552 txmsg.class = 0;
1553 txmsg.tag = 0;
1554 err = pr_iucv->message_send(iucv->path, &txmsg,
1555 IUCV_IPRMDATA, 0, (void *) iprm_shutdown, 8);
1556 if (err) {
1557 switch (err) {
1558 case 1:
1559 err = -ENOTCONN;
1560 break;
1561 case 2:
1562 err = -ECONNRESET;
1563 break;
1564 default:
1565 err = -ENOTCONN;
1566 break;
1567 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001568 }
Ursula Braun82492a32012-03-07 02:06:24 +00001569 } else
1570 iucv_send_ctrl(sk, AF_IUCV_FLAG_SHT);
Jennifer Hunteac37312007-02-08 13:51:54 -08001571 }
1572
Ursula Braun82492a32012-03-07 02:06:24 +00001573 sk->sk_shutdown |= how;
Jennifer Hunteac37312007-02-08 13:51:54 -08001574 if (how == RCV_SHUTDOWN || how == SHUTDOWN_MASK) {
Ursula Braun1042cab2014-07-21 12:54:44 +02001575 if ((iucv->transport == AF_IUCV_TRANS_IUCV) &&
1576 iucv->path) {
Ursula Braun82492a32012-03-07 02:06:24 +00001577 err = pr_iucv->path_quiesce(iucv->path, NULL);
1578 if (err)
1579 err = -ENOTCONN;
1580/* skb_queue_purge(&sk->sk_receive_queue); */
1581 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001582 skb_queue_purge(&sk->sk_receive_queue);
1583 }
1584
1585 /* Wake up anyone sleeping in poll */
1586 sk->sk_state_change(sk);
1587
1588fail:
1589 release_sock(sk);
1590 return err;
1591}
1592
1593static int iucv_sock_release(struct socket *sock)
1594{
1595 struct sock *sk = sock->sk;
1596 int err = 0;
1597
1598 if (!sk)
1599 return 0;
1600
1601 iucv_sock_close(sk);
1602
Jennifer Hunteac37312007-02-08 13:51:54 -08001603 sock_orphan(sk);
1604 iucv_sock_kill(sk);
1605 return err;
1606}
1607
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00001608/* getsockopt and setsockopt */
1609static int iucv_sock_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07001610 char __user *optval, unsigned int optlen)
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00001611{
1612 struct sock *sk = sock->sk;
1613 struct iucv_sock *iucv = iucv_sk(sk);
1614 int val;
1615 int rc;
1616
1617 if (level != SOL_IUCV)
1618 return -ENOPROTOOPT;
1619
1620 if (optlen < sizeof(int))
1621 return -EINVAL;
1622
1623 if (get_user(val, (int __user *) optval))
1624 return -EFAULT;
1625
1626 rc = 0;
1627
1628 lock_sock(sk);
1629 switch (optname) {
1630 case SO_IPRMDATA_MSG:
1631 if (val)
1632 iucv->flags |= IUCV_IPRMDATA;
1633 else
1634 iucv->flags &= ~IUCV_IPRMDATA;
1635 break;
Hendrik Brueckner09488e22009-04-21 23:26:27 +00001636 case SO_MSGLIMIT:
1637 switch (sk->sk_state) {
1638 case IUCV_OPEN:
1639 case IUCV_BOUND:
1640 if (val < 1 || val > (u16)(~0))
1641 rc = -EINVAL;
1642 else
1643 iucv->msglimit = val;
1644 break;
1645 default:
1646 rc = -EINVAL;
1647 break;
1648 }
1649 break;
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00001650 default:
1651 rc = -ENOPROTOOPT;
1652 break;
1653 }
1654 release_sock(sk);
1655
1656 return rc;
1657}
1658
1659static int iucv_sock_getsockopt(struct socket *sock, int level, int optname,
1660 char __user *optval, int __user *optlen)
1661{
1662 struct sock *sk = sock->sk;
1663 struct iucv_sock *iucv = iucv_sk(sk);
Ursula Braun51363b82012-02-08 00:19:48 +00001664 unsigned int val;
1665 int len;
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00001666
1667 if (level != SOL_IUCV)
1668 return -ENOPROTOOPT;
1669
1670 if (get_user(len, optlen))
1671 return -EFAULT;
1672
1673 if (len < 0)
1674 return -EINVAL;
1675
1676 len = min_t(unsigned int, len, sizeof(int));
1677
1678 switch (optname) {
1679 case SO_IPRMDATA_MSG:
1680 val = (iucv->flags & IUCV_IPRMDATA) ? 1 : 0;
1681 break;
Hendrik Brueckner09488e22009-04-21 23:26:27 +00001682 case SO_MSGLIMIT:
1683 lock_sock(sk);
1684 val = (iucv->path != NULL) ? iucv->path->msglim /* connected */
1685 : iucv->msglimit; /* default */
1686 release_sock(sk);
1687 break;
Ursula Braun51363b82012-02-08 00:19:48 +00001688 case SO_MSGSIZE:
1689 if (sk->sk_state == IUCV_OPEN)
1690 return -EBADFD;
1691 val = (iucv->hs_dev) ? iucv->hs_dev->mtu -
1692 sizeof(struct af_iucv_trans_hdr) - ETH_HLEN :
1693 0x7fffffff;
1694 break;
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00001695 default:
1696 return -ENOPROTOOPT;
1697 }
1698
1699 if (put_user(len, optlen))
1700 return -EFAULT;
1701 if (copy_to_user(optval, &val, len))
1702 return -EFAULT;
1703
1704 return 0;
1705}
1706
1707
Jennifer Hunteac37312007-02-08 13:51:54 -08001708/* Callback wrappers - called from iucv base support */
1709static int iucv_callback_connreq(struct iucv_path *path,
1710 u8 ipvmid[8], u8 ipuser[16])
1711{
1712 unsigned char user_data[16];
1713 unsigned char nuser_data[16];
1714 unsigned char src_name[8];
Jennifer Hunteac37312007-02-08 13:51:54 -08001715 struct sock *sk, *nsk;
1716 struct iucv_sock *iucv, *niucv;
1717 int err;
1718
1719 memcpy(src_name, ipuser, 8);
1720 EBCASC(src_name, 8);
1721 /* Find out if this path belongs to af_iucv. */
1722 read_lock(&iucv_sk_list.lock);
1723 iucv = NULL;
Ursula Braunfebca282007-07-14 19:04:25 -07001724 sk = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001725 sk_for_each(sk, &iucv_sk_list.head)
Jennifer Hunteac37312007-02-08 13:51:54 -08001726 if (sk->sk_state == IUCV_LISTEN &&
1727 !memcmp(&iucv_sk(sk)->src_name, src_name, 8)) {
1728 /*
1729 * Found a listening socket with
1730 * src_name == ipuser[0-7].
1731 */
1732 iucv = iucv_sk(sk);
1733 break;
1734 }
1735 read_unlock(&iucv_sk_list.lock);
1736 if (!iucv)
1737 /* No socket found, not one of our paths. */
1738 return -EINVAL;
1739
1740 bh_lock_sock(sk);
1741
1742 /* Check if parent socket is listening */
1743 low_nmcpy(user_data, iucv->src_name);
1744 high_nmcpy(user_data, iucv->dst_name);
1745 ASCEBC(user_data, sizeof(user_data));
1746 if (sk->sk_state != IUCV_LISTEN) {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001747 err = pr_iucv->path_sever(path, user_data);
Hendrik Brueckner65dbd7c2009-01-05 18:08:23 -08001748 iucv_path_free(path);
Jennifer Hunteac37312007-02-08 13:51:54 -08001749 goto fail;
1750 }
1751
1752 /* Check for backlog size */
1753 if (sk_acceptq_is_full(sk)) {
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 /* Create the new socket */
Eric W. Biederman11aa9c22015-05-08 21:09:13 -05001760 nsk = iucv_sock_alloc(NULL, sk->sk_type, GFP_ATOMIC, 0);
Heiko Carstensda99f052007-05-04 12:23:27 -07001761 if (!nsk) {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001762 err = pr_iucv->path_sever(path, user_data);
Hendrik Brueckner65dbd7c2009-01-05 18:08:23 -08001763 iucv_path_free(path);
Jennifer Hunteac37312007-02-08 13:51:54 -08001764 goto fail;
1765 }
1766
1767 niucv = iucv_sk(nsk);
1768 iucv_sock_init(nsk, sk);
1769
1770 /* Set the new iucv_sock */
1771 memcpy(niucv->dst_name, ipuser + 8, 8);
1772 EBCASC(niucv->dst_name, 8);
1773 memcpy(niucv->dst_user_id, ipvmid, 8);
1774 memcpy(niucv->src_name, iucv->src_name, 8);
1775 memcpy(niucv->src_user_id, iucv->src_user_id, 8);
1776 niucv->path = path;
1777
1778 /* Call iucv_accept */
1779 high_nmcpy(nuser_data, ipuser + 8);
1780 memcpy(nuser_data + 8, niucv->src_name, 8);
1781 ASCEBC(nuser_data + 8, 8);
1782
Hendrik Brueckner09488e22009-04-21 23:26:27 +00001783 /* set message limit for path based on msglimit of accepting socket */
1784 niucv->msglimit = iucv->msglimit;
1785 path->msglim = iucv->msglimit;
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001786 err = pr_iucv->path_accept(path, &af_iucv_handler, nuser_data, nsk);
Heiko Carstensda99f052007-05-04 12:23:27 -07001787 if (err) {
Ursula Braun7d316b92012-02-08 00:19:45 +00001788 iucv_sever_path(nsk, 1);
Hendrik Brueckner65dbd7c2009-01-05 18:08:23 -08001789 iucv_sock_kill(nsk);
Jennifer Hunteac37312007-02-08 13:51:54 -08001790 goto fail;
1791 }
1792
1793 iucv_accept_enqueue(sk, nsk);
1794
1795 /* Wake up accept */
1796 nsk->sk_state = IUCV_CONNECTED;
David S. Miller676d2362014-04-11 16:15:36 -04001797 sk->sk_data_ready(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -08001798 err = 0;
1799fail:
1800 bh_unlock_sock(sk);
1801 return 0;
1802}
1803
1804static void iucv_callback_connack(struct iucv_path *path, u8 ipuser[16])
1805{
1806 struct sock *sk = path->private;
1807
1808 sk->sk_state = IUCV_CONNECTED;
1809 sk->sk_state_change(sk);
1810}
1811
1812static void iucv_callback_rx(struct iucv_path *path, struct iucv_message *msg)
1813{
1814 struct sock *sk = path->private;
Jennifer Hunt561e0362007-05-04 12:22:07 -07001815 struct iucv_sock *iucv = iucv_sk(sk);
Ursula Braunf0703c82007-10-08 02:03:31 -07001816 struct sk_buff *skb;
1817 struct sock_msg_q *save_msg;
1818 int len;
Jennifer Hunt561e0362007-05-04 12:22:07 -07001819
Hendrik Bruecknerfe86e542009-04-21 06:04:22 +00001820 if (sk->sk_shutdown & RCV_SHUTDOWN) {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001821 pr_iucv->message_reject(path, msg);
Jennifer Hunteac37312007-02-08 13:51:54 -08001822 return;
Hendrik Bruecknerfe86e542009-04-21 06:04:22 +00001823 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001824
Hendrik Brueckner3fa6b5a2009-04-21 06:04:24 +00001825 spin_lock(&iucv->message_q.lock);
Jennifer Hunteac37312007-02-08 13:51:54 -08001826
Ursula Braunf0703c82007-10-08 02:03:31 -07001827 if (!list_empty(&iucv->message_q.list) ||
1828 !skb_queue_empty(&iucv->backlog_skb_q))
1829 goto save_message;
1830
1831 len = atomic_read(&sk->sk_rmem_alloc);
Eric Dumazet87fb4b72011-10-13 07:28:54 +00001832 len += SKB_TRUESIZE(iucv_msg_length(msg));
Ursula Braunf0703c82007-10-08 02:03:31 -07001833 if (len > sk->sk_rcvbuf)
1834 goto save_message;
1835
Eugene Crossera006353a92016-06-13 18:46:16 +02001836 skb = alloc_iucv_recv_skb(iucv_msg_length(msg));
Ursula Braunf0703c82007-10-08 02:03:31 -07001837 if (!skb)
1838 goto save_message;
Jennifer Hunteac37312007-02-08 13:51:54 -08001839
Ursula Braunf0703c82007-10-08 02:03:31 -07001840 iucv_process_message(sk, skb, path, msg);
Hendrik Brueckner3fa6b5a2009-04-21 06:04:24 +00001841 goto out_unlock;
Jennifer Hunteac37312007-02-08 13:51:54 -08001842
Ursula Braunf0703c82007-10-08 02:03:31 -07001843save_message:
1844 save_msg = kzalloc(sizeof(struct sock_msg_q), GFP_ATOMIC | GFP_DMA);
Ursula Braund4444722008-02-07 18:07:19 -08001845 if (!save_msg)
Julia Lawalla56635a2010-05-26 05:56:48 +00001846 goto out_unlock;
Ursula Braunf0703c82007-10-08 02:03:31 -07001847 save_msg->path = path;
1848 save_msg->msg = *msg;
1849
Ursula Braunf0703c82007-10-08 02:03:31 -07001850 list_add_tail(&save_msg->list, &iucv->message_q.list);
Hendrik Brueckner3fa6b5a2009-04-21 06:04:24 +00001851
1852out_unlock:
Ursula Braunf0703c82007-10-08 02:03:31 -07001853 spin_unlock(&iucv->message_q.lock);
Jennifer Hunteac37312007-02-08 13:51:54 -08001854}
1855
1856static void iucv_callback_txdone(struct iucv_path *path,
1857 struct iucv_message *msg)
1858{
1859 struct sock *sk = path->private;
Ursula Braunf2a77992008-02-07 18:07:44 -08001860 struct sk_buff *this = NULL;
Jennifer Hunteac37312007-02-08 13:51:54 -08001861 struct sk_buff_head *list = &iucv_sk(sk)->send_skb_q;
1862 struct sk_buff *list_skb = list->next;
1863 unsigned long flags;
1864
Ursula Braun7d316b92012-02-08 00:19:45 +00001865 bh_lock_sock(sk);
Ursula Braunf2a77992008-02-07 18:07:44 -08001866 if (!skb_queue_empty(list)) {
Jennifer Hunt561e0362007-05-04 12:22:07 -07001867 spin_lock_irqsave(&list->lock, flags);
Jennifer Hunteac37312007-02-08 13:51:54 -08001868
Ursula Braunf2a77992008-02-07 18:07:44 -08001869 while (list_skb != (struct sk_buff *)list) {
Ursula Braunf5738e22014-05-13 14:38:02 +02001870 if (msg->tag == IUCV_SKB_CB(list_skb)->tag) {
Ursula Braunf2a77992008-02-07 18:07:44 -08001871 this = list_skb;
1872 break;
1873 }
Jennifer Hunt561e0362007-05-04 12:22:07 -07001874 list_skb = list_skb->next;
Ursula Braunf2a77992008-02-07 18:07:44 -08001875 }
1876 if (this)
1877 __skb_unlink(this, list);
Jennifer Hunteac37312007-02-08 13:51:54 -08001878
Jennifer Hunt561e0362007-05-04 12:22:07 -07001879 spin_unlock_irqrestore(&list->lock, flags);
Jennifer Hunteac37312007-02-08 13:51:54 -08001880
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +00001881 if (this) {
1882 kfree_skb(this);
1883 /* wake up any process waiting for sending */
1884 iucv_sock_wake_msglim(sk);
1885 }
Jennifer Hunt561e0362007-05-04 12:22:07 -07001886 }
1887
Heiko Carstensda99f052007-05-04 12:23:27 -07001888 if (sk->sk_state == IUCV_CLOSING) {
Jennifer Hunt561e0362007-05-04 12:22:07 -07001889 if (skb_queue_empty(&iucv_sk(sk)->send_skb_q)) {
1890 sk->sk_state = IUCV_CLOSED;
1891 sk->sk_state_change(sk);
1892 }
1893 }
Ursula Braun7d316b92012-02-08 00:19:45 +00001894 bh_unlock_sock(sk);
Jennifer Hunt561e0362007-05-04 12:22:07 -07001895
Jennifer Hunteac37312007-02-08 13:51:54 -08001896}
1897
1898static void iucv_callback_connrej(struct iucv_path *path, u8 ipuser[16])
1899{
1900 struct sock *sk = path->private;
1901
Ursula Braun7d316b92012-02-08 00:19:45 +00001902 if (sk->sk_state == IUCV_CLOSED)
1903 return;
1904
1905 bh_lock_sock(sk);
1906 iucv_sever_path(sk, 1);
Ursula Braunaac63992011-12-19 22:56:31 +00001907 sk->sk_state = IUCV_DISCONN;
Jennifer Hunteac37312007-02-08 13:51:54 -08001908
1909 sk->sk_state_change(sk);
Ursula Braun7d316b92012-02-08 00:19:45 +00001910 bh_unlock_sock(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -08001911}
1912
Hendrik Brueckneraf88b522009-04-21 23:26:21 +00001913/* called if the other communication side shuts down its RECV direction;
1914 * in turn, the callback sets SEND_SHUTDOWN to disable sending of data.
1915 */
1916static void iucv_callback_shutdown(struct iucv_path *path, u8 ipuser[16])
1917{
1918 struct sock *sk = path->private;
1919
1920 bh_lock_sock(sk);
1921 if (sk->sk_state != IUCV_CLOSED) {
1922 sk->sk_shutdown |= SEND_SHUTDOWN;
1923 sk->sk_state_change(sk);
1924 }
1925 bh_unlock_sock(sk);
1926}
1927
Ursula Braun3881ac42011-08-08 01:33:54 +00001928/***************** HiperSockets transport callbacks ********************/
1929static void afiucv_swap_src_dest(struct sk_buff *skb)
1930{
1931 struct af_iucv_trans_hdr *trans_hdr =
1932 (struct af_iucv_trans_hdr *)skb->data;
1933 char tmpID[8];
1934 char tmpName[8];
1935
1936 ASCEBC(trans_hdr->destUserID, sizeof(trans_hdr->destUserID));
1937 ASCEBC(trans_hdr->destAppName, sizeof(trans_hdr->destAppName));
1938 ASCEBC(trans_hdr->srcUserID, sizeof(trans_hdr->srcUserID));
1939 ASCEBC(trans_hdr->srcAppName, sizeof(trans_hdr->srcAppName));
1940 memcpy(tmpID, trans_hdr->srcUserID, 8);
1941 memcpy(tmpName, trans_hdr->srcAppName, 8);
1942 memcpy(trans_hdr->srcUserID, trans_hdr->destUserID, 8);
1943 memcpy(trans_hdr->srcAppName, trans_hdr->destAppName, 8);
1944 memcpy(trans_hdr->destUserID, tmpID, 8);
1945 memcpy(trans_hdr->destAppName, tmpName, 8);
1946 skb_push(skb, ETH_HLEN);
1947 memset(skb->data, 0, ETH_HLEN);
1948}
1949
1950/**
1951 * afiucv_hs_callback_syn - react on received SYN
1952 **/
1953static int afiucv_hs_callback_syn(struct sock *sk, struct sk_buff *skb)
1954{
1955 struct sock *nsk;
1956 struct iucv_sock *iucv, *niucv;
1957 struct af_iucv_trans_hdr *trans_hdr;
1958 int err;
1959
1960 iucv = iucv_sk(sk);
1961 trans_hdr = (struct af_iucv_trans_hdr *)skb->data;
1962 if (!iucv) {
1963 /* no sock - connection refused */
1964 afiucv_swap_src_dest(skb);
1965 trans_hdr->flags = AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN;
1966 err = dev_queue_xmit(skb);
1967 goto out;
1968 }
1969
Eric W. Biederman11aa9c22015-05-08 21:09:13 -05001970 nsk = iucv_sock_alloc(NULL, sk->sk_type, GFP_ATOMIC, 0);
Ursula Braun3881ac42011-08-08 01:33:54 +00001971 bh_lock_sock(sk);
1972 if ((sk->sk_state != IUCV_LISTEN) ||
1973 sk_acceptq_is_full(sk) ||
1974 !nsk) {
1975 /* error on server socket - connection refused */
Ursula Braun3881ac42011-08-08 01:33:54 +00001976 afiucv_swap_src_dest(skb);
1977 trans_hdr->flags = AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN;
1978 err = dev_queue_xmit(skb);
Ursula Braun4d520f62014-05-28 10:22:29 +02001979 iucv_sock_kill(nsk);
Ursula Braun3881ac42011-08-08 01:33:54 +00001980 bh_unlock_sock(sk);
1981 goto out;
1982 }
1983
1984 niucv = iucv_sk(nsk);
1985 iucv_sock_init(nsk, sk);
1986 niucv->transport = AF_IUCV_TRANS_HIPER;
1987 niucv->msglimit = iucv->msglimit;
1988 if (!trans_hdr->window)
1989 niucv->msglimit_peer = IUCV_HIPER_MSGLIM_DEFAULT;
1990 else
1991 niucv->msglimit_peer = trans_hdr->window;
1992 memcpy(niucv->dst_name, trans_hdr->srcAppName, 8);
1993 memcpy(niucv->dst_user_id, trans_hdr->srcUserID, 8);
1994 memcpy(niucv->src_name, iucv->src_name, 8);
1995 memcpy(niucv->src_user_id, iucv->src_user_id, 8);
1996 nsk->sk_bound_dev_if = sk->sk_bound_dev_if;
Ursula Braun800c5eb2012-02-08 00:19:47 +00001997 niucv->hs_dev = iucv->hs_dev;
1998 dev_hold(niucv->hs_dev);
Ursula Braun3881ac42011-08-08 01:33:54 +00001999 afiucv_swap_src_dest(skb);
2000 trans_hdr->flags = AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_ACK;
2001 trans_hdr->window = niucv->msglimit;
2002 /* if receiver acks the xmit connection is established */
2003 err = dev_queue_xmit(skb);
2004 if (!err) {
2005 iucv_accept_enqueue(sk, nsk);
2006 nsk->sk_state = IUCV_CONNECTED;
David S. Miller676d2362014-04-11 16:15:36 -04002007 sk->sk_data_ready(sk);
Ursula Braun3881ac42011-08-08 01:33:54 +00002008 } else
2009 iucv_sock_kill(nsk);
2010 bh_unlock_sock(sk);
2011
2012out:
2013 return NET_RX_SUCCESS;
2014}
2015
2016/**
2017 * afiucv_hs_callback_synack() - react on received SYN-ACK
2018 **/
2019static int afiucv_hs_callback_synack(struct sock *sk, struct sk_buff *skb)
2020{
2021 struct iucv_sock *iucv = iucv_sk(sk);
2022 struct af_iucv_trans_hdr *trans_hdr =
2023 (struct af_iucv_trans_hdr *)skb->data;
2024
2025 if (!iucv)
2026 goto out;
2027 if (sk->sk_state != IUCV_BOUND)
2028 goto out;
2029 bh_lock_sock(sk);
2030 iucv->msglimit_peer = trans_hdr->window;
2031 sk->sk_state = IUCV_CONNECTED;
2032 sk->sk_state_change(sk);
2033 bh_unlock_sock(sk);
2034out:
2035 kfree_skb(skb);
2036 return NET_RX_SUCCESS;
2037}
2038
2039/**
2040 * afiucv_hs_callback_synfin() - react on received SYN_FIN
2041 **/
2042static int afiucv_hs_callback_synfin(struct sock *sk, struct sk_buff *skb)
2043{
2044 struct iucv_sock *iucv = iucv_sk(sk);
2045
2046 if (!iucv)
2047 goto out;
2048 if (sk->sk_state != IUCV_BOUND)
2049 goto out;
2050 bh_lock_sock(sk);
2051 sk->sk_state = IUCV_DISCONN;
2052 sk->sk_state_change(sk);
2053 bh_unlock_sock(sk);
2054out:
2055 kfree_skb(skb);
2056 return NET_RX_SUCCESS;
2057}
2058
2059/**
2060 * afiucv_hs_callback_fin() - react on received FIN
2061 **/
2062static int afiucv_hs_callback_fin(struct sock *sk, struct sk_buff *skb)
2063{
2064 struct iucv_sock *iucv = iucv_sk(sk);
2065
2066 /* other end of connection closed */
Ursula Braun800c5eb2012-02-08 00:19:47 +00002067 if (!iucv)
2068 goto out;
2069 bh_lock_sock(sk);
2070 if (sk->sk_state == IUCV_CONNECTED) {
Ursula Braunaac63992011-12-19 22:56:31 +00002071 sk->sk_state = IUCV_DISCONN;
Ursula Braun3881ac42011-08-08 01:33:54 +00002072 sk->sk_state_change(sk);
Ursula Braun3881ac42011-08-08 01:33:54 +00002073 }
Ursula Braun800c5eb2012-02-08 00:19:47 +00002074 bh_unlock_sock(sk);
2075out:
Ursula Braun3881ac42011-08-08 01:33:54 +00002076 kfree_skb(skb);
2077 return NET_RX_SUCCESS;
2078}
2079
2080/**
2081 * afiucv_hs_callback_win() - react on received WIN
2082 **/
2083static int afiucv_hs_callback_win(struct sock *sk, struct sk_buff *skb)
2084{
2085 struct iucv_sock *iucv = iucv_sk(sk);
2086 struct af_iucv_trans_hdr *trans_hdr =
2087 (struct af_iucv_trans_hdr *)skb->data;
2088
2089 if (!iucv)
2090 return NET_RX_SUCCESS;
2091
2092 if (sk->sk_state != IUCV_CONNECTED)
2093 return NET_RX_SUCCESS;
2094
2095 atomic_sub(trans_hdr->window, &iucv->msg_sent);
2096 iucv_sock_wake_msglim(sk);
2097 return NET_RX_SUCCESS;
2098}
2099
2100/**
2101 * afiucv_hs_callback_rx() - react on received data
2102 **/
2103static int afiucv_hs_callback_rx(struct sock *sk, struct sk_buff *skb)
2104{
2105 struct iucv_sock *iucv = iucv_sk(sk);
2106
2107 if (!iucv) {
2108 kfree_skb(skb);
2109 return NET_RX_SUCCESS;
2110 }
2111
2112 if (sk->sk_state != IUCV_CONNECTED) {
2113 kfree_skb(skb);
2114 return NET_RX_SUCCESS;
2115 }
2116
Ursula Braun82492a32012-03-07 02:06:24 +00002117 if (sk->sk_shutdown & RCV_SHUTDOWN) {
2118 kfree_skb(skb);
2119 return NET_RX_SUCCESS;
2120 }
2121
Eugene Crosser979f66b2015-12-11 12:27:51 +01002122 /* write stuff from iucv_msg to skb cb */
Ursula Braun3881ac42011-08-08 01:33:54 +00002123 skb_pull(skb, sizeof(struct af_iucv_trans_hdr));
2124 skb_reset_transport_header(skb);
2125 skb_reset_network_header(skb);
Ursula Braunf9c41a62013-04-07 22:19:26 +00002126 IUCV_SKB_CB(skb)->offset = 0;
Ursula Braun3881ac42011-08-08 01:33:54 +00002127 spin_lock(&iucv->message_q.lock);
2128 if (skb_queue_empty(&iucv->backlog_skb_q)) {
2129 if (sock_queue_rcv_skb(sk, skb)) {
2130 /* handle rcv queue full */
2131 skb_queue_tail(&iucv->backlog_skb_q, skb);
2132 }
2133 } else
2134 skb_queue_tail(&iucv_sk(sk)->backlog_skb_q, skb);
2135 spin_unlock(&iucv->message_q.lock);
2136 return NET_RX_SUCCESS;
2137}
2138
2139/**
2140 * afiucv_hs_rcv() - base function for arriving data through HiperSockets
2141 * transport
2142 * called from netif RX softirq
2143 **/
2144static int afiucv_hs_rcv(struct sk_buff *skb, struct net_device *dev,
2145 struct packet_type *pt, struct net_device *orig_dev)
2146{
Ursula Braun3881ac42011-08-08 01:33:54 +00002147 struct sock *sk;
2148 struct iucv_sock *iucv;
2149 struct af_iucv_trans_hdr *trans_hdr;
2150 char nullstring[8];
2151 int err = 0;
2152
Eugene Crosser979f66b2015-12-11 12:27:51 +01002153 if (skb->len < (ETH_HLEN + sizeof(struct af_iucv_trans_hdr))) {
2154 WARN_ONCE(1, "AF_IUCV too short skb, len=%d, min=%d",
2155 (int)skb->len,
2156 (int)(ETH_HLEN + sizeof(struct af_iucv_trans_hdr)));
2157 kfree_skb(skb);
2158 return NET_RX_SUCCESS;
2159 }
2160 if (skb_headlen(skb) < (ETH_HLEN + sizeof(struct af_iucv_trans_hdr)))
2161 if (skb_linearize(skb)) {
2162 WARN_ONCE(1, "AF_IUCV skb_linearize failed, len=%d",
2163 (int)skb->len);
2164 kfree_skb(skb);
2165 return NET_RX_SUCCESS;
2166 }
Ursula Braun3881ac42011-08-08 01:33:54 +00002167 skb_pull(skb, ETH_HLEN);
2168 trans_hdr = (struct af_iucv_trans_hdr *)skb->data;
2169 EBCASC(trans_hdr->destAppName, sizeof(trans_hdr->destAppName));
2170 EBCASC(trans_hdr->destUserID, sizeof(trans_hdr->destUserID));
2171 EBCASC(trans_hdr->srcAppName, sizeof(trans_hdr->srcAppName));
2172 EBCASC(trans_hdr->srcUserID, sizeof(trans_hdr->srcUserID));
2173 memset(nullstring, 0, sizeof(nullstring));
2174 iucv = NULL;
2175 sk = NULL;
2176 read_lock(&iucv_sk_list.lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08002177 sk_for_each(sk, &iucv_sk_list.head) {
Ursula Braun3881ac42011-08-08 01:33:54 +00002178 if (trans_hdr->flags == AF_IUCV_FLAG_SYN) {
2179 if ((!memcmp(&iucv_sk(sk)->src_name,
2180 trans_hdr->destAppName, 8)) &&
2181 (!memcmp(&iucv_sk(sk)->src_user_id,
2182 trans_hdr->destUserID, 8)) &&
2183 (!memcmp(&iucv_sk(sk)->dst_name, nullstring, 8)) &&
2184 (!memcmp(&iucv_sk(sk)->dst_user_id,
2185 nullstring, 8))) {
2186 iucv = iucv_sk(sk);
2187 break;
2188 }
2189 } else {
2190 if ((!memcmp(&iucv_sk(sk)->src_name,
2191 trans_hdr->destAppName, 8)) &&
2192 (!memcmp(&iucv_sk(sk)->src_user_id,
2193 trans_hdr->destUserID, 8)) &&
2194 (!memcmp(&iucv_sk(sk)->dst_name,
2195 trans_hdr->srcAppName, 8)) &&
2196 (!memcmp(&iucv_sk(sk)->dst_user_id,
2197 trans_hdr->srcUserID, 8))) {
2198 iucv = iucv_sk(sk);
2199 break;
2200 }
2201 }
2202 }
2203 read_unlock(&iucv_sk_list.lock);
2204 if (!iucv)
2205 sk = NULL;
2206
2207 /* no sock
2208 how should we send with no sock
2209 1) send without sock no send rc checking?
2210 2) introduce default sock to handle this cases
2211
2212 SYN -> send SYN|ACK in good case, send SYN|FIN in bad case
2213 data -> send FIN
2214 SYN|ACK, SYN|FIN, FIN -> no action? */
2215
2216 switch (trans_hdr->flags) {
2217 case AF_IUCV_FLAG_SYN:
2218 /* connect request */
2219 err = afiucv_hs_callback_syn(sk, skb);
2220 break;
2221 case (AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_ACK):
2222 /* connect request confirmed */
2223 err = afiucv_hs_callback_synack(sk, skb);
2224 break;
2225 case (AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN):
2226 /* connect request refused */
2227 err = afiucv_hs_callback_synfin(sk, skb);
2228 break;
2229 case (AF_IUCV_FLAG_FIN):
2230 /* close request */
2231 err = afiucv_hs_callback_fin(sk, skb);
2232 break;
2233 case (AF_IUCV_FLAG_WIN):
2234 err = afiucv_hs_callback_win(sk, skb);
Ursula Braun800c5eb2012-02-08 00:19:47 +00002235 if (skb->len == sizeof(struct af_iucv_trans_hdr)) {
2236 kfree_skb(skb);
2237 break;
2238 }
Ursula Braun82492a32012-03-07 02:06:24 +00002239 /* fall through and receive non-zero length data */
2240 case (AF_IUCV_FLAG_SHT):
2241 /* shutdown request */
2242 /* fall through and receive zero length data */
Ursula Braun3881ac42011-08-08 01:33:54 +00002243 case 0:
2244 /* plain data frame */
Ursula Braunf9c41a62013-04-07 22:19:26 +00002245 IUCV_SKB_CB(skb)->class = trans_hdr->iucv_hdr.class;
Ursula Braun3881ac42011-08-08 01:33:54 +00002246 err = afiucv_hs_callback_rx(sk, skb);
2247 break;
2248 default:
2249 ;
2250 }
2251
2252 return err;
2253}
2254
2255/**
2256 * afiucv_hs_callback_txnotify() - handle send notifcations from HiperSockets
2257 * transport
2258 **/
2259static void afiucv_hs_callback_txnotify(struct sk_buff *skb,
2260 enum iucv_tx_notify n)
2261{
2262 struct sock *isk = skb->sk;
2263 struct sock *sk = NULL;
2264 struct iucv_sock *iucv = NULL;
2265 struct sk_buff_head *list;
2266 struct sk_buff *list_skb;
Ursula Braun800c5eb2012-02-08 00:19:47 +00002267 struct sk_buff *nskb;
Ursula Braun3881ac42011-08-08 01:33:54 +00002268 unsigned long flags;
Ursula Braun3881ac42011-08-08 01:33:54 +00002269
Ursula Braun800c5eb2012-02-08 00:19:47 +00002270 read_lock_irqsave(&iucv_sk_list.lock, flags);
Sasha Levinb67bfe02013-02-27 17:06:00 -08002271 sk_for_each(sk, &iucv_sk_list.head)
Ursula Braun3881ac42011-08-08 01:33:54 +00002272 if (sk == isk) {
2273 iucv = iucv_sk(sk);
2274 break;
2275 }
Ursula Braun800c5eb2012-02-08 00:19:47 +00002276 read_unlock_irqrestore(&iucv_sk_list.lock, flags);
Ursula Braun3881ac42011-08-08 01:33:54 +00002277
Ursula Braun800c5eb2012-02-08 00:19:47 +00002278 if (!iucv || sock_flag(sk, SOCK_ZAPPED))
Ursula Braun3881ac42011-08-08 01:33:54 +00002279 return;
2280
Ursula Braun3881ac42011-08-08 01:33:54 +00002281 list = &iucv->send_skb_q;
Ursula Braun800c5eb2012-02-08 00:19:47 +00002282 spin_lock_irqsave(&list->lock, flags);
Ursula Braun3881ac42011-08-08 01:33:54 +00002283 if (skb_queue_empty(list))
2284 goto out_unlock;
Ursula Braun800c5eb2012-02-08 00:19:47 +00002285 list_skb = list->next;
2286 nskb = list_skb->next;
Ursula Braun3881ac42011-08-08 01:33:54 +00002287 while (list_skb != (struct sk_buff *)list) {
2288 if (skb_shinfo(list_skb) == skb_shinfo(skb)) {
Ursula Braun3881ac42011-08-08 01:33:54 +00002289 switch (n) {
2290 case TX_NOTIFY_OK:
Ursula Braun800c5eb2012-02-08 00:19:47 +00002291 __skb_unlink(list_skb, list);
2292 kfree_skb(list_skb);
Ursula Braun3881ac42011-08-08 01:33:54 +00002293 iucv_sock_wake_msglim(sk);
Ursula Braun3881ac42011-08-08 01:33:54 +00002294 break;
2295 case TX_NOTIFY_PENDING:
2296 atomic_inc(&iucv->pendings);
2297 break;
2298 case TX_NOTIFY_DELAYED_OK:
Ursula Braun800c5eb2012-02-08 00:19:47 +00002299 __skb_unlink(list_skb, list);
Ursula Braun3881ac42011-08-08 01:33:54 +00002300 atomic_dec(&iucv->pendings);
2301 if (atomic_read(&iucv->pendings) <= 0)
2302 iucv_sock_wake_msglim(sk);
Ursula Braun800c5eb2012-02-08 00:19:47 +00002303 kfree_skb(list_skb);
Ursula Braun3881ac42011-08-08 01:33:54 +00002304 break;
2305 case TX_NOTIFY_UNREACHABLE:
2306 case TX_NOTIFY_DELAYED_UNREACHABLE:
2307 case TX_NOTIFY_TPQFULL: /* not yet used */
2308 case TX_NOTIFY_GENERALERROR:
2309 case TX_NOTIFY_DELAYED_GENERALERROR:
Ursula Braun800c5eb2012-02-08 00:19:47 +00002310 __skb_unlink(list_skb, list);
2311 kfree_skb(list_skb);
2312 if (sk->sk_state == IUCV_CONNECTED) {
2313 sk->sk_state = IUCV_DISCONN;
2314 sk->sk_state_change(sk);
2315 }
Ursula Braun3881ac42011-08-08 01:33:54 +00002316 break;
2317 }
2318 break;
2319 }
Ursula Braun800c5eb2012-02-08 00:19:47 +00002320 list_skb = nskb;
2321 nskb = nskb->next;
Ursula Braun3881ac42011-08-08 01:33:54 +00002322 }
Ursula Braun800c5eb2012-02-08 00:19:47 +00002323out_unlock:
Ursula Braun3881ac42011-08-08 01:33:54 +00002324 spin_unlock_irqrestore(&list->lock, flags);
2325
Ursula Braun42bd48e2011-12-19 22:56:28 +00002326 if (sk->sk_state == IUCV_CLOSING) {
2327 if (skb_queue_empty(&iucv_sk(sk)->send_skb_q)) {
2328 sk->sk_state = IUCV_CLOSED;
2329 sk->sk_state_change(sk);
2330 }
2331 }
2332
Ursula Braun3881ac42011-08-08 01:33:54 +00002333}
Ursula Braun9fbd87d2012-03-07 02:06:23 +00002334
2335/*
2336 * afiucv_netdev_event: handle netdev notifier chain events
2337 */
2338static int afiucv_netdev_event(struct notifier_block *this,
2339 unsigned long event, void *ptr)
2340{
Jiri Pirko351638e2013-05-28 01:30:21 +00002341 struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
Ursula Braun9fbd87d2012-03-07 02:06:23 +00002342 struct sock *sk;
2343 struct iucv_sock *iucv;
2344
2345 switch (event) {
2346 case NETDEV_REBOOT:
2347 case NETDEV_GOING_DOWN:
Sasha Levinb67bfe02013-02-27 17:06:00 -08002348 sk_for_each(sk, &iucv_sk_list.head) {
Ursula Braun9fbd87d2012-03-07 02:06:23 +00002349 iucv = iucv_sk(sk);
2350 if ((iucv->hs_dev == event_dev) &&
2351 (sk->sk_state == IUCV_CONNECTED)) {
2352 if (event == NETDEV_GOING_DOWN)
2353 iucv_send_ctrl(sk, AF_IUCV_FLAG_FIN);
2354 sk->sk_state = IUCV_DISCONN;
2355 sk->sk_state_change(sk);
2356 }
2357 }
2358 break;
2359 case NETDEV_DOWN:
2360 case NETDEV_UNREGISTER:
2361 default:
2362 break;
2363 }
2364 return NOTIFY_DONE;
2365}
2366
2367static struct notifier_block afiucv_netdev_notifier = {
2368 .notifier_call = afiucv_netdev_event,
2369};
2370
Alexey Dobriyan5708e862009-09-14 12:23:23 +00002371static const struct proto_ops iucv_sock_ops = {
Jennifer Hunteac37312007-02-08 13:51:54 -08002372 .family = PF_IUCV,
2373 .owner = THIS_MODULE,
2374 .release = iucv_sock_release,
2375 .bind = iucv_sock_bind,
2376 .connect = iucv_sock_connect,
2377 .listen = iucv_sock_listen,
2378 .accept = iucv_sock_accept,
2379 .getname = iucv_sock_getname,
2380 .sendmsg = iucv_sock_sendmsg,
2381 .recvmsg = iucv_sock_recvmsg,
2382 .poll = iucv_sock_poll,
2383 .ioctl = sock_no_ioctl,
2384 .mmap = sock_no_mmap,
2385 .socketpair = sock_no_socketpair,
2386 .shutdown = iucv_sock_shutdown,
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00002387 .setsockopt = iucv_sock_setsockopt,
2388 .getsockopt = iucv_sock_getsockopt,
Jennifer Hunteac37312007-02-08 13:51:54 -08002389};
2390
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00002391static const struct net_proto_family iucv_sock_family_ops = {
Jennifer Hunteac37312007-02-08 13:51:54 -08002392 .family = AF_IUCV,
2393 .owner = THIS_MODULE,
2394 .create = iucv_sock_create,
2395};
2396
Ursula Braun3881ac42011-08-08 01:33:54 +00002397static struct packet_type iucv_packet_type = {
2398 .type = cpu_to_be16(ETH_P_AF_IUCV),
2399 .func = afiucv_hs_rcv,
2400};
2401
2402static int afiucv_iucv_init(void)
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00002403{
2404 int err;
2405
2406 err = pr_iucv->iucv_register(&af_iucv_handler, 0);
2407 if (err)
2408 goto out;
2409 /* establish dummy device */
2410 af_iucv_driver.bus = pr_iucv->bus;
2411 err = driver_register(&af_iucv_driver);
2412 if (err)
2413 goto out_iucv;
2414 af_iucv_dev = kzalloc(sizeof(struct device), GFP_KERNEL);
2415 if (!af_iucv_dev) {
2416 err = -ENOMEM;
2417 goto out_driver;
2418 }
2419 dev_set_name(af_iucv_dev, "af_iucv");
2420 af_iucv_dev->bus = pr_iucv->bus;
2421 af_iucv_dev->parent = pr_iucv->root;
2422 af_iucv_dev->release = (void (*)(struct device *))kfree;
2423 af_iucv_dev->driver = &af_iucv_driver;
2424 err = device_register(af_iucv_dev);
2425 if (err)
2426 goto out_driver;
2427 return 0;
2428
2429out_driver:
2430 driver_unregister(&af_iucv_driver);
2431out_iucv:
2432 pr_iucv->iucv_unregister(&af_iucv_handler, 0);
2433out:
2434 return err;
2435}
2436
Heiko Carstensda99f052007-05-04 12:23:27 -07002437static int __init afiucv_init(void)
Jennifer Hunteac37312007-02-08 13:51:54 -08002438{
2439 int err;
2440
Ursula Braun3881ac42011-08-08 01:33:54 +00002441 if (MACHINE_IS_VM) {
2442 cpcmd("QUERY USERID", iucv_userid, sizeof(iucv_userid), &err);
2443 if (unlikely(err)) {
2444 WARN_ON(err);
2445 err = -EPROTONOSUPPORT;
2446 goto out;
2447 }
Jennifer Hunteac37312007-02-08 13:51:54 -08002448
Ursula Braun3881ac42011-08-08 01:33:54 +00002449 pr_iucv = try_then_request_module(symbol_get(iucv_if), "iucv");
2450 if (!pr_iucv) {
2451 printk(KERN_WARNING "iucv_if lookup failed\n");
2452 memset(&iucv_userid, 0, sizeof(iucv_userid));
2453 }
2454 } else {
2455 memset(&iucv_userid, 0, sizeof(iucv_userid));
2456 pr_iucv = NULL;
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00002457 }
2458
Jennifer Hunteac37312007-02-08 13:51:54 -08002459 err = proto_register(&iucv_proto, 0);
2460 if (err)
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00002461 goto out;
Jennifer Hunteac37312007-02-08 13:51:54 -08002462 err = sock_register(&iucv_sock_family_ops);
2463 if (err)
2464 goto out_proto;
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00002465
Ursula Braun3881ac42011-08-08 01:33:54 +00002466 if (pr_iucv) {
2467 err = afiucv_iucv_init();
2468 if (err)
2469 goto out_sock;
Ursula Braun9fbd87d2012-03-07 02:06:23 +00002470 } else
2471 register_netdevice_notifier(&afiucv_netdev_notifier);
Ursula Braun3881ac42011-08-08 01:33:54 +00002472 dev_add_pack(&iucv_packet_type);
Jennifer Hunteac37312007-02-08 13:51:54 -08002473 return 0;
2474
Ursula Braunc23cad92009-06-16 10:30:44 +02002475out_sock:
2476 sock_unregister(PF_IUCV);
Jennifer Hunteac37312007-02-08 13:51:54 -08002477out_proto:
2478 proto_unregister(&iucv_proto);
Jennifer Hunteac37312007-02-08 13:51:54 -08002479out:
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00002480 if (pr_iucv)
2481 symbol_put(iucv_if);
Jennifer Hunteac37312007-02-08 13:51:54 -08002482 return err;
2483}
2484
2485static void __exit afiucv_exit(void)
2486{
Ursula Braun3881ac42011-08-08 01:33:54 +00002487 if (pr_iucv) {
2488 device_unregister(af_iucv_dev);
2489 driver_unregister(&af_iucv_driver);
2490 pr_iucv->iucv_unregister(&af_iucv_handler, 0);
2491 symbol_put(iucv_if);
Ursula Braun9fbd87d2012-03-07 02:06:23 +00002492 } else
2493 unregister_netdevice_notifier(&afiucv_netdev_notifier);
Ursula Braun3881ac42011-08-08 01:33:54 +00002494 dev_remove_pack(&iucv_packet_type);
Jennifer Hunteac37312007-02-08 13:51:54 -08002495 sock_unregister(PF_IUCV);
2496 proto_unregister(&iucv_proto);
Jennifer Hunteac37312007-02-08 13:51:54 -08002497}
2498
2499module_init(afiucv_init);
2500module_exit(afiucv_exit);
2501
2502MODULE_AUTHOR("Jennifer Hunt <jenhunt@us.ibm.com>");
2503MODULE_DESCRIPTION("IUCV Sockets ver " VERSION);
2504MODULE_VERSION(VERSION);
2505MODULE_LICENSE("GPL");
2506MODULE_ALIAS_NETPROTO(PF_IUCV);
Ursula Braun3881ac42011-08-08 01:33:54 +00002507