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