1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * IUCV protocol stack for Linux on zSeries 4 * 5 * Copyright IBM Corp. 2006, 2009 6 * 7 * Author(s): Jennifer Hunt <jenhunt@us.ibm.com> 8 * Hendrik Brueckner <brueckner@linux.vnet.ibm.com> 9 * PM functions: 10 * Ursula Braun <ursula.braun@de.ibm.com> 11 */ 12 13 #define KMSG_COMPONENT "af_iucv" 14 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt 15 16 #include <linux/module.h> 17 #include <linux/netdevice.h> 18 #include <linux/types.h> 19 #include <linux/list.h> 20 #include <linux/errno.h> 21 #include <linux/kernel.h> 22 #include <linux/sched/signal.h> 23 #include <linux/slab.h> 24 #include <linux/skbuff.h> 25 #include <linux/init.h> 26 #include <linux/poll.h> 27 #include <linux/security.h> 28 #include <net/sock.h> 29 #include <asm/ebcdic.h> 30 #include <asm/cpcmd.h> 31 #include <linux/kmod.h> 32 33 #include <net/iucv/af_iucv.h> 34 35 #define VERSION "1.2" 36 37 static char iucv_userid[80]; 38 39 static const struct proto_ops iucv_sock_ops; 40 41 static struct proto iucv_proto = { 42 .name = "AF_IUCV", 43 .owner = THIS_MODULE, 44 .obj_size = sizeof(struct iucv_sock), 45 }; 46 47 static struct iucv_interface *pr_iucv; 48 49 /* special AF_IUCV IPRM messages */ 50 static const u8 iprm_shutdown[8] = 51 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}; 52 53 #define TRGCLS_SIZE sizeof_field(struct iucv_message, class) 54 55 #define __iucv_sock_wait(sk, condition, timeo, ret) \ 56 do { \ 57 DEFINE_WAIT(__wait); \ 58 long __timeo = timeo; \ 59 ret = 0; \ 60 prepare_to_wait(sk_sleep(sk), &__wait, TASK_INTERRUPTIBLE); \ 61 while (!(condition)) { \ 62 if (!__timeo) { \ 63 ret = -EAGAIN; \ 64 break; \ 65 } \ 66 if (signal_pending(current)) { \ 67 ret = sock_intr_errno(__timeo); \ 68 break; \ 69 } \ 70 release_sock(sk); \ 71 __timeo = schedule_timeout(__timeo); \ 72 lock_sock(sk); \ 73 ret = sock_error(sk); \ 74 if (ret) \ 75 break; \ 76 } \ 77 finish_wait(sk_sleep(sk), &__wait); \ 78 } while (0) 79 80 #define iucv_sock_wait(sk, condition, timeo) \ 81 ({ \ 82 int __ret = 0; \ 83 if (!(condition)) \ 84 __iucv_sock_wait(sk, condition, timeo, __ret); \ 85 __ret; \ 86 }) 87 88 static void iucv_sock_kill(struct sock *sk); 89 static void iucv_sock_close(struct sock *sk); 90 static void iucv_sever_path(struct sock *, int); 91 92 static int afiucv_hs_rcv(struct sk_buff *skb, struct net_device *dev, 93 struct packet_type *pt, struct net_device *orig_dev); 94 static int afiucv_hs_send(struct iucv_message *imsg, struct sock *sock, 95 struct sk_buff *skb, u8 flags); 96 static void afiucv_hs_callback_txnotify(struct sk_buff *, enum iucv_tx_notify); 97 98 /* Call Back functions */ 99 static void iucv_callback_rx(struct iucv_path *, struct iucv_message *); 100 static void iucv_callback_txdone(struct iucv_path *, struct iucv_message *); 101 static void iucv_callback_connack(struct iucv_path *, u8 *); 102 static int iucv_callback_connreq(struct iucv_path *, u8 *, u8 *); 103 static void iucv_callback_connrej(struct iucv_path *, u8 *); 104 static void iucv_callback_shutdown(struct iucv_path *, u8 *); 105 106 static struct iucv_sock_list iucv_sk_list = { 107 .lock = __RW_LOCK_UNLOCKED(iucv_sk_list.lock), 108 .autobind_name = ATOMIC_INIT(0) 109 }; 110 111 static struct iucv_handler af_iucv_handler = { 112 .path_pending = iucv_callback_connreq, 113 .path_complete = iucv_callback_connack, 114 .path_severed = iucv_callback_connrej, 115 .message_pending = iucv_callback_rx, 116 .message_complete = iucv_callback_txdone, 117 .path_quiesced = iucv_callback_shutdown, 118 }; 119 120 static inline void high_nmcpy(unsigned char *dst, char *src) 121 { 122 memcpy(dst, src, 8); 123 } 124 125 static inline void low_nmcpy(unsigned char *dst, char *src) 126 { 127 memcpy(&dst[8], src, 8); 128 } 129 130 static int afiucv_pm_prepare(struct device *dev) 131 { 132 #ifdef CONFIG_PM_DEBUG 133 printk(KERN_WARNING "afiucv_pm_prepare\n"); 134 #endif 135 return 0; 136 } 137 138 static void afiucv_pm_complete(struct device *dev) 139 { 140 #ifdef CONFIG_PM_DEBUG 141 printk(KERN_WARNING "afiucv_pm_complete\n"); 142 #endif 143 } 144 145 /** 146 * afiucv_pm_freeze() - Freeze PM callback 147 * @dev: AFIUCV dummy device 148 * 149 * Sever all established IUCV communication pathes 150 */ 151 static int afiucv_pm_freeze(struct device *dev) 152 { 153 struct iucv_sock *iucv; 154 struct sock *sk; 155 156 #ifdef CONFIG_PM_DEBUG 157 printk(KERN_WARNING "afiucv_pm_freeze\n"); 158 #endif 159 read_lock(&iucv_sk_list.lock); 160 sk_for_each(sk, &iucv_sk_list.head) { 161 iucv = iucv_sk(sk); 162 switch (sk->sk_state) { 163 case IUCV_DISCONN: 164 case IUCV_CLOSING: 165 case IUCV_CONNECTED: 166 iucv_sever_path(sk, 0); 167 break; 168 case IUCV_OPEN: 169 case IUCV_BOUND: 170 case IUCV_LISTEN: 171 case IUCV_CLOSED: 172 default: 173 break; 174 } 175 skb_queue_purge(&iucv->send_skb_q); 176 skb_queue_purge(&iucv->backlog_skb_q); 177 } 178 read_unlock(&iucv_sk_list.lock); 179 return 0; 180 } 181 182 /** 183 * afiucv_pm_restore_thaw() - Thaw and restore PM callback 184 * @dev: AFIUCV dummy device 185 * 186 * socket clean up after freeze 187 */ 188 static int afiucv_pm_restore_thaw(struct device *dev) 189 { 190 struct sock *sk; 191 192 #ifdef CONFIG_PM_DEBUG 193 printk(KERN_WARNING "afiucv_pm_restore_thaw\n"); 194 #endif 195 read_lock(&iucv_sk_list.lock); 196 sk_for_each(sk, &iucv_sk_list.head) { 197 switch (sk->sk_state) { 198 case IUCV_CONNECTED: 199 sk->sk_err = EPIPE; 200 sk->sk_state = IUCV_DISCONN; 201 sk->sk_state_change(sk); 202 break; 203 case IUCV_DISCONN: 204 case IUCV_CLOSING: 205 case IUCV_LISTEN: 206 case IUCV_BOUND: 207 case IUCV_OPEN: 208 default: 209 break; 210 } 211 } 212 read_unlock(&iucv_sk_list.lock); 213 return 0; 214 } 215 216 static const struct dev_pm_ops afiucv_pm_ops = { 217 .prepare = afiucv_pm_prepare, 218 .complete = afiucv_pm_complete, 219 .freeze = afiucv_pm_freeze, 220 .thaw = afiucv_pm_restore_thaw, 221 .restore = afiucv_pm_restore_thaw, 222 }; 223 224 static struct device_driver af_iucv_driver = { 225 .owner = THIS_MODULE, 226 .name = "afiucv", 227 .bus = NULL, 228 .pm = &afiucv_pm_ops, 229 }; 230 231 /* dummy device used as trigger for PM functions */ 232 static struct device *af_iucv_dev; 233 234 /** 235 * iucv_msg_length() - Returns the length of an iucv message. 236 * @msg: Pointer to struct iucv_message, MUST NOT be NULL 237 * 238 * The function returns the length of the specified iucv message @msg of data 239 * stored in a buffer and of data stored in the parameter list (PRMDATA). 240 * 241 * For IUCV_IPRMDATA, AF_IUCV uses the following convention to transport socket 242 * data: 243 * PRMDATA[0..6] socket data (max 7 bytes); 244 * PRMDATA[7] socket data length value (len is 0xff - PRMDATA[7]) 245 * 246 * The socket data length is computed by subtracting the socket data length 247 * value from 0xFF. 248 * If the socket data len is greater 7, then PRMDATA can be used for special 249 * notifications (see iucv_sock_shutdown); and further, 250 * if the socket data len is > 7, the function returns 8. 251 * 252 * Use this function to allocate socket buffers to store iucv message data. 253 */ 254 static inline size_t iucv_msg_length(struct iucv_message *msg) 255 { 256 size_t datalen; 257 258 if (msg->flags & IUCV_IPRMDATA) { 259 datalen = 0xff - msg->rmmsg[7]; 260 return (datalen < 8) ? datalen : 8; 261 } 262 return msg->length; 263 } 264 265 /** 266 * iucv_sock_in_state() - check for specific states 267 * @sk: sock structure 268 * @state: first iucv sk state 269 * @state: second iucv sk state 270 * 271 * Returns true if the socket in either in the first or second state. 272 */ 273 static int iucv_sock_in_state(struct sock *sk, int state, int state2) 274 { 275 return (sk->sk_state == state || sk->sk_state == state2); 276 } 277 278 /** 279 * iucv_below_msglim() - function to check if messages can be sent 280 * @sk: sock structure 281 * 282 * Returns true if the send queue length is lower than the message limit. 283 * Always returns true if the socket is not connected (no iucv path for 284 * checking the message limit). 285 */ 286 static inline int iucv_below_msglim(struct sock *sk) 287 { 288 struct iucv_sock *iucv = iucv_sk(sk); 289 290 if (sk->sk_state != IUCV_CONNECTED) 291 return 1; 292 if (iucv->transport == AF_IUCV_TRANS_IUCV) 293 return (skb_queue_len(&iucv->send_skb_q) < iucv->path->msglim); 294 else 295 return ((atomic_read(&iucv->msg_sent) < iucv->msglimit_peer) && 296 (atomic_read(&iucv->pendings) <= 0)); 297 } 298 299 /** 300 * iucv_sock_wake_msglim() - Wake up thread waiting on msg limit 301 */ 302 static void iucv_sock_wake_msglim(struct sock *sk) 303 { 304 struct socket_wq *wq; 305 306 rcu_read_lock(); 307 wq = rcu_dereference(sk->sk_wq); 308 if (skwq_has_sleeper(wq)) 309 wake_up_interruptible_all(&wq->wait); 310 sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT); 311 rcu_read_unlock(); 312 } 313 314 /** 315 * afiucv_hs_send() - send a message through HiperSockets transport 316 */ 317 static int afiucv_hs_send(struct iucv_message *imsg, struct sock *sock, 318 struct sk_buff *skb, u8 flags) 319 { 320 struct iucv_sock *iucv = iucv_sk(sock); 321 struct af_iucv_trans_hdr *phs_hdr; 322 struct sk_buff *nskb; 323 int err, confirm_recv = 0; 324 325 phs_hdr = skb_push(skb, sizeof(*phs_hdr)); 326 memset(phs_hdr, 0, sizeof(*phs_hdr)); 327 skb_reset_network_header(skb); 328 329 phs_hdr->magic = ETH_P_AF_IUCV; 330 phs_hdr->version = 1; 331 phs_hdr->flags = flags; 332 if (flags == AF_IUCV_FLAG_SYN) 333 phs_hdr->window = iucv->msglimit; 334 else if ((flags == AF_IUCV_FLAG_WIN) || !flags) { 335 confirm_recv = atomic_read(&iucv->msg_recv); 336 phs_hdr->window = confirm_recv; 337 if (confirm_recv) 338 phs_hdr->flags = phs_hdr->flags | AF_IUCV_FLAG_WIN; 339 } 340 memcpy(phs_hdr->destUserID, iucv->dst_user_id, 8); 341 memcpy(phs_hdr->destAppName, iucv->dst_name, 8); 342 memcpy(phs_hdr->srcUserID, iucv->src_user_id, 8); 343 memcpy(phs_hdr->srcAppName, iucv->src_name, 8); 344 ASCEBC(phs_hdr->destUserID, sizeof(phs_hdr->destUserID)); 345 ASCEBC(phs_hdr->destAppName, sizeof(phs_hdr->destAppName)); 346 ASCEBC(phs_hdr->srcUserID, sizeof(phs_hdr->srcUserID)); 347 ASCEBC(phs_hdr->srcAppName, sizeof(phs_hdr->srcAppName)); 348 if (imsg) 349 memcpy(&phs_hdr->iucv_hdr, imsg, sizeof(struct iucv_message)); 350 351 skb->dev = iucv->hs_dev; 352 if (!skb->dev) { 353 err = -ENODEV; 354 goto err_free; 355 } 356 357 dev_hard_header(skb, skb->dev, ETH_P_AF_IUCV, NULL, NULL, skb->len); 358 359 if (!(skb->dev->flags & IFF_UP) || !netif_carrier_ok(skb->dev)) { 360 err = -ENETDOWN; 361 goto err_free; 362 } 363 if (skb->len > skb->dev->mtu) { 364 if (sock->sk_type == SOCK_SEQPACKET) { 365 err = -EMSGSIZE; 366 goto err_free; 367 } 368 skb_trim(skb, skb->dev->mtu); 369 } 370 skb->protocol = cpu_to_be16(ETH_P_AF_IUCV); 371 372 __skb_header_release(skb); 373 nskb = skb_clone(skb, GFP_ATOMIC); 374 if (!nskb) { 375 err = -ENOMEM; 376 goto err_free; 377 } 378 379 skb_queue_tail(&iucv->send_skb_q, nskb); 380 err = dev_queue_xmit(skb); 381 if (net_xmit_eval(err)) { 382 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 } 388 return net_xmit_eval(err); 389 390 err_free: 391 kfree_skb(skb); 392 return err; 393 } 394 395 static struct sock *__iucv_get_sock_by_name(char *nm) 396 { 397 struct sock *sk; 398 399 sk_for_each(sk, &iucv_sk_list.head) 400 if (!memcmp(&iucv_sk(sk)->src_name, nm, 8)) 401 return sk; 402 403 return NULL; 404 } 405 406 static void iucv_sock_destruct(struct sock *sk) 407 { 408 skb_queue_purge(&sk->sk_receive_queue); 409 skb_queue_purge(&sk->sk_error_queue); 410 411 sk_mem_reclaim(sk); 412 413 if (!sock_flag(sk, SOCK_DEAD)) { 414 pr_err("Attempt to release alive iucv socket %p\n", sk); 415 return; 416 } 417 418 WARN_ON(atomic_read(&sk->sk_rmem_alloc)); 419 WARN_ON(refcount_read(&sk->sk_wmem_alloc)); 420 WARN_ON(sk->sk_wmem_queued); 421 WARN_ON(sk->sk_forward_alloc); 422 } 423 424 /* Cleanup Listen */ 425 static void iucv_sock_cleanup_listen(struct sock *parent) 426 { 427 struct sock *sk; 428 429 /* Close non-accepted connections */ 430 while ((sk = iucv_accept_dequeue(parent, NULL))) { 431 iucv_sock_close(sk); 432 iucv_sock_kill(sk); 433 } 434 435 parent->sk_state = IUCV_CLOSED; 436 } 437 438 /* Kill socket (only if zapped and orphaned) */ 439 static void iucv_sock_kill(struct sock *sk) 440 { 441 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket) 442 return; 443 444 iucv_sock_unlink(&iucv_sk_list, sk); 445 sock_set_flag(sk, SOCK_DEAD); 446 sock_put(sk); 447 } 448 449 /* Terminate an IUCV path */ 450 static void iucv_sever_path(struct sock *sk, int with_user_data) 451 { 452 unsigned char user_data[16]; 453 struct iucv_sock *iucv = iucv_sk(sk); 454 struct iucv_path *path = iucv->path; 455 456 if (iucv->path) { 457 iucv->path = NULL; 458 if (with_user_data) { 459 low_nmcpy(user_data, iucv->src_name); 460 high_nmcpy(user_data, iucv->dst_name); 461 ASCEBC(user_data, sizeof(user_data)); 462 pr_iucv->path_sever(path, user_data); 463 } else 464 pr_iucv->path_sever(path, NULL); 465 iucv_path_free(path); 466 } 467 } 468 469 /* Send controlling flags through an IUCV socket for HIPER transport */ 470 static int iucv_send_ctrl(struct sock *sk, u8 flags) 471 { 472 struct iucv_sock *iucv = iucv_sk(sk); 473 int err = 0; 474 int blen; 475 struct sk_buff *skb; 476 u8 shutdown = 0; 477 478 blen = sizeof(struct af_iucv_trans_hdr) + 479 LL_RESERVED_SPACE(iucv->hs_dev); 480 if (sk->sk_shutdown & SEND_SHUTDOWN) { 481 /* controlling flags should be sent anyway */ 482 shutdown = sk->sk_shutdown; 483 sk->sk_shutdown &= RCV_SHUTDOWN; 484 } 485 skb = sock_alloc_send_skb(sk, blen, 1, &err); 486 if (skb) { 487 skb_reserve(skb, blen); 488 err = afiucv_hs_send(NULL, sk, skb, flags); 489 } 490 if (shutdown) 491 sk->sk_shutdown = shutdown; 492 return err; 493 } 494 495 /* Close an IUCV socket */ 496 static void iucv_sock_close(struct sock *sk) 497 { 498 struct iucv_sock *iucv = iucv_sk(sk); 499 unsigned long timeo; 500 int err = 0; 501 502 lock_sock(sk); 503 504 switch (sk->sk_state) { 505 case IUCV_LISTEN: 506 iucv_sock_cleanup_listen(sk); 507 break; 508 509 case IUCV_CONNECTED: 510 if (iucv->transport == AF_IUCV_TRANS_HIPER) { 511 err = iucv_send_ctrl(sk, AF_IUCV_FLAG_FIN); 512 sk->sk_state = IUCV_DISCONN; 513 sk->sk_state_change(sk); 514 } 515 /* fall through */ 516 517 case IUCV_DISCONN: 518 sk->sk_state = IUCV_CLOSING; 519 sk->sk_state_change(sk); 520 521 if (!err && !skb_queue_empty(&iucv->send_skb_q)) { 522 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime) 523 timeo = sk->sk_lingertime; 524 else 525 timeo = IUCV_DISCONN_TIMEOUT; 526 iucv_sock_wait(sk, 527 iucv_sock_in_state(sk, IUCV_CLOSED, 0), 528 timeo); 529 } 530 /* fall through */ 531 532 case IUCV_CLOSING: 533 sk->sk_state = IUCV_CLOSED; 534 sk->sk_state_change(sk); 535 536 sk->sk_err = ECONNRESET; 537 sk->sk_state_change(sk); 538 539 skb_queue_purge(&iucv->send_skb_q); 540 skb_queue_purge(&iucv->backlog_skb_q); 541 /* fall through */ 542 543 default: 544 iucv_sever_path(sk, 1); 545 } 546 547 if (iucv->hs_dev) { 548 dev_put(iucv->hs_dev); 549 iucv->hs_dev = NULL; 550 sk->sk_bound_dev_if = 0; 551 } 552 553 /* mark socket for deletion by iucv_sock_kill() */ 554 sock_set_flag(sk, SOCK_ZAPPED); 555 556 release_sock(sk); 557 } 558 559 static void iucv_sock_init(struct sock *sk, struct sock *parent) 560 { 561 if (parent) { 562 sk->sk_type = parent->sk_type; 563 security_sk_clone(parent, sk); 564 } 565 } 566 567 static struct sock *iucv_sock_alloc(struct socket *sock, int proto, gfp_t prio, int kern) 568 { 569 struct sock *sk; 570 struct iucv_sock *iucv; 571 572 sk = sk_alloc(&init_net, PF_IUCV, prio, &iucv_proto, kern); 573 if (!sk) 574 return NULL; 575 iucv = iucv_sk(sk); 576 577 sock_init_data(sock, sk); 578 INIT_LIST_HEAD(&iucv->accept_q); 579 spin_lock_init(&iucv->accept_q_lock); 580 skb_queue_head_init(&iucv->send_skb_q); 581 INIT_LIST_HEAD(&iucv->message_q.list); 582 spin_lock_init(&iucv->message_q.lock); 583 skb_queue_head_init(&iucv->backlog_skb_q); 584 iucv->send_tag = 0; 585 atomic_set(&iucv->pendings, 0); 586 iucv->flags = 0; 587 iucv->msglimit = 0; 588 atomic_set(&iucv->msg_sent, 0); 589 atomic_set(&iucv->msg_recv, 0); 590 iucv->path = NULL; 591 iucv->sk_txnotify = afiucv_hs_callback_txnotify; 592 memset(&iucv->src_user_id , 0, 32); 593 if (pr_iucv) 594 iucv->transport = AF_IUCV_TRANS_IUCV; 595 else 596 iucv->transport = AF_IUCV_TRANS_HIPER; 597 598 sk->sk_destruct = iucv_sock_destruct; 599 sk->sk_sndtimeo = IUCV_CONN_TIMEOUT; 600 601 sock_reset_flag(sk, SOCK_ZAPPED); 602 603 sk->sk_protocol = proto; 604 sk->sk_state = IUCV_OPEN; 605 606 iucv_sock_link(&iucv_sk_list, sk); 607 return sk; 608 } 609 610 /* Create an IUCV socket */ 611 static int iucv_sock_create(struct net *net, struct socket *sock, int protocol, 612 int kern) 613 { 614 struct sock *sk; 615 616 if (protocol && protocol != PF_IUCV) 617 return -EPROTONOSUPPORT; 618 619 sock->state = SS_UNCONNECTED; 620 621 switch (sock->type) { 622 case SOCK_STREAM: 623 sock->ops = &iucv_sock_ops; 624 break; 625 case SOCK_SEQPACKET: 626 /* currently, proto ops can handle both sk types */ 627 sock->ops = &iucv_sock_ops; 628 break; 629 default: 630 return -ESOCKTNOSUPPORT; 631 } 632 633 sk = iucv_sock_alloc(sock, protocol, GFP_KERNEL, kern); 634 if (!sk) 635 return -ENOMEM; 636 637 iucv_sock_init(sk, NULL); 638 639 return 0; 640 } 641 642 void iucv_sock_link(struct iucv_sock_list *l, struct sock *sk) 643 { 644 write_lock_bh(&l->lock); 645 sk_add_node(sk, &l->head); 646 write_unlock_bh(&l->lock); 647 } 648 649 void iucv_sock_unlink(struct iucv_sock_list *l, struct sock *sk) 650 { 651 write_lock_bh(&l->lock); 652 sk_del_node_init(sk); 653 write_unlock_bh(&l->lock); 654 } 655 656 void iucv_accept_enqueue(struct sock *parent, struct sock *sk) 657 { 658 unsigned long flags; 659 struct iucv_sock *par = iucv_sk(parent); 660 661 sock_hold(sk); 662 spin_lock_irqsave(&par->accept_q_lock, flags); 663 list_add_tail(&iucv_sk(sk)->accept_q, &par->accept_q); 664 spin_unlock_irqrestore(&par->accept_q_lock, flags); 665 iucv_sk(sk)->parent = parent; 666 sk_acceptq_added(parent); 667 } 668 669 void iucv_accept_unlink(struct sock *sk) 670 { 671 unsigned long flags; 672 struct iucv_sock *par = iucv_sk(iucv_sk(sk)->parent); 673 674 spin_lock_irqsave(&par->accept_q_lock, flags); 675 list_del_init(&iucv_sk(sk)->accept_q); 676 spin_unlock_irqrestore(&par->accept_q_lock, flags); 677 sk_acceptq_removed(iucv_sk(sk)->parent); 678 iucv_sk(sk)->parent = NULL; 679 sock_put(sk); 680 } 681 682 struct sock *iucv_accept_dequeue(struct sock *parent, struct socket *newsock) 683 { 684 struct iucv_sock *isk, *n; 685 struct sock *sk; 686 687 list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) { 688 sk = (struct sock *) isk; 689 lock_sock(sk); 690 691 if (sk->sk_state == IUCV_CLOSED) { 692 iucv_accept_unlink(sk); 693 release_sock(sk); 694 continue; 695 } 696 697 if (sk->sk_state == IUCV_CONNECTED || 698 sk->sk_state == IUCV_DISCONN || 699 !newsock) { 700 iucv_accept_unlink(sk); 701 if (newsock) 702 sock_graft(sk, newsock); 703 704 release_sock(sk); 705 return sk; 706 } 707 708 release_sock(sk); 709 } 710 return NULL; 711 } 712 713 static void __iucv_auto_name(struct iucv_sock *iucv) 714 { 715 char name[12]; 716 717 sprintf(name, "%08x", atomic_inc_return(&iucv_sk_list.autobind_name)); 718 while (__iucv_get_sock_by_name(name)) { 719 sprintf(name, "%08x", 720 atomic_inc_return(&iucv_sk_list.autobind_name)); 721 } 722 memcpy(iucv->src_name, name, 8); 723 } 724 725 /* Bind an unbound socket */ 726 static int iucv_sock_bind(struct socket *sock, struct sockaddr *addr, 727 int addr_len) 728 { 729 struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr; 730 struct sock *sk = sock->sk; 731 struct iucv_sock *iucv; 732 int err = 0; 733 struct net_device *dev; 734 char uid[9]; 735 736 /* Verify the input sockaddr */ 737 if (addr_len < sizeof(struct sockaddr_iucv) || 738 addr->sa_family != AF_IUCV) 739 return -EINVAL; 740 741 lock_sock(sk); 742 if (sk->sk_state != IUCV_OPEN) { 743 err = -EBADFD; 744 goto done; 745 } 746 747 write_lock_bh(&iucv_sk_list.lock); 748 749 iucv = iucv_sk(sk); 750 if (__iucv_get_sock_by_name(sa->siucv_name)) { 751 err = -EADDRINUSE; 752 goto done_unlock; 753 } 754 if (iucv->path) 755 goto done_unlock; 756 757 /* Bind the socket */ 758 if (pr_iucv) 759 if (!memcmp(sa->siucv_user_id, iucv_userid, 8)) 760 goto vm_bind; /* VM IUCV transport */ 761 762 /* try hiper transport */ 763 memcpy(uid, sa->siucv_user_id, sizeof(uid)); 764 ASCEBC(uid, 8); 765 rcu_read_lock(); 766 for_each_netdev_rcu(&init_net, dev) { 767 if (!memcmp(dev->perm_addr, uid, 8)) { 768 memcpy(iucv->src_user_id, sa->siucv_user_id, 8); 769 /* Check for unitialized siucv_name */ 770 if (strncmp(sa->siucv_name, " ", 8) == 0) 771 __iucv_auto_name(iucv); 772 else 773 memcpy(iucv->src_name, sa->siucv_name, 8); 774 sk->sk_bound_dev_if = dev->ifindex; 775 iucv->hs_dev = dev; 776 dev_hold(dev); 777 sk->sk_state = IUCV_BOUND; 778 iucv->transport = AF_IUCV_TRANS_HIPER; 779 if (!iucv->msglimit) 780 iucv->msglimit = IUCV_HIPER_MSGLIM_DEFAULT; 781 rcu_read_unlock(); 782 goto done_unlock; 783 } 784 } 785 rcu_read_unlock(); 786 vm_bind: 787 if (pr_iucv) { 788 /* use local userid for backward compat */ 789 memcpy(iucv->src_name, sa->siucv_name, 8); 790 memcpy(iucv->src_user_id, iucv_userid, 8); 791 sk->sk_state = IUCV_BOUND; 792 iucv->transport = AF_IUCV_TRANS_IUCV; 793 sk->sk_allocation |= GFP_DMA; 794 if (!iucv->msglimit) 795 iucv->msglimit = IUCV_QUEUELEN_DEFAULT; 796 goto done_unlock; 797 } 798 /* found no dev to bind */ 799 err = -ENODEV; 800 done_unlock: 801 /* Release the socket list lock */ 802 write_unlock_bh(&iucv_sk_list.lock); 803 done: 804 release_sock(sk); 805 return err; 806 } 807 808 /* Automatically bind an unbound socket */ 809 static int iucv_sock_autobind(struct sock *sk) 810 { 811 struct iucv_sock *iucv = iucv_sk(sk); 812 int err = 0; 813 814 if (unlikely(!pr_iucv)) 815 return -EPROTO; 816 817 memcpy(iucv->src_user_id, iucv_userid, 8); 818 iucv->transport = AF_IUCV_TRANS_IUCV; 819 sk->sk_allocation |= GFP_DMA; 820 821 write_lock_bh(&iucv_sk_list.lock); 822 __iucv_auto_name(iucv); 823 write_unlock_bh(&iucv_sk_list.lock); 824 825 if (!iucv->msglimit) 826 iucv->msglimit = IUCV_QUEUELEN_DEFAULT; 827 828 return err; 829 } 830 831 static int afiucv_path_connect(struct socket *sock, struct sockaddr *addr) 832 { 833 struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr; 834 struct sock *sk = sock->sk; 835 struct iucv_sock *iucv = iucv_sk(sk); 836 unsigned char user_data[16]; 837 int err; 838 839 high_nmcpy(user_data, sa->siucv_name); 840 low_nmcpy(user_data, iucv->src_name); 841 ASCEBC(user_data, sizeof(user_data)); 842 843 /* Create path. */ 844 iucv->path = iucv_path_alloc(iucv->msglimit, 845 IUCV_IPRMDATA, GFP_KERNEL); 846 if (!iucv->path) { 847 err = -ENOMEM; 848 goto done; 849 } 850 err = pr_iucv->path_connect(iucv->path, &af_iucv_handler, 851 sa->siucv_user_id, NULL, user_data, 852 sk); 853 if (err) { 854 iucv_path_free(iucv->path); 855 iucv->path = NULL; 856 switch (err) { 857 case 0x0b: /* Target communicator is not logged on */ 858 err = -ENETUNREACH; 859 break; 860 case 0x0d: /* Max connections for this guest exceeded */ 861 case 0x0e: /* Max connections for target guest exceeded */ 862 err = -EAGAIN; 863 break; 864 case 0x0f: /* Missing IUCV authorization */ 865 err = -EACCES; 866 break; 867 default: 868 err = -ECONNREFUSED; 869 break; 870 } 871 } 872 done: 873 return err; 874 } 875 876 /* Connect an unconnected socket */ 877 static int iucv_sock_connect(struct socket *sock, struct sockaddr *addr, 878 int alen, int flags) 879 { 880 struct sockaddr_iucv *sa = (struct sockaddr_iucv *) addr; 881 struct sock *sk = sock->sk; 882 struct iucv_sock *iucv = iucv_sk(sk); 883 int err; 884 885 if (alen < sizeof(struct sockaddr_iucv) || addr->sa_family != AF_IUCV) 886 return -EINVAL; 887 888 if (sk->sk_state != IUCV_OPEN && sk->sk_state != IUCV_BOUND) 889 return -EBADFD; 890 891 if (sk->sk_state == IUCV_OPEN && 892 iucv->transport == AF_IUCV_TRANS_HIPER) 893 return -EBADFD; /* explicit bind required */ 894 895 if (sk->sk_type != SOCK_STREAM && sk->sk_type != SOCK_SEQPACKET) 896 return -EINVAL; 897 898 if (sk->sk_state == IUCV_OPEN) { 899 err = iucv_sock_autobind(sk); 900 if (unlikely(err)) 901 return err; 902 } 903 904 lock_sock(sk); 905 906 /* Set the destination information */ 907 memcpy(iucv->dst_user_id, sa->siucv_user_id, 8); 908 memcpy(iucv->dst_name, sa->siucv_name, 8); 909 910 if (iucv->transport == AF_IUCV_TRANS_HIPER) 911 err = iucv_send_ctrl(sock->sk, AF_IUCV_FLAG_SYN); 912 else 913 err = afiucv_path_connect(sock, addr); 914 if (err) 915 goto done; 916 917 if (sk->sk_state != IUCV_CONNECTED) 918 err = iucv_sock_wait(sk, iucv_sock_in_state(sk, IUCV_CONNECTED, 919 IUCV_DISCONN), 920 sock_sndtimeo(sk, flags & O_NONBLOCK)); 921 922 if (sk->sk_state == IUCV_DISCONN || sk->sk_state == IUCV_CLOSED) 923 err = -ECONNREFUSED; 924 925 if (err && iucv->transport == AF_IUCV_TRANS_IUCV) 926 iucv_sever_path(sk, 0); 927 928 done: 929 release_sock(sk); 930 return err; 931 } 932 933 /* Move a socket into listening state. */ 934 static int iucv_sock_listen(struct socket *sock, int backlog) 935 { 936 struct sock *sk = sock->sk; 937 int err; 938 939 lock_sock(sk); 940 941 err = -EINVAL; 942 if (sk->sk_state != IUCV_BOUND) 943 goto done; 944 945 if (sock->type != SOCK_STREAM && sock->type != SOCK_SEQPACKET) 946 goto done; 947 948 sk->sk_max_ack_backlog = backlog; 949 sk->sk_ack_backlog = 0; 950 sk->sk_state = IUCV_LISTEN; 951 err = 0; 952 953 done: 954 release_sock(sk); 955 return err; 956 } 957 958 /* Accept a pending connection */ 959 static int iucv_sock_accept(struct socket *sock, struct socket *newsock, 960 int flags, bool kern) 961 { 962 DECLARE_WAITQUEUE(wait, current); 963 struct sock *sk = sock->sk, *nsk; 964 long timeo; 965 int err = 0; 966 967 lock_sock_nested(sk, SINGLE_DEPTH_NESTING); 968 969 if (sk->sk_state != IUCV_LISTEN) { 970 err = -EBADFD; 971 goto done; 972 } 973 974 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK); 975 976 /* Wait for an incoming connection */ 977 add_wait_queue_exclusive(sk_sleep(sk), &wait); 978 while (!(nsk = iucv_accept_dequeue(sk, newsock))) { 979 set_current_state(TASK_INTERRUPTIBLE); 980 if (!timeo) { 981 err = -EAGAIN; 982 break; 983 } 984 985 release_sock(sk); 986 timeo = schedule_timeout(timeo); 987 lock_sock_nested(sk, SINGLE_DEPTH_NESTING); 988 989 if (sk->sk_state != IUCV_LISTEN) { 990 err = -EBADFD; 991 break; 992 } 993 994 if (signal_pending(current)) { 995 err = sock_intr_errno(timeo); 996 break; 997 } 998 } 999 1000 set_current_state(TASK_RUNNING); 1001 remove_wait_queue(sk_sleep(sk), &wait); 1002 1003 if (err) 1004 goto done; 1005 1006 newsock->state = SS_CONNECTED; 1007 1008 done: 1009 release_sock(sk); 1010 return err; 1011 } 1012 1013 static int iucv_sock_getname(struct socket *sock, struct sockaddr *addr, 1014 int peer) 1015 { 1016 struct sockaddr_iucv *siucv = (struct sockaddr_iucv *) addr; 1017 struct sock *sk = sock->sk; 1018 struct iucv_sock *iucv = iucv_sk(sk); 1019 1020 addr->sa_family = AF_IUCV; 1021 1022 if (peer) { 1023 memcpy(siucv->siucv_user_id, iucv->dst_user_id, 8); 1024 memcpy(siucv->siucv_name, iucv->dst_name, 8); 1025 } else { 1026 memcpy(siucv->siucv_user_id, iucv->src_user_id, 8); 1027 memcpy(siucv->siucv_name, iucv->src_name, 8); 1028 } 1029 memset(&siucv->siucv_port, 0, sizeof(siucv->siucv_port)); 1030 memset(&siucv->siucv_addr, 0, sizeof(siucv->siucv_addr)); 1031 memset(&siucv->siucv_nodeid, 0, sizeof(siucv->siucv_nodeid)); 1032 1033 return sizeof(struct sockaddr_iucv); 1034 } 1035 1036 /** 1037 * iucv_send_iprm() - Send socket data in parameter list of an iucv message. 1038 * @path: IUCV path 1039 * @msg: Pointer to a struct iucv_message 1040 * @skb: The socket data to send, skb->len MUST BE <= 7 1041 * 1042 * Send the socket data in the parameter list in the iucv message 1043 * (IUCV_IPRMDATA). The socket data is stored at index 0 to 6 in the parameter 1044 * list and the socket data len at index 7 (last byte). 1045 * See also iucv_msg_length(). 1046 * 1047 * Returns the error code from the iucv_message_send() call. 1048 */ 1049 static int iucv_send_iprm(struct iucv_path *path, struct iucv_message *msg, 1050 struct sk_buff *skb) 1051 { 1052 u8 prmdata[8]; 1053 1054 memcpy(prmdata, (void *) skb->data, skb->len); 1055 prmdata[7] = 0xff - (u8) skb->len; 1056 return pr_iucv->message_send(path, msg, IUCV_IPRMDATA, 0, 1057 (void *) prmdata, 8); 1058 } 1059 1060 static int iucv_sock_sendmsg(struct socket *sock, struct msghdr *msg, 1061 size_t len) 1062 { 1063 struct sock *sk = sock->sk; 1064 struct iucv_sock *iucv = iucv_sk(sk); 1065 size_t headroom = 0; 1066 size_t linear; 1067 struct sk_buff *skb; 1068 struct iucv_message txmsg = {0}; 1069 struct cmsghdr *cmsg; 1070 int cmsg_done; 1071 long timeo; 1072 char user_id[9]; 1073 char appl_id[9]; 1074 int err; 1075 int noblock = msg->msg_flags & MSG_DONTWAIT; 1076 1077 err = sock_error(sk); 1078 if (err) 1079 return err; 1080 1081 if (msg->msg_flags & MSG_OOB) 1082 return -EOPNOTSUPP; 1083 1084 /* SOCK_SEQPACKET: we do not support segmented records */ 1085 if (sk->sk_type == SOCK_SEQPACKET && !(msg->msg_flags & MSG_EOR)) 1086 return -EOPNOTSUPP; 1087 1088 lock_sock(sk); 1089 1090 if (sk->sk_shutdown & SEND_SHUTDOWN) { 1091 err = -EPIPE; 1092 goto out; 1093 } 1094 1095 /* Return if the socket is not in connected state */ 1096 if (sk->sk_state != IUCV_CONNECTED) { 1097 err = -ENOTCONN; 1098 goto out; 1099 } 1100 1101 /* initialize defaults */ 1102 cmsg_done = 0; /* check for duplicate headers */ 1103 txmsg.class = 0; 1104 1105 /* iterate over control messages */ 1106 for_each_cmsghdr(cmsg, msg) { 1107 if (!CMSG_OK(msg, cmsg)) { 1108 err = -EINVAL; 1109 goto out; 1110 } 1111 1112 if (cmsg->cmsg_level != SOL_IUCV) 1113 continue; 1114 1115 if (cmsg->cmsg_type & cmsg_done) { 1116 err = -EINVAL; 1117 goto out; 1118 } 1119 cmsg_done |= cmsg->cmsg_type; 1120 1121 switch (cmsg->cmsg_type) { 1122 case SCM_IUCV_TRGCLS: 1123 if (cmsg->cmsg_len != CMSG_LEN(TRGCLS_SIZE)) { 1124 err = -EINVAL; 1125 goto out; 1126 } 1127 1128 /* set iucv message target class */ 1129 memcpy(&txmsg.class, 1130 (void *) CMSG_DATA(cmsg), TRGCLS_SIZE); 1131 1132 break; 1133 1134 default: 1135 err = -EINVAL; 1136 goto out; 1137 } 1138 } 1139 1140 /* allocate one skb for each iucv message: 1141 * this is fine for SOCK_SEQPACKET (unless we want to support 1142 * segmented records using the MSG_EOR flag), but 1143 * for SOCK_STREAM we might want to improve it in future */ 1144 if (iucv->transport == AF_IUCV_TRANS_HIPER) { 1145 headroom = sizeof(struct af_iucv_trans_hdr) + 1146 LL_RESERVED_SPACE(iucv->hs_dev); 1147 linear = len; 1148 } else { 1149 if (len < PAGE_SIZE) { 1150 linear = len; 1151 } else { 1152 /* In nonlinear "classic" iucv skb, 1153 * reserve space for iucv_array 1154 */ 1155 headroom = sizeof(struct iucv_array) * 1156 (MAX_SKB_FRAGS + 1); 1157 linear = PAGE_SIZE - headroom; 1158 } 1159 } 1160 skb = sock_alloc_send_pskb(sk, headroom + linear, len - linear, 1161 noblock, &err, 0); 1162 if (!skb) 1163 goto out; 1164 if (headroom) 1165 skb_reserve(skb, headroom); 1166 skb_put(skb, linear); 1167 skb->len = len; 1168 skb->data_len = len - linear; 1169 err = skb_copy_datagram_from_iter(skb, 0, &msg->msg_iter, len); 1170 if (err) 1171 goto fail; 1172 1173 /* wait if outstanding messages for iucv path has reached */ 1174 timeo = sock_sndtimeo(sk, noblock); 1175 err = iucv_sock_wait(sk, iucv_below_msglim(sk), timeo); 1176 if (err) 1177 goto fail; 1178 1179 /* return -ECONNRESET if the socket is no longer connected */ 1180 if (sk->sk_state != IUCV_CONNECTED) { 1181 err = -ECONNRESET; 1182 goto fail; 1183 } 1184 1185 /* increment and save iucv message tag for msg_completion cbk */ 1186 txmsg.tag = iucv->send_tag++; 1187 IUCV_SKB_CB(skb)->tag = txmsg.tag; 1188 1189 if (iucv->transport == AF_IUCV_TRANS_HIPER) { 1190 atomic_inc(&iucv->msg_sent); 1191 err = afiucv_hs_send(&txmsg, sk, skb, 0); 1192 if (err) { 1193 atomic_dec(&iucv->msg_sent); 1194 goto out; 1195 } 1196 } else { /* Classic VM IUCV transport */ 1197 skb_queue_tail(&iucv->send_skb_q, skb); 1198 1199 if (((iucv->path->flags & IUCV_IPRMDATA) & iucv->flags) && 1200 skb->len <= 7) { 1201 err = iucv_send_iprm(iucv->path, &txmsg, skb); 1202 1203 /* on success: there is no message_complete callback */ 1204 /* for an IPRMDATA msg; remove skb from send queue */ 1205 if (err == 0) { 1206 skb_unlink(skb, &iucv->send_skb_q); 1207 kfree_skb(skb); 1208 } 1209 1210 /* this error should never happen since the */ 1211 /* IUCV_IPRMDATA path flag is set... sever path */ 1212 if (err == 0x15) { 1213 pr_iucv->path_sever(iucv->path, NULL); 1214 skb_unlink(skb, &iucv->send_skb_q); 1215 err = -EPIPE; 1216 goto fail; 1217 } 1218 } else if (skb_is_nonlinear(skb)) { 1219 struct iucv_array *iba = (struct iucv_array *)skb->head; 1220 int i; 1221 1222 /* skip iucv_array lying in the headroom */ 1223 iba[0].address = (u32)(addr_t)skb->data; 1224 iba[0].length = (u32)skb_headlen(skb); 1225 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 1226 skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 1227 1228 iba[i + 1].address = 1229 (u32)(addr_t)skb_frag_address(frag); 1230 iba[i + 1].length = (u32)skb_frag_size(frag); 1231 } 1232 err = pr_iucv->message_send(iucv->path, &txmsg, 1233 IUCV_IPBUFLST, 0, 1234 (void *)iba, skb->len); 1235 } else { /* non-IPRM Linear skb */ 1236 err = pr_iucv->message_send(iucv->path, &txmsg, 1237 0, 0, (void *)skb->data, skb->len); 1238 } 1239 if (err) { 1240 if (err == 3) { 1241 user_id[8] = 0; 1242 memcpy(user_id, iucv->dst_user_id, 8); 1243 appl_id[8] = 0; 1244 memcpy(appl_id, iucv->dst_name, 8); 1245 pr_err( 1246 "Application %s on z/VM guest %s exceeds message limit\n", 1247 appl_id, user_id); 1248 err = -EAGAIN; 1249 } else { 1250 err = -EPIPE; 1251 } 1252 skb_unlink(skb, &iucv->send_skb_q); 1253 goto fail; 1254 } 1255 } 1256 1257 release_sock(sk); 1258 return len; 1259 1260 fail: 1261 kfree_skb(skb); 1262 out: 1263 release_sock(sk); 1264 return err; 1265 } 1266 1267 static struct sk_buff *alloc_iucv_recv_skb(unsigned long len) 1268 { 1269 size_t headroom, linear; 1270 struct sk_buff *skb; 1271 int err; 1272 1273 if (len < PAGE_SIZE) { 1274 headroom = 0; 1275 linear = len; 1276 } else { 1277 headroom = sizeof(struct iucv_array) * (MAX_SKB_FRAGS + 1); 1278 linear = PAGE_SIZE - headroom; 1279 } 1280 skb = alloc_skb_with_frags(headroom + linear, len - linear, 1281 0, &err, GFP_ATOMIC | GFP_DMA); 1282 WARN_ONCE(!skb, 1283 "alloc of recv iucv skb len=%lu failed with errcode=%d\n", 1284 len, err); 1285 if (skb) { 1286 if (headroom) 1287 skb_reserve(skb, headroom); 1288 skb_put(skb, linear); 1289 skb->len = len; 1290 skb->data_len = len - linear; 1291 } 1292 return skb; 1293 } 1294 1295 /* iucv_process_message() - Receive a single outstanding IUCV message 1296 * 1297 * Locking: must be called with message_q.lock held 1298 */ 1299 static void iucv_process_message(struct sock *sk, struct sk_buff *skb, 1300 struct iucv_path *path, 1301 struct iucv_message *msg) 1302 { 1303 int rc; 1304 unsigned int len; 1305 1306 len = iucv_msg_length(msg); 1307 1308 /* store msg target class in the second 4 bytes of skb ctrl buffer */ 1309 /* Note: the first 4 bytes are reserved for msg tag */ 1310 IUCV_SKB_CB(skb)->class = msg->class; 1311 1312 /* check for special IPRM messages (e.g. iucv_sock_shutdown) */ 1313 if ((msg->flags & IUCV_IPRMDATA) && len > 7) { 1314 if (memcmp(msg->rmmsg, iprm_shutdown, 8) == 0) { 1315 skb->data = NULL; 1316 skb->len = 0; 1317 } 1318 } else { 1319 if (skb_is_nonlinear(skb)) { 1320 struct iucv_array *iba = (struct iucv_array *)skb->head; 1321 int i; 1322 1323 iba[0].address = (u32)(addr_t)skb->data; 1324 iba[0].length = (u32)skb_headlen(skb); 1325 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 1326 skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 1327 1328 iba[i + 1].address = 1329 (u32)(addr_t)skb_frag_address(frag); 1330 iba[i + 1].length = (u32)skb_frag_size(frag); 1331 } 1332 rc = pr_iucv->message_receive(path, msg, 1333 IUCV_IPBUFLST, 1334 (void *)iba, len, NULL); 1335 } else { 1336 rc = pr_iucv->message_receive(path, msg, 1337 msg->flags & IUCV_IPRMDATA, 1338 skb->data, len, NULL); 1339 } 1340 if (rc) { 1341 kfree_skb(skb); 1342 return; 1343 } 1344 WARN_ON_ONCE(skb->len != len); 1345 } 1346 1347 IUCV_SKB_CB(skb)->offset = 0; 1348 if (sk_filter(sk, skb)) { 1349 atomic_inc(&sk->sk_drops); /* skb rejected by filter */ 1350 kfree_skb(skb); 1351 return; 1352 } 1353 if (__sock_queue_rcv_skb(sk, skb)) /* handle rcv queue full */ 1354 skb_queue_tail(&iucv_sk(sk)->backlog_skb_q, skb); 1355 } 1356 1357 /* iucv_process_message_q() - Process outstanding IUCV messages 1358 * 1359 * Locking: must be called with message_q.lock held 1360 */ 1361 static void iucv_process_message_q(struct sock *sk) 1362 { 1363 struct iucv_sock *iucv = iucv_sk(sk); 1364 struct sk_buff *skb; 1365 struct sock_msg_q *p, *n; 1366 1367 list_for_each_entry_safe(p, n, &iucv->message_q.list, list) { 1368 skb = alloc_iucv_recv_skb(iucv_msg_length(&p->msg)); 1369 if (!skb) 1370 break; 1371 iucv_process_message(sk, skb, p->path, &p->msg); 1372 list_del(&p->list); 1373 kfree(p); 1374 if (!skb_queue_empty(&iucv->backlog_skb_q)) 1375 break; 1376 } 1377 } 1378 1379 static int iucv_sock_recvmsg(struct socket *sock, struct msghdr *msg, 1380 size_t len, int flags) 1381 { 1382 int noblock = flags & MSG_DONTWAIT; 1383 struct sock *sk = sock->sk; 1384 struct iucv_sock *iucv = iucv_sk(sk); 1385 unsigned int copied, rlen; 1386 struct sk_buff *skb, *rskb, *cskb; 1387 int err = 0; 1388 u32 offset; 1389 1390 if ((sk->sk_state == IUCV_DISCONN) && 1391 skb_queue_empty(&iucv->backlog_skb_q) && 1392 skb_queue_empty(&sk->sk_receive_queue) && 1393 list_empty(&iucv->message_q.list)) 1394 return 0; 1395 1396 if (flags & (MSG_OOB)) 1397 return -EOPNOTSUPP; 1398 1399 /* receive/dequeue next skb: 1400 * the function understands MSG_PEEK and, thus, does not dequeue skb */ 1401 skb = skb_recv_datagram(sk, flags, noblock, &err); 1402 if (!skb) { 1403 if (sk->sk_shutdown & RCV_SHUTDOWN) 1404 return 0; 1405 return err; 1406 } 1407 1408 offset = IUCV_SKB_CB(skb)->offset; 1409 rlen = skb->len - offset; /* real length of skb */ 1410 copied = min_t(unsigned int, rlen, len); 1411 if (!rlen) 1412 sk->sk_shutdown = sk->sk_shutdown | RCV_SHUTDOWN; 1413 1414 cskb = skb; 1415 if (skb_copy_datagram_msg(cskb, offset, msg, copied)) { 1416 if (!(flags & MSG_PEEK)) 1417 skb_queue_head(&sk->sk_receive_queue, skb); 1418 return -EFAULT; 1419 } 1420 1421 /* SOCK_SEQPACKET: set MSG_TRUNC if recv buf size is too small */ 1422 if (sk->sk_type == SOCK_SEQPACKET) { 1423 if (copied < rlen) 1424 msg->msg_flags |= MSG_TRUNC; 1425 /* each iucv message contains a complete record */ 1426 msg->msg_flags |= MSG_EOR; 1427 } 1428 1429 /* create control message to store iucv msg target class: 1430 * get the trgcls from the control buffer of the skb due to 1431 * fragmentation of original iucv message. */ 1432 err = put_cmsg(msg, SOL_IUCV, SCM_IUCV_TRGCLS, 1433 sizeof(IUCV_SKB_CB(skb)->class), 1434 (void *)&IUCV_SKB_CB(skb)->class); 1435 if (err) { 1436 if (!(flags & MSG_PEEK)) 1437 skb_queue_head(&sk->sk_receive_queue, skb); 1438 return err; 1439 } 1440 1441 /* Mark read part of skb as used */ 1442 if (!(flags & MSG_PEEK)) { 1443 1444 /* SOCK_STREAM: re-queue skb if it contains unreceived data */ 1445 if (sk->sk_type == SOCK_STREAM) { 1446 if (copied < rlen) { 1447 IUCV_SKB_CB(skb)->offset = offset + copied; 1448 skb_queue_head(&sk->sk_receive_queue, skb); 1449 goto done; 1450 } 1451 } 1452 1453 kfree_skb(skb); 1454 if (iucv->transport == AF_IUCV_TRANS_HIPER) { 1455 atomic_inc(&iucv->msg_recv); 1456 if (atomic_read(&iucv->msg_recv) > iucv->msglimit) { 1457 WARN_ON(1); 1458 iucv_sock_close(sk); 1459 return -EFAULT; 1460 } 1461 } 1462 1463 /* Queue backlog skbs */ 1464 spin_lock_bh(&iucv->message_q.lock); 1465 rskb = skb_dequeue(&iucv->backlog_skb_q); 1466 while (rskb) { 1467 IUCV_SKB_CB(rskb)->offset = 0; 1468 if (__sock_queue_rcv_skb(sk, rskb)) { 1469 /* handle rcv queue full */ 1470 skb_queue_head(&iucv->backlog_skb_q, 1471 rskb); 1472 break; 1473 } 1474 rskb = skb_dequeue(&iucv->backlog_skb_q); 1475 } 1476 if (skb_queue_empty(&iucv->backlog_skb_q)) { 1477 if (!list_empty(&iucv->message_q.list)) 1478 iucv_process_message_q(sk); 1479 if (atomic_read(&iucv->msg_recv) >= 1480 iucv->msglimit / 2) { 1481 err = iucv_send_ctrl(sk, AF_IUCV_FLAG_WIN); 1482 if (err) { 1483 sk->sk_state = IUCV_DISCONN; 1484 sk->sk_state_change(sk); 1485 } 1486 } 1487 } 1488 spin_unlock_bh(&iucv->message_q.lock); 1489 } 1490 1491 done: 1492 /* SOCK_SEQPACKET: return real length if MSG_TRUNC is set */ 1493 if (sk->sk_type == SOCK_SEQPACKET && (flags & MSG_TRUNC)) 1494 copied = rlen; 1495 1496 return copied; 1497 } 1498 1499 static inline __poll_t iucv_accept_poll(struct sock *parent) 1500 { 1501 struct iucv_sock *isk, *n; 1502 struct sock *sk; 1503 1504 list_for_each_entry_safe(isk, n, &iucv_sk(parent)->accept_q, accept_q) { 1505 sk = (struct sock *) isk; 1506 1507 if (sk->sk_state == IUCV_CONNECTED) 1508 return EPOLLIN | EPOLLRDNORM; 1509 } 1510 1511 return 0; 1512 } 1513 1514 __poll_t iucv_sock_poll(struct file *file, struct socket *sock, 1515 poll_table *wait) 1516 { 1517 struct sock *sk = sock->sk; 1518 __poll_t mask = 0; 1519 1520 sock_poll_wait(file, sock, wait); 1521 1522 if (sk->sk_state == IUCV_LISTEN) 1523 return iucv_accept_poll(sk); 1524 1525 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue)) 1526 mask |= EPOLLERR | 1527 (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? EPOLLPRI : 0); 1528 1529 if (sk->sk_shutdown & RCV_SHUTDOWN) 1530 mask |= EPOLLRDHUP; 1531 1532 if (sk->sk_shutdown == SHUTDOWN_MASK) 1533 mask |= EPOLLHUP; 1534 1535 if (!skb_queue_empty(&sk->sk_receive_queue) || 1536 (sk->sk_shutdown & RCV_SHUTDOWN)) 1537 mask |= EPOLLIN | EPOLLRDNORM; 1538 1539 if (sk->sk_state == IUCV_CLOSED) 1540 mask |= EPOLLHUP; 1541 1542 if (sk->sk_state == IUCV_DISCONN) 1543 mask |= EPOLLIN; 1544 1545 if (sock_writeable(sk) && iucv_below_msglim(sk)) 1546 mask |= EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND; 1547 else 1548 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk); 1549 1550 return mask; 1551 } 1552 1553 static int iucv_sock_shutdown(struct socket *sock, int how) 1554 { 1555 struct sock *sk = sock->sk; 1556 struct iucv_sock *iucv = iucv_sk(sk); 1557 struct iucv_message txmsg; 1558 int err = 0; 1559 1560 how++; 1561 1562 if ((how & ~SHUTDOWN_MASK) || !how) 1563 return -EINVAL; 1564 1565 lock_sock(sk); 1566 switch (sk->sk_state) { 1567 case IUCV_LISTEN: 1568 case IUCV_DISCONN: 1569 case IUCV_CLOSING: 1570 case IUCV_CLOSED: 1571 err = -ENOTCONN; 1572 goto fail; 1573 default: 1574 break; 1575 } 1576 1577 if (how == SEND_SHUTDOWN || how == SHUTDOWN_MASK) { 1578 if (iucv->transport == AF_IUCV_TRANS_IUCV) { 1579 txmsg.class = 0; 1580 txmsg.tag = 0; 1581 err = pr_iucv->message_send(iucv->path, &txmsg, 1582 IUCV_IPRMDATA, 0, (void *) iprm_shutdown, 8); 1583 if (err) { 1584 switch (err) { 1585 case 1: 1586 err = -ENOTCONN; 1587 break; 1588 case 2: 1589 err = -ECONNRESET; 1590 break; 1591 default: 1592 err = -ENOTCONN; 1593 break; 1594 } 1595 } 1596 } else 1597 iucv_send_ctrl(sk, AF_IUCV_FLAG_SHT); 1598 } 1599 1600 sk->sk_shutdown |= how; 1601 if (how == RCV_SHUTDOWN || how == SHUTDOWN_MASK) { 1602 if ((iucv->transport == AF_IUCV_TRANS_IUCV) && 1603 iucv->path) { 1604 err = pr_iucv->path_quiesce(iucv->path, NULL); 1605 if (err) 1606 err = -ENOTCONN; 1607 /* skb_queue_purge(&sk->sk_receive_queue); */ 1608 } 1609 skb_queue_purge(&sk->sk_receive_queue); 1610 } 1611 1612 /* Wake up anyone sleeping in poll */ 1613 sk->sk_state_change(sk); 1614 1615 fail: 1616 release_sock(sk); 1617 return err; 1618 } 1619 1620 static int iucv_sock_release(struct socket *sock) 1621 { 1622 struct sock *sk = sock->sk; 1623 int err = 0; 1624 1625 if (!sk) 1626 return 0; 1627 1628 iucv_sock_close(sk); 1629 1630 sock_orphan(sk); 1631 iucv_sock_kill(sk); 1632 return err; 1633 } 1634 1635 /* getsockopt and setsockopt */ 1636 static int iucv_sock_setsockopt(struct socket *sock, int level, int optname, 1637 char __user *optval, unsigned int optlen) 1638 { 1639 struct sock *sk = sock->sk; 1640 struct iucv_sock *iucv = iucv_sk(sk); 1641 int val; 1642 int rc; 1643 1644 if (level != SOL_IUCV) 1645 return -ENOPROTOOPT; 1646 1647 if (optlen < sizeof(int)) 1648 return -EINVAL; 1649 1650 if (get_user(val, (int __user *) optval)) 1651 return -EFAULT; 1652 1653 rc = 0; 1654 1655 lock_sock(sk); 1656 switch (optname) { 1657 case SO_IPRMDATA_MSG: 1658 if (val) 1659 iucv->flags |= IUCV_IPRMDATA; 1660 else 1661 iucv->flags &= ~IUCV_IPRMDATA; 1662 break; 1663 case SO_MSGLIMIT: 1664 switch (sk->sk_state) { 1665 case IUCV_OPEN: 1666 case IUCV_BOUND: 1667 if (val < 1 || val > (u16)(~0)) 1668 rc = -EINVAL; 1669 else 1670 iucv->msglimit = val; 1671 break; 1672 default: 1673 rc = -EINVAL; 1674 break; 1675 } 1676 break; 1677 default: 1678 rc = -ENOPROTOOPT; 1679 break; 1680 } 1681 release_sock(sk); 1682 1683 return rc; 1684 } 1685 1686 static int iucv_sock_getsockopt(struct socket *sock, int level, int optname, 1687 char __user *optval, int __user *optlen) 1688 { 1689 struct sock *sk = sock->sk; 1690 struct iucv_sock *iucv = iucv_sk(sk); 1691 unsigned int val; 1692 int len; 1693 1694 if (level != SOL_IUCV) 1695 return -ENOPROTOOPT; 1696 1697 if (get_user(len, optlen)) 1698 return -EFAULT; 1699 1700 if (len < 0) 1701 return -EINVAL; 1702 1703 len = min_t(unsigned int, len, sizeof(int)); 1704 1705 switch (optname) { 1706 case SO_IPRMDATA_MSG: 1707 val = (iucv->flags & IUCV_IPRMDATA) ? 1 : 0; 1708 break; 1709 case SO_MSGLIMIT: 1710 lock_sock(sk); 1711 val = (iucv->path != NULL) ? iucv->path->msglim /* connected */ 1712 : iucv->msglimit; /* default */ 1713 release_sock(sk); 1714 break; 1715 case SO_MSGSIZE: 1716 if (sk->sk_state == IUCV_OPEN) 1717 return -EBADFD; 1718 val = (iucv->hs_dev) ? iucv->hs_dev->mtu - 1719 sizeof(struct af_iucv_trans_hdr) - ETH_HLEN : 1720 0x7fffffff; 1721 break; 1722 default: 1723 return -ENOPROTOOPT; 1724 } 1725 1726 if (put_user(len, optlen)) 1727 return -EFAULT; 1728 if (copy_to_user(optval, &val, len)) 1729 return -EFAULT; 1730 1731 return 0; 1732 } 1733 1734 1735 /* Callback wrappers - called from iucv base support */ 1736 static int iucv_callback_connreq(struct iucv_path *path, 1737 u8 ipvmid[8], u8 ipuser[16]) 1738 { 1739 unsigned char user_data[16]; 1740 unsigned char nuser_data[16]; 1741 unsigned char src_name[8]; 1742 struct sock *sk, *nsk; 1743 struct iucv_sock *iucv, *niucv; 1744 int err; 1745 1746 memcpy(src_name, ipuser, 8); 1747 EBCASC(src_name, 8); 1748 /* Find out if this path belongs to af_iucv. */ 1749 read_lock(&iucv_sk_list.lock); 1750 iucv = NULL; 1751 sk = NULL; 1752 sk_for_each(sk, &iucv_sk_list.head) 1753 if (sk->sk_state == IUCV_LISTEN && 1754 !memcmp(&iucv_sk(sk)->src_name, src_name, 8)) { 1755 /* 1756 * Found a listening socket with 1757 * src_name == ipuser[0-7]. 1758 */ 1759 iucv = iucv_sk(sk); 1760 break; 1761 } 1762 read_unlock(&iucv_sk_list.lock); 1763 if (!iucv) 1764 /* No socket found, not one of our paths. */ 1765 return -EINVAL; 1766 1767 bh_lock_sock(sk); 1768 1769 /* Check if parent socket is listening */ 1770 low_nmcpy(user_data, iucv->src_name); 1771 high_nmcpy(user_data, iucv->dst_name); 1772 ASCEBC(user_data, sizeof(user_data)); 1773 if (sk->sk_state != IUCV_LISTEN) { 1774 err = pr_iucv->path_sever(path, user_data); 1775 iucv_path_free(path); 1776 goto fail; 1777 } 1778 1779 /* Check for backlog size */ 1780 if (sk_acceptq_is_full(sk)) { 1781 err = pr_iucv->path_sever(path, user_data); 1782 iucv_path_free(path); 1783 goto fail; 1784 } 1785 1786 /* Create the new socket */ 1787 nsk = iucv_sock_alloc(NULL, sk->sk_type, GFP_ATOMIC, 0); 1788 if (!nsk) { 1789 err = pr_iucv->path_sever(path, user_data); 1790 iucv_path_free(path); 1791 goto fail; 1792 } 1793 1794 niucv = iucv_sk(nsk); 1795 iucv_sock_init(nsk, sk); 1796 niucv->transport = AF_IUCV_TRANS_IUCV; 1797 nsk->sk_allocation |= GFP_DMA; 1798 1799 /* Set the new iucv_sock */ 1800 memcpy(niucv->dst_name, ipuser + 8, 8); 1801 EBCASC(niucv->dst_name, 8); 1802 memcpy(niucv->dst_user_id, ipvmid, 8); 1803 memcpy(niucv->src_name, iucv->src_name, 8); 1804 memcpy(niucv->src_user_id, iucv->src_user_id, 8); 1805 niucv->path = path; 1806 1807 /* Call iucv_accept */ 1808 high_nmcpy(nuser_data, ipuser + 8); 1809 memcpy(nuser_data + 8, niucv->src_name, 8); 1810 ASCEBC(nuser_data + 8, 8); 1811 1812 /* set message limit for path based on msglimit of accepting socket */ 1813 niucv->msglimit = iucv->msglimit; 1814 path->msglim = iucv->msglimit; 1815 err = pr_iucv->path_accept(path, &af_iucv_handler, nuser_data, nsk); 1816 if (err) { 1817 iucv_sever_path(nsk, 1); 1818 iucv_sock_kill(nsk); 1819 goto fail; 1820 } 1821 1822 iucv_accept_enqueue(sk, nsk); 1823 1824 /* Wake up accept */ 1825 nsk->sk_state = IUCV_CONNECTED; 1826 sk->sk_data_ready(sk); 1827 err = 0; 1828 fail: 1829 bh_unlock_sock(sk); 1830 return 0; 1831 } 1832 1833 static void iucv_callback_connack(struct iucv_path *path, u8 ipuser[16]) 1834 { 1835 struct sock *sk = path->private; 1836 1837 sk->sk_state = IUCV_CONNECTED; 1838 sk->sk_state_change(sk); 1839 } 1840 1841 static void iucv_callback_rx(struct iucv_path *path, struct iucv_message *msg) 1842 { 1843 struct sock *sk = path->private; 1844 struct iucv_sock *iucv = iucv_sk(sk); 1845 struct sk_buff *skb; 1846 struct sock_msg_q *save_msg; 1847 int len; 1848 1849 if (sk->sk_shutdown & RCV_SHUTDOWN) { 1850 pr_iucv->message_reject(path, msg); 1851 return; 1852 } 1853 1854 spin_lock(&iucv->message_q.lock); 1855 1856 if (!list_empty(&iucv->message_q.list) || 1857 !skb_queue_empty(&iucv->backlog_skb_q)) 1858 goto save_message; 1859 1860 len = atomic_read(&sk->sk_rmem_alloc); 1861 len += SKB_TRUESIZE(iucv_msg_length(msg)); 1862 if (len > sk->sk_rcvbuf) 1863 goto save_message; 1864 1865 skb = alloc_iucv_recv_skb(iucv_msg_length(msg)); 1866 if (!skb) 1867 goto save_message; 1868 1869 iucv_process_message(sk, skb, path, msg); 1870 goto out_unlock; 1871 1872 save_message: 1873 save_msg = kzalloc(sizeof(struct sock_msg_q), GFP_ATOMIC | GFP_DMA); 1874 if (!save_msg) 1875 goto out_unlock; 1876 save_msg->path = path; 1877 save_msg->msg = *msg; 1878 1879 list_add_tail(&save_msg->list, &iucv->message_q.list); 1880 1881 out_unlock: 1882 spin_unlock(&iucv->message_q.lock); 1883 } 1884 1885 static void iucv_callback_txdone(struct iucv_path *path, 1886 struct iucv_message *msg) 1887 { 1888 struct sock *sk = path->private; 1889 struct sk_buff *this = NULL; 1890 struct sk_buff_head *list = &iucv_sk(sk)->send_skb_q; 1891 struct sk_buff *list_skb; 1892 unsigned long flags; 1893 1894 bh_lock_sock(sk); 1895 1896 spin_lock_irqsave(&list->lock, flags); 1897 skb_queue_walk(list, list_skb) { 1898 if (msg->tag == IUCV_SKB_CB(list_skb)->tag) { 1899 this = list_skb; 1900 break; 1901 } 1902 } 1903 if (this) 1904 __skb_unlink(this, list); 1905 spin_unlock_irqrestore(&list->lock, flags); 1906 1907 if (this) { 1908 kfree_skb(this); 1909 /* wake up any process waiting for sending */ 1910 iucv_sock_wake_msglim(sk); 1911 } 1912 1913 if (sk->sk_state == IUCV_CLOSING) { 1914 if (skb_queue_empty(&iucv_sk(sk)->send_skb_q)) { 1915 sk->sk_state = IUCV_CLOSED; 1916 sk->sk_state_change(sk); 1917 } 1918 } 1919 bh_unlock_sock(sk); 1920 1921 } 1922 1923 static void iucv_callback_connrej(struct iucv_path *path, u8 ipuser[16]) 1924 { 1925 struct sock *sk = path->private; 1926 1927 if (sk->sk_state == IUCV_CLOSED) 1928 return; 1929 1930 bh_lock_sock(sk); 1931 iucv_sever_path(sk, 1); 1932 sk->sk_state = IUCV_DISCONN; 1933 1934 sk->sk_state_change(sk); 1935 bh_unlock_sock(sk); 1936 } 1937 1938 /* called if the other communication side shuts down its RECV direction; 1939 * in turn, the callback sets SEND_SHUTDOWN to disable sending of data. 1940 */ 1941 static void iucv_callback_shutdown(struct iucv_path *path, u8 ipuser[16]) 1942 { 1943 struct sock *sk = path->private; 1944 1945 bh_lock_sock(sk); 1946 if (sk->sk_state != IUCV_CLOSED) { 1947 sk->sk_shutdown |= SEND_SHUTDOWN; 1948 sk->sk_state_change(sk); 1949 } 1950 bh_unlock_sock(sk); 1951 } 1952 1953 /***************** HiperSockets transport callbacks ********************/ 1954 static void afiucv_swap_src_dest(struct sk_buff *skb) 1955 { 1956 struct af_iucv_trans_hdr *trans_hdr = iucv_trans_hdr(skb); 1957 char tmpID[8]; 1958 char tmpName[8]; 1959 1960 ASCEBC(trans_hdr->destUserID, sizeof(trans_hdr->destUserID)); 1961 ASCEBC(trans_hdr->destAppName, sizeof(trans_hdr->destAppName)); 1962 ASCEBC(trans_hdr->srcUserID, sizeof(trans_hdr->srcUserID)); 1963 ASCEBC(trans_hdr->srcAppName, sizeof(trans_hdr->srcAppName)); 1964 memcpy(tmpID, trans_hdr->srcUserID, 8); 1965 memcpy(tmpName, trans_hdr->srcAppName, 8); 1966 memcpy(trans_hdr->srcUserID, trans_hdr->destUserID, 8); 1967 memcpy(trans_hdr->srcAppName, trans_hdr->destAppName, 8); 1968 memcpy(trans_hdr->destUserID, tmpID, 8); 1969 memcpy(trans_hdr->destAppName, tmpName, 8); 1970 skb_push(skb, ETH_HLEN); 1971 memset(skb->data, 0, ETH_HLEN); 1972 } 1973 1974 /** 1975 * afiucv_hs_callback_syn - react on received SYN 1976 **/ 1977 static int afiucv_hs_callback_syn(struct sock *sk, struct sk_buff *skb) 1978 { 1979 struct af_iucv_trans_hdr *trans_hdr = iucv_trans_hdr(skb); 1980 struct sock *nsk; 1981 struct iucv_sock *iucv, *niucv; 1982 int err; 1983 1984 iucv = iucv_sk(sk); 1985 if (!iucv) { 1986 /* no sock - connection refused */ 1987 afiucv_swap_src_dest(skb); 1988 trans_hdr->flags = AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN; 1989 err = dev_queue_xmit(skb); 1990 goto out; 1991 } 1992 1993 nsk = iucv_sock_alloc(NULL, sk->sk_type, GFP_ATOMIC, 0); 1994 bh_lock_sock(sk); 1995 if ((sk->sk_state != IUCV_LISTEN) || 1996 sk_acceptq_is_full(sk) || 1997 !nsk) { 1998 /* error on server socket - connection refused */ 1999 afiucv_swap_src_dest(skb); 2000 trans_hdr->flags = AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN; 2001 err = dev_queue_xmit(skb); 2002 iucv_sock_kill(nsk); 2003 bh_unlock_sock(sk); 2004 goto out; 2005 } 2006 2007 niucv = iucv_sk(nsk); 2008 iucv_sock_init(nsk, sk); 2009 niucv->transport = AF_IUCV_TRANS_HIPER; 2010 niucv->msglimit = iucv->msglimit; 2011 if (!trans_hdr->window) 2012 niucv->msglimit_peer = IUCV_HIPER_MSGLIM_DEFAULT; 2013 else 2014 niucv->msglimit_peer = trans_hdr->window; 2015 memcpy(niucv->dst_name, trans_hdr->srcAppName, 8); 2016 memcpy(niucv->dst_user_id, trans_hdr->srcUserID, 8); 2017 memcpy(niucv->src_name, iucv->src_name, 8); 2018 memcpy(niucv->src_user_id, iucv->src_user_id, 8); 2019 nsk->sk_bound_dev_if = sk->sk_bound_dev_if; 2020 niucv->hs_dev = iucv->hs_dev; 2021 dev_hold(niucv->hs_dev); 2022 afiucv_swap_src_dest(skb); 2023 trans_hdr->flags = AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_ACK; 2024 trans_hdr->window = niucv->msglimit; 2025 /* if receiver acks the xmit connection is established */ 2026 err = dev_queue_xmit(skb); 2027 if (!err) { 2028 iucv_accept_enqueue(sk, nsk); 2029 nsk->sk_state = IUCV_CONNECTED; 2030 sk->sk_data_ready(sk); 2031 } else 2032 iucv_sock_kill(nsk); 2033 bh_unlock_sock(sk); 2034 2035 out: 2036 return NET_RX_SUCCESS; 2037 } 2038 2039 /** 2040 * afiucv_hs_callback_synack() - react on received SYN-ACK 2041 **/ 2042 static int afiucv_hs_callback_synack(struct sock *sk, struct sk_buff *skb) 2043 { 2044 struct iucv_sock *iucv = iucv_sk(sk); 2045 2046 if (!iucv) 2047 goto out; 2048 if (sk->sk_state != IUCV_BOUND) 2049 goto out; 2050 bh_lock_sock(sk); 2051 iucv->msglimit_peer = iucv_trans_hdr(skb)->window; 2052 sk->sk_state = IUCV_CONNECTED; 2053 sk->sk_state_change(sk); 2054 bh_unlock_sock(sk); 2055 out: 2056 kfree_skb(skb); 2057 return NET_RX_SUCCESS; 2058 } 2059 2060 /** 2061 * afiucv_hs_callback_synfin() - react on received SYN_FIN 2062 **/ 2063 static int afiucv_hs_callback_synfin(struct sock *sk, struct sk_buff *skb) 2064 { 2065 struct iucv_sock *iucv = iucv_sk(sk); 2066 2067 if (!iucv) 2068 goto out; 2069 if (sk->sk_state != IUCV_BOUND) 2070 goto out; 2071 bh_lock_sock(sk); 2072 sk->sk_state = IUCV_DISCONN; 2073 sk->sk_state_change(sk); 2074 bh_unlock_sock(sk); 2075 out: 2076 kfree_skb(skb); 2077 return NET_RX_SUCCESS; 2078 } 2079 2080 /** 2081 * afiucv_hs_callback_fin() - react on received FIN 2082 **/ 2083 static int afiucv_hs_callback_fin(struct sock *sk, struct sk_buff *skb) 2084 { 2085 struct iucv_sock *iucv = iucv_sk(sk); 2086 2087 /* other end of connection closed */ 2088 if (!iucv) 2089 goto out; 2090 bh_lock_sock(sk); 2091 if (sk->sk_state == IUCV_CONNECTED) { 2092 sk->sk_state = IUCV_DISCONN; 2093 sk->sk_state_change(sk); 2094 } 2095 bh_unlock_sock(sk); 2096 out: 2097 kfree_skb(skb); 2098 return NET_RX_SUCCESS; 2099 } 2100 2101 /** 2102 * afiucv_hs_callback_win() - react on received WIN 2103 **/ 2104 static int afiucv_hs_callback_win(struct sock *sk, struct sk_buff *skb) 2105 { 2106 struct iucv_sock *iucv = iucv_sk(sk); 2107 2108 if (!iucv) 2109 return NET_RX_SUCCESS; 2110 2111 if (sk->sk_state != IUCV_CONNECTED) 2112 return NET_RX_SUCCESS; 2113 2114 atomic_sub(iucv_trans_hdr(skb)->window, &iucv->msg_sent); 2115 iucv_sock_wake_msglim(sk); 2116 return NET_RX_SUCCESS; 2117 } 2118 2119 /** 2120 * afiucv_hs_callback_rx() - react on received data 2121 **/ 2122 static int afiucv_hs_callback_rx(struct sock *sk, struct sk_buff *skb) 2123 { 2124 struct iucv_sock *iucv = iucv_sk(sk); 2125 2126 if (!iucv) { 2127 kfree_skb(skb); 2128 return NET_RX_SUCCESS; 2129 } 2130 2131 if (sk->sk_state != IUCV_CONNECTED) { 2132 kfree_skb(skb); 2133 return NET_RX_SUCCESS; 2134 } 2135 2136 if (sk->sk_shutdown & RCV_SHUTDOWN) { 2137 kfree_skb(skb); 2138 return NET_RX_SUCCESS; 2139 } 2140 2141 /* write stuff from iucv_msg to skb cb */ 2142 skb_pull(skb, sizeof(struct af_iucv_trans_hdr)); 2143 skb_reset_transport_header(skb); 2144 skb_reset_network_header(skb); 2145 IUCV_SKB_CB(skb)->offset = 0; 2146 if (sk_filter(sk, skb)) { 2147 atomic_inc(&sk->sk_drops); /* skb rejected by filter */ 2148 kfree_skb(skb); 2149 return NET_RX_SUCCESS; 2150 } 2151 2152 spin_lock(&iucv->message_q.lock); 2153 if (skb_queue_empty(&iucv->backlog_skb_q)) { 2154 if (__sock_queue_rcv_skb(sk, skb)) 2155 /* handle rcv queue full */ 2156 skb_queue_tail(&iucv->backlog_skb_q, skb); 2157 } else 2158 skb_queue_tail(&iucv_sk(sk)->backlog_skb_q, skb); 2159 spin_unlock(&iucv->message_q.lock); 2160 return NET_RX_SUCCESS; 2161 } 2162 2163 /** 2164 * afiucv_hs_rcv() - base function for arriving data through HiperSockets 2165 * transport 2166 * called from netif RX softirq 2167 **/ 2168 static int afiucv_hs_rcv(struct sk_buff *skb, struct net_device *dev, 2169 struct packet_type *pt, struct net_device *orig_dev) 2170 { 2171 struct sock *sk; 2172 struct iucv_sock *iucv; 2173 struct af_iucv_trans_hdr *trans_hdr; 2174 int err = NET_RX_SUCCESS; 2175 char nullstring[8]; 2176 2177 if (!pskb_may_pull(skb, sizeof(*trans_hdr))) { 2178 WARN_ONCE(1, "AF_IUCV failed to receive skb, len=%u", skb->len); 2179 kfree_skb(skb); 2180 return NET_RX_SUCCESS; 2181 } 2182 2183 trans_hdr = iucv_trans_hdr(skb); 2184 EBCASC(trans_hdr->destAppName, sizeof(trans_hdr->destAppName)); 2185 EBCASC(trans_hdr->destUserID, sizeof(trans_hdr->destUserID)); 2186 EBCASC(trans_hdr->srcAppName, sizeof(trans_hdr->srcAppName)); 2187 EBCASC(trans_hdr->srcUserID, sizeof(trans_hdr->srcUserID)); 2188 memset(nullstring, 0, sizeof(nullstring)); 2189 iucv = NULL; 2190 sk = NULL; 2191 read_lock(&iucv_sk_list.lock); 2192 sk_for_each(sk, &iucv_sk_list.head) { 2193 if (trans_hdr->flags == AF_IUCV_FLAG_SYN) { 2194 if ((!memcmp(&iucv_sk(sk)->src_name, 2195 trans_hdr->destAppName, 8)) && 2196 (!memcmp(&iucv_sk(sk)->src_user_id, 2197 trans_hdr->destUserID, 8)) && 2198 (!memcmp(&iucv_sk(sk)->dst_name, nullstring, 8)) && 2199 (!memcmp(&iucv_sk(sk)->dst_user_id, 2200 nullstring, 8))) { 2201 iucv = iucv_sk(sk); 2202 break; 2203 } 2204 } else { 2205 if ((!memcmp(&iucv_sk(sk)->src_name, 2206 trans_hdr->destAppName, 8)) && 2207 (!memcmp(&iucv_sk(sk)->src_user_id, 2208 trans_hdr->destUserID, 8)) && 2209 (!memcmp(&iucv_sk(sk)->dst_name, 2210 trans_hdr->srcAppName, 8)) && 2211 (!memcmp(&iucv_sk(sk)->dst_user_id, 2212 trans_hdr->srcUserID, 8))) { 2213 iucv = iucv_sk(sk); 2214 break; 2215 } 2216 } 2217 } 2218 read_unlock(&iucv_sk_list.lock); 2219 if (!iucv) 2220 sk = NULL; 2221 2222 /* no sock 2223 how should we send with no sock 2224 1) send without sock no send rc checking? 2225 2) introduce default sock to handle this cases 2226 2227 SYN -> send SYN|ACK in good case, send SYN|FIN in bad case 2228 data -> send FIN 2229 SYN|ACK, SYN|FIN, FIN -> no action? */ 2230 2231 switch (trans_hdr->flags) { 2232 case AF_IUCV_FLAG_SYN: 2233 /* connect request */ 2234 err = afiucv_hs_callback_syn(sk, skb); 2235 break; 2236 case (AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_ACK): 2237 /* connect request confirmed */ 2238 err = afiucv_hs_callback_synack(sk, skb); 2239 break; 2240 case (AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN): 2241 /* connect request refused */ 2242 err = afiucv_hs_callback_synfin(sk, skb); 2243 break; 2244 case (AF_IUCV_FLAG_FIN): 2245 /* close request */ 2246 err = afiucv_hs_callback_fin(sk, skb); 2247 break; 2248 case (AF_IUCV_FLAG_WIN): 2249 err = afiucv_hs_callback_win(sk, skb); 2250 if (skb->len == sizeof(struct af_iucv_trans_hdr)) { 2251 kfree_skb(skb); 2252 break; 2253 } 2254 /* fall through - and receive non-zero length data */ 2255 case (AF_IUCV_FLAG_SHT): 2256 /* shutdown request */ 2257 /* fall through - and receive zero length data */ 2258 case 0: 2259 /* plain data frame */ 2260 IUCV_SKB_CB(skb)->class = trans_hdr->iucv_hdr.class; 2261 err = afiucv_hs_callback_rx(sk, skb); 2262 break; 2263 default: 2264 kfree_skb(skb); 2265 } 2266 2267 return err; 2268 } 2269 2270 /** 2271 * afiucv_hs_callback_txnotify() - handle send notifcations from HiperSockets 2272 * transport 2273 **/ 2274 static void afiucv_hs_callback_txnotify(struct sk_buff *skb, 2275 enum iucv_tx_notify n) 2276 { 2277 struct sock *isk = skb->sk; 2278 struct sock *sk = NULL; 2279 struct iucv_sock *iucv = NULL; 2280 struct sk_buff_head *list; 2281 struct sk_buff *list_skb; 2282 struct sk_buff *nskb; 2283 unsigned long flags; 2284 2285 read_lock_irqsave(&iucv_sk_list.lock, flags); 2286 sk_for_each(sk, &iucv_sk_list.head) 2287 if (sk == isk) { 2288 iucv = iucv_sk(sk); 2289 break; 2290 } 2291 read_unlock_irqrestore(&iucv_sk_list.lock, flags); 2292 2293 if (!iucv || sock_flag(sk, SOCK_ZAPPED)) 2294 return; 2295 2296 list = &iucv->send_skb_q; 2297 spin_lock_irqsave(&list->lock, flags); 2298 skb_queue_walk_safe(list, list_skb, nskb) { 2299 if (skb_shinfo(list_skb) == skb_shinfo(skb)) { 2300 switch (n) { 2301 case TX_NOTIFY_OK: 2302 __skb_unlink(list_skb, list); 2303 kfree_skb(list_skb); 2304 iucv_sock_wake_msglim(sk); 2305 break; 2306 case TX_NOTIFY_PENDING: 2307 atomic_inc(&iucv->pendings); 2308 break; 2309 case TX_NOTIFY_DELAYED_OK: 2310 __skb_unlink(list_skb, list); 2311 atomic_dec(&iucv->pendings); 2312 if (atomic_read(&iucv->pendings) <= 0) 2313 iucv_sock_wake_msglim(sk); 2314 kfree_skb(list_skb); 2315 break; 2316 case TX_NOTIFY_UNREACHABLE: 2317 case TX_NOTIFY_DELAYED_UNREACHABLE: 2318 case TX_NOTIFY_TPQFULL: /* not yet used */ 2319 case TX_NOTIFY_GENERALERROR: 2320 case TX_NOTIFY_DELAYED_GENERALERROR: 2321 __skb_unlink(list_skb, list); 2322 kfree_skb(list_skb); 2323 if (sk->sk_state == IUCV_CONNECTED) { 2324 sk->sk_state = IUCV_DISCONN; 2325 sk->sk_state_change(sk); 2326 } 2327 break; 2328 } 2329 break; 2330 } 2331 } 2332 spin_unlock_irqrestore(&list->lock, flags); 2333 2334 if (sk->sk_state == IUCV_CLOSING) { 2335 if (skb_queue_empty(&iucv_sk(sk)->send_skb_q)) { 2336 sk->sk_state = IUCV_CLOSED; 2337 sk->sk_state_change(sk); 2338 } 2339 } 2340 2341 } 2342 2343 /* 2344 * afiucv_netdev_event: handle netdev notifier chain events 2345 */ 2346 static int afiucv_netdev_event(struct notifier_block *this, 2347 unsigned long event, void *ptr) 2348 { 2349 struct net_device *event_dev = netdev_notifier_info_to_dev(ptr); 2350 struct sock *sk; 2351 struct iucv_sock *iucv; 2352 2353 switch (event) { 2354 case NETDEV_REBOOT: 2355 case NETDEV_GOING_DOWN: 2356 sk_for_each(sk, &iucv_sk_list.head) { 2357 iucv = iucv_sk(sk); 2358 if ((iucv->hs_dev == event_dev) && 2359 (sk->sk_state == IUCV_CONNECTED)) { 2360 if (event == NETDEV_GOING_DOWN) 2361 iucv_send_ctrl(sk, AF_IUCV_FLAG_FIN); 2362 sk->sk_state = IUCV_DISCONN; 2363 sk->sk_state_change(sk); 2364 } 2365 } 2366 break; 2367 case NETDEV_DOWN: 2368 case NETDEV_UNREGISTER: 2369 default: 2370 break; 2371 } 2372 return NOTIFY_DONE; 2373 } 2374 2375 static struct notifier_block afiucv_netdev_notifier = { 2376 .notifier_call = afiucv_netdev_event, 2377 }; 2378 2379 static const struct proto_ops iucv_sock_ops = { 2380 .family = PF_IUCV, 2381 .owner = THIS_MODULE, 2382 .release = iucv_sock_release, 2383 .bind = iucv_sock_bind, 2384 .connect = iucv_sock_connect, 2385 .listen = iucv_sock_listen, 2386 .accept = iucv_sock_accept, 2387 .getname = iucv_sock_getname, 2388 .sendmsg = iucv_sock_sendmsg, 2389 .recvmsg = iucv_sock_recvmsg, 2390 .poll = iucv_sock_poll, 2391 .ioctl = sock_no_ioctl, 2392 .mmap = sock_no_mmap, 2393 .socketpair = sock_no_socketpair, 2394 .shutdown = iucv_sock_shutdown, 2395 .setsockopt = iucv_sock_setsockopt, 2396 .getsockopt = iucv_sock_getsockopt, 2397 }; 2398 2399 static const struct net_proto_family iucv_sock_family_ops = { 2400 .family = AF_IUCV, 2401 .owner = THIS_MODULE, 2402 .create = iucv_sock_create, 2403 }; 2404 2405 static struct packet_type iucv_packet_type = { 2406 .type = cpu_to_be16(ETH_P_AF_IUCV), 2407 .func = afiucv_hs_rcv, 2408 }; 2409 2410 static int afiucv_iucv_init(void) 2411 { 2412 int err; 2413 2414 err = pr_iucv->iucv_register(&af_iucv_handler, 0); 2415 if (err) 2416 goto out; 2417 /* establish dummy device */ 2418 af_iucv_driver.bus = pr_iucv->bus; 2419 err = driver_register(&af_iucv_driver); 2420 if (err) 2421 goto out_iucv; 2422 af_iucv_dev = kzalloc(sizeof(struct device), GFP_KERNEL); 2423 if (!af_iucv_dev) { 2424 err = -ENOMEM; 2425 goto out_driver; 2426 } 2427 dev_set_name(af_iucv_dev, "af_iucv"); 2428 af_iucv_dev->bus = pr_iucv->bus; 2429 af_iucv_dev->parent = pr_iucv->root; 2430 af_iucv_dev->release = (void (*)(struct device *))kfree; 2431 af_iucv_dev->driver = &af_iucv_driver; 2432 err = device_register(af_iucv_dev); 2433 if (err) 2434 goto out_iucv_dev; 2435 return 0; 2436 2437 out_iucv_dev: 2438 put_device(af_iucv_dev); 2439 out_driver: 2440 driver_unregister(&af_iucv_driver); 2441 out_iucv: 2442 pr_iucv->iucv_unregister(&af_iucv_handler, 0); 2443 out: 2444 return err; 2445 } 2446 2447 static void afiucv_iucv_exit(void) 2448 { 2449 device_unregister(af_iucv_dev); 2450 driver_unregister(&af_iucv_driver); 2451 pr_iucv->iucv_unregister(&af_iucv_handler, 0); 2452 } 2453 2454 static int __init afiucv_init(void) 2455 { 2456 int err; 2457 2458 if (MACHINE_IS_VM) { 2459 cpcmd("QUERY USERID", iucv_userid, sizeof(iucv_userid), &err); 2460 if (unlikely(err)) { 2461 WARN_ON(err); 2462 err = -EPROTONOSUPPORT; 2463 goto out; 2464 } 2465 2466 pr_iucv = try_then_request_module(symbol_get(iucv_if), "iucv"); 2467 if (!pr_iucv) { 2468 printk(KERN_WARNING "iucv_if lookup failed\n"); 2469 memset(&iucv_userid, 0, sizeof(iucv_userid)); 2470 } 2471 } else { 2472 memset(&iucv_userid, 0, sizeof(iucv_userid)); 2473 pr_iucv = NULL; 2474 } 2475 2476 err = proto_register(&iucv_proto, 0); 2477 if (err) 2478 goto out; 2479 err = sock_register(&iucv_sock_family_ops); 2480 if (err) 2481 goto out_proto; 2482 2483 if (pr_iucv) { 2484 err = afiucv_iucv_init(); 2485 if (err) 2486 goto out_sock; 2487 } 2488 2489 err = register_netdevice_notifier(&afiucv_netdev_notifier); 2490 if (err) 2491 goto out_notifier; 2492 2493 dev_add_pack(&iucv_packet_type); 2494 return 0; 2495 2496 out_notifier: 2497 if (pr_iucv) 2498 afiucv_iucv_exit(); 2499 out_sock: 2500 sock_unregister(PF_IUCV); 2501 out_proto: 2502 proto_unregister(&iucv_proto); 2503 out: 2504 if (pr_iucv) 2505 symbol_put(iucv_if); 2506 return err; 2507 } 2508 2509 static void __exit afiucv_exit(void) 2510 { 2511 if (pr_iucv) { 2512 afiucv_iucv_exit(); 2513 symbol_put(iucv_if); 2514 } 2515 2516 unregister_netdevice_notifier(&afiucv_netdev_notifier); 2517 dev_remove_pack(&iucv_packet_type); 2518 sock_unregister(PF_IUCV); 2519 proto_unregister(&iucv_proto); 2520 } 2521 2522 module_init(afiucv_init); 2523 module_exit(afiucv_exit); 2524 2525 MODULE_AUTHOR("Jennifer Hunt <jenhunt@us.ibm.com>"); 2526 MODULE_DESCRIPTION("IUCV Sockets ver " VERSION); 2527 MODULE_VERSION(VERSION); 2528 MODULE_LICENSE("GPL"); 2529 MODULE_ALIAS_NETPROTO(PF_IUCV); 2530