1 /* 2 * Copyright (C) ST-Ericsson AB 2010 3 * Author: Sjur Brendeland 4 * License terms: GNU General Public License (GPL) version 2 5 */ 6 7 #define pr_fmt(fmt) KBUILD_MODNAME ":%s(): " fmt, __func__ 8 9 #include <linux/fs.h> 10 #include <linux/init.h> 11 #include <linux/module.h> 12 #include <linux/sched.h> 13 #include <linux/spinlock.h> 14 #include <linux/mutex.h> 15 #include <linux/list.h> 16 #include <linux/wait.h> 17 #include <linux/poll.h> 18 #include <linux/tcp.h> 19 #include <linux/uaccess.h> 20 #include <linux/debugfs.h> 21 #include <linux/caif/caif_socket.h> 22 #include <linux/pkt_sched.h> 23 #include <net/sock.h> 24 #include <net/tcp_states.h> 25 #include <net/caif/caif_layer.h> 26 #include <net/caif/caif_dev.h> 27 #include <net/caif/cfpkt.h> 28 29 MODULE_LICENSE("GPL"); 30 MODULE_ALIAS_NETPROTO(AF_CAIF); 31 32 /* 33 * CAIF state is re-using the TCP socket states. 34 * caif_states stored in sk_state reflect the state as reported by 35 * the CAIF stack, while sk_socket->state is the state of the socket. 36 */ 37 enum caif_states { 38 CAIF_CONNECTED = TCP_ESTABLISHED, 39 CAIF_CONNECTING = TCP_SYN_SENT, 40 CAIF_DISCONNECTED = TCP_CLOSE 41 }; 42 43 #define TX_FLOW_ON_BIT 1 44 #define RX_FLOW_ON_BIT 2 45 46 struct caifsock { 47 struct sock sk; /* must be first member */ 48 struct cflayer layer; 49 u32 flow_state; 50 struct caif_connect_request conn_req; 51 struct mutex readlock; 52 struct dentry *debugfs_socket_dir; 53 int headroom, tailroom, maxframe; 54 }; 55 56 static int rx_flow_is_on(struct caifsock *cf_sk) 57 { 58 return test_bit(RX_FLOW_ON_BIT, 59 (void *) &cf_sk->flow_state); 60 } 61 62 static int tx_flow_is_on(struct caifsock *cf_sk) 63 { 64 return test_bit(TX_FLOW_ON_BIT, 65 (void *) &cf_sk->flow_state); 66 } 67 68 static void set_rx_flow_off(struct caifsock *cf_sk) 69 { 70 clear_bit(RX_FLOW_ON_BIT, 71 (void *) &cf_sk->flow_state); 72 } 73 74 static void set_rx_flow_on(struct caifsock *cf_sk) 75 { 76 set_bit(RX_FLOW_ON_BIT, 77 (void *) &cf_sk->flow_state); 78 } 79 80 static void set_tx_flow_off(struct caifsock *cf_sk) 81 { 82 clear_bit(TX_FLOW_ON_BIT, 83 (void *) &cf_sk->flow_state); 84 } 85 86 static void set_tx_flow_on(struct caifsock *cf_sk) 87 { 88 set_bit(TX_FLOW_ON_BIT, 89 (void *) &cf_sk->flow_state); 90 } 91 92 static void caif_read_lock(struct sock *sk) 93 { 94 struct caifsock *cf_sk; 95 cf_sk = container_of(sk, struct caifsock, sk); 96 mutex_lock(&cf_sk->readlock); 97 } 98 99 static void caif_read_unlock(struct sock *sk) 100 { 101 struct caifsock *cf_sk; 102 cf_sk = container_of(sk, struct caifsock, sk); 103 mutex_unlock(&cf_sk->readlock); 104 } 105 106 static int sk_rcvbuf_lowwater(struct caifsock *cf_sk) 107 { 108 /* A quarter of full buffer is used a low water mark */ 109 return cf_sk->sk.sk_rcvbuf / 4; 110 } 111 112 static void caif_flow_ctrl(struct sock *sk, int mode) 113 { 114 struct caifsock *cf_sk; 115 cf_sk = container_of(sk, struct caifsock, sk); 116 if (cf_sk->layer.dn && cf_sk->layer.dn->modemcmd) 117 cf_sk->layer.dn->modemcmd(cf_sk->layer.dn, mode); 118 } 119 120 /* 121 * Copied from sock.c:sock_queue_rcv_skb(), but changed so packets are 122 * not dropped, but CAIF is sending flow off instead. 123 */ 124 static int caif_queue_rcv_skb(struct sock *sk, struct sk_buff *skb) 125 { 126 int err; 127 unsigned long flags; 128 struct sk_buff_head *list = &sk->sk_receive_queue; 129 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk); 130 131 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >= 132 (unsigned int)sk->sk_rcvbuf && rx_flow_is_on(cf_sk)) { 133 net_dbg_ratelimited("sending flow OFF (queue len = %d %d)\n", 134 atomic_read(&cf_sk->sk.sk_rmem_alloc), 135 sk_rcvbuf_lowwater(cf_sk)); 136 set_rx_flow_off(cf_sk); 137 caif_flow_ctrl(sk, CAIF_MODEMCMD_FLOW_OFF_REQ); 138 } 139 140 err = sk_filter(sk, skb); 141 if (err) 142 return err; 143 if (!sk_rmem_schedule(sk, skb, skb->truesize) && rx_flow_is_on(cf_sk)) { 144 set_rx_flow_off(cf_sk); 145 net_dbg_ratelimited("sending flow OFF due to rmem_schedule\n"); 146 caif_flow_ctrl(sk, CAIF_MODEMCMD_FLOW_OFF_REQ); 147 } 148 skb->dev = NULL; 149 skb_set_owner_r(skb, sk); 150 /* Cache the SKB length before we tack it onto the receive 151 * queue. Once it is added it no longer belongs to us and 152 * may be freed by other threads of control pulling packets 153 * from the queue. 154 */ 155 spin_lock_irqsave(&list->lock, flags); 156 if (!sock_flag(sk, SOCK_DEAD)) 157 __skb_queue_tail(list, skb); 158 spin_unlock_irqrestore(&list->lock, flags); 159 160 if (!sock_flag(sk, SOCK_DEAD)) 161 sk->sk_data_ready(sk); 162 else 163 kfree_skb(skb); 164 return 0; 165 } 166 167 /* Packet Receive Callback function called from CAIF Stack */ 168 static int caif_sktrecv_cb(struct cflayer *layr, struct cfpkt *pkt) 169 { 170 struct caifsock *cf_sk; 171 struct sk_buff *skb; 172 173 cf_sk = container_of(layr, struct caifsock, layer); 174 skb = cfpkt_tonative(pkt); 175 176 if (unlikely(cf_sk->sk.sk_state != CAIF_CONNECTED)) { 177 kfree_skb(skb); 178 return 0; 179 } 180 caif_queue_rcv_skb(&cf_sk->sk, skb); 181 return 0; 182 } 183 184 static void cfsk_hold(struct cflayer *layr) 185 { 186 struct caifsock *cf_sk = container_of(layr, struct caifsock, layer); 187 sock_hold(&cf_sk->sk); 188 } 189 190 static void cfsk_put(struct cflayer *layr) 191 { 192 struct caifsock *cf_sk = container_of(layr, struct caifsock, layer); 193 sock_put(&cf_sk->sk); 194 } 195 196 /* Packet Control Callback function called from CAIF */ 197 static void caif_ctrl_cb(struct cflayer *layr, 198 enum caif_ctrlcmd flow, 199 int phyid) 200 { 201 struct caifsock *cf_sk = container_of(layr, struct caifsock, layer); 202 switch (flow) { 203 case CAIF_CTRLCMD_FLOW_ON_IND: 204 /* OK from modem to start sending again */ 205 set_tx_flow_on(cf_sk); 206 cf_sk->sk.sk_state_change(&cf_sk->sk); 207 break; 208 209 case CAIF_CTRLCMD_FLOW_OFF_IND: 210 /* Modem asks us to shut up */ 211 set_tx_flow_off(cf_sk); 212 cf_sk->sk.sk_state_change(&cf_sk->sk); 213 break; 214 215 case CAIF_CTRLCMD_INIT_RSP: 216 /* We're now connected */ 217 caif_client_register_refcnt(&cf_sk->layer, 218 cfsk_hold, cfsk_put); 219 cf_sk->sk.sk_state = CAIF_CONNECTED; 220 set_tx_flow_on(cf_sk); 221 cf_sk->sk.sk_shutdown = 0; 222 cf_sk->sk.sk_state_change(&cf_sk->sk); 223 break; 224 225 case CAIF_CTRLCMD_DEINIT_RSP: 226 /* We're now disconnected */ 227 cf_sk->sk.sk_state = CAIF_DISCONNECTED; 228 cf_sk->sk.sk_state_change(&cf_sk->sk); 229 break; 230 231 case CAIF_CTRLCMD_INIT_FAIL_RSP: 232 /* Connect request failed */ 233 cf_sk->sk.sk_err = ECONNREFUSED; 234 cf_sk->sk.sk_state = CAIF_DISCONNECTED; 235 cf_sk->sk.sk_shutdown = SHUTDOWN_MASK; 236 /* 237 * Socket "standards" seems to require POLLOUT to 238 * be set at connect failure. 239 */ 240 set_tx_flow_on(cf_sk); 241 cf_sk->sk.sk_state_change(&cf_sk->sk); 242 break; 243 244 case CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND: 245 /* Modem has closed this connection, or device is down. */ 246 cf_sk->sk.sk_shutdown = SHUTDOWN_MASK; 247 cf_sk->sk.sk_err = ECONNRESET; 248 set_rx_flow_on(cf_sk); 249 cf_sk->sk.sk_error_report(&cf_sk->sk); 250 break; 251 252 default: 253 pr_debug("Unexpected flow command %d\n", flow); 254 } 255 } 256 257 static void caif_check_flow_release(struct sock *sk) 258 { 259 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk); 260 261 if (rx_flow_is_on(cf_sk)) 262 return; 263 264 if (atomic_read(&sk->sk_rmem_alloc) <= sk_rcvbuf_lowwater(cf_sk)) { 265 set_rx_flow_on(cf_sk); 266 caif_flow_ctrl(sk, CAIF_MODEMCMD_FLOW_ON_REQ); 267 } 268 } 269 270 /* 271 * Copied from unix_dgram_recvmsg, but removed credit checks, 272 * changed locking, address handling and added MSG_TRUNC. 273 */ 274 static int caif_seqpkt_recvmsg(struct socket *sock, struct msghdr *m, 275 size_t len, int flags) 276 277 { 278 struct sock *sk = sock->sk; 279 struct sk_buff *skb; 280 int ret; 281 int copylen; 282 283 ret = -EOPNOTSUPP; 284 if (flags & MSG_OOB) 285 goto read_error; 286 287 skb = skb_recv_datagram(sk, flags, 0 , &ret); 288 if (!skb) 289 goto read_error; 290 copylen = skb->len; 291 if (len < copylen) { 292 m->msg_flags |= MSG_TRUNC; 293 copylen = len; 294 } 295 296 ret = skb_copy_datagram_msg(skb, 0, m, copylen); 297 if (ret) 298 goto out_free; 299 300 ret = (flags & MSG_TRUNC) ? skb->len : copylen; 301 out_free: 302 skb_free_datagram(sk, skb); 303 caif_check_flow_release(sk); 304 return ret; 305 306 read_error: 307 return ret; 308 } 309 310 311 /* Copied from unix_stream_wait_data, identical except for lock call. */ 312 static long caif_stream_data_wait(struct sock *sk, long timeo) 313 { 314 DEFINE_WAIT(wait); 315 lock_sock(sk); 316 317 for (;;) { 318 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); 319 320 if (!skb_queue_empty(&sk->sk_receive_queue) || 321 sk->sk_err || 322 sk->sk_state != CAIF_CONNECTED || 323 sock_flag(sk, SOCK_DEAD) || 324 (sk->sk_shutdown & RCV_SHUTDOWN) || 325 signal_pending(current) || 326 !timeo) 327 break; 328 329 set_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags); 330 release_sock(sk); 331 timeo = schedule_timeout(timeo); 332 lock_sock(sk); 333 clear_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags); 334 } 335 336 finish_wait(sk_sleep(sk), &wait); 337 release_sock(sk); 338 return timeo; 339 } 340 341 342 /* 343 * Copied from unix_stream_recvmsg, but removed credit checks, 344 * changed locking calls, changed address handling. 345 */ 346 static int caif_stream_recvmsg(struct socket *sock, struct msghdr *msg, 347 size_t size, int flags) 348 { 349 struct sock *sk = sock->sk; 350 int copied = 0; 351 int target; 352 int err = 0; 353 long timeo; 354 355 err = -EOPNOTSUPP; 356 if (flags&MSG_OOB) 357 goto out; 358 359 /* 360 * Lock the socket to prevent queue disordering 361 * while sleeps in memcpy_tomsg 362 */ 363 err = -EAGAIN; 364 if (sk->sk_state == CAIF_CONNECTING) 365 goto out; 366 367 caif_read_lock(sk); 368 target = sock_rcvlowat(sk, flags&MSG_WAITALL, size); 369 timeo = sock_rcvtimeo(sk, flags&MSG_DONTWAIT); 370 371 do { 372 int chunk; 373 struct sk_buff *skb; 374 375 lock_sock(sk); 376 skb = skb_dequeue(&sk->sk_receive_queue); 377 caif_check_flow_release(sk); 378 379 if (skb == NULL) { 380 if (copied >= target) 381 goto unlock; 382 /* 383 * POSIX 1003.1g mandates this order. 384 */ 385 err = sock_error(sk); 386 if (err) 387 goto unlock; 388 err = -ECONNRESET; 389 if (sk->sk_shutdown & RCV_SHUTDOWN) 390 goto unlock; 391 392 err = -EPIPE; 393 if (sk->sk_state != CAIF_CONNECTED) 394 goto unlock; 395 if (sock_flag(sk, SOCK_DEAD)) 396 goto unlock; 397 398 release_sock(sk); 399 400 err = -EAGAIN; 401 if (!timeo) 402 break; 403 404 caif_read_unlock(sk); 405 406 timeo = caif_stream_data_wait(sk, timeo); 407 408 if (signal_pending(current)) { 409 err = sock_intr_errno(timeo); 410 goto out; 411 } 412 caif_read_lock(sk); 413 continue; 414 unlock: 415 release_sock(sk); 416 break; 417 } 418 release_sock(sk); 419 chunk = min_t(unsigned int, skb->len, size); 420 if (memcpy_to_msg(msg, skb->data, chunk)) { 421 skb_queue_head(&sk->sk_receive_queue, skb); 422 if (copied == 0) 423 copied = -EFAULT; 424 break; 425 } 426 copied += chunk; 427 size -= chunk; 428 429 /* Mark read part of skb as used */ 430 if (!(flags & MSG_PEEK)) { 431 skb_pull(skb, chunk); 432 433 /* put the skb back if we didn't use it up. */ 434 if (skb->len) { 435 skb_queue_head(&sk->sk_receive_queue, skb); 436 break; 437 } 438 kfree_skb(skb); 439 440 } else { 441 /* 442 * It is questionable, see note in unix_dgram_recvmsg. 443 */ 444 /* put message back and return */ 445 skb_queue_head(&sk->sk_receive_queue, skb); 446 break; 447 } 448 } while (size); 449 caif_read_unlock(sk); 450 451 out: 452 return copied ? : err; 453 } 454 455 /* 456 * Copied from sock.c:sock_wait_for_wmem, but change to wait for 457 * CAIF flow-on and sock_writable. 458 */ 459 static long caif_wait_for_flow_on(struct caifsock *cf_sk, 460 int wait_writeable, long timeo, int *err) 461 { 462 struct sock *sk = &cf_sk->sk; 463 DEFINE_WAIT(wait); 464 for (;;) { 465 *err = 0; 466 if (tx_flow_is_on(cf_sk) && 467 (!wait_writeable || sock_writeable(&cf_sk->sk))) 468 break; 469 *err = -ETIMEDOUT; 470 if (!timeo) 471 break; 472 *err = -ERESTARTSYS; 473 if (signal_pending(current)) 474 break; 475 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); 476 *err = -ECONNRESET; 477 if (sk->sk_shutdown & SHUTDOWN_MASK) 478 break; 479 *err = -sk->sk_err; 480 if (sk->sk_err) 481 break; 482 *err = -EPIPE; 483 if (cf_sk->sk.sk_state != CAIF_CONNECTED) 484 break; 485 timeo = schedule_timeout(timeo); 486 } 487 finish_wait(sk_sleep(sk), &wait); 488 return timeo; 489 } 490 491 /* 492 * Transmit a SKB. The device may temporarily request re-transmission 493 * by returning EAGAIN. 494 */ 495 static int transmit_skb(struct sk_buff *skb, struct caifsock *cf_sk, 496 int noblock, long timeo) 497 { 498 struct cfpkt *pkt; 499 500 pkt = cfpkt_fromnative(CAIF_DIR_OUT, skb); 501 memset(skb->cb, 0, sizeof(struct caif_payload_info)); 502 cfpkt_set_prio(pkt, cf_sk->sk.sk_priority); 503 504 if (cf_sk->layer.dn == NULL) { 505 kfree_skb(skb); 506 return -EINVAL; 507 } 508 509 return cf_sk->layer.dn->transmit(cf_sk->layer.dn, pkt); 510 } 511 512 /* Copied from af_unix:unix_dgram_sendmsg, and adapted to CAIF */ 513 static int caif_seqpkt_sendmsg(struct socket *sock, struct msghdr *msg, 514 size_t len) 515 { 516 struct sock *sk = sock->sk; 517 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk); 518 int buffer_size; 519 int ret = 0; 520 struct sk_buff *skb = NULL; 521 int noblock; 522 long timeo; 523 caif_assert(cf_sk); 524 ret = sock_error(sk); 525 if (ret) 526 goto err; 527 528 ret = -EOPNOTSUPP; 529 if (msg->msg_flags&MSG_OOB) 530 goto err; 531 532 ret = -EOPNOTSUPP; 533 if (msg->msg_namelen) 534 goto err; 535 536 ret = -EINVAL; 537 if (unlikely(msg->msg_iter.iov->iov_base == NULL)) 538 goto err; 539 noblock = msg->msg_flags & MSG_DONTWAIT; 540 541 timeo = sock_sndtimeo(sk, noblock); 542 timeo = caif_wait_for_flow_on(container_of(sk, struct caifsock, sk), 543 1, timeo, &ret); 544 545 if (ret) 546 goto err; 547 ret = -EPIPE; 548 if (cf_sk->sk.sk_state != CAIF_CONNECTED || 549 sock_flag(sk, SOCK_DEAD) || 550 (sk->sk_shutdown & RCV_SHUTDOWN)) 551 goto err; 552 553 /* Error if trying to write more than maximum frame size. */ 554 ret = -EMSGSIZE; 555 if (len > cf_sk->maxframe && cf_sk->sk.sk_protocol != CAIFPROTO_RFM) 556 goto err; 557 558 buffer_size = len + cf_sk->headroom + cf_sk->tailroom; 559 560 ret = -ENOMEM; 561 skb = sock_alloc_send_skb(sk, buffer_size, noblock, &ret); 562 563 if (!skb || skb_tailroom(skb) < buffer_size) 564 goto err; 565 566 skb_reserve(skb, cf_sk->headroom); 567 568 ret = memcpy_from_msg(skb_put(skb, len), msg, len); 569 570 if (ret) 571 goto err; 572 ret = transmit_skb(skb, cf_sk, noblock, timeo); 573 if (ret < 0) 574 /* skb is already freed */ 575 return ret; 576 577 return len; 578 err: 579 kfree_skb(skb); 580 return ret; 581 } 582 583 /* 584 * Copied from unix_stream_sendmsg and adapted to CAIF: 585 * Changed removed permission handling and added waiting for flow on 586 * and other minor adaptations. 587 */ 588 static int caif_stream_sendmsg(struct socket *sock, struct msghdr *msg, 589 size_t len) 590 { 591 struct sock *sk = sock->sk; 592 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk); 593 int err, size; 594 struct sk_buff *skb; 595 int sent = 0; 596 long timeo; 597 598 err = -EOPNOTSUPP; 599 if (unlikely(msg->msg_flags&MSG_OOB)) 600 goto out_err; 601 602 if (unlikely(msg->msg_namelen)) 603 goto out_err; 604 605 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT); 606 timeo = caif_wait_for_flow_on(cf_sk, 1, timeo, &err); 607 608 if (unlikely(sk->sk_shutdown & SEND_SHUTDOWN)) 609 goto pipe_err; 610 611 while (sent < len) { 612 613 size = len-sent; 614 615 if (size > cf_sk->maxframe) 616 size = cf_sk->maxframe; 617 618 /* If size is more than half of sndbuf, chop up message */ 619 if (size > ((sk->sk_sndbuf >> 1) - 64)) 620 size = (sk->sk_sndbuf >> 1) - 64; 621 622 if (size > SKB_MAX_ALLOC) 623 size = SKB_MAX_ALLOC; 624 625 skb = sock_alloc_send_skb(sk, 626 size + cf_sk->headroom + 627 cf_sk->tailroom, 628 msg->msg_flags&MSG_DONTWAIT, 629 &err); 630 if (skb == NULL) 631 goto out_err; 632 633 skb_reserve(skb, cf_sk->headroom); 634 /* 635 * If you pass two values to the sock_alloc_send_skb 636 * it tries to grab the large buffer with GFP_NOFS 637 * (which can fail easily), and if it fails grab the 638 * fallback size buffer which is under a page and will 639 * succeed. [Alan] 640 */ 641 size = min_t(int, size, skb_tailroom(skb)); 642 643 err = memcpy_from_msg(skb_put(skb, size), msg, size); 644 if (err) { 645 kfree_skb(skb); 646 goto out_err; 647 } 648 err = transmit_skb(skb, cf_sk, 649 msg->msg_flags&MSG_DONTWAIT, timeo); 650 if (err < 0) 651 /* skb is already freed */ 652 goto pipe_err; 653 654 sent += size; 655 } 656 657 return sent; 658 659 pipe_err: 660 if (sent == 0 && !(msg->msg_flags&MSG_NOSIGNAL)) 661 send_sig(SIGPIPE, current, 0); 662 err = -EPIPE; 663 out_err: 664 return sent ? : err; 665 } 666 667 static int setsockopt(struct socket *sock, 668 int lvl, int opt, char __user *ov, unsigned int ol) 669 { 670 struct sock *sk = sock->sk; 671 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk); 672 int linksel; 673 674 if (cf_sk->sk.sk_socket->state != SS_UNCONNECTED) 675 return -ENOPROTOOPT; 676 677 switch (opt) { 678 case CAIFSO_LINK_SELECT: 679 if (ol < sizeof(int)) 680 return -EINVAL; 681 if (lvl != SOL_CAIF) 682 goto bad_sol; 683 if (copy_from_user(&linksel, ov, sizeof(int))) 684 return -EINVAL; 685 lock_sock(&(cf_sk->sk)); 686 cf_sk->conn_req.link_selector = linksel; 687 release_sock(&cf_sk->sk); 688 return 0; 689 690 case CAIFSO_REQ_PARAM: 691 if (lvl != SOL_CAIF) 692 goto bad_sol; 693 if (cf_sk->sk.sk_protocol != CAIFPROTO_UTIL) 694 return -ENOPROTOOPT; 695 lock_sock(&(cf_sk->sk)); 696 if (ol > sizeof(cf_sk->conn_req.param.data) || 697 copy_from_user(&cf_sk->conn_req.param.data, ov, ol)) { 698 release_sock(&cf_sk->sk); 699 return -EINVAL; 700 } 701 cf_sk->conn_req.param.size = ol; 702 release_sock(&cf_sk->sk); 703 return 0; 704 705 default: 706 return -ENOPROTOOPT; 707 } 708 709 return 0; 710 bad_sol: 711 return -ENOPROTOOPT; 712 713 } 714 715 /* 716 * caif_connect() - Connect a CAIF Socket 717 * Copied and modified af_irda.c:irda_connect(). 718 * 719 * Note : by consulting "errno", the user space caller may learn the cause 720 * of the failure. Most of them are visible in the function, others may come 721 * from subroutines called and are listed here : 722 * o -EAFNOSUPPORT: bad socket family or type. 723 * o -ESOCKTNOSUPPORT: bad socket type or protocol 724 * o -EINVAL: bad socket address, or CAIF link type 725 * o -ECONNREFUSED: remote end refused the connection. 726 * o -EINPROGRESS: connect request sent but timed out (or non-blocking) 727 * o -EISCONN: already connected. 728 * o -ETIMEDOUT: Connection timed out (send timeout) 729 * o -ENODEV: No link layer to send request 730 * o -ECONNRESET: Received Shutdown indication or lost link layer 731 * o -ENOMEM: Out of memory 732 * 733 * State Strategy: 734 * o sk_state: holds the CAIF_* protocol state, it's updated by 735 * caif_ctrl_cb. 736 * o sock->state: holds the SS_* socket state and is updated by connect and 737 * disconnect. 738 */ 739 static int caif_connect(struct socket *sock, struct sockaddr *uaddr, 740 int addr_len, int flags) 741 { 742 struct sock *sk = sock->sk; 743 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk); 744 long timeo; 745 int err; 746 int ifindex, headroom, tailroom; 747 unsigned int mtu; 748 struct net_device *dev; 749 750 lock_sock(sk); 751 752 err = -EAFNOSUPPORT; 753 if (uaddr->sa_family != AF_CAIF) 754 goto out; 755 756 switch (sock->state) { 757 case SS_UNCONNECTED: 758 /* Normal case, a fresh connect */ 759 caif_assert(sk->sk_state == CAIF_DISCONNECTED); 760 break; 761 case SS_CONNECTING: 762 switch (sk->sk_state) { 763 case CAIF_CONNECTED: 764 sock->state = SS_CONNECTED; 765 err = -EISCONN; 766 goto out; 767 case CAIF_DISCONNECTED: 768 /* Reconnect allowed */ 769 break; 770 case CAIF_CONNECTING: 771 err = -EALREADY; 772 if (flags & O_NONBLOCK) 773 goto out; 774 goto wait_connect; 775 } 776 break; 777 case SS_CONNECTED: 778 caif_assert(sk->sk_state == CAIF_CONNECTED || 779 sk->sk_state == CAIF_DISCONNECTED); 780 if (sk->sk_shutdown & SHUTDOWN_MASK) { 781 /* Allow re-connect after SHUTDOWN_IND */ 782 caif_disconnect_client(sock_net(sk), &cf_sk->layer); 783 caif_free_client(&cf_sk->layer); 784 break; 785 } 786 /* No reconnect on a seqpacket socket */ 787 err = -EISCONN; 788 goto out; 789 case SS_DISCONNECTING: 790 case SS_FREE: 791 caif_assert(1); /*Should never happen */ 792 break; 793 } 794 sk->sk_state = CAIF_DISCONNECTED; 795 sock->state = SS_UNCONNECTED; 796 sk_stream_kill_queues(&cf_sk->sk); 797 798 err = -EINVAL; 799 if (addr_len != sizeof(struct sockaddr_caif)) 800 goto out; 801 802 memcpy(&cf_sk->conn_req.sockaddr, uaddr, 803 sizeof(struct sockaddr_caif)); 804 805 /* Move to connecting socket, start sending Connect Requests */ 806 sock->state = SS_CONNECTING; 807 sk->sk_state = CAIF_CONNECTING; 808 809 /* Check priority value comming from socket */ 810 /* if priority value is out of range it will be ajusted */ 811 if (cf_sk->sk.sk_priority > CAIF_PRIO_MAX) 812 cf_sk->conn_req.priority = CAIF_PRIO_MAX; 813 else if (cf_sk->sk.sk_priority < CAIF_PRIO_MIN) 814 cf_sk->conn_req.priority = CAIF_PRIO_MIN; 815 else 816 cf_sk->conn_req.priority = cf_sk->sk.sk_priority; 817 818 /*ifindex = id of the interface.*/ 819 cf_sk->conn_req.ifindex = cf_sk->sk.sk_bound_dev_if; 820 821 cf_sk->layer.receive = caif_sktrecv_cb; 822 823 err = caif_connect_client(sock_net(sk), &cf_sk->conn_req, 824 &cf_sk->layer, &ifindex, &headroom, &tailroom); 825 826 if (err < 0) { 827 cf_sk->sk.sk_socket->state = SS_UNCONNECTED; 828 cf_sk->sk.sk_state = CAIF_DISCONNECTED; 829 goto out; 830 } 831 832 err = -ENODEV; 833 rcu_read_lock(); 834 dev = dev_get_by_index_rcu(sock_net(sk), ifindex); 835 if (!dev) { 836 rcu_read_unlock(); 837 goto out; 838 } 839 cf_sk->headroom = LL_RESERVED_SPACE_EXTRA(dev, headroom); 840 mtu = dev->mtu; 841 rcu_read_unlock(); 842 843 cf_sk->tailroom = tailroom; 844 cf_sk->maxframe = mtu - (headroom + tailroom); 845 if (cf_sk->maxframe < 1) { 846 pr_warn("CAIF Interface MTU too small (%d)\n", dev->mtu); 847 err = -ENODEV; 848 goto out; 849 } 850 851 err = -EINPROGRESS; 852 wait_connect: 853 854 if (sk->sk_state != CAIF_CONNECTED && (flags & O_NONBLOCK)) 855 goto out; 856 857 timeo = sock_sndtimeo(sk, flags & O_NONBLOCK); 858 859 release_sock(sk); 860 err = -ERESTARTSYS; 861 timeo = wait_event_interruptible_timeout(*sk_sleep(sk), 862 sk->sk_state != CAIF_CONNECTING, 863 timeo); 864 lock_sock(sk); 865 if (timeo < 0) 866 goto out; /* -ERESTARTSYS */ 867 868 err = -ETIMEDOUT; 869 if (timeo == 0 && sk->sk_state != CAIF_CONNECTED) 870 goto out; 871 if (sk->sk_state != CAIF_CONNECTED) { 872 sock->state = SS_UNCONNECTED; 873 err = sock_error(sk); 874 if (!err) 875 err = -ECONNREFUSED; 876 goto out; 877 } 878 sock->state = SS_CONNECTED; 879 err = 0; 880 out: 881 release_sock(sk); 882 return err; 883 } 884 885 /* 886 * caif_release() - Disconnect a CAIF Socket 887 * Copied and modified af_irda.c:irda_release(). 888 */ 889 static int caif_release(struct socket *sock) 890 { 891 struct sock *sk = sock->sk; 892 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk); 893 894 if (!sk) 895 return 0; 896 897 set_tx_flow_off(cf_sk); 898 899 /* 900 * Ensure that packets are not queued after this point in time. 901 * caif_queue_rcv_skb checks SOCK_DEAD holding the queue lock, 902 * this ensures no packets when sock is dead. 903 */ 904 spin_lock_bh(&sk->sk_receive_queue.lock); 905 sock_set_flag(sk, SOCK_DEAD); 906 spin_unlock_bh(&sk->sk_receive_queue.lock); 907 sock->sk = NULL; 908 909 WARN_ON(IS_ERR(cf_sk->debugfs_socket_dir)); 910 debugfs_remove_recursive(cf_sk->debugfs_socket_dir); 911 912 lock_sock(&(cf_sk->sk)); 913 sk->sk_state = CAIF_DISCONNECTED; 914 sk->sk_shutdown = SHUTDOWN_MASK; 915 916 caif_disconnect_client(sock_net(sk), &cf_sk->layer); 917 cf_sk->sk.sk_socket->state = SS_DISCONNECTING; 918 wake_up_interruptible_poll(sk_sleep(sk), POLLERR|POLLHUP); 919 920 sock_orphan(sk); 921 sk_stream_kill_queues(&cf_sk->sk); 922 release_sock(sk); 923 sock_put(sk); 924 return 0; 925 } 926 927 /* Copied from af_unix.c:unix_poll(), added CAIF tx_flow handling */ 928 static unsigned int caif_poll(struct file *file, 929 struct socket *sock, poll_table *wait) 930 { 931 struct sock *sk = sock->sk; 932 unsigned int mask; 933 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk); 934 935 sock_poll_wait(file, sk_sleep(sk), wait); 936 mask = 0; 937 938 /* exceptional events? */ 939 if (sk->sk_err) 940 mask |= POLLERR; 941 if (sk->sk_shutdown == SHUTDOWN_MASK) 942 mask |= POLLHUP; 943 if (sk->sk_shutdown & RCV_SHUTDOWN) 944 mask |= POLLRDHUP; 945 946 /* readable? */ 947 if (!skb_queue_empty(&sk->sk_receive_queue) || 948 (sk->sk_shutdown & RCV_SHUTDOWN)) 949 mask |= POLLIN | POLLRDNORM; 950 951 /* 952 * we set writable also when the other side has shut down the 953 * connection. This prevents stuck sockets. 954 */ 955 if (sock_writeable(sk) && tx_flow_is_on(cf_sk)) 956 mask |= POLLOUT | POLLWRNORM | POLLWRBAND; 957 958 return mask; 959 } 960 961 static const struct proto_ops caif_seqpacket_ops = { 962 .family = PF_CAIF, 963 .owner = THIS_MODULE, 964 .release = caif_release, 965 .bind = sock_no_bind, 966 .connect = caif_connect, 967 .socketpair = sock_no_socketpair, 968 .accept = sock_no_accept, 969 .getname = sock_no_getname, 970 .poll = caif_poll, 971 .ioctl = sock_no_ioctl, 972 .listen = sock_no_listen, 973 .shutdown = sock_no_shutdown, 974 .setsockopt = setsockopt, 975 .getsockopt = sock_no_getsockopt, 976 .sendmsg = caif_seqpkt_sendmsg, 977 .recvmsg = caif_seqpkt_recvmsg, 978 .mmap = sock_no_mmap, 979 .sendpage = sock_no_sendpage, 980 }; 981 982 static const struct proto_ops caif_stream_ops = { 983 .family = PF_CAIF, 984 .owner = THIS_MODULE, 985 .release = caif_release, 986 .bind = sock_no_bind, 987 .connect = caif_connect, 988 .socketpair = sock_no_socketpair, 989 .accept = sock_no_accept, 990 .getname = sock_no_getname, 991 .poll = caif_poll, 992 .ioctl = sock_no_ioctl, 993 .listen = sock_no_listen, 994 .shutdown = sock_no_shutdown, 995 .setsockopt = setsockopt, 996 .getsockopt = sock_no_getsockopt, 997 .sendmsg = caif_stream_sendmsg, 998 .recvmsg = caif_stream_recvmsg, 999 .mmap = sock_no_mmap, 1000 .sendpage = sock_no_sendpage, 1001 }; 1002 1003 /* This function is called when a socket is finally destroyed. */ 1004 static void caif_sock_destructor(struct sock *sk) 1005 { 1006 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk); 1007 caif_assert(!atomic_read(&sk->sk_wmem_alloc)); 1008 caif_assert(sk_unhashed(sk)); 1009 caif_assert(!sk->sk_socket); 1010 if (!sock_flag(sk, SOCK_DEAD)) { 1011 pr_debug("Attempt to release alive CAIF socket: %p\n", sk); 1012 return; 1013 } 1014 sk_stream_kill_queues(&cf_sk->sk); 1015 caif_free_client(&cf_sk->layer); 1016 } 1017 1018 static int caif_create(struct net *net, struct socket *sock, int protocol, 1019 int kern) 1020 { 1021 struct sock *sk = NULL; 1022 struct caifsock *cf_sk = NULL; 1023 static struct proto prot = {.name = "PF_CAIF", 1024 .owner = THIS_MODULE, 1025 .obj_size = sizeof(struct caifsock), 1026 }; 1027 1028 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_NET_ADMIN)) 1029 return -EPERM; 1030 /* 1031 * The sock->type specifies the socket type to use. 1032 * The CAIF socket is a packet stream in the sense 1033 * that it is packet based. CAIF trusts the reliability 1034 * of the link, no resending is implemented. 1035 */ 1036 if (sock->type == SOCK_SEQPACKET) 1037 sock->ops = &caif_seqpacket_ops; 1038 else if (sock->type == SOCK_STREAM) 1039 sock->ops = &caif_stream_ops; 1040 else 1041 return -ESOCKTNOSUPPORT; 1042 1043 if (protocol < 0 || protocol >= CAIFPROTO_MAX) 1044 return -EPROTONOSUPPORT; 1045 /* 1046 * Set the socket state to unconnected. The socket state 1047 * is really not used at all in the net/core or socket.c but the 1048 * initialization makes sure that sock->state is not uninitialized. 1049 */ 1050 sk = sk_alloc(net, PF_CAIF, GFP_KERNEL, &prot); 1051 if (!sk) 1052 return -ENOMEM; 1053 1054 cf_sk = container_of(sk, struct caifsock, sk); 1055 1056 /* Store the protocol */ 1057 sk->sk_protocol = (unsigned char) protocol; 1058 1059 /* Initialize default priority for well-known cases */ 1060 switch (protocol) { 1061 case CAIFPROTO_AT: 1062 sk->sk_priority = TC_PRIO_CONTROL; 1063 break; 1064 case CAIFPROTO_RFM: 1065 sk->sk_priority = TC_PRIO_INTERACTIVE_BULK; 1066 break; 1067 default: 1068 sk->sk_priority = TC_PRIO_BESTEFFORT; 1069 } 1070 1071 /* 1072 * Lock in order to try to stop someone from opening the socket 1073 * too early. 1074 */ 1075 lock_sock(&(cf_sk->sk)); 1076 1077 /* Initialize the nozero default sock structure data. */ 1078 sock_init_data(sock, sk); 1079 sk->sk_destruct = caif_sock_destructor; 1080 1081 mutex_init(&cf_sk->readlock); /* single task reading lock */ 1082 cf_sk->layer.ctrlcmd = caif_ctrl_cb; 1083 cf_sk->sk.sk_socket->state = SS_UNCONNECTED; 1084 cf_sk->sk.sk_state = CAIF_DISCONNECTED; 1085 1086 set_tx_flow_off(cf_sk); 1087 set_rx_flow_on(cf_sk); 1088 1089 /* Set default options on configuration */ 1090 cf_sk->conn_req.link_selector = CAIF_LINK_LOW_LATENCY; 1091 cf_sk->conn_req.protocol = protocol; 1092 release_sock(&cf_sk->sk); 1093 return 0; 1094 } 1095 1096 1097 static struct net_proto_family caif_family_ops = { 1098 .family = PF_CAIF, 1099 .create = caif_create, 1100 .owner = THIS_MODULE, 1101 }; 1102 1103 static int __init caif_sktinit_module(void) 1104 { 1105 int err = sock_register(&caif_family_ops); 1106 if (!err) 1107 return err; 1108 return 0; 1109 } 1110 1111 static void __exit caif_sktexit_module(void) 1112 { 1113 sock_unregister(PF_CAIF); 1114 } 1115 module_init(caif_sktinit_module); 1116 module_exit(caif_sktexit_module); 1117