blob: cd6f7a991d8035bdfc6d883a8bcf21a433405030 [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;
Ursula Braun3881ac42011-08-08 01:33:54 +0000375 nskb = skb_clone(skb, GFP_ATOMIC);
376 if (!nskb)
377 return -ENOMEM;
378 skb_queue_tail(&iucv->send_skb_q, nskb);
379 err = dev_queue_xmit(skb);
Ursula Braun800c5eb2012-02-08 00:19:47 +0000380 if (net_xmit_eval(err)) {
Ursula Braun3881ac42011-08-08 01:33:54 +0000381 skb_unlink(nskb, &iucv->send_skb_q);
382 kfree_skb(nskb);
383 } else {
384 atomic_sub(confirm_recv, &iucv->msg_recv);
385 WARN_ON(atomic_read(&iucv->msg_recv) < 0);
386 }
Ursula Braun800c5eb2012-02-08 00:19:47 +0000387 return net_xmit_eval(err);
Ursula Braun3881ac42011-08-08 01:33:54 +0000388}
389
Jennifer Hunteac37312007-02-08 13:51:54 -0800390static struct sock *__iucv_get_sock_by_name(char *nm)
391{
392 struct sock *sk;
393 struct hlist_node *node;
394
395 sk_for_each(sk, node, &iucv_sk_list.head)
396 if (!memcmp(&iucv_sk(sk)->src_name, nm, 8))
397 return sk;
398
399 return NULL;
400}
401
402static void iucv_sock_destruct(struct sock *sk)
403{
404 skb_queue_purge(&sk->sk_receive_queue);
Ursula Braun82492a32012-03-07 02:06:24 +0000405 skb_queue_purge(&sk->sk_error_queue);
406
407 sk_mem_reclaim(sk);
408
409 if (!sock_flag(sk, SOCK_DEAD)) {
410 pr_err("Attempt to release alive iucv socket %p\n", sk);
411 return;
412 }
413
414 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
415 WARN_ON(atomic_read(&sk->sk_wmem_alloc));
416 WARN_ON(sk->sk_wmem_queued);
417 WARN_ON(sk->sk_forward_alloc);
Jennifer Hunteac37312007-02-08 13:51:54 -0800418}
419
420/* Cleanup Listen */
421static void iucv_sock_cleanup_listen(struct sock *parent)
422{
423 struct sock *sk;
424
425 /* Close non-accepted connections */
426 while ((sk = iucv_accept_dequeue(parent, NULL))) {
427 iucv_sock_close(sk);
428 iucv_sock_kill(sk);
429 }
430
431 parent->sk_state = IUCV_CLOSED;
Jennifer Hunteac37312007-02-08 13:51:54 -0800432}
433
Hendrik Brueckner7514bab2009-09-16 04:37:27 +0000434/* Kill socket (only if zapped and orphaned) */
Jennifer Hunteac37312007-02-08 13:51:54 -0800435static void iucv_sock_kill(struct sock *sk)
436{
437 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
438 return;
439
440 iucv_sock_unlink(&iucv_sk_list, sk);
441 sock_set_flag(sk, SOCK_DEAD);
442 sock_put(sk);
443}
444
Ursula Braun7d316b92012-02-08 00:19:45 +0000445/* Terminate an IUCV path */
446static void iucv_sever_path(struct sock *sk, int with_user_data)
447{
448 unsigned char user_data[16];
449 struct iucv_sock *iucv = iucv_sk(sk);
450 struct iucv_path *path = iucv->path;
451
452 if (iucv->path) {
453 iucv->path = NULL;
454 if (with_user_data) {
455 low_nmcpy(user_data, iucv->src_name);
456 high_nmcpy(user_data, iucv->dst_name);
457 ASCEBC(user_data, sizeof(user_data));
458 pr_iucv->path_sever(path, user_data);
459 } else
460 pr_iucv->path_sever(path, NULL);
461 iucv_path_free(path);
462 }
463}
464
Ursula Braun9fbd87d2012-03-07 02:06:23 +0000465/* Send FIN through an IUCV socket for HIPER transport */
466static int iucv_send_ctrl(struct sock *sk, u8 flags)
467{
468 int err = 0;
469 int blen;
470 struct sk_buff *skb;
471
472 blen = sizeof(struct af_iucv_trans_hdr) + ETH_HLEN;
473 skb = sock_alloc_send_skb(sk, blen, 1, &err);
474 if (skb) {
475 skb_reserve(skb, blen);
476 err = afiucv_hs_send(NULL, sk, skb, flags);
477 }
478 return err;
479}
480
Jennifer Hunteac37312007-02-08 13:51:54 -0800481/* Close an IUCV socket */
482static void iucv_sock_close(struct sock *sk)
483{
Jennifer Hunteac37312007-02-08 13:51:54 -0800484 struct iucv_sock *iucv = iucv_sk(sk);
Jennifer Hunt561e0362007-05-04 12:22:07 -0700485 unsigned long timeo;
Ursula Braun800c5eb2012-02-08 00:19:47 +0000486 int err = 0;
Jennifer Hunteac37312007-02-08 13:51:54 -0800487
Jennifer Hunteac37312007-02-08 13:51:54 -0800488 lock_sock(sk);
489
Heiko Carstensda99f052007-05-04 12:23:27 -0700490 switch (sk->sk_state) {
Jennifer Hunteac37312007-02-08 13:51:54 -0800491 case IUCV_LISTEN:
492 iucv_sock_cleanup_listen(sk);
493 break;
494
495 case IUCV_CONNECTED:
Ursula Braun3881ac42011-08-08 01:33:54 +0000496 if (iucv->transport == AF_IUCV_TRANS_HIPER) {
Ursula Braun9fbd87d2012-03-07 02:06:23 +0000497 err = iucv_send_ctrl(sk, AF_IUCV_FLAG_FIN);
Ursula Braun3881ac42011-08-08 01:33:54 +0000498 sk->sk_state = IUCV_DISCONN;
499 sk->sk_state_change(sk);
500 }
Ursula Braun816abba2011-12-19 22:56:29 +0000501 case IUCV_DISCONN: /* fall through */
Jennifer Hunt561e0362007-05-04 12:22:07 -0700502 sk->sk_state = IUCV_CLOSING;
503 sk->sk_state_change(sk);
504
Ursula Braun800c5eb2012-02-08 00:19:47 +0000505 if (!err && !skb_queue_empty(&iucv->send_skb_q)) {
Jennifer Hunt561e0362007-05-04 12:22:07 -0700506 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
507 timeo = sk->sk_lingertime;
508 else
509 timeo = IUCV_DISCONN_TIMEOUT;
Ursula Braun9f6298a2011-05-12 18:45:08 +0000510 iucv_sock_wait(sk,
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +0000511 iucv_sock_in_state(sk, IUCV_CLOSED, 0),
512 timeo);
Jennifer Hunt561e0362007-05-04 12:22:07 -0700513 }
514
Ursula Braunbbe188c2009-04-21 06:04:20 +0000515 case IUCV_CLOSING: /* fall through */
Jennifer Hunt561e0362007-05-04 12:22:07 -0700516 sk->sk_state = IUCV_CLOSED;
517 sk->sk_state_change(sk);
518
Jennifer Hunteac37312007-02-08 13:51:54 -0800519 sk->sk_err = ECONNRESET;
520 sk->sk_state_change(sk);
521
Ursula Braun800c5eb2012-02-08 00:19:47 +0000522 skb_queue_purge(&iucv->send_skb_q);
Jennifer Hunt561e0362007-05-04 12:22:07 -0700523 skb_queue_purge(&iucv->backlog_skb_q);
Jennifer Hunteac37312007-02-08 13:51:54 -0800524
Ursula Braun7d316b92012-02-08 00:19:45 +0000525 default: /* fall through */
526 iucv_sever_path(sk, 1);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700527 }
Jennifer Hunteac37312007-02-08 13:51:54 -0800528
Ursula Braun800c5eb2012-02-08 00:19:47 +0000529 if (iucv->hs_dev) {
530 dev_put(iucv->hs_dev);
531 iucv->hs_dev = NULL;
532 sk->sk_bound_dev_if = 0;
533 }
534
Hendrik Brueckner7514bab2009-09-16 04:37:27 +0000535 /* mark socket for deletion by iucv_sock_kill() */
536 sock_set_flag(sk, SOCK_ZAPPED);
537
Jennifer Hunteac37312007-02-08 13:51:54 -0800538 release_sock(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800539}
540
541static void iucv_sock_init(struct sock *sk, struct sock *parent)
542{
543 if (parent)
544 sk->sk_type = parent->sk_type;
545}
546
547static struct sock *iucv_sock_alloc(struct socket *sock, int proto, gfp_t prio)
548{
549 struct sock *sk;
Ursula Braun493d3972011-08-08 01:33:52 +0000550 struct iucv_sock *iucv;
Jennifer Hunteac37312007-02-08 13:51:54 -0800551
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700552 sk = sk_alloc(&init_net, PF_IUCV, prio, &iucv_proto);
Jennifer Hunteac37312007-02-08 13:51:54 -0800553 if (!sk)
554 return NULL;
Ursula Braun493d3972011-08-08 01:33:52 +0000555 iucv = iucv_sk(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800556
557 sock_init_data(sock, sk);
Ursula Braun493d3972011-08-08 01:33:52 +0000558 INIT_LIST_HEAD(&iucv->accept_q);
559 spin_lock_init(&iucv->accept_q_lock);
560 skb_queue_head_init(&iucv->send_skb_q);
561 INIT_LIST_HEAD(&iucv->message_q.list);
562 spin_lock_init(&iucv->message_q.lock);
563 skb_queue_head_init(&iucv->backlog_skb_q);
564 iucv->send_tag = 0;
Ursula Braun3881ac42011-08-08 01:33:54 +0000565 atomic_set(&iucv->pendings, 0);
Ursula Braun493d3972011-08-08 01:33:52 +0000566 iucv->flags = 0;
Ursula Braun3881ac42011-08-08 01:33:54 +0000567 iucv->msglimit = 0;
568 atomic_set(&iucv->msg_sent, 0);
569 atomic_set(&iucv->msg_recv, 0);
Ursula Braun493d3972011-08-08 01:33:52 +0000570 iucv->path = NULL;
Ursula Braun3881ac42011-08-08 01:33:54 +0000571 iucv->sk_txnotify = afiucv_hs_callback_txnotify;
Ursula Braun493d3972011-08-08 01:33:52 +0000572 memset(&iucv->src_user_id , 0, 32);
Ursula Braun3881ac42011-08-08 01:33:54 +0000573 if (pr_iucv)
574 iucv->transport = AF_IUCV_TRANS_IUCV;
575 else
576 iucv->transport = AF_IUCV_TRANS_HIPER;
Jennifer Hunteac37312007-02-08 13:51:54 -0800577
578 sk->sk_destruct = iucv_sock_destruct;
579 sk->sk_sndtimeo = IUCV_CONN_TIMEOUT;
580 sk->sk_allocation = GFP_DMA;
581
582 sock_reset_flag(sk, SOCK_ZAPPED);
583
584 sk->sk_protocol = proto;
585 sk->sk_state = IUCV_OPEN;
586
Jennifer Hunteac37312007-02-08 13:51:54 -0800587 iucv_sock_link(&iucv_sk_list, sk);
588 return sk;
589}
590
591/* Create an IUCV socket */
Eric Paris3f378b62009-11-05 22:18:14 -0800592static int iucv_sock_create(struct net *net, struct socket *sock, int protocol,
593 int kern)
Jennifer Hunteac37312007-02-08 13:51:54 -0800594{
595 struct sock *sk;
596
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +0000597 if (protocol && protocol != PF_IUCV)
598 return -EPROTONOSUPPORT;
Jennifer Hunteac37312007-02-08 13:51:54 -0800599
600 sock->state = SS_UNCONNECTED;
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +0000601
602 switch (sock->type) {
603 case SOCK_STREAM:
604 sock->ops = &iucv_sock_ops;
605 break;
606 case SOCK_SEQPACKET:
607 /* currently, proto ops can handle both sk types */
608 sock->ops = &iucv_sock_ops;
609 break;
610 default:
611 return -ESOCKTNOSUPPORT;
612 }
Jennifer Hunteac37312007-02-08 13:51:54 -0800613
614 sk = iucv_sock_alloc(sock, protocol, GFP_KERNEL);
615 if (!sk)
616 return -ENOMEM;
617
618 iucv_sock_init(sk, NULL);
619
620 return 0;
621}
622
623void iucv_sock_link(struct iucv_sock_list *l, struct sock *sk)
624{
625 write_lock_bh(&l->lock);
626 sk_add_node(sk, &l->head);
627 write_unlock_bh(&l->lock);
628}
629
630void iucv_sock_unlink(struct iucv_sock_list *l, struct sock *sk)
631{
632 write_lock_bh(&l->lock);
633 sk_del_node_init(sk);
634 write_unlock_bh(&l->lock);
635}
636
637void iucv_accept_enqueue(struct sock *parent, struct sock *sk)
638{
Ursula Braunfebca282007-07-14 19:04:25 -0700639 unsigned long flags;
640 struct iucv_sock *par = iucv_sk(parent);
641
Jennifer Hunteac37312007-02-08 13:51:54 -0800642 sock_hold(sk);
Ursula Braunfebca282007-07-14 19:04:25 -0700643 spin_lock_irqsave(&par->accept_q_lock, flags);
644 list_add_tail(&iucv_sk(sk)->accept_q, &par->accept_q);
645 spin_unlock_irqrestore(&par->accept_q_lock, flags);
Jennifer Hunteac37312007-02-08 13:51:54 -0800646 iucv_sk(sk)->parent = parent;
Hendrik Brueckner49f5eba2009-10-14 22:54:55 +0000647 sk_acceptq_added(parent);
Jennifer Hunteac37312007-02-08 13:51:54 -0800648}
649
650void iucv_accept_unlink(struct sock *sk)
651{
Ursula Braunfebca282007-07-14 19:04:25 -0700652 unsigned long flags;
653 struct iucv_sock *par = iucv_sk(iucv_sk(sk)->parent);
654
655 spin_lock_irqsave(&par->accept_q_lock, flags);
Jennifer Hunteac37312007-02-08 13:51:54 -0800656 list_del_init(&iucv_sk(sk)->accept_q);
Ursula Braunfebca282007-07-14 19:04:25 -0700657 spin_unlock_irqrestore(&par->accept_q_lock, flags);
Hendrik Brueckner49f5eba2009-10-14 22:54:55 +0000658 sk_acceptq_removed(iucv_sk(sk)->parent);
Jennifer Hunteac37312007-02-08 13:51:54 -0800659 iucv_sk(sk)->parent = NULL;
660 sock_put(sk);
661}
662
663struct sock *iucv_accept_dequeue(struct sock *parent, struct socket *newsock)
664{
665 struct iucv_sock *isk, *n;
666 struct sock *sk;
667
Heiko Carstensda99f052007-05-04 12:23:27 -0700668 list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) {
Jennifer Hunteac37312007-02-08 13:51:54 -0800669 sk = (struct sock *) isk;
670 lock_sock(sk);
671
672 if (sk->sk_state == IUCV_CLOSED) {
Jennifer Hunteac37312007-02-08 13:51:54 -0800673 iucv_accept_unlink(sk);
Ursula Braunfebca282007-07-14 19:04:25 -0700674 release_sock(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800675 continue;
676 }
677
678 if (sk->sk_state == IUCV_CONNECTED ||
Ursula Braunaac63992011-12-19 22:56:31 +0000679 sk->sk_state == IUCV_DISCONN ||
Jennifer Hunteac37312007-02-08 13:51:54 -0800680 !newsock) {
681 iucv_accept_unlink(sk);
682 if (newsock)
683 sock_graft(sk, newsock);
684
Jennifer Hunteac37312007-02-08 13:51:54 -0800685 release_sock(sk);
686 return sk;
687 }
688
689 release_sock(sk);
690 }
691 return NULL;
692}
693
Jennifer Hunteac37312007-02-08 13:51:54 -0800694/* Bind an unbound socket */
695static int iucv_sock_bind(struct socket *sock, struct sockaddr *addr,
696 int addr_len)
697{
698 struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
699 struct sock *sk = sock->sk;
700 struct iucv_sock *iucv;
Ursula Braun3881ac42011-08-08 01:33:54 +0000701 int err = 0;
702 struct net_device *dev;
703 char uid[9];
Jennifer Hunteac37312007-02-08 13:51:54 -0800704
705 /* Verify the input sockaddr */
706 if (!addr || addr->sa_family != AF_IUCV)
707 return -EINVAL;
708
709 lock_sock(sk);
710 if (sk->sk_state != IUCV_OPEN) {
711 err = -EBADFD;
712 goto done;
713 }
714
715 write_lock_bh(&iucv_sk_list.lock);
716
717 iucv = iucv_sk(sk);
718 if (__iucv_get_sock_by_name(sa->siucv_name)) {
719 err = -EADDRINUSE;
720 goto done_unlock;
721 }
Ursula Braun3881ac42011-08-08 01:33:54 +0000722 if (iucv->path)
Jennifer Hunteac37312007-02-08 13:51:54 -0800723 goto done_unlock;
Jennifer Hunteac37312007-02-08 13:51:54 -0800724
725 /* Bind the socket */
Ursula Braun3881ac42011-08-08 01:33:54 +0000726 if (pr_iucv)
727 if (!memcmp(sa->siucv_user_id, iucv_userid, 8))
728 goto vm_bind; /* VM IUCV transport */
Jennifer Hunteac37312007-02-08 13:51:54 -0800729
Ursula Braun3881ac42011-08-08 01:33:54 +0000730 /* try hiper transport */
731 memcpy(uid, sa->siucv_user_id, sizeof(uid));
732 ASCEBC(uid, 8);
733 rcu_read_lock();
734 for_each_netdev_rcu(&init_net, dev) {
735 if (!memcmp(dev->perm_addr, uid, 8)) {
736 memcpy(iucv->src_name, sa->siucv_name, 8);
737 memcpy(iucv->src_user_id, sa->siucv_user_id, 8);
Ursula Braun816abba2011-12-19 22:56:29 +0000738 sk->sk_bound_dev_if = dev->ifindex;
Ursula Braun800c5eb2012-02-08 00:19:47 +0000739 iucv->hs_dev = dev;
740 dev_hold(dev);
Ursula Braun3881ac42011-08-08 01:33:54 +0000741 sk->sk_state = IUCV_BOUND;
742 iucv->transport = AF_IUCV_TRANS_HIPER;
743 if (!iucv->msglimit)
744 iucv->msglimit = IUCV_HIPER_MSGLIM_DEFAULT;
745 rcu_read_unlock();
746 goto done_unlock;
747 }
748 }
749 rcu_read_unlock();
750vm_bind:
751 if (pr_iucv) {
752 /* use local userid for backward compat */
753 memcpy(iucv->src_name, sa->siucv_name, 8);
754 memcpy(iucv->src_user_id, iucv_userid, 8);
755 sk->sk_state = IUCV_BOUND;
756 iucv->transport = AF_IUCV_TRANS_IUCV;
757 if (!iucv->msglimit)
758 iucv->msglimit = IUCV_QUEUELEN_DEFAULT;
759 goto done_unlock;
760 }
761 /* found no dev to bind */
762 err = -ENODEV;
Jennifer Hunteac37312007-02-08 13:51:54 -0800763done_unlock:
764 /* Release the socket list lock */
765 write_unlock_bh(&iucv_sk_list.lock);
766done:
767 release_sock(sk);
768 return err;
769}
770
771/* Automatically bind an unbound socket */
772static int iucv_sock_autobind(struct sock *sk)
773{
774 struct iucv_sock *iucv = iucv_sk(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800775 char name[12];
776 int err = 0;
777
Ursula Braunaac63992011-12-19 22:56:31 +0000778 if (unlikely(!pr_iucv))
Jennifer Hunteac37312007-02-08 13:51:54 -0800779 return -EPROTO;
780
Ursula Braunaac63992011-12-19 22:56:31 +0000781 memcpy(iucv->src_user_id, iucv_userid, 8);
Jennifer Hunteac37312007-02-08 13:51:54 -0800782
783 write_lock_bh(&iucv_sk_list.lock);
784
785 sprintf(name, "%08x", atomic_inc_return(&iucv_sk_list.autobind_name));
786 while (__iucv_get_sock_by_name(name)) {
787 sprintf(name, "%08x",
788 atomic_inc_return(&iucv_sk_list.autobind_name));
789 }
790
791 write_unlock_bh(&iucv_sk_list.lock);
792
793 memcpy(&iucv->src_name, name, 8);
794
Ursula Braun3881ac42011-08-08 01:33:54 +0000795 if (!iucv->msglimit)
796 iucv->msglimit = IUCV_QUEUELEN_DEFAULT;
797
Jennifer Hunteac37312007-02-08 13:51:54 -0800798 return err;
799}
800
Ursula Braun3881ac42011-08-08 01:33:54 +0000801static int afiucv_path_connect(struct socket *sock, struct sockaddr *addr)
Jennifer Hunteac37312007-02-08 13:51:54 -0800802{
803 struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
804 struct sock *sk = sock->sk;
Ursula Braun493d3972011-08-08 01:33:52 +0000805 struct iucv_sock *iucv = iucv_sk(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800806 unsigned char user_data[16];
807 int err;
808
Jennifer Hunteac37312007-02-08 13:51:54 -0800809 high_nmcpy(user_data, sa->siucv_name);
Ursula Braun493d3972011-08-08 01:33:52 +0000810 low_nmcpy(user_data, iucv->src_name);
Jennifer Hunteac37312007-02-08 13:51:54 -0800811 ASCEBC(user_data, sizeof(user_data));
812
Jennifer Hunteac37312007-02-08 13:51:54 -0800813 /* Create path. */
Hendrik Brueckner09488e22009-04-21 23:26:27 +0000814 iucv->path = iucv_path_alloc(iucv->msglimit,
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000815 IUCV_IPRMDATA, GFP_KERNEL);
Ursula Braund4444722008-02-07 18:07:19 -0800816 if (!iucv->path) {
817 err = -ENOMEM;
818 goto done;
819 }
Frank Blaschka6fcd61f2011-08-08 01:33:51 +0000820 err = pr_iucv->path_connect(iucv->path, &af_iucv_handler,
821 sa->siucv_user_id, NULL, user_data,
822 sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800823 if (err) {
824 iucv_path_free(iucv->path);
825 iucv->path = NULL;
Hendrik Brueckner55cdea92009-01-05 18:07:07 -0800826 switch (err) {
827 case 0x0b: /* Target communicator is not logged on */
828 err = -ENETUNREACH;
829 break;
830 case 0x0d: /* Max connections for this guest exceeded */
831 case 0x0e: /* Max connections for target guest exceeded */
832 err = -EAGAIN;
833 break;
834 case 0x0f: /* Missing IUCV authorization */
835 err = -EACCES;
836 break;
837 default:
838 err = -ECONNREFUSED;
839 break;
840 }
Ursula Braun3881ac42011-08-08 01:33:54 +0000841 }
842done:
843 return err;
844}
845
846/* Connect an unconnected socket */
847static int iucv_sock_connect(struct socket *sock, struct sockaddr *addr,
848 int alen, int flags)
849{
850 struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr;
851 struct sock *sk = sock->sk;
852 struct iucv_sock *iucv = iucv_sk(sk);
853 int err;
854
855 if (addr->sa_family != AF_IUCV || alen < sizeof(struct sockaddr_iucv))
856 return -EINVAL;
857
858 if (sk->sk_state != IUCV_OPEN && sk->sk_state != IUCV_BOUND)
859 return -EBADFD;
860
861 if (sk->sk_state == IUCV_OPEN &&
862 iucv->transport == AF_IUCV_TRANS_HIPER)
863 return -EBADFD; /* explicit bind required */
864
865 if (sk->sk_type != SOCK_STREAM && sk->sk_type != SOCK_SEQPACKET)
866 return -EINVAL;
867
868 if (sk->sk_state == IUCV_OPEN) {
869 err = iucv_sock_autobind(sk);
870 if (unlikely(err))
871 return err;
Jennifer Hunteac37312007-02-08 13:51:54 -0800872 }
873
Ursula Braun3881ac42011-08-08 01:33:54 +0000874 lock_sock(sk);
875
876 /* Set the destination information */
877 memcpy(iucv->dst_user_id, sa->siucv_user_id, 8);
878 memcpy(iucv->dst_name, sa->siucv_name, 8);
879
880 if (iucv->transport == AF_IUCV_TRANS_HIPER)
Ursula Braun9fbd87d2012-03-07 02:06:23 +0000881 err = iucv_send_ctrl(sock->sk, AF_IUCV_FLAG_SYN);
Ursula Braun3881ac42011-08-08 01:33:54 +0000882 else
883 err = afiucv_path_connect(sock, addr);
884 if (err)
885 goto done;
886
887 if (sk->sk_state != IUCV_CONNECTED)
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +0000888 err = iucv_sock_wait(sk, iucv_sock_in_state(sk, IUCV_CONNECTED,
889 IUCV_DISCONN),
890 sock_sndtimeo(sk, flags & O_NONBLOCK));
Jennifer Hunteac37312007-02-08 13:51:54 -0800891
Ursula Braun3881ac42011-08-08 01:33:54 +0000892 if (sk->sk_state == IUCV_DISCONN || sk->sk_state == IUCV_CLOSED)
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +0000893 err = -ECONNREFUSED;
Ursula Braun18becbc2009-01-05 18:07:46 -0800894
Ursula Braun7d316b92012-02-08 00:19:45 +0000895 if (err && iucv->transport == AF_IUCV_TRANS_IUCV)
896 iucv_sever_path(sk, 0);
Ursula Braun18becbc2009-01-05 18:07:46 -0800897
Jennifer Hunteac37312007-02-08 13:51:54 -0800898done:
899 release_sock(sk);
900 return err;
901}
902
903/* Move a socket into listening state. */
904static int iucv_sock_listen(struct socket *sock, int backlog)
905{
906 struct sock *sk = sock->sk;
907 int err;
908
909 lock_sock(sk);
910
911 err = -EINVAL;
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +0000912 if (sk->sk_state != IUCV_BOUND)
913 goto done;
914
915 if (sock->type != SOCK_STREAM && sock->type != SOCK_SEQPACKET)
Jennifer Hunteac37312007-02-08 13:51:54 -0800916 goto done;
917
918 sk->sk_max_ack_backlog = backlog;
919 sk->sk_ack_backlog = 0;
920 sk->sk_state = IUCV_LISTEN;
921 err = 0;
922
923done:
924 release_sock(sk);
925 return err;
926}
927
928/* Accept a pending connection */
929static int iucv_sock_accept(struct socket *sock, struct socket *newsock,
930 int flags)
931{
932 DECLARE_WAITQUEUE(wait, current);
933 struct sock *sk = sock->sk, *nsk;
934 long timeo;
935 int err = 0;
936
Jennifer Hunt561e0362007-05-04 12:22:07 -0700937 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
Jennifer Hunteac37312007-02-08 13:51:54 -0800938
939 if (sk->sk_state != IUCV_LISTEN) {
940 err = -EBADFD;
941 goto done;
942 }
943
944 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
945
946 /* Wait for an incoming connection */
Eric Dumazetaa395142010-04-20 13:03:51 +0000947 add_wait_queue_exclusive(sk_sleep(sk), &wait);
Heiko Carstensda99f052007-05-04 12:23:27 -0700948 while (!(nsk = iucv_accept_dequeue(sk, newsock))) {
Jennifer Hunteac37312007-02-08 13:51:54 -0800949 set_current_state(TASK_INTERRUPTIBLE);
950 if (!timeo) {
951 err = -EAGAIN;
952 break;
953 }
954
955 release_sock(sk);
956 timeo = schedule_timeout(timeo);
Jennifer Hunt561e0362007-05-04 12:22:07 -0700957 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
Jennifer Hunteac37312007-02-08 13:51:54 -0800958
959 if (sk->sk_state != IUCV_LISTEN) {
960 err = -EBADFD;
961 break;
962 }
963
964 if (signal_pending(current)) {
965 err = sock_intr_errno(timeo);
966 break;
967 }
968 }
969
970 set_current_state(TASK_RUNNING);
Eric Dumazetaa395142010-04-20 13:03:51 +0000971 remove_wait_queue(sk_sleep(sk), &wait);
Jennifer Hunteac37312007-02-08 13:51:54 -0800972
973 if (err)
974 goto done;
975
976 newsock->state = SS_CONNECTED;
977
978done:
979 release_sock(sk);
980 return err;
981}
982
983static int iucv_sock_getname(struct socket *sock, struct sockaddr *addr,
984 int *len, int peer)
985{
986 struct sockaddr_iucv *siucv = (struct sockaddr_iucv *) addr;
987 struct sock *sk = sock->sk;
Ursula Braun493d3972011-08-08 01:33:52 +0000988 struct iucv_sock *iucv = iucv_sk(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -0800989
990 addr->sa_family = AF_IUCV;
991 *len = sizeof(struct sockaddr_iucv);
992
993 if (peer) {
Ursula Braun493d3972011-08-08 01:33:52 +0000994 memcpy(siucv->siucv_user_id, iucv->dst_user_id, 8);
995 memcpy(siucv->siucv_name, iucv->dst_name, 8);
Jennifer Hunteac37312007-02-08 13:51:54 -0800996 } else {
Ursula Braun493d3972011-08-08 01:33:52 +0000997 memcpy(siucv->siucv_user_id, iucv->src_user_id, 8);
998 memcpy(siucv->siucv_name, iucv->src_name, 8);
Jennifer Hunteac37312007-02-08 13:51:54 -0800999 }
1000 memset(&siucv->siucv_port, 0, sizeof(siucv->siucv_port));
1001 memset(&siucv->siucv_addr, 0, sizeof(siucv->siucv_addr));
Ursula Braun493d3972011-08-08 01:33:52 +00001002 memset(&siucv->siucv_nodeid, 0, sizeof(siucv->siucv_nodeid));
Jennifer Hunteac37312007-02-08 13:51:54 -08001003
1004 return 0;
1005}
1006
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001007/**
1008 * iucv_send_iprm() - Send socket data in parameter list of an iucv message.
1009 * @path: IUCV path
1010 * @msg: Pointer to a struct iucv_message
1011 * @skb: The socket data to send, skb->len MUST BE <= 7
1012 *
1013 * Send the socket data in the parameter list in the iucv message
1014 * (IUCV_IPRMDATA). The socket data is stored at index 0 to 6 in the parameter
1015 * list and the socket data len at index 7 (last byte).
1016 * See also iucv_msg_length().
1017 *
1018 * Returns the error code from the iucv_message_send() call.
1019 */
1020static int iucv_send_iprm(struct iucv_path *path, struct iucv_message *msg,
1021 struct sk_buff *skb)
1022{
1023 u8 prmdata[8];
1024
1025 memcpy(prmdata, (void *) skb->data, skb->len);
1026 prmdata[7] = 0xff - (u8) skb->len;
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001027 return pr_iucv->message_send(path, msg, IUCV_IPRMDATA, 0,
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001028 (void *) prmdata, 8);
1029}
1030
Jennifer Hunteac37312007-02-08 13:51:54 -08001031static int iucv_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
1032 struct msghdr *msg, size_t len)
1033{
1034 struct sock *sk = sock->sk;
1035 struct iucv_sock *iucv = iucv_sk(sk);
1036 struct sk_buff *skb;
1037 struct iucv_message txmsg;
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001038 struct cmsghdr *cmsg;
1039 int cmsg_done;
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +00001040 long timeo;
Ursula Braun8f7c5022008-12-25 13:39:47 +01001041 char user_id[9];
1042 char appl_id[9];
Jennifer Hunteac37312007-02-08 13:51:54 -08001043 int err;
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +00001044 int noblock = msg->msg_flags & MSG_DONTWAIT;
Jennifer Hunteac37312007-02-08 13:51:54 -08001045
1046 err = sock_error(sk);
1047 if (err)
1048 return err;
1049
1050 if (msg->msg_flags & MSG_OOB)
1051 return -EOPNOTSUPP;
1052
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001053 /* SOCK_SEQPACKET: we do not support segmented records */
1054 if (sk->sk_type == SOCK_SEQPACKET && !(msg->msg_flags & MSG_EOR))
1055 return -EOPNOTSUPP;
1056
Jennifer Hunteac37312007-02-08 13:51:54 -08001057 lock_sock(sk);
1058
1059 if (sk->sk_shutdown & SEND_SHUTDOWN) {
1060 err = -EPIPE;
1061 goto out;
1062 }
1063
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001064 /* Return if the socket is not in connected state */
1065 if (sk->sk_state != IUCV_CONNECTED) {
1066 err = -ENOTCONN;
1067 goto out;
1068 }
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001069
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001070 /* initialize defaults */
1071 cmsg_done = 0; /* check for duplicate headers */
1072 txmsg.class = 0;
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001073
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001074 /* iterate over control messages */
1075 for (cmsg = CMSG_FIRSTHDR(msg); cmsg;
1076 cmsg = CMSG_NXTHDR(msg, cmsg)) {
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001077
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001078 if (!CMSG_OK(msg, cmsg)) {
1079 err = -EINVAL;
Jennifer Hunt561e0362007-05-04 12:22:07 -07001080 goto out;
Jennifer Hunteac37312007-02-08 13:51:54 -08001081 }
1082
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001083 if (cmsg->cmsg_level != SOL_IUCV)
1084 continue;
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001085
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001086 if (cmsg->cmsg_type & cmsg_done) {
1087 err = -EINVAL;
1088 goto out;
1089 }
1090 cmsg_done |= cmsg->cmsg_type;
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001091
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001092 switch (cmsg->cmsg_type) {
1093 case SCM_IUCV_TRGCLS:
1094 if (cmsg->cmsg_len != CMSG_LEN(TRGCLS_SIZE)) {
1095 err = -EINVAL;
1096 goto out;
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001097 }
1098
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001099 /* set iucv message target class */
1100 memcpy(&txmsg.class,
1101 (void *) CMSG_DATA(cmsg), TRGCLS_SIZE);
1102
1103 break;
1104
1105 default:
1106 err = -EINVAL;
1107 goto out;
1108 break;
1109 }
1110 }
1111
1112 /* allocate one skb for each iucv message:
1113 * this is fine for SOCK_SEQPACKET (unless we want to support
1114 * segmented records using the MSG_EOR flag), but
1115 * for SOCK_STREAM we might want to improve it in future */
Ursula Braun3881ac42011-08-08 01:33:54 +00001116 if (iucv->transport == AF_IUCV_TRANS_HIPER)
1117 skb = sock_alloc_send_skb(sk,
1118 len + sizeof(struct af_iucv_trans_hdr) + ETH_HLEN,
1119 noblock, &err);
1120 else
1121 skb = sock_alloc_send_skb(sk, len, noblock, &err);
Ursula Braun800c5eb2012-02-08 00:19:47 +00001122 if (!skb) {
1123 err = -ENOMEM;
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001124 goto out;
Ursula Braun800c5eb2012-02-08 00:19:47 +00001125 }
Ursula Braun3881ac42011-08-08 01:33:54 +00001126 if (iucv->transport == AF_IUCV_TRANS_HIPER)
1127 skb_reserve(skb, sizeof(struct af_iucv_trans_hdr) + ETH_HLEN);
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001128 if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
1129 err = -EFAULT;
1130 goto fail;
1131 }
1132
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +00001133 /* wait if outstanding messages for iucv path has reached */
1134 timeo = sock_sndtimeo(sk, noblock);
1135 err = iucv_sock_wait(sk, iucv_below_msglim(sk), timeo);
1136 if (err)
1137 goto fail;
1138
1139 /* return -ECONNRESET if the socket is no longer connected */
1140 if (sk->sk_state != IUCV_CONNECTED) {
1141 err = -ECONNRESET;
1142 goto fail;
1143 }
1144
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001145 /* increment and save iucv message tag for msg_completion cbk */
1146 txmsg.tag = iucv->send_tag++;
1147 memcpy(CB_TAG(skb), &txmsg.tag, CB_TAG_LEN);
Ursula Braun800c5eb2012-02-08 00:19:47 +00001148
Ursula Braun3881ac42011-08-08 01:33:54 +00001149 if (iucv->transport == AF_IUCV_TRANS_HIPER) {
1150 atomic_inc(&iucv->msg_sent);
1151 err = afiucv_hs_send(&txmsg, sk, skb, 0);
1152 if (err) {
1153 atomic_dec(&iucv->msg_sent);
1154 goto fail;
1155 }
1156 goto release;
1157 }
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001158 skb_queue_tail(&iucv->send_skb_q, skb);
1159
1160 if (((iucv->path->flags & IUCV_IPRMDATA) & iucv->flags)
1161 && skb->len <= 7) {
1162 err = iucv_send_iprm(iucv->path, &txmsg, skb);
1163
1164 /* on success: there is no message_complete callback
1165 * for an IPRMDATA msg; remove skb from send queue */
1166 if (err == 0) {
1167 skb_unlink(skb, &iucv->send_skb_q);
1168 kfree_skb(skb);
1169 }
1170
1171 /* this error should never happen since the
1172 * IUCV_IPRMDATA path flag is set... sever path */
1173 if (err == 0x15) {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001174 pr_iucv->path_sever(iucv->path, NULL);
Jennifer Hunteac37312007-02-08 13:51:54 -08001175 skb_unlink(skb, &iucv->send_skb_q);
1176 err = -EPIPE;
1177 goto fail;
1178 }
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001179 } else
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001180 err = pr_iucv->message_send(iucv->path, &txmsg, 0, 0,
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001181 (void *) skb->data, skb->len);
1182 if (err) {
1183 if (err == 3) {
1184 user_id[8] = 0;
1185 memcpy(user_id, iucv->dst_user_id, 8);
1186 appl_id[8] = 0;
1187 memcpy(appl_id, iucv->dst_name, 8);
1188 pr_err("Application %s on z/VM guest %s"
1189 " exceeds message limit\n",
1190 appl_id, user_id);
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +00001191 err = -EAGAIN;
1192 } else
1193 err = -EPIPE;
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001194 skb_unlink(skb, &iucv->send_skb_q);
Hendrik Bruecknerbb664f42009-06-17 21:54:47 +00001195 goto fail;
Jennifer Hunteac37312007-02-08 13:51:54 -08001196 }
1197
Ursula Braun3881ac42011-08-08 01:33:54 +00001198release:
Jennifer Hunteac37312007-02-08 13:51:54 -08001199 release_sock(sk);
1200 return len;
1201
1202fail:
1203 kfree_skb(skb);
1204out:
1205 release_sock(sk);
1206 return err;
1207}
1208
Hendrik Bruecknerbf95d202009-09-16 04:37:28 +00001209/* iucv_fragment_skb() - Fragment a single IUCV message into multiple skb's
1210 *
1211 * Locking: must be called with message_q.lock held
1212 */
Ursula Braunf0703c82007-10-08 02:03:31 -07001213static int iucv_fragment_skb(struct sock *sk, struct sk_buff *skb, int len)
1214{
1215 int dataleft, size, copied = 0;
1216 struct sk_buff *nskb;
1217
1218 dataleft = len;
1219 while (dataleft) {
1220 if (dataleft >= sk->sk_rcvbuf / 4)
1221 size = sk->sk_rcvbuf / 4;
1222 else
1223 size = dataleft;
1224
1225 nskb = alloc_skb(size, GFP_ATOMIC | GFP_DMA);
1226 if (!nskb)
1227 return -ENOMEM;
1228
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001229 /* copy target class to control buffer of new skb */
1230 memcpy(CB_TRGCLS(nskb), CB_TRGCLS(skb), CB_TRGCLS_LEN);
1231
1232 /* copy data fragment */
Ursula Braunf0703c82007-10-08 02:03:31 -07001233 memcpy(nskb->data, skb->data + copied, size);
1234 copied += size;
1235 dataleft -= size;
1236
1237 skb_reset_transport_header(nskb);
1238 skb_reset_network_header(nskb);
1239 nskb->len = size;
1240
1241 skb_queue_tail(&iucv_sk(sk)->backlog_skb_q, nskb);
1242 }
1243
1244 return 0;
1245}
1246
Hendrik Bruecknerbf95d202009-09-16 04:37:28 +00001247/* iucv_process_message() - Receive a single outstanding IUCV message
1248 *
1249 * Locking: must be called with message_q.lock held
1250 */
Ursula Braunf0703c82007-10-08 02:03:31 -07001251static void iucv_process_message(struct sock *sk, struct sk_buff *skb,
1252 struct iucv_path *path,
1253 struct iucv_message *msg)
1254{
1255 int rc;
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001256 unsigned int len;
Ursula Braunf0703c82007-10-08 02:03:31 -07001257
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001258 len = iucv_msg_length(msg);
1259
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001260 /* store msg target class in the second 4 bytes of skb ctrl buffer */
1261 /* Note: the first 4 bytes are reserved for msg tag */
1262 memcpy(CB_TRGCLS(skb), &msg->class, CB_TRGCLS_LEN);
1263
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001264 /* check for special IPRM messages (e.g. iucv_sock_shutdown) */
1265 if ((msg->flags & IUCV_IPRMDATA) && len > 7) {
1266 if (memcmp(msg->rmmsg, iprm_shutdown, 8) == 0) {
1267 skb->data = NULL;
1268 skb->len = 0;
1269 }
Ursula Braunf0703c82007-10-08 02:03:31 -07001270 } else {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001271 rc = pr_iucv->message_receive(path, msg,
1272 msg->flags & IUCV_IPRMDATA,
1273 skb->data, len, NULL);
Ursula Braunf0703c82007-10-08 02:03:31 -07001274 if (rc) {
1275 kfree_skb(skb);
1276 return;
1277 }
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001278 /* we need to fragment iucv messages for SOCK_STREAM only;
1279 * for SOCK_SEQPACKET, it is only relevant if we support
1280 * record segmentation using MSG_EOR (see also recvmsg()) */
1281 if (sk->sk_type == SOCK_STREAM &&
1282 skb->truesize >= sk->sk_rcvbuf / 4) {
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001283 rc = iucv_fragment_skb(sk, skb, len);
Ursula Braunf0703c82007-10-08 02:03:31 -07001284 kfree_skb(skb);
1285 skb = NULL;
1286 if (rc) {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001287 pr_iucv->path_sever(path, NULL);
Ursula Braunf0703c82007-10-08 02:03:31 -07001288 return;
1289 }
1290 skb = skb_dequeue(&iucv_sk(sk)->backlog_skb_q);
1291 } else {
1292 skb_reset_transport_header(skb);
1293 skb_reset_network_header(skb);
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001294 skb->len = len;
Ursula Braunf0703c82007-10-08 02:03:31 -07001295 }
1296 }
1297
1298 if (sock_queue_rcv_skb(sk, skb))
1299 skb_queue_head(&iucv_sk(sk)->backlog_skb_q, skb);
1300}
1301
Hendrik Bruecknerbf95d202009-09-16 04:37:28 +00001302/* iucv_process_message_q() - Process outstanding IUCV messages
1303 *
1304 * Locking: must be called with message_q.lock held
1305 */
Ursula Braunf0703c82007-10-08 02:03:31 -07001306static void iucv_process_message_q(struct sock *sk)
1307{
1308 struct iucv_sock *iucv = iucv_sk(sk);
1309 struct sk_buff *skb;
1310 struct sock_msg_q *p, *n;
1311
1312 list_for_each_entry_safe(p, n, &iucv->message_q.list, list) {
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001313 skb = alloc_skb(iucv_msg_length(&p->msg), GFP_ATOMIC | GFP_DMA);
Ursula Braunf0703c82007-10-08 02:03:31 -07001314 if (!skb)
1315 break;
1316 iucv_process_message(sk, skb, p->path, &p->msg);
1317 list_del(&p->list);
1318 kfree(p);
1319 if (!skb_queue_empty(&iucv->backlog_skb_q))
1320 break;
1321 }
1322}
1323
Jennifer Hunteac37312007-02-08 13:51:54 -08001324static int iucv_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
1325 struct msghdr *msg, size_t len, int flags)
1326{
1327 int noblock = flags & MSG_DONTWAIT;
1328 struct sock *sk = sock->sk;
Jennifer Hunt561e0362007-05-04 12:22:07 -07001329 struct iucv_sock *iucv = iucv_sk(sk);
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001330 unsigned int copied, rlen;
Ursula Braun9fbd87d2012-03-07 02:06:23 +00001331 struct sk_buff *skb, *rskb, *cskb;
Jennifer Hunteac37312007-02-08 13:51:54 -08001332 int err = 0;
1333
Ursula Braunaac63992011-12-19 22:56:31 +00001334 if ((sk->sk_state == IUCV_DISCONN) &&
Ursula Braunf0703c82007-10-08 02:03:31 -07001335 skb_queue_empty(&iucv->backlog_skb_q) &&
1336 skb_queue_empty(&sk->sk_receive_queue) &&
1337 list_empty(&iucv->message_q.list))
Jennifer Hunt561e0362007-05-04 12:22:07 -07001338 return 0;
1339
Jennifer Hunteac37312007-02-08 13:51:54 -08001340 if (flags & (MSG_OOB))
1341 return -EOPNOTSUPP;
1342
Hendrik Brueckner60d37052009-04-21 06:04:21 +00001343 /* receive/dequeue next skb:
1344 * the function understands MSG_PEEK and, thus, does not dequeue skb */
Jennifer Hunteac37312007-02-08 13:51:54 -08001345 skb = skb_recv_datagram(sk, flags, noblock, &err);
1346 if (!skb) {
1347 if (sk->sk_shutdown & RCV_SHUTDOWN)
1348 return 0;
1349 return err;
1350 }
1351
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001352 rlen = skb->len; /* real length of skb */
1353 copied = min_t(unsigned int, rlen, len);
Ursula Braun82492a32012-03-07 02:06:24 +00001354 if (!rlen)
1355 sk->sk_shutdown = sk->sk_shutdown | RCV_SHUTDOWN;
Jennifer Hunteac37312007-02-08 13:51:54 -08001356
Jennifer Hunt561e0362007-05-04 12:22:07 -07001357 cskb = skb;
Ursula Braun3881ac42011-08-08 01:33:54 +00001358 if (skb_copy_datagram_iovec(cskb, 0, msg->msg_iov, copied)) {
Hendrik Brueckner802788b2009-04-21 23:26:26 +00001359 if (!(flags & MSG_PEEK))
1360 skb_queue_head(&sk->sk_receive_queue, skb);
1361 return -EFAULT;
Jennifer Hunteac37312007-02-08 13:51:54 -08001362 }
1363
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001364 /* SOCK_SEQPACKET: set MSG_TRUNC if recv buf size is too small */
1365 if (sk->sk_type == SOCK_SEQPACKET) {
1366 if (copied < rlen)
1367 msg->msg_flags |= MSG_TRUNC;
1368 /* each iucv message contains a complete record */
1369 msg->msg_flags |= MSG_EOR;
1370 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001371
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001372 /* create control message to store iucv msg target class:
1373 * get the trgcls from the control buffer of the skb due to
1374 * fragmentation of original iucv message. */
1375 err = put_cmsg(msg, SOL_IUCV, SCM_IUCV_TRGCLS,
1376 CB_TRGCLS_LEN, CB_TRGCLS(skb));
1377 if (err) {
1378 if (!(flags & MSG_PEEK))
1379 skb_queue_head(&sk->sk_receive_queue, skb);
1380 return err;
1381 }
1382
Jennifer Hunteac37312007-02-08 13:51:54 -08001383 /* Mark read part of skb as used */
1384 if (!(flags & MSG_PEEK)) {
Jennifer Hunteac37312007-02-08 13:51:54 -08001385
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001386 /* SOCK_STREAM: re-queue skb if it contains unreceived data */
1387 if (sk->sk_type == SOCK_STREAM) {
1388 skb_pull(skb, copied);
1389 if (skb->len) {
1390 skb_queue_head(&sk->sk_receive_queue, skb);
1391 goto done;
1392 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001393 }
1394
1395 kfree_skb(skb);
Ursula Braun800c5eb2012-02-08 00:19:47 +00001396 if (iucv->transport == AF_IUCV_TRANS_HIPER) {
1397 atomic_inc(&iucv->msg_recv);
1398 if (atomic_read(&iucv->msg_recv) > iucv->msglimit) {
1399 WARN_ON(1);
1400 iucv_sock_close(sk);
1401 return -EFAULT;
1402 }
1403 }
Jennifer Hunt561e0362007-05-04 12:22:07 -07001404
1405 /* Queue backlog skbs */
Hendrik Bruecknerbf95d202009-09-16 04:37:28 +00001406 spin_lock_bh(&iucv->message_q.lock);
Ursula Braunf0703c82007-10-08 02:03:31 -07001407 rskb = skb_dequeue(&iucv->backlog_skb_q);
Heiko Carstensda99f052007-05-04 12:23:27 -07001408 while (rskb) {
Jennifer Hunt561e0362007-05-04 12:22:07 -07001409 if (sock_queue_rcv_skb(sk, rskb)) {
Ursula Braunf0703c82007-10-08 02:03:31 -07001410 skb_queue_head(&iucv->backlog_skb_q,
Jennifer Hunt561e0362007-05-04 12:22:07 -07001411 rskb);
1412 break;
1413 } else {
Ursula Braunf0703c82007-10-08 02:03:31 -07001414 rskb = skb_dequeue(&iucv->backlog_skb_q);
Jennifer Hunt561e0362007-05-04 12:22:07 -07001415 }
1416 }
Ursula Braunf0703c82007-10-08 02:03:31 -07001417 if (skb_queue_empty(&iucv->backlog_skb_q)) {
Ursula Braunf0703c82007-10-08 02:03:31 -07001418 if (!list_empty(&iucv->message_q.list))
1419 iucv_process_message_q(sk);
Ursula Braun3881ac42011-08-08 01:33:54 +00001420 if (atomic_read(&iucv->msg_recv) >=
1421 iucv->msglimit / 2) {
Ursula Braun9fbd87d2012-03-07 02:06:23 +00001422 err = iucv_send_ctrl(sk, AF_IUCV_FLAG_WIN);
Ursula Braun3881ac42011-08-08 01:33:54 +00001423 if (err) {
1424 sk->sk_state = IUCV_DISCONN;
1425 sk->sk_state_change(sk);
1426 }
1427 }
Ursula Braunf0703c82007-10-08 02:03:31 -07001428 }
Hendrik Bruecknerbf95d202009-09-16 04:37:28 +00001429 spin_unlock_bh(&iucv->message_q.lock);
Hendrik Brueckner60d37052009-04-21 06:04:21 +00001430 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001431
1432done:
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001433 /* SOCK_SEQPACKET: return real length if MSG_TRUNC is set */
1434 if (sk->sk_type == SOCK_SEQPACKET && (flags & MSG_TRUNC))
1435 copied = rlen;
1436
1437 return copied;
Jennifer Hunteac37312007-02-08 13:51:54 -08001438}
1439
1440static inline unsigned int iucv_accept_poll(struct sock *parent)
1441{
1442 struct iucv_sock *isk, *n;
1443 struct sock *sk;
1444
Heiko Carstensda99f052007-05-04 12:23:27 -07001445 list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) {
Jennifer Hunteac37312007-02-08 13:51:54 -08001446 sk = (struct sock *) isk;
1447
1448 if (sk->sk_state == IUCV_CONNECTED)
1449 return POLLIN | POLLRDNORM;
1450 }
1451
1452 return 0;
1453}
1454
1455unsigned int iucv_sock_poll(struct file *file, struct socket *sock,
1456 poll_table *wait)
1457{
1458 struct sock *sk = sock->sk;
1459 unsigned int mask = 0;
1460
Eric Dumazetaa395142010-04-20 13:03:51 +00001461 sock_poll_wait(file, sk_sleep(sk), wait);
Jennifer Hunteac37312007-02-08 13:51:54 -08001462
1463 if (sk->sk_state == IUCV_LISTEN)
1464 return iucv_accept_poll(sk);
1465
1466 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
1467 mask |= POLLERR;
1468
1469 if (sk->sk_shutdown & RCV_SHUTDOWN)
1470 mask |= POLLRDHUP;
1471
1472 if (sk->sk_shutdown == SHUTDOWN_MASK)
1473 mask |= POLLHUP;
1474
1475 if (!skb_queue_empty(&sk->sk_receive_queue) ||
Heiko Carstensda99f052007-05-04 12:23:27 -07001476 (sk->sk_shutdown & RCV_SHUTDOWN))
Jennifer Hunteac37312007-02-08 13:51:54 -08001477 mask |= POLLIN | POLLRDNORM;
1478
1479 if (sk->sk_state == IUCV_CLOSED)
1480 mask |= POLLHUP;
1481
Ursula Braunaac63992011-12-19 22:56:31 +00001482 if (sk->sk_state == IUCV_DISCONN)
Jennifer Hunt561e0362007-05-04 12:22:07 -07001483 mask |= POLLIN;
1484
Ursula Braun7f1b0ea2012-02-08 00:19:46 +00001485 if (sock_writeable(sk) && iucv_below_msglim(sk))
Jennifer Hunteac37312007-02-08 13:51:54 -08001486 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
1487 else
1488 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
1489
1490 return mask;
1491}
1492
1493static int iucv_sock_shutdown(struct socket *sock, int how)
1494{
1495 struct sock *sk = sock->sk;
1496 struct iucv_sock *iucv = iucv_sk(sk);
1497 struct iucv_message txmsg;
1498 int err = 0;
Jennifer Hunteac37312007-02-08 13:51:54 -08001499
1500 how++;
1501
1502 if ((how & ~SHUTDOWN_MASK) || !how)
1503 return -EINVAL;
1504
1505 lock_sock(sk);
Heiko Carstensda99f052007-05-04 12:23:27 -07001506 switch (sk->sk_state) {
Ursula Braun82492a32012-03-07 02:06:24 +00001507 case IUCV_LISTEN:
Hendrik Bruecknere14ad5f2009-04-21 06:04:23 +00001508 case IUCV_DISCONN:
1509 case IUCV_CLOSING:
Jennifer Hunteac37312007-02-08 13:51:54 -08001510 case IUCV_CLOSED:
1511 err = -ENOTCONN;
1512 goto fail;
Jennifer Hunteac37312007-02-08 13:51:54 -08001513 default:
Jennifer Hunteac37312007-02-08 13:51:54 -08001514 break;
1515 }
1516
1517 if (how == SEND_SHUTDOWN || how == SHUTDOWN_MASK) {
Ursula Braun82492a32012-03-07 02:06:24 +00001518 if (iucv->transport == AF_IUCV_TRANS_IUCV) {
1519 txmsg.class = 0;
1520 txmsg.tag = 0;
1521 err = pr_iucv->message_send(iucv->path, &txmsg,
1522 IUCV_IPRMDATA, 0, (void *) iprm_shutdown, 8);
1523 if (err) {
1524 switch (err) {
1525 case 1:
1526 err = -ENOTCONN;
1527 break;
1528 case 2:
1529 err = -ECONNRESET;
1530 break;
1531 default:
1532 err = -ENOTCONN;
1533 break;
1534 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001535 }
Ursula Braun82492a32012-03-07 02:06:24 +00001536 } else
1537 iucv_send_ctrl(sk, AF_IUCV_FLAG_SHT);
Jennifer Hunteac37312007-02-08 13:51:54 -08001538 }
1539
Ursula Braun82492a32012-03-07 02:06:24 +00001540 sk->sk_shutdown |= how;
Jennifer Hunteac37312007-02-08 13:51:54 -08001541 if (how == RCV_SHUTDOWN || how == SHUTDOWN_MASK) {
Ursula Braun82492a32012-03-07 02:06:24 +00001542 if (iucv->transport == AF_IUCV_TRANS_IUCV) {
1543 err = pr_iucv->path_quiesce(iucv->path, NULL);
1544 if (err)
1545 err = -ENOTCONN;
1546/* skb_queue_purge(&sk->sk_receive_queue); */
1547 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001548 skb_queue_purge(&sk->sk_receive_queue);
1549 }
1550
1551 /* Wake up anyone sleeping in poll */
1552 sk->sk_state_change(sk);
1553
1554fail:
1555 release_sock(sk);
1556 return err;
1557}
1558
1559static int iucv_sock_release(struct socket *sock)
1560{
1561 struct sock *sk = sock->sk;
1562 int err = 0;
1563
1564 if (!sk)
1565 return 0;
1566
1567 iucv_sock_close(sk);
1568
Jennifer Hunteac37312007-02-08 13:51:54 -08001569 sock_orphan(sk);
1570 iucv_sock_kill(sk);
1571 return err;
1572}
1573
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00001574/* getsockopt and setsockopt */
1575static int iucv_sock_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07001576 char __user *optval, unsigned int optlen)
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00001577{
1578 struct sock *sk = sock->sk;
1579 struct iucv_sock *iucv = iucv_sk(sk);
1580 int val;
1581 int rc;
1582
1583 if (level != SOL_IUCV)
1584 return -ENOPROTOOPT;
1585
1586 if (optlen < sizeof(int))
1587 return -EINVAL;
1588
1589 if (get_user(val, (int __user *) optval))
1590 return -EFAULT;
1591
1592 rc = 0;
1593
1594 lock_sock(sk);
1595 switch (optname) {
1596 case SO_IPRMDATA_MSG:
1597 if (val)
1598 iucv->flags |= IUCV_IPRMDATA;
1599 else
1600 iucv->flags &= ~IUCV_IPRMDATA;
1601 break;
Hendrik Brueckner09488e22009-04-21 23:26:27 +00001602 case SO_MSGLIMIT:
1603 switch (sk->sk_state) {
1604 case IUCV_OPEN:
1605 case IUCV_BOUND:
1606 if (val < 1 || val > (u16)(~0))
1607 rc = -EINVAL;
1608 else
1609 iucv->msglimit = val;
1610 break;
1611 default:
1612 rc = -EINVAL;
1613 break;
1614 }
1615 break;
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00001616 default:
1617 rc = -ENOPROTOOPT;
1618 break;
1619 }
1620 release_sock(sk);
1621
1622 return rc;
1623}
1624
1625static int iucv_sock_getsockopt(struct socket *sock, int level, int optname,
1626 char __user *optval, int __user *optlen)
1627{
1628 struct sock *sk = sock->sk;
1629 struct iucv_sock *iucv = iucv_sk(sk);
Ursula Braun51363b82012-02-08 00:19:48 +00001630 unsigned int val;
1631 int len;
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00001632
1633 if (level != SOL_IUCV)
1634 return -ENOPROTOOPT;
1635
1636 if (get_user(len, optlen))
1637 return -EFAULT;
1638
1639 if (len < 0)
1640 return -EINVAL;
1641
1642 len = min_t(unsigned int, len, sizeof(int));
1643
1644 switch (optname) {
1645 case SO_IPRMDATA_MSG:
1646 val = (iucv->flags & IUCV_IPRMDATA) ? 1 : 0;
1647 break;
Hendrik Brueckner09488e22009-04-21 23:26:27 +00001648 case SO_MSGLIMIT:
1649 lock_sock(sk);
1650 val = (iucv->path != NULL) ? iucv->path->msglim /* connected */
1651 : iucv->msglimit; /* default */
1652 release_sock(sk);
1653 break;
Ursula Braun51363b82012-02-08 00:19:48 +00001654 case SO_MSGSIZE:
1655 if (sk->sk_state == IUCV_OPEN)
1656 return -EBADFD;
1657 val = (iucv->hs_dev) ? iucv->hs_dev->mtu -
1658 sizeof(struct af_iucv_trans_hdr) - ETH_HLEN :
1659 0x7fffffff;
1660 break;
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00001661 default:
1662 return -ENOPROTOOPT;
1663 }
1664
1665 if (put_user(len, optlen))
1666 return -EFAULT;
1667 if (copy_to_user(optval, &val, len))
1668 return -EFAULT;
1669
1670 return 0;
1671}
1672
1673
Jennifer Hunteac37312007-02-08 13:51:54 -08001674/* Callback wrappers - called from iucv base support */
1675static int iucv_callback_connreq(struct iucv_path *path,
1676 u8 ipvmid[8], u8 ipuser[16])
1677{
1678 unsigned char user_data[16];
1679 unsigned char nuser_data[16];
1680 unsigned char src_name[8];
1681 struct hlist_node *node;
1682 struct sock *sk, *nsk;
1683 struct iucv_sock *iucv, *niucv;
1684 int err;
1685
1686 memcpy(src_name, ipuser, 8);
1687 EBCASC(src_name, 8);
1688 /* Find out if this path belongs to af_iucv. */
1689 read_lock(&iucv_sk_list.lock);
1690 iucv = NULL;
Ursula Braunfebca282007-07-14 19:04:25 -07001691 sk = NULL;
Jennifer Hunteac37312007-02-08 13:51:54 -08001692 sk_for_each(sk, node, &iucv_sk_list.head)
1693 if (sk->sk_state == IUCV_LISTEN &&
1694 !memcmp(&iucv_sk(sk)->src_name, src_name, 8)) {
1695 /*
1696 * Found a listening socket with
1697 * src_name == ipuser[0-7].
1698 */
1699 iucv = iucv_sk(sk);
1700 break;
1701 }
1702 read_unlock(&iucv_sk_list.lock);
1703 if (!iucv)
1704 /* No socket found, not one of our paths. */
1705 return -EINVAL;
1706
1707 bh_lock_sock(sk);
1708
1709 /* Check if parent socket is listening */
1710 low_nmcpy(user_data, iucv->src_name);
1711 high_nmcpy(user_data, iucv->dst_name);
1712 ASCEBC(user_data, sizeof(user_data));
1713 if (sk->sk_state != IUCV_LISTEN) {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001714 err = pr_iucv->path_sever(path, user_data);
Hendrik Brueckner65dbd7c2009-01-05 18:08:23 -08001715 iucv_path_free(path);
Jennifer Hunteac37312007-02-08 13:51:54 -08001716 goto fail;
1717 }
1718
1719 /* Check for backlog size */
1720 if (sk_acceptq_is_full(sk)) {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001721 err = pr_iucv->path_sever(path, user_data);
Hendrik Brueckner65dbd7c2009-01-05 18:08:23 -08001722 iucv_path_free(path);
Jennifer Hunteac37312007-02-08 13:51:54 -08001723 goto fail;
1724 }
1725
1726 /* Create the new socket */
Hendrik Brueckneraa8e71f2009-04-21 23:26:25 +00001727 nsk = iucv_sock_alloc(NULL, sk->sk_type, GFP_ATOMIC);
Heiko Carstensda99f052007-05-04 12:23:27 -07001728 if (!nsk) {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001729 err = pr_iucv->path_sever(path, user_data);
Hendrik Brueckner65dbd7c2009-01-05 18:08:23 -08001730 iucv_path_free(path);
Jennifer Hunteac37312007-02-08 13:51:54 -08001731 goto fail;
1732 }
1733
1734 niucv = iucv_sk(nsk);
1735 iucv_sock_init(nsk, sk);
1736
1737 /* Set the new iucv_sock */
1738 memcpy(niucv->dst_name, ipuser + 8, 8);
1739 EBCASC(niucv->dst_name, 8);
1740 memcpy(niucv->dst_user_id, ipvmid, 8);
1741 memcpy(niucv->src_name, iucv->src_name, 8);
1742 memcpy(niucv->src_user_id, iucv->src_user_id, 8);
1743 niucv->path = path;
1744
1745 /* Call iucv_accept */
1746 high_nmcpy(nuser_data, ipuser + 8);
1747 memcpy(nuser_data + 8, niucv->src_name, 8);
1748 ASCEBC(nuser_data + 8, 8);
1749
Hendrik Brueckner09488e22009-04-21 23:26:27 +00001750 /* set message limit for path based on msglimit of accepting socket */
1751 niucv->msglimit = iucv->msglimit;
1752 path->msglim = iucv->msglimit;
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001753 err = pr_iucv->path_accept(path, &af_iucv_handler, nuser_data, nsk);
Heiko Carstensda99f052007-05-04 12:23:27 -07001754 if (err) {
Ursula Braun7d316b92012-02-08 00:19:45 +00001755 iucv_sever_path(nsk, 1);
Hendrik Brueckner65dbd7c2009-01-05 18:08:23 -08001756 iucv_sock_kill(nsk);
Jennifer Hunteac37312007-02-08 13:51:54 -08001757 goto fail;
1758 }
1759
1760 iucv_accept_enqueue(sk, nsk);
1761
1762 /* Wake up accept */
1763 nsk->sk_state = IUCV_CONNECTED;
1764 sk->sk_data_ready(sk, 1);
1765 err = 0;
1766fail:
1767 bh_unlock_sock(sk);
1768 return 0;
1769}
1770
1771static void iucv_callback_connack(struct iucv_path *path, u8 ipuser[16])
1772{
1773 struct sock *sk = path->private;
1774
1775 sk->sk_state = IUCV_CONNECTED;
1776 sk->sk_state_change(sk);
1777}
1778
1779static void iucv_callback_rx(struct iucv_path *path, struct iucv_message *msg)
1780{
1781 struct sock *sk = path->private;
Jennifer Hunt561e0362007-05-04 12:22:07 -07001782 struct iucv_sock *iucv = iucv_sk(sk);
Ursula Braunf0703c82007-10-08 02:03:31 -07001783 struct sk_buff *skb;
1784 struct sock_msg_q *save_msg;
1785 int len;
Jennifer Hunt561e0362007-05-04 12:22:07 -07001786
Hendrik Bruecknerfe86e542009-04-21 06:04:22 +00001787 if (sk->sk_shutdown & RCV_SHUTDOWN) {
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00001788 pr_iucv->message_reject(path, msg);
Jennifer Hunteac37312007-02-08 13:51:54 -08001789 return;
Hendrik Bruecknerfe86e542009-04-21 06:04:22 +00001790 }
Jennifer Hunteac37312007-02-08 13:51:54 -08001791
Hendrik Brueckner3fa6b5a2009-04-21 06:04:24 +00001792 spin_lock(&iucv->message_q.lock);
Jennifer Hunteac37312007-02-08 13:51:54 -08001793
Ursula Braunf0703c82007-10-08 02:03:31 -07001794 if (!list_empty(&iucv->message_q.list) ||
1795 !skb_queue_empty(&iucv->backlog_skb_q))
1796 goto save_message;
1797
1798 len = atomic_read(&sk->sk_rmem_alloc);
Eric Dumazet87fb4b72011-10-13 07:28:54 +00001799 len += SKB_TRUESIZE(iucv_msg_length(msg));
Ursula Braunf0703c82007-10-08 02:03:31 -07001800 if (len > sk->sk_rcvbuf)
1801 goto save_message;
1802
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001803 skb = alloc_skb(iucv_msg_length(msg), GFP_ATOMIC | GFP_DMA);
Ursula Braunf0703c82007-10-08 02:03:31 -07001804 if (!skb)
1805 goto save_message;
Jennifer Hunteac37312007-02-08 13:51:54 -08001806
Ursula Braunf0703c82007-10-08 02:03:31 -07001807 iucv_process_message(sk, skb, path, msg);
Hendrik Brueckner3fa6b5a2009-04-21 06:04:24 +00001808 goto out_unlock;
Jennifer Hunteac37312007-02-08 13:51:54 -08001809
Ursula Braunf0703c82007-10-08 02:03:31 -07001810save_message:
1811 save_msg = kzalloc(sizeof(struct sock_msg_q), GFP_ATOMIC | GFP_DMA);
Ursula Braund4444722008-02-07 18:07:19 -08001812 if (!save_msg)
Julia Lawalla56635a2010-05-26 05:56:48 +00001813 goto out_unlock;
Ursula Braunf0703c82007-10-08 02:03:31 -07001814 save_msg->path = path;
1815 save_msg->msg = *msg;
1816
Ursula Braunf0703c82007-10-08 02:03:31 -07001817 list_add_tail(&save_msg->list, &iucv->message_q.list);
Hendrik Brueckner3fa6b5a2009-04-21 06:04:24 +00001818
1819out_unlock:
Ursula Braunf0703c82007-10-08 02:03:31 -07001820 spin_unlock(&iucv->message_q.lock);
Jennifer Hunteac37312007-02-08 13:51:54 -08001821}
1822
1823static void iucv_callback_txdone(struct iucv_path *path,
1824 struct iucv_message *msg)
1825{
1826 struct sock *sk = path->private;
Ursula Braunf2a77992008-02-07 18:07:44 -08001827 struct sk_buff *this = NULL;
Jennifer Hunteac37312007-02-08 13:51:54 -08001828 struct sk_buff_head *list = &iucv_sk(sk)->send_skb_q;
1829 struct sk_buff *list_skb = list->next;
1830 unsigned long flags;
1831
Ursula Braun7d316b92012-02-08 00:19:45 +00001832 bh_lock_sock(sk);
Ursula Braunf2a77992008-02-07 18:07:44 -08001833 if (!skb_queue_empty(list)) {
Jennifer Hunt561e0362007-05-04 12:22:07 -07001834 spin_lock_irqsave(&list->lock, flags);
Jennifer Hunteac37312007-02-08 13:51:54 -08001835
Ursula Braunf2a77992008-02-07 18:07:44 -08001836 while (list_skb != (struct sk_buff *)list) {
Hendrik Brueckner44b1e6b2009-04-21 23:26:24 +00001837 if (!memcmp(&msg->tag, CB_TAG(list_skb), CB_TAG_LEN)) {
Ursula Braunf2a77992008-02-07 18:07:44 -08001838 this = list_skb;
1839 break;
1840 }
Jennifer Hunt561e0362007-05-04 12:22:07 -07001841 list_skb = list_skb->next;
Ursula Braunf2a77992008-02-07 18:07:44 -08001842 }
1843 if (this)
1844 __skb_unlink(this, list);
Jennifer Hunteac37312007-02-08 13:51:54 -08001845
Jennifer Hunt561e0362007-05-04 12:22:07 -07001846 spin_unlock_irqrestore(&list->lock, flags);
Jennifer Hunteac37312007-02-08 13:51:54 -08001847
Hendrik Brueckner0ea920d2009-06-17 21:54:48 +00001848 if (this) {
1849 kfree_skb(this);
1850 /* wake up any process waiting for sending */
1851 iucv_sock_wake_msglim(sk);
1852 }
Jennifer Hunt561e0362007-05-04 12:22:07 -07001853 }
1854
Heiko Carstensda99f052007-05-04 12:23:27 -07001855 if (sk->sk_state == IUCV_CLOSING) {
Jennifer Hunt561e0362007-05-04 12:22:07 -07001856 if (skb_queue_empty(&iucv_sk(sk)->send_skb_q)) {
1857 sk->sk_state = IUCV_CLOSED;
1858 sk->sk_state_change(sk);
1859 }
1860 }
Ursula Braun7d316b92012-02-08 00:19:45 +00001861 bh_unlock_sock(sk);
Jennifer Hunt561e0362007-05-04 12:22:07 -07001862
Jennifer Hunteac37312007-02-08 13:51:54 -08001863}
1864
1865static void iucv_callback_connrej(struct iucv_path *path, u8 ipuser[16])
1866{
1867 struct sock *sk = path->private;
1868
Ursula Braun7d316b92012-02-08 00:19:45 +00001869 if (sk->sk_state == IUCV_CLOSED)
1870 return;
1871
1872 bh_lock_sock(sk);
1873 iucv_sever_path(sk, 1);
Ursula Braunaac63992011-12-19 22:56:31 +00001874 sk->sk_state = IUCV_DISCONN;
Jennifer Hunteac37312007-02-08 13:51:54 -08001875
1876 sk->sk_state_change(sk);
Ursula Braun7d316b92012-02-08 00:19:45 +00001877 bh_unlock_sock(sk);
Jennifer Hunteac37312007-02-08 13:51:54 -08001878}
1879
Hendrik Brueckneraf88b522009-04-21 23:26:21 +00001880/* called if the other communication side shuts down its RECV direction;
1881 * in turn, the callback sets SEND_SHUTDOWN to disable sending of data.
1882 */
1883static void iucv_callback_shutdown(struct iucv_path *path, u8 ipuser[16])
1884{
1885 struct sock *sk = path->private;
1886
1887 bh_lock_sock(sk);
1888 if (sk->sk_state != IUCV_CLOSED) {
1889 sk->sk_shutdown |= SEND_SHUTDOWN;
1890 sk->sk_state_change(sk);
1891 }
1892 bh_unlock_sock(sk);
1893}
1894
Ursula Braun3881ac42011-08-08 01:33:54 +00001895/***************** HiperSockets transport callbacks ********************/
1896static void afiucv_swap_src_dest(struct sk_buff *skb)
1897{
1898 struct af_iucv_trans_hdr *trans_hdr =
1899 (struct af_iucv_trans_hdr *)skb->data;
1900 char tmpID[8];
1901 char tmpName[8];
1902
1903 ASCEBC(trans_hdr->destUserID, sizeof(trans_hdr->destUserID));
1904 ASCEBC(trans_hdr->destAppName, sizeof(trans_hdr->destAppName));
1905 ASCEBC(trans_hdr->srcUserID, sizeof(trans_hdr->srcUserID));
1906 ASCEBC(trans_hdr->srcAppName, sizeof(trans_hdr->srcAppName));
1907 memcpy(tmpID, trans_hdr->srcUserID, 8);
1908 memcpy(tmpName, trans_hdr->srcAppName, 8);
1909 memcpy(trans_hdr->srcUserID, trans_hdr->destUserID, 8);
1910 memcpy(trans_hdr->srcAppName, trans_hdr->destAppName, 8);
1911 memcpy(trans_hdr->destUserID, tmpID, 8);
1912 memcpy(trans_hdr->destAppName, tmpName, 8);
1913 skb_push(skb, ETH_HLEN);
1914 memset(skb->data, 0, ETH_HLEN);
1915}
1916
1917/**
1918 * afiucv_hs_callback_syn - react on received SYN
1919 **/
1920static int afiucv_hs_callback_syn(struct sock *sk, struct sk_buff *skb)
1921{
1922 struct sock *nsk;
1923 struct iucv_sock *iucv, *niucv;
1924 struct af_iucv_trans_hdr *trans_hdr;
1925 int err;
1926
1927 iucv = iucv_sk(sk);
1928 trans_hdr = (struct af_iucv_trans_hdr *)skb->data;
1929 if (!iucv) {
1930 /* no sock - connection refused */
1931 afiucv_swap_src_dest(skb);
1932 trans_hdr->flags = AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN;
1933 err = dev_queue_xmit(skb);
1934 goto out;
1935 }
1936
1937 nsk = iucv_sock_alloc(NULL, sk->sk_type, GFP_ATOMIC);
1938 bh_lock_sock(sk);
1939 if ((sk->sk_state != IUCV_LISTEN) ||
1940 sk_acceptq_is_full(sk) ||
1941 !nsk) {
1942 /* error on server socket - connection refused */
1943 if (nsk)
1944 sk_free(nsk);
1945 afiucv_swap_src_dest(skb);
1946 trans_hdr->flags = AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN;
1947 err = dev_queue_xmit(skb);
1948 bh_unlock_sock(sk);
1949 goto out;
1950 }
1951
1952 niucv = iucv_sk(nsk);
1953 iucv_sock_init(nsk, sk);
1954 niucv->transport = AF_IUCV_TRANS_HIPER;
1955 niucv->msglimit = iucv->msglimit;
1956 if (!trans_hdr->window)
1957 niucv->msglimit_peer = IUCV_HIPER_MSGLIM_DEFAULT;
1958 else
1959 niucv->msglimit_peer = trans_hdr->window;
1960 memcpy(niucv->dst_name, trans_hdr->srcAppName, 8);
1961 memcpy(niucv->dst_user_id, trans_hdr->srcUserID, 8);
1962 memcpy(niucv->src_name, iucv->src_name, 8);
1963 memcpy(niucv->src_user_id, iucv->src_user_id, 8);
1964 nsk->sk_bound_dev_if = sk->sk_bound_dev_if;
Ursula Braun800c5eb2012-02-08 00:19:47 +00001965 niucv->hs_dev = iucv->hs_dev;
1966 dev_hold(niucv->hs_dev);
Ursula Braun3881ac42011-08-08 01:33:54 +00001967 afiucv_swap_src_dest(skb);
1968 trans_hdr->flags = AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_ACK;
1969 trans_hdr->window = niucv->msglimit;
1970 /* if receiver acks the xmit connection is established */
1971 err = dev_queue_xmit(skb);
1972 if (!err) {
1973 iucv_accept_enqueue(sk, nsk);
1974 nsk->sk_state = IUCV_CONNECTED;
1975 sk->sk_data_ready(sk, 1);
1976 } else
1977 iucv_sock_kill(nsk);
1978 bh_unlock_sock(sk);
1979
1980out:
1981 return NET_RX_SUCCESS;
1982}
1983
1984/**
1985 * afiucv_hs_callback_synack() - react on received SYN-ACK
1986 **/
1987static int afiucv_hs_callback_synack(struct sock *sk, struct sk_buff *skb)
1988{
1989 struct iucv_sock *iucv = iucv_sk(sk);
1990 struct af_iucv_trans_hdr *trans_hdr =
1991 (struct af_iucv_trans_hdr *)skb->data;
1992
1993 if (!iucv)
1994 goto out;
1995 if (sk->sk_state != IUCV_BOUND)
1996 goto out;
1997 bh_lock_sock(sk);
1998 iucv->msglimit_peer = trans_hdr->window;
1999 sk->sk_state = IUCV_CONNECTED;
2000 sk->sk_state_change(sk);
2001 bh_unlock_sock(sk);
2002out:
2003 kfree_skb(skb);
2004 return NET_RX_SUCCESS;
2005}
2006
2007/**
2008 * afiucv_hs_callback_synfin() - react on received SYN_FIN
2009 **/
2010static int afiucv_hs_callback_synfin(struct sock *sk, struct sk_buff *skb)
2011{
2012 struct iucv_sock *iucv = iucv_sk(sk);
2013
2014 if (!iucv)
2015 goto out;
2016 if (sk->sk_state != IUCV_BOUND)
2017 goto out;
2018 bh_lock_sock(sk);
2019 sk->sk_state = IUCV_DISCONN;
2020 sk->sk_state_change(sk);
2021 bh_unlock_sock(sk);
2022out:
2023 kfree_skb(skb);
2024 return NET_RX_SUCCESS;
2025}
2026
2027/**
2028 * afiucv_hs_callback_fin() - react on received FIN
2029 **/
2030static int afiucv_hs_callback_fin(struct sock *sk, struct sk_buff *skb)
2031{
2032 struct iucv_sock *iucv = iucv_sk(sk);
2033
2034 /* other end of connection closed */
Ursula Braun800c5eb2012-02-08 00:19:47 +00002035 if (!iucv)
2036 goto out;
2037 bh_lock_sock(sk);
2038 if (sk->sk_state == IUCV_CONNECTED) {
Ursula Braunaac63992011-12-19 22:56:31 +00002039 sk->sk_state = IUCV_DISCONN;
Ursula Braun3881ac42011-08-08 01:33:54 +00002040 sk->sk_state_change(sk);
Ursula Braun3881ac42011-08-08 01:33:54 +00002041 }
Ursula Braun800c5eb2012-02-08 00:19:47 +00002042 bh_unlock_sock(sk);
2043out:
Ursula Braun3881ac42011-08-08 01:33:54 +00002044 kfree_skb(skb);
2045 return NET_RX_SUCCESS;
2046}
2047
2048/**
2049 * afiucv_hs_callback_win() - react on received WIN
2050 **/
2051static int afiucv_hs_callback_win(struct sock *sk, struct sk_buff *skb)
2052{
2053 struct iucv_sock *iucv = iucv_sk(sk);
2054 struct af_iucv_trans_hdr *trans_hdr =
2055 (struct af_iucv_trans_hdr *)skb->data;
2056
2057 if (!iucv)
2058 return NET_RX_SUCCESS;
2059
2060 if (sk->sk_state != IUCV_CONNECTED)
2061 return NET_RX_SUCCESS;
2062
2063 atomic_sub(trans_hdr->window, &iucv->msg_sent);
2064 iucv_sock_wake_msglim(sk);
2065 return NET_RX_SUCCESS;
2066}
2067
2068/**
2069 * afiucv_hs_callback_rx() - react on received data
2070 **/
2071static int afiucv_hs_callback_rx(struct sock *sk, struct sk_buff *skb)
2072{
2073 struct iucv_sock *iucv = iucv_sk(sk);
2074
2075 if (!iucv) {
2076 kfree_skb(skb);
2077 return NET_RX_SUCCESS;
2078 }
2079
2080 if (sk->sk_state != IUCV_CONNECTED) {
2081 kfree_skb(skb);
2082 return NET_RX_SUCCESS;
2083 }
2084
Ursula Braun82492a32012-03-07 02:06:24 +00002085 if (sk->sk_shutdown & RCV_SHUTDOWN) {
2086 kfree_skb(skb);
2087 return NET_RX_SUCCESS;
2088 }
2089
Ursula Braun3881ac42011-08-08 01:33:54 +00002090 /* write stuff from iucv_msg to skb cb */
Ursula Braun82492a32012-03-07 02:06:24 +00002091 if (skb->len < sizeof(struct af_iucv_trans_hdr)) {
Ursula Braun3881ac42011-08-08 01:33:54 +00002092 kfree_skb(skb);
2093 return NET_RX_SUCCESS;
2094 }
2095 skb_pull(skb, sizeof(struct af_iucv_trans_hdr));
2096 skb_reset_transport_header(skb);
2097 skb_reset_network_header(skb);
2098 spin_lock(&iucv->message_q.lock);
2099 if (skb_queue_empty(&iucv->backlog_skb_q)) {
2100 if (sock_queue_rcv_skb(sk, skb)) {
2101 /* handle rcv queue full */
2102 skb_queue_tail(&iucv->backlog_skb_q, skb);
2103 }
2104 } else
2105 skb_queue_tail(&iucv_sk(sk)->backlog_skb_q, skb);
2106 spin_unlock(&iucv->message_q.lock);
2107 return NET_RX_SUCCESS;
2108}
2109
2110/**
2111 * afiucv_hs_rcv() - base function for arriving data through HiperSockets
2112 * transport
2113 * called from netif RX softirq
2114 **/
2115static int afiucv_hs_rcv(struct sk_buff *skb, struct net_device *dev,
2116 struct packet_type *pt, struct net_device *orig_dev)
2117{
2118 struct hlist_node *node;
2119 struct sock *sk;
2120 struct iucv_sock *iucv;
2121 struct af_iucv_trans_hdr *trans_hdr;
2122 char nullstring[8];
2123 int err = 0;
2124
2125 skb_pull(skb, ETH_HLEN);
2126 trans_hdr = (struct af_iucv_trans_hdr *)skb->data;
2127 EBCASC(trans_hdr->destAppName, sizeof(trans_hdr->destAppName));
2128 EBCASC(trans_hdr->destUserID, sizeof(trans_hdr->destUserID));
2129 EBCASC(trans_hdr->srcAppName, sizeof(trans_hdr->srcAppName));
2130 EBCASC(trans_hdr->srcUserID, sizeof(trans_hdr->srcUserID));
2131 memset(nullstring, 0, sizeof(nullstring));
2132 iucv = NULL;
2133 sk = NULL;
2134 read_lock(&iucv_sk_list.lock);
2135 sk_for_each(sk, node, &iucv_sk_list.head) {
2136 if (trans_hdr->flags == AF_IUCV_FLAG_SYN) {
2137 if ((!memcmp(&iucv_sk(sk)->src_name,
2138 trans_hdr->destAppName, 8)) &&
2139 (!memcmp(&iucv_sk(sk)->src_user_id,
2140 trans_hdr->destUserID, 8)) &&
2141 (!memcmp(&iucv_sk(sk)->dst_name, nullstring, 8)) &&
2142 (!memcmp(&iucv_sk(sk)->dst_user_id,
2143 nullstring, 8))) {
2144 iucv = iucv_sk(sk);
2145 break;
2146 }
2147 } else {
2148 if ((!memcmp(&iucv_sk(sk)->src_name,
2149 trans_hdr->destAppName, 8)) &&
2150 (!memcmp(&iucv_sk(sk)->src_user_id,
2151 trans_hdr->destUserID, 8)) &&
2152 (!memcmp(&iucv_sk(sk)->dst_name,
2153 trans_hdr->srcAppName, 8)) &&
2154 (!memcmp(&iucv_sk(sk)->dst_user_id,
2155 trans_hdr->srcUserID, 8))) {
2156 iucv = iucv_sk(sk);
2157 break;
2158 }
2159 }
2160 }
2161 read_unlock(&iucv_sk_list.lock);
2162 if (!iucv)
2163 sk = NULL;
2164
2165 /* no sock
2166 how should we send with no sock
2167 1) send without sock no send rc checking?
2168 2) introduce default sock to handle this cases
2169
2170 SYN -> send SYN|ACK in good case, send SYN|FIN in bad case
2171 data -> send FIN
2172 SYN|ACK, SYN|FIN, FIN -> no action? */
2173
2174 switch (trans_hdr->flags) {
2175 case AF_IUCV_FLAG_SYN:
2176 /* connect request */
2177 err = afiucv_hs_callback_syn(sk, skb);
2178 break;
2179 case (AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_ACK):
2180 /* connect request confirmed */
2181 err = afiucv_hs_callback_synack(sk, skb);
2182 break;
2183 case (AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN):
2184 /* connect request refused */
2185 err = afiucv_hs_callback_synfin(sk, skb);
2186 break;
2187 case (AF_IUCV_FLAG_FIN):
2188 /* close request */
2189 err = afiucv_hs_callback_fin(sk, skb);
2190 break;
2191 case (AF_IUCV_FLAG_WIN):
2192 err = afiucv_hs_callback_win(sk, skb);
Ursula Braun800c5eb2012-02-08 00:19:47 +00002193 if (skb->len == sizeof(struct af_iucv_trans_hdr)) {
2194 kfree_skb(skb);
2195 break;
2196 }
Ursula Braun82492a32012-03-07 02:06:24 +00002197 /* fall through and receive non-zero length data */
2198 case (AF_IUCV_FLAG_SHT):
2199 /* shutdown request */
2200 /* fall through and receive zero length data */
Ursula Braun3881ac42011-08-08 01:33:54 +00002201 case 0:
2202 /* plain data frame */
Ursula Braunc64d3f82011-12-19 22:56:27 +00002203 memcpy(CB_TRGCLS(skb), &trans_hdr->iucv_hdr.class,
2204 CB_TRGCLS_LEN);
Ursula Braun3881ac42011-08-08 01:33:54 +00002205 err = afiucv_hs_callback_rx(sk, skb);
2206 break;
2207 default:
2208 ;
2209 }
2210
2211 return err;
2212}
2213
2214/**
2215 * afiucv_hs_callback_txnotify() - handle send notifcations from HiperSockets
2216 * transport
2217 **/
2218static void afiucv_hs_callback_txnotify(struct sk_buff *skb,
2219 enum iucv_tx_notify n)
2220{
2221 struct sock *isk = skb->sk;
2222 struct sock *sk = NULL;
2223 struct iucv_sock *iucv = NULL;
2224 struct sk_buff_head *list;
2225 struct sk_buff *list_skb;
Ursula Braun800c5eb2012-02-08 00:19:47 +00002226 struct sk_buff *nskb;
Ursula Braun3881ac42011-08-08 01:33:54 +00002227 unsigned long flags;
2228 struct hlist_node *node;
2229
Ursula Braun800c5eb2012-02-08 00:19:47 +00002230 read_lock_irqsave(&iucv_sk_list.lock, flags);
Ursula Braun3881ac42011-08-08 01:33:54 +00002231 sk_for_each(sk, node, &iucv_sk_list.head)
2232 if (sk == isk) {
2233 iucv = iucv_sk(sk);
2234 break;
2235 }
Ursula Braun800c5eb2012-02-08 00:19:47 +00002236 read_unlock_irqrestore(&iucv_sk_list.lock, flags);
Ursula Braun3881ac42011-08-08 01:33:54 +00002237
Ursula Braun800c5eb2012-02-08 00:19:47 +00002238 if (!iucv || sock_flag(sk, SOCK_ZAPPED))
Ursula Braun3881ac42011-08-08 01:33:54 +00002239 return;
2240
Ursula Braun3881ac42011-08-08 01:33:54 +00002241 list = &iucv->send_skb_q;
Ursula Braun800c5eb2012-02-08 00:19:47 +00002242 spin_lock_irqsave(&list->lock, flags);
Ursula Braun3881ac42011-08-08 01:33:54 +00002243 if (skb_queue_empty(list))
2244 goto out_unlock;
Ursula Braun800c5eb2012-02-08 00:19:47 +00002245 list_skb = list->next;
2246 nskb = list_skb->next;
Ursula Braun3881ac42011-08-08 01:33:54 +00002247 while (list_skb != (struct sk_buff *)list) {
2248 if (skb_shinfo(list_skb) == skb_shinfo(skb)) {
Ursula Braun3881ac42011-08-08 01:33:54 +00002249 switch (n) {
2250 case TX_NOTIFY_OK:
Ursula Braun800c5eb2012-02-08 00:19:47 +00002251 __skb_unlink(list_skb, list);
2252 kfree_skb(list_skb);
Ursula Braun3881ac42011-08-08 01:33:54 +00002253 iucv_sock_wake_msglim(sk);
Ursula Braun3881ac42011-08-08 01:33:54 +00002254 break;
2255 case TX_NOTIFY_PENDING:
2256 atomic_inc(&iucv->pendings);
2257 break;
2258 case TX_NOTIFY_DELAYED_OK:
Ursula Braun800c5eb2012-02-08 00:19:47 +00002259 __skb_unlink(list_skb, list);
Ursula Braun3881ac42011-08-08 01:33:54 +00002260 atomic_dec(&iucv->pendings);
2261 if (atomic_read(&iucv->pendings) <= 0)
2262 iucv_sock_wake_msglim(sk);
Ursula Braun800c5eb2012-02-08 00:19:47 +00002263 kfree_skb(list_skb);
Ursula Braun3881ac42011-08-08 01:33:54 +00002264 break;
2265 case TX_NOTIFY_UNREACHABLE:
2266 case TX_NOTIFY_DELAYED_UNREACHABLE:
2267 case TX_NOTIFY_TPQFULL: /* not yet used */
2268 case TX_NOTIFY_GENERALERROR:
2269 case TX_NOTIFY_DELAYED_GENERALERROR:
Ursula Braun800c5eb2012-02-08 00:19:47 +00002270 __skb_unlink(list_skb, list);
2271 kfree_skb(list_skb);
2272 if (sk->sk_state == IUCV_CONNECTED) {
2273 sk->sk_state = IUCV_DISCONN;
2274 sk->sk_state_change(sk);
2275 }
Ursula Braun3881ac42011-08-08 01:33:54 +00002276 break;
2277 }
2278 break;
2279 }
Ursula Braun800c5eb2012-02-08 00:19:47 +00002280 list_skb = nskb;
2281 nskb = nskb->next;
Ursula Braun3881ac42011-08-08 01:33:54 +00002282 }
Ursula Braun800c5eb2012-02-08 00:19:47 +00002283out_unlock:
Ursula Braun3881ac42011-08-08 01:33:54 +00002284 spin_unlock_irqrestore(&list->lock, flags);
2285
Ursula Braun42bd48e2011-12-19 22:56:28 +00002286 if (sk->sk_state == IUCV_CLOSING) {
2287 if (skb_queue_empty(&iucv_sk(sk)->send_skb_q)) {
2288 sk->sk_state = IUCV_CLOSED;
2289 sk->sk_state_change(sk);
2290 }
2291 }
2292
Ursula Braun3881ac42011-08-08 01:33:54 +00002293}
Ursula Braun9fbd87d2012-03-07 02:06:23 +00002294
2295/*
2296 * afiucv_netdev_event: handle netdev notifier chain events
2297 */
2298static int afiucv_netdev_event(struct notifier_block *this,
2299 unsigned long event, void *ptr)
2300{
2301 struct net_device *event_dev = (struct net_device *)ptr;
2302 struct hlist_node *node;
2303 struct sock *sk;
2304 struct iucv_sock *iucv;
2305
2306 switch (event) {
2307 case NETDEV_REBOOT:
2308 case NETDEV_GOING_DOWN:
2309 sk_for_each(sk, node, &iucv_sk_list.head) {
2310 iucv = iucv_sk(sk);
2311 if ((iucv->hs_dev == event_dev) &&
2312 (sk->sk_state == IUCV_CONNECTED)) {
2313 if (event == NETDEV_GOING_DOWN)
2314 iucv_send_ctrl(sk, AF_IUCV_FLAG_FIN);
2315 sk->sk_state = IUCV_DISCONN;
2316 sk->sk_state_change(sk);
2317 }
2318 }
2319 break;
2320 case NETDEV_DOWN:
2321 case NETDEV_UNREGISTER:
2322 default:
2323 break;
2324 }
2325 return NOTIFY_DONE;
2326}
2327
2328static struct notifier_block afiucv_netdev_notifier = {
2329 .notifier_call = afiucv_netdev_event,
2330};
2331
Alexey Dobriyan5708e862009-09-14 12:23:23 +00002332static const struct proto_ops iucv_sock_ops = {
Jennifer Hunteac37312007-02-08 13:51:54 -08002333 .family = PF_IUCV,
2334 .owner = THIS_MODULE,
2335 .release = iucv_sock_release,
2336 .bind = iucv_sock_bind,
2337 .connect = iucv_sock_connect,
2338 .listen = iucv_sock_listen,
2339 .accept = iucv_sock_accept,
2340 .getname = iucv_sock_getname,
2341 .sendmsg = iucv_sock_sendmsg,
2342 .recvmsg = iucv_sock_recvmsg,
2343 .poll = iucv_sock_poll,
2344 .ioctl = sock_no_ioctl,
2345 .mmap = sock_no_mmap,
2346 .socketpair = sock_no_socketpair,
2347 .shutdown = iucv_sock_shutdown,
Hendrik Brueckner9d5c5d82009-04-21 23:26:22 +00002348 .setsockopt = iucv_sock_setsockopt,
2349 .getsockopt = iucv_sock_getsockopt,
Jennifer Hunteac37312007-02-08 13:51:54 -08002350};
2351
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00002352static const struct net_proto_family iucv_sock_family_ops = {
Jennifer Hunteac37312007-02-08 13:51:54 -08002353 .family = AF_IUCV,
2354 .owner = THIS_MODULE,
2355 .create = iucv_sock_create,
2356};
2357
Ursula Braun3881ac42011-08-08 01:33:54 +00002358static struct packet_type iucv_packet_type = {
2359 .type = cpu_to_be16(ETH_P_AF_IUCV),
2360 .func = afiucv_hs_rcv,
2361};
2362
2363static int afiucv_iucv_init(void)
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00002364{
2365 int err;
2366
2367 err = pr_iucv->iucv_register(&af_iucv_handler, 0);
2368 if (err)
2369 goto out;
2370 /* establish dummy device */
2371 af_iucv_driver.bus = pr_iucv->bus;
2372 err = driver_register(&af_iucv_driver);
2373 if (err)
2374 goto out_iucv;
2375 af_iucv_dev = kzalloc(sizeof(struct device), GFP_KERNEL);
2376 if (!af_iucv_dev) {
2377 err = -ENOMEM;
2378 goto out_driver;
2379 }
2380 dev_set_name(af_iucv_dev, "af_iucv");
2381 af_iucv_dev->bus = pr_iucv->bus;
2382 af_iucv_dev->parent = pr_iucv->root;
2383 af_iucv_dev->release = (void (*)(struct device *))kfree;
2384 af_iucv_dev->driver = &af_iucv_driver;
2385 err = device_register(af_iucv_dev);
2386 if (err)
2387 goto out_driver;
2388 return 0;
2389
2390out_driver:
2391 driver_unregister(&af_iucv_driver);
2392out_iucv:
2393 pr_iucv->iucv_unregister(&af_iucv_handler, 0);
2394out:
2395 return err;
2396}
2397
Heiko Carstensda99f052007-05-04 12:23:27 -07002398static int __init afiucv_init(void)
Jennifer Hunteac37312007-02-08 13:51:54 -08002399{
2400 int err;
2401
Ursula Braun3881ac42011-08-08 01:33:54 +00002402 if (MACHINE_IS_VM) {
2403 cpcmd("QUERY USERID", iucv_userid, sizeof(iucv_userid), &err);
2404 if (unlikely(err)) {
2405 WARN_ON(err);
2406 err = -EPROTONOSUPPORT;
2407 goto out;
2408 }
Jennifer Hunteac37312007-02-08 13:51:54 -08002409
Ursula Braun3881ac42011-08-08 01:33:54 +00002410 pr_iucv = try_then_request_module(symbol_get(iucv_if), "iucv");
2411 if (!pr_iucv) {
2412 printk(KERN_WARNING "iucv_if lookup failed\n");
2413 memset(&iucv_userid, 0, sizeof(iucv_userid));
2414 }
2415 } else {
2416 memset(&iucv_userid, 0, sizeof(iucv_userid));
2417 pr_iucv = NULL;
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00002418 }
2419
Jennifer Hunteac37312007-02-08 13:51:54 -08002420 err = proto_register(&iucv_proto, 0);
2421 if (err)
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00002422 goto out;
Jennifer Hunteac37312007-02-08 13:51:54 -08002423 err = sock_register(&iucv_sock_family_ops);
2424 if (err)
2425 goto out_proto;
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00002426
Ursula Braun3881ac42011-08-08 01:33:54 +00002427 if (pr_iucv) {
2428 err = afiucv_iucv_init();
2429 if (err)
2430 goto out_sock;
Ursula Braun9fbd87d2012-03-07 02:06:23 +00002431 } else
2432 register_netdevice_notifier(&afiucv_netdev_notifier);
Ursula Braun3881ac42011-08-08 01:33:54 +00002433 dev_add_pack(&iucv_packet_type);
Jennifer Hunteac37312007-02-08 13:51:54 -08002434 return 0;
2435
Ursula Braunc23cad92009-06-16 10:30:44 +02002436out_sock:
2437 sock_unregister(PF_IUCV);
Jennifer Hunteac37312007-02-08 13:51:54 -08002438out_proto:
2439 proto_unregister(&iucv_proto);
Jennifer Hunteac37312007-02-08 13:51:54 -08002440out:
Frank Blaschka6fcd61f2011-08-08 01:33:51 +00002441 if (pr_iucv)
2442 symbol_put(iucv_if);
Jennifer Hunteac37312007-02-08 13:51:54 -08002443 return err;
2444}
2445
2446static void __exit afiucv_exit(void)
2447{
Ursula Braun3881ac42011-08-08 01:33:54 +00002448 if (pr_iucv) {
2449 device_unregister(af_iucv_dev);
2450 driver_unregister(&af_iucv_driver);
2451 pr_iucv->iucv_unregister(&af_iucv_handler, 0);
2452 symbol_put(iucv_if);
Ursula Braun9fbd87d2012-03-07 02:06:23 +00002453 } else
2454 unregister_netdevice_notifier(&afiucv_netdev_notifier);
Ursula Braun3881ac42011-08-08 01:33:54 +00002455 dev_remove_pack(&iucv_packet_type);
Jennifer Hunteac37312007-02-08 13:51:54 -08002456 sock_unregister(PF_IUCV);
2457 proto_unregister(&iucv_proto);
Jennifer Hunteac37312007-02-08 13:51:54 -08002458}
2459
2460module_init(afiucv_init);
2461module_exit(afiucv_exit);
2462
2463MODULE_AUTHOR("Jennifer Hunt <jenhunt@us.ibm.com>");
2464MODULE_DESCRIPTION("IUCV Sockets ver " VERSION);
2465MODULE_VERSION(VERSION);
2466MODULE_LICENSE("GPL");
2467MODULE_ALIAS_NETPROTO(PF_IUCV);
Ursula Braun3881ac42011-08-08 01:33:54 +00002468