blob: 07d7d55a1b93af13013b23c361d8c2f7ff8382aa [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
52/* macros to set/get socket control buffer at correct offset */
53#define CB_TAG(skb) ((skb)->cb) /* iucv message tag */
54#define CB_TAG_LEN (sizeof(((struct iucv_message *) 0)->tag))
55#define CB_TRGCLS(skb) ((skb)->cb + CB_TAG_LEN) /* iucv msg target class */
56#define CB_TRGCLS_LEN (TRGCLS_SIZE)
57
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +000058#define __iucv_sock_wait(sk, condition, timeo, ret) \
59do { \
60 DEFINE_WAIT(__wait); \
61 long __timeo = timeo; \
62 ret = 0; \
Eric Dumazetaa395142010-04-20 13:03:51 +000063 prepare_to_wait(sk_sleep(sk), &__wait, TASK_INTERRUPTIBLE); \
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +000064 while (!(condition)) { \
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +000065 if (!__timeo) { \
66 ret = -EAGAIN; \
67 break; \
68 } \
69 if (signal_pending(current)) { \
70 ret = sock_intr_errno(__timeo); \
71 break; \
72 } \
73 release_sock(sk); \
74 __timeo = schedule_timeout(__timeo); \
75 lock_sock(sk); \
76 ret = sock_error(sk); \
77 if (ret) \
78 break; \
79 } \
Eric Dumazetaa395142010-04-20 13:03:51 +000080 finish_wait(sk_sleep(sk), &__wait); \
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +000081} while (0)
82
83#define iucv_sock_wait(sk, condition, timeo) \
84({ \
85 int __ret = 0; \
86 if (!(condition)) \
87 __iucv_sock_wait(sk, condition, timeo, __ret); \
88 __ret; \
89})
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +000090
Heiko Carstens57f20442007-10-08 02:02:52 -070091static void iucv_sock_kill(struct sock *sk);
92static void iucv_sock_close(struct sock *sk);
Ursula Braun7d316b92012-02-08 00:19:45 +000093static void iucv_sever_path(struct sock *, int);
Heiko Carstens57f20442007-10-08 02:02:52 -070094
Ursula Braun3881ac42011-08-08 01:33:54 +000095static int afiucv_hs_rcv(struct sk_buff *skb, struct net_device *dev,
96 struct packet_type *pt, struct net_device *orig_dev);
97static int afiucv_hs_send(struct iucv_message *imsg, struct sock *sock,
98 struct sk_buff *skb, u8 flags);
99static void afiucv_hs_callback_txnotify(struct sk_buff *, enum iucv_tx_notify);
100
Jennifer Hunteac37312007-02-08 13:51:54 -0800101/* Call Back functions */
102static void iucv_callback_rx(struct iucv_path *, struct iucv_message *);
103static void iucv_callback_txdone(struct iucv_path *, struct iucv_message *);
104static void iucv_callback_connack(struct iucv_path *, u8 ipuser[16]);
Heiko Carstensda99f052007-05-04 12:23:27 -0700105static int iucv_callback_connreq(struct iucv_path *, u8 ipvmid[8],
106 u8 ipuser[16]);
Jennifer Hunteac37312007-02-08 13:51:54 -0800107static void iucv_callback_connrej(struct iucv_path *, u8 ipuser[16]);
Hendrik Brueckneraf88b522009-04-21 23:26:21 +0000108static void iucv_callback_shutdown(struct iucv_path *, u8 ipuser[16]);
Jennifer Hunteac37312007-02-08 13:51:54 -0800109
110static struct iucv_sock_list iucv_sk_list = {
Robert P. J. Day3db8ce32008-04-10 02:11:24 -0700111 .lock = __RW_LOCK_UNLOCKED(iucv_sk_list.lock),
Jennifer Hunteac37312007-02-08 13:51:54 -0800112 .autobind_name = ATOMIC_INIT(0)
113};
114
115static struct iucv_handler af_iucv_handler = {
116 .path_pending = iucv_callback_connreq,
117 .path_complete = iucv_callback_connack,
118 .path_severed = iucv_callback_connrej,
119 .message_pending = iucv_callback_rx,
Hendrik Brueckneraf88b522009-04-21 23:26:21 +0000120 .message_complete = iucv_callback_txdone,
121 .path_quiesced = iucv_callback_shutdown,
Jennifer Hunteac37312007-02-08 13:51:54 -0800122};
123
124static inline void high_nmcpy(unsigned char *dst, char *src)
125{
126 memcpy(dst, src, 8);
127}
128
129static inline void low_nmcpy(unsigned char *dst, char *src)
130{
131 memcpy(&dst[8], src, 8);
132}
133
Ursula Braunc23cad92009-06-16 10:30:44 +0200134static int afiucv_pm_prepare(struct device *dev)
135{
136#ifdef CONFIG_PM_DEBUG
137 printk(KERN_WARNING "afiucv_pm_prepare\n");
138#endif
139 return 0;
140}
141
142static void afiucv_pm_complete(struct device *dev)
143{
144#ifdef CONFIG_PM_DEBUG
145 printk(KERN_WARNING "afiucv_pm_complete\n");
146#endif
Ursula Braunc23cad92009-06-16 10:30:44 +0200147}
148
149/**
150 * afiucv_pm_freeze() - Freeze PM callback
151 * @dev: AFIUCV dummy device
152 *
153 * Sever all established IUCV communication pathes
154 */
155static int afiucv_pm_freeze(struct device *dev)
156{
157 struct iucv_sock *iucv;
158 struct sock *sk;
159 struct hlist_node *node;
160 int err = 0;
161
162#ifdef CONFIG_PM_DEBUG
163 printk(KERN_WARNING "afiucv_pm_freeze\n");
164#endif
165 read_lock(&iucv_sk_list.lock);
166 sk_for_each(sk, node, &iucv_sk_list.head) {
167 iucv = iucv_sk(sk);
Ursula Braunc23cad92009-06-16 10:30:44 +0200168 switch (sk->sk_state) {
Ursula Braunc23cad92009-06-16 10:30:44 +0200169 case IUCV_DISCONN:
170 case IUCV_CLOSING:
171 case IUCV_CONNECTED:
Ursula Braun7d316b92012-02-08 00:19:45 +0000172 iucv_sever_path(sk, 0);
Ursula Braunc23cad92009-06-16 10:30:44 +0200173 break;
174 case IUCV_OPEN:
175 case IUCV_BOUND:
176 case IUCV_LISTEN:
177 case IUCV_CLOSED:
178 default:
179 break;
180 }
Ursula Braun7d316b92012-02-08 00:19:45 +0000181 skb_queue_purge(&iucv->send_skb_q);
182 skb_queue_purge(&iucv->backlog_skb_q);
Ursula Braunc23cad92009-06-16 10:30:44 +0200183 }
184 read_unlock(&iucv_sk_list.lock);
185 return err;
186}
187
188/**
189 * afiucv_pm_restore_thaw() - Thaw and restore PM callback
190 * @dev: AFIUCV dummy device
191 *
192 * socket clean up after freeze
193 */
194static int afiucv_pm_restore_thaw(struct device *dev)
195{
Ursula Braunc23cad92009-06-16 10:30:44 +0200196 struct sock *sk;
197 struct hlist_node *node;
198
199#ifdef CONFIG_PM_DEBUG
200 printk(KERN_WARNING "afiucv_pm_restore_thaw\n");
201#endif
202 read_lock(&iucv_sk_list.lock);
203 sk_for_each(sk, node, &iucv_sk_list.head) {
Ursula Braunc23cad92009-06-16 10:30:44 +0200204 switch (sk->sk_state) {
205 case IUCV_CONNECTED:
206 sk->sk_err = EPIPE;
207 sk->sk_state = IUCV_DISCONN;
208 sk->sk_state_change(sk);
209 break;
210 case IUCV_DISCONN:
Ursula Braunc23cad92009-06-16 10:30:44 +0200211 case IUCV_CLOSING:
212 case IUCV_LISTEN:
213 case IUCV_BOUND:
214 case IUCV_OPEN:
215 default:
216 break;
217 }
218 }
219 read_unlock(&iucv_sk_list.lock);
220 return 0;
221}
222
Alexey Dobriyan47145212009-12-14 18:00:08 -0800223static const struct dev_pm_ops afiucv_pm_ops = {
Ursula Braunc23cad92009-06-16 10:30:44 +0200224 .prepare = afiucv_pm_prepare,
225 .complete = afiucv_pm_complete,
226 .freeze = afiucv_pm_freeze,
227 .thaw = afiucv_pm_restore_thaw,
228 .restore = afiucv_pm_restore_thaw,
229};
230
231static struct device_driver af_iucv_driver = {
232 .owner = THIS_MODULE,
233 .name = "afiucv",
Frank Blaschka6fcd61f2011-08-08 01:33:51 +0000234 .bus = NULL,
Ursula Braunc23cad92009-06-16 10:30:44 +0200235 .pm = &afiucv_pm_ops,
236};
237
238/* dummy device used as trigger for PM functions */
239static struct device *af_iucv_dev;
240
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000241/**
242 * iucv_msg_length() - Returns the length of an iucv message.
243 * @msg: Pointer to struct iucv_message, MUST NOT be NULL
244 *
245 * The function returns the length of the specified iucv message @msg of data
246 * stored in a buffer and of data stored in the parameter list (PRMDATA).
247 *
248 * For IUCV_IPRMDATA, AF_IUCV uses the following convention to transport socket
249 * data:
250 * PRMDATA[0..6] socket data (max 7 bytes);
251 * PRMDATA[7] socket data length value (len is 0xff - PRMDATA[7])
252 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300253 * The socket data length is computed by subtracting the socket data length
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000254 * value from 0xFF.
255 * If the socket data len is greater 7, then PRMDATA can be used for special
256 * notifications (see iucv_sock_shutdown); and further,
257 * if the socket data len is > 7, the function returns 8.
258 *
259 * Use this function to allocate socket buffers to store iucv message data.
260 */
261static inline size_t iucv_msg_length(struct iucv_message *msg)
262{
263 size_t datalen;
264
265 if (msg->flags & IUCV_IPRMDATA) {
266 datalen = 0xff - msg->rmmsg[7];
267 return (datalen < 8) ? datalen : 8;
268 }
269 return msg->length;
270}
271
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +0000272/**
273 * iucv_sock_in_state() - check for specific states
274 * @sk: sock structure
275 * @state: first iucv sk state
276 * @state: second iucv sk state
277 *
278 * Returns true if the socket in either in the first or second state.
279 */
280static int iucv_sock_in_state(struct sock *sk, int state, int state2)
281{
282 return (sk->sk_state == state || sk->sk_state == state2);
283}
284
285/**
286 * iucv_below_msglim() - function to check if messages can be sent
287 * @sk: sock structure
288 *
289 * Returns true if the send queue length is lower than the message limit.
290 * Always returns true if the socket is not connected (no iucv path for
291 * checking the message limit).
292 */
293static inline int iucv_below_msglim(struct sock *sk)
294{
295 struct iucv_sock *iucv = iucv_sk(sk);
296
297 if (sk->sk_state != IUCV_CONNECTED)
298 return 1;
Ursula Braun3881ac42011-08-08 01:33:54 +0000299 if (iucv->transport == AF_IUCV_TRANS_IUCV)
300 return (skb_queue_len(&iucv->send_skb_q) < iucv->path->msglim);
301 else
302 return ((atomic_read(&iucv->msg_sent) < iucv->msglimit_peer) &&
303 (atomic_read(&iucv->pendings) <= 0));
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +0000304}
305
306/**
307 * iucv_sock_wake_msglim() - Wake up thread waiting on msg limit
308 */
309static void iucv_sock_wake_msglim(struct sock *sk)
310{
Eric Dumazet43815482010-04-29 11:01:49 +0000311 struct socket_wq *wq;
312
313 rcu_read_lock();
314 wq = rcu_dereference(sk->sk_wq);
315 if (wq_has_sleeper(wq))
316 wake_up_interruptible_all(&wq->wait);
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +0000317 sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
Eric Dumazet43815482010-04-29 11:01:49 +0000318 rcu_read_unlock();
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +0000319}
320
Ursula Braun3881ac42011-08-08 01:33:54 +0000321/**
322 * afiucv_hs_send() - send a message through HiperSockets transport
323 */
324static int afiucv_hs_send(struct iucv_message *imsg, struct sock *sock,
325 struct sk_buff *skb, u8 flags)
326{
Ursula Braun3881ac42011-08-08 01:33:54 +0000327 struct iucv_sock *iucv = iucv_sk(sock);
328 struct af_iucv_trans_hdr *phs_hdr;
329 struct sk_buff *nskb;
330 int err, confirm_recv = 0;
331
332 memset(skb->head, 0, ETH_HLEN);
333 phs_hdr = (struct af_iucv_trans_hdr *)skb_push(skb,
334 sizeof(struct af_iucv_trans_hdr));
335 skb_reset_mac_header(skb);
336 skb_reset_network_header(skb);
337 skb_push(skb, ETH_HLEN);
338 skb_reset_mac_header(skb);
339 memset(phs_hdr, 0, sizeof(struct af_iucv_trans_hdr));
340
341 phs_hdr->magic = ETH_P_AF_IUCV;
342 phs_hdr->version = 1;
343 phs_hdr->flags = flags;
344 if (flags == AF_IUCV_FLAG_SYN)
345 phs_hdr->window = iucv->msglimit;
346 else if ((flags == AF_IUCV_FLAG_WIN) || !flags) {
347 confirm_recv = atomic_read(&iucv->msg_recv);
348 phs_hdr->window = confirm_recv;
349 if (confirm_recv)
350 phs_hdr->flags = phs_hdr->flags | AF_IUCV_FLAG_WIN;
351 }
352 memcpy(phs_hdr->destUserID, iucv->dst_user_id, 8);
353 memcpy(phs_hdr->destAppName, iucv->dst_name, 8);
354 memcpy(phs_hdr->srcUserID, iucv->src_user_id, 8);
355 memcpy(phs_hdr->srcAppName, iucv->src_name, 8);
356 ASCEBC(phs_hdr->destUserID, sizeof(phs_hdr->destUserID));
357 ASCEBC(phs_hdr->destAppName, sizeof(phs_hdr->destAppName));
358 ASCEBC(phs_hdr->srcUserID, sizeof(phs_hdr->srcUserID));
359 ASCEBC(phs_hdr->srcAppName, sizeof(phs_hdr->srcAppName));
360 if (imsg)
361 memcpy(&phs_hdr->iucv_hdr, imsg, sizeof(struct iucv_message));
362
Ursula Braun800c5eb2012-02-08 00:19:47 +0000363 skb->dev = iucv->hs_dev;
Ursula Braun3881ac42011-08-08 01:33:54 +0000364 if (!skb->dev)
365 return -ENODEV;
Ursula Braun800c5eb2012-02-08 00:19:47 +0000366 if (!(skb->dev->flags & IFF_UP) || !netif_carrier_ok(skb->dev))
Ursula Braun3881ac42011-08-08 01:33:54 +0000367 return -ENETDOWN;
368 if (skb->len > skb->dev->mtu) {
369 if (sock->sk_type == SOCK_SEQPACKET)
370 return -EMSGSIZE;
371 else
372 skb_trim(skb, skb->dev->mtu);
373 }
374 skb->protocol = ETH_P_AF_IUCV;
375 skb_shinfo(skb)->tx_flags |= SKBTX_DRV_NEEDS_SK_REF;
376 nskb = skb_clone(skb, GFP_ATOMIC);
377 if (!nskb)
378 return -ENOMEM;
379 skb_queue_tail(&iucv->send_skb_q, nskb);
380 err = dev_queue_xmit(skb);
Ursula Braun800c5eb2012-02-08 00:19:47 +0000381 if (net_xmit_eval(err)) {
Ursula Braun3881ac42011-08-08 01:33:54 +0000382 skb_unlink(nskb, &iucv->send_skb_q);
383 kfree_skb(nskb);
384 } else {
385 atomic_sub(confirm_recv, &iucv->msg_recv);
386 WARN_ON(atomic_read(&iucv->msg_recv) < 0);
387 }
Ursula Braun800c5eb2012-02-08 00:19:47 +0000388 return net_xmit_eval(err);
Ursula Braun3881ac42011-08-08 01:33:54 +0000389}
390
Jennifer Hunteac37312007-02-08 13:51:54 -0800391static struct sock *__iucv_get_sock_by_name(char *nm)
392{
393 struct sock *sk;
394 struct hlist_node *node;
395
396 sk_for_each(sk, node, &iucv_sk_list.head)
397 if (!memcmp(&iucv_sk(sk)->src_name, nm, 8))
398 return sk;
399
400 return NULL;
401}
402
403static void iucv_sock_destruct(struct sock *sk)
404{
405 skb_queue_purge(&sk->sk_receive_queue);
Ursula Braun82492a32012-03-07 02:06:24 +0000406 skb_queue_purge(&sk->sk_error_queue);
407
408 sk_mem_reclaim(sk);
409
410 if (!sock_flag(sk, SOCK_DEAD)) {
411 pr_err("Attempt to release alive iucv socket %p\n", sk);
412 return;
413 }
414
415 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
416 WARN_ON(atomic_read(&sk->sk_wmem_alloc));
417 WARN_ON(sk->sk_wmem_queued);
418 WARN_ON(sk->sk_forward_alloc);
Jennifer Hunteac37312007-02-08 13:51:54 -0800419}
420
421/* Cleanup Listen */
422static void iucv_sock_cleanup_listen(struct sock *parent)
423{
424 struct sock *sk;
425
426 /* Close non-accepted connections */
427 while ((sk = iucv_accept_dequeue(parent, NULL))) {
428 iucv_sock_close(sk);
429 iucv_sock_kill(sk);
430 }
431
432 parent->sk_state = IUCV_CLOSED;
Jennifer Hunteac37312007-02-08 13:51:54 -0800433}
434
Hendrik Brueckner7514bab2009-09-16 04:37:27 +0000435/* Kill socket (only if zapped and orphaned) */
Jennifer Hunteac37312007-02-08 13:51:54 -0800436static void iucv_sock_kill(struct sock *sk)
437{
438 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
439 return;
440
441 iucv_sock_unlink(&iucv_sk_list, sk);
442 sock_set_flag(sk, SOCK_DEAD);
443 sock_put(sk);
444}
445
Ursula Braun7d316b92012-02-08 00:19:45 +0000446/* Terminate an IUCV path */
447static void iucv_sever_path(struct sock *sk, int with_user_data)
448{
449 unsigned char user_data[16];
450 struct iucv_sock *iucv = iucv_sk(sk);
451 struct iucv_path *path = iucv->path;
452
453 if (iucv->path) {
454 iucv->path = NULL;
455 if (with_user_data) {
456 low_nmcpy(user_data, iucv->src_name);
457 high_nmcpy(user_data, iucv->dst_name);
458 ASCEBC(user_data, sizeof(user_data));
459 pr_iucv->path_sever(path, user_data);
460 } else
461 pr_iucv->path_sever(path, NULL);
462 iucv_path_free(path);
463 }
464}
465
Ursula Braun9fbd87d2012-03-07 02:06:23 +0000466/* Send FIN through an IUCV socket for HIPER transport */
467static int iucv_send_ctrl(struct sock *sk, u8 flags)
468{
469 int err = 0;
470 int blen;
471 struct sk_buff *skb;
472
473 blen = sizeof(struct af_iucv_trans_hdr) + ETH_HLEN;
474 skb = sock_alloc_send_skb(sk, blen, 1, &err);
475 if (skb) {
476 skb_reserve(skb, blen);
477 err = afiucv_hs_send(NULL, sk, skb, flags);
478 }
479 return err;
480}
481
Jennifer Hunteac37312007-02-08 13:51:54 -0800482/* Close an IUCV socket */
483static void iucv_sock_close(struct sock *sk)
484{
Jennifer Hunteac37312007-02-08 13:51:54 -0800485 struct iucv_sock *iucv = iucv_sk(sk);
Jennifer Hunt561e0362007-05-04 12:22:07 -0700486 unsigned long timeo;
Ursula Braun800c5eb2012-02-08 00:19:47 +0000487 int err = 0;
Jennifer Hunteac37312007-02-08 13:51:54 -0800488
Jennifer Hunteac37312007-02-08 13:51:54 -0800489 lock_sock(sk);
490
Heiko Carstensda99f052007-05-04 12:23:27 -0700491 switch (sk->sk_state) {
Jennifer Hunteac37312007-02-08 13:51:54 -0800492 case IUCV_LISTEN:
493 iucv_sock_cleanup_listen(sk);
494 break;
495
496 case IUCV_CONNECTED:
Ursula Braun3881ac42011-08-08 01:33:54 +0000497 if (iucv->transport == AF_IUCV_TRANS_HIPER) {
Ursula Braun9fbd87d2012-03-07 02:06:23 +0000498 err = iucv_send_ctrl(sk, AF_IUCV_FLAG_FIN);
Ursula Braun3881ac42011-08-08 01:33:54 +0000499 sk->sk_state = IUCV_DISCONN;
500 sk->sk_state_change(sk);
501 }
Ursula Braun816abba2011-12-19 22:56:29 +0000502 case IUCV_DISCONN: /* fall through */
Jennifer Hunt561e0362007-05-04 12:22:07 -0700503 sk->sk_state = IUCV_CLOSING;
504 sk->sk_state_change(sk);
505
Ursula Braun800c5eb2012-02-08 00:19:47 +0000506 if (!err && !skb_queue_empty(&iucv->send_skb_q)) {
Jennifer Hunt561e0362007-05-04 12:22:07 -0700507 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
508 timeo = sk->sk_lingertime;
509 else
510 timeo = IUCV_DISCONN_TIMEOUT;
Ursula Braun9f6298a2011-05-12 18:45:08 +0000511 iucv_sock_wait(sk,
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +0000512 iucv_sock_in_state(sk, IUCV_CLOSED, 0),
513 timeo);
Jennifer Hunt561e0362007-05-04 12:22:07 -0700514 }
515
Ursula Braunbbe188c2009-04-21 06:04:20 +0000516 case IUCV_CLOSING: /* fall through */
Jennifer Hunt561e0362007-05-04 12:22:07 -0700517 sk->sk_state = IUCV_CLOSED;
518 sk->sk_state_change(sk);
519
Jennifer Hunteac37312007-02-08 13:51:54 -0800520 sk->sk_err = ECONNRESET;
521 sk->sk_state_change(sk);
522
Ursula Braun800c5eb2012-02-08 00:19:47 +0000523 skb_queue_purge(&iucv->send_skb_q);
Jennifer Hunt561e0362007-05-04 12:22:07 -0700524 skb_queue_purge(&iucv->backlog_skb_q);
Jennifer Hunteac37312007-02-08 13:51:54 -0800525
Ursula Braun7d316b92012-02-08 00:19:45 +0000526 default: /* fall through */
527 iucv_sever_path(sk, 1);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700528 }
Jennifer Hunteac37312007-02-08 13:51:54 -0800529
Ursula Braun800c5eb2012-02-08 00:19:47 +0000530 if (iucv->hs_dev) {
531 dev_put(iucv->hs_dev);
532 iucv->hs_dev = NULL;
533 sk->sk_bound_dev_if = 0;
534 }
535
Hendrik Brueckner7514bab2009-09-16 04:37:27 +0000536 /* mark socket for deletion by iucv_sock_kill() */
537 sock_set_flag(sk, SOCK_ZAPPED);
538
Jennifer Hunteac37312007-02-08 13:51:54 -0800539 release_sock(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800540}
541
542static void iucv_sock_init(struct sock *sk, struct sock *parent)
543{
544 if (parent)
545 sk->sk_type = parent->sk_type;
546}
547
548static struct sock *iucv_sock_alloc(struct socket *sock, int proto, gfp_t prio)
549{
550 struct sock *sk;
Ursula Braun493d3972011-08-08 01:33:52 +0000551 struct iucv_sock *iucv;
Jennifer Hunteac37312007-02-08 13:51:54 -0800552
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700553 sk = sk_alloc(&init_net, PF_IUCV, prio, &iucv_proto);
Jennifer Hunteac37312007-02-08 13:51:54 -0800554 if (!sk)
555 return NULL;
Ursula Braun493d3972011-08-08 01:33:52 +0000556 iucv = iucv_sk(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800557
558 sock_init_data(sock, sk);
Ursula Braun493d3972011-08-08 01:33:52 +0000559 INIT_LIST_HEAD(&iucv->accept_q);
560 spin_lock_init(&iucv->accept_q_lock);
561 skb_queue_head_init(&iucv->send_skb_q);
562 INIT_LIST_HEAD(&iucv->message_q.list);
563 spin_lock_init(&iucv->message_q.lock);
564 skb_queue_head_init(&iucv->backlog_skb_q);
565 iucv->send_tag = 0;
Ursula Braun3881ac42011-08-08 01:33:54 +0000566 atomic_set(&iucv->pendings, 0);
Ursula Braun493d3972011-08-08 01:33:52 +0000567 iucv->flags = 0;
Ursula Braun3881ac42011-08-08 01:33:54 +0000568 iucv->msglimit = 0;
569 atomic_set(&iucv->msg_sent, 0);
570 atomic_set(&iucv->msg_recv, 0);
Ursula Braun493d3972011-08-08 01:33:52 +0000571 iucv->path = NULL;
Ursula Braun3881ac42011-08-08 01:33:54 +0000572 iucv->sk_txnotify = afiucv_hs_callback_txnotify;
Ursula Braun493d3972011-08-08 01:33:52 +0000573 memset(&iucv->src_user_id , 0, 32);
Ursula Braun3881ac42011-08-08 01:33:54 +0000574 if (pr_iucv)
575 iucv->transport = AF_IUCV_TRANS_IUCV;
576 else
577 iucv->transport = AF_IUCV_TRANS_HIPER;
Jennifer Hunteac37312007-02-08 13:51:54 -0800578
579 sk->sk_destruct = iucv_sock_destruct;
580 sk->sk_sndtimeo = IUCV_CONN_TIMEOUT;
581 sk->sk_allocation = GFP_DMA;
582
583 sock_reset_flag(sk, SOCK_ZAPPED);
584
585 sk->sk_protocol = proto;
586 sk->sk_state = IUCV_OPEN;
587
Jennifer Hunteac37312007-02-08 13:51:54 -0800588 iucv_sock_link(&iucv_sk_list, sk);
589 return sk;
590}
591
592/* Create an IUCV socket */
Eric Paris3f378b62009-11-05 22:18:14 -0800593static int iucv_sock_create(struct net *net, struct socket *sock, int protocol,
594 int kern)
Jennifer Hunteac37312007-02-08 13:51:54 -0800595{
596 struct sock *sk;
597
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +0000598 if (protocol && protocol != PF_IUCV)
599 return -EPROTONOSUPPORT;
Jennifer Hunteac37312007-02-08 13:51:54 -0800600
601 sock->state = SS_UNCONNECTED;
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +0000602
603 switch (sock->type) {
604 case SOCK_STREAM:
605 sock->ops = &iucv_sock_ops;
606 break;
607 case SOCK_SEQPACKET:
608 /* currently, proto ops can handle both sk types */
609 sock->ops = &iucv_sock_ops;
610 break;
611 default:
612 return -ESOCKTNOSUPPORT;
613 }
Jennifer Hunteac37312007-02-08 13:51:54 -0800614
615 sk = iucv_sock_alloc(sock, protocol, GFP_KERNEL);
616 if (!sk)
617 return -ENOMEM;
618
619 iucv_sock_init(sk, NULL);
620
621 return 0;
622}
623
624void iucv_sock_link(struct iucv_sock_list *l, struct sock *sk)
625{
626 write_lock_bh(&l->lock);
627 sk_add_node(sk, &l->head);
628 write_unlock_bh(&l->lock);
629}
630
631void iucv_sock_unlink(struct iucv_sock_list *l, struct sock *sk)
632{
633 write_lock_bh(&l->lock);
634 sk_del_node_init(sk);
635 write_unlock_bh(&l->lock);
636}
637
638void iucv_accept_enqueue(struct sock *parent, struct sock *sk)
639{
Ursula Braunfebca282007-07-14 19:04:25 -0700640 unsigned long flags;
641 struct iucv_sock *par = iucv_sk(parent);
642
Jennifer Hunteac37312007-02-08 13:51:54 -0800643 sock_hold(sk);
Ursula Braunfebca282007-07-14 19:04:25 -0700644 spin_lock_irqsave(&par->accept_q_lock, flags);
645 list_add_tail(&iucv_sk(sk)->accept_q, &par->accept_q);
646 spin_unlock_irqrestore(&par->accept_q_lock, flags);
Jennifer Hunteac37312007-02-08 13:51:54 -0800647 iucv_sk(sk)->parent = parent;
Hendrik Brueckner49f5eba2009-10-14 22:54:55 +0000648 sk_acceptq_added(parent);
Jennifer Hunteac37312007-02-08 13:51:54 -0800649}
650
651void iucv_accept_unlink(struct sock *sk)
652{
Ursula Braunfebca282007-07-14 19:04:25 -0700653 unsigned long flags;
654 struct iucv_sock *par = iucv_sk(iucv_sk(sk)->parent);
655
656 spin_lock_irqsave(&par->accept_q_lock, flags);
Jennifer Hunteac37312007-02-08 13:51:54 -0800657 list_del_init(&iucv_sk(sk)->accept_q);
Ursula Braunfebca282007-07-14 19:04:25 -0700658 spin_unlock_irqrestore(&par->accept_q_lock, flags);
Hendrik Brueckner49f5eba2009-10-14 22:54:55 +0000659 sk_acceptq_removed(iucv_sk(sk)->parent);
Jennifer Hunteac37312007-02-08 13:51:54 -0800660 iucv_sk(sk)->parent = NULL;
661 sock_put(sk);
662}
663
664struct sock *iucv_accept_dequeue(struct sock *parent, struct socket *newsock)
665{
666 struct iucv_sock *isk, *n;
667 struct sock *sk;
668
Heiko Carstensda99f052007-05-04 12:23:27 -0700669 list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) {
Jennifer Hunteac37312007-02-08 13:51:54 -0800670 sk = (struct sock *) isk;
671 lock_sock(sk);
672
673 if (sk->sk_state == IUCV_CLOSED) {
Jennifer Hunteac37312007-02-08 13:51:54 -0800674 iucv_accept_unlink(sk);
Ursula Braunfebca282007-07-14 19:04:25 -0700675 release_sock(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800676 continue;
677 }
678
679 if (sk->sk_state == IUCV_CONNECTED ||
Ursula Braunaac63992011-12-19 22:56:31 +0000680 sk->sk_state == IUCV_DISCONN ||
Jennifer Hunteac37312007-02-08 13:51:54 -0800681 !newsock) {
682 iucv_accept_unlink(sk);
683 if (newsock)
684 sock_graft(sk, newsock);
685
Jennifer Hunteac37312007-02-08 13:51:54 -0800686 release_sock(sk);
687 return sk;
688 }
689
690 release_sock(sk);
691 }
692 return NULL;
693}
694
Jennifer Hunteac37312007-02-08 13:51:54 -0800695/* Bind an unbound socket */
696static int iucv_sock_bind(struct socket *sock, struct sockaddr *addr,
697 int addr_len)
698{
699 struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
700 struct sock *sk = sock->sk;
701 struct iucv_sock *iucv;
Ursula Braun3881ac42011-08-08 01:33:54 +0000702 int err = 0;
703 struct net_device *dev;
704 char uid[9];
Jennifer Hunteac37312007-02-08 13:51:54 -0800705
706 /* Verify the input sockaddr */
707 if (!addr || addr->sa_family != AF_IUCV)
708 return -EINVAL;
709
710 lock_sock(sk);
711 if (sk->sk_state != IUCV_OPEN) {
712 err = -EBADFD;
713 goto done;
714 }
715
716 write_lock_bh(&iucv_sk_list.lock);
717
718 iucv = iucv_sk(sk);
719 if (__iucv_get_sock_by_name(sa->siucv_name)) {
720 err = -EADDRINUSE;
721 goto done_unlock;
722 }
Ursula Braun3881ac42011-08-08 01:33:54 +0000723 if (iucv->path)
Jennifer Hunteac37312007-02-08 13:51:54 -0800724 goto done_unlock;
Jennifer Hunteac37312007-02-08 13:51:54 -0800725
726 /* Bind the socket */
Ursula Braun3881ac42011-08-08 01:33:54 +0000727 if (pr_iucv)
728 if (!memcmp(sa->siucv_user_id, iucv_userid, 8))
729 goto vm_bind; /* VM IUCV transport */
Jennifer Hunteac37312007-02-08 13:51:54 -0800730
Ursula Braun3881ac42011-08-08 01:33:54 +0000731 /* try hiper transport */
732 memcpy(uid, sa->siucv_user_id, sizeof(uid));
733 ASCEBC(uid, 8);
734 rcu_read_lock();
735 for_each_netdev_rcu(&init_net, dev) {
736 if (!memcmp(dev->perm_addr, uid, 8)) {
737 memcpy(iucv->src_name, sa->siucv_name, 8);
738 memcpy(iucv->src_user_id, sa->siucv_user_id, 8);
Ursula Braun816abba2011-12-19 22:56:29 +0000739 sk->sk_bound_dev_if = dev->ifindex;
Ursula Braun800c5eb2012-02-08 00:19:47 +0000740 iucv->hs_dev = dev;
741 dev_hold(dev);
Ursula Braun3881ac42011-08-08 01:33:54 +0000742 sk->sk_state = IUCV_BOUND;
743 iucv->transport = AF_IUCV_TRANS_HIPER;
744 if (!iucv->msglimit)
745 iucv->msglimit = IUCV_HIPER_MSGLIM_DEFAULT;
746 rcu_read_unlock();
747 goto done_unlock;
748 }
749 }
750 rcu_read_unlock();
751vm_bind:
752 if (pr_iucv) {
753 /* use local userid for backward compat */
754 memcpy(iucv->src_name, sa->siucv_name, 8);
755 memcpy(iucv->src_user_id, iucv_userid, 8);
756 sk->sk_state = IUCV_BOUND;
757 iucv->transport = AF_IUCV_TRANS_IUCV;
758 if (!iucv->msglimit)
759 iucv->msglimit = IUCV_QUEUELEN_DEFAULT;
760 goto done_unlock;
761 }
762 /* found no dev to bind */
763 err = -ENODEV;
Jennifer Hunteac37312007-02-08 13:51:54 -0800764done_unlock:
765 /* Release the socket list lock */
766 write_unlock_bh(&iucv_sk_list.lock);
767done:
768 release_sock(sk);
769 return err;
770}
771
772/* Automatically bind an unbound socket */
773static int iucv_sock_autobind(struct sock *sk)
774{
775 struct iucv_sock *iucv = iucv_sk(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800776 char name[12];
777 int err = 0;
778
Ursula Braunaac63992011-12-19 22:56:31 +0000779 if (unlikely(!pr_iucv))
Jennifer Hunteac37312007-02-08 13:51:54 -0800780 return -EPROTO;
781
Ursula Braunaac63992011-12-19 22:56:31 +0000782 memcpy(iucv->src_user_id, iucv_userid, 8);
Jennifer Hunteac37312007-02-08 13:51:54 -0800783
784 write_lock_bh(&iucv_sk_list.lock);
785
786 sprintf(name, "%08x", atomic_inc_return(&iucv_sk_list.autobind_name));
787 while (__iucv_get_sock_by_name(name)) {
788 sprintf(name, "%08x",
789 atomic_inc_return(&iucv_sk_list.autobind_name));
790 }
791
792 write_unlock_bh(&iucv_sk_list.lock);
793
794 memcpy(&iucv->src_name, name, 8);
795
Ursula Braun3881ac42011-08-08 01:33:54 +0000796 if (!iucv->msglimit)
797 iucv->msglimit = IUCV_QUEUELEN_DEFAULT;
798
Jennifer Hunteac37312007-02-08 13:51:54 -0800799 return err;
800}
801
Ursula Braun3881ac42011-08-08 01:33:54 +0000802static int afiucv_path_connect(struct socket *sock, struct sockaddr *addr)
Jennifer Hunteac37312007-02-08 13:51:54 -0800803{
804 struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
805 struct sock *sk = sock->sk;
Ursula Braun493d3972011-08-08 01:33:52 +0000806 struct iucv_sock *iucv = iucv_sk(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800807 unsigned char user_data[16];
808 int err;
809
Jennifer Hunteac37312007-02-08 13:51:54 -0800810 high_nmcpy(user_data, sa->siucv_name);
Ursula Braun493d3972011-08-08 01:33:52 +0000811 low_nmcpy(user_data, iucv->src_name);
Jennifer Hunteac37312007-02-08 13:51:54 -0800812 ASCEBC(user_data, sizeof(user_data));
813
Jennifer Hunteac37312007-02-08 13:51:54 -0800814 /* Create path. */
Hendrik Brueckner09488e2e2009-04-21 23:26:27 +0000815 iucv->path = iucv_path_alloc(iucv->msglimit,
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000816 IUCV_IPRMDATA, GFP_KERNEL);
Ursula Braund4444722008-02-07 18:07:19 -0800817 if (!iucv->path) {
818 err = -ENOMEM;
819 goto done;
820 }
Frank Blaschka6fcd61f2011-08-08 01:33:51 +0000821 err = pr_iucv->path_connect(iucv->path, &af_iucv_handler,
822 sa->siucv_user_id, NULL, user_data,
823 sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800824 if (err) {
825 iucv_path_free(iucv->path);
826 iucv->path = NULL;
Hendrik Brueckner55cdea92009-01-05 18:07:07 -0800827 switch (err) {
828 case 0x0b: /* Target communicator is not logged on */
829 err = -ENETUNREACH;
830 break;
831 case 0x0d: /* Max connections for this guest exceeded */
832 case 0x0e: /* Max connections for target guest exceeded */
833 err = -EAGAIN;
834 break;
835 case 0x0f: /* Missing IUCV authorization */
836 err = -EACCES;
837 break;
838 default:
839 err = -ECONNREFUSED;
840 break;
841 }
Ursula Braun3881ac42011-08-08 01:33:54 +0000842 }
843done:
844 return err;
845}
846
847/* Connect an unconnected socket */
848static int iucv_sock_connect(struct socket *sock, struct sockaddr *addr,
849 int alen, int flags)
850{
851 struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
852 struct sock *sk = sock->sk;
853 struct iucv_sock *iucv = iucv_sk(sk);
854 int err;
855
856 if (addr->sa_family != AF_IUCV || alen < sizeof(struct sockaddr_iucv))
857 return -EINVAL;
858
859 if (sk->sk_state != IUCV_OPEN && sk->sk_state != IUCV_BOUND)
860 return -EBADFD;
861
862 if (sk->sk_state == IUCV_OPEN &&
863 iucv->transport == AF_IUCV_TRANS_HIPER)
864 return -EBADFD; /* explicit bind required */
865
866 if (sk->sk_type != SOCK_STREAM && sk->sk_type != SOCK_SEQPACKET)
867 return -EINVAL;
868
869 if (sk->sk_state == IUCV_OPEN) {
870 err = iucv_sock_autobind(sk);
871 if (unlikely(err))
872 return err;
Jennifer Hunteac37312007-02-08 13:51:54 -0800873 }
874
Ursula Braun3881ac42011-08-08 01:33:54 +0000875 lock_sock(sk);
876
877 /* Set the destination information */
878 memcpy(iucv->dst_user_id, sa->siucv_user_id, 8);
879 memcpy(iucv->dst_name, sa->siucv_name, 8);
880
881 if (iucv->transport == AF_IUCV_TRANS_HIPER)
Ursula Braun9fbd87d2012-03-07 02:06:23 +0000882 err = iucv_send_ctrl(sock->sk, AF_IUCV_FLAG_SYN);
Ursula Braun3881ac42011-08-08 01:33:54 +0000883 else
884 err = afiucv_path_connect(sock, addr);
885 if (err)
886 goto done;
887
888 if (sk->sk_state != IUCV_CONNECTED)
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +0000889 err = iucv_sock_wait(sk, iucv_sock_in_state(sk, IUCV_CONNECTED,
890 IUCV_DISCONN),
891 sock_sndtimeo(sk, flags & O_NONBLOCK));
Jennifer Hunteac37312007-02-08 13:51:54 -0800892
Ursula Braun3881ac42011-08-08 01:33:54 +0000893 if (sk->sk_state == IUCV_DISCONN || sk->sk_state == IUCV_CLOSED)
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000894 err = -ECONNREFUSED;
Ursula Braun18becbc2009-01-05 18:07:46 -0800895
Ursula Braun7d316b92012-02-08 00:19:45 +0000896 if (err && iucv->transport == AF_IUCV_TRANS_IUCV)
897 iucv_sever_path(sk, 0);
Ursula Braun18becbc2009-01-05 18:07:46 -0800898
Jennifer Hunteac37312007-02-08 13:51:54 -0800899done:
900 release_sock(sk);
901 return err;
902}
903
904/* Move a socket into listening state. */
905static int iucv_sock_listen(struct socket *sock, int backlog)
906{
907 struct sock *sk = sock->sk;
908 int err;
909
910 lock_sock(sk);
911
912 err = -EINVAL;
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +0000913 if (sk->sk_state != IUCV_BOUND)
914 goto done;
915
916 if (sock->type != SOCK_STREAM && sock->type != SOCK_SEQPACKET)
Jennifer Hunteac37312007-02-08 13:51:54 -0800917 goto done;
918
919 sk->sk_max_ack_backlog = backlog;
920 sk->sk_ack_backlog = 0;
921 sk->sk_state = IUCV_LISTEN;
922 err = 0;
923
924done:
925 release_sock(sk);
926 return err;
927}
928
929/* Accept a pending connection */
930static int iucv_sock_accept(struct socket *sock, struct socket *newsock,
931 int flags)
932{
933 DECLARE_WAITQUEUE(wait, current);
934 struct sock *sk = sock->sk, *nsk;
935 long timeo;
936 int err = 0;
937
Jennifer Hunt561e0362007-05-04 12:22:07 -0700938 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
Jennifer Hunteac37312007-02-08 13:51:54 -0800939
940 if (sk->sk_state != IUCV_LISTEN) {
941 err = -EBADFD;
942 goto done;
943 }
944
945 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
946
947 /* Wait for an incoming connection */
Eric Dumazetaa395142010-04-20 13:03:51 +0000948 add_wait_queue_exclusive(sk_sleep(sk), &wait);
Heiko Carstensda99f052007-05-04 12:23:27 -0700949 while (!(nsk = iucv_accept_dequeue(sk, newsock))) {
Jennifer Hunteac37312007-02-08 13:51:54 -0800950 set_current_state(TASK_INTERRUPTIBLE);
951 if (!timeo) {
952 err = -EAGAIN;
953 break;
954 }
955
956 release_sock(sk);
957 timeo = schedule_timeout(timeo);
Jennifer Hunt561e0362007-05-04 12:22:07 -0700958 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
Jennifer Hunteac37312007-02-08 13:51:54 -0800959
960 if (sk->sk_state != IUCV_LISTEN) {
961 err = -EBADFD;
962 break;
963 }
964
965 if (signal_pending(current)) {
966 err = sock_intr_errno(timeo);
967 break;
968 }
969 }
970
971 set_current_state(TASK_RUNNING);
Eric Dumazetaa395142010-04-20 13:03:51 +0000972 remove_wait_queue(sk_sleep(sk), &wait);
Jennifer Hunteac37312007-02-08 13:51:54 -0800973
974 if (err)
975 goto done;
976
977 newsock->state = SS_CONNECTED;
978
979done:
980 release_sock(sk);
981 return err;
982}
983
984static int iucv_sock_getname(struct socket *sock, struct sockaddr *addr,
985 int *len, int peer)
986{
987 struct sockaddr_iucv *siucv = (struct sockaddr_iucv *) addr;
988 struct sock *sk = sock->sk;
Ursula Braun493d3972011-08-08 01:33:52 +0000989 struct iucv_sock *iucv = iucv_sk(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800990
991 addr->sa_family = AF_IUCV;
992 *len = sizeof(struct sockaddr_iucv);
993
994 if (peer) {
Ursula Braun493d3972011-08-08 01:33:52 +0000995 memcpy(siucv->siucv_user_id, iucv->dst_user_id, 8);
996 memcpy(siucv->siucv_name, iucv->dst_name, 8);
Jennifer Hunteac37312007-02-08 13:51:54 -0800997 } else {
Ursula Braun493d3972011-08-08 01:33:52 +0000998 memcpy(siucv->siucv_user_id, iucv->src_user_id, 8);
999 memcpy(siucv->siucv_name, iucv->src_name, 8);
Jennifer Hunteac37312007-02-08 13:51:54 -08001000 }
1001 memset(&siucv->siucv_port, 0, sizeof(siucv->siucv_port));
1002 memset(&siucv->siucv_addr, 0, sizeof(siucv->siucv_addr));
Ursula Braun493d3972011-08-08 01:33:52 +00001003 memset(&siucv->siucv_nodeid, 0, sizeof(siucv->siucv_nodeid));
Jennifer Hunteac37312007-02-08 13:51:54 -08001004
1005 return 0;
1006}
1007
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001008/**
1009 * iucv_send_iprm() - Send socket data in parameter list of an iucv message.
1010 * @path: IUCV path
1011 * @msg: Pointer to a struct iucv_message
1012 * @skb: The socket data to send, skb->len MUST BE <= 7
1013 *
1014 * Send the socket data in the parameter list in the iucv message
1015 * (IUCV_IPRMDATA). The socket data is stored at index 0 to 6 in the parameter
1016 * list and the socket data len at index 7 (last byte).
1017 * See also iucv_msg_length().
1018 *
1019 * Returns the error code from the iucv_message_send() call.
1020 */
1021static int iucv_send_iprm(struct iucv_path *path, struct iucv_message *msg,
1022 struct sk_buff *skb)
1023{
1024 u8 prmdata[8];
1025
1026 memcpy(prmdata, (void *) skb->data, skb->len);
1027 prmdata[7] = 0xff - (u8) skb->len;
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001028 return pr_iucv->message_send(path, msg, IUCV_IPRMDATA, 0,
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001029 (void *) prmdata, 8);
1030}
1031
Jennifer Hunteac37312007-02-08 13:51:54 -08001032static int iucv_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
1033 struct msghdr *msg, size_t len)
1034{
1035 struct sock *sk = sock->sk;
1036 struct iucv_sock *iucv = iucv_sk(sk);
1037 struct sk_buff *skb;
1038 struct iucv_message txmsg;
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001039 struct cmsghdr *cmsg;
1040 int cmsg_done;
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +00001041 long timeo;
Ursula Braun8f7c5022008-12-25 13:39:47 +01001042 char user_id[9];
1043 char appl_id[9];
Jennifer Hunteac37312007-02-08 13:51:54 -08001044 int err;
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +00001045 int noblock = msg->msg_flags & MSG_DONTWAIT;
Jennifer Hunteac37312007-02-08 13:51:54 -08001046
1047 err = sock_error(sk);
1048 if (err)
1049 return err;
1050
1051 if (msg->msg_flags & MSG_OOB)
1052 return -EOPNOTSUPP;
1053
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001054 /* SOCK_SEQPACKET: we do not support segmented records */
1055 if (sk->sk_type == SOCK_SEQPACKET && !(msg->msg_flags & MSG_EOR))
1056 return -EOPNOTSUPP;
1057
Jennifer Hunteac37312007-02-08 13:51:54 -08001058 lock_sock(sk);
1059
1060 if (sk->sk_shutdown & SEND_SHUTDOWN) {
1061 err = -EPIPE;
1062 goto out;
1063 }
1064
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001065 /* Return if the socket is not in connected state */
1066 if (sk->sk_state != IUCV_CONNECTED) {
1067 err = -ENOTCONN;
1068 goto out;
1069 }
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001070
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001071 /* initialize defaults */
1072 cmsg_done = 0; /* check for duplicate headers */
1073 txmsg.class = 0;
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001074
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001075 /* iterate over control messages */
1076 for (cmsg = CMSG_FIRSTHDR(msg); cmsg;
1077 cmsg = CMSG_NXTHDR(msg, cmsg)) {
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001078
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001079 if (!CMSG_OK(msg, cmsg)) {
1080 err = -EINVAL;
Jennifer Hunt561e0362007-05-04 12:22:07 -07001081 goto out;
Jennifer Hunteac37312007-02-08 13:51:54 -08001082 }
1083
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001084 if (cmsg->cmsg_level != SOL_IUCV)
1085 continue;
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001086
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001087 if (cmsg->cmsg_type & cmsg_done) {
1088 err = -EINVAL;
1089 goto out;
1090 }
1091 cmsg_done |= cmsg->cmsg_type;
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001092
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001093 switch (cmsg->cmsg_type) {
1094 case SCM_IUCV_TRGCLS:
1095 if (cmsg->cmsg_len != CMSG_LEN(TRGCLS_SIZE)) {
1096 err = -EINVAL;
1097 goto out;
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001098 }
1099
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001100 /* set iucv message target class */
1101 memcpy(&txmsg.class,
1102 (void *) CMSG_DATA(cmsg), TRGCLS_SIZE);
1103
1104 break;
1105
1106 default:
1107 err = -EINVAL;
1108 goto out;
1109 break;
1110 }
1111 }
1112
1113 /* allocate one skb for each iucv message:
1114 * this is fine for SOCK_SEQPACKET (unless we want to support
1115 * segmented records using the MSG_EOR flag), but
1116 * for SOCK_STREAM we might want to improve it in future */
Ursula Braun3881ac42011-08-08 01:33:54 +00001117 if (iucv->transport == AF_IUCV_TRANS_HIPER)
1118 skb = sock_alloc_send_skb(sk,
1119 len + sizeof(struct af_iucv_trans_hdr) + ETH_HLEN,
1120 noblock, &err);
1121 else
1122 skb = sock_alloc_send_skb(sk, len, noblock, &err);
Ursula Braun800c5eb2012-02-08 00:19:47 +00001123 if (!skb) {
1124 err = -ENOMEM;
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001125 goto out;
Ursula Braun800c5eb2012-02-08 00:19:47 +00001126 }
Ursula Braun3881ac42011-08-08 01:33:54 +00001127 if (iucv->transport == AF_IUCV_TRANS_HIPER)
1128 skb_reserve(skb, sizeof(struct af_iucv_trans_hdr) + ETH_HLEN);
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001129 if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
1130 err = -EFAULT;
1131 goto fail;
1132 }
1133
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +00001134 /* wait if outstanding messages for iucv path has reached */
1135 timeo = sock_sndtimeo(sk, noblock);
1136 err = iucv_sock_wait(sk, iucv_below_msglim(sk), timeo);
1137 if (err)
1138 goto fail;
1139
1140 /* return -ECONNRESET if the socket is no longer connected */
1141 if (sk->sk_state != IUCV_CONNECTED) {
1142 err = -ECONNRESET;
1143 goto fail;
1144 }
1145
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001146 /* increment and save iucv message tag for msg_completion cbk */
1147 txmsg.tag = iucv->send_tag++;
1148 memcpy(CB_TAG(skb), &txmsg.tag, CB_TAG_LEN);
Ursula Braun800c5eb2012-02-08 00:19:47 +00001149
Ursula Braun3881ac42011-08-08 01:33:54 +00001150 if (iucv->transport == AF_IUCV_TRANS_HIPER) {
1151 atomic_inc(&iucv->msg_sent);
1152 err = afiucv_hs_send(&txmsg, sk, skb, 0);
1153 if (err) {
1154 atomic_dec(&iucv->msg_sent);
1155 goto fail;
1156 }
1157 goto release;
1158 }
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001159 skb_queue_tail(&iucv->send_skb_q, skb);
1160
1161 if (((iucv->path->flags & IUCV_IPRMDATA) & iucv->flags)
1162 && skb->len <= 7) {
1163 err = iucv_send_iprm(iucv->path, &txmsg, skb);
1164
1165 /* on success: there is no message_complete callback
1166 * for an IPRMDATA msg; remove skb from send queue */
1167 if (err == 0) {
1168 skb_unlink(skb, &iucv->send_skb_q);
1169 kfree_skb(skb);
1170 }
1171
1172 /* this error should never happen since the
1173 * IUCV_IPRMDATA path flag is set... sever path */
1174 if (err == 0x15) {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001175 pr_iucv->path_sever(iucv->path, NULL);
Jennifer Hunteac37312007-02-08 13:51:54 -08001176 skb_unlink(skb, &iucv->send_skb_q);
1177 err = -EPIPE;
1178 goto fail;
1179 }
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001180 } else
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001181 err = pr_iucv->message_send(iucv->path, &txmsg, 0, 0,
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001182 (void *) skb->data, skb->len);
1183 if (err) {
1184 if (err == 3) {
1185 user_id[8] = 0;
1186 memcpy(user_id, iucv->dst_user_id, 8);
1187 appl_id[8] = 0;
1188 memcpy(appl_id, iucv->dst_name, 8);
1189 pr_err("Application %s on z/VM guest %s"
1190 " exceeds message limit\n",
1191 appl_id, user_id);
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +00001192 err = -EAGAIN;
1193 } else
1194 err = -EPIPE;
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001195 skb_unlink(skb, &iucv->send_skb_q);
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001196 goto fail;
Jennifer Hunteac37312007-02-08 13:51:54 -08001197 }
1198
Ursula Braun3881ac42011-08-08 01:33:54 +00001199release:
Jennifer Hunteac37312007-02-08 13:51:54 -08001200 release_sock(sk);
1201 return len;
1202
1203fail:
1204 kfree_skb(skb);
1205out:
1206 release_sock(sk);
1207 return err;
1208}
1209
Hendrik Bruecknerbf95d202009-09-16 04:37:28 +00001210/* iucv_fragment_skb() - Fragment a single IUCV message into multiple skb's
1211 *
1212 * Locking: must be called with message_q.lock held
1213 */
Ursula Braunf0703c82007-10-08 02:03:31 -07001214static int iucv_fragment_skb(struct sock *sk, struct sk_buff *skb, int len)
1215{
1216 int dataleft, size, copied = 0;
1217 struct sk_buff *nskb;
1218
1219 dataleft = len;
1220 while (dataleft) {
1221 if (dataleft >= sk->sk_rcvbuf / 4)
1222 size = sk->sk_rcvbuf / 4;
1223 else
1224 size = dataleft;
1225
1226 nskb = alloc_skb(size, GFP_ATOMIC | GFP_DMA);
1227 if (!nskb)
1228 return -ENOMEM;
1229
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001230 /* copy target class to control buffer of new skb */
1231 memcpy(CB_TRGCLS(nskb), CB_TRGCLS(skb), CB_TRGCLS_LEN);
1232
1233 /* copy data fragment */
Ursula Braunf0703c82007-10-08 02:03:31 -07001234 memcpy(nskb->data, skb->data + copied, size);
1235 copied += size;
1236 dataleft -= size;
1237
1238 skb_reset_transport_header(nskb);
1239 skb_reset_network_header(nskb);
1240 nskb->len = size;
1241
1242 skb_queue_tail(&iucv_sk(sk)->backlog_skb_q, nskb);
1243 }
1244
1245 return 0;
1246}
1247
Hendrik Bruecknerbf95d202009-09-16 04:37:28 +00001248/* iucv_process_message() - Receive a single outstanding IUCV message
1249 *
1250 * Locking: must be called with message_q.lock held
1251 */
Ursula Braunf0703c82007-10-08 02:03:31 -07001252static void iucv_process_message(struct sock *sk, struct sk_buff *skb,
1253 struct iucv_path *path,
1254 struct iucv_message *msg)
1255{
1256 int rc;
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001257 unsigned int len;
Ursula Braunf0703c82007-10-08 02:03:31 -07001258
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001259 len = iucv_msg_length(msg);
1260
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001261 /* store msg target class in the second 4 bytes of skb ctrl buffer */
1262 /* Note: the first 4 bytes are reserved for msg tag */
1263 memcpy(CB_TRGCLS(skb), &msg->class, CB_TRGCLS_LEN);
1264
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001265 /* check for special IPRM messages (e.g. iucv_sock_shutdown) */
1266 if ((msg->flags & IUCV_IPRMDATA) && len > 7) {
1267 if (memcmp(msg->rmmsg, iprm_shutdown, 8) == 0) {
1268 skb->data = NULL;
1269 skb->len = 0;
1270 }
Ursula Braunf0703c82007-10-08 02:03:31 -07001271 } else {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001272 rc = pr_iucv->message_receive(path, msg,
1273 msg->flags & IUCV_IPRMDATA,
1274 skb->data, len, NULL);
Ursula Braunf0703c82007-10-08 02:03:31 -07001275 if (rc) {
1276 kfree_skb(skb);
1277 return;
1278 }
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001279 /* we need to fragment iucv messages for SOCK_STREAM only;
1280 * for SOCK_SEQPACKET, it is only relevant if we support
1281 * record segmentation using MSG_EOR (see also recvmsg()) */
1282 if (sk->sk_type == SOCK_STREAM &&
1283 skb->truesize >= sk->sk_rcvbuf / 4) {
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001284 rc = iucv_fragment_skb(sk, skb, len);
Ursula Braunf0703c82007-10-08 02:03:31 -07001285 kfree_skb(skb);
1286 skb = NULL;
1287 if (rc) {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001288 pr_iucv->path_sever(path, NULL);
Ursula Braunf0703c82007-10-08 02:03:31 -07001289 return;
1290 }
1291 skb = skb_dequeue(&iucv_sk(sk)->backlog_skb_q);
1292 } else {
1293 skb_reset_transport_header(skb);
1294 skb_reset_network_header(skb);
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001295 skb->len = len;
Ursula Braunf0703c82007-10-08 02:03:31 -07001296 }
1297 }
1298
1299 if (sock_queue_rcv_skb(sk, skb))
1300 skb_queue_head(&iucv_sk(sk)->backlog_skb_q, skb);
1301}
1302
Hendrik Bruecknerbf95d202009-09-16 04:37:28 +00001303/* iucv_process_message_q() - Process outstanding IUCV messages
1304 *
1305 * Locking: must be called with message_q.lock held
1306 */
Ursula Braunf0703c82007-10-08 02:03:31 -07001307static void iucv_process_message_q(struct sock *sk)
1308{
1309 struct iucv_sock *iucv = iucv_sk(sk);
1310 struct sk_buff *skb;
1311 struct sock_msg_q *p, *n;
1312
1313 list_for_each_entry_safe(p, n, &iucv->message_q.list, list) {
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001314 skb = alloc_skb(iucv_msg_length(&p->msg), GFP_ATOMIC | GFP_DMA);
Ursula Braunf0703c82007-10-08 02:03:31 -07001315 if (!skb)
1316 break;
1317 iucv_process_message(sk, skb, p->path, &p->msg);
1318 list_del(&p->list);
1319 kfree(p);
1320 if (!skb_queue_empty(&iucv->backlog_skb_q))
1321 break;
1322 }
1323}
1324
Jennifer Hunteac37312007-02-08 13:51:54 -08001325static int iucv_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
1326 struct msghdr *msg, size_t len, int flags)
1327{
1328 int noblock = flags & MSG_DONTWAIT;
1329 struct sock *sk = sock->sk;
Jennifer Hunt561e0362007-05-04 12:22:07 -07001330 struct iucv_sock *iucv = iucv_sk(sk);
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001331 unsigned int copied, rlen;
Ursula Braun9fbd87d2012-03-07 02:06:23 +00001332 struct sk_buff *skb, *rskb, *cskb;
Jennifer Hunteac37312007-02-08 13:51:54 -08001333 int err = 0;
1334
Ursula Braunaac63992011-12-19 22:56:31 +00001335 if ((sk->sk_state == IUCV_DISCONN) &&
Ursula Braunf0703c82007-10-08 02:03:31 -07001336 skb_queue_empty(&iucv->backlog_skb_q) &&
1337 skb_queue_empty(&sk->sk_receive_queue) &&
1338 list_empty(&iucv->message_q.list))
Jennifer Hunt561e0362007-05-04 12:22:07 -07001339 return 0;
1340
Jennifer Hunteac37312007-02-08 13:51:54 -08001341 if (flags & (MSG_OOB))
1342 return -EOPNOTSUPP;
1343
Hendrik Brueckner60d37052009-04-21 06:04:21 +00001344 /* receive/dequeue next skb:
1345 * the function understands MSG_PEEK and, thus, does not dequeue skb */
Jennifer Hunteac37312007-02-08 13:51:54 -08001346 skb = skb_recv_datagram(sk, flags, noblock, &err);
1347 if (!skb) {
1348 if (sk->sk_shutdown & RCV_SHUTDOWN)
1349 return 0;
1350 return err;
1351 }
1352
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001353 rlen = skb->len; /* real length of skb */
1354 copied = min_t(unsigned int, rlen, len);
Ursula Braun82492a32012-03-07 02:06:24 +00001355 if (!rlen)
1356 sk->sk_shutdown = sk->sk_shutdown | RCV_SHUTDOWN;
Jennifer Hunteac37312007-02-08 13:51:54 -08001357
Jennifer Hunt561e0362007-05-04 12:22:07 -07001358 cskb = skb;
Ursula Braun3881ac42011-08-08 01:33:54 +00001359 if (skb_copy_datagram_iovec(cskb, 0, msg->msg_iov, copied)) {
Hendrik Brueckner802788b2009-04-21 23:26:26 +00001360 if (!(flags & MSG_PEEK))
1361 skb_queue_head(&sk->sk_receive_queue, skb);
1362 return -EFAULT;
Jennifer Hunteac37312007-02-08 13:51:54 -08001363 }
1364
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001365 /* SOCK_SEQPACKET: set MSG_TRUNC if recv buf size is too small */
1366 if (sk->sk_type == SOCK_SEQPACKET) {
1367 if (copied < rlen)
1368 msg->msg_flags |= MSG_TRUNC;
1369 /* each iucv message contains a complete record */
1370 msg->msg_flags |= MSG_EOR;
1371 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001372
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001373 /* create control message to store iucv msg target class:
1374 * get the trgcls from the control buffer of the skb due to
1375 * fragmentation of original iucv message. */
1376 err = put_cmsg(msg, SOL_IUCV, SCM_IUCV_TRGCLS,
1377 CB_TRGCLS_LEN, CB_TRGCLS(skb));
1378 if (err) {
1379 if (!(flags & MSG_PEEK))
1380 skb_queue_head(&sk->sk_receive_queue, skb);
1381 return err;
1382 }
1383
Jennifer Hunteac37312007-02-08 13:51:54 -08001384 /* Mark read part of skb as used */
1385 if (!(flags & MSG_PEEK)) {
Jennifer Hunteac37312007-02-08 13:51:54 -08001386
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001387 /* SOCK_STREAM: re-queue skb if it contains unreceived data */
1388 if (sk->sk_type == SOCK_STREAM) {
1389 skb_pull(skb, copied);
1390 if (skb->len) {
1391 skb_queue_head(&sk->sk_receive_queue, skb);
1392 goto done;
1393 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001394 }
1395
1396 kfree_skb(skb);
Ursula Braun800c5eb2012-02-08 00:19:47 +00001397 if (iucv->transport == AF_IUCV_TRANS_HIPER) {
1398 atomic_inc(&iucv->msg_recv);
1399 if (atomic_read(&iucv->msg_recv) > iucv->msglimit) {
1400 WARN_ON(1);
1401 iucv_sock_close(sk);
1402 return -EFAULT;
1403 }
1404 }
Jennifer Hunt561e0362007-05-04 12:22:07 -07001405
1406 /* Queue backlog skbs */
Hendrik Bruecknerbf95d202009-09-16 04:37:28 +00001407 spin_lock_bh(&iucv->message_q.lock);
Ursula Braunf0703c82007-10-08 02:03:31 -07001408 rskb = skb_dequeue(&iucv->backlog_skb_q);
Heiko Carstensda99f052007-05-04 12:23:27 -07001409 while (rskb) {
Jennifer Hunt561e0362007-05-04 12:22:07 -07001410 if (sock_queue_rcv_skb(sk, rskb)) {
Ursula Braunf0703c82007-10-08 02:03:31 -07001411 skb_queue_head(&iucv->backlog_skb_q,
Jennifer Hunt561e0362007-05-04 12:22:07 -07001412 rskb);
1413 break;
1414 } else {
Ursula Braunf0703c82007-10-08 02:03:31 -07001415 rskb = skb_dequeue(&iucv->backlog_skb_q);
Jennifer Hunt561e0362007-05-04 12:22:07 -07001416 }
1417 }
Ursula Braunf0703c82007-10-08 02:03:31 -07001418 if (skb_queue_empty(&iucv->backlog_skb_q)) {
Ursula Braunf0703c82007-10-08 02:03:31 -07001419 if (!list_empty(&iucv->message_q.list))
1420 iucv_process_message_q(sk);
Ursula Braun3881ac42011-08-08 01:33:54 +00001421 if (atomic_read(&iucv->msg_recv) >=
1422 iucv->msglimit / 2) {
Ursula Braun9fbd87d2012-03-07 02:06:23 +00001423 err = iucv_send_ctrl(sk, AF_IUCV_FLAG_WIN);
Ursula Braun3881ac42011-08-08 01:33:54 +00001424 if (err) {
1425 sk->sk_state = IUCV_DISCONN;
1426 sk->sk_state_change(sk);
1427 }
1428 }
Ursula Braunf0703c82007-10-08 02:03:31 -07001429 }
Hendrik Bruecknerbf95d202009-09-16 04:37:28 +00001430 spin_unlock_bh(&iucv->message_q.lock);
Hendrik Brueckner60d37052009-04-21 06:04:21 +00001431 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001432
1433done:
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001434 /* SOCK_SEQPACKET: return real length if MSG_TRUNC is set */
1435 if (sk->sk_type == SOCK_SEQPACKET && (flags & MSG_TRUNC))
1436 copied = rlen;
1437
1438 return copied;
Jennifer Hunteac37312007-02-08 13:51:54 -08001439}
1440
1441static inline unsigned int iucv_accept_poll(struct sock *parent)
1442{
1443 struct iucv_sock *isk, *n;
1444 struct sock *sk;
1445
Heiko Carstensda99f052007-05-04 12:23:27 -07001446 list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) {
Jennifer Hunteac37312007-02-08 13:51:54 -08001447 sk = (struct sock *) isk;
1448
1449 if (sk->sk_state == IUCV_CONNECTED)
1450 return POLLIN | POLLRDNORM;
1451 }
1452
1453 return 0;
1454}
1455
1456unsigned int iucv_sock_poll(struct file *file, struct socket *sock,
1457 poll_table *wait)
1458{
1459 struct sock *sk = sock->sk;
1460 unsigned int mask = 0;
1461
Eric Dumazetaa395142010-04-20 13:03:51 +00001462 sock_poll_wait(file, sk_sleep(sk), wait);
Jennifer Hunteac37312007-02-08 13:51:54 -08001463
1464 if (sk->sk_state == IUCV_LISTEN)
1465 return iucv_accept_poll(sk);
1466
1467 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
1468 mask |= POLLERR;
1469
1470 if (sk->sk_shutdown & RCV_SHUTDOWN)
1471 mask |= POLLRDHUP;
1472
1473 if (sk->sk_shutdown == SHUTDOWN_MASK)
1474 mask |= POLLHUP;
1475
1476 if (!skb_queue_empty(&sk->sk_receive_queue) ||
Heiko Carstensda99f052007-05-04 12:23:27 -07001477 (sk->sk_shutdown & RCV_SHUTDOWN))
Jennifer Hunteac37312007-02-08 13:51:54 -08001478 mask |= POLLIN | POLLRDNORM;
1479
1480 if (sk->sk_state == IUCV_CLOSED)
1481 mask |= POLLHUP;
1482
Ursula Braunaac63992011-12-19 22:56:31 +00001483 if (sk->sk_state == IUCV_DISCONN)
Jennifer Hunt561e0362007-05-04 12:22:07 -07001484 mask |= POLLIN;
1485
Ursula Braun7f1b0ea2012-02-08 00:19:46 +00001486 if (sock_writeable(sk) && iucv_below_msglim(sk))
Jennifer Hunteac37312007-02-08 13:51:54 -08001487 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
1488 else
1489 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
1490
1491 return mask;
1492}
1493
1494static int iucv_sock_shutdown(struct socket *sock, int how)
1495{
1496 struct sock *sk = sock->sk;
1497 struct iucv_sock *iucv = iucv_sk(sk);
1498 struct iucv_message txmsg;
1499 int err = 0;
Jennifer Hunteac37312007-02-08 13:51:54 -08001500
1501 how++;
1502
1503 if ((how & ~SHUTDOWN_MASK) || !how)
1504 return -EINVAL;
1505
1506 lock_sock(sk);
Heiko Carstensda99f052007-05-04 12:23:27 -07001507 switch (sk->sk_state) {
Ursula Braun82492a32012-03-07 02:06:24 +00001508 case IUCV_LISTEN:
Hendrik Bruecknere14ad5f2009-04-21 06:04:23 +00001509 case IUCV_DISCONN:
1510 case IUCV_CLOSING:
Jennifer Hunteac37312007-02-08 13:51:54 -08001511 case IUCV_CLOSED:
1512 err = -ENOTCONN;
1513 goto fail;
Jennifer Hunteac37312007-02-08 13:51:54 -08001514 default:
Jennifer Hunteac37312007-02-08 13:51:54 -08001515 break;
1516 }
1517
1518 if (how == SEND_SHUTDOWN || how == SHUTDOWN_MASK) {
Ursula Braun82492a32012-03-07 02:06:24 +00001519 if (iucv->transport == AF_IUCV_TRANS_IUCV) {
1520 txmsg.class = 0;
1521 txmsg.tag = 0;
1522 err = pr_iucv->message_send(iucv->path, &txmsg,
1523 IUCV_IPRMDATA, 0, (void *) iprm_shutdown, 8);
1524 if (err) {
1525 switch (err) {
1526 case 1:
1527 err = -ENOTCONN;
1528 break;
1529 case 2:
1530 err = -ECONNRESET;
1531 break;
1532 default:
1533 err = -ENOTCONN;
1534 break;
1535 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001536 }
Ursula Braun82492a32012-03-07 02:06:24 +00001537 } else
1538 iucv_send_ctrl(sk, AF_IUCV_FLAG_SHT);
Jennifer Hunteac37312007-02-08 13:51:54 -08001539 }
1540
Ursula Braun82492a32012-03-07 02:06:24 +00001541 sk->sk_shutdown |= how;
Jennifer Hunteac37312007-02-08 13:51:54 -08001542 if (how == RCV_SHUTDOWN || how == SHUTDOWN_MASK) {
Ursula Braun82492a32012-03-07 02:06:24 +00001543 if (iucv->transport == AF_IUCV_TRANS_IUCV) {
1544 err = pr_iucv->path_quiesce(iucv->path, NULL);
1545 if (err)
1546 err = -ENOTCONN;
1547/* skb_queue_purge(&sk->sk_receive_queue); */
1548 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001549 skb_queue_purge(&sk->sk_receive_queue);
1550 }
1551
1552 /* Wake up anyone sleeping in poll */
1553 sk->sk_state_change(sk);
1554
1555fail:
1556 release_sock(sk);
1557 return err;
1558}
1559
1560static int iucv_sock_release(struct socket *sock)
1561{
1562 struct sock *sk = sock->sk;
1563 int err = 0;
1564
1565 if (!sk)
1566 return 0;
1567
1568 iucv_sock_close(sk);
1569
Jennifer Hunteac37312007-02-08 13:51:54 -08001570 sock_orphan(sk);
1571 iucv_sock_kill(sk);
1572 return err;
1573}
1574
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00001575/* getsockopt and setsockopt */
1576static int iucv_sock_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07001577 char __user *optval, unsigned int optlen)
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00001578{
1579 struct sock *sk = sock->sk;
1580 struct iucv_sock *iucv = iucv_sk(sk);
1581 int val;
1582 int rc;
1583
1584 if (level != SOL_IUCV)
1585 return -ENOPROTOOPT;
1586
1587 if (optlen < sizeof(int))
1588 return -EINVAL;
1589
1590 if (get_user(val, (int __user *) optval))
1591 return -EFAULT;
1592
1593 rc = 0;
1594
1595 lock_sock(sk);
1596 switch (optname) {
1597 case SO_IPRMDATA_MSG:
1598 if (val)
1599 iucv->flags |= IUCV_IPRMDATA;
1600 else
1601 iucv->flags &= ~IUCV_IPRMDATA;
1602 break;
Hendrik Brueckner09488e2e2009-04-21 23:26:27 +00001603 case SO_MSGLIMIT:
1604 switch (sk->sk_state) {
1605 case IUCV_OPEN:
1606 case IUCV_BOUND:
1607 if (val < 1 || val > (u16)(~0))
1608 rc = -EINVAL;
1609 else
1610 iucv->msglimit = val;
1611 break;
1612 default:
1613 rc = -EINVAL;
1614 break;
1615 }
1616 break;
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00001617 default:
1618 rc = -ENOPROTOOPT;
1619 break;
1620 }
1621 release_sock(sk);
1622
1623 return rc;
1624}
1625
1626static int iucv_sock_getsockopt(struct socket *sock, int level, int optname,
1627 char __user *optval, int __user *optlen)
1628{
1629 struct sock *sk = sock->sk;
1630 struct iucv_sock *iucv = iucv_sk(sk);
Ursula Braun51363b82012-02-08 00:19:48 +00001631 unsigned int val;
1632 int len;
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00001633
1634 if (level != SOL_IUCV)
1635 return -ENOPROTOOPT;
1636
1637 if (get_user(len, optlen))
1638 return -EFAULT;
1639
1640 if (len < 0)
1641 return -EINVAL;
1642
1643 len = min_t(unsigned int, len, sizeof(int));
1644
1645 switch (optname) {
1646 case SO_IPRMDATA_MSG:
1647 val = (iucv->flags & IUCV_IPRMDATA) ? 1 : 0;
1648 break;
Hendrik Brueckner09488e2e2009-04-21 23:26:27 +00001649 case SO_MSGLIMIT:
1650 lock_sock(sk);
1651 val = (iucv->path != NULL) ? iucv->path->msglim /* connected */
1652 : iucv->msglimit; /* default */
1653 release_sock(sk);
1654 break;
Ursula Braun51363b82012-02-08 00:19:48 +00001655 case SO_MSGSIZE:
1656 if (sk->sk_state == IUCV_OPEN)
1657 return -EBADFD;
1658 val = (iucv->hs_dev) ? iucv->hs_dev->mtu -
1659 sizeof(struct af_iucv_trans_hdr) - ETH_HLEN :
1660 0x7fffffff;
1661 break;
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00001662 default:
1663 return -ENOPROTOOPT;
1664 }
1665
1666 if (put_user(len, optlen))
1667 return -EFAULT;
1668 if (copy_to_user(optval, &val, len))
1669 return -EFAULT;
1670
1671 return 0;
1672}
1673
1674
Jennifer Hunteac37312007-02-08 13:51:54 -08001675/* Callback wrappers - called from iucv base support */
1676static int iucv_callback_connreq(struct iucv_path *path,
1677 u8 ipvmid[8], u8 ipuser[16])
1678{
1679 unsigned char user_data[16];
1680 unsigned char nuser_data[16];
1681 unsigned char src_name[8];
1682 struct hlist_node *node;
1683 struct sock *sk, *nsk;
1684 struct iucv_sock *iucv, *niucv;
1685 int err;
1686
1687 memcpy(src_name, ipuser, 8);
1688 EBCASC(src_name, 8);
1689 /* Find out if this path belongs to af_iucv. */
1690 read_lock(&iucv_sk_list.lock);
1691 iucv = NULL;
Ursula Braunfebca282007-07-14 19:04:25 -07001692 sk = NULL;
Jennifer Hunteac37312007-02-08 13:51:54 -08001693 sk_for_each(sk, node, &iucv_sk_list.head)
1694 if (sk->sk_state == IUCV_LISTEN &&
1695 !memcmp(&iucv_sk(sk)->src_name, src_name, 8)) {
1696 /*
1697 * Found a listening socket with
1698 * src_name == ipuser[0-7].
1699 */
1700 iucv = iucv_sk(sk);
1701 break;
1702 }
1703 read_unlock(&iucv_sk_list.lock);
1704 if (!iucv)
1705 /* No socket found, not one of our paths. */
1706 return -EINVAL;
1707
1708 bh_lock_sock(sk);
1709
1710 /* Check if parent socket is listening */
1711 low_nmcpy(user_data, iucv->src_name);
1712 high_nmcpy(user_data, iucv->dst_name);
1713 ASCEBC(user_data, sizeof(user_data));
1714 if (sk->sk_state != IUCV_LISTEN) {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001715 err = pr_iucv->path_sever(path, user_data);
Hendrik Brueckner65dbd7c2009-01-05 18:08:23 -08001716 iucv_path_free(path);
Jennifer Hunteac37312007-02-08 13:51:54 -08001717 goto fail;
1718 }
1719
1720 /* Check for backlog size */
1721 if (sk_acceptq_is_full(sk)) {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001722 err = pr_iucv->path_sever(path, user_data);
Hendrik Brueckner65dbd7c2009-01-05 18:08:23 -08001723 iucv_path_free(path);
Jennifer Hunteac37312007-02-08 13:51:54 -08001724 goto fail;
1725 }
1726
1727 /* Create the new socket */
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001728 nsk = iucv_sock_alloc(NULL, sk->sk_type, GFP_ATOMIC);
Heiko Carstensda99f052007-05-04 12:23:27 -07001729 if (!nsk) {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001730 err = pr_iucv->path_sever(path, user_data);
Hendrik Brueckner65dbd7c2009-01-05 18:08:23 -08001731 iucv_path_free(path);
Jennifer Hunteac37312007-02-08 13:51:54 -08001732 goto fail;
1733 }
1734
1735 niucv = iucv_sk(nsk);
1736 iucv_sock_init(nsk, sk);
1737
1738 /* Set the new iucv_sock */
1739 memcpy(niucv->dst_name, ipuser + 8, 8);
1740 EBCASC(niucv->dst_name, 8);
1741 memcpy(niucv->dst_user_id, ipvmid, 8);
1742 memcpy(niucv->src_name, iucv->src_name, 8);
1743 memcpy(niucv->src_user_id, iucv->src_user_id, 8);
1744 niucv->path = path;
1745
1746 /* Call iucv_accept */
1747 high_nmcpy(nuser_data, ipuser + 8);
1748 memcpy(nuser_data + 8, niucv->src_name, 8);
1749 ASCEBC(nuser_data + 8, 8);
1750
Hendrik Brueckner09488e2e2009-04-21 23:26:27 +00001751 /* set message limit for path based on msglimit of accepting socket */
1752 niucv->msglimit = iucv->msglimit;
1753 path->msglim = iucv->msglimit;
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001754 err = pr_iucv->path_accept(path, &af_iucv_handler, nuser_data, nsk);
Heiko Carstensda99f052007-05-04 12:23:27 -07001755 if (err) {
Ursula Braun7d316b92012-02-08 00:19:45 +00001756 iucv_sever_path(nsk, 1);
Hendrik Brueckner65dbd7c2009-01-05 18:08:23 -08001757 iucv_sock_kill(nsk);
Jennifer Hunteac37312007-02-08 13:51:54 -08001758 goto fail;
1759 }
1760
1761 iucv_accept_enqueue(sk, nsk);
1762
1763 /* Wake up accept */
1764 nsk->sk_state = IUCV_CONNECTED;
1765 sk->sk_data_ready(sk, 1);
1766 err = 0;
1767fail:
1768 bh_unlock_sock(sk);
1769 return 0;
1770}
1771
1772static void iucv_callback_connack(struct iucv_path *path, u8 ipuser[16])
1773{
1774 struct sock *sk = path->private;
1775
1776 sk->sk_state = IUCV_CONNECTED;
1777 sk->sk_state_change(sk);
1778}
1779
1780static void iucv_callback_rx(struct iucv_path *path, struct iucv_message *msg)
1781{
1782 struct sock *sk = path->private;
Jennifer Hunt561e0362007-05-04 12:22:07 -07001783 struct iucv_sock *iucv = iucv_sk(sk);
Ursula Braunf0703c82007-10-08 02:03:31 -07001784 struct sk_buff *skb;
1785 struct sock_msg_q *save_msg;
1786 int len;
Jennifer Hunt561e0362007-05-04 12:22:07 -07001787
Hendrik Bruecknerfe86e542009-04-21 06:04:22 +00001788 if (sk->sk_shutdown & RCV_SHUTDOWN) {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001789 pr_iucv->message_reject(path, msg);
Jennifer Hunteac37312007-02-08 13:51:54 -08001790 return;
Hendrik Bruecknerfe86e542009-04-21 06:04:22 +00001791 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001792
Hendrik Brueckner3fa6b5a2009-04-21 06:04:24 +00001793 spin_lock(&iucv->message_q.lock);
Jennifer Hunteac37312007-02-08 13:51:54 -08001794
Ursula Braunf0703c82007-10-08 02:03:31 -07001795 if (!list_empty(&iucv->message_q.list) ||
1796 !skb_queue_empty(&iucv->backlog_skb_q))
1797 goto save_message;
1798
1799 len = atomic_read(&sk->sk_rmem_alloc);
Eric Dumazet87fb4b72011-10-13 07:28:54 +00001800 len += SKB_TRUESIZE(iucv_msg_length(msg));
Ursula Braunf0703c82007-10-08 02:03:31 -07001801 if (len > sk->sk_rcvbuf)
1802 goto save_message;
1803
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001804 skb = alloc_skb(iucv_msg_length(msg), GFP_ATOMIC | GFP_DMA);
Ursula Braunf0703c82007-10-08 02:03:31 -07001805 if (!skb)
1806 goto save_message;
Jennifer Hunteac37312007-02-08 13:51:54 -08001807
Ursula Braunf0703c82007-10-08 02:03:31 -07001808 iucv_process_message(sk, skb, path, msg);
Hendrik Brueckner3fa6b5a2009-04-21 06:04:24 +00001809 goto out_unlock;
Jennifer Hunteac37312007-02-08 13:51:54 -08001810
Ursula Braunf0703c82007-10-08 02:03:31 -07001811save_message:
1812 save_msg = kzalloc(sizeof(struct sock_msg_q), GFP_ATOMIC | GFP_DMA);
Ursula Braund4444722008-02-07 18:07:19 -08001813 if (!save_msg)
Julia Lawalla56635a2010-05-26 05:56:48 +00001814 goto out_unlock;
Ursula Braunf0703c82007-10-08 02:03:31 -07001815 save_msg->path = path;
1816 save_msg->msg = *msg;
1817
Ursula Braunf0703c82007-10-08 02:03:31 -07001818 list_add_tail(&save_msg->list, &iucv->message_q.list);
Hendrik Brueckner3fa6b5a2009-04-21 06:04:24 +00001819
1820out_unlock:
Ursula Braunf0703c82007-10-08 02:03:31 -07001821 spin_unlock(&iucv->message_q.lock);
Jennifer Hunteac37312007-02-08 13:51:54 -08001822}
1823
1824static void iucv_callback_txdone(struct iucv_path *path,
1825 struct iucv_message *msg)
1826{
1827 struct sock *sk = path->private;
Ursula Braunf2a77992008-02-07 18:07:44 -08001828 struct sk_buff *this = NULL;
Jennifer Hunteac37312007-02-08 13:51:54 -08001829 struct sk_buff_head *list = &iucv_sk(sk)->send_skb_q;
1830 struct sk_buff *list_skb = list->next;
1831 unsigned long flags;
1832
Ursula Braun7d316b92012-02-08 00:19:45 +00001833 bh_lock_sock(sk);
Ursula Braunf2a77992008-02-07 18:07:44 -08001834 if (!skb_queue_empty(list)) {
Jennifer Hunt561e0362007-05-04 12:22:07 -07001835 spin_lock_irqsave(&list->lock, flags);
Jennifer Hunteac37312007-02-08 13:51:54 -08001836
Ursula Braunf2a77992008-02-07 18:07:44 -08001837 while (list_skb != (struct sk_buff *)list) {
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001838 if (!memcmp(&msg->tag, CB_TAG(list_skb), CB_TAG_LEN)) {
Ursula Braunf2a77992008-02-07 18:07:44 -08001839 this = list_skb;
1840 break;
1841 }
Jennifer Hunt561e0362007-05-04 12:22:07 -07001842 list_skb = list_skb->next;
Ursula Braunf2a77992008-02-07 18:07:44 -08001843 }
1844 if (this)
1845 __skb_unlink(this, list);
Jennifer Hunteac37312007-02-08 13:51:54 -08001846
Jennifer Hunt561e0362007-05-04 12:22:07 -07001847 spin_unlock_irqrestore(&list->lock, flags);
Jennifer Hunteac37312007-02-08 13:51:54 -08001848
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +00001849 if (this) {
1850 kfree_skb(this);
1851 /* wake up any process waiting for sending */
1852 iucv_sock_wake_msglim(sk);
1853 }
Jennifer Hunt561e0362007-05-04 12:22:07 -07001854 }
1855
Heiko Carstensda99f052007-05-04 12:23:27 -07001856 if (sk->sk_state == IUCV_CLOSING) {
Jennifer Hunt561e0362007-05-04 12:22:07 -07001857 if (skb_queue_empty(&iucv_sk(sk)->send_skb_q)) {
1858 sk->sk_state = IUCV_CLOSED;
1859 sk->sk_state_change(sk);
1860 }
1861 }
Ursula Braun7d316b92012-02-08 00:19:45 +00001862 bh_unlock_sock(sk);
Jennifer Hunt561e0362007-05-04 12:22:07 -07001863
Jennifer Hunteac37312007-02-08 13:51:54 -08001864}
1865
1866static void iucv_callback_connrej(struct iucv_path *path, u8 ipuser[16])
1867{
1868 struct sock *sk = path->private;
1869
Ursula Braun7d316b92012-02-08 00:19:45 +00001870 if (sk->sk_state == IUCV_CLOSED)
1871 return;
1872
1873 bh_lock_sock(sk);
1874 iucv_sever_path(sk, 1);
Ursula Braunaac63992011-12-19 22:56:31 +00001875 sk->sk_state = IUCV_DISCONN;
Jennifer Hunteac37312007-02-08 13:51:54 -08001876
1877 sk->sk_state_change(sk);
Ursula Braun7d316b92012-02-08 00:19:45 +00001878 bh_unlock_sock(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -08001879}
1880
Hendrik Brueckneraf88b522009-04-21 23:26:21 +00001881/* called if the other communication side shuts down its RECV direction;
1882 * in turn, the callback sets SEND_SHUTDOWN to disable sending of data.
1883 */
1884static void iucv_callback_shutdown(struct iucv_path *path, u8 ipuser[16])
1885{
1886 struct sock *sk = path->private;
1887
1888 bh_lock_sock(sk);
1889 if (sk->sk_state != IUCV_CLOSED) {
1890 sk->sk_shutdown |= SEND_SHUTDOWN;
1891 sk->sk_state_change(sk);
1892 }
1893 bh_unlock_sock(sk);
1894}
1895
Ursula Braun3881ac42011-08-08 01:33:54 +00001896/***************** HiperSockets transport callbacks ********************/
1897static void afiucv_swap_src_dest(struct sk_buff *skb)
1898{
1899 struct af_iucv_trans_hdr *trans_hdr =
1900 (struct af_iucv_trans_hdr *)skb->data;
1901 char tmpID[8];
1902 char tmpName[8];
1903
1904 ASCEBC(trans_hdr->destUserID, sizeof(trans_hdr->destUserID));
1905 ASCEBC(trans_hdr->destAppName, sizeof(trans_hdr->destAppName));
1906 ASCEBC(trans_hdr->srcUserID, sizeof(trans_hdr->srcUserID));
1907 ASCEBC(trans_hdr->srcAppName, sizeof(trans_hdr->srcAppName));
1908 memcpy(tmpID, trans_hdr->srcUserID, 8);
1909 memcpy(tmpName, trans_hdr->srcAppName, 8);
1910 memcpy(trans_hdr->srcUserID, trans_hdr->destUserID, 8);
1911 memcpy(trans_hdr->srcAppName, trans_hdr->destAppName, 8);
1912 memcpy(trans_hdr->destUserID, tmpID, 8);
1913 memcpy(trans_hdr->destAppName, tmpName, 8);
1914 skb_push(skb, ETH_HLEN);
1915 memset(skb->data, 0, ETH_HLEN);
1916}
1917
1918/**
1919 * afiucv_hs_callback_syn - react on received SYN
1920 **/
1921static int afiucv_hs_callback_syn(struct sock *sk, struct sk_buff *skb)
1922{
1923 struct sock *nsk;
1924 struct iucv_sock *iucv, *niucv;
1925 struct af_iucv_trans_hdr *trans_hdr;
1926 int err;
1927
1928 iucv = iucv_sk(sk);
1929 trans_hdr = (struct af_iucv_trans_hdr *)skb->data;
1930 if (!iucv) {
1931 /* no sock - connection refused */
1932 afiucv_swap_src_dest(skb);
1933 trans_hdr->flags = AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN;
1934 err = dev_queue_xmit(skb);
1935 goto out;
1936 }
1937
1938 nsk = iucv_sock_alloc(NULL, sk->sk_type, GFP_ATOMIC);
1939 bh_lock_sock(sk);
1940 if ((sk->sk_state != IUCV_LISTEN) ||
1941 sk_acceptq_is_full(sk) ||
1942 !nsk) {
1943 /* error on server socket - connection refused */
1944 if (nsk)
1945 sk_free(nsk);
1946 afiucv_swap_src_dest(skb);
1947 trans_hdr->flags = AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN;
1948 err = dev_queue_xmit(skb);
1949 bh_unlock_sock(sk);
1950 goto out;
1951 }
1952
1953 niucv = iucv_sk(nsk);
1954 iucv_sock_init(nsk, sk);
1955 niucv->transport = AF_IUCV_TRANS_HIPER;
1956 niucv->msglimit = iucv->msglimit;
1957 if (!trans_hdr->window)
1958 niucv->msglimit_peer = IUCV_HIPER_MSGLIM_DEFAULT;
1959 else
1960 niucv->msglimit_peer = trans_hdr->window;
1961 memcpy(niucv->dst_name, trans_hdr->srcAppName, 8);
1962 memcpy(niucv->dst_user_id, trans_hdr->srcUserID, 8);
1963 memcpy(niucv->src_name, iucv->src_name, 8);
1964 memcpy(niucv->src_user_id, iucv->src_user_id, 8);
1965 nsk->sk_bound_dev_if = sk->sk_bound_dev_if;
Ursula Braun800c5eb2012-02-08 00:19:47 +00001966 niucv->hs_dev = iucv->hs_dev;
1967 dev_hold(niucv->hs_dev);
Ursula Braun3881ac42011-08-08 01:33:54 +00001968 afiucv_swap_src_dest(skb);
1969 trans_hdr->flags = AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_ACK;
1970 trans_hdr->window = niucv->msglimit;
1971 /* if receiver acks the xmit connection is established */
1972 err = dev_queue_xmit(skb);
1973 if (!err) {
1974 iucv_accept_enqueue(sk, nsk);
1975 nsk->sk_state = IUCV_CONNECTED;
1976 sk->sk_data_ready(sk, 1);
1977 } else
1978 iucv_sock_kill(nsk);
1979 bh_unlock_sock(sk);
1980
1981out:
1982 return NET_RX_SUCCESS;
1983}
1984
1985/**
1986 * afiucv_hs_callback_synack() - react on received SYN-ACK
1987 **/
1988static int afiucv_hs_callback_synack(struct sock *sk, struct sk_buff *skb)
1989{
1990 struct iucv_sock *iucv = iucv_sk(sk);
1991 struct af_iucv_trans_hdr *trans_hdr =
1992 (struct af_iucv_trans_hdr *)skb->data;
1993
1994 if (!iucv)
1995 goto out;
1996 if (sk->sk_state != IUCV_BOUND)
1997 goto out;
1998 bh_lock_sock(sk);
1999 iucv->msglimit_peer = trans_hdr->window;
2000 sk->sk_state = IUCV_CONNECTED;
2001 sk->sk_state_change(sk);
2002 bh_unlock_sock(sk);
2003out:
2004 kfree_skb(skb);
2005 return NET_RX_SUCCESS;
2006}
2007
2008/**
2009 * afiucv_hs_callback_synfin() - react on received SYN_FIN
2010 **/
2011static int afiucv_hs_callback_synfin(struct sock *sk, struct sk_buff *skb)
2012{
2013 struct iucv_sock *iucv = iucv_sk(sk);
2014
2015 if (!iucv)
2016 goto out;
2017 if (sk->sk_state != IUCV_BOUND)
2018 goto out;
2019 bh_lock_sock(sk);
2020 sk->sk_state = IUCV_DISCONN;
2021 sk->sk_state_change(sk);
2022 bh_unlock_sock(sk);
2023out:
2024 kfree_skb(skb);
2025 return NET_RX_SUCCESS;
2026}
2027
2028/**
2029 * afiucv_hs_callback_fin() - react on received FIN
2030 **/
2031static int afiucv_hs_callback_fin(struct sock *sk, struct sk_buff *skb)
2032{
2033 struct iucv_sock *iucv = iucv_sk(sk);
2034
2035 /* other end of connection closed */
Ursula Braun800c5eb2012-02-08 00:19:47 +00002036 if (!iucv)
2037 goto out;
2038 bh_lock_sock(sk);
2039 if (sk->sk_state == IUCV_CONNECTED) {
Ursula Braunaac63992011-12-19 22:56:31 +00002040 sk->sk_state = IUCV_DISCONN;
Ursula Braun3881ac42011-08-08 01:33:54 +00002041 sk->sk_state_change(sk);
Ursula Braun3881ac42011-08-08 01:33:54 +00002042 }
Ursula Braun800c5eb2012-02-08 00:19:47 +00002043 bh_unlock_sock(sk);
2044out:
Ursula Braun3881ac42011-08-08 01:33:54 +00002045 kfree_skb(skb);
2046 return NET_RX_SUCCESS;
2047}
2048
2049/**
2050 * afiucv_hs_callback_win() - react on received WIN
2051 **/
2052static int afiucv_hs_callback_win(struct sock *sk, struct sk_buff *skb)
2053{
2054 struct iucv_sock *iucv = iucv_sk(sk);
2055 struct af_iucv_trans_hdr *trans_hdr =
2056 (struct af_iucv_trans_hdr *)skb->data;
2057
2058 if (!iucv)
2059 return NET_RX_SUCCESS;
2060
2061 if (sk->sk_state != IUCV_CONNECTED)
2062 return NET_RX_SUCCESS;
2063
2064 atomic_sub(trans_hdr->window, &iucv->msg_sent);
2065 iucv_sock_wake_msglim(sk);
2066 return NET_RX_SUCCESS;
2067}
2068
2069/**
2070 * afiucv_hs_callback_rx() - react on received data
2071 **/
2072static int afiucv_hs_callback_rx(struct sock *sk, struct sk_buff *skb)
2073{
2074 struct iucv_sock *iucv = iucv_sk(sk);
2075
2076 if (!iucv) {
2077 kfree_skb(skb);
2078 return NET_RX_SUCCESS;
2079 }
2080
2081 if (sk->sk_state != IUCV_CONNECTED) {
2082 kfree_skb(skb);
2083 return NET_RX_SUCCESS;
2084 }
2085
Ursula Braun82492a32012-03-07 02:06:24 +00002086 if (sk->sk_shutdown & RCV_SHUTDOWN) {
2087 kfree_skb(skb);
2088 return NET_RX_SUCCESS;
2089 }
2090
Ursula Braun3881ac42011-08-08 01:33:54 +00002091 /* write stuff from iucv_msg to skb cb */
Ursula Braun82492a32012-03-07 02:06:24 +00002092 if (skb->len < sizeof(struct af_iucv_trans_hdr)) {
Ursula Braun3881ac42011-08-08 01:33:54 +00002093 kfree_skb(skb);
2094 return NET_RX_SUCCESS;
2095 }
2096 skb_pull(skb, sizeof(struct af_iucv_trans_hdr));
2097 skb_reset_transport_header(skb);
2098 skb_reset_network_header(skb);
2099 spin_lock(&iucv->message_q.lock);
2100 if (skb_queue_empty(&iucv->backlog_skb_q)) {
2101 if (sock_queue_rcv_skb(sk, skb)) {
2102 /* handle rcv queue full */
2103 skb_queue_tail(&iucv->backlog_skb_q, skb);
2104 }
2105 } else
2106 skb_queue_tail(&iucv_sk(sk)->backlog_skb_q, skb);
2107 spin_unlock(&iucv->message_q.lock);
2108 return NET_RX_SUCCESS;
2109}
2110
2111/**
2112 * afiucv_hs_rcv() - base function for arriving data through HiperSockets
2113 * transport
2114 * called from netif RX softirq
2115 **/
2116static int afiucv_hs_rcv(struct sk_buff *skb, struct net_device *dev,
2117 struct packet_type *pt, struct net_device *orig_dev)
2118{
2119 struct hlist_node *node;
2120 struct sock *sk;
2121 struct iucv_sock *iucv;
2122 struct af_iucv_trans_hdr *trans_hdr;
2123 char nullstring[8];
2124 int err = 0;
2125
2126 skb_pull(skb, ETH_HLEN);
2127 trans_hdr = (struct af_iucv_trans_hdr *)skb->data;
2128 EBCASC(trans_hdr->destAppName, sizeof(trans_hdr->destAppName));
2129 EBCASC(trans_hdr->destUserID, sizeof(trans_hdr->destUserID));
2130 EBCASC(trans_hdr->srcAppName, sizeof(trans_hdr->srcAppName));
2131 EBCASC(trans_hdr->srcUserID, sizeof(trans_hdr->srcUserID));
2132 memset(nullstring, 0, sizeof(nullstring));
2133 iucv = NULL;
2134 sk = NULL;
2135 read_lock(&iucv_sk_list.lock);
2136 sk_for_each(sk, node, &iucv_sk_list.head) {
2137 if (trans_hdr->flags == AF_IUCV_FLAG_SYN) {
2138 if ((!memcmp(&iucv_sk(sk)->src_name,
2139 trans_hdr->destAppName, 8)) &&
2140 (!memcmp(&iucv_sk(sk)->src_user_id,
2141 trans_hdr->destUserID, 8)) &&
2142 (!memcmp(&iucv_sk(sk)->dst_name, nullstring, 8)) &&
2143 (!memcmp(&iucv_sk(sk)->dst_user_id,
2144 nullstring, 8))) {
2145 iucv = iucv_sk(sk);
2146 break;
2147 }
2148 } else {
2149 if ((!memcmp(&iucv_sk(sk)->src_name,
2150 trans_hdr->destAppName, 8)) &&
2151 (!memcmp(&iucv_sk(sk)->src_user_id,
2152 trans_hdr->destUserID, 8)) &&
2153 (!memcmp(&iucv_sk(sk)->dst_name,
2154 trans_hdr->srcAppName, 8)) &&
2155 (!memcmp(&iucv_sk(sk)->dst_user_id,
2156 trans_hdr->srcUserID, 8))) {
2157 iucv = iucv_sk(sk);
2158 break;
2159 }
2160 }
2161 }
2162 read_unlock(&iucv_sk_list.lock);
2163 if (!iucv)
2164 sk = NULL;
2165
2166 /* no sock
2167 how should we send with no sock
2168 1) send without sock no send rc checking?
2169 2) introduce default sock to handle this cases
2170
2171 SYN -> send SYN|ACK in good case, send SYN|FIN in bad case
2172 data -> send FIN
2173 SYN|ACK, SYN|FIN, FIN -> no action? */
2174
2175 switch (trans_hdr->flags) {
2176 case AF_IUCV_FLAG_SYN:
2177 /* connect request */
2178 err = afiucv_hs_callback_syn(sk, skb);
2179 break;
2180 case (AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_ACK):
2181 /* connect request confirmed */
2182 err = afiucv_hs_callback_synack(sk, skb);
2183 break;
2184 case (AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN):
2185 /* connect request refused */
2186 err = afiucv_hs_callback_synfin(sk, skb);
2187 break;
2188 case (AF_IUCV_FLAG_FIN):
2189 /* close request */
2190 err = afiucv_hs_callback_fin(sk, skb);
2191 break;
2192 case (AF_IUCV_FLAG_WIN):
2193 err = afiucv_hs_callback_win(sk, skb);
Ursula Braun800c5eb2012-02-08 00:19:47 +00002194 if (skb->len == sizeof(struct af_iucv_trans_hdr)) {
2195 kfree_skb(skb);
2196 break;
2197 }
Ursula Braun82492a32012-03-07 02:06:24 +00002198 /* fall through and receive non-zero length data */
2199 case (AF_IUCV_FLAG_SHT):
2200 /* shutdown request */
2201 /* fall through and receive zero length data */
Ursula Braun3881ac42011-08-08 01:33:54 +00002202 case 0:
2203 /* plain data frame */
Ursula Braunc64d3f82011-12-19 22:56:27 +00002204 memcpy(CB_TRGCLS(skb), &trans_hdr->iucv_hdr.class,
2205 CB_TRGCLS_LEN);
Ursula Braun3881ac42011-08-08 01:33:54 +00002206 err = afiucv_hs_callback_rx(sk, skb);
2207 break;
2208 default:
2209 ;
2210 }
2211
2212 return err;
2213}
2214
2215/**
2216 * afiucv_hs_callback_txnotify() - handle send notifcations from HiperSockets
2217 * transport
2218 **/
2219static void afiucv_hs_callback_txnotify(struct sk_buff *skb,
2220 enum iucv_tx_notify n)
2221{
2222 struct sock *isk = skb->sk;
2223 struct sock *sk = NULL;
2224 struct iucv_sock *iucv = NULL;
2225 struct sk_buff_head *list;
2226 struct sk_buff *list_skb;
Ursula Braun800c5eb2012-02-08 00:19:47 +00002227 struct sk_buff *nskb;
Ursula Braun3881ac42011-08-08 01:33:54 +00002228 unsigned long flags;
2229 struct hlist_node *node;
2230
Ursula Braun800c5eb2012-02-08 00:19:47 +00002231 read_lock_irqsave(&iucv_sk_list.lock, flags);
Ursula Braun3881ac42011-08-08 01:33:54 +00002232 sk_for_each(sk, node, &iucv_sk_list.head)
2233 if (sk == isk) {
2234 iucv = iucv_sk(sk);
2235 break;
2236 }
Ursula Braun800c5eb2012-02-08 00:19:47 +00002237 read_unlock_irqrestore(&iucv_sk_list.lock, flags);
Ursula Braun3881ac42011-08-08 01:33:54 +00002238
Ursula Braun800c5eb2012-02-08 00:19:47 +00002239 if (!iucv || sock_flag(sk, SOCK_ZAPPED))
Ursula Braun3881ac42011-08-08 01:33:54 +00002240 return;
2241
Ursula Braun3881ac42011-08-08 01:33:54 +00002242 list = &iucv->send_skb_q;
Ursula Braun800c5eb2012-02-08 00:19:47 +00002243 spin_lock_irqsave(&list->lock, flags);
Ursula Braun3881ac42011-08-08 01:33:54 +00002244 if (skb_queue_empty(list))
2245 goto out_unlock;
Ursula Braun800c5eb2012-02-08 00:19:47 +00002246 list_skb = list->next;
2247 nskb = list_skb->next;
Ursula Braun3881ac42011-08-08 01:33:54 +00002248 while (list_skb != (struct sk_buff *)list) {
2249 if (skb_shinfo(list_skb) == skb_shinfo(skb)) {
Ursula Braun3881ac42011-08-08 01:33:54 +00002250 switch (n) {
2251 case TX_NOTIFY_OK:
Ursula Braun800c5eb2012-02-08 00:19:47 +00002252 __skb_unlink(list_skb, list);
2253 kfree_skb(list_skb);
Ursula Braun3881ac42011-08-08 01:33:54 +00002254 iucv_sock_wake_msglim(sk);
Ursula Braun3881ac42011-08-08 01:33:54 +00002255 break;
2256 case TX_NOTIFY_PENDING:
2257 atomic_inc(&iucv->pendings);
2258 break;
2259 case TX_NOTIFY_DELAYED_OK:
Ursula Braun800c5eb2012-02-08 00:19:47 +00002260 __skb_unlink(list_skb, list);
Ursula Braun3881ac42011-08-08 01:33:54 +00002261 atomic_dec(&iucv->pendings);
2262 if (atomic_read(&iucv->pendings) <= 0)
2263 iucv_sock_wake_msglim(sk);
Ursula Braun800c5eb2012-02-08 00:19:47 +00002264 kfree_skb(list_skb);
Ursula Braun3881ac42011-08-08 01:33:54 +00002265 break;
2266 case TX_NOTIFY_UNREACHABLE:
2267 case TX_NOTIFY_DELAYED_UNREACHABLE:
2268 case TX_NOTIFY_TPQFULL: /* not yet used */
2269 case TX_NOTIFY_GENERALERROR:
2270 case TX_NOTIFY_DELAYED_GENERALERROR:
Ursula Braun800c5eb2012-02-08 00:19:47 +00002271 __skb_unlink(list_skb, list);
2272 kfree_skb(list_skb);
2273 if (sk->sk_state == IUCV_CONNECTED) {
2274 sk->sk_state = IUCV_DISCONN;
2275 sk->sk_state_change(sk);
2276 }
Ursula Braun3881ac42011-08-08 01:33:54 +00002277 break;
2278 }
2279 break;
2280 }
Ursula Braun800c5eb2012-02-08 00:19:47 +00002281 list_skb = nskb;
2282 nskb = nskb->next;
Ursula Braun3881ac42011-08-08 01:33:54 +00002283 }
Ursula Braun800c5eb2012-02-08 00:19:47 +00002284out_unlock:
Ursula Braun3881ac42011-08-08 01:33:54 +00002285 spin_unlock_irqrestore(&list->lock, flags);
2286
Ursula Braun42bd48e2011-12-19 22:56:28 +00002287 if (sk->sk_state == IUCV_CLOSING) {
2288 if (skb_queue_empty(&iucv_sk(sk)->send_skb_q)) {
2289 sk->sk_state = IUCV_CLOSED;
2290 sk->sk_state_change(sk);
2291 }
2292 }
2293
Ursula Braun3881ac42011-08-08 01:33:54 +00002294}
Ursula Braun9fbd87d2012-03-07 02:06:23 +00002295
2296/*
2297 * afiucv_netdev_event: handle netdev notifier chain events
2298 */
2299static int afiucv_netdev_event(struct notifier_block *this,
2300 unsigned long event, void *ptr)
2301{
2302 struct net_device *event_dev = (struct net_device *)ptr;
2303 struct hlist_node *node;
2304 struct sock *sk;
2305 struct iucv_sock *iucv;
2306
2307 switch (event) {
2308 case NETDEV_REBOOT:
2309 case NETDEV_GOING_DOWN:
2310 sk_for_each(sk, node, &iucv_sk_list.head) {
2311 iucv = iucv_sk(sk);
2312 if ((iucv->hs_dev == event_dev) &&
2313 (sk->sk_state == IUCV_CONNECTED)) {
2314 if (event == NETDEV_GOING_DOWN)
2315 iucv_send_ctrl(sk, AF_IUCV_FLAG_FIN);
2316 sk->sk_state = IUCV_DISCONN;
2317 sk->sk_state_change(sk);
2318 }
2319 }
2320 break;
2321 case NETDEV_DOWN:
2322 case NETDEV_UNREGISTER:
2323 default:
2324 break;
2325 }
2326 return NOTIFY_DONE;
2327}
2328
2329static struct notifier_block afiucv_netdev_notifier = {
2330 .notifier_call = afiucv_netdev_event,
2331};
2332
Alexey Dobriyan5708e862009-09-14 12:23:23 +00002333static const struct proto_ops iucv_sock_ops = {
Jennifer Hunteac37312007-02-08 13:51:54 -08002334 .family = PF_IUCV,
2335 .owner = THIS_MODULE,
2336 .release = iucv_sock_release,
2337 .bind = iucv_sock_bind,
2338 .connect = iucv_sock_connect,
2339 .listen = iucv_sock_listen,
2340 .accept = iucv_sock_accept,
2341 .getname = iucv_sock_getname,
2342 .sendmsg = iucv_sock_sendmsg,
2343 .recvmsg = iucv_sock_recvmsg,
2344 .poll = iucv_sock_poll,
2345 .ioctl = sock_no_ioctl,
2346 .mmap = sock_no_mmap,
2347 .socketpair = sock_no_socketpair,
2348 .shutdown = iucv_sock_shutdown,
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00002349 .setsockopt = iucv_sock_setsockopt,
2350 .getsockopt = iucv_sock_getsockopt,
Jennifer Hunteac37312007-02-08 13:51:54 -08002351};
2352
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00002353static const struct net_proto_family iucv_sock_family_ops = {
Jennifer Hunteac37312007-02-08 13:51:54 -08002354 .family = AF_IUCV,
2355 .owner = THIS_MODULE,
2356 .create = iucv_sock_create,
2357};
2358
Ursula Braun3881ac42011-08-08 01:33:54 +00002359static struct packet_type iucv_packet_type = {
2360 .type = cpu_to_be16(ETH_P_AF_IUCV),
2361 .func = afiucv_hs_rcv,
2362};
2363
2364static int afiucv_iucv_init(void)
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00002365{
2366 int err;
2367
2368 err = pr_iucv->iucv_register(&af_iucv_handler, 0);
2369 if (err)
2370 goto out;
2371 /* establish dummy device */
2372 af_iucv_driver.bus = pr_iucv->bus;
2373 err = driver_register(&af_iucv_driver);
2374 if (err)
2375 goto out_iucv;
2376 af_iucv_dev = kzalloc(sizeof(struct device), GFP_KERNEL);
2377 if (!af_iucv_dev) {
2378 err = -ENOMEM;
2379 goto out_driver;
2380 }
2381 dev_set_name(af_iucv_dev, "af_iucv");
2382 af_iucv_dev->bus = pr_iucv->bus;
2383 af_iucv_dev->parent = pr_iucv->root;
2384 af_iucv_dev->release = (void (*)(struct device *))kfree;
2385 af_iucv_dev->driver = &af_iucv_driver;
2386 err = device_register(af_iucv_dev);
2387 if (err)
2388 goto out_driver;
2389 return 0;
2390
2391out_driver:
2392 driver_unregister(&af_iucv_driver);
2393out_iucv:
2394 pr_iucv->iucv_unregister(&af_iucv_handler, 0);
2395out:
2396 return err;
2397}
2398
Heiko Carstensda99f052007-05-04 12:23:27 -07002399static int __init afiucv_init(void)
Jennifer Hunteac37312007-02-08 13:51:54 -08002400{
2401 int err;
2402
Ursula Braun3881ac42011-08-08 01:33:54 +00002403 if (MACHINE_IS_VM) {
2404 cpcmd("QUERY USERID", iucv_userid, sizeof(iucv_userid), &err);
2405 if (unlikely(err)) {
2406 WARN_ON(err);
2407 err = -EPROTONOSUPPORT;
2408 goto out;
2409 }
Jennifer Hunteac37312007-02-08 13:51:54 -08002410
Ursula Braun3881ac42011-08-08 01:33:54 +00002411 pr_iucv = try_then_request_module(symbol_get(iucv_if), "iucv");
2412 if (!pr_iucv) {
2413 printk(KERN_WARNING "iucv_if lookup failed\n");
2414 memset(&iucv_userid, 0, sizeof(iucv_userid));
2415 }
2416 } else {
2417 memset(&iucv_userid, 0, sizeof(iucv_userid));
2418 pr_iucv = NULL;
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00002419 }
2420
Jennifer Hunteac37312007-02-08 13:51:54 -08002421 err = proto_register(&iucv_proto, 0);
2422 if (err)
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00002423 goto out;
Jennifer Hunteac37312007-02-08 13:51:54 -08002424 err = sock_register(&iucv_sock_family_ops);
2425 if (err)
2426 goto out_proto;
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00002427
Ursula Braun3881ac42011-08-08 01:33:54 +00002428 if (pr_iucv) {
2429 err = afiucv_iucv_init();
2430 if (err)
2431 goto out_sock;
Ursula Braun9fbd87d2012-03-07 02:06:23 +00002432 } else
2433 register_netdevice_notifier(&afiucv_netdev_notifier);
Ursula Braun3881ac42011-08-08 01:33:54 +00002434 dev_add_pack(&iucv_packet_type);
Jennifer Hunteac37312007-02-08 13:51:54 -08002435 return 0;
2436
Ursula Braunc23cad92009-06-16 10:30:44 +02002437out_sock:
2438 sock_unregister(PF_IUCV);
Jennifer Hunteac37312007-02-08 13:51:54 -08002439out_proto:
2440 proto_unregister(&iucv_proto);
Jennifer Hunteac37312007-02-08 13:51:54 -08002441out:
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00002442 if (pr_iucv)
2443 symbol_put(iucv_if);
Jennifer Hunteac37312007-02-08 13:51:54 -08002444 return err;
2445}
2446
2447static void __exit afiucv_exit(void)
2448{
Ursula Braun3881ac42011-08-08 01:33:54 +00002449 if (pr_iucv) {
2450 device_unregister(af_iucv_dev);
2451 driver_unregister(&af_iucv_driver);
2452 pr_iucv->iucv_unregister(&af_iucv_handler, 0);
2453 symbol_put(iucv_if);
Ursula Braun9fbd87d2012-03-07 02:06:23 +00002454 } else
2455 unregister_netdevice_notifier(&afiucv_netdev_notifier);
Ursula Braun3881ac42011-08-08 01:33:54 +00002456 dev_remove_pack(&iucv_packet_type);
Jennifer Hunteac37312007-02-08 13:51:54 -08002457 sock_unregister(PF_IUCV);
2458 proto_unregister(&iucv_proto);
Jennifer Hunteac37312007-02-08 13:51:54 -08002459}
2460
2461module_init(afiucv_init);
2462module_exit(afiucv_exit);
2463
2464MODULE_AUTHOR("Jennifer Hunt <jenhunt@us.ibm.com>");
2465MODULE_DESCRIPTION("IUCV Sockets ver " VERSION);
2466MODULE_VERSION(VERSION);
2467MODULE_LICENSE("GPL");
2468MODULE_ALIAS_NETPROTO(PF_IUCV);
Ursula Braun3881ac42011-08-08 01:33:54 +00002469