blob: c4b7218058b648856066bb24dacf38d54d16defc [file] [log] [blame]
Jennifer Hunteac37312007-02-08 13:51:54 -08001/*
Jennifer Hunteac37312007-02-08 13:51:54 -08002 * IUCV protocol stack for Linux on zSeries
3 *
Ursula Braunc23cad92009-06-16 10:30:44 +02004 * Copyright IBM Corp. 2006, 2009
Jennifer Hunteac37312007-02-08 13:51:54 -08005 *
6 * Author(s): Jennifer Hunt <jenhunt@us.ibm.com>
Ursula Braunc23cad92009-06-16 10:30:44 +02007 * Hendrik Brueckner <brueckner@linux.vnet.ibm.com>
8 * PM functions:
9 * Ursula Braun <ursula.braun@de.ibm.com>
Jennifer Hunteac37312007-02-08 13:51:54 -080010 */
11
Ursula Braun8f7c5022008-12-25 13:39:47 +010012#define KMSG_COMPONENT "af_iucv"
13#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
14
Jennifer Hunteac37312007-02-08 13:51:54 -080015#include <linux/module.h>
16#include <linux/types.h>
17#include <linux/list.h>
18#include <linux/errno.h>
19#include <linux/kernel.h>
20#include <linux/sched.h>
21#include <linux/slab.h>
22#include <linux/skbuff.h>
23#include <linux/init.h>
24#include <linux/poll.h>
25#include <net/sock.h>
26#include <asm/ebcdic.h>
27#include <asm/cpcmd.h>
28#include <linux/kmod.h>
29
Jennifer Hunteac37312007-02-08 13:51:54 -080030#include <net/iucv/af_iucv.h>
31
Ursula Braun3881ac42011-08-08 01:33:54 +000032#define VERSION "1.2"
Jennifer Hunteac37312007-02-08 13:51:54 -080033
34static char iucv_userid[80];
35
Alexey Dobriyan5708e862009-09-14 12:23:23 +000036static const struct proto_ops iucv_sock_ops;
Jennifer Hunteac37312007-02-08 13:51:54 -080037
38static struct proto iucv_proto = {
39 .name = "AF_IUCV",
40 .owner = THIS_MODULE,
41 .obj_size = sizeof(struct iucv_sock),
42};
43
Frank Blaschka6fcd61f2011-08-08 01:33:51 +000044static struct iucv_interface *pr_iucv;
45
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +000046/* special AF_IUCV IPRM messages */
47static const u8 iprm_shutdown[8] =
48 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01};
49
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +000050#define TRGCLS_SIZE (sizeof(((struct iucv_message *)0)->class))
51
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +000052#define __iucv_sock_wait(sk, condition, timeo, ret) \
53do { \
54 DEFINE_WAIT(__wait); \
55 long __timeo = timeo; \
56 ret = 0; \
Eric Dumazetaa395142010-04-20 13:03:51 +000057 prepare_to_wait(sk_sleep(sk), &__wait, TASK_INTERRUPTIBLE); \
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +000058 while (!(condition)) { \
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +000059 if (!__timeo) { \
60 ret = -EAGAIN; \
61 break; \
62 } \
63 if (signal_pending(current)) { \
64 ret = sock_intr_errno(__timeo); \
65 break; \
66 } \
67 release_sock(sk); \
68 __timeo = schedule_timeout(__timeo); \
69 lock_sock(sk); \
70 ret = sock_error(sk); \
71 if (ret) \
72 break; \
73 } \
Eric Dumazetaa395142010-04-20 13:03:51 +000074 finish_wait(sk_sleep(sk), &__wait); \
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +000075} while (0)
76
77#define iucv_sock_wait(sk, condition, timeo) \
78({ \
79 int __ret = 0; \
80 if (!(condition)) \
81 __iucv_sock_wait(sk, condition, timeo, __ret); \
82 __ret; \
83})
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +000084
Heiko Carstens57f20442007-10-08 02:02:52 -070085static void iucv_sock_kill(struct sock *sk);
86static void iucv_sock_close(struct sock *sk);
Ursula Braun7d316b92012-02-08 00:19:45 +000087static void iucv_sever_path(struct sock *, int);
Heiko Carstens57f20442007-10-08 02:02:52 -070088
Ursula Braun3881ac42011-08-08 01:33:54 +000089static int afiucv_hs_rcv(struct sk_buff *skb, struct net_device *dev,
90 struct packet_type *pt, struct net_device *orig_dev);
91static int afiucv_hs_send(struct iucv_message *imsg, struct sock *sock,
92 struct sk_buff *skb, u8 flags);
93static void afiucv_hs_callback_txnotify(struct sk_buff *, enum iucv_tx_notify);
94
Jennifer Hunteac37312007-02-08 13:51:54 -080095/* Call Back functions */
96static void iucv_callback_rx(struct iucv_path *, struct iucv_message *);
97static void iucv_callback_txdone(struct iucv_path *, struct iucv_message *);
98static void iucv_callback_connack(struct iucv_path *, u8 ipuser[16]);
Heiko Carstensda99f052007-05-04 12:23:27 -070099static int iucv_callback_connreq(struct iucv_path *, u8 ipvmid[8],
100 u8 ipuser[16]);
Jennifer Hunteac37312007-02-08 13:51:54 -0800101static void iucv_callback_connrej(struct iucv_path *, u8 ipuser[16]);
Hendrik Brueckneraf88b522009-04-21 23:26:21 +0000102static void iucv_callback_shutdown(struct iucv_path *, u8 ipuser[16]);
Jennifer Hunteac37312007-02-08 13:51:54 -0800103
104static struct iucv_sock_list iucv_sk_list = {
Robert P. J. Day3db8ce32008-04-10 02:11:24 -0700105 .lock = __RW_LOCK_UNLOCKED(iucv_sk_list.lock),
Jennifer Hunteac37312007-02-08 13:51:54 -0800106 .autobind_name = ATOMIC_INIT(0)
107};
108
109static struct iucv_handler af_iucv_handler = {
110 .path_pending = iucv_callback_connreq,
111 .path_complete = iucv_callback_connack,
112 .path_severed = iucv_callback_connrej,
113 .message_pending = iucv_callback_rx,
Hendrik Brueckneraf88b522009-04-21 23:26:21 +0000114 .message_complete = iucv_callback_txdone,
115 .path_quiesced = iucv_callback_shutdown,
Jennifer Hunteac37312007-02-08 13:51:54 -0800116};
117
118static inline void high_nmcpy(unsigned char *dst, char *src)
119{
120 memcpy(dst, src, 8);
121}
122
123static inline void low_nmcpy(unsigned char *dst, char *src)
124{
125 memcpy(&dst[8], src, 8);
126}
127
Ursula Braunc23cad92009-06-16 10:30:44 +0200128static int afiucv_pm_prepare(struct device *dev)
129{
130#ifdef CONFIG_PM_DEBUG
131 printk(KERN_WARNING "afiucv_pm_prepare\n");
132#endif
133 return 0;
134}
135
136static void afiucv_pm_complete(struct device *dev)
137{
138#ifdef CONFIG_PM_DEBUG
139 printk(KERN_WARNING "afiucv_pm_complete\n");
140#endif
Ursula Braunc23cad92009-06-16 10:30:44 +0200141}
142
143/**
144 * afiucv_pm_freeze() - Freeze PM callback
145 * @dev: AFIUCV dummy device
146 *
147 * Sever all established IUCV communication pathes
148 */
149static int afiucv_pm_freeze(struct device *dev)
150{
151 struct iucv_sock *iucv;
152 struct sock *sk;
Ursula Braunc23cad92009-06-16 10:30:44 +0200153 int err = 0;
154
155#ifdef CONFIG_PM_DEBUG
156 printk(KERN_WARNING "afiucv_pm_freeze\n");
157#endif
158 read_lock(&iucv_sk_list.lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800159 sk_for_each(sk, &iucv_sk_list.head) {
Ursula Braunc23cad92009-06-16 10:30:44 +0200160 iucv = iucv_sk(sk);
Ursula Braunc23cad92009-06-16 10:30:44 +0200161 switch (sk->sk_state) {
Ursula Braunc23cad92009-06-16 10:30:44 +0200162 case IUCV_DISCONN:
163 case IUCV_CLOSING:
164 case IUCV_CONNECTED:
Ursula Braun7d316b92012-02-08 00:19:45 +0000165 iucv_sever_path(sk, 0);
Ursula Braunc23cad92009-06-16 10:30:44 +0200166 break;
167 case IUCV_OPEN:
168 case IUCV_BOUND:
169 case IUCV_LISTEN:
170 case IUCV_CLOSED:
171 default:
172 break;
173 }
Ursula Braun7d316b92012-02-08 00:19:45 +0000174 skb_queue_purge(&iucv->send_skb_q);
175 skb_queue_purge(&iucv->backlog_skb_q);
Ursula Braunc23cad92009-06-16 10:30:44 +0200176 }
177 read_unlock(&iucv_sk_list.lock);
178 return err;
179}
180
181/**
182 * afiucv_pm_restore_thaw() - Thaw and restore PM callback
183 * @dev: AFIUCV dummy device
184 *
185 * socket clean up after freeze
186 */
187static int afiucv_pm_restore_thaw(struct device *dev)
188{
Ursula Braunc23cad92009-06-16 10:30:44 +0200189 struct sock *sk;
Ursula Braunc23cad92009-06-16 10:30:44 +0200190
191#ifdef CONFIG_PM_DEBUG
192 printk(KERN_WARNING "afiucv_pm_restore_thaw\n");
193#endif
194 read_lock(&iucv_sk_list.lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800195 sk_for_each(sk, &iucv_sk_list.head) {
Ursula Braunc23cad92009-06-16 10:30:44 +0200196 switch (sk->sk_state) {
197 case IUCV_CONNECTED:
198 sk->sk_err = EPIPE;
199 sk->sk_state = IUCV_DISCONN;
200 sk->sk_state_change(sk);
201 break;
202 case IUCV_DISCONN:
Ursula Braunc23cad92009-06-16 10:30:44 +0200203 case IUCV_CLOSING:
204 case IUCV_LISTEN:
205 case IUCV_BOUND:
206 case IUCV_OPEN:
207 default:
208 break;
209 }
210 }
211 read_unlock(&iucv_sk_list.lock);
212 return 0;
213}
214
Alexey Dobriyan47145212009-12-14 18:00:08 -0800215static const struct dev_pm_ops afiucv_pm_ops = {
Ursula Braunc23cad92009-06-16 10:30:44 +0200216 .prepare = afiucv_pm_prepare,
217 .complete = afiucv_pm_complete,
218 .freeze = afiucv_pm_freeze,
219 .thaw = afiucv_pm_restore_thaw,
220 .restore = afiucv_pm_restore_thaw,
221};
222
223static struct device_driver af_iucv_driver = {
224 .owner = THIS_MODULE,
225 .name = "afiucv",
Frank Blaschka6fcd61f2011-08-08 01:33:51 +0000226 .bus = NULL,
Ursula Braunc23cad92009-06-16 10:30:44 +0200227 .pm = &afiucv_pm_ops,
228};
229
230/* dummy device used as trigger for PM functions */
231static struct device *af_iucv_dev;
232
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000233/**
234 * iucv_msg_length() - Returns the length of an iucv message.
235 * @msg: Pointer to struct iucv_message, MUST NOT be NULL
236 *
237 * The function returns the length of the specified iucv message @msg of data
238 * stored in a buffer and of data stored in the parameter list (PRMDATA).
239 *
240 * For IUCV_IPRMDATA, AF_IUCV uses the following convention to transport socket
241 * data:
242 * PRMDATA[0..6] socket data (max 7 bytes);
243 * PRMDATA[7] socket data length value (len is 0xff - PRMDATA[7])
244 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300245 * The socket data length is computed by subtracting the socket data length
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000246 * value from 0xFF.
247 * If the socket data len is greater 7, then PRMDATA can be used for special
248 * notifications (see iucv_sock_shutdown); and further,
249 * if the socket data len is > 7, the function returns 8.
250 *
251 * Use this function to allocate socket buffers to store iucv message data.
252 */
253static inline size_t iucv_msg_length(struct iucv_message *msg)
254{
255 size_t datalen;
256
257 if (msg->flags & IUCV_IPRMDATA) {
258 datalen = 0xff - msg->rmmsg[7];
259 return (datalen < 8) ? datalen : 8;
260 }
261 return msg->length;
262}
263
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +0000264/**
265 * iucv_sock_in_state() - check for specific states
266 * @sk: sock structure
267 * @state: first iucv sk state
268 * @state: second iucv sk state
269 *
270 * Returns true if the socket in either in the first or second state.
271 */
272static int iucv_sock_in_state(struct sock *sk, int state, int state2)
273{
274 return (sk->sk_state == state || sk->sk_state == state2);
275}
276
277/**
278 * iucv_below_msglim() - function to check if messages can be sent
279 * @sk: sock structure
280 *
281 * Returns true if the send queue length is lower than the message limit.
282 * Always returns true if the socket is not connected (no iucv path for
283 * checking the message limit).
284 */
285static inline int iucv_below_msglim(struct sock *sk)
286{
287 struct iucv_sock *iucv = iucv_sk(sk);
288
289 if (sk->sk_state != IUCV_CONNECTED)
290 return 1;
Ursula Braun3881ac42011-08-08 01:33:54 +0000291 if (iucv->transport == AF_IUCV_TRANS_IUCV)
292 return (skb_queue_len(&iucv->send_skb_q) < iucv->path->msglim);
293 else
294 return ((atomic_read(&iucv->msg_sent) < iucv->msglimit_peer) &&
295 (atomic_read(&iucv->pendings) <= 0));
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +0000296}
297
298/**
299 * iucv_sock_wake_msglim() - Wake up thread waiting on msg limit
300 */
301static void iucv_sock_wake_msglim(struct sock *sk)
302{
Eric Dumazet43815482010-04-29 11:01:49 +0000303 struct socket_wq *wq;
304
305 rcu_read_lock();
306 wq = rcu_dereference(sk->sk_wq);
307 if (wq_has_sleeper(wq))
308 wake_up_interruptible_all(&wq->wait);
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +0000309 sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
Eric Dumazet43815482010-04-29 11:01:49 +0000310 rcu_read_unlock();
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +0000311}
312
Ursula Braun3881ac42011-08-08 01:33:54 +0000313/**
314 * afiucv_hs_send() - send a message through HiperSockets transport
315 */
316static int afiucv_hs_send(struct iucv_message *imsg, struct sock *sock,
317 struct sk_buff *skb, u8 flags)
318{
Ursula Braun3881ac42011-08-08 01:33:54 +0000319 struct iucv_sock *iucv = iucv_sk(sock);
320 struct af_iucv_trans_hdr *phs_hdr;
321 struct sk_buff *nskb;
322 int err, confirm_recv = 0;
323
324 memset(skb->head, 0, ETH_HLEN);
325 phs_hdr = (struct af_iucv_trans_hdr *)skb_push(skb,
326 sizeof(struct af_iucv_trans_hdr));
327 skb_reset_mac_header(skb);
328 skb_reset_network_header(skb);
329 skb_push(skb, ETH_HLEN);
330 skb_reset_mac_header(skb);
331 memset(phs_hdr, 0, sizeof(struct af_iucv_trans_hdr));
332
333 phs_hdr->magic = ETH_P_AF_IUCV;
334 phs_hdr->version = 1;
335 phs_hdr->flags = flags;
336 if (flags == AF_IUCV_FLAG_SYN)
337 phs_hdr->window = iucv->msglimit;
338 else if ((flags == AF_IUCV_FLAG_WIN) || !flags) {
339 confirm_recv = atomic_read(&iucv->msg_recv);
340 phs_hdr->window = confirm_recv;
341 if (confirm_recv)
342 phs_hdr->flags = phs_hdr->flags | AF_IUCV_FLAG_WIN;
343 }
344 memcpy(phs_hdr->destUserID, iucv->dst_user_id, 8);
345 memcpy(phs_hdr->destAppName, iucv->dst_name, 8);
346 memcpy(phs_hdr->srcUserID, iucv->src_user_id, 8);
347 memcpy(phs_hdr->srcAppName, iucv->src_name, 8);
348 ASCEBC(phs_hdr->destUserID, sizeof(phs_hdr->destUserID));
349 ASCEBC(phs_hdr->destAppName, sizeof(phs_hdr->destAppName));
350 ASCEBC(phs_hdr->srcUserID, sizeof(phs_hdr->srcUserID));
351 ASCEBC(phs_hdr->srcAppName, sizeof(phs_hdr->srcAppName));
352 if (imsg)
353 memcpy(&phs_hdr->iucv_hdr, imsg, sizeof(struct iucv_message));
354
Ursula Braun800c5eb2012-02-08 00:19:47 +0000355 skb->dev = iucv->hs_dev;
Ursula Braun3881ac42011-08-08 01:33:54 +0000356 if (!skb->dev)
357 return -ENODEV;
Ursula Braun800c5eb2012-02-08 00:19:47 +0000358 if (!(skb->dev->flags & IFF_UP) || !netif_carrier_ok(skb->dev))
Ursula Braun3881ac42011-08-08 01:33:54 +0000359 return -ENETDOWN;
360 if (skb->len > skb->dev->mtu) {
361 if (sock->sk_type == SOCK_SEQPACKET)
362 return -EMSGSIZE;
363 else
364 skb_trim(skb, skb->dev->mtu);
365 }
366 skb->protocol = ETH_P_AF_IUCV;
Ursula Braun3881ac42011-08-08 01:33:54 +0000367 nskb = skb_clone(skb, GFP_ATOMIC);
368 if (!nskb)
369 return -ENOMEM;
370 skb_queue_tail(&iucv->send_skb_q, nskb);
371 err = dev_queue_xmit(skb);
Ursula Braun800c5eb2012-02-08 00:19:47 +0000372 if (net_xmit_eval(err)) {
Ursula Braun3881ac42011-08-08 01:33:54 +0000373 skb_unlink(nskb, &iucv->send_skb_q);
374 kfree_skb(nskb);
375 } else {
376 atomic_sub(confirm_recv, &iucv->msg_recv);
377 WARN_ON(atomic_read(&iucv->msg_recv) < 0);
378 }
Ursula Braun800c5eb2012-02-08 00:19:47 +0000379 return net_xmit_eval(err);
Ursula Braun3881ac42011-08-08 01:33:54 +0000380}
381
Jennifer Hunteac37312007-02-08 13:51:54 -0800382static struct sock *__iucv_get_sock_by_name(char *nm)
383{
384 struct sock *sk;
Jennifer Hunteac37312007-02-08 13:51:54 -0800385
Sasha Levinb67bfe02013-02-27 17:06:00 -0800386 sk_for_each(sk, &iucv_sk_list.head)
Jennifer Hunteac37312007-02-08 13:51:54 -0800387 if (!memcmp(&iucv_sk(sk)->src_name, nm, 8))
388 return sk;
389
390 return NULL;
391}
392
393static void iucv_sock_destruct(struct sock *sk)
394{
395 skb_queue_purge(&sk->sk_receive_queue);
Ursula Braun82492a32012-03-07 02:06:24 +0000396 skb_queue_purge(&sk->sk_error_queue);
397
398 sk_mem_reclaim(sk);
399
400 if (!sock_flag(sk, SOCK_DEAD)) {
401 pr_err("Attempt to release alive iucv socket %p\n", sk);
402 return;
403 }
404
405 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
406 WARN_ON(atomic_read(&sk->sk_wmem_alloc));
407 WARN_ON(sk->sk_wmem_queued);
408 WARN_ON(sk->sk_forward_alloc);
Jennifer Hunteac37312007-02-08 13:51:54 -0800409}
410
411/* Cleanup Listen */
412static void iucv_sock_cleanup_listen(struct sock *parent)
413{
414 struct sock *sk;
415
416 /* Close non-accepted connections */
417 while ((sk = iucv_accept_dequeue(parent, NULL))) {
418 iucv_sock_close(sk);
419 iucv_sock_kill(sk);
420 }
421
422 parent->sk_state = IUCV_CLOSED;
Jennifer Hunteac37312007-02-08 13:51:54 -0800423}
424
Hendrik Brueckner7514bab2009-09-16 04:37:27 +0000425/* Kill socket (only if zapped and orphaned) */
Jennifer Hunteac37312007-02-08 13:51:54 -0800426static void iucv_sock_kill(struct sock *sk)
427{
428 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
429 return;
430
431 iucv_sock_unlink(&iucv_sk_list, sk);
432 sock_set_flag(sk, SOCK_DEAD);
433 sock_put(sk);
434}
435
Ursula Braun7d316b92012-02-08 00:19:45 +0000436/* Terminate an IUCV path */
437static void iucv_sever_path(struct sock *sk, int with_user_data)
438{
439 unsigned char user_data[16];
440 struct iucv_sock *iucv = iucv_sk(sk);
441 struct iucv_path *path = iucv->path;
442
443 if (iucv->path) {
444 iucv->path = NULL;
445 if (with_user_data) {
446 low_nmcpy(user_data, iucv->src_name);
447 high_nmcpy(user_data, iucv->dst_name);
448 ASCEBC(user_data, sizeof(user_data));
449 pr_iucv->path_sever(path, user_data);
450 } else
451 pr_iucv->path_sever(path, NULL);
452 iucv_path_free(path);
453 }
454}
455
Ursula Braun9fbd87d2012-03-07 02:06:23 +0000456/* Send FIN through an IUCV socket for HIPER transport */
457static int iucv_send_ctrl(struct sock *sk, u8 flags)
458{
459 int err = 0;
460 int blen;
461 struct sk_buff *skb;
462
463 blen = sizeof(struct af_iucv_trans_hdr) + ETH_HLEN;
464 skb = sock_alloc_send_skb(sk, blen, 1, &err);
465 if (skb) {
466 skb_reserve(skb, blen);
467 err = afiucv_hs_send(NULL, sk, skb, flags);
468 }
469 return err;
470}
471
Jennifer Hunteac37312007-02-08 13:51:54 -0800472/* Close an IUCV socket */
473static void iucv_sock_close(struct sock *sk)
474{
Jennifer Hunteac37312007-02-08 13:51:54 -0800475 struct iucv_sock *iucv = iucv_sk(sk);
Jennifer Hunt561e0362007-05-04 12:22:07 -0700476 unsigned long timeo;
Ursula Braun800c5eb2012-02-08 00:19:47 +0000477 int err = 0;
Jennifer Hunteac37312007-02-08 13:51:54 -0800478
Jennifer Hunteac37312007-02-08 13:51:54 -0800479 lock_sock(sk);
480
Heiko Carstensda99f052007-05-04 12:23:27 -0700481 switch (sk->sk_state) {
Jennifer Hunteac37312007-02-08 13:51:54 -0800482 case IUCV_LISTEN:
483 iucv_sock_cleanup_listen(sk);
484 break;
485
486 case IUCV_CONNECTED:
Ursula Braun3881ac42011-08-08 01:33:54 +0000487 if (iucv->transport == AF_IUCV_TRANS_HIPER) {
Ursula Braun9fbd87d2012-03-07 02:06:23 +0000488 err = iucv_send_ctrl(sk, AF_IUCV_FLAG_FIN);
Ursula Braun3881ac42011-08-08 01:33:54 +0000489 sk->sk_state = IUCV_DISCONN;
490 sk->sk_state_change(sk);
491 }
Ursula Braun816abba2011-12-19 22:56:29 +0000492 case IUCV_DISCONN: /* fall through */
Jennifer Hunt561e0362007-05-04 12:22:07 -0700493 sk->sk_state = IUCV_CLOSING;
494 sk->sk_state_change(sk);
495
Ursula Braun800c5eb2012-02-08 00:19:47 +0000496 if (!err && !skb_queue_empty(&iucv->send_skb_q)) {
Jennifer Hunt561e0362007-05-04 12:22:07 -0700497 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
498 timeo = sk->sk_lingertime;
499 else
500 timeo = IUCV_DISCONN_TIMEOUT;
Ursula Braun9f6298a2011-05-12 18:45:08 +0000501 iucv_sock_wait(sk,
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +0000502 iucv_sock_in_state(sk, IUCV_CLOSED, 0),
503 timeo);
Jennifer Hunt561e0362007-05-04 12:22:07 -0700504 }
505
Ursula Braunbbe188c2009-04-21 06:04:20 +0000506 case IUCV_CLOSING: /* fall through */
Jennifer Hunt561e0362007-05-04 12:22:07 -0700507 sk->sk_state = IUCV_CLOSED;
508 sk->sk_state_change(sk);
509
Jennifer Hunteac37312007-02-08 13:51:54 -0800510 sk->sk_err = ECONNRESET;
511 sk->sk_state_change(sk);
512
Ursula Braun800c5eb2012-02-08 00:19:47 +0000513 skb_queue_purge(&iucv->send_skb_q);
Jennifer Hunt561e0362007-05-04 12:22:07 -0700514 skb_queue_purge(&iucv->backlog_skb_q);
Jennifer Hunteac37312007-02-08 13:51:54 -0800515
Ursula Braun7d316b92012-02-08 00:19:45 +0000516 default: /* fall through */
517 iucv_sever_path(sk, 1);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700518 }
Jennifer Hunteac37312007-02-08 13:51:54 -0800519
Ursula Braun800c5eb2012-02-08 00:19:47 +0000520 if (iucv->hs_dev) {
521 dev_put(iucv->hs_dev);
522 iucv->hs_dev = NULL;
523 sk->sk_bound_dev_if = 0;
524 }
525
Hendrik Brueckner7514bab2009-09-16 04:37:27 +0000526 /* mark socket for deletion by iucv_sock_kill() */
527 sock_set_flag(sk, SOCK_ZAPPED);
528
Jennifer Hunteac37312007-02-08 13:51:54 -0800529 release_sock(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800530}
531
532static void iucv_sock_init(struct sock *sk, struct sock *parent)
533{
534 if (parent)
535 sk->sk_type = parent->sk_type;
536}
537
538static struct sock *iucv_sock_alloc(struct socket *sock, int proto, gfp_t prio)
539{
540 struct sock *sk;
Ursula Braun493d3972011-08-08 01:33:52 +0000541 struct iucv_sock *iucv;
Jennifer Hunteac37312007-02-08 13:51:54 -0800542
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700543 sk = sk_alloc(&init_net, PF_IUCV, prio, &iucv_proto);
Jennifer Hunteac37312007-02-08 13:51:54 -0800544 if (!sk)
545 return NULL;
Ursula Braun493d3972011-08-08 01:33:52 +0000546 iucv = iucv_sk(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800547
548 sock_init_data(sock, sk);
Ursula Braun493d3972011-08-08 01:33:52 +0000549 INIT_LIST_HEAD(&iucv->accept_q);
550 spin_lock_init(&iucv->accept_q_lock);
551 skb_queue_head_init(&iucv->send_skb_q);
552 INIT_LIST_HEAD(&iucv->message_q.list);
553 spin_lock_init(&iucv->message_q.lock);
554 skb_queue_head_init(&iucv->backlog_skb_q);
555 iucv->send_tag = 0;
Ursula Braun3881ac42011-08-08 01:33:54 +0000556 atomic_set(&iucv->pendings, 0);
Ursula Braun493d3972011-08-08 01:33:52 +0000557 iucv->flags = 0;
Ursula Braun3881ac42011-08-08 01:33:54 +0000558 iucv->msglimit = 0;
559 atomic_set(&iucv->msg_sent, 0);
560 atomic_set(&iucv->msg_recv, 0);
Ursula Braun493d3972011-08-08 01:33:52 +0000561 iucv->path = NULL;
Ursula Braun3881ac42011-08-08 01:33:54 +0000562 iucv->sk_txnotify = afiucv_hs_callback_txnotify;
Ursula Braun493d3972011-08-08 01:33:52 +0000563 memset(&iucv->src_user_id , 0, 32);
Ursula Braun3881ac42011-08-08 01:33:54 +0000564 if (pr_iucv)
565 iucv->transport = AF_IUCV_TRANS_IUCV;
566 else
567 iucv->transport = AF_IUCV_TRANS_HIPER;
Jennifer Hunteac37312007-02-08 13:51:54 -0800568
569 sk->sk_destruct = iucv_sock_destruct;
570 sk->sk_sndtimeo = IUCV_CONN_TIMEOUT;
571 sk->sk_allocation = GFP_DMA;
572
573 sock_reset_flag(sk, SOCK_ZAPPED);
574
575 sk->sk_protocol = proto;
576 sk->sk_state = IUCV_OPEN;
577
Jennifer Hunteac37312007-02-08 13:51:54 -0800578 iucv_sock_link(&iucv_sk_list, sk);
579 return sk;
580}
581
582/* Create an IUCV socket */
Eric Paris3f378b62009-11-05 22:18:14 -0800583static int iucv_sock_create(struct net *net, struct socket *sock, int protocol,
584 int kern)
Jennifer Hunteac37312007-02-08 13:51:54 -0800585{
586 struct sock *sk;
587
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +0000588 if (protocol && protocol != PF_IUCV)
589 return -EPROTONOSUPPORT;
Jennifer Hunteac37312007-02-08 13:51:54 -0800590
591 sock->state = SS_UNCONNECTED;
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +0000592
593 switch (sock->type) {
594 case SOCK_STREAM:
595 sock->ops = &iucv_sock_ops;
596 break;
597 case SOCK_SEQPACKET:
598 /* currently, proto ops can handle both sk types */
599 sock->ops = &iucv_sock_ops;
600 break;
601 default:
602 return -ESOCKTNOSUPPORT;
603 }
Jennifer Hunteac37312007-02-08 13:51:54 -0800604
605 sk = iucv_sock_alloc(sock, protocol, GFP_KERNEL);
606 if (!sk)
607 return -ENOMEM;
608
609 iucv_sock_init(sk, NULL);
610
611 return 0;
612}
613
614void iucv_sock_link(struct iucv_sock_list *l, struct sock *sk)
615{
616 write_lock_bh(&l->lock);
617 sk_add_node(sk, &l->head);
618 write_unlock_bh(&l->lock);
619}
620
621void iucv_sock_unlink(struct iucv_sock_list *l, struct sock *sk)
622{
623 write_lock_bh(&l->lock);
624 sk_del_node_init(sk);
625 write_unlock_bh(&l->lock);
626}
627
628void iucv_accept_enqueue(struct sock *parent, struct sock *sk)
629{
Ursula Braunfebca282007-07-14 19:04:25 -0700630 unsigned long flags;
631 struct iucv_sock *par = iucv_sk(parent);
632
Jennifer Hunteac37312007-02-08 13:51:54 -0800633 sock_hold(sk);
Ursula Braunfebca282007-07-14 19:04:25 -0700634 spin_lock_irqsave(&par->accept_q_lock, flags);
635 list_add_tail(&iucv_sk(sk)->accept_q, &par->accept_q);
636 spin_unlock_irqrestore(&par->accept_q_lock, flags);
Jennifer Hunteac37312007-02-08 13:51:54 -0800637 iucv_sk(sk)->parent = parent;
Hendrik Brueckner49f5eba2009-10-14 22:54:55 +0000638 sk_acceptq_added(parent);
Jennifer Hunteac37312007-02-08 13:51:54 -0800639}
640
641void iucv_accept_unlink(struct sock *sk)
642{
Ursula Braunfebca282007-07-14 19:04:25 -0700643 unsigned long flags;
644 struct iucv_sock *par = iucv_sk(iucv_sk(sk)->parent);
645
646 spin_lock_irqsave(&par->accept_q_lock, flags);
Jennifer Hunteac37312007-02-08 13:51:54 -0800647 list_del_init(&iucv_sk(sk)->accept_q);
Ursula Braunfebca282007-07-14 19:04:25 -0700648 spin_unlock_irqrestore(&par->accept_q_lock, flags);
Hendrik Brueckner49f5eba2009-10-14 22:54:55 +0000649 sk_acceptq_removed(iucv_sk(sk)->parent);
Jennifer Hunteac37312007-02-08 13:51:54 -0800650 iucv_sk(sk)->parent = NULL;
651 sock_put(sk);
652}
653
654struct sock *iucv_accept_dequeue(struct sock *parent, struct socket *newsock)
655{
656 struct iucv_sock *isk, *n;
657 struct sock *sk;
658
Heiko Carstensda99f052007-05-04 12:23:27 -0700659 list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) {
Jennifer Hunteac37312007-02-08 13:51:54 -0800660 sk = (struct sock *) isk;
661 lock_sock(sk);
662
663 if (sk->sk_state == IUCV_CLOSED) {
Jennifer Hunteac37312007-02-08 13:51:54 -0800664 iucv_accept_unlink(sk);
Ursula Braunfebca282007-07-14 19:04:25 -0700665 release_sock(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800666 continue;
667 }
668
669 if (sk->sk_state == IUCV_CONNECTED ||
Ursula Braunaac63992011-12-19 22:56:31 +0000670 sk->sk_state == IUCV_DISCONN ||
Jennifer Hunteac37312007-02-08 13:51:54 -0800671 !newsock) {
672 iucv_accept_unlink(sk);
673 if (newsock)
674 sock_graft(sk, newsock);
675
Jennifer Hunteac37312007-02-08 13:51:54 -0800676 release_sock(sk);
677 return sk;
678 }
679
680 release_sock(sk);
681 }
682 return NULL;
683}
684
Jennifer Hunteac37312007-02-08 13:51:54 -0800685/* Bind an unbound socket */
686static int iucv_sock_bind(struct socket *sock, struct sockaddr *addr,
687 int addr_len)
688{
689 struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
690 struct sock *sk = sock->sk;
691 struct iucv_sock *iucv;
Ursula Braun3881ac42011-08-08 01:33:54 +0000692 int err = 0;
693 struct net_device *dev;
694 char uid[9];
Jennifer Hunteac37312007-02-08 13:51:54 -0800695
696 /* Verify the input sockaddr */
697 if (!addr || addr->sa_family != AF_IUCV)
698 return -EINVAL;
699
700 lock_sock(sk);
701 if (sk->sk_state != IUCV_OPEN) {
702 err = -EBADFD;
703 goto done;
704 }
705
706 write_lock_bh(&iucv_sk_list.lock);
707
708 iucv = iucv_sk(sk);
709 if (__iucv_get_sock_by_name(sa->siucv_name)) {
710 err = -EADDRINUSE;
711 goto done_unlock;
712 }
Ursula Braun3881ac42011-08-08 01:33:54 +0000713 if (iucv->path)
Jennifer Hunteac37312007-02-08 13:51:54 -0800714 goto done_unlock;
Jennifer Hunteac37312007-02-08 13:51:54 -0800715
716 /* Bind the socket */
Ursula Braun3881ac42011-08-08 01:33:54 +0000717 if (pr_iucv)
718 if (!memcmp(sa->siucv_user_id, iucv_userid, 8))
719 goto vm_bind; /* VM IUCV transport */
Jennifer Hunteac37312007-02-08 13:51:54 -0800720
Ursula Braun3881ac42011-08-08 01:33:54 +0000721 /* try hiper transport */
722 memcpy(uid, sa->siucv_user_id, sizeof(uid));
723 ASCEBC(uid, 8);
724 rcu_read_lock();
725 for_each_netdev_rcu(&init_net, dev) {
726 if (!memcmp(dev->perm_addr, uid, 8)) {
727 memcpy(iucv->src_name, sa->siucv_name, 8);
728 memcpy(iucv->src_user_id, sa->siucv_user_id, 8);
Ursula Braun816abba2011-12-19 22:56:29 +0000729 sk->sk_bound_dev_if = dev->ifindex;
Ursula Braun800c5eb2012-02-08 00:19:47 +0000730 iucv->hs_dev = dev;
731 dev_hold(dev);
Ursula Braun3881ac42011-08-08 01:33:54 +0000732 sk->sk_state = IUCV_BOUND;
733 iucv->transport = AF_IUCV_TRANS_HIPER;
734 if (!iucv->msglimit)
735 iucv->msglimit = IUCV_HIPER_MSGLIM_DEFAULT;
736 rcu_read_unlock();
737 goto done_unlock;
738 }
739 }
740 rcu_read_unlock();
741vm_bind:
742 if (pr_iucv) {
743 /* use local userid for backward compat */
744 memcpy(iucv->src_name, sa->siucv_name, 8);
745 memcpy(iucv->src_user_id, iucv_userid, 8);
746 sk->sk_state = IUCV_BOUND;
747 iucv->transport = AF_IUCV_TRANS_IUCV;
748 if (!iucv->msglimit)
749 iucv->msglimit = IUCV_QUEUELEN_DEFAULT;
750 goto done_unlock;
751 }
752 /* found no dev to bind */
753 err = -ENODEV;
Jennifer Hunteac37312007-02-08 13:51:54 -0800754done_unlock:
755 /* Release the socket list lock */
756 write_unlock_bh(&iucv_sk_list.lock);
757done:
758 release_sock(sk);
759 return err;
760}
761
762/* Automatically bind an unbound socket */
763static int iucv_sock_autobind(struct sock *sk)
764{
765 struct iucv_sock *iucv = iucv_sk(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800766 char name[12];
767 int err = 0;
768
Ursula Braunaac63992011-12-19 22:56:31 +0000769 if (unlikely(!pr_iucv))
Jennifer Hunteac37312007-02-08 13:51:54 -0800770 return -EPROTO;
771
Ursula Braunaac63992011-12-19 22:56:31 +0000772 memcpy(iucv->src_user_id, iucv_userid, 8);
Jennifer Hunteac37312007-02-08 13:51:54 -0800773
774 write_lock_bh(&iucv_sk_list.lock);
775
776 sprintf(name, "%08x", atomic_inc_return(&iucv_sk_list.autobind_name));
777 while (__iucv_get_sock_by_name(name)) {
778 sprintf(name, "%08x",
779 atomic_inc_return(&iucv_sk_list.autobind_name));
780 }
781
782 write_unlock_bh(&iucv_sk_list.lock);
783
784 memcpy(&iucv->src_name, name, 8);
785
Ursula Braun3881ac42011-08-08 01:33:54 +0000786 if (!iucv->msglimit)
787 iucv->msglimit = IUCV_QUEUELEN_DEFAULT;
788
Jennifer Hunteac37312007-02-08 13:51:54 -0800789 return err;
790}
791
Ursula Braun3881ac42011-08-08 01:33:54 +0000792static int afiucv_path_connect(struct socket *sock, struct sockaddr *addr)
Jennifer Hunteac37312007-02-08 13:51:54 -0800793{
794 struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
795 struct sock *sk = sock->sk;
Ursula Braun493d3972011-08-08 01:33:52 +0000796 struct iucv_sock *iucv = iucv_sk(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800797 unsigned char user_data[16];
798 int err;
799
Jennifer Hunteac37312007-02-08 13:51:54 -0800800 high_nmcpy(user_data, sa->siucv_name);
Ursula Braun493d3972011-08-08 01:33:52 +0000801 low_nmcpy(user_data, iucv->src_name);
Jennifer Hunteac37312007-02-08 13:51:54 -0800802 ASCEBC(user_data, sizeof(user_data));
803
Jennifer Hunteac37312007-02-08 13:51:54 -0800804 /* Create path. */
Hendrik Brueckner09488e2e2009-04-21 23:26:27 +0000805 iucv->path = iucv_path_alloc(iucv->msglimit,
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000806 IUCV_IPRMDATA, GFP_KERNEL);
Ursula Braund4444722008-02-07 18:07:19 -0800807 if (!iucv->path) {
808 err = -ENOMEM;
809 goto done;
810 }
Frank Blaschka6fcd61f2011-08-08 01:33:51 +0000811 err = pr_iucv->path_connect(iucv->path, &af_iucv_handler,
812 sa->siucv_user_id, NULL, user_data,
813 sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800814 if (err) {
815 iucv_path_free(iucv->path);
816 iucv->path = NULL;
Hendrik Brueckner55cdea92009-01-05 18:07:07 -0800817 switch (err) {
818 case 0x0b: /* Target communicator is not logged on */
819 err = -ENETUNREACH;
820 break;
821 case 0x0d: /* Max connections for this guest exceeded */
822 case 0x0e: /* Max connections for target guest exceeded */
823 err = -EAGAIN;
824 break;
825 case 0x0f: /* Missing IUCV authorization */
826 err = -EACCES;
827 break;
828 default:
829 err = -ECONNREFUSED;
830 break;
831 }
Ursula Braun3881ac42011-08-08 01:33:54 +0000832 }
833done:
834 return err;
835}
836
837/* Connect an unconnected socket */
838static int iucv_sock_connect(struct socket *sock, struct sockaddr *addr,
839 int alen, int flags)
840{
841 struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
842 struct sock *sk = sock->sk;
843 struct iucv_sock *iucv = iucv_sk(sk);
844 int err;
845
846 if (addr->sa_family != AF_IUCV || alen < sizeof(struct sockaddr_iucv))
847 return -EINVAL;
848
849 if (sk->sk_state != IUCV_OPEN && sk->sk_state != IUCV_BOUND)
850 return -EBADFD;
851
852 if (sk->sk_state == IUCV_OPEN &&
853 iucv->transport == AF_IUCV_TRANS_HIPER)
854 return -EBADFD; /* explicit bind required */
855
856 if (sk->sk_type != SOCK_STREAM && sk->sk_type != SOCK_SEQPACKET)
857 return -EINVAL;
858
859 if (sk->sk_state == IUCV_OPEN) {
860 err = iucv_sock_autobind(sk);
861 if (unlikely(err))
862 return err;
Jennifer Hunteac37312007-02-08 13:51:54 -0800863 }
864
Ursula Braun3881ac42011-08-08 01:33:54 +0000865 lock_sock(sk);
866
867 /* Set the destination information */
868 memcpy(iucv->dst_user_id, sa->siucv_user_id, 8);
869 memcpy(iucv->dst_name, sa->siucv_name, 8);
870
871 if (iucv->transport == AF_IUCV_TRANS_HIPER)
Ursula Braun9fbd87d2012-03-07 02:06:23 +0000872 err = iucv_send_ctrl(sock->sk, AF_IUCV_FLAG_SYN);
Ursula Braun3881ac42011-08-08 01:33:54 +0000873 else
874 err = afiucv_path_connect(sock, addr);
875 if (err)
876 goto done;
877
878 if (sk->sk_state != IUCV_CONNECTED)
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +0000879 err = iucv_sock_wait(sk, iucv_sock_in_state(sk, IUCV_CONNECTED,
880 IUCV_DISCONN),
881 sock_sndtimeo(sk, flags & O_NONBLOCK));
Jennifer Hunteac37312007-02-08 13:51:54 -0800882
Ursula Braun3881ac42011-08-08 01:33:54 +0000883 if (sk->sk_state == IUCV_DISCONN || sk->sk_state == IUCV_CLOSED)
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000884 err = -ECONNREFUSED;
Ursula Braun18becbc2009-01-05 18:07:46 -0800885
Ursula Braun7d316b92012-02-08 00:19:45 +0000886 if (err && iucv->transport == AF_IUCV_TRANS_IUCV)
887 iucv_sever_path(sk, 0);
Ursula Braun18becbc2009-01-05 18:07:46 -0800888
Jennifer Hunteac37312007-02-08 13:51:54 -0800889done:
890 release_sock(sk);
891 return err;
892}
893
894/* Move a socket into listening state. */
895static int iucv_sock_listen(struct socket *sock, int backlog)
896{
897 struct sock *sk = sock->sk;
898 int err;
899
900 lock_sock(sk);
901
902 err = -EINVAL;
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +0000903 if (sk->sk_state != IUCV_BOUND)
904 goto done;
905
906 if (sock->type != SOCK_STREAM && sock->type != SOCK_SEQPACKET)
Jennifer Hunteac37312007-02-08 13:51:54 -0800907 goto done;
908
909 sk->sk_max_ack_backlog = backlog;
910 sk->sk_ack_backlog = 0;
911 sk->sk_state = IUCV_LISTEN;
912 err = 0;
913
914done:
915 release_sock(sk);
916 return err;
917}
918
919/* Accept a pending connection */
920static int iucv_sock_accept(struct socket *sock, struct socket *newsock,
921 int flags)
922{
923 DECLARE_WAITQUEUE(wait, current);
924 struct sock *sk = sock->sk, *nsk;
925 long timeo;
926 int err = 0;
927
Jennifer Hunt561e0362007-05-04 12:22:07 -0700928 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
Jennifer Hunteac37312007-02-08 13:51:54 -0800929
930 if (sk->sk_state != IUCV_LISTEN) {
931 err = -EBADFD;
932 goto done;
933 }
934
935 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
936
937 /* Wait for an incoming connection */
Eric Dumazetaa395142010-04-20 13:03:51 +0000938 add_wait_queue_exclusive(sk_sleep(sk), &wait);
Heiko Carstensda99f052007-05-04 12:23:27 -0700939 while (!(nsk = iucv_accept_dequeue(sk, newsock))) {
Jennifer Hunteac37312007-02-08 13:51:54 -0800940 set_current_state(TASK_INTERRUPTIBLE);
941 if (!timeo) {
942 err = -EAGAIN;
943 break;
944 }
945
946 release_sock(sk);
947 timeo = schedule_timeout(timeo);
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 break;
953 }
954
955 if (signal_pending(current)) {
956 err = sock_intr_errno(timeo);
957 break;
958 }
959 }
960
961 set_current_state(TASK_RUNNING);
Eric Dumazetaa395142010-04-20 13:03:51 +0000962 remove_wait_queue(sk_sleep(sk), &wait);
Jennifer Hunteac37312007-02-08 13:51:54 -0800963
964 if (err)
965 goto done;
966
967 newsock->state = SS_CONNECTED;
968
969done:
970 release_sock(sk);
971 return err;
972}
973
974static int iucv_sock_getname(struct socket *sock, struct sockaddr *addr,
975 int *len, int peer)
976{
977 struct sockaddr_iucv *siucv = (struct sockaddr_iucv *) addr;
978 struct sock *sk = sock->sk;
Ursula Braun493d3972011-08-08 01:33:52 +0000979 struct iucv_sock *iucv = iucv_sk(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800980
981 addr->sa_family = AF_IUCV;
982 *len = sizeof(struct sockaddr_iucv);
983
984 if (peer) {
Ursula Braun493d3972011-08-08 01:33:52 +0000985 memcpy(siucv->siucv_user_id, iucv->dst_user_id, 8);
986 memcpy(siucv->siucv_name, iucv->dst_name, 8);
Jennifer Hunteac37312007-02-08 13:51:54 -0800987 } else {
Ursula Braun493d3972011-08-08 01:33:52 +0000988 memcpy(siucv->siucv_user_id, iucv->src_user_id, 8);
989 memcpy(siucv->siucv_name, iucv->src_name, 8);
Jennifer Hunteac37312007-02-08 13:51:54 -0800990 }
991 memset(&siucv->siucv_port, 0, sizeof(siucv->siucv_port));
992 memset(&siucv->siucv_addr, 0, sizeof(siucv->siucv_addr));
Ursula Braun493d3972011-08-08 01:33:52 +0000993 memset(&siucv->siucv_nodeid, 0, sizeof(siucv->siucv_nodeid));
Jennifer Hunteac37312007-02-08 13:51:54 -0800994
995 return 0;
996}
997
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000998/**
999 * iucv_send_iprm() - Send socket data in parameter list of an iucv message.
1000 * @path: IUCV path
1001 * @msg: Pointer to a struct iucv_message
1002 * @skb: The socket data to send, skb->len MUST BE <= 7
1003 *
1004 * Send the socket data in the parameter list in the iucv message
1005 * (IUCV_IPRMDATA). The socket data is stored at index 0 to 6 in the parameter
1006 * list and the socket data len at index 7 (last byte).
1007 * See also iucv_msg_length().
1008 *
1009 * Returns the error code from the iucv_message_send() call.
1010 */
1011static int iucv_send_iprm(struct iucv_path *path, struct iucv_message *msg,
1012 struct sk_buff *skb)
1013{
1014 u8 prmdata[8];
1015
1016 memcpy(prmdata, (void *) skb->data, skb->len);
1017 prmdata[7] = 0xff - (u8) skb->len;
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001018 return pr_iucv->message_send(path, msg, IUCV_IPRMDATA, 0,
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001019 (void *) prmdata, 8);
1020}
1021
Jennifer Hunteac37312007-02-08 13:51:54 -08001022static int iucv_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
1023 struct msghdr *msg, size_t len)
1024{
1025 struct sock *sk = sock->sk;
1026 struct iucv_sock *iucv = iucv_sk(sk);
1027 struct sk_buff *skb;
1028 struct iucv_message txmsg;
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001029 struct cmsghdr *cmsg;
1030 int cmsg_done;
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +00001031 long timeo;
Ursula Braun8f7c5022008-12-25 13:39:47 +01001032 char user_id[9];
1033 char appl_id[9];
Jennifer Hunteac37312007-02-08 13:51:54 -08001034 int err;
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +00001035 int noblock = msg->msg_flags & MSG_DONTWAIT;
Jennifer Hunteac37312007-02-08 13:51:54 -08001036
1037 err = sock_error(sk);
1038 if (err)
1039 return err;
1040
1041 if (msg->msg_flags & MSG_OOB)
1042 return -EOPNOTSUPP;
1043
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001044 /* SOCK_SEQPACKET: we do not support segmented records */
1045 if (sk->sk_type == SOCK_SEQPACKET && !(msg->msg_flags & MSG_EOR))
1046 return -EOPNOTSUPP;
1047
Jennifer Hunteac37312007-02-08 13:51:54 -08001048 lock_sock(sk);
1049
1050 if (sk->sk_shutdown & SEND_SHUTDOWN) {
1051 err = -EPIPE;
1052 goto out;
1053 }
1054
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001055 /* Return if the socket is not in connected state */
1056 if (sk->sk_state != IUCV_CONNECTED) {
1057 err = -ENOTCONN;
1058 goto out;
1059 }
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001060
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001061 /* initialize defaults */
1062 cmsg_done = 0; /* check for duplicate headers */
1063 txmsg.class = 0;
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001064
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001065 /* iterate over control messages */
1066 for (cmsg = CMSG_FIRSTHDR(msg); cmsg;
1067 cmsg = CMSG_NXTHDR(msg, cmsg)) {
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001068
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001069 if (!CMSG_OK(msg, cmsg)) {
1070 err = -EINVAL;
Jennifer Hunt561e0362007-05-04 12:22:07 -07001071 goto out;
Jennifer Hunteac37312007-02-08 13:51:54 -08001072 }
1073
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001074 if (cmsg->cmsg_level != SOL_IUCV)
1075 continue;
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001076
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001077 if (cmsg->cmsg_type & cmsg_done) {
1078 err = -EINVAL;
1079 goto out;
1080 }
1081 cmsg_done |= cmsg->cmsg_type;
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001082
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001083 switch (cmsg->cmsg_type) {
1084 case SCM_IUCV_TRGCLS:
1085 if (cmsg->cmsg_len != CMSG_LEN(TRGCLS_SIZE)) {
1086 err = -EINVAL;
1087 goto out;
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001088 }
1089
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001090 /* set iucv message target class */
1091 memcpy(&txmsg.class,
1092 (void *) CMSG_DATA(cmsg), TRGCLS_SIZE);
1093
1094 break;
1095
1096 default:
1097 err = -EINVAL;
1098 goto out;
1099 break;
1100 }
1101 }
1102
1103 /* allocate one skb for each iucv message:
1104 * this is fine for SOCK_SEQPACKET (unless we want to support
1105 * segmented records using the MSG_EOR flag), but
1106 * for SOCK_STREAM we might want to improve it in future */
Ursula Braun3881ac42011-08-08 01:33:54 +00001107 if (iucv->transport == AF_IUCV_TRANS_HIPER)
1108 skb = sock_alloc_send_skb(sk,
1109 len + sizeof(struct af_iucv_trans_hdr) + ETH_HLEN,
1110 noblock, &err);
1111 else
1112 skb = sock_alloc_send_skb(sk, len, noblock, &err);
Ursula Braun800c5eb2012-02-08 00:19:47 +00001113 if (!skb) {
1114 err = -ENOMEM;
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001115 goto out;
Ursula Braun800c5eb2012-02-08 00:19:47 +00001116 }
Ursula Braun3881ac42011-08-08 01:33:54 +00001117 if (iucv->transport == AF_IUCV_TRANS_HIPER)
1118 skb_reserve(skb, sizeof(struct af_iucv_trans_hdr) + ETH_HLEN);
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001119 if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
1120 err = -EFAULT;
1121 goto fail;
1122 }
1123
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +00001124 /* wait if outstanding messages for iucv path has reached */
1125 timeo = sock_sndtimeo(sk, noblock);
1126 err = iucv_sock_wait(sk, iucv_below_msglim(sk), timeo);
1127 if (err)
1128 goto fail;
1129
1130 /* return -ECONNRESET if the socket is no longer connected */
1131 if (sk->sk_state != IUCV_CONNECTED) {
1132 err = -ECONNRESET;
1133 goto fail;
1134 }
1135
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001136 /* increment and save iucv message tag for msg_completion cbk */
1137 txmsg.tag = iucv->send_tag++;
Ursula Braunf9c41a62013-04-07 22:19:26 +00001138 IUCV_SKB_CB(skb)->tag = txmsg.tag;
Ursula Braun800c5eb2012-02-08 00:19:47 +00001139
Ursula Braun3881ac42011-08-08 01:33:54 +00001140 if (iucv->transport == AF_IUCV_TRANS_HIPER) {
1141 atomic_inc(&iucv->msg_sent);
1142 err = afiucv_hs_send(&txmsg, sk, skb, 0);
1143 if (err) {
1144 atomic_dec(&iucv->msg_sent);
1145 goto fail;
1146 }
1147 goto release;
1148 }
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001149 skb_queue_tail(&iucv->send_skb_q, skb);
1150
1151 if (((iucv->path->flags & IUCV_IPRMDATA) & iucv->flags)
1152 && skb->len <= 7) {
1153 err = iucv_send_iprm(iucv->path, &txmsg, skb);
1154
1155 /* on success: there is no message_complete callback
1156 * for an IPRMDATA msg; remove skb from send queue */
1157 if (err == 0) {
1158 skb_unlink(skb, &iucv->send_skb_q);
1159 kfree_skb(skb);
1160 }
1161
1162 /* this error should never happen since the
1163 * IUCV_IPRMDATA path flag is set... sever path */
1164 if (err == 0x15) {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001165 pr_iucv->path_sever(iucv->path, NULL);
Jennifer Hunteac37312007-02-08 13:51:54 -08001166 skb_unlink(skb, &iucv->send_skb_q);
1167 err = -EPIPE;
1168 goto fail;
1169 }
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001170 } else
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001171 err = pr_iucv->message_send(iucv->path, &txmsg, 0, 0,
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001172 (void *) skb->data, skb->len);
1173 if (err) {
1174 if (err == 3) {
1175 user_id[8] = 0;
1176 memcpy(user_id, iucv->dst_user_id, 8);
1177 appl_id[8] = 0;
1178 memcpy(appl_id, iucv->dst_name, 8);
1179 pr_err("Application %s on z/VM guest %s"
1180 " exceeds message limit\n",
1181 appl_id, user_id);
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +00001182 err = -EAGAIN;
1183 } else
1184 err = -EPIPE;
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001185 skb_unlink(skb, &iucv->send_skb_q);
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001186 goto fail;
Jennifer Hunteac37312007-02-08 13:51:54 -08001187 }
1188
Ursula Braun3881ac42011-08-08 01:33:54 +00001189release:
Jennifer Hunteac37312007-02-08 13:51:54 -08001190 release_sock(sk);
1191 return len;
1192
1193fail:
1194 kfree_skb(skb);
1195out:
1196 release_sock(sk);
1197 return err;
1198}
1199
Hendrik Bruecknerbf95d202009-09-16 04:37:28 +00001200/* iucv_fragment_skb() - Fragment a single IUCV message into multiple skb's
1201 *
1202 * Locking: must be called with message_q.lock held
1203 */
Ursula Braunf0703c82007-10-08 02:03:31 -07001204static int iucv_fragment_skb(struct sock *sk, struct sk_buff *skb, int len)
1205{
1206 int dataleft, size, copied = 0;
1207 struct sk_buff *nskb;
1208
1209 dataleft = len;
1210 while (dataleft) {
1211 if (dataleft >= sk->sk_rcvbuf / 4)
1212 size = sk->sk_rcvbuf / 4;
1213 else
1214 size = dataleft;
1215
1216 nskb = alloc_skb(size, GFP_ATOMIC | GFP_DMA);
1217 if (!nskb)
1218 return -ENOMEM;
1219
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001220 /* copy target class to control buffer of new skb */
Ursula Braunf9c41a62013-04-07 22:19:26 +00001221 IUCV_SKB_CB(nskb)->class = IUCV_SKB_CB(skb)->class;
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001222
1223 /* copy data fragment */
Ursula Braunf0703c82007-10-08 02:03:31 -07001224 memcpy(nskb->data, skb->data + copied, size);
1225 copied += size;
1226 dataleft -= size;
1227
1228 skb_reset_transport_header(nskb);
1229 skb_reset_network_header(nskb);
1230 nskb->len = size;
1231
1232 skb_queue_tail(&iucv_sk(sk)->backlog_skb_q, nskb);
1233 }
1234
1235 return 0;
1236}
1237
Hendrik Bruecknerbf95d202009-09-16 04:37:28 +00001238/* iucv_process_message() - Receive a single outstanding IUCV message
1239 *
1240 * Locking: must be called with message_q.lock held
1241 */
Ursula Braunf0703c82007-10-08 02:03:31 -07001242static void iucv_process_message(struct sock *sk, struct sk_buff *skb,
1243 struct iucv_path *path,
1244 struct iucv_message *msg)
1245{
1246 int rc;
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001247 unsigned int len;
Ursula Braunf0703c82007-10-08 02:03:31 -07001248
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001249 len = iucv_msg_length(msg);
1250
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001251 /* store msg target class in the second 4 bytes of skb ctrl buffer */
1252 /* Note: the first 4 bytes are reserved for msg tag */
Ursula Braunf9c41a62013-04-07 22:19:26 +00001253 IUCV_SKB_CB(skb)->class = msg->class;
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001254
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001255 /* check for special IPRM messages (e.g. iucv_sock_shutdown) */
1256 if ((msg->flags & IUCV_IPRMDATA) && len > 7) {
1257 if (memcmp(msg->rmmsg, iprm_shutdown, 8) == 0) {
1258 skb->data = NULL;
1259 skb->len = 0;
1260 }
Ursula Braunf0703c82007-10-08 02:03:31 -07001261 } else {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001262 rc = pr_iucv->message_receive(path, msg,
1263 msg->flags & IUCV_IPRMDATA,
1264 skb->data, len, NULL);
Ursula Braunf0703c82007-10-08 02:03:31 -07001265 if (rc) {
1266 kfree_skb(skb);
1267 return;
1268 }
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001269 /* we need to fragment iucv messages for SOCK_STREAM only;
1270 * for SOCK_SEQPACKET, it is only relevant if we support
1271 * record segmentation using MSG_EOR (see also recvmsg()) */
1272 if (sk->sk_type == SOCK_STREAM &&
1273 skb->truesize >= sk->sk_rcvbuf / 4) {
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001274 rc = iucv_fragment_skb(sk, skb, len);
Ursula Braunf0703c82007-10-08 02:03:31 -07001275 kfree_skb(skb);
1276 skb = NULL;
1277 if (rc) {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001278 pr_iucv->path_sever(path, NULL);
Ursula Braunf0703c82007-10-08 02:03:31 -07001279 return;
1280 }
1281 skb = skb_dequeue(&iucv_sk(sk)->backlog_skb_q);
1282 } else {
1283 skb_reset_transport_header(skb);
1284 skb_reset_network_header(skb);
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001285 skb->len = len;
Ursula Braunf0703c82007-10-08 02:03:31 -07001286 }
1287 }
1288
Ursula Braunf9c41a62013-04-07 22:19:26 +00001289 IUCV_SKB_CB(skb)->offset = 0;
Ursula Braunf0703c82007-10-08 02:03:31 -07001290 if (sock_queue_rcv_skb(sk, skb))
1291 skb_queue_head(&iucv_sk(sk)->backlog_skb_q, skb);
1292}
1293
Hendrik Bruecknerbf95d202009-09-16 04:37:28 +00001294/* iucv_process_message_q() - Process outstanding IUCV messages
1295 *
1296 * Locking: must be called with message_q.lock held
1297 */
Ursula Braunf0703c82007-10-08 02:03:31 -07001298static void iucv_process_message_q(struct sock *sk)
1299{
1300 struct iucv_sock *iucv = iucv_sk(sk);
1301 struct sk_buff *skb;
1302 struct sock_msg_q *p, *n;
1303
1304 list_for_each_entry_safe(p, n, &iucv->message_q.list, list) {
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001305 skb = alloc_skb(iucv_msg_length(&p->msg), GFP_ATOMIC | GFP_DMA);
Ursula Braunf0703c82007-10-08 02:03:31 -07001306 if (!skb)
1307 break;
1308 iucv_process_message(sk, skb, p->path, &p->msg);
1309 list_del(&p->list);
1310 kfree(p);
1311 if (!skb_queue_empty(&iucv->backlog_skb_q))
1312 break;
1313 }
1314}
1315
Jennifer Hunteac37312007-02-08 13:51:54 -08001316static int iucv_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
1317 struct msghdr *msg, size_t len, int flags)
1318{
1319 int noblock = flags & MSG_DONTWAIT;
1320 struct sock *sk = sock->sk;
Jennifer Hunt561e0362007-05-04 12:22:07 -07001321 struct iucv_sock *iucv = iucv_sk(sk);
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001322 unsigned int copied, rlen;
Ursula Braun9fbd87d2012-03-07 02:06:23 +00001323 struct sk_buff *skb, *rskb, *cskb;
Jennifer Hunteac37312007-02-08 13:51:54 -08001324 int err = 0;
Ursula Braunf9c41a62013-04-07 22:19:26 +00001325 u32 offset;
Jennifer Hunteac37312007-02-08 13:51:54 -08001326
Ursula Braunaac63992011-12-19 22:56:31 +00001327 if ((sk->sk_state == IUCV_DISCONN) &&
Ursula Braunf0703c82007-10-08 02:03:31 -07001328 skb_queue_empty(&iucv->backlog_skb_q) &&
1329 skb_queue_empty(&sk->sk_receive_queue) &&
1330 list_empty(&iucv->message_q.list))
Jennifer Hunt561e0362007-05-04 12:22:07 -07001331 return 0;
1332
Jennifer Hunteac37312007-02-08 13:51:54 -08001333 if (flags & (MSG_OOB))
1334 return -EOPNOTSUPP;
1335
Hendrik Brueckner60d37052009-04-21 06:04:21 +00001336 /* receive/dequeue next skb:
1337 * the function understands MSG_PEEK and, thus, does not dequeue skb */
Jennifer Hunteac37312007-02-08 13:51:54 -08001338 skb = skb_recv_datagram(sk, flags, noblock, &err);
1339 if (!skb) {
1340 if (sk->sk_shutdown & RCV_SHUTDOWN)
1341 return 0;
1342 return err;
1343 }
1344
Ursula Braunf9c41a62013-04-07 22:19:26 +00001345 offset = IUCV_SKB_CB(skb)->offset;
1346 rlen = skb->len - offset; /* real length of skb */
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001347 copied = min_t(unsigned int, rlen, len);
Ursula Braun82492a32012-03-07 02:06:24 +00001348 if (!rlen)
1349 sk->sk_shutdown = sk->sk_shutdown | RCV_SHUTDOWN;
Jennifer Hunteac37312007-02-08 13:51:54 -08001350
Jennifer Hunt561e0362007-05-04 12:22:07 -07001351 cskb = skb;
Ursula Braunf9c41a62013-04-07 22:19:26 +00001352 if (skb_copy_datagram_iovec(cskb, offset, msg->msg_iov, copied)) {
Hendrik Brueckner802788b2009-04-21 23:26:26 +00001353 if (!(flags & MSG_PEEK))
1354 skb_queue_head(&sk->sk_receive_queue, skb);
1355 return -EFAULT;
Jennifer Hunteac37312007-02-08 13:51:54 -08001356 }
1357
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001358 /* SOCK_SEQPACKET: set MSG_TRUNC if recv buf size is too small */
1359 if (sk->sk_type == SOCK_SEQPACKET) {
1360 if (copied < rlen)
1361 msg->msg_flags |= MSG_TRUNC;
1362 /* each iucv message contains a complete record */
1363 msg->msg_flags |= MSG_EOR;
1364 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001365
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001366 /* create control message to store iucv msg target class:
1367 * get the trgcls from the control buffer of the skb due to
1368 * fragmentation of original iucv message. */
1369 err = put_cmsg(msg, SOL_IUCV, SCM_IUCV_TRGCLS,
Ursula Braunf9c41a62013-04-07 22:19:26 +00001370 sizeof(IUCV_SKB_CB(skb)->class),
1371 (void *)&IUCV_SKB_CB(skb)->class);
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001372 if (err) {
1373 if (!(flags & MSG_PEEK))
1374 skb_queue_head(&sk->sk_receive_queue, skb);
1375 return err;
1376 }
1377
Jennifer Hunteac37312007-02-08 13:51:54 -08001378 /* Mark read part of skb as used */
1379 if (!(flags & MSG_PEEK)) {
Jennifer Hunteac37312007-02-08 13:51:54 -08001380
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001381 /* SOCK_STREAM: re-queue skb if it contains unreceived data */
1382 if (sk->sk_type == SOCK_STREAM) {
Ursula Braunf9c41a62013-04-07 22:19:26 +00001383 if (copied < rlen) {
1384 IUCV_SKB_CB(skb)->offset = offset + copied;
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001385 goto done;
1386 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001387 }
1388
1389 kfree_skb(skb);
Ursula Braun800c5eb2012-02-08 00:19:47 +00001390 if (iucv->transport == AF_IUCV_TRANS_HIPER) {
1391 atomic_inc(&iucv->msg_recv);
1392 if (atomic_read(&iucv->msg_recv) > iucv->msglimit) {
1393 WARN_ON(1);
1394 iucv_sock_close(sk);
1395 return -EFAULT;
1396 }
1397 }
Jennifer Hunt561e0362007-05-04 12:22:07 -07001398
1399 /* Queue backlog skbs */
Hendrik Bruecknerbf95d202009-09-16 04:37:28 +00001400 spin_lock_bh(&iucv->message_q.lock);
Ursula Braunf0703c82007-10-08 02:03:31 -07001401 rskb = skb_dequeue(&iucv->backlog_skb_q);
Heiko Carstensda99f052007-05-04 12:23:27 -07001402 while (rskb) {
Ursula Braunf9c41a62013-04-07 22:19:26 +00001403 IUCV_SKB_CB(rskb)->offset = 0;
Jennifer Hunt561e0362007-05-04 12:22:07 -07001404 if (sock_queue_rcv_skb(sk, rskb)) {
Ursula Braunf0703c82007-10-08 02:03:31 -07001405 skb_queue_head(&iucv->backlog_skb_q,
Jennifer Hunt561e0362007-05-04 12:22:07 -07001406 rskb);
1407 break;
1408 } else {
Ursula Braunf0703c82007-10-08 02:03:31 -07001409 rskb = skb_dequeue(&iucv->backlog_skb_q);
Jennifer Hunt561e0362007-05-04 12:22:07 -07001410 }
1411 }
Ursula Braunf0703c82007-10-08 02:03:31 -07001412 if (skb_queue_empty(&iucv->backlog_skb_q)) {
Ursula Braunf0703c82007-10-08 02:03:31 -07001413 if (!list_empty(&iucv->message_q.list))
1414 iucv_process_message_q(sk);
Ursula Braun3881ac42011-08-08 01:33:54 +00001415 if (atomic_read(&iucv->msg_recv) >=
1416 iucv->msglimit / 2) {
Ursula Braun9fbd87d2012-03-07 02:06:23 +00001417 err = iucv_send_ctrl(sk, AF_IUCV_FLAG_WIN);
Ursula Braun3881ac42011-08-08 01:33:54 +00001418 if (err) {
1419 sk->sk_state = IUCV_DISCONN;
1420 sk->sk_state_change(sk);
1421 }
1422 }
Ursula Braunf0703c82007-10-08 02:03:31 -07001423 }
Hendrik Bruecknerbf95d202009-09-16 04:37:28 +00001424 spin_unlock_bh(&iucv->message_q.lock);
Hendrik Brueckner60d37052009-04-21 06:04:21 +00001425 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001426
1427done:
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001428 /* SOCK_SEQPACKET: return real length if MSG_TRUNC is set */
1429 if (sk->sk_type == SOCK_SEQPACKET && (flags & MSG_TRUNC))
1430 copied = rlen;
1431
1432 return copied;
Jennifer Hunteac37312007-02-08 13:51:54 -08001433}
1434
1435static inline unsigned int iucv_accept_poll(struct sock *parent)
1436{
1437 struct iucv_sock *isk, *n;
1438 struct sock *sk;
1439
Heiko Carstensda99f052007-05-04 12:23:27 -07001440 list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) {
Jennifer Hunteac37312007-02-08 13:51:54 -08001441 sk = (struct sock *) isk;
1442
1443 if (sk->sk_state == IUCV_CONNECTED)
1444 return POLLIN | POLLRDNORM;
1445 }
1446
1447 return 0;
1448}
1449
1450unsigned int iucv_sock_poll(struct file *file, struct socket *sock,
1451 poll_table *wait)
1452{
1453 struct sock *sk = sock->sk;
1454 unsigned int mask = 0;
1455
Eric Dumazetaa395142010-04-20 13:03:51 +00001456 sock_poll_wait(file, sk_sleep(sk), wait);
Jennifer Hunteac37312007-02-08 13:51:54 -08001457
1458 if (sk->sk_state == IUCV_LISTEN)
1459 return iucv_accept_poll(sk);
1460
1461 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
Keller, Jacob E7d4c04f2013-03-28 11:19:25 +00001462 mask |= POLLERR |
Jacob Keller8facd5f2013-04-02 13:55:40 -07001463 (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? POLLPRI : 0);
Jennifer Hunteac37312007-02-08 13:51:54 -08001464
1465 if (sk->sk_shutdown & RCV_SHUTDOWN)
1466 mask |= POLLRDHUP;
1467
1468 if (sk->sk_shutdown == SHUTDOWN_MASK)
1469 mask |= POLLHUP;
1470
1471 if (!skb_queue_empty(&sk->sk_receive_queue) ||
Heiko Carstensda99f052007-05-04 12:23:27 -07001472 (sk->sk_shutdown & RCV_SHUTDOWN))
Jennifer Hunteac37312007-02-08 13:51:54 -08001473 mask |= POLLIN | POLLRDNORM;
1474
1475 if (sk->sk_state == IUCV_CLOSED)
1476 mask |= POLLHUP;
1477
Ursula Braunaac63992011-12-19 22:56:31 +00001478 if (sk->sk_state == IUCV_DISCONN)
Jennifer Hunt561e0362007-05-04 12:22:07 -07001479 mask |= POLLIN;
1480
Ursula Braun7f1b0ea2012-02-08 00:19:46 +00001481 if (sock_writeable(sk) && iucv_below_msglim(sk))
Jennifer Hunteac37312007-02-08 13:51:54 -08001482 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
1483 else
1484 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
1485
1486 return mask;
1487}
1488
1489static int iucv_sock_shutdown(struct socket *sock, int how)
1490{
1491 struct sock *sk = sock->sk;
1492 struct iucv_sock *iucv = iucv_sk(sk);
1493 struct iucv_message txmsg;
1494 int err = 0;
Jennifer Hunteac37312007-02-08 13:51:54 -08001495
1496 how++;
1497
1498 if ((how & ~SHUTDOWN_MASK) || !how)
1499 return -EINVAL;
1500
1501 lock_sock(sk);
Heiko Carstensda99f052007-05-04 12:23:27 -07001502 switch (sk->sk_state) {
Ursula Braun82492a32012-03-07 02:06:24 +00001503 case IUCV_LISTEN:
Hendrik Bruecknere14ad5f2009-04-21 06:04:23 +00001504 case IUCV_DISCONN:
1505 case IUCV_CLOSING:
Jennifer Hunteac37312007-02-08 13:51:54 -08001506 case IUCV_CLOSED:
1507 err = -ENOTCONN;
1508 goto fail;
Jennifer Hunteac37312007-02-08 13:51:54 -08001509 default:
Jennifer Hunteac37312007-02-08 13:51:54 -08001510 break;
1511 }
1512
1513 if (how == SEND_SHUTDOWN || how == SHUTDOWN_MASK) {
Ursula Braun82492a32012-03-07 02:06:24 +00001514 if (iucv->transport == AF_IUCV_TRANS_IUCV) {
1515 txmsg.class = 0;
1516 txmsg.tag = 0;
1517 err = pr_iucv->message_send(iucv->path, &txmsg,
1518 IUCV_IPRMDATA, 0, (void *) iprm_shutdown, 8);
1519 if (err) {
1520 switch (err) {
1521 case 1:
1522 err = -ENOTCONN;
1523 break;
1524 case 2:
1525 err = -ECONNRESET;
1526 break;
1527 default:
1528 err = -ENOTCONN;
1529 break;
1530 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001531 }
Ursula Braun82492a32012-03-07 02:06:24 +00001532 } else
1533 iucv_send_ctrl(sk, AF_IUCV_FLAG_SHT);
Jennifer Hunteac37312007-02-08 13:51:54 -08001534 }
1535
Ursula Braun82492a32012-03-07 02:06:24 +00001536 sk->sk_shutdown |= how;
Jennifer Hunteac37312007-02-08 13:51:54 -08001537 if (how == RCV_SHUTDOWN || how == SHUTDOWN_MASK) {
Ursula Braun82492a32012-03-07 02:06:24 +00001538 if (iucv->transport == AF_IUCV_TRANS_IUCV) {
1539 err = pr_iucv->path_quiesce(iucv->path, NULL);
1540 if (err)
1541 err = -ENOTCONN;
1542/* skb_queue_purge(&sk->sk_receive_queue); */
1543 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001544 skb_queue_purge(&sk->sk_receive_queue);
1545 }
1546
1547 /* Wake up anyone sleeping in poll */
1548 sk->sk_state_change(sk);
1549
1550fail:
1551 release_sock(sk);
1552 return err;
1553}
1554
1555static int iucv_sock_release(struct socket *sock)
1556{
1557 struct sock *sk = sock->sk;
1558 int err = 0;
1559
1560 if (!sk)
1561 return 0;
1562
1563 iucv_sock_close(sk);
1564
Jennifer Hunteac37312007-02-08 13:51:54 -08001565 sock_orphan(sk);
1566 iucv_sock_kill(sk);
1567 return err;
1568}
1569
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00001570/* getsockopt and setsockopt */
1571static int iucv_sock_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07001572 char __user *optval, unsigned int optlen)
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00001573{
1574 struct sock *sk = sock->sk;
1575 struct iucv_sock *iucv = iucv_sk(sk);
1576 int val;
1577 int rc;
1578
1579 if (level != SOL_IUCV)
1580 return -ENOPROTOOPT;
1581
1582 if (optlen < sizeof(int))
1583 return -EINVAL;
1584
1585 if (get_user(val, (int __user *) optval))
1586 return -EFAULT;
1587
1588 rc = 0;
1589
1590 lock_sock(sk);
1591 switch (optname) {
1592 case SO_IPRMDATA_MSG:
1593 if (val)
1594 iucv->flags |= IUCV_IPRMDATA;
1595 else
1596 iucv->flags &= ~IUCV_IPRMDATA;
1597 break;
Hendrik Brueckner09488e2e2009-04-21 23:26:27 +00001598 case SO_MSGLIMIT:
1599 switch (sk->sk_state) {
1600 case IUCV_OPEN:
1601 case IUCV_BOUND:
1602 if (val < 1 || val > (u16)(~0))
1603 rc = -EINVAL;
1604 else
1605 iucv->msglimit = val;
1606 break;
1607 default:
1608 rc = -EINVAL;
1609 break;
1610 }
1611 break;
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00001612 default:
1613 rc = -ENOPROTOOPT;
1614 break;
1615 }
1616 release_sock(sk);
1617
1618 return rc;
1619}
1620
1621static int iucv_sock_getsockopt(struct socket *sock, int level, int optname,
1622 char __user *optval, int __user *optlen)
1623{
1624 struct sock *sk = sock->sk;
1625 struct iucv_sock *iucv = iucv_sk(sk);
Ursula Braun51363b82012-02-08 00:19:48 +00001626 unsigned int val;
1627 int len;
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00001628
1629 if (level != SOL_IUCV)
1630 return -ENOPROTOOPT;
1631
1632 if (get_user(len, optlen))
1633 return -EFAULT;
1634
1635 if (len < 0)
1636 return -EINVAL;
1637
1638 len = min_t(unsigned int, len, sizeof(int));
1639
1640 switch (optname) {
1641 case SO_IPRMDATA_MSG:
1642 val = (iucv->flags & IUCV_IPRMDATA) ? 1 : 0;
1643 break;
Hendrik Brueckner09488e2e2009-04-21 23:26:27 +00001644 case SO_MSGLIMIT:
1645 lock_sock(sk);
1646 val = (iucv->path != NULL) ? iucv->path->msglim /* connected */
1647 : iucv->msglimit; /* default */
1648 release_sock(sk);
1649 break;
Ursula Braun51363b82012-02-08 00:19:48 +00001650 case SO_MSGSIZE:
1651 if (sk->sk_state == IUCV_OPEN)
1652 return -EBADFD;
1653 val = (iucv->hs_dev) ? iucv->hs_dev->mtu -
1654 sizeof(struct af_iucv_trans_hdr) - ETH_HLEN :
1655 0x7fffffff;
1656 break;
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00001657 default:
1658 return -ENOPROTOOPT;
1659 }
1660
1661 if (put_user(len, optlen))
1662 return -EFAULT;
1663 if (copy_to_user(optval, &val, len))
1664 return -EFAULT;
1665
1666 return 0;
1667}
1668
1669
Jennifer Hunteac37312007-02-08 13:51:54 -08001670/* Callback wrappers - called from iucv base support */
1671static int iucv_callback_connreq(struct iucv_path *path,
1672 u8 ipvmid[8], u8 ipuser[16])
1673{
1674 unsigned char user_data[16];
1675 unsigned char nuser_data[16];
1676 unsigned char src_name[8];
Jennifer Hunteac37312007-02-08 13:51:54 -08001677 struct sock *sk, *nsk;
1678 struct iucv_sock *iucv, *niucv;
1679 int err;
1680
1681 memcpy(src_name, ipuser, 8);
1682 EBCASC(src_name, 8);
1683 /* Find out if this path belongs to af_iucv. */
1684 read_lock(&iucv_sk_list.lock);
1685 iucv = NULL;
Ursula Braunfebca282007-07-14 19:04:25 -07001686 sk = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001687 sk_for_each(sk, &iucv_sk_list.head)
Jennifer Hunteac37312007-02-08 13:51:54 -08001688 if (sk->sk_state == IUCV_LISTEN &&
1689 !memcmp(&iucv_sk(sk)->src_name, src_name, 8)) {
1690 /*
1691 * Found a listening socket with
1692 * src_name == ipuser[0-7].
1693 */
1694 iucv = iucv_sk(sk);
1695 break;
1696 }
1697 read_unlock(&iucv_sk_list.lock);
1698 if (!iucv)
1699 /* No socket found, not one of our paths. */
1700 return -EINVAL;
1701
1702 bh_lock_sock(sk);
1703
1704 /* Check if parent socket is listening */
1705 low_nmcpy(user_data, iucv->src_name);
1706 high_nmcpy(user_data, iucv->dst_name);
1707 ASCEBC(user_data, sizeof(user_data));
1708 if (sk->sk_state != IUCV_LISTEN) {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001709 err = pr_iucv->path_sever(path, user_data);
Hendrik Brueckner65dbd7c2009-01-05 18:08:23 -08001710 iucv_path_free(path);
Jennifer Hunteac37312007-02-08 13:51:54 -08001711 goto fail;
1712 }
1713
1714 /* Check for backlog size */
1715 if (sk_acceptq_is_full(sk)) {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001716 err = pr_iucv->path_sever(path, user_data);
Hendrik Brueckner65dbd7c2009-01-05 18:08:23 -08001717 iucv_path_free(path);
Jennifer Hunteac37312007-02-08 13:51:54 -08001718 goto fail;
1719 }
1720
1721 /* Create the new socket */
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001722 nsk = iucv_sock_alloc(NULL, sk->sk_type, GFP_ATOMIC);
Heiko Carstensda99f052007-05-04 12:23:27 -07001723 if (!nsk) {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001724 err = pr_iucv->path_sever(path, user_data);
Hendrik Brueckner65dbd7c2009-01-05 18:08:23 -08001725 iucv_path_free(path);
Jennifer Hunteac37312007-02-08 13:51:54 -08001726 goto fail;
1727 }
1728
1729 niucv = iucv_sk(nsk);
1730 iucv_sock_init(nsk, sk);
1731
1732 /* Set the new iucv_sock */
1733 memcpy(niucv->dst_name, ipuser + 8, 8);
1734 EBCASC(niucv->dst_name, 8);
1735 memcpy(niucv->dst_user_id, ipvmid, 8);
1736 memcpy(niucv->src_name, iucv->src_name, 8);
1737 memcpy(niucv->src_user_id, iucv->src_user_id, 8);
1738 niucv->path = path;
1739
1740 /* Call iucv_accept */
1741 high_nmcpy(nuser_data, ipuser + 8);
1742 memcpy(nuser_data + 8, niucv->src_name, 8);
1743 ASCEBC(nuser_data + 8, 8);
1744
Hendrik Brueckner09488e2e2009-04-21 23:26:27 +00001745 /* set message limit for path based on msglimit of accepting socket */
1746 niucv->msglimit = iucv->msglimit;
1747 path->msglim = iucv->msglimit;
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001748 err = pr_iucv->path_accept(path, &af_iucv_handler, nuser_data, nsk);
Heiko Carstensda99f052007-05-04 12:23:27 -07001749 if (err) {
Ursula Braun7d316b92012-02-08 00:19:45 +00001750 iucv_sever_path(nsk, 1);
Hendrik Brueckner65dbd7c2009-01-05 18:08:23 -08001751 iucv_sock_kill(nsk);
Jennifer Hunteac37312007-02-08 13:51:54 -08001752 goto fail;
1753 }
1754
1755 iucv_accept_enqueue(sk, nsk);
1756
1757 /* Wake up accept */
1758 nsk->sk_state = IUCV_CONNECTED;
1759 sk->sk_data_ready(sk, 1);
1760 err = 0;
1761fail:
1762 bh_unlock_sock(sk);
1763 return 0;
1764}
1765
1766static void iucv_callback_connack(struct iucv_path *path, u8 ipuser[16])
1767{
1768 struct sock *sk = path->private;
1769
1770 sk->sk_state = IUCV_CONNECTED;
1771 sk->sk_state_change(sk);
1772}
1773
1774static void iucv_callback_rx(struct iucv_path *path, struct iucv_message *msg)
1775{
1776 struct sock *sk = path->private;
Jennifer Hunt561e0362007-05-04 12:22:07 -07001777 struct iucv_sock *iucv = iucv_sk(sk);
Ursula Braunf0703c82007-10-08 02:03:31 -07001778 struct sk_buff *skb;
1779 struct sock_msg_q *save_msg;
1780 int len;
Jennifer Hunt561e0362007-05-04 12:22:07 -07001781
Hendrik Bruecknerfe86e542009-04-21 06:04:22 +00001782 if (sk->sk_shutdown & RCV_SHUTDOWN) {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001783 pr_iucv->message_reject(path, msg);
Jennifer Hunteac37312007-02-08 13:51:54 -08001784 return;
Hendrik Bruecknerfe86e542009-04-21 06:04:22 +00001785 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001786
Hendrik Brueckner3fa6b5a2009-04-21 06:04:24 +00001787 spin_lock(&iucv->message_q.lock);
Jennifer Hunteac37312007-02-08 13:51:54 -08001788
Ursula Braunf0703c82007-10-08 02:03:31 -07001789 if (!list_empty(&iucv->message_q.list) ||
1790 !skb_queue_empty(&iucv->backlog_skb_q))
1791 goto save_message;
1792
1793 len = atomic_read(&sk->sk_rmem_alloc);
Eric Dumazet87fb4b72011-10-13 07:28:54 +00001794 len += SKB_TRUESIZE(iucv_msg_length(msg));
Ursula Braunf0703c82007-10-08 02:03:31 -07001795 if (len > sk->sk_rcvbuf)
1796 goto save_message;
1797
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001798 skb = alloc_skb(iucv_msg_length(msg), GFP_ATOMIC | GFP_DMA);
Ursula Braunf0703c82007-10-08 02:03:31 -07001799 if (!skb)
1800 goto save_message;
Jennifer Hunteac37312007-02-08 13:51:54 -08001801
Ursula Braunf0703c82007-10-08 02:03:31 -07001802 iucv_process_message(sk, skb, path, msg);
Hendrik Brueckner3fa6b5a2009-04-21 06:04:24 +00001803 goto out_unlock;
Jennifer Hunteac37312007-02-08 13:51:54 -08001804
Ursula Braunf0703c82007-10-08 02:03:31 -07001805save_message:
1806 save_msg = kzalloc(sizeof(struct sock_msg_q), GFP_ATOMIC | GFP_DMA);
Ursula Braund4444722008-02-07 18:07:19 -08001807 if (!save_msg)
Julia Lawalla56635a2010-05-26 05:56:48 +00001808 goto out_unlock;
Ursula Braunf0703c82007-10-08 02:03:31 -07001809 save_msg->path = path;
1810 save_msg->msg = *msg;
1811
Ursula Braunf0703c82007-10-08 02:03:31 -07001812 list_add_tail(&save_msg->list, &iucv->message_q.list);
Hendrik Brueckner3fa6b5a2009-04-21 06:04:24 +00001813
1814out_unlock:
Ursula Braunf0703c82007-10-08 02:03:31 -07001815 spin_unlock(&iucv->message_q.lock);
Jennifer Hunteac37312007-02-08 13:51:54 -08001816}
1817
1818static void iucv_callback_txdone(struct iucv_path *path,
1819 struct iucv_message *msg)
1820{
1821 struct sock *sk = path->private;
Ursula Braunf2a77992008-02-07 18:07:44 -08001822 struct sk_buff *this = NULL;
Jennifer Hunteac37312007-02-08 13:51:54 -08001823 struct sk_buff_head *list = &iucv_sk(sk)->send_skb_q;
1824 struct sk_buff *list_skb = list->next;
1825 unsigned long flags;
1826
Ursula Braun7d316b92012-02-08 00:19:45 +00001827 bh_lock_sock(sk);
Ursula Braunf2a77992008-02-07 18:07:44 -08001828 if (!skb_queue_empty(list)) {
Jennifer Hunt561e0362007-05-04 12:22:07 -07001829 spin_lock_irqsave(&list->lock, flags);
Jennifer Hunteac37312007-02-08 13:51:54 -08001830
Ursula Braunf2a77992008-02-07 18:07:44 -08001831 while (list_skb != (struct sk_buff *)list) {
Ursula Braunf9c41a62013-04-07 22:19:26 +00001832 if (msg->tag != IUCV_SKB_CB(list_skb)->tag) {
Ursula Braunf2a77992008-02-07 18:07:44 -08001833 this = list_skb;
1834 break;
1835 }
Jennifer Hunt561e0362007-05-04 12:22:07 -07001836 list_skb = list_skb->next;
Ursula Braunf2a77992008-02-07 18:07:44 -08001837 }
1838 if (this)
1839 __skb_unlink(this, list);
Jennifer Hunteac37312007-02-08 13:51:54 -08001840
Jennifer Hunt561e0362007-05-04 12:22:07 -07001841 spin_unlock_irqrestore(&list->lock, flags);
Jennifer Hunteac37312007-02-08 13:51:54 -08001842
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +00001843 if (this) {
1844 kfree_skb(this);
1845 /* wake up any process waiting for sending */
1846 iucv_sock_wake_msglim(sk);
1847 }
Jennifer Hunt561e0362007-05-04 12:22:07 -07001848 }
1849
Heiko Carstensda99f052007-05-04 12:23:27 -07001850 if (sk->sk_state == IUCV_CLOSING) {
Jennifer Hunt561e0362007-05-04 12:22:07 -07001851 if (skb_queue_empty(&iucv_sk(sk)->send_skb_q)) {
1852 sk->sk_state = IUCV_CLOSED;
1853 sk->sk_state_change(sk);
1854 }
1855 }
Ursula Braun7d316b92012-02-08 00:19:45 +00001856 bh_unlock_sock(sk);
Jennifer Hunt561e0362007-05-04 12:22:07 -07001857
Jennifer Hunteac37312007-02-08 13:51:54 -08001858}
1859
1860static void iucv_callback_connrej(struct iucv_path *path, u8 ipuser[16])
1861{
1862 struct sock *sk = path->private;
1863
Ursula Braun7d316b92012-02-08 00:19:45 +00001864 if (sk->sk_state == IUCV_CLOSED)
1865 return;
1866
1867 bh_lock_sock(sk);
1868 iucv_sever_path(sk, 1);
Ursula Braunaac63992011-12-19 22:56:31 +00001869 sk->sk_state = IUCV_DISCONN;
Jennifer Hunteac37312007-02-08 13:51:54 -08001870
1871 sk->sk_state_change(sk);
Ursula Braun7d316b92012-02-08 00:19:45 +00001872 bh_unlock_sock(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -08001873}
1874
Hendrik Brueckneraf88b522009-04-21 23:26:21 +00001875/* called if the other communication side shuts down its RECV direction;
1876 * in turn, the callback sets SEND_SHUTDOWN to disable sending of data.
1877 */
1878static void iucv_callback_shutdown(struct iucv_path *path, u8 ipuser[16])
1879{
1880 struct sock *sk = path->private;
1881
1882 bh_lock_sock(sk);
1883 if (sk->sk_state != IUCV_CLOSED) {
1884 sk->sk_shutdown |= SEND_SHUTDOWN;
1885 sk->sk_state_change(sk);
1886 }
1887 bh_unlock_sock(sk);
1888}
1889
Ursula Braun3881ac42011-08-08 01:33:54 +00001890/***************** HiperSockets transport callbacks ********************/
1891static void afiucv_swap_src_dest(struct sk_buff *skb)
1892{
1893 struct af_iucv_trans_hdr *trans_hdr =
1894 (struct af_iucv_trans_hdr *)skb->data;
1895 char tmpID[8];
1896 char tmpName[8];
1897
1898 ASCEBC(trans_hdr->destUserID, sizeof(trans_hdr->destUserID));
1899 ASCEBC(trans_hdr->destAppName, sizeof(trans_hdr->destAppName));
1900 ASCEBC(trans_hdr->srcUserID, sizeof(trans_hdr->srcUserID));
1901 ASCEBC(trans_hdr->srcAppName, sizeof(trans_hdr->srcAppName));
1902 memcpy(tmpID, trans_hdr->srcUserID, 8);
1903 memcpy(tmpName, trans_hdr->srcAppName, 8);
1904 memcpy(trans_hdr->srcUserID, trans_hdr->destUserID, 8);
1905 memcpy(trans_hdr->srcAppName, trans_hdr->destAppName, 8);
1906 memcpy(trans_hdr->destUserID, tmpID, 8);
1907 memcpy(trans_hdr->destAppName, tmpName, 8);
1908 skb_push(skb, ETH_HLEN);
1909 memset(skb->data, 0, ETH_HLEN);
1910}
1911
1912/**
1913 * afiucv_hs_callback_syn - react on received SYN
1914 **/
1915static int afiucv_hs_callback_syn(struct sock *sk, struct sk_buff *skb)
1916{
1917 struct sock *nsk;
1918 struct iucv_sock *iucv, *niucv;
1919 struct af_iucv_trans_hdr *trans_hdr;
1920 int err;
1921
1922 iucv = iucv_sk(sk);
1923 trans_hdr = (struct af_iucv_trans_hdr *)skb->data;
1924 if (!iucv) {
1925 /* no sock - connection refused */
1926 afiucv_swap_src_dest(skb);
1927 trans_hdr->flags = AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN;
1928 err = dev_queue_xmit(skb);
1929 goto out;
1930 }
1931
1932 nsk = iucv_sock_alloc(NULL, sk->sk_type, GFP_ATOMIC);
1933 bh_lock_sock(sk);
1934 if ((sk->sk_state != IUCV_LISTEN) ||
1935 sk_acceptq_is_full(sk) ||
1936 !nsk) {
1937 /* error on server socket - connection refused */
1938 if (nsk)
1939 sk_free(nsk);
1940 afiucv_swap_src_dest(skb);
1941 trans_hdr->flags = AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN;
1942 err = dev_queue_xmit(skb);
1943 bh_unlock_sock(sk);
1944 goto out;
1945 }
1946
1947 niucv = iucv_sk(nsk);
1948 iucv_sock_init(nsk, sk);
1949 niucv->transport = AF_IUCV_TRANS_HIPER;
1950 niucv->msglimit = iucv->msglimit;
1951 if (!trans_hdr->window)
1952 niucv->msglimit_peer = IUCV_HIPER_MSGLIM_DEFAULT;
1953 else
1954 niucv->msglimit_peer = trans_hdr->window;
1955 memcpy(niucv->dst_name, trans_hdr->srcAppName, 8);
1956 memcpy(niucv->dst_user_id, trans_hdr->srcUserID, 8);
1957 memcpy(niucv->src_name, iucv->src_name, 8);
1958 memcpy(niucv->src_user_id, iucv->src_user_id, 8);
1959 nsk->sk_bound_dev_if = sk->sk_bound_dev_if;
Ursula Braun800c5eb2012-02-08 00:19:47 +00001960 niucv->hs_dev = iucv->hs_dev;
1961 dev_hold(niucv->hs_dev);
Ursula Braun3881ac42011-08-08 01:33:54 +00001962 afiucv_swap_src_dest(skb);
1963 trans_hdr->flags = AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_ACK;
1964 trans_hdr->window = niucv->msglimit;
1965 /* if receiver acks the xmit connection is established */
1966 err = dev_queue_xmit(skb);
1967 if (!err) {
1968 iucv_accept_enqueue(sk, nsk);
1969 nsk->sk_state = IUCV_CONNECTED;
1970 sk->sk_data_ready(sk, 1);
1971 } else
1972 iucv_sock_kill(nsk);
1973 bh_unlock_sock(sk);
1974
1975out:
1976 return NET_RX_SUCCESS;
1977}
1978
1979/**
1980 * afiucv_hs_callback_synack() - react on received SYN-ACK
1981 **/
1982static int afiucv_hs_callback_synack(struct sock *sk, struct sk_buff *skb)
1983{
1984 struct iucv_sock *iucv = iucv_sk(sk);
1985 struct af_iucv_trans_hdr *trans_hdr =
1986 (struct af_iucv_trans_hdr *)skb->data;
1987
1988 if (!iucv)
1989 goto out;
1990 if (sk->sk_state != IUCV_BOUND)
1991 goto out;
1992 bh_lock_sock(sk);
1993 iucv->msglimit_peer = trans_hdr->window;
1994 sk->sk_state = IUCV_CONNECTED;
1995 sk->sk_state_change(sk);
1996 bh_unlock_sock(sk);
1997out:
1998 kfree_skb(skb);
1999 return NET_RX_SUCCESS;
2000}
2001
2002/**
2003 * afiucv_hs_callback_synfin() - react on received SYN_FIN
2004 **/
2005static int afiucv_hs_callback_synfin(struct sock *sk, struct sk_buff *skb)
2006{
2007 struct iucv_sock *iucv = iucv_sk(sk);
2008
2009 if (!iucv)
2010 goto out;
2011 if (sk->sk_state != IUCV_BOUND)
2012 goto out;
2013 bh_lock_sock(sk);
2014 sk->sk_state = IUCV_DISCONN;
2015 sk->sk_state_change(sk);
2016 bh_unlock_sock(sk);
2017out:
2018 kfree_skb(skb);
2019 return NET_RX_SUCCESS;
2020}
2021
2022/**
2023 * afiucv_hs_callback_fin() - react on received FIN
2024 **/
2025static int afiucv_hs_callback_fin(struct sock *sk, struct sk_buff *skb)
2026{
2027 struct iucv_sock *iucv = iucv_sk(sk);
2028
2029 /* other end of connection closed */
Ursula Braun800c5eb2012-02-08 00:19:47 +00002030 if (!iucv)
2031 goto out;
2032 bh_lock_sock(sk);
2033 if (sk->sk_state == IUCV_CONNECTED) {
Ursula Braunaac63992011-12-19 22:56:31 +00002034 sk->sk_state = IUCV_DISCONN;
Ursula Braun3881ac42011-08-08 01:33:54 +00002035 sk->sk_state_change(sk);
Ursula Braun3881ac42011-08-08 01:33:54 +00002036 }
Ursula Braun800c5eb2012-02-08 00:19:47 +00002037 bh_unlock_sock(sk);
2038out:
Ursula Braun3881ac42011-08-08 01:33:54 +00002039 kfree_skb(skb);
2040 return NET_RX_SUCCESS;
2041}
2042
2043/**
2044 * afiucv_hs_callback_win() - react on received WIN
2045 **/
2046static int afiucv_hs_callback_win(struct sock *sk, struct sk_buff *skb)
2047{
2048 struct iucv_sock *iucv = iucv_sk(sk);
2049 struct af_iucv_trans_hdr *trans_hdr =
2050 (struct af_iucv_trans_hdr *)skb->data;
2051
2052 if (!iucv)
2053 return NET_RX_SUCCESS;
2054
2055 if (sk->sk_state != IUCV_CONNECTED)
2056 return NET_RX_SUCCESS;
2057
2058 atomic_sub(trans_hdr->window, &iucv->msg_sent);
2059 iucv_sock_wake_msglim(sk);
2060 return NET_RX_SUCCESS;
2061}
2062
2063/**
2064 * afiucv_hs_callback_rx() - react on received data
2065 **/
2066static int afiucv_hs_callback_rx(struct sock *sk, struct sk_buff *skb)
2067{
2068 struct iucv_sock *iucv = iucv_sk(sk);
2069
2070 if (!iucv) {
2071 kfree_skb(skb);
2072 return NET_RX_SUCCESS;
2073 }
2074
2075 if (sk->sk_state != IUCV_CONNECTED) {
2076 kfree_skb(skb);
2077 return NET_RX_SUCCESS;
2078 }
2079
Ursula Braun82492a32012-03-07 02:06:24 +00002080 if (sk->sk_shutdown & RCV_SHUTDOWN) {
2081 kfree_skb(skb);
2082 return NET_RX_SUCCESS;
2083 }
2084
Ursula Braun3881ac42011-08-08 01:33:54 +00002085 /* write stuff from iucv_msg to skb cb */
Ursula Braun82492a32012-03-07 02:06:24 +00002086 if (skb->len < sizeof(struct af_iucv_trans_hdr)) {
Ursula Braun3881ac42011-08-08 01:33:54 +00002087 kfree_skb(skb);
2088 return NET_RX_SUCCESS;
2089 }
2090 skb_pull(skb, sizeof(struct af_iucv_trans_hdr));
2091 skb_reset_transport_header(skb);
2092 skb_reset_network_header(skb);
Ursula Braunf9c41a62013-04-07 22:19:26 +00002093 IUCV_SKB_CB(skb)->offset = 0;
Ursula Braun3881ac42011-08-08 01:33:54 +00002094 spin_lock(&iucv->message_q.lock);
2095 if (skb_queue_empty(&iucv->backlog_skb_q)) {
2096 if (sock_queue_rcv_skb(sk, skb)) {
2097 /* handle rcv queue full */
2098 skb_queue_tail(&iucv->backlog_skb_q, skb);
2099 }
2100 } else
2101 skb_queue_tail(&iucv_sk(sk)->backlog_skb_q, skb);
2102 spin_unlock(&iucv->message_q.lock);
2103 return NET_RX_SUCCESS;
2104}
2105
2106/**
2107 * afiucv_hs_rcv() - base function for arriving data through HiperSockets
2108 * transport
2109 * called from netif RX softirq
2110 **/
2111static int afiucv_hs_rcv(struct sk_buff *skb, struct net_device *dev,
2112 struct packet_type *pt, struct net_device *orig_dev)
2113{
Ursula Braun3881ac42011-08-08 01:33:54 +00002114 struct sock *sk;
2115 struct iucv_sock *iucv;
2116 struct af_iucv_trans_hdr *trans_hdr;
2117 char nullstring[8];
2118 int err = 0;
2119
2120 skb_pull(skb, ETH_HLEN);
2121 trans_hdr = (struct af_iucv_trans_hdr *)skb->data;
2122 EBCASC(trans_hdr->destAppName, sizeof(trans_hdr->destAppName));
2123 EBCASC(trans_hdr->destUserID, sizeof(trans_hdr->destUserID));
2124 EBCASC(trans_hdr->srcAppName, sizeof(trans_hdr->srcAppName));
2125 EBCASC(trans_hdr->srcUserID, sizeof(trans_hdr->srcUserID));
2126 memset(nullstring, 0, sizeof(nullstring));
2127 iucv = NULL;
2128 sk = NULL;
2129 read_lock(&iucv_sk_list.lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08002130 sk_for_each(sk, &iucv_sk_list.head) {
Ursula Braun3881ac42011-08-08 01:33:54 +00002131 if (trans_hdr->flags == AF_IUCV_FLAG_SYN) {
2132 if ((!memcmp(&iucv_sk(sk)->src_name,
2133 trans_hdr->destAppName, 8)) &&
2134 (!memcmp(&iucv_sk(sk)->src_user_id,
2135 trans_hdr->destUserID, 8)) &&
2136 (!memcmp(&iucv_sk(sk)->dst_name, nullstring, 8)) &&
2137 (!memcmp(&iucv_sk(sk)->dst_user_id,
2138 nullstring, 8))) {
2139 iucv = iucv_sk(sk);
2140 break;
2141 }
2142 } else {
2143 if ((!memcmp(&iucv_sk(sk)->src_name,
2144 trans_hdr->destAppName, 8)) &&
2145 (!memcmp(&iucv_sk(sk)->src_user_id,
2146 trans_hdr->destUserID, 8)) &&
2147 (!memcmp(&iucv_sk(sk)->dst_name,
2148 trans_hdr->srcAppName, 8)) &&
2149 (!memcmp(&iucv_sk(sk)->dst_user_id,
2150 trans_hdr->srcUserID, 8))) {
2151 iucv = iucv_sk(sk);
2152 break;
2153 }
2154 }
2155 }
2156 read_unlock(&iucv_sk_list.lock);
2157 if (!iucv)
2158 sk = NULL;
2159
2160 /* no sock
2161 how should we send with no sock
2162 1) send without sock no send rc checking?
2163 2) introduce default sock to handle this cases
2164
2165 SYN -> send SYN|ACK in good case, send SYN|FIN in bad case
2166 data -> send FIN
2167 SYN|ACK, SYN|FIN, FIN -> no action? */
2168
2169 switch (trans_hdr->flags) {
2170 case AF_IUCV_FLAG_SYN:
2171 /* connect request */
2172 err = afiucv_hs_callback_syn(sk, skb);
2173 break;
2174 case (AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_ACK):
2175 /* connect request confirmed */
2176 err = afiucv_hs_callback_synack(sk, skb);
2177 break;
2178 case (AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN):
2179 /* connect request refused */
2180 err = afiucv_hs_callback_synfin(sk, skb);
2181 break;
2182 case (AF_IUCV_FLAG_FIN):
2183 /* close request */
2184 err = afiucv_hs_callback_fin(sk, skb);
2185 break;
2186 case (AF_IUCV_FLAG_WIN):
2187 err = afiucv_hs_callback_win(sk, skb);
Ursula Braun800c5eb2012-02-08 00:19:47 +00002188 if (skb->len == sizeof(struct af_iucv_trans_hdr)) {
2189 kfree_skb(skb);
2190 break;
2191 }
Ursula Braun82492a32012-03-07 02:06:24 +00002192 /* fall through and receive non-zero length data */
2193 case (AF_IUCV_FLAG_SHT):
2194 /* shutdown request */
2195 /* fall through and receive zero length data */
Ursula Braun3881ac42011-08-08 01:33:54 +00002196 case 0:
2197 /* plain data frame */
Ursula Braunf9c41a62013-04-07 22:19:26 +00002198 IUCV_SKB_CB(skb)->class = trans_hdr->iucv_hdr.class;
Ursula Braun3881ac42011-08-08 01:33:54 +00002199 err = afiucv_hs_callback_rx(sk, skb);
2200 break;
2201 default:
2202 ;
2203 }
2204
2205 return err;
2206}
2207
2208/**
2209 * afiucv_hs_callback_txnotify() - handle send notifcations from HiperSockets
2210 * transport
2211 **/
2212static void afiucv_hs_callback_txnotify(struct sk_buff *skb,
2213 enum iucv_tx_notify n)
2214{
2215 struct sock *isk = skb->sk;
2216 struct sock *sk = NULL;
2217 struct iucv_sock *iucv = NULL;
2218 struct sk_buff_head *list;
2219 struct sk_buff *list_skb;
Ursula Braun800c5eb2012-02-08 00:19:47 +00002220 struct sk_buff *nskb;
Ursula Braun3881ac42011-08-08 01:33:54 +00002221 unsigned long flags;
Ursula Braun3881ac42011-08-08 01:33:54 +00002222
Ursula Braun800c5eb2012-02-08 00:19:47 +00002223 read_lock_irqsave(&iucv_sk_list.lock, flags);
Sasha Levinb67bfe02013-02-27 17:06:00 -08002224 sk_for_each(sk, &iucv_sk_list.head)
Ursula Braun3881ac42011-08-08 01:33:54 +00002225 if (sk == isk) {
2226 iucv = iucv_sk(sk);
2227 break;
2228 }
Ursula Braun800c5eb2012-02-08 00:19:47 +00002229 read_unlock_irqrestore(&iucv_sk_list.lock, flags);
Ursula Braun3881ac42011-08-08 01:33:54 +00002230
Ursula Braun800c5eb2012-02-08 00:19:47 +00002231 if (!iucv || sock_flag(sk, SOCK_ZAPPED))
Ursula Braun3881ac42011-08-08 01:33:54 +00002232 return;
2233
Ursula Braun3881ac42011-08-08 01:33:54 +00002234 list = &iucv->send_skb_q;
Ursula Braun800c5eb2012-02-08 00:19:47 +00002235 spin_lock_irqsave(&list->lock, flags);
Ursula Braun3881ac42011-08-08 01:33:54 +00002236 if (skb_queue_empty(list))
2237 goto out_unlock;
Ursula Braun800c5eb2012-02-08 00:19:47 +00002238 list_skb = list->next;
2239 nskb = list_skb->next;
Ursula Braun3881ac42011-08-08 01:33:54 +00002240 while (list_skb != (struct sk_buff *)list) {
2241 if (skb_shinfo(list_skb) == skb_shinfo(skb)) {
Ursula Braun3881ac42011-08-08 01:33:54 +00002242 switch (n) {
2243 case TX_NOTIFY_OK:
Ursula Braun800c5eb2012-02-08 00:19:47 +00002244 __skb_unlink(list_skb, list);
2245 kfree_skb(list_skb);
Ursula Braun3881ac42011-08-08 01:33:54 +00002246 iucv_sock_wake_msglim(sk);
Ursula Braun3881ac42011-08-08 01:33:54 +00002247 break;
2248 case TX_NOTIFY_PENDING:
2249 atomic_inc(&iucv->pendings);
2250 break;
2251 case TX_NOTIFY_DELAYED_OK:
Ursula Braun800c5eb2012-02-08 00:19:47 +00002252 __skb_unlink(list_skb, list);
Ursula Braun3881ac42011-08-08 01:33:54 +00002253 atomic_dec(&iucv->pendings);
2254 if (atomic_read(&iucv->pendings) <= 0)
2255 iucv_sock_wake_msglim(sk);
Ursula Braun800c5eb2012-02-08 00:19:47 +00002256 kfree_skb(list_skb);
Ursula Braun3881ac42011-08-08 01:33:54 +00002257 break;
2258 case TX_NOTIFY_UNREACHABLE:
2259 case TX_NOTIFY_DELAYED_UNREACHABLE:
2260 case TX_NOTIFY_TPQFULL: /* not yet used */
2261 case TX_NOTIFY_GENERALERROR:
2262 case TX_NOTIFY_DELAYED_GENERALERROR:
Ursula Braun800c5eb2012-02-08 00:19:47 +00002263 __skb_unlink(list_skb, list);
2264 kfree_skb(list_skb);
2265 if (sk->sk_state == IUCV_CONNECTED) {
2266 sk->sk_state = IUCV_DISCONN;
2267 sk->sk_state_change(sk);
2268 }
Ursula Braun3881ac42011-08-08 01:33:54 +00002269 break;
2270 }
2271 break;
2272 }
Ursula Braun800c5eb2012-02-08 00:19:47 +00002273 list_skb = nskb;
2274 nskb = nskb->next;
Ursula Braun3881ac42011-08-08 01:33:54 +00002275 }
Ursula Braun800c5eb2012-02-08 00:19:47 +00002276out_unlock:
Ursula Braun3881ac42011-08-08 01:33:54 +00002277 spin_unlock_irqrestore(&list->lock, flags);
2278
Ursula Braun42bd48e2011-12-19 22:56:28 +00002279 if (sk->sk_state == IUCV_CLOSING) {
2280 if (skb_queue_empty(&iucv_sk(sk)->send_skb_q)) {
2281 sk->sk_state = IUCV_CLOSED;
2282 sk->sk_state_change(sk);
2283 }
2284 }
2285
Ursula Braun3881ac42011-08-08 01:33:54 +00002286}
Ursula Braun9fbd87d2012-03-07 02:06:23 +00002287
2288/*
2289 * afiucv_netdev_event: handle netdev notifier chain events
2290 */
2291static int afiucv_netdev_event(struct notifier_block *this,
2292 unsigned long event, void *ptr)
2293{
Jiri Pirko351638e2013-05-28 01:30:21 +00002294 struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
Ursula Braun9fbd87d2012-03-07 02:06:23 +00002295 struct sock *sk;
2296 struct iucv_sock *iucv;
2297
2298 switch (event) {
2299 case NETDEV_REBOOT:
2300 case NETDEV_GOING_DOWN:
Sasha Levinb67bfe02013-02-27 17:06:00 -08002301 sk_for_each(sk, &iucv_sk_list.head) {
Ursula Braun9fbd87d2012-03-07 02:06:23 +00002302 iucv = iucv_sk(sk);
2303 if ((iucv->hs_dev == event_dev) &&
2304 (sk->sk_state == IUCV_CONNECTED)) {
2305 if (event == NETDEV_GOING_DOWN)
2306 iucv_send_ctrl(sk, AF_IUCV_FLAG_FIN);
2307 sk->sk_state = IUCV_DISCONN;
2308 sk->sk_state_change(sk);
2309 }
2310 }
2311 break;
2312 case NETDEV_DOWN:
2313 case NETDEV_UNREGISTER:
2314 default:
2315 break;
2316 }
2317 return NOTIFY_DONE;
2318}
2319
2320static struct notifier_block afiucv_netdev_notifier = {
2321 .notifier_call = afiucv_netdev_event,
2322};
2323
Alexey Dobriyan5708e862009-09-14 12:23:23 +00002324static const struct proto_ops iucv_sock_ops = {
Jennifer Hunteac37312007-02-08 13:51:54 -08002325 .family = PF_IUCV,
2326 .owner = THIS_MODULE,
2327 .release = iucv_sock_release,
2328 .bind = iucv_sock_bind,
2329 .connect = iucv_sock_connect,
2330 .listen = iucv_sock_listen,
2331 .accept = iucv_sock_accept,
2332 .getname = iucv_sock_getname,
2333 .sendmsg = iucv_sock_sendmsg,
2334 .recvmsg = iucv_sock_recvmsg,
2335 .poll = iucv_sock_poll,
2336 .ioctl = sock_no_ioctl,
2337 .mmap = sock_no_mmap,
2338 .socketpair = sock_no_socketpair,
2339 .shutdown = iucv_sock_shutdown,
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00002340 .setsockopt = iucv_sock_setsockopt,
2341 .getsockopt = iucv_sock_getsockopt,
Jennifer Hunteac37312007-02-08 13:51:54 -08002342};
2343
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00002344static const struct net_proto_family iucv_sock_family_ops = {
Jennifer Hunteac37312007-02-08 13:51:54 -08002345 .family = AF_IUCV,
2346 .owner = THIS_MODULE,
2347 .create = iucv_sock_create,
2348};
2349
Ursula Braun3881ac42011-08-08 01:33:54 +00002350static struct packet_type iucv_packet_type = {
2351 .type = cpu_to_be16(ETH_P_AF_IUCV),
2352 .func = afiucv_hs_rcv,
2353};
2354
2355static int afiucv_iucv_init(void)
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00002356{
2357 int err;
2358
2359 err = pr_iucv->iucv_register(&af_iucv_handler, 0);
2360 if (err)
2361 goto out;
2362 /* establish dummy device */
2363 af_iucv_driver.bus = pr_iucv->bus;
2364 err = driver_register(&af_iucv_driver);
2365 if (err)
2366 goto out_iucv;
2367 af_iucv_dev = kzalloc(sizeof(struct device), GFP_KERNEL);
2368 if (!af_iucv_dev) {
2369 err = -ENOMEM;
2370 goto out_driver;
2371 }
2372 dev_set_name(af_iucv_dev, "af_iucv");
2373 af_iucv_dev->bus = pr_iucv->bus;
2374 af_iucv_dev->parent = pr_iucv->root;
2375 af_iucv_dev->release = (void (*)(struct device *))kfree;
2376 af_iucv_dev->driver = &af_iucv_driver;
2377 err = device_register(af_iucv_dev);
2378 if (err)
2379 goto out_driver;
2380 return 0;
2381
2382out_driver:
2383 driver_unregister(&af_iucv_driver);
2384out_iucv:
2385 pr_iucv->iucv_unregister(&af_iucv_handler, 0);
2386out:
2387 return err;
2388}
2389
Heiko Carstensda99f052007-05-04 12:23:27 -07002390static int __init afiucv_init(void)
Jennifer Hunteac37312007-02-08 13:51:54 -08002391{
2392 int err;
2393
Ursula Braun3881ac42011-08-08 01:33:54 +00002394 if (MACHINE_IS_VM) {
2395 cpcmd("QUERY USERID", iucv_userid, sizeof(iucv_userid), &err);
2396 if (unlikely(err)) {
2397 WARN_ON(err);
2398 err = -EPROTONOSUPPORT;
2399 goto out;
2400 }
Jennifer Hunteac37312007-02-08 13:51:54 -08002401
Ursula Braun3881ac42011-08-08 01:33:54 +00002402 pr_iucv = try_then_request_module(symbol_get(iucv_if), "iucv");
2403 if (!pr_iucv) {
2404 printk(KERN_WARNING "iucv_if lookup failed\n");
2405 memset(&iucv_userid, 0, sizeof(iucv_userid));
2406 }
2407 } else {
2408 memset(&iucv_userid, 0, sizeof(iucv_userid));
2409 pr_iucv = NULL;
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00002410 }
2411
Jennifer Hunteac37312007-02-08 13:51:54 -08002412 err = proto_register(&iucv_proto, 0);
2413 if (err)
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00002414 goto out;
Jennifer Hunteac37312007-02-08 13:51:54 -08002415 err = sock_register(&iucv_sock_family_ops);
2416 if (err)
2417 goto out_proto;
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00002418
Ursula Braun3881ac42011-08-08 01:33:54 +00002419 if (pr_iucv) {
2420 err = afiucv_iucv_init();
2421 if (err)
2422 goto out_sock;
Ursula Braun9fbd87d2012-03-07 02:06:23 +00002423 } else
2424 register_netdevice_notifier(&afiucv_netdev_notifier);
Ursula Braun3881ac42011-08-08 01:33:54 +00002425 dev_add_pack(&iucv_packet_type);
Jennifer Hunteac37312007-02-08 13:51:54 -08002426 return 0;
2427
Ursula Braunc23cad92009-06-16 10:30:44 +02002428out_sock:
2429 sock_unregister(PF_IUCV);
Jennifer Hunteac37312007-02-08 13:51:54 -08002430out_proto:
2431 proto_unregister(&iucv_proto);
Jennifer Hunteac37312007-02-08 13:51:54 -08002432out:
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00002433 if (pr_iucv)
2434 symbol_put(iucv_if);
Jennifer Hunteac37312007-02-08 13:51:54 -08002435 return err;
2436}
2437
2438static void __exit afiucv_exit(void)
2439{
Ursula Braun3881ac42011-08-08 01:33:54 +00002440 if (pr_iucv) {
2441 device_unregister(af_iucv_dev);
2442 driver_unregister(&af_iucv_driver);
2443 pr_iucv->iucv_unregister(&af_iucv_handler, 0);
2444 symbol_put(iucv_if);
Ursula Braun9fbd87d2012-03-07 02:06:23 +00002445 } else
2446 unregister_netdevice_notifier(&afiucv_netdev_notifier);
Ursula Braun3881ac42011-08-08 01:33:54 +00002447 dev_remove_pack(&iucv_packet_type);
Jennifer Hunteac37312007-02-08 13:51:54 -08002448 sock_unregister(PF_IUCV);
2449 proto_unregister(&iucv_proto);
Jennifer Hunteac37312007-02-08 13:51:54 -08002450}
2451
2452module_init(afiucv_init);
2453module_exit(afiucv_exit);
2454
2455MODULE_AUTHOR("Jennifer Hunt <jenhunt@us.ibm.com>");
2456MODULE_DESCRIPTION("IUCV Sockets ver " VERSION);
2457MODULE_VERSION(VERSION);
2458MODULE_LICENSE("GPL");
2459MODULE_ALIAS_NETPROTO(PF_IUCV);
Ursula Braun3881ac42011-08-08 01:33:54 +00002460