linux/net/core/datagram.c
<<
>>
Prefs
   1/*
   2 *      SUCS NET3:
   3 *
   4 *      Generic datagram handling routines. These are generic for all
   5 *      protocols. Possibly a generic IP version on top of these would
   6 *      make sense. Not tonight however 8-).
   7 *      This is used because UDP, RAW, PACKET, DDP, IPX, AX.25 and
   8 *      NetROM layer all have identical poll code and mostly
   9 *      identical recvmsg() code. So we share it here. The poll was
  10 *      shared before but buried in udp.c so I moved it.
  11 *
  12 *      Authors:        Alan Cox <alan@lxorguk.ukuu.org.uk>. (datagram_poll() from old
  13 *                                                   udp.c code)
  14 *
  15 *      Fixes:
  16 *              Alan Cox        :       NULL return from skb_peek_copy()
  17 *                                      understood
  18 *              Alan Cox        :       Rewrote skb_read_datagram to avoid the
  19 *                                      skb_peek_copy stuff.
  20 *              Alan Cox        :       Added support for SOCK_SEQPACKET.
  21 *                                      IPX can no longer use the SO_TYPE hack
  22 *                                      but AX.25 now works right, and SPX is
  23 *                                      feasible.
  24 *              Alan Cox        :       Fixed write poll of non IP protocol
  25 *                                      crash.
  26 *              Florian  La Roche:      Changed for my new skbuff handling.
  27 *              Darryl Miles    :       Fixed non-blocking SOCK_SEQPACKET.
  28 *              Linus Torvalds  :       BSD semantic fixes.
  29 *              Alan Cox        :       Datagram iovec handling
  30 *              Darryl Miles    :       Fixed non-blocking SOCK_STREAM.
  31 *              Alan Cox        :       POSIXisms
  32 *              Pete Wyckoff    :       Unconnected accept() fix.
  33 *
  34 */
  35
  36#include <linux/module.h>
  37#include <linux/types.h>
  38#include <linux/kernel.h>
  39#include <asm/uaccess.h>
  40#include <asm/system.h>
  41#include <linux/mm.h>
  42#include <linux/interrupt.h>
  43#include <linux/errno.h>
  44#include <linux/sched.h>
  45#include <linux/inet.h>
  46#include <linux/netdevice.h>
  47#include <linux/rtnetlink.h>
  48#include <linux/poll.h>
  49#include <linux/highmem.h>
  50#include <linux/spinlock.h>
  51
  52#include <net/protocol.h>
  53#include <linux/skbuff.h>
  54
  55#include <net/checksum.h>
  56#include <net/sock.h>
  57#include <net/tcp_states.h>
  58
  59/*
  60 *      Is a socket 'connection oriented' ?
  61 */
  62static inline int connection_based(struct sock *sk)
  63{
  64        return sk->sk_type == SOCK_SEQPACKET || sk->sk_type == SOCK_STREAM;
  65}
  66
  67static int receiver_wake_function(wait_queue_t *wait, unsigned mode, int sync,
  68                                  void *key)
  69{
  70        unsigned long bits = (unsigned long)key;
  71
  72        /*
  73         * Avoid a wakeup if event not interesting for us
  74         */
  75        if (bits && !(bits & (POLLIN | POLLERR)))
  76                return 0;
  77        return autoremove_wake_function(wait, mode, sync, key);
  78}
  79/*
  80 * Wait for a packet..
  81 */
  82static int wait_for_packet(struct sock *sk, int *err, long *timeo_p)
  83{
  84        int error;
  85        DEFINE_WAIT_FUNC(wait, receiver_wake_function);
  86
  87        prepare_to_wait_exclusive(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
  88
  89        /* Socket errors? */
  90        error = sock_error(sk);
  91        if (error)
  92                goto out_err;
  93
  94        if (!skb_queue_empty(&sk->sk_receive_queue))
  95                goto out;
  96
  97        /* Socket shut down? */
  98        if (sk->sk_shutdown & RCV_SHUTDOWN)
  99                goto out_noerr;
 100
 101        /* Sequenced packets can come disconnected.
 102         * If so we report the problem
 103         */
 104        error = -ENOTCONN;
 105        if (connection_based(sk) &&
 106            !(sk->sk_state == TCP_ESTABLISHED || sk->sk_state == TCP_LISTEN))
 107                goto out_err;
 108
 109        /* handle signals */
 110        if (signal_pending(current))
 111                goto interrupted;
 112
 113        error = 0;
 114        *timeo_p = schedule_timeout(*timeo_p);
 115out:
 116        finish_wait(sk->sk_sleep, &wait);
 117        return error;
 118interrupted:
 119        error = sock_intr_errno(*timeo_p);
 120out_err:
 121        *err = error;
 122        goto out;
 123out_noerr:
 124        *err = 0;
 125        error = 1;
 126        goto out;
 127}
 128
 129/**
 130 *      __skb_recv_datagram - Receive a datagram skbuff
 131 *      @sk: socket
 132 *      @flags: MSG_ flags
 133 *      @peeked: returns non-zero if this packet has been seen before
 134 *      @err: error code returned
 135 *
 136 *      Get a datagram skbuff, understands the peeking, nonblocking wakeups
 137 *      and possible races. This replaces identical code in packet, raw and
 138 *      udp, as well as the IPX AX.25 and Appletalk. It also finally fixes
 139 *      the long standing peek and read race for datagram sockets. If you
 140 *      alter this routine remember it must be re-entrant.
 141 *
 142 *      This function will lock the socket if a skb is returned, so the caller
 143 *      needs to unlock the socket in that case (usually by calling
 144 *      skb_free_datagram)
 145 *
 146 *      * It does not lock socket since today. This function is
 147 *      * free of race conditions. This measure should/can improve
 148 *      * significantly datagram socket latencies at high loads,
 149 *      * when data copying to user space takes lots of time.
 150 *      * (BTW I've just killed the last cli() in IP/IPv6/core/netlink/packet
 151 *      *  8) Great win.)
 152 *      *                                           --ANK (980729)
 153 *
 154 *      The order of the tests when we find no data waiting are specified
 155 *      quite explicitly by POSIX 1003.1g, don't change them without having
 156 *      the standard around please.
 157 */
 158struct sk_buff *__skb_recv_datagram(struct sock *sk, unsigned flags,
 159                                    int *peeked, int *err)
 160{
 161        struct sk_buff *skb;
 162        long timeo;
 163        /*
 164         * Caller is allowed not to check sk->sk_err before skb_recv_datagram()
 165         */
 166        int error = sock_error(sk);
 167
 168        if (error)
 169                goto no_packet;
 170
 171        timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
 172
 173        do {
 174                /* Again only user level code calls this function, so nothing
 175                 * interrupt level will suddenly eat the receive_queue.
 176                 *
 177                 * Look at current nfs client by the way...
 178                 * However, this function was corrent in any case. 8)
 179                 */
 180                unsigned long cpu_flags;
 181
 182                spin_lock_irqsave(&sk->sk_receive_queue.lock, cpu_flags);
 183                skb = skb_peek(&sk->sk_receive_queue);
 184                if (skb) {
 185                        *peeked = skb->peeked;
 186                        if (flags & MSG_PEEK) {
 187                                skb->peeked = 1;
 188                                atomic_inc(&skb->users);
 189                        } else
 190                                __skb_unlink(skb, &sk->sk_receive_queue);
 191                }
 192                spin_unlock_irqrestore(&sk->sk_receive_queue.lock, cpu_flags);
 193
 194                if (skb)
 195                        return skb;
 196
 197                /* User doesn't want to wait */
 198                error = -EAGAIN;
 199                if (!timeo)
 200                        goto no_packet;
 201
 202        } while (!wait_for_packet(sk, err, &timeo));
 203
 204        return NULL;
 205
 206no_packet:
 207        *err = error;
 208        return NULL;
 209}
 210EXPORT_SYMBOL(__skb_recv_datagram);
 211
 212struct sk_buff *skb_recv_datagram(struct sock *sk, unsigned flags,
 213                                  int noblock, int *err)
 214{
 215        int peeked;
 216
 217        return __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
 218                                   &peeked, err);
 219}
 220
 221void skb_free_datagram(struct sock *sk, struct sk_buff *skb)
 222{
 223        consume_skb(skb);
 224        sk_mem_reclaim_partial(sk);
 225}
 226
 227/**
 228 *      skb_kill_datagram - Free a datagram skbuff forcibly
 229 *      @sk: socket
 230 *      @skb: datagram skbuff
 231 *      @flags: MSG_ flags
 232 *
 233 *      This function frees a datagram skbuff that was received by
 234 *      skb_recv_datagram.  The flags argument must match the one
 235 *      used for skb_recv_datagram.
 236 *
 237 *      If the MSG_PEEK flag is set, and the packet is still on the
 238 *      receive queue of the socket, it will be taken off the queue
 239 *      before it is freed.
 240 *
 241 *      This function currently only disables BH when acquiring the
 242 *      sk_receive_queue lock.  Therefore it must not be used in a
 243 *      context where that lock is acquired in an IRQ context.
 244 *
 245 *      It returns 0 if the packet was removed by us.
 246 */
 247
 248int skb_kill_datagram(struct sock *sk, struct sk_buff *skb, unsigned int flags)
 249{
 250        int err = 0;
 251
 252        if (flags & MSG_PEEK) {
 253                err = -ENOENT;
 254                spin_lock_bh(&sk->sk_receive_queue.lock);
 255                if (skb == skb_peek(&sk->sk_receive_queue)) {
 256                        __skb_unlink(skb, &sk->sk_receive_queue);
 257                        atomic_dec(&skb->users);
 258                        err = 0;
 259                }
 260                spin_unlock_bh(&sk->sk_receive_queue.lock);
 261        }
 262
 263        skb_free_datagram(sk, skb);
 264        return err;
 265}
 266
 267EXPORT_SYMBOL(skb_kill_datagram);
 268
 269/**
 270 *      skb_copy_datagram_iovec - Copy a datagram to an iovec.
 271 *      @skb: buffer to copy
 272 *      @offset: offset in the buffer to start copying from
 273 *      @to: io vector to copy to
 274 *      @len: amount of data to copy from buffer to iovec
 275 *
 276 *      Note: the iovec is modified during the copy.
 277 */
 278int skb_copy_datagram_iovec(const struct sk_buff *skb, int offset,
 279                            struct iovec *to, int len)
 280{
 281        int start = skb_headlen(skb);
 282        int i, copy = start - offset;
 283
 284        /* Copy header. */
 285        if (copy > 0) {
 286                if (copy > len)
 287                        copy = len;
 288                if (memcpy_toiovec(to, skb->data + offset, copy))
 289                        goto fault;
 290                if ((len -= copy) == 0)
 291                        return 0;
 292                offset += copy;
 293        }
 294
 295        /* Copy paged appendix. Hmm... why does this look so complicated? */
 296        for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
 297                int end;
 298
 299                WARN_ON(start > offset + len);
 300
 301                end = start + skb_shinfo(skb)->frags[i].size;
 302                if ((copy = end - offset) > 0) {
 303                        int err;
 304                        u8  *vaddr;
 305                        skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
 306                        struct page *page = frag->page;
 307
 308                        if (copy > len)
 309                                copy = len;
 310                        vaddr = kmap(page);
 311                        err = memcpy_toiovec(to, vaddr + frag->page_offset +
 312                                             offset - start, copy);
 313                        kunmap(page);
 314                        if (err)
 315                                goto fault;
 316                        if (!(len -= copy))
 317                                return 0;
 318                        offset += copy;
 319                }
 320                start = end;
 321        }
 322
 323        if (skb_shinfo(skb)->frag_list) {
 324                struct sk_buff *list = skb_shinfo(skb)->frag_list;
 325
 326                for (; list; list = list->next) {
 327                        int end;
 328
 329                        WARN_ON(start > offset + len);
 330
 331                        end = start + list->len;
 332                        if ((copy = end - offset) > 0) {
 333                                if (copy > len)
 334                                        copy = len;
 335                                if (skb_copy_datagram_iovec(list,
 336                                                            offset - start,
 337                                                            to, copy))
 338                                        goto fault;
 339                                if ((len -= copy) == 0)
 340                                        return 0;
 341                                offset += copy;
 342                        }
 343                        start = end;
 344                }
 345        }
 346        if (!len)
 347                return 0;
 348
 349fault:
 350        return -EFAULT;
 351}
 352
 353/**
 354 *      skb_copy_datagram_from_iovec - Copy a datagram from an iovec.
 355 *      @skb: buffer to copy
 356 *      @offset: offset in the buffer to start copying to
 357 *      @from: io vector to copy to
 358 *      @len: amount of data to copy to buffer from iovec
 359 *
 360 *      Returns 0 or -EFAULT.
 361 *      Note: the iovec is modified during the copy.
 362 */
 363int skb_copy_datagram_from_iovec(struct sk_buff *skb, int offset,
 364                                 struct iovec *from, int len)
 365{
 366        int start = skb_headlen(skb);
 367        int i, copy = start - offset;
 368
 369        /* Copy header. */
 370        if (copy > 0) {
 371                if (copy > len)
 372                        copy = len;
 373                if (memcpy_fromiovec(skb->data + offset, from, copy))
 374                        goto fault;
 375                if ((len -= copy) == 0)
 376                        return 0;
 377                offset += copy;
 378        }
 379
 380        /* Copy paged appendix. Hmm... why does this look so complicated? */
 381        for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
 382                int end;
 383
 384                WARN_ON(start > offset + len);
 385
 386                end = start + skb_shinfo(skb)->frags[i].size;
 387                if ((copy = end - offset) > 0) {
 388                        int err;
 389                        u8  *vaddr;
 390                        skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
 391                        struct page *page = frag->page;
 392
 393                        if (copy > len)
 394                                copy = len;
 395                        vaddr = kmap(page);
 396                        err = memcpy_fromiovec(vaddr + frag->page_offset +
 397                                               offset - start, from, copy);
 398                        kunmap(page);
 399                        if (err)
 400                                goto fault;
 401
 402                        if (!(len -= copy))
 403                                return 0;
 404                        offset += copy;
 405                }
 406                start = end;
 407        }
 408
 409        if (skb_shinfo(skb)->frag_list) {
 410                struct sk_buff *list = skb_shinfo(skb)->frag_list;
 411
 412                for (; list; list = list->next) {
 413                        int end;
 414
 415                        WARN_ON(start > offset + len);
 416
 417                        end = start + list->len;
 418                        if ((copy = end - offset) > 0) {
 419                                if (copy > len)
 420                                        copy = len;
 421                                if (skb_copy_datagram_from_iovec(list,
 422                                                                 offset - start,
 423                                                                 from, copy))
 424                                        goto fault;
 425                                if ((len -= copy) == 0)
 426                                        return 0;
 427                                offset += copy;
 428                        }
 429                        start = end;
 430                }
 431        }
 432        if (!len)
 433                return 0;
 434
 435fault:
 436        return -EFAULT;
 437}
 438EXPORT_SYMBOL(skb_copy_datagram_from_iovec);
 439
 440static int skb_copy_and_csum_datagram(const struct sk_buff *skb, int offset,
 441                                      u8 __user *to, int len,
 442                                      __wsum *csump)
 443{
 444        int start = skb_headlen(skb);
 445        int pos = 0;
 446        int i, copy = start - offset;
 447
 448        /* Copy header. */
 449        if (copy > 0) {
 450                int err = 0;
 451                if (copy > len)
 452                        copy = len;
 453                *csump = csum_and_copy_to_user(skb->data + offset, to, copy,
 454                                               *csump, &err);
 455                if (err)
 456                        goto fault;
 457                if ((len -= copy) == 0)
 458                        return 0;
 459                offset += copy;
 460                to += copy;
 461                pos = copy;
 462        }
 463
 464        for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
 465                int end;
 466
 467                WARN_ON(start > offset + len);
 468
 469                end = start + skb_shinfo(skb)->frags[i].size;
 470                if ((copy = end - offset) > 0) {
 471                        __wsum csum2;
 472                        int err = 0;
 473                        u8  *vaddr;
 474                        skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
 475                        struct page *page = frag->page;
 476
 477                        if (copy > len)
 478                                copy = len;
 479                        vaddr = kmap(page);
 480                        csum2 = csum_and_copy_to_user(vaddr +
 481                                                        frag->page_offset +
 482                                                        offset - start,
 483                                                      to, copy, 0, &err);
 484                        kunmap(page);
 485                        if (err)
 486                                goto fault;
 487                        *csump = csum_block_add(*csump, csum2, pos);
 488                        if (!(len -= copy))
 489                                return 0;
 490                        offset += copy;
 491                        to += copy;
 492                        pos += copy;
 493                }
 494                start = end;
 495        }
 496
 497        if (skb_shinfo(skb)->frag_list) {
 498                struct sk_buff *list = skb_shinfo(skb)->frag_list;
 499
 500                for (; list; list=list->next) {
 501                        int end;
 502
 503                        WARN_ON(start > offset + len);
 504
 505                        end = start + list->len;
 506                        if ((copy = end - offset) > 0) {
 507                                __wsum csum2 = 0;
 508                                if (copy > len)
 509                                        copy = len;
 510                                if (skb_copy_and_csum_datagram(list,
 511                                                               offset - start,
 512                                                               to, copy,
 513                                                               &csum2))
 514                                        goto fault;
 515                                *csump = csum_block_add(*csump, csum2, pos);
 516                                if ((len -= copy) == 0)
 517                                        return 0;
 518                                offset += copy;
 519                                to += copy;
 520                                pos += copy;
 521                        }
 522                        start = end;
 523                }
 524        }
 525        if (!len)
 526                return 0;
 527
 528fault:
 529        return -EFAULT;
 530}
 531
 532__sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len)
 533{
 534        __sum16 sum;
 535
 536        sum = csum_fold(skb_checksum(skb, 0, len, skb->csum));
 537        if (likely(!sum)) {
 538                if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE))
 539                        netdev_rx_csum_fault(skb->dev);
 540                skb->ip_summed = CHECKSUM_UNNECESSARY;
 541        }
 542        return sum;
 543}
 544EXPORT_SYMBOL(__skb_checksum_complete_head);
 545
 546__sum16 __skb_checksum_complete(struct sk_buff *skb)
 547{
 548        return __skb_checksum_complete_head(skb, skb->len);
 549}
 550EXPORT_SYMBOL(__skb_checksum_complete);
 551
 552/**
 553 *      skb_copy_and_csum_datagram_iovec - Copy and checkum skb to user iovec.
 554 *      @skb: skbuff
 555 *      @hlen: hardware length
 556 *      @iov: io vector
 557 *
 558 *      Caller _must_ check that skb will fit to this iovec.
 559 *
 560 *      Returns: 0       - success.
 561 *               -EINVAL - checksum failure.
 562 *               -EFAULT - fault during copy. Beware, in this case iovec
 563 *                         can be modified!
 564 */
 565int skb_copy_and_csum_datagram_iovec(struct sk_buff *skb,
 566                                     int hlen, struct iovec *iov)
 567{
 568        __wsum csum;
 569        int chunk = skb->len - hlen;
 570
 571        if (!chunk)
 572                return 0;
 573
 574        /* Skip filled elements.
 575         * Pretty silly, look at memcpy_toiovec, though 8)
 576         */
 577        while (!iov->iov_len)
 578                iov++;
 579
 580        if (iov->iov_len < chunk) {
 581                if (__skb_checksum_complete(skb))
 582                        goto csum_error;
 583                if (skb_copy_datagram_iovec(skb, hlen, iov, chunk))
 584                        goto fault;
 585        } else {
 586                csum = csum_partial(skb->data, hlen, skb->csum);
 587                if (skb_copy_and_csum_datagram(skb, hlen, iov->iov_base,
 588                                               chunk, &csum))
 589                        goto fault;
 590                if (csum_fold(csum))
 591                        goto csum_error;
 592                if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE))
 593                        netdev_rx_csum_fault(skb->dev);
 594                iov->iov_len -= chunk;
 595                iov->iov_base += chunk;
 596        }
 597        return 0;
 598csum_error:
 599        return -EINVAL;
 600fault:
 601        return -EFAULT;
 602}
 603
 604/**
 605 *      datagram_poll - generic datagram poll
 606 *      @file: file struct
 607 *      @sock: socket
 608 *      @wait: poll table
 609 *
 610 *      Datagram poll: Again totally generic. This also handles
 611 *      sequenced packet sockets providing the socket receive queue
 612 *      is only ever holding data ready to receive.
 613 *
 614 *      Note: when you _don't_ use this routine for this protocol,
 615 *      and you use a different write policy from sock_writeable()
 616 *      then please supply your own write_space callback.
 617 */
 618unsigned int datagram_poll(struct file *file, struct socket *sock,
 619                           poll_table *wait)
 620{
 621        struct sock *sk = sock->sk;
 622        unsigned int mask;
 623
 624        poll_wait(file, sk->sk_sleep, wait);
 625        mask = 0;
 626
 627        /* exceptional events? */
 628        if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
 629                mask |= POLLERR;
 630        if (sk->sk_shutdown & RCV_SHUTDOWN)
 631                mask |= POLLRDHUP;
 632        if (sk->sk_shutdown == SHUTDOWN_MASK)
 633                mask |= POLLHUP;
 634
 635        /* readable? */
 636        if (!skb_queue_empty(&sk->sk_receive_queue) ||
 637            (sk->sk_shutdown & RCV_SHUTDOWN))
 638                mask |= POLLIN | POLLRDNORM;
 639
 640        /* Connection-based need to check for termination and startup */
 641        if (connection_based(sk)) {
 642                if (sk->sk_state == TCP_CLOSE)
 643                        mask |= POLLHUP;
 644                /* connection hasn't started yet? */
 645                if (sk->sk_state == TCP_SYN_SENT)
 646                        return mask;
 647        }
 648
 649        /* writable? */
 650        if (sock_writeable(sk))
 651                mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
 652        else
 653                set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
 654
 655        return mask;
 656}
 657
 658EXPORT_SYMBOL(datagram_poll);
 659EXPORT_SYMBOL(skb_copy_and_csum_datagram_iovec);
 660EXPORT_SYMBOL(skb_copy_datagram_iovec);
 661EXPORT_SYMBOL(skb_free_datagram);
 662EXPORT_SYMBOL(skb_recv_datagram);
 663