linux/net/core/skbuff.c
<<
>>
Prefs
   1/*
   2 *      Routines having to do with the 'struct sk_buff' memory handlers.
   3 *
   4 *      Authors:        Alan Cox <alan@lxorguk.ukuu.org.uk>
   5 *                      Florian La Roche <rzsfl@rz.uni-sb.de>
   6 *
   7 *      Fixes:
   8 *              Alan Cox        :       Fixed the worst of the load
   9 *                                      balancer bugs.
  10 *              Dave Platt      :       Interrupt stacking fix.
  11 *      Richard Kooijman        :       Timestamp fixes.
  12 *              Alan Cox        :       Changed buffer format.
  13 *              Alan Cox        :       destructor hook for AF_UNIX etc.
  14 *              Linus Torvalds  :       Better skb_clone.
  15 *              Alan Cox        :       Added skb_copy.
  16 *              Alan Cox        :       Added all the changed routines Linus
  17 *                                      only put in the headers
  18 *              Ray VanTassle   :       Fixed --skb->lock in free
  19 *              Alan Cox        :       skb_copy copy arp field
  20 *              Andi Kleen      :       slabified it.
  21 *              Robert Olsson   :       Removed skb_head_pool
  22 *
  23 *      NOTE:
  24 *              The __skb_ routines should be called with interrupts
  25 *      disabled, or you better be *real* sure that the operation is atomic
  26 *      with respect to whatever list is being frobbed (e.g. via lock_sock()
  27 *      or via disabling bottom half handlers, etc).
  28 *
  29 *      This program is free software; you can redistribute it and/or
  30 *      modify it under the terms of the GNU General Public License
  31 *      as published by the Free Software Foundation; either version
  32 *      2 of the License, or (at your option) any later version.
  33 */
  34
  35/*
  36 *      The functions in this file will not compile correctly with gcc 2.4.x
  37 */
  38
  39#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  40
  41#include <linux/module.h>
  42#include <linux/types.h>
  43#include <linux/kernel.h>
  44#include <linux/kmemcheck.h>
  45#include <linux/mm.h>
  46#include <linux/interrupt.h>
  47#include <linux/in.h>
  48#include <linux/inet.h>
  49#include <linux/slab.h>
  50#include <linux/netdevice.h>
  51#ifdef CONFIG_NET_CLS_ACT
  52#include <net/pkt_sched.h>
  53#endif
  54#include <linux/string.h>
  55#include <linux/skbuff.h>
  56#include <linux/splice.h>
  57#include <linux/cache.h>
  58#include <linux/rtnetlink.h>
  59#include <linux/init.h>
  60#include <linux/scatterlist.h>
  61#include <linux/errqueue.h>
  62#include <linux/prefetch.h>
  63
  64#include <net/protocol.h>
  65#include <net/dst.h>
  66#include <net/sock.h>
  67#include <net/checksum.h>
  68#include <net/xfrm.h>
  69
  70#include <asm/uaccess.h>
  71#include <trace/events/skb.h>
  72#include <linux/highmem.h>
  73
  74struct kmem_cache *skbuff_head_cache __read_mostly;
  75static struct kmem_cache *skbuff_fclone_cache __read_mostly;
  76
  77static void sock_pipe_buf_release(struct pipe_inode_info *pipe,
  78                                  struct pipe_buffer *buf)
  79{
  80        put_page(buf->page);
  81}
  82
  83static void sock_pipe_buf_get(struct pipe_inode_info *pipe,
  84                                struct pipe_buffer *buf)
  85{
  86        get_page(buf->page);
  87}
  88
  89static int sock_pipe_buf_steal(struct pipe_inode_info *pipe,
  90                               struct pipe_buffer *buf)
  91{
  92        return 1;
  93}
  94
  95
  96/* Pipe buffer operations for a socket. */
  97static const struct pipe_buf_operations sock_pipe_buf_ops = {
  98        .can_merge = 0,
  99        .map = generic_pipe_buf_map,
 100        .unmap = generic_pipe_buf_unmap,
 101        .confirm = generic_pipe_buf_confirm,
 102        .release = sock_pipe_buf_release,
 103        .steal = sock_pipe_buf_steal,
 104        .get = sock_pipe_buf_get,
 105};
 106
 107/*
 108 *      Keep out-of-line to prevent kernel bloat.
 109 *      __builtin_return_address is not used because it is not always
 110 *      reliable.
 111 */
 112
 113/**
 114 *      skb_over_panic  -       private function
 115 *      @skb: buffer
 116 *      @sz: size
 117 *      @here: address
 118 *
 119 *      Out of line support code for skb_put(). Not user callable.
 120 */
 121static void skb_over_panic(struct sk_buff *skb, int sz, void *here)
 122{
 123        pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n",
 124                 __func__, here, skb->len, sz, skb->head, skb->data,
 125                 (unsigned long)skb->tail, (unsigned long)skb->end,
 126                 skb->dev ? skb->dev->name : "<NULL>");
 127        BUG();
 128}
 129
 130/**
 131 *      skb_under_panic -       private function
 132 *      @skb: buffer
 133 *      @sz: size
 134 *      @here: address
 135 *
 136 *      Out of line support code for skb_push(). Not user callable.
 137 */
 138
 139static void skb_under_panic(struct sk_buff *skb, int sz, void *here)
 140{
 141        pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n",
 142                 __func__, here, skb->len, sz, skb->head, skb->data,
 143                 (unsigned long)skb->tail, (unsigned long)skb->end,
 144                 skb->dev ? skb->dev->name : "<NULL>");
 145        BUG();
 146}
 147
 148
 149/*
 150 * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
 151 * the caller if emergency pfmemalloc reserves are being used. If it is and
 152 * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
 153 * may be used. Otherwise, the packet data may be discarded until enough
 154 * memory is free
 155 */
 156#define kmalloc_reserve(size, gfp, node, pfmemalloc) \
 157         __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc)
 158void *__kmalloc_reserve(size_t size, gfp_t flags, int node, unsigned long ip,
 159                         bool *pfmemalloc)
 160{
 161        void *obj;
 162        bool ret_pfmemalloc = false;
 163
 164        /*
 165         * Try a regular allocation, when that fails and we're not entitled
 166         * to the reserves, fail.
 167         */
 168        obj = kmalloc_node_track_caller(size,
 169                                        flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
 170                                        node);
 171        if (obj || !(gfp_pfmemalloc_allowed(flags)))
 172                goto out;
 173
 174        /* Try again but now we are using pfmemalloc reserves */
 175        ret_pfmemalloc = true;
 176        obj = kmalloc_node_track_caller(size, flags, node);
 177
 178out:
 179        if (pfmemalloc)
 180                *pfmemalloc = ret_pfmemalloc;
 181
 182        return obj;
 183}
 184
 185/*      Allocate a new skbuff. We do this ourselves so we can fill in a few
 186 *      'private' fields and also do memory statistics to find all the
 187 *      [BEEP] leaks.
 188 *
 189 */
 190
 191/**
 192 *      __alloc_skb     -       allocate a network buffer
 193 *      @size: size to allocate
 194 *      @gfp_mask: allocation mask
 195 *      @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
 196 *              instead of head cache and allocate a cloned (child) skb.
 197 *              If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
 198 *              allocations in case the data is required for writeback
 199 *      @node: numa node to allocate memory on
 200 *
 201 *      Allocate a new &sk_buff. The returned buffer has no headroom and a
 202 *      tail room of at least size bytes. The object has a reference count
 203 *      of one. The return is the buffer. On a failure the return is %NULL.
 204 *
 205 *      Buffers may only be allocated from interrupts using a @gfp_mask of
 206 *      %GFP_ATOMIC.
 207 */
 208struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
 209                            int flags, int node)
 210{
 211        struct kmem_cache *cache;
 212        struct skb_shared_info *shinfo;
 213        struct sk_buff *skb;
 214        u8 *data;
 215        bool pfmemalloc;
 216
 217        cache = (flags & SKB_ALLOC_FCLONE)
 218                ? skbuff_fclone_cache : skbuff_head_cache;
 219
 220        if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX))
 221                gfp_mask |= __GFP_MEMALLOC;
 222
 223        /* Get the HEAD */
 224        skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
 225        if (!skb)
 226                goto out;
 227        prefetchw(skb);
 228
 229        /* We do our best to align skb_shared_info on a separate cache
 230         * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
 231         * aligned memory blocks, unless SLUB/SLAB debug is enabled.
 232         * Both skb->head and skb_shared_info are cache line aligned.
 233         */
 234        size = SKB_DATA_ALIGN(size);
 235        size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
 236        data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc);
 237        if (!data)
 238                goto nodata;
 239        /* kmalloc(size) might give us more room than requested.
 240         * Put skb_shared_info exactly at the end of allocated zone,
 241         * to allow max possible filling before reallocation.
 242         */
 243        size = SKB_WITH_OVERHEAD(ksize(data));
 244        prefetchw(data + size);
 245
 246        /*
 247         * Only clear those fields we need to clear, not those that we will
 248         * actually initialise below. Hence, don't put any more fields after
 249         * the tail pointer in struct sk_buff!
 250         */
 251        memset(skb, 0, offsetof(struct sk_buff, tail));
 252        /* Account for allocated memory : skb + skb->head */
 253        skb->truesize = SKB_TRUESIZE(size);
 254        skb->pfmemalloc = pfmemalloc;
 255        atomic_set(&skb->users, 1);
 256        skb->head = data;
 257        skb->data = data;
 258        skb_reset_tail_pointer(skb);
 259        skb->end = skb->tail + size;
 260#ifdef NET_SKBUFF_DATA_USES_OFFSET
 261        skb->mac_header = ~0U;
 262#endif
 263
 264        /* make sure we initialize shinfo sequentially */
 265        shinfo = skb_shinfo(skb);
 266        memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
 267        atomic_set(&shinfo->dataref, 1);
 268        kmemcheck_annotate_variable(shinfo->destructor_arg);
 269
 270        if (flags & SKB_ALLOC_FCLONE) {
 271                struct sk_buff *child = skb + 1;
 272                atomic_t *fclone_ref = (atomic_t *) (child + 1);
 273
 274                kmemcheck_annotate_bitfield(child, flags1);
 275                kmemcheck_annotate_bitfield(child, flags2);
 276                skb->fclone = SKB_FCLONE_ORIG;
 277                atomic_set(fclone_ref, 1);
 278
 279                child->fclone = SKB_FCLONE_UNAVAILABLE;
 280                child->pfmemalloc = pfmemalloc;
 281        }
 282out:
 283        return skb;
 284nodata:
 285        kmem_cache_free(cache, skb);
 286        skb = NULL;
 287        goto out;
 288}
 289EXPORT_SYMBOL(__alloc_skb);
 290
 291/**
 292 * build_skb - build a network buffer
 293 * @data: data buffer provided by caller
 294 * @frag_size: size of fragment, or 0 if head was kmalloced
 295 *
 296 * Allocate a new &sk_buff. Caller provides space holding head and
 297 * skb_shared_info. @data must have been allocated by kmalloc()
 298 * The return is the new skb buffer.
 299 * On a failure the return is %NULL, and @data is not freed.
 300 * Notes :
 301 *  Before IO, driver allocates only data buffer where NIC put incoming frame
 302 *  Driver should add room at head (NET_SKB_PAD) and
 303 *  MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
 304 *  After IO, driver calls build_skb(), to allocate sk_buff and populate it
 305 *  before giving packet to stack.
 306 *  RX rings only contains data buffers, not full skbs.
 307 */
 308struct sk_buff *build_skb(void *data, unsigned int frag_size)
 309{
 310        struct skb_shared_info *shinfo;
 311        struct sk_buff *skb;
 312        unsigned int size = frag_size ? : ksize(data);
 313
 314        skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
 315        if (!skb)
 316                return NULL;
 317
 318        size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
 319
 320        memset(skb, 0, offsetof(struct sk_buff, tail));
 321        skb->truesize = SKB_TRUESIZE(size);
 322        skb->head_frag = frag_size != 0;
 323        atomic_set(&skb->users, 1);
 324        skb->head = data;
 325        skb->data = data;
 326        skb_reset_tail_pointer(skb);
 327        skb->end = skb->tail + size;
 328#ifdef NET_SKBUFF_DATA_USES_OFFSET
 329        skb->mac_header = ~0U;
 330#endif
 331
 332        /* make sure we initialize shinfo sequentially */
 333        shinfo = skb_shinfo(skb);
 334        memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
 335        atomic_set(&shinfo->dataref, 1);
 336        kmemcheck_annotate_variable(shinfo->destructor_arg);
 337
 338        return skb;
 339}
 340EXPORT_SYMBOL(build_skb);
 341
 342struct netdev_alloc_cache {
 343        struct page *page;
 344        unsigned int offset;
 345        unsigned int pagecnt_bias;
 346};
 347static DEFINE_PER_CPU(struct netdev_alloc_cache, netdev_alloc_cache);
 348
 349#define NETDEV_PAGECNT_BIAS (PAGE_SIZE / SMP_CACHE_BYTES)
 350
 351static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
 352{
 353        struct netdev_alloc_cache *nc;
 354        void *data = NULL;
 355        unsigned long flags;
 356
 357        local_irq_save(flags);
 358        nc = &__get_cpu_var(netdev_alloc_cache);
 359        if (unlikely(!nc->page)) {
 360refill:
 361                nc->page = alloc_page(gfp_mask);
 362                if (unlikely(!nc->page))
 363                        goto end;
 364recycle:
 365                atomic_set(&nc->page->_count, NETDEV_PAGECNT_BIAS);
 366                nc->pagecnt_bias = NETDEV_PAGECNT_BIAS;
 367                nc->offset = 0;
 368        }
 369
 370        if (nc->offset + fragsz > PAGE_SIZE) {
 371                /* avoid unnecessary locked operations if possible */
 372                if ((atomic_read(&nc->page->_count) == nc->pagecnt_bias) ||
 373                    atomic_sub_and_test(nc->pagecnt_bias, &nc->page->_count))
 374                        goto recycle;
 375                goto refill;
 376        }
 377
 378        data = page_address(nc->page) + nc->offset;
 379        nc->offset += fragsz;
 380        nc->pagecnt_bias--;
 381end:
 382        local_irq_restore(flags);
 383        return data;
 384}
 385
 386/**
 387 * netdev_alloc_frag - allocate a page fragment
 388 * @fragsz: fragment size
 389 *
 390 * Allocates a frag from a page for receive buffer.
 391 * Uses GFP_ATOMIC allocations.
 392 */
 393void *netdev_alloc_frag(unsigned int fragsz)
 394{
 395        return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
 396}
 397EXPORT_SYMBOL(netdev_alloc_frag);
 398
 399/**
 400 *      __netdev_alloc_skb - allocate an skbuff for rx on a specific device
 401 *      @dev: network device to receive on
 402 *      @length: length to allocate
 403 *      @gfp_mask: get_free_pages mask, passed to alloc_skb
 404 *
 405 *      Allocate a new &sk_buff and assign it a usage count of one. The
 406 *      buffer has unspecified headroom built in. Users should allocate
 407 *      the headroom they think they need without accounting for the
 408 *      built in space. The built in space is used for optimisations.
 409 *
 410 *      %NULL is returned if there is no free memory.
 411 */
 412struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
 413                                   unsigned int length, gfp_t gfp_mask)
 414{
 415        struct sk_buff *skb = NULL;
 416        unsigned int fragsz = SKB_DATA_ALIGN(length + NET_SKB_PAD) +
 417                              SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
 418
 419        if (fragsz <= PAGE_SIZE && !(gfp_mask & (__GFP_WAIT | GFP_DMA))) {
 420                void *data;
 421
 422                if (sk_memalloc_socks())
 423                        gfp_mask |= __GFP_MEMALLOC;
 424
 425                data = __netdev_alloc_frag(fragsz, gfp_mask);
 426
 427                if (likely(data)) {
 428                        skb = build_skb(data, fragsz);
 429                        if (unlikely(!skb))
 430                                put_page(virt_to_head_page(data));
 431                }
 432        } else {
 433                skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask,
 434                                  SKB_ALLOC_RX, NUMA_NO_NODE);
 435        }
 436        if (likely(skb)) {
 437                skb_reserve(skb, NET_SKB_PAD);
 438                skb->dev = dev;
 439        }
 440        return skb;
 441}
 442EXPORT_SYMBOL(__netdev_alloc_skb);
 443
 444void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
 445                     int size, unsigned int truesize)
 446{
 447        skb_fill_page_desc(skb, i, page, off, size);
 448        skb->len += size;
 449        skb->data_len += size;
 450        skb->truesize += truesize;
 451}
 452EXPORT_SYMBOL(skb_add_rx_frag);
 453
 454static void skb_drop_list(struct sk_buff **listp)
 455{
 456        struct sk_buff *list = *listp;
 457
 458        *listp = NULL;
 459
 460        do {
 461                struct sk_buff *this = list;
 462                list = list->next;
 463                kfree_skb(this);
 464        } while (list);
 465}
 466
 467static inline void skb_drop_fraglist(struct sk_buff *skb)
 468{
 469        skb_drop_list(&skb_shinfo(skb)->frag_list);
 470}
 471
 472static void skb_clone_fraglist(struct sk_buff *skb)
 473{
 474        struct sk_buff *list;
 475
 476        skb_walk_frags(skb, list)
 477                skb_get(list);
 478}
 479
 480static void skb_free_head(struct sk_buff *skb)
 481{
 482        if (skb->head_frag)
 483                put_page(virt_to_head_page(skb->head));
 484        else
 485                kfree(skb->head);
 486}
 487
 488static void skb_release_data(struct sk_buff *skb)
 489{
 490        if (!skb->cloned ||
 491            !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
 492                               &skb_shinfo(skb)->dataref)) {
 493                if (skb_shinfo(skb)->nr_frags) {
 494                        int i;
 495                        for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
 496                                skb_frag_unref(skb, i);
 497                }
 498
 499                /*
 500                 * If skb buf is from userspace, we need to notify the caller
 501                 * the lower device DMA has done;
 502                 */
 503                if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
 504                        struct ubuf_info *uarg;
 505
 506                        uarg = skb_shinfo(skb)->destructor_arg;
 507                        if (uarg->callback)
 508                                uarg->callback(uarg);
 509                }
 510
 511                if (skb_has_frag_list(skb))
 512                        skb_drop_fraglist(skb);
 513
 514                skb_free_head(skb);
 515        }
 516}
 517
 518/*
 519 *      Free an skbuff by memory without cleaning the state.
 520 */
 521static void kfree_skbmem(struct sk_buff *skb)
 522{
 523        struct sk_buff *other;
 524        atomic_t *fclone_ref;
 525
 526        switch (skb->fclone) {
 527        case SKB_FCLONE_UNAVAILABLE:
 528                kmem_cache_free(skbuff_head_cache, skb);
 529                break;
 530
 531        case SKB_FCLONE_ORIG:
 532                fclone_ref = (atomic_t *) (skb + 2);
 533                if (atomic_dec_and_test(fclone_ref))
 534                        kmem_cache_free(skbuff_fclone_cache, skb);
 535                break;
 536
 537        case SKB_FCLONE_CLONE:
 538                fclone_ref = (atomic_t *) (skb + 1);
 539                other = skb - 1;
 540
 541                /* The clone portion is available for
 542                 * fast-cloning again.
 543                 */
 544                skb->fclone = SKB_FCLONE_UNAVAILABLE;
 545
 546                if (atomic_dec_and_test(fclone_ref))
 547                        kmem_cache_free(skbuff_fclone_cache, other);
 548                break;
 549        }
 550}
 551
 552static void skb_release_head_state(struct sk_buff *skb)
 553{
 554        skb_dst_drop(skb);
 555#ifdef CONFIG_XFRM
 556        secpath_put(skb->sp);
 557#endif
 558        if (skb->destructor) {
 559                WARN_ON(in_irq());
 560                skb->destructor(skb);
 561        }
 562#if IS_ENABLED(CONFIG_NF_CONNTRACK)
 563        nf_conntrack_put(skb->nfct);
 564#endif
 565#ifdef NET_SKBUFF_NF_DEFRAG_NEEDED
 566        nf_conntrack_put_reasm(skb->nfct_reasm);
 567#endif
 568#ifdef CONFIG_BRIDGE_NETFILTER
 569        nf_bridge_put(skb->nf_bridge);
 570#endif
 571/* XXX: IS this still necessary? - JHS */
 572#ifdef CONFIG_NET_SCHED
 573        skb->tc_index = 0;
 574#ifdef CONFIG_NET_CLS_ACT
 575        skb->tc_verd = 0;
 576#endif
 577#endif
 578}
 579
 580/* Free everything but the sk_buff shell. */
 581static void skb_release_all(struct sk_buff *skb)
 582{
 583        skb_release_head_state(skb);
 584        skb_release_data(skb);
 585}
 586
 587/**
 588 *      __kfree_skb - private function
 589 *      @skb: buffer
 590 *
 591 *      Free an sk_buff. Release anything attached to the buffer.
 592 *      Clean the state. This is an internal helper function. Users should
 593 *      always call kfree_skb
 594 */
 595
 596void __kfree_skb(struct sk_buff *skb)
 597{
 598        skb_release_all(skb);
 599        kfree_skbmem(skb);
 600}
 601EXPORT_SYMBOL(__kfree_skb);
 602
 603/**
 604 *      kfree_skb - free an sk_buff
 605 *      @skb: buffer to free
 606 *
 607 *      Drop a reference to the buffer and free it if the usage count has
 608 *      hit zero.
 609 */
 610void kfree_skb(struct sk_buff *skb)
 611{
 612        if (unlikely(!skb))
 613                return;
 614        if (likely(atomic_read(&skb->users) == 1))
 615                smp_rmb();
 616        else if (likely(!atomic_dec_and_test(&skb->users)))
 617                return;
 618        trace_kfree_skb(skb, __builtin_return_address(0));
 619        __kfree_skb(skb);
 620}
 621EXPORT_SYMBOL(kfree_skb);
 622
 623/**
 624 *      consume_skb - free an skbuff
 625 *      @skb: buffer to free
 626 *
 627 *      Drop a ref to the buffer and free it if the usage count has hit zero
 628 *      Functions identically to kfree_skb, but kfree_skb assumes that the frame
 629 *      is being dropped after a failure and notes that
 630 */
 631void consume_skb(struct sk_buff *skb)
 632{
 633        if (unlikely(!skb))
 634                return;
 635        if (likely(atomic_read(&skb->users) == 1))
 636                smp_rmb();
 637        else if (likely(!atomic_dec_and_test(&skb->users)))
 638                return;
 639        trace_consume_skb(skb);
 640        __kfree_skb(skb);
 641}
 642EXPORT_SYMBOL(consume_skb);
 643
 644static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
 645{
 646        new->tstamp             = old->tstamp;
 647        new->dev                = old->dev;
 648        new->transport_header   = old->transport_header;
 649        new->network_header     = old->network_header;
 650        new->mac_header         = old->mac_header;
 651        skb_dst_copy(new, old);
 652        new->rxhash             = old->rxhash;
 653        new->ooo_okay           = old->ooo_okay;
 654        new->l4_rxhash          = old->l4_rxhash;
 655        new->no_fcs             = old->no_fcs;
 656#ifdef CONFIG_XFRM
 657        new->sp                 = secpath_get(old->sp);
 658#endif
 659        memcpy(new->cb, old->cb, sizeof(old->cb));
 660        new->csum               = old->csum;
 661        new->local_df           = old->local_df;
 662        new->pkt_type           = old->pkt_type;
 663        new->ip_summed          = old->ip_summed;
 664        skb_copy_queue_mapping(new, old);
 665        new->priority           = old->priority;
 666#if IS_ENABLED(CONFIG_IP_VS)
 667        new->ipvs_property      = old->ipvs_property;
 668#endif
 669        new->pfmemalloc         = old->pfmemalloc;
 670        new->protocol           = old->protocol;
 671        new->mark               = old->mark;
 672        new->skb_iif            = old->skb_iif;
 673        __nf_copy(new, old);
 674#if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE)
 675        new->nf_trace           = old->nf_trace;
 676#endif
 677#ifdef CONFIG_NET_SCHED
 678        new->tc_index           = old->tc_index;
 679#ifdef CONFIG_NET_CLS_ACT
 680        new->tc_verd            = old->tc_verd;
 681#endif
 682#endif
 683        new->vlan_tci           = old->vlan_tci;
 684
 685        skb_copy_secmark(new, old);
 686}
 687
 688/*
 689 * You should not add any new code to this function.  Add it to
 690 * __copy_skb_header above instead.
 691 */
 692static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
 693{
 694#define C(x) n->x = skb->x
 695
 696        n->next = n->prev = NULL;
 697        n->sk = NULL;
 698        __copy_skb_header(n, skb);
 699
 700        C(len);
 701        C(data_len);
 702        C(mac_len);
 703        n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
 704        n->cloned = 1;
 705        n->nohdr = 0;
 706        n->destructor = NULL;
 707        C(tail);
 708        C(end);
 709        C(head);
 710        C(head_frag);
 711        C(data);
 712        C(truesize);
 713        atomic_set(&n->users, 1);
 714
 715        atomic_inc(&(skb_shinfo(skb)->dataref));
 716        skb->cloned = 1;
 717
 718        return n;
 719#undef C
 720}
 721
 722/**
 723 *      skb_morph       -       morph one skb into another
 724 *      @dst: the skb to receive the contents
 725 *      @src: the skb to supply the contents
 726 *
 727 *      This is identical to skb_clone except that the target skb is
 728 *      supplied by the user.
 729 *
 730 *      The target skb is returned upon exit.
 731 */
 732struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
 733{
 734        skb_release_all(dst);
 735        return __skb_clone(dst, src);
 736}
 737EXPORT_SYMBOL_GPL(skb_morph);
 738
 739/**
 740 *      skb_copy_ubufs  -       copy userspace skb frags buffers to kernel
 741 *      @skb: the skb to modify
 742 *      @gfp_mask: allocation priority
 743 *
 744 *      This must be called on SKBTX_DEV_ZEROCOPY skb.
 745 *      It will copy all frags into kernel and drop the reference
 746 *      to userspace pages.
 747 *
 748 *      If this function is called from an interrupt gfp_mask() must be
 749 *      %GFP_ATOMIC.
 750 *
 751 *      Returns 0 on success or a negative error code on failure
 752 *      to allocate kernel memory to copy to.
 753 */
 754int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
 755{
 756        int i;
 757        int num_frags = skb_shinfo(skb)->nr_frags;
 758        struct page *page, *head = NULL;
 759        struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg;
 760
 761        for (i = 0; i < num_frags; i++) {
 762                u8 *vaddr;
 763                skb_frag_t *f = &skb_shinfo(skb)->frags[i];
 764
 765                page = alloc_page(gfp_mask);
 766                if (!page) {
 767                        while (head) {
 768                                struct page *next = (struct page *)head->private;
 769                                put_page(head);
 770                                head = next;
 771                        }
 772                        return -ENOMEM;
 773                }
 774                vaddr = kmap_atomic(skb_frag_page(f));
 775                memcpy(page_address(page),
 776                       vaddr + f->page_offset, skb_frag_size(f));
 777                kunmap_atomic(vaddr);
 778                page->private = (unsigned long)head;
 779                head = page;
 780        }
 781
 782        /* skb frags release userspace buffers */
 783        for (i = 0; i < num_frags; i++)
 784                skb_frag_unref(skb, i);
 785
 786        uarg->callback(uarg);
 787
 788        /* skb frags point to kernel buffers */
 789        for (i = num_frags - 1; i >= 0; i--) {
 790                __skb_fill_page_desc(skb, i, head, 0,
 791                                     skb_shinfo(skb)->frags[i].size);
 792                head = (struct page *)head->private;
 793        }
 794
 795        skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
 796        return 0;
 797}
 798EXPORT_SYMBOL_GPL(skb_copy_ubufs);
 799
 800/**
 801 *      skb_clone       -       duplicate an sk_buff
 802 *      @skb: buffer to clone
 803 *      @gfp_mask: allocation priority
 804 *
 805 *      Duplicate an &sk_buff. The new one is not owned by a socket. Both
 806 *      copies share the same packet data but not structure. The new
 807 *      buffer has a reference count of 1. If the allocation fails the
 808 *      function returns %NULL otherwise the new buffer is returned.
 809 *
 810 *      If this function is called from an interrupt gfp_mask() must be
 811 *      %GFP_ATOMIC.
 812 */
 813
 814struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
 815{
 816        struct sk_buff *n;
 817
 818        if (skb_orphan_frags(skb, gfp_mask))
 819                return NULL;
 820
 821        n = skb + 1;
 822        if (skb->fclone == SKB_FCLONE_ORIG &&
 823            n->fclone == SKB_FCLONE_UNAVAILABLE) {
 824                atomic_t *fclone_ref = (atomic_t *) (n + 1);
 825                n->fclone = SKB_FCLONE_CLONE;
 826                atomic_inc(fclone_ref);
 827        } else {
 828                if (skb_pfmemalloc(skb))
 829                        gfp_mask |= __GFP_MEMALLOC;
 830
 831                n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
 832                if (!n)
 833                        return NULL;
 834
 835                kmemcheck_annotate_bitfield(n, flags1);
 836                kmemcheck_annotate_bitfield(n, flags2);
 837                n->fclone = SKB_FCLONE_UNAVAILABLE;
 838        }
 839
 840        return __skb_clone(n, skb);
 841}
 842EXPORT_SYMBOL(skb_clone);
 843
 844static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
 845{
 846#ifndef NET_SKBUFF_DATA_USES_OFFSET
 847        /*
 848         *      Shift between the two data areas in bytes
 849         */
 850        unsigned long offset = new->data - old->data;
 851#endif
 852
 853        __copy_skb_header(new, old);
 854
 855#ifndef NET_SKBUFF_DATA_USES_OFFSET
 856        /* {transport,network,mac}_header are relative to skb->head */
 857        new->transport_header += offset;
 858        new->network_header   += offset;
 859        if (skb_mac_header_was_set(new))
 860                new->mac_header       += offset;
 861#endif
 862        skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
 863        skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
 864        skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
 865}
 866
 867static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
 868{
 869        if (skb_pfmemalloc(skb))
 870                return SKB_ALLOC_RX;
 871        return 0;
 872}
 873
 874/**
 875 *      skb_copy        -       create private copy of an sk_buff
 876 *      @skb: buffer to copy
 877 *      @gfp_mask: allocation priority
 878 *
 879 *      Make a copy of both an &sk_buff and its data. This is used when the
 880 *      caller wishes to modify the data and needs a private copy of the
 881 *      data to alter. Returns %NULL on failure or the pointer to the buffer
 882 *      on success. The returned buffer has a reference count of 1.
 883 *
 884 *      As by-product this function converts non-linear &sk_buff to linear
 885 *      one, so that &sk_buff becomes completely private and caller is allowed
 886 *      to modify all the data of returned buffer. This means that this
 887 *      function is not recommended for use in circumstances when only
 888 *      header is going to be modified. Use pskb_copy() instead.
 889 */
 890
 891struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
 892{
 893        int headerlen = skb_headroom(skb);
 894        unsigned int size = skb_end_offset(skb) + skb->data_len;
 895        struct sk_buff *n = __alloc_skb(size, gfp_mask,
 896                                        skb_alloc_rx_flag(skb), NUMA_NO_NODE);
 897
 898        if (!n)
 899                return NULL;
 900
 901        /* Set the data pointer */
 902        skb_reserve(n, headerlen);
 903        /* Set the tail pointer and length */
 904        skb_put(n, skb->len);
 905
 906        if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
 907                BUG();
 908
 909        copy_skb_header(n, skb);
 910        return n;
 911}
 912EXPORT_SYMBOL(skb_copy);
 913
 914/**
 915 *      __pskb_copy     -       create copy of an sk_buff with private head.
 916 *      @skb: buffer to copy
 917 *      @headroom: headroom of new skb
 918 *      @gfp_mask: allocation priority
 919 *
 920 *      Make a copy of both an &sk_buff and part of its data, located
 921 *      in header. Fragmented data remain shared. This is used when
 922 *      the caller wishes to modify only header of &sk_buff and needs
 923 *      private copy of the header to alter. Returns %NULL on failure
 924 *      or the pointer to the buffer on success.
 925 *      The returned buffer has a reference count of 1.
 926 */
 927
 928struct sk_buff *__pskb_copy(struct sk_buff *skb, int headroom, gfp_t gfp_mask)
 929{
 930        unsigned int size = skb_headlen(skb) + headroom;
 931        struct sk_buff *n = __alloc_skb(size, gfp_mask,
 932                                        skb_alloc_rx_flag(skb), NUMA_NO_NODE);
 933
 934        if (!n)
 935                goto out;
 936
 937        /* Set the data pointer */
 938        skb_reserve(n, headroom);
 939        /* Set the tail pointer and length */
 940        skb_put(n, skb_headlen(skb));
 941        /* Copy the bytes */
 942        skb_copy_from_linear_data(skb, n->data, n->len);
 943
 944        n->truesize += skb->data_len;
 945        n->data_len  = skb->data_len;
 946        n->len       = skb->len;
 947
 948        if (skb_shinfo(skb)->nr_frags) {
 949                int i;
 950
 951                if (skb_orphan_frags(skb, gfp_mask)) {
 952                        kfree_skb(n);
 953                        n = NULL;
 954                        goto out;
 955                }
 956                for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
 957                        skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
 958                        skb_frag_ref(skb, i);
 959                }
 960                skb_shinfo(n)->nr_frags = i;
 961        }
 962
 963        if (skb_has_frag_list(skb)) {
 964                skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
 965                skb_clone_fraglist(n);
 966        }
 967
 968        copy_skb_header(n, skb);
 969out:
 970        return n;
 971}
 972EXPORT_SYMBOL(__pskb_copy);
 973
 974/**
 975 *      pskb_expand_head - reallocate header of &sk_buff
 976 *      @skb: buffer to reallocate
 977 *      @nhead: room to add at head
 978 *      @ntail: room to add at tail
 979 *      @gfp_mask: allocation priority
 980 *
 981 *      Expands (or creates identical copy, if &nhead and &ntail are zero)
 982 *      header of skb. &sk_buff itself is not changed. &sk_buff MUST have
 983 *      reference count of 1. Returns zero in the case of success or error,
 984 *      if expansion failed. In the last case, &sk_buff is not changed.
 985 *
 986 *      All the pointers pointing into skb header may change and must be
 987 *      reloaded after call to this function.
 988 */
 989
 990int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
 991                     gfp_t gfp_mask)
 992{
 993        int i;
 994        u8 *data;
 995        int size = nhead + skb_end_offset(skb) + ntail;
 996        long off;
 997
 998        BUG_ON(nhead < 0);
 999
1000        if (skb_shared(skb))
1001                BUG();
1002
1003        size = SKB_DATA_ALIGN(size);
1004
1005        if (skb_pfmemalloc(skb))
1006                gfp_mask |= __GFP_MEMALLOC;
1007        data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1008                               gfp_mask, NUMA_NO_NODE, NULL);
1009        if (!data)
1010                goto nodata;
1011        size = SKB_WITH_OVERHEAD(ksize(data));
1012
1013        /* Copy only real data... and, alas, header. This should be
1014         * optimized for the cases when header is void.
1015         */
1016        memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
1017
1018        memcpy((struct skb_shared_info *)(data + size),
1019               skb_shinfo(skb),
1020               offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
1021
1022        /*
1023         * if shinfo is shared we must drop the old head gracefully, but if it
1024         * is not we can just drop the old head and let the existing refcount
1025         * be since all we did is relocate the values
1026         */
1027        if (skb_cloned(skb)) {
1028                /* copy this zero copy skb frags */
1029                if (skb_orphan_frags(skb, gfp_mask))
1030                        goto nofrags;
1031                for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1032                        skb_frag_ref(skb, i);
1033
1034                if (skb_has_frag_list(skb))
1035                        skb_clone_fraglist(skb);
1036
1037                skb_release_data(skb);
1038        } else {
1039                skb_free_head(skb);
1040        }
1041        off = (data + nhead) - skb->head;
1042
1043        skb->head     = data;
1044        skb->head_frag = 0;
1045        skb->data    += off;
1046#ifdef NET_SKBUFF_DATA_USES_OFFSET
1047        skb->end      = size;
1048        off           = nhead;
1049#else
1050        skb->end      = skb->head + size;
1051#endif
1052        /* {transport,network,mac}_header and tail are relative to skb->head */
1053        skb->tail             += off;
1054        skb->transport_header += off;
1055        skb->network_header   += off;
1056        if (skb_mac_header_was_set(skb))
1057                skb->mac_header += off;
1058        /* Only adjust this if it actually is csum_start rather than csum */
1059        if (skb->ip_summed == CHECKSUM_PARTIAL)
1060                skb->csum_start += nhead;
1061        skb->cloned   = 0;
1062        skb->hdr_len  = 0;
1063        skb->nohdr    = 0;
1064        atomic_set(&skb_shinfo(skb)->dataref, 1);
1065        return 0;
1066
1067nofrags:
1068        kfree(data);
1069nodata:
1070        return -ENOMEM;
1071}
1072EXPORT_SYMBOL(pskb_expand_head);
1073
1074/* Make private copy of skb with writable head and some headroom */
1075
1076struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
1077{
1078        struct sk_buff *skb2;
1079        int delta = headroom - skb_headroom(skb);
1080
1081        if (delta <= 0)
1082                skb2 = pskb_copy(skb, GFP_ATOMIC);
1083        else {
1084                skb2 = skb_clone(skb, GFP_ATOMIC);
1085                if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
1086                                             GFP_ATOMIC)) {
1087                        kfree_skb(skb2);
1088                        skb2 = NULL;
1089                }
1090        }
1091        return skb2;
1092}
1093EXPORT_SYMBOL(skb_realloc_headroom);
1094
1095/**
1096 *      skb_copy_expand -       copy and expand sk_buff
1097 *      @skb: buffer to copy
1098 *      @newheadroom: new free bytes at head
1099 *      @newtailroom: new free bytes at tail
1100 *      @gfp_mask: allocation priority
1101 *
1102 *      Make a copy of both an &sk_buff and its data and while doing so
1103 *      allocate additional space.
1104 *
1105 *      This is used when the caller wishes to modify the data and needs a
1106 *      private copy of the data to alter as well as more space for new fields.
1107 *      Returns %NULL on failure or the pointer to the buffer
1108 *      on success. The returned buffer has a reference count of 1.
1109 *
1110 *      You must pass %GFP_ATOMIC as the allocation priority if this function
1111 *      is called from an interrupt.
1112 */
1113struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
1114                                int newheadroom, int newtailroom,
1115                                gfp_t gfp_mask)
1116{
1117        /*
1118         *      Allocate the copy buffer
1119         */
1120        struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1121                                        gfp_mask, skb_alloc_rx_flag(skb),
1122                                        NUMA_NO_NODE);
1123        int oldheadroom = skb_headroom(skb);
1124        int head_copy_len, head_copy_off;
1125        int off;
1126
1127        if (!n)
1128                return NULL;
1129
1130        skb_reserve(n, newheadroom);
1131
1132        /* Set the tail pointer and length */
1133        skb_put(n, skb->len);
1134
1135        head_copy_len = oldheadroom;
1136        head_copy_off = 0;
1137        if (newheadroom <= head_copy_len)
1138                head_copy_len = newheadroom;
1139        else
1140                head_copy_off = newheadroom - head_copy_len;
1141
1142        /* Copy the linear header and data. */
1143        if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1144                          skb->len + head_copy_len))
1145                BUG();
1146
1147        copy_skb_header(n, skb);
1148
1149        off                  = newheadroom - oldheadroom;
1150        if (n->ip_summed == CHECKSUM_PARTIAL)
1151                n->csum_start += off;
1152#ifdef NET_SKBUFF_DATA_USES_OFFSET
1153        n->transport_header += off;
1154        n->network_header   += off;
1155        if (skb_mac_header_was_set(skb))
1156                n->mac_header += off;
1157#endif
1158
1159        return n;
1160}
1161EXPORT_SYMBOL(skb_copy_expand);
1162
1163/**
1164 *      skb_pad                 -       zero pad the tail of an skb
1165 *      @skb: buffer to pad
1166 *      @pad: space to pad
1167 *
1168 *      Ensure that a buffer is followed by a padding area that is zero
1169 *      filled. Used by network drivers which may DMA or transfer data
1170 *      beyond the buffer end onto the wire.
1171 *
1172 *      May return error in out of memory cases. The skb is freed on error.
1173 */
1174
1175int skb_pad(struct sk_buff *skb, int pad)
1176{
1177        int err;
1178        int ntail;
1179
1180        /* If the skbuff is non linear tailroom is always zero.. */
1181        if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
1182                memset(skb->data+skb->len, 0, pad);
1183                return 0;
1184        }
1185
1186        ntail = skb->data_len + pad - (skb->end - skb->tail);
1187        if (likely(skb_cloned(skb) || ntail > 0)) {
1188                err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1189                if (unlikely(err))
1190                        goto free_skb;
1191        }
1192
1193        /* FIXME: The use of this function with non-linear skb's really needs
1194         * to be audited.
1195         */
1196        err = skb_linearize(skb);
1197        if (unlikely(err))
1198                goto free_skb;
1199
1200        memset(skb->data + skb->len, 0, pad);
1201        return 0;
1202
1203free_skb:
1204        kfree_skb(skb);
1205        return err;
1206}
1207EXPORT_SYMBOL(skb_pad);
1208
1209/**
1210 *      skb_put - add data to a buffer
1211 *      @skb: buffer to use
1212 *      @len: amount of data to add
1213 *
1214 *      This function extends the used data area of the buffer. If this would
1215 *      exceed the total buffer size the kernel will panic. A pointer to the
1216 *      first byte of the extra data is returned.
1217 */
1218unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
1219{
1220        unsigned char *tmp = skb_tail_pointer(skb);
1221        SKB_LINEAR_ASSERT(skb);
1222        skb->tail += len;
1223        skb->len  += len;
1224        if (unlikely(skb->tail > skb->end))
1225                skb_over_panic(skb, len, __builtin_return_address(0));
1226        return tmp;
1227}
1228EXPORT_SYMBOL(skb_put);
1229
1230/**
1231 *      skb_push - add data to the start of a buffer
1232 *      @skb: buffer to use
1233 *      @len: amount of data to add
1234 *
1235 *      This function extends the used data area of the buffer at the buffer
1236 *      start. If this would exceed the total buffer headroom the kernel will
1237 *      panic. A pointer to the first byte of the extra data is returned.
1238 */
1239unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
1240{
1241        skb->data -= len;
1242        skb->len  += len;
1243        if (unlikely(skb->data<skb->head))
1244                skb_under_panic(skb, len, __builtin_return_address(0));
1245        return skb->data;
1246}
1247EXPORT_SYMBOL(skb_push);
1248
1249/**
1250 *      skb_pull - remove data from the start of a buffer
1251 *      @skb: buffer to use
1252 *      @len: amount of data to remove
1253 *
1254 *      This function removes data from the start of a buffer, returning
1255 *      the memory to the headroom. A pointer to the next data in the buffer
1256 *      is returned. Once the data has been pulled future pushes will overwrite
1257 *      the old data.
1258 */
1259unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
1260{
1261        return skb_pull_inline(skb, len);
1262}
1263EXPORT_SYMBOL(skb_pull);
1264
1265/**
1266 *      skb_trim - remove end from a buffer
1267 *      @skb: buffer to alter
1268 *      @len: new length
1269 *
1270 *      Cut the length of a buffer down by removing data from the tail. If
1271 *      the buffer is already under the length specified it is not modified.
1272 *      The skb must be linear.
1273 */
1274void skb_trim(struct sk_buff *skb, unsigned int len)
1275{
1276        if (skb->len > len)
1277                __skb_trim(skb, len);
1278}
1279EXPORT_SYMBOL(skb_trim);
1280
1281/* Trims skb to length len. It can change skb pointers.
1282 */
1283
1284int ___pskb_trim(struct sk_buff *skb, unsigned int len)
1285{
1286        struct sk_buff **fragp;
1287        struct sk_buff *frag;
1288        int offset = skb_headlen(skb);
1289        int nfrags = skb_shinfo(skb)->nr_frags;
1290        int i;
1291        int err;
1292
1293        if (skb_cloned(skb) &&
1294            unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1295                return err;
1296
1297        i = 0;
1298        if (offset >= len)
1299                goto drop_pages;
1300
1301        for (; i < nfrags; i++) {
1302                int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
1303
1304                if (end < len) {
1305                        offset = end;
1306                        continue;
1307                }
1308
1309                skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
1310
1311drop_pages:
1312                skb_shinfo(skb)->nr_frags = i;
1313
1314                for (; i < nfrags; i++)
1315                        skb_frag_unref(skb, i);
1316
1317                if (skb_has_frag_list(skb))
1318                        skb_drop_fraglist(skb);
1319                goto done;
1320        }
1321
1322        for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1323             fragp = &frag->next) {
1324                int end = offset + frag->len;
1325
1326                if (skb_shared(frag)) {
1327                        struct sk_buff *nfrag;
1328
1329                        nfrag = skb_clone(frag, GFP_ATOMIC);
1330                        if (unlikely(!nfrag))
1331                                return -ENOMEM;
1332
1333                        nfrag->next = frag->next;
1334                        consume_skb(frag);
1335                        frag = nfrag;
1336                        *fragp = frag;
1337                }
1338
1339                if (end < len) {
1340                        offset = end;
1341                        continue;
1342                }
1343
1344                if (end > len &&
1345                    unlikely((err = pskb_trim(frag, len - offset))))
1346                        return err;
1347
1348                if (frag->next)
1349                        skb_drop_list(&frag->next);
1350                break;
1351        }
1352
1353done:
1354        if (len > skb_headlen(skb)) {
1355                skb->data_len -= skb->len - len;
1356                skb->len       = len;
1357        } else {
1358                skb->len       = len;
1359                skb->data_len  = 0;
1360                skb_set_tail_pointer(skb, len);
1361        }
1362
1363        return 0;
1364}
1365EXPORT_SYMBOL(___pskb_trim);
1366
1367/**
1368 *      __pskb_pull_tail - advance tail of skb header
1369 *      @skb: buffer to reallocate
1370 *      @delta: number of bytes to advance tail
1371 *
1372 *      The function makes a sense only on a fragmented &sk_buff,
1373 *      it expands header moving its tail forward and copying necessary
1374 *      data from fragmented part.
1375 *
1376 *      &sk_buff MUST have reference count of 1.
1377 *
1378 *      Returns %NULL (and &sk_buff does not change) if pull failed
1379 *      or value of new tail of skb in the case of success.
1380 *
1381 *      All the pointers pointing into skb header may change and must be
1382 *      reloaded after call to this function.
1383 */
1384
1385/* Moves tail of skb head forward, copying data from fragmented part,
1386 * when it is necessary.
1387 * 1. It may fail due to malloc failure.
1388 * 2. It may change skb pointers.
1389 *
1390 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1391 */
1392unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
1393{
1394        /* If skb has not enough free space at tail, get new one
1395         * plus 128 bytes for future expansions. If we have enough
1396         * room at tail, reallocate without expansion only if skb is cloned.
1397         */
1398        int i, k, eat = (skb->tail + delta) - skb->end;
1399
1400        if (eat > 0 || skb_cloned(skb)) {
1401                if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1402                                     GFP_ATOMIC))
1403                        return NULL;
1404        }
1405
1406        if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
1407                BUG();
1408
1409        /* Optimization: no fragments, no reasons to preestimate
1410         * size of pulled pages. Superb.
1411         */
1412        if (!skb_has_frag_list(skb))
1413                goto pull_pages;
1414
1415        /* Estimate size of pulled pages. */
1416        eat = delta;
1417        for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1418                int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1419
1420                if (size >= eat)
1421                        goto pull_pages;
1422                eat -= size;
1423        }
1424
1425        /* If we need update frag list, we are in troubles.
1426         * Certainly, it possible to add an offset to skb data,
1427         * but taking into account that pulling is expected to
1428         * be very rare operation, it is worth to fight against
1429         * further bloating skb head and crucify ourselves here instead.
1430         * Pure masohism, indeed. 8)8)
1431         */
1432        if (eat) {
1433                struct sk_buff *list = skb_shinfo(skb)->frag_list;
1434                struct sk_buff *clone = NULL;
1435                struct sk_buff *insp = NULL;
1436
1437                do {
1438                        BUG_ON(!list);
1439
1440                        if (list->len <= eat) {
1441                                /* Eaten as whole. */
1442                                eat -= list->len;
1443                                list = list->next;
1444                                insp = list;
1445                        } else {
1446                                /* Eaten partially. */
1447
1448                                if (skb_shared(list)) {
1449                                        /* Sucks! We need to fork list. :-( */
1450                                        clone = skb_clone(list, GFP_ATOMIC);
1451                                        if (!clone)
1452                                                return NULL;
1453                                        insp = list->next;
1454                                        list = clone;
1455                                } else {
1456                                        /* This may be pulled without
1457                                         * problems. */
1458                                        insp = list;
1459                                }
1460                                if (!pskb_pull(list, eat)) {
1461                                        kfree_skb(clone);
1462                                        return NULL;
1463                                }
1464                                break;
1465                        }
1466                } while (eat);
1467
1468                /* Free pulled out fragments. */
1469                while ((list = skb_shinfo(skb)->frag_list) != insp) {
1470                        skb_shinfo(skb)->frag_list = list->next;
1471                        kfree_skb(list);
1472                }
1473                /* And insert new clone at head. */
1474                if (clone) {
1475                        clone->next = list;
1476                        skb_shinfo(skb)->frag_list = clone;
1477                }
1478        }
1479        /* Success! Now we may commit changes to skb data. */
1480
1481pull_pages:
1482        eat = delta;
1483        k = 0;
1484        for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1485                int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1486
1487                if (size <= eat) {
1488                        skb_frag_unref(skb, i);
1489                        eat -= size;
1490                } else {
1491                        skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1492                        if (eat) {
1493                                skb_shinfo(skb)->frags[k].page_offset += eat;
1494                                skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
1495                                eat = 0;
1496                        }
1497                        k++;
1498                }
1499        }
1500        skb_shinfo(skb)->nr_frags = k;
1501
1502        skb->tail     += delta;
1503        skb->data_len -= delta;
1504
1505        return skb_tail_pointer(skb);
1506}
1507EXPORT_SYMBOL(__pskb_pull_tail);
1508
1509/**
1510 *      skb_copy_bits - copy bits from skb to kernel buffer
1511 *      @skb: source skb
1512 *      @offset: offset in source
1513 *      @to: destination buffer
1514 *      @len: number of bytes to copy
1515 *
1516 *      Copy the specified number of bytes from the source skb to the
1517 *      destination buffer.
1518 *
1519 *      CAUTION ! :
1520 *              If its prototype is ever changed,
1521 *              check arch/{*}/net/{*}.S files,
1522 *              since it is called from BPF assembly code.
1523 */
1524int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1525{
1526        int start = skb_headlen(skb);
1527        struct sk_buff *frag_iter;
1528        int i, copy;
1529
1530        if (offset > (int)skb->len - len)
1531                goto fault;
1532
1533        /* Copy header. */
1534        if ((copy = start - offset) > 0) {
1535                if (copy > len)
1536                        copy = len;
1537                skb_copy_from_linear_data_offset(skb, offset, to, copy);
1538                if ((len -= copy) == 0)
1539                        return 0;
1540                offset += copy;
1541                to     += copy;
1542        }
1543
1544        for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1545                int end;
1546                skb_frag_t *f = &skb_shinfo(skb)->frags[i];
1547
1548                WARN_ON(start > offset + len);
1549
1550                end = start + skb_frag_size(f);
1551                if ((copy = end - offset) > 0) {
1552                        u8 *vaddr;
1553
1554                        if (copy > len)
1555                                copy = len;
1556
1557                        vaddr = kmap_atomic(skb_frag_page(f));
1558                        memcpy(to,
1559                               vaddr + f->page_offset + offset - start,
1560                               copy);
1561                        kunmap_atomic(vaddr);
1562
1563                        if ((len -= copy) == 0)
1564                                return 0;
1565                        offset += copy;
1566                        to     += copy;
1567                }
1568                start = end;
1569        }
1570
1571        skb_walk_frags(skb, frag_iter) {
1572                int end;
1573
1574                WARN_ON(start > offset + len);
1575
1576                end = start + frag_iter->len;
1577                if ((copy = end - offset) > 0) {
1578                        if (copy > len)
1579                                copy = len;
1580                        if (skb_copy_bits(frag_iter, offset - start, to, copy))
1581                                goto fault;
1582                        if ((len -= copy) == 0)
1583                                return 0;
1584                        offset += copy;
1585                        to     += copy;
1586                }
1587                start = end;
1588        }
1589
1590        if (!len)
1591                return 0;
1592
1593fault:
1594        return -EFAULT;
1595}
1596EXPORT_SYMBOL(skb_copy_bits);
1597
1598/*
1599 * Callback from splice_to_pipe(), if we need to release some pages
1600 * at the end of the spd in case we error'ed out in filling the pipe.
1601 */
1602static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1603{
1604        put_page(spd->pages[i]);
1605}
1606
1607static struct page *linear_to_page(struct page *page, unsigned int *len,
1608                                   unsigned int *offset,
1609                                   struct sk_buff *skb, struct sock *sk)
1610{
1611        struct page *p = sk->sk_sndmsg_page;
1612        unsigned int off;
1613
1614        if (!p) {
1615new_page:
1616                p = sk->sk_sndmsg_page = alloc_pages(sk->sk_allocation, 0);
1617                if (!p)
1618                        return NULL;
1619
1620                off = sk->sk_sndmsg_off = 0;
1621                /* hold one ref to this page until it's full */
1622        } else {
1623                unsigned int mlen;
1624
1625                /* If we are the only user of the page, we can reset offset */
1626                if (page_count(p) == 1)
1627                        sk->sk_sndmsg_off = 0;
1628                off = sk->sk_sndmsg_off;
1629                mlen = PAGE_SIZE - off;
1630                if (mlen < 64 && mlen < *len) {
1631                        put_page(p);
1632                        goto new_page;
1633                }
1634
1635                *len = min_t(unsigned int, *len, mlen);
1636        }
1637
1638        memcpy(page_address(p) + off, page_address(page) + *offset, *len);
1639        sk->sk_sndmsg_off += *len;
1640        *offset = off;
1641
1642        return p;
1643}
1644
1645static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
1646                             struct page *page,
1647                             unsigned int offset)
1648{
1649        return  spd->nr_pages &&
1650                spd->pages[spd->nr_pages - 1] == page &&
1651                (spd->partial[spd->nr_pages - 1].offset +
1652                 spd->partial[spd->nr_pages - 1].len == offset);
1653}
1654
1655/*
1656 * Fill page/offset/length into spd, if it can hold more pages.
1657 */
1658static bool spd_fill_page(struct splice_pipe_desc *spd,
1659                          struct pipe_inode_info *pipe, struct page *page,
1660                          unsigned int *len, unsigned int offset,
1661                          struct sk_buff *skb, bool linear,
1662                          struct sock *sk)
1663{
1664        if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
1665                return true;
1666
1667        if (linear) {
1668                page = linear_to_page(page, len, &offset, skb, sk);
1669                if (!page)
1670                        return true;
1671        }
1672        if (spd_can_coalesce(spd, page, offset)) {
1673                spd->partial[spd->nr_pages - 1].len += *len;
1674                return false;
1675        }
1676        get_page(page);
1677        spd->pages[spd->nr_pages] = page;
1678        spd->partial[spd->nr_pages].len = *len;
1679        spd->partial[spd->nr_pages].offset = offset;
1680        spd->nr_pages++;
1681
1682        return false;
1683}
1684
1685static inline void __segment_seek(struct page **page, unsigned int *poff,
1686                                  unsigned int *plen, unsigned int off)
1687{
1688        unsigned long n;
1689
1690        *poff += off;
1691        n = *poff / PAGE_SIZE;
1692        if (n)
1693                *page = nth_page(*page, n);
1694
1695        *poff = *poff % PAGE_SIZE;
1696        *plen -= off;
1697}
1698
1699static bool __splice_segment(struct page *page, unsigned int poff,
1700                             unsigned int plen, unsigned int *off,
1701                             unsigned int *len, struct sk_buff *skb,
1702                             struct splice_pipe_desc *spd, bool linear,
1703                             struct sock *sk,
1704                             struct pipe_inode_info *pipe)
1705{
1706        if (!*len)
1707                return true;
1708
1709        /* skip this segment if already processed */
1710        if (*off >= plen) {
1711                *off -= plen;
1712                return false;
1713        }
1714
1715        /* ignore any bits we already processed */
1716        if (*off) {
1717                __segment_seek(&page, &poff, &plen, *off);
1718                *off = 0;
1719        }
1720
1721        do {
1722                unsigned int flen = min(*len, plen);
1723
1724                /* the linear region may spread across several pages  */
1725                flen = min_t(unsigned int, flen, PAGE_SIZE - poff);
1726
1727                if (spd_fill_page(spd, pipe, page, &flen, poff, skb, linear, sk))
1728                        return true;
1729
1730                __segment_seek(&page, &poff, &plen, flen);
1731                *len -= flen;
1732
1733        } while (*len && plen);
1734
1735        return false;
1736}
1737
1738/*
1739 * Map linear and fragment data from the skb to spd. It reports true if the
1740 * pipe is full or if we already spliced the requested length.
1741 */
1742static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
1743                              unsigned int *offset, unsigned int *len,
1744                              struct splice_pipe_desc *spd, struct sock *sk)
1745{
1746        int seg;
1747
1748        /* map the linear part :
1749         * If skb->head_frag is set, this 'linear' part is backed by a
1750         * fragment, and if the head is not shared with any clones then
1751         * we can avoid a copy since we own the head portion of this page.
1752         */
1753        if (__splice_segment(virt_to_page(skb->data),
1754                             (unsigned long) skb->data & (PAGE_SIZE - 1),
1755                             skb_headlen(skb),
1756                             offset, len, skb, spd,
1757                             skb_head_is_locked(skb),
1758                             sk, pipe))
1759                return true;
1760
1761        /*
1762         * then map the fragments
1763         */
1764        for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1765                const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1766
1767                if (__splice_segment(skb_frag_page(f),
1768                                     f->page_offset, skb_frag_size(f),
1769                                     offset, len, skb, spd, false, sk, pipe))
1770                        return true;
1771        }
1772
1773        return false;
1774}
1775
1776/*
1777 * Map data from the skb to a pipe. Should handle both the linear part,
1778 * the fragments, and the frag list. It does NOT handle frag lists within
1779 * the frag list, if such a thing exists. We'd probably need to recurse to
1780 * handle that cleanly.
1781 */
1782int skb_splice_bits(struct sk_buff *skb, unsigned int offset,
1783                    struct pipe_inode_info *pipe, unsigned int tlen,
1784                    unsigned int flags)
1785{
1786        struct partial_page partial[MAX_SKB_FRAGS];
1787        struct page *pages[MAX_SKB_FRAGS];
1788        struct splice_pipe_desc spd = {
1789                .pages = pages,
1790                .partial = partial,
1791                .nr_pages_max = MAX_SKB_FRAGS,
1792                .flags = flags,
1793                .ops = &sock_pipe_buf_ops,
1794                .spd_release = sock_spd_release,
1795        };
1796        struct sk_buff *frag_iter;
1797        struct sock *sk = skb->sk;
1798        int ret = 0;
1799
1800        /*
1801         * __skb_splice_bits() only fails if the output has no room left,
1802         * so no point in going over the frag_list for the error case.
1803         */
1804        if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk))
1805                goto done;
1806        else if (!tlen)
1807                goto done;
1808
1809        /*
1810         * now see if we have a frag_list to map
1811         */
1812        skb_walk_frags(skb, frag_iter) {
1813                if (!tlen)
1814                        break;
1815                if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk))
1816                        break;
1817        }
1818
1819done:
1820        if (spd.nr_pages) {
1821                /*
1822                 * Drop the socket lock, otherwise we have reverse
1823                 * locking dependencies between sk_lock and i_mutex
1824                 * here as compared to sendfile(). We enter here
1825                 * with the socket lock held, and splice_to_pipe() will
1826                 * grab the pipe inode lock. For sendfile() emulation,
1827                 * we call into ->sendpage() with the i_mutex lock held
1828                 * and networking will grab the socket lock.
1829                 */
1830                release_sock(sk);
1831                ret = splice_to_pipe(pipe, &spd);
1832                lock_sock(sk);
1833        }
1834
1835        return ret;
1836}
1837
1838/**
1839 *      skb_store_bits - store bits from kernel buffer to skb
1840 *      @skb: destination buffer
1841 *      @offset: offset in destination
1842 *      @from: source buffer
1843 *      @len: number of bytes to copy
1844 *
1845 *      Copy the specified number of bytes from the source buffer to the
1846 *      destination skb.  This function handles all the messy bits of
1847 *      traversing fragment lists and such.
1848 */
1849
1850int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
1851{
1852        int start = skb_headlen(skb);
1853        struct sk_buff *frag_iter;
1854        int i, copy;
1855
1856        if (offset > (int)skb->len - len)
1857                goto fault;
1858
1859        if ((copy = start - offset) > 0) {
1860                if (copy > len)
1861                        copy = len;
1862                skb_copy_to_linear_data_offset(skb, offset, from, copy);
1863                if ((len -= copy) == 0)
1864                        return 0;
1865                offset += copy;
1866                from += copy;
1867        }
1868
1869        for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1870                skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1871                int end;
1872
1873                WARN_ON(start > offset + len);
1874
1875                end = start + skb_frag_size(frag);
1876                if ((copy = end - offset) > 0) {
1877                        u8 *vaddr;
1878
1879                        if (copy > len)
1880                                copy = len;
1881
1882                        vaddr = kmap_atomic(skb_frag_page(frag));
1883                        memcpy(vaddr + frag->page_offset + offset - start,
1884                               from, copy);
1885                        kunmap_atomic(vaddr);
1886
1887                        if ((len -= copy) == 0)
1888                                return 0;
1889                        offset += copy;
1890                        from += copy;
1891                }
1892                start = end;
1893        }
1894
1895        skb_walk_frags(skb, frag_iter) {
1896                int end;
1897
1898                WARN_ON(start > offset + len);
1899
1900                end = start + frag_iter->len;
1901                if ((copy = end - offset) > 0) {
1902                        if (copy > len)
1903                                copy = len;
1904                        if (skb_store_bits(frag_iter, offset - start,
1905                                           from, copy))
1906                                goto fault;
1907                        if ((len -= copy) == 0)
1908                                return 0;
1909                        offset += copy;
1910                        from += copy;
1911                }
1912                start = end;
1913        }
1914        if (!len)
1915                return 0;
1916
1917fault:
1918        return -EFAULT;
1919}
1920EXPORT_SYMBOL(skb_store_bits);
1921
1922/* Checksum skb data. */
1923
1924__wsum skb_checksum(const struct sk_buff *skb, int offset,
1925                          int len, __wsum csum)
1926{
1927        int start = skb_headlen(skb);
1928        int i, copy = start - offset;
1929        struct sk_buff *frag_iter;
1930        int pos = 0;
1931
1932        /* Checksum header. */
1933        if (copy > 0) {
1934                if (copy > len)
1935                        copy = len;
1936                csum = csum_partial(skb->data + offset, copy, csum);
1937                if ((len -= copy) == 0)
1938                        return csum;
1939                offset += copy;
1940                pos     = copy;
1941        }
1942
1943        for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1944                int end;
1945                skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1946
1947                WARN_ON(start > offset + len);
1948
1949                end = start + skb_frag_size(frag);
1950                if ((copy = end - offset) > 0) {
1951                        __wsum csum2;
1952                        u8 *vaddr;
1953
1954                        if (copy > len)
1955                                copy = len;
1956                        vaddr = kmap_atomic(skb_frag_page(frag));
1957                        csum2 = csum_partial(vaddr + frag->page_offset +
1958                                             offset - start, copy, 0);
1959                        kunmap_atomic(vaddr);
1960                        csum = csum_block_add(csum, csum2, pos);
1961                        if (!(len -= copy))
1962                                return csum;
1963                        offset += copy;
1964                        pos    += copy;
1965                }
1966                start = end;
1967        }
1968
1969        skb_walk_frags(skb, frag_iter) {
1970                int end;
1971
1972                WARN_ON(start > offset + len);
1973
1974                end = start + frag_iter->len;
1975                if ((copy = end - offset) > 0) {
1976                        __wsum csum2;
1977                        if (copy > len)
1978                                copy = len;
1979                        csum2 = skb_checksum(frag_iter, offset - start,
1980                                             copy, 0);
1981                        csum = csum_block_add(csum, csum2, pos);
1982                        if ((len -= copy) == 0)
1983                                return csum;
1984                        offset += copy;
1985                        pos    += copy;
1986                }
1987                start = end;
1988        }
1989        BUG_ON(len);
1990
1991        return csum;
1992}
1993EXPORT_SYMBOL(skb_checksum);
1994
1995/* Both of above in one bottle. */
1996
1997__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1998                                    u8 *to, int len, __wsum csum)
1999{
2000        int start = skb_headlen(skb);
2001        int i, copy = start - offset;
2002        struct sk_buff *frag_iter;
2003        int pos = 0;
2004
2005        /* Copy header. */
2006        if (copy > 0) {
2007                if (copy > len)
2008                        copy = len;
2009                csum = csum_partial_copy_nocheck(skb->data + offset, to,
2010                                                 copy, csum);
2011                if ((len -= copy) == 0)
2012                        return csum;
2013                offset += copy;
2014                to     += copy;
2015                pos     = copy;
2016        }
2017
2018        for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2019                int end;
2020
2021                WARN_ON(start > offset + len);
2022
2023                end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
2024                if ((copy = end - offset) > 0) {
2025                        __wsum csum2;
2026                        u8 *vaddr;
2027                        skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2028
2029                        if (copy > len)
2030                                copy = len;
2031                        vaddr = kmap_atomic(skb_frag_page(frag));
2032                        csum2 = csum_partial_copy_nocheck(vaddr +
2033                                                          frag->page_offset +
2034                                                          offset - start, to,
2035                                                          copy, 0);
2036                        kunmap_atomic(vaddr);
2037                        csum = csum_block_add(csum, csum2, pos);
2038                        if (!(len -= copy))
2039                                return csum;
2040                        offset += copy;
2041                        to     += copy;
2042                        pos    += copy;
2043                }
2044                start = end;
2045        }
2046
2047        skb_walk_frags(skb, frag_iter) {
2048                __wsum csum2;
2049                int end;
2050
2051                WARN_ON(start > offset + len);
2052
2053                end = start + frag_iter->len;
2054                if ((copy = end - offset) > 0) {
2055                        if (copy > len)
2056                                copy = len;
2057                        csum2 = skb_copy_and_csum_bits(frag_iter,
2058                                                       offset - start,
2059                                                       to, copy, 0);
2060                        csum = csum_block_add(csum, csum2, pos);
2061                        if ((len -= copy) == 0)
2062                                return csum;
2063                        offset += copy;
2064                        to     += copy;
2065                        pos    += copy;
2066                }
2067                start = end;
2068        }
2069        BUG_ON(len);
2070        return csum;
2071}
2072EXPORT_SYMBOL(skb_copy_and_csum_bits);
2073
2074void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
2075{
2076        __wsum csum;
2077        long csstart;
2078
2079        if (skb->ip_summed == CHECKSUM_PARTIAL)
2080                csstart = skb_checksum_start_offset(skb);
2081        else
2082                csstart = skb_headlen(skb);
2083
2084        BUG_ON(csstart > skb_headlen(skb));
2085
2086        skb_copy_from_linear_data(skb, to, csstart);
2087
2088        csum = 0;
2089        if (csstart != skb->len)
2090                csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
2091                                              skb->len - csstart, 0);
2092
2093        if (skb->ip_summed == CHECKSUM_PARTIAL) {
2094                long csstuff = csstart + skb->csum_offset;
2095
2096                *((__sum16 *)(to + csstuff)) = csum_fold(csum);
2097        }
2098}
2099EXPORT_SYMBOL(skb_copy_and_csum_dev);
2100
2101/**
2102 *      skb_dequeue - remove from the head of the queue
2103 *      @list: list to dequeue from
2104 *
2105 *      Remove the head of the list. The list lock is taken so the function
2106 *      may be used safely with other locking list functions. The head item is
2107 *      returned or %NULL if the list is empty.
2108 */
2109
2110struct sk_buff *skb_dequeue(struct sk_buff_head *list)
2111{
2112        unsigned long flags;
2113        struct sk_buff *result;
2114
2115        spin_lock_irqsave(&list->lock, flags);
2116        result = __skb_dequeue(list);
2117        spin_unlock_irqrestore(&list->lock, flags);
2118        return result;
2119}
2120EXPORT_SYMBOL(skb_dequeue);
2121
2122/**
2123 *      skb_dequeue_tail - remove from the tail of the queue
2124 *      @list: list to dequeue from
2125 *
2126 *      Remove the tail of the list. The list lock is taken so the function
2127 *      may be used safely with other locking list functions. The tail item is
2128 *      returned or %NULL if the list is empty.
2129 */
2130struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
2131{
2132        unsigned long flags;
2133        struct sk_buff *result;
2134
2135        spin_lock_irqsave(&list->lock, flags);
2136        result = __skb_dequeue_tail(list);
2137        spin_unlock_irqrestore(&list->lock, flags);
2138        return result;
2139}
2140EXPORT_SYMBOL(skb_dequeue_tail);
2141
2142/**
2143 *      skb_queue_purge - empty a list
2144 *      @list: list to empty
2145 *
2146 *      Delete all buffers on an &sk_buff list. Each buffer is removed from
2147 *      the list and one reference dropped. This function takes the list
2148 *      lock and is atomic with respect to other list locking functions.
2149 */
2150void skb_queue_purge(struct sk_buff_head *list)
2151{
2152        struct sk_buff *skb;
2153        while ((skb = skb_dequeue(list)) != NULL)
2154                kfree_skb(skb);
2155}
2156EXPORT_SYMBOL(skb_queue_purge);
2157
2158/**
2159 *      skb_queue_head - queue a buffer at the list head
2160 *      @list: list to use
2161 *      @newsk: buffer to queue
2162 *
2163 *      Queue a buffer at the start of the list. This function takes the
2164 *      list lock and can be used safely with other locking &sk_buff functions
2165 *      safely.
2166 *
2167 *      A buffer cannot be placed on two lists at the same time.
2168 */
2169void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
2170{
2171        unsigned long flags;
2172
2173        spin_lock_irqsave(&list->lock, flags);
2174        __skb_queue_head(list, newsk);
2175        spin_unlock_irqrestore(&list->lock, flags);
2176}
2177EXPORT_SYMBOL(skb_queue_head);
2178
2179/**
2180 *      skb_queue_tail - queue a buffer at the list tail
2181 *      @list: list to use
2182 *      @newsk: buffer to queue
2183 *
2184 *      Queue a buffer at the tail of the list. This function takes the
2185 *      list lock and can be used safely with other locking &sk_buff functions
2186 *      safely.
2187 *
2188 *      A buffer cannot be placed on two lists at the same time.
2189 */
2190void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
2191{
2192        unsigned long flags;
2193
2194        spin_lock_irqsave(&list->lock, flags);
2195        __skb_queue_tail(list, newsk);
2196        spin_unlock_irqrestore(&list->lock, flags);
2197}
2198EXPORT_SYMBOL(skb_queue_tail);
2199
2200/**
2201 *      skb_unlink      -       remove a buffer from a list
2202 *      @skb: buffer to remove
2203 *      @list: list to use
2204 *
2205 *      Remove a packet from a list. The list locks are taken and this
2206 *      function is atomic with respect to other list locked calls
2207 *
2208 *      You must know what list the SKB is on.
2209 */
2210void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
2211{
2212        unsigned long flags;
2213
2214        spin_lock_irqsave(&list->lock, flags);
2215        __skb_unlink(skb, list);
2216        spin_unlock_irqrestore(&list->lock, flags);
2217}
2218EXPORT_SYMBOL(skb_unlink);
2219
2220/**
2221 *      skb_append      -       append a buffer
2222 *      @old: buffer to insert after
2223 *      @newsk: buffer to insert
2224 *      @list: list to use
2225 *
2226 *      Place a packet after a given packet in a list. The list locks are taken
2227 *      and this function is atomic with respect to other list locked calls.
2228 *      A buffer cannot be placed on two lists at the same time.
2229 */
2230void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
2231{
2232        unsigned long flags;
2233
2234        spin_lock_irqsave(&list->lock, flags);
2235        __skb_queue_after(list, old, newsk);
2236        spin_unlock_irqrestore(&list->lock, flags);
2237}
2238EXPORT_SYMBOL(skb_append);
2239
2240/**
2241 *      skb_insert      -       insert a buffer
2242 *      @old: buffer to insert before
2243 *      @newsk: buffer to insert
2244 *      @list: list to use
2245 *
2246 *      Place a packet before a given packet in a list. The list locks are
2247 *      taken and this function is atomic with respect to other list locked
2248 *      calls.
2249 *
2250 *      A buffer cannot be placed on two lists at the same time.
2251 */
2252void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
2253{
2254        unsigned long flags;
2255
2256        spin_lock_irqsave(&list->lock, flags);
2257        __skb_insert(newsk, old->prev, old, list);
2258        spin_unlock_irqrestore(&list->lock, flags);
2259}
2260EXPORT_SYMBOL(skb_insert);
2261
2262static inline void skb_split_inside_header(struct sk_buff *skb,
2263                                           struct sk_buff* skb1,
2264                                           const u32 len, const int pos)
2265{
2266        int i;
2267
2268        skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
2269                                         pos - len);
2270        /* And move data appendix as is. */
2271        for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
2272                skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
2273
2274        skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
2275        skb_shinfo(skb)->nr_frags  = 0;
2276        skb1->data_len             = skb->data_len;
2277        skb1->len                  += skb1->data_len;
2278        skb->data_len              = 0;
2279        skb->len                   = len;
2280        skb_set_tail_pointer(skb, len);
2281}
2282
2283static inline void skb_split_no_header(struct sk_buff *skb,
2284                                       struct sk_buff* skb1,
2285                                       const u32 len, int pos)
2286{
2287        int i, k = 0;
2288        const int nfrags = skb_shinfo(skb)->nr_frags;
2289
2290        skb_shinfo(skb)->nr_frags = 0;
2291        skb1->len                 = skb1->data_len = skb->len - len;
2292        skb->len                  = len;
2293        skb->data_len             = len - pos;
2294
2295        for (i = 0; i < nfrags; i++) {
2296                int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
2297
2298                if (pos + size > len) {
2299                        skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
2300
2301                        if (pos < len) {
2302                                /* Split frag.
2303                                 * We have two variants in this case:
2304                                 * 1. Move all the frag to the second
2305                                 *    part, if it is possible. F.e.
2306                                 *    this approach is mandatory for TUX,
2307                                 *    where splitting is expensive.
2308                                 * 2. Split is accurately. We make this.
2309                                 */
2310                                skb_frag_ref(skb, i);
2311                                skb_shinfo(skb1)->frags[0].page_offset += len - pos;
2312                                skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
2313                                skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
2314                                skb_shinfo(skb)->nr_frags++;
2315                        }
2316                        k++;
2317                } else
2318                        skb_shinfo(skb)->nr_frags++;
2319                pos += size;
2320        }
2321        skb_shinfo(skb1)->nr_frags = k;
2322}
2323
2324/**
2325 * skb_split - Split fragmented skb to two parts at length len.
2326 * @skb: the buffer to split
2327 * @skb1: the buffer to receive the second part
2328 * @len: new length for skb
2329 */
2330void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
2331{
2332        int pos = skb_headlen(skb);
2333
2334        if (len < pos)  /* Split line is inside header. */
2335                skb_split_inside_header(skb, skb1, len, pos);
2336        else            /* Second chunk has no header, nothing to copy. */
2337                skb_split_no_header(skb, skb1, len, pos);
2338}
2339EXPORT_SYMBOL(skb_split);
2340
2341/* Shifting from/to a cloned skb is a no-go.
2342 *
2343 * Caller cannot keep skb_shinfo related pointers past calling here!
2344 */
2345static int skb_prepare_for_shift(struct sk_buff *skb)
2346{
2347        return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2348}
2349
2350/**
2351 * skb_shift - Shifts paged data partially from skb to another
2352 * @tgt: buffer into which tail data gets added
2353 * @skb: buffer from which the paged data comes from
2354 * @shiftlen: shift up to this many bytes
2355 *
2356 * Attempts to shift up to shiftlen worth of bytes, which may be less than
2357 * the length of the skb, from skb to tgt. Returns number bytes shifted.
2358 * It's up to caller to free skb if everything was shifted.
2359 *
2360 * If @tgt runs out of frags, the whole operation is aborted.
2361 *
2362 * Skb cannot include anything else but paged data while tgt is allowed
2363 * to have non-paged data as well.
2364 *
2365 * TODO: full sized shift could be optimized but that would need
2366 * specialized skb free'er to handle frags without up-to-date nr_frags.
2367 */
2368int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2369{
2370        int from, to, merge, todo;
2371        struct skb_frag_struct *fragfrom, *fragto;
2372
2373        BUG_ON(shiftlen > skb->len);
2374        BUG_ON(skb_headlen(skb));       /* Would corrupt stream */
2375
2376        todo = shiftlen;
2377        from = 0;
2378        to = skb_shinfo(tgt)->nr_frags;
2379        fragfrom = &skb_shinfo(skb)->frags[from];
2380
2381        /* Actual merge is delayed until the point when we know we can
2382         * commit all, so that we don't have to undo partial changes
2383         */
2384        if (!to ||
2385            !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
2386                              fragfrom->page_offset)) {
2387                merge = -1;
2388        } else {
2389                merge = to - 1;
2390
2391                todo -= skb_frag_size(fragfrom);
2392                if (todo < 0) {
2393                        if (skb_prepare_for_shift(skb) ||
2394                            skb_prepare_for_shift(tgt))
2395                                return 0;
2396
2397                        /* All previous frag pointers might be stale! */
2398                        fragfrom = &skb_shinfo(skb)->frags[from];
2399                        fragto = &skb_shinfo(tgt)->frags[merge];
2400
2401                        skb_frag_size_add(fragto, shiftlen);
2402                        skb_frag_size_sub(fragfrom, shiftlen);
2403                        fragfrom->page_offset += shiftlen;
2404
2405                        goto onlymerged;
2406                }
2407
2408                from++;
2409        }
2410
2411        /* Skip full, not-fitting skb to avoid expensive operations */
2412        if ((shiftlen == skb->len) &&
2413            (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2414                return 0;
2415
2416        if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2417                return 0;
2418
2419        while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2420                if (to == MAX_SKB_FRAGS)
2421                        return 0;
2422
2423                fragfrom = &skb_shinfo(skb)->frags[from];
2424                fragto = &skb_shinfo(tgt)->frags[to];
2425
2426                if (todo >= skb_frag_size(fragfrom)) {
2427                        *fragto = *fragfrom;
2428                        todo -= skb_frag_size(fragfrom);
2429                        from++;
2430                        to++;
2431
2432                } else {
2433                        __skb_frag_ref(fragfrom);
2434                        fragto->page = fragfrom->page;
2435                        fragto->page_offset = fragfrom->page_offset;
2436                        skb_frag_size_set(fragto, todo);
2437
2438                        fragfrom->page_offset += todo;
2439                        skb_frag_size_sub(fragfrom, todo);
2440                        todo = 0;
2441
2442                        to++;
2443                        break;
2444                }
2445        }
2446
2447        /* Ready to "commit" this state change to tgt */
2448        skb_shinfo(tgt)->nr_frags = to;
2449
2450        if (merge >= 0) {
2451                fragfrom = &skb_shinfo(skb)->frags[0];
2452                fragto = &skb_shinfo(tgt)->frags[merge];
2453
2454                skb_frag_size_add(fragto, skb_frag_size(fragfrom));
2455                __skb_frag_unref(fragfrom);
2456        }
2457
2458        /* Reposition in the original skb */
2459        to = 0;
2460        while (from < skb_shinfo(skb)->nr_frags)
2461                skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2462        skb_shinfo(skb)->nr_frags = to;
2463
2464        BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2465
2466onlymerged:
2467        /* Most likely the tgt won't ever need its checksum anymore, skb on
2468         * the other hand might need it if it needs to be resent
2469         */
2470        tgt->ip_summed = CHECKSUM_PARTIAL;
2471        skb->ip_summed = CHECKSUM_PARTIAL;
2472
2473        /* Yak, is it really working this way? Some helper please? */
2474        skb->len -= shiftlen;
2475        skb->data_len -= shiftlen;
2476        skb->truesize -= shiftlen;
2477        tgt->len += shiftlen;
2478        tgt->data_len += shiftlen;
2479        tgt->truesize += shiftlen;
2480
2481        return shiftlen;
2482}
2483
2484/**
2485 * skb_prepare_seq_read - Prepare a sequential read of skb data
2486 * @skb: the buffer to read
2487 * @from: lower offset of data to be read
2488 * @to: upper offset of data to be read
2489 * @st: state variable
2490 *
2491 * Initializes the specified state variable. Must be called before
2492 * invoking skb_seq_read() for the first time.
2493 */
2494void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2495                          unsigned int to, struct skb_seq_state *st)
2496{
2497        st->lower_offset = from;
2498        st->upper_offset = to;
2499        st->root_skb = st->cur_skb = skb;
2500        st->frag_idx = st->stepped_offset = 0;
2501        st->frag_data = NULL;
2502}
2503EXPORT_SYMBOL(skb_prepare_seq_read);
2504
2505/**
2506 * skb_seq_read - Sequentially read skb data
2507 * @consumed: number of bytes consumed by the caller so far
2508 * @data: destination pointer for data to be returned
2509 * @st: state variable
2510 *
2511 * Reads a block of skb data at &consumed relative to the
2512 * lower offset specified to skb_prepare_seq_read(). Assigns
2513 * the head of the data block to &data and returns the length
2514 * of the block or 0 if the end of the skb data or the upper
2515 * offset has been reached.
2516 *
2517 * The caller is not required to consume all of the data
2518 * returned, i.e. &consumed is typically set to the number
2519 * of bytes already consumed and the next call to
2520 * skb_seq_read() will return the remaining part of the block.
2521 *
2522 * Note 1: The size of each block of data returned can be arbitrary,
2523 *       this limitation is the cost for zerocopy seqeuental
2524 *       reads of potentially non linear data.
2525 *
2526 * Note 2: Fragment lists within fragments are not implemented
2527 *       at the moment, state->root_skb could be replaced with
2528 *       a stack for this purpose.
2529 */
2530unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2531                          struct skb_seq_state *st)
2532{
2533        unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2534        skb_frag_t *frag;
2535
2536        if (unlikely(abs_offset >= st->upper_offset))
2537                return 0;
2538
2539next_skb:
2540        block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
2541
2542        if (abs_offset < block_limit && !st->frag_data) {
2543                *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
2544                return block_limit - abs_offset;
2545        }
2546
2547        if (st->frag_idx == 0 && !st->frag_data)
2548                st->stepped_offset += skb_headlen(st->cur_skb);
2549
2550        while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2551                frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
2552                block_limit = skb_frag_size(frag) + st->stepped_offset;
2553
2554                if (abs_offset < block_limit) {
2555                        if (!st->frag_data)
2556                                st->frag_data = kmap_atomic(skb_frag_page(frag));
2557
2558                        *data = (u8 *) st->frag_data + frag->page_offset +
2559                                (abs_offset - st->stepped_offset);
2560
2561                        return block_limit - abs_offset;
2562                }
2563
2564                if (st->frag_data) {
2565                        kunmap_atomic(st->frag_data);
2566                        st->frag_data = NULL;
2567                }
2568
2569                st->frag_idx++;
2570                st->stepped_offset += skb_frag_size(frag);
2571        }
2572
2573        if (st->frag_data) {
2574                kunmap_atomic(st->frag_data);
2575                st->frag_data = NULL;
2576        }
2577
2578        if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
2579                st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
2580                st->frag_idx = 0;
2581                goto next_skb;
2582        } else if (st->cur_skb->next) {
2583                st->cur_skb = st->cur_skb->next;
2584                st->frag_idx = 0;
2585                goto next_skb;
2586        }
2587
2588        return 0;
2589}
2590EXPORT_SYMBOL(skb_seq_read);
2591
2592/**
2593 * skb_abort_seq_read - Abort a sequential read of skb data
2594 * @st: state variable
2595 *
2596 * Must be called if skb_seq_read() was not called until it
2597 * returned 0.
2598 */
2599void skb_abort_seq_read(struct skb_seq_state *st)
2600{
2601        if (st->frag_data)
2602                kunmap_atomic(st->frag_data);
2603}
2604EXPORT_SYMBOL(skb_abort_seq_read);
2605
2606#define TS_SKB_CB(state)        ((struct skb_seq_state *) &((state)->cb))
2607
2608static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2609                                          struct ts_config *conf,
2610                                          struct ts_state *state)
2611{
2612        return skb_seq_read(offset, text, TS_SKB_CB(state));
2613}
2614
2615static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2616{
2617        skb_abort_seq_read(TS_SKB_CB(state));
2618}
2619
2620/**
2621 * skb_find_text - Find a text pattern in skb data
2622 * @skb: the buffer to look in
2623 * @from: search offset
2624 * @to: search limit
2625 * @config: textsearch configuration
2626 * @state: uninitialized textsearch state variable
2627 *
2628 * Finds a pattern in the skb data according to the specified
2629 * textsearch configuration. Use textsearch_next() to retrieve
2630 * subsequent occurrences of the pattern. Returns the offset
2631 * to the first occurrence or UINT_MAX if no match was found.
2632 */
2633unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
2634                           unsigned int to, struct ts_config *config,
2635                           struct ts_state *state)
2636{
2637        unsigned int ret;
2638
2639        config->get_next_block = skb_ts_get_next_block;
2640        config->finish = skb_ts_finish;
2641
2642        skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
2643
2644        ret = textsearch_find(config, state);
2645        return (ret <= to - from ? ret : UINT_MAX);
2646}
2647EXPORT_SYMBOL(skb_find_text);
2648
2649/**
2650 * skb_append_datato_frags - append the user data to a skb
2651 * @sk: sock  structure
2652 * @skb: skb structure to be appened with user data.
2653 * @getfrag: call back function to be used for getting the user data
2654 * @from: pointer to user message iov
2655 * @length: length of the iov message
2656 *
2657 * Description: This procedure append the user data in the fragment part
2658 * of the skb if any page alloc fails user this procedure returns  -ENOMEM
2659 */
2660int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
2661                        int (*getfrag)(void *from, char *to, int offset,
2662                                        int len, int odd, struct sk_buff *skb),
2663                        void *from, int length)
2664{
2665        int frg_cnt = 0;
2666        skb_frag_t *frag = NULL;
2667        struct page *page = NULL;
2668        int copy, left;
2669        int offset = 0;
2670        int ret;
2671
2672        do {
2673                /* Return error if we don't have space for new frag */
2674                frg_cnt = skb_shinfo(skb)->nr_frags;
2675                if (frg_cnt >= MAX_SKB_FRAGS)
2676                        return -EFAULT;
2677
2678                /* allocate a new page for next frag */
2679                page = alloc_pages(sk->sk_allocation, 0);
2680
2681                /* If alloc_page fails just return failure and caller will
2682                 * free previous allocated pages by doing kfree_skb()
2683                 */
2684                if (page == NULL)
2685                        return -ENOMEM;
2686
2687                /* initialize the next frag */
2688                skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
2689                skb->truesize += PAGE_SIZE;
2690                atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
2691
2692                /* get the new initialized frag */
2693                frg_cnt = skb_shinfo(skb)->nr_frags;
2694                frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
2695
2696                /* copy the user data to page */
2697                left = PAGE_SIZE - frag->page_offset;
2698                copy = (length > left)? left : length;
2699
2700                ret = getfrag(from, skb_frag_address(frag) + skb_frag_size(frag),
2701                            offset, copy, 0, skb);
2702                if (ret < 0)
2703                        return -EFAULT;
2704
2705                /* copy was successful so update the size parameters */
2706                skb_frag_size_add(frag, copy);
2707                skb->len += copy;
2708                skb->data_len += copy;
2709                offset += copy;
2710                length -= copy;
2711
2712        } while (length > 0);
2713
2714        return 0;
2715}
2716EXPORT_SYMBOL(skb_append_datato_frags);
2717
2718/**
2719 *      skb_pull_rcsum - pull skb and update receive checksum
2720 *      @skb: buffer to update
2721 *      @len: length of data pulled
2722 *
2723 *      This function performs an skb_pull on the packet and updates
2724 *      the CHECKSUM_COMPLETE checksum.  It should be used on
2725 *      receive path processing instead of skb_pull unless you know
2726 *      that the checksum difference is zero (e.g., a valid IP header)
2727 *      or you are setting ip_summed to CHECKSUM_NONE.
2728 */
2729unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2730{
2731        BUG_ON(len > skb->len);
2732        skb->len -= len;
2733        BUG_ON(skb->len < skb->data_len);
2734        skb_postpull_rcsum(skb, skb->data, len);
2735        return skb->data += len;
2736}
2737EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2738
2739/**
2740 *      skb_segment - Perform protocol segmentation on skb.
2741 *      @skb: buffer to segment
2742 *      @features: features for the output path (see dev->features)
2743 *
2744 *      This function performs segmentation on the given skb.  It returns
2745 *      a pointer to the first in a list of new skbs for the segments.
2746 *      In case of error it returns ERR_PTR(err).
2747 */
2748struct sk_buff *skb_segment(struct sk_buff *skb, netdev_features_t features)
2749{
2750        struct sk_buff *segs = NULL;
2751        struct sk_buff *tail = NULL;
2752        struct sk_buff *fskb = skb_shinfo(skb)->frag_list;
2753        unsigned int mss = skb_shinfo(skb)->gso_size;
2754        unsigned int doffset = skb->data - skb_mac_header(skb);
2755        unsigned int offset = doffset;
2756        unsigned int headroom;
2757        unsigned int len;
2758        int sg = !!(features & NETIF_F_SG);
2759        int nfrags = skb_shinfo(skb)->nr_frags;
2760        int err = -ENOMEM;
2761        int i = 0;
2762        int pos;
2763
2764        __skb_push(skb, doffset);
2765        headroom = skb_headroom(skb);
2766        pos = skb_headlen(skb);
2767
2768        do {
2769                struct sk_buff *nskb;
2770                skb_frag_t *frag;
2771                int hsize;
2772                int size;
2773
2774                len = skb->len - offset;
2775                if (len > mss)
2776                        len = mss;
2777
2778                hsize = skb_headlen(skb) - offset;
2779                if (hsize < 0)
2780                        hsize = 0;
2781                if (hsize > len || !sg)
2782                        hsize = len;
2783
2784                if (!hsize && i >= nfrags) {
2785                        BUG_ON(fskb->len != len);
2786
2787                        pos += len;
2788                        nskb = skb_clone(fskb, GFP_ATOMIC);
2789                        fskb = fskb->next;
2790
2791                        if (unlikely(!nskb))
2792                                goto err;
2793
2794                        hsize = skb_end_offset(nskb);
2795                        if (skb_cow_head(nskb, doffset + headroom)) {
2796                                kfree_skb(nskb);
2797                                goto err;
2798                        }
2799
2800                        nskb->truesize += skb_end_offset(nskb) - hsize;
2801                        skb_release_head_state(nskb);
2802                        __skb_push(nskb, doffset);
2803                } else {
2804                        nskb = __alloc_skb(hsize + doffset + headroom,
2805                                           GFP_ATOMIC, skb_alloc_rx_flag(skb),
2806                                           NUMA_NO_NODE);
2807
2808                        if (unlikely(!nskb))
2809                                goto err;
2810
2811                        skb_reserve(nskb, headroom);
2812                        __skb_put(nskb, doffset);
2813                }
2814
2815                if (segs)
2816                        tail->next = nskb;
2817                else
2818                        segs = nskb;
2819                tail = nskb;
2820
2821                __copy_skb_header(nskb, skb);
2822                nskb->mac_len = skb->mac_len;
2823
2824                /* nskb and skb might have different headroom */
2825                if (nskb->ip_summed == CHECKSUM_PARTIAL)
2826                        nskb->csum_start += skb_headroom(nskb) - headroom;
2827
2828                skb_reset_mac_header(nskb);
2829                skb_set_network_header(nskb, skb->mac_len);
2830                nskb->transport_header = (nskb->network_header +
2831                                          skb_network_header_len(skb));
2832                skb_copy_from_linear_data(skb, nskb->data, doffset);
2833
2834                if (fskb != skb_shinfo(skb)->frag_list)
2835                        continue;
2836
2837                if (!sg) {
2838                        nskb->ip_summed = CHECKSUM_NONE;
2839                        nskb->csum = skb_copy_and_csum_bits(skb, offset,
2840                                                            skb_put(nskb, len),
2841                                                            len, 0);
2842                        continue;
2843                }
2844
2845                frag = skb_shinfo(nskb)->frags;
2846
2847                skb_copy_from_linear_data_offset(skb, offset,
2848                                                 skb_put(nskb, hsize), hsize);
2849
2850                while (pos < offset + len && i < nfrags) {
2851                        *frag = skb_shinfo(skb)->frags[i];
2852                        __skb_frag_ref(frag);
2853                        size = skb_frag_size(frag);
2854
2855                        if (pos < offset) {
2856                                frag->page_offset += offset - pos;
2857                                skb_frag_size_sub(frag, offset - pos);
2858                        }
2859
2860                        skb_shinfo(nskb)->nr_frags++;
2861
2862                        if (pos + size <= offset + len) {
2863                                i++;
2864                                pos += size;
2865                        } else {
2866                                skb_frag_size_sub(frag, pos + size - (offset + len));
2867                                goto skip_fraglist;
2868                        }
2869
2870                        frag++;
2871                }
2872
2873                if (pos < offset + len) {
2874                        struct sk_buff *fskb2 = fskb;
2875
2876                        BUG_ON(pos + fskb->len != offset + len);
2877
2878                        pos += fskb->len;
2879                        fskb = fskb->next;
2880
2881                        if (fskb2->next) {
2882                                fskb2 = skb_clone(fskb2, GFP_ATOMIC);
2883                                if (!fskb2)
2884                                        goto err;
2885                        } else
2886                                skb_get(fskb2);
2887
2888                        SKB_FRAG_ASSERT(nskb);
2889                        skb_shinfo(nskb)->frag_list = fskb2;
2890                }
2891
2892skip_fraglist:
2893                nskb->data_len = len - hsize;
2894                nskb->len += nskb->data_len;
2895                nskb->truesize += nskb->data_len;
2896        } while ((offset += len) < skb->len);
2897
2898        return segs;
2899
2900err:
2901        while ((skb = segs)) {
2902                segs = skb->next;
2903                kfree_skb(skb);
2904        }
2905        return ERR_PTR(err);
2906}
2907EXPORT_SYMBOL_GPL(skb_segment);
2908
2909int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
2910{
2911        struct sk_buff *p = *head;
2912        struct sk_buff *nskb;
2913        struct skb_shared_info *skbinfo = skb_shinfo(skb);
2914        struct skb_shared_info *pinfo = skb_shinfo(p);
2915        unsigned int headroom;
2916        unsigned int len = skb_gro_len(skb);
2917        unsigned int offset = skb_gro_offset(skb);
2918        unsigned int headlen = skb_headlen(skb);
2919        unsigned int delta_truesize;
2920
2921        if (p->len + len >= 65536)
2922                return -E2BIG;
2923
2924        if (pinfo->frag_list)
2925                goto merge;
2926        else if (headlen <= offset) {
2927                skb_frag_t *frag;
2928                skb_frag_t *frag2;
2929                int i = skbinfo->nr_frags;
2930                int nr_frags = pinfo->nr_frags + i;
2931
2932                offset -= headlen;
2933
2934                if (nr_frags > MAX_SKB_FRAGS)
2935                        return -E2BIG;
2936
2937                pinfo->nr_frags = nr_frags;
2938                skbinfo->nr_frags = 0;
2939
2940                frag = pinfo->frags + nr_frags;
2941                frag2 = skbinfo->frags + i;
2942                do {
2943                        *--frag = *--frag2;
2944                } while (--i);
2945
2946                frag->page_offset += offset;
2947                skb_frag_size_sub(frag, offset);
2948
2949                /* all fragments truesize : remove (head size + sk_buff) */
2950                delta_truesize = skb->truesize -
2951                                 SKB_TRUESIZE(skb_end_offset(skb));
2952
2953                skb->truesize -= skb->data_len;
2954                skb->len -= skb->data_len;
2955                skb->data_len = 0;
2956
2957                NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
2958                goto done;
2959        } else if (skb->head_frag) {
2960                int nr_frags = pinfo->nr_frags;
2961                skb_frag_t *frag = pinfo->frags + nr_frags;
2962                struct page *page = virt_to_head_page(skb->head);
2963                unsigned int first_size = headlen - offset;
2964                unsigned int first_offset;
2965
2966                if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
2967                        return -E2BIG;
2968
2969                first_offset = skb->data -
2970                               (unsigned char *)page_address(page) +
2971                               offset;
2972
2973                pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
2974
2975                frag->page.p      = page;
2976                frag->page_offset = first_offset;
2977                skb_frag_size_set(frag, first_size);
2978
2979                memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
2980                /* We dont need to clear skbinfo->nr_frags here */
2981
2982                delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
2983                NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
2984                goto done;
2985        } else if (skb_gro_len(p) != pinfo->gso_size)
2986                return -E2BIG;
2987
2988        headroom = skb_headroom(p);
2989        nskb = alloc_skb(headroom + skb_gro_offset(p), GFP_ATOMIC);
2990        if (unlikely(!nskb))
2991                return -ENOMEM;
2992
2993        __copy_skb_header(nskb, p);
2994        nskb->mac_len = p->mac_len;
2995
2996        skb_reserve(nskb, headroom);
2997        __skb_put(nskb, skb_gro_offset(p));
2998
2999        skb_set_mac_header(nskb, skb_mac_header(p) - p->data);
3000        skb_set_network_header(nskb, skb_network_offset(p));
3001        skb_set_transport_header(nskb, skb_transport_offset(p));
3002
3003        __skb_pull(p, skb_gro_offset(p));
3004        memcpy(skb_mac_header(nskb), skb_mac_header(p),
3005               p->data - skb_mac_header(p));
3006
3007        *NAPI_GRO_CB(nskb) = *NAPI_GRO_CB(p);
3008        skb_shinfo(nskb)->frag_list = p;
3009        skb_shinfo(nskb)->gso_size = pinfo->gso_size;
3010        pinfo->gso_size = 0;
3011        skb_header_release(p);
3012        nskb->prev = p;
3013
3014        nskb->data_len += p->len;
3015        nskb->truesize += p->truesize;
3016        nskb->len += p->len;
3017
3018        *head = nskb;
3019        nskb->next = p->next;
3020        p->next = NULL;
3021
3022        p = nskb;
3023
3024merge:
3025        delta_truesize = skb->truesize;
3026        if (offset > headlen) {
3027                unsigned int eat = offset - headlen;
3028
3029                skbinfo->frags[0].page_offset += eat;
3030                skb_frag_size_sub(&skbinfo->frags[0], eat);
3031                skb->data_len -= eat;
3032                skb->len -= eat;
3033                offset = headlen;
3034        }
3035
3036        __skb_pull(skb, offset);
3037
3038        p->prev->next = skb;
3039        p->prev = skb;
3040        skb_header_release(skb);
3041
3042done:
3043        NAPI_GRO_CB(p)->count++;
3044        p->data_len += len;
3045        p->truesize += delta_truesize;
3046        p->len += len;
3047
3048        NAPI_GRO_CB(skb)->same_flow = 1;
3049        return 0;
3050}
3051EXPORT_SYMBOL_GPL(skb_gro_receive);
3052
3053void __init skb_init(void)
3054{
3055        skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
3056                                              sizeof(struct sk_buff),
3057                                              0,
3058                                              SLAB_HWCACHE_ALIGN|SLAB_PANIC,
3059                                              NULL);
3060        skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
3061                                                (2*sizeof(struct sk_buff)) +
3062                                                sizeof(atomic_t),
3063                                                0,
3064                                                SLAB_HWCACHE_ALIGN|SLAB_PANIC,
3065                                                NULL);
3066}
3067
3068/**
3069 *      skb_to_sgvec - Fill a scatter-gather list from a socket buffer
3070 *      @skb: Socket buffer containing the buffers to be mapped
3071 *      @sg: The scatter-gather list to map into
3072 *      @offset: The offset into the buffer's contents to start mapping
3073 *      @len: Length of buffer space to be mapped
3074 *
3075 *      Fill the specified scatter-gather list with mappings/pointers into a
3076 *      region of the buffer space attached to a socket buffer.
3077 */
3078static int
3079__skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3080{
3081        int start = skb_headlen(skb);
3082        int i, copy = start - offset;
3083        struct sk_buff *frag_iter;
3084        int elt = 0;
3085
3086        if (copy > 0) {
3087                if (copy > len)
3088                        copy = len;
3089                sg_set_buf(sg, skb->data + offset, copy);
3090                elt++;
3091                if ((len -= copy) == 0)
3092                        return elt;
3093                offset += copy;
3094        }
3095
3096        for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
3097                int end;
3098
3099                WARN_ON(start > offset + len);
3100
3101                end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
3102                if ((copy = end - offset) > 0) {
3103                        skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3104
3105                        if (copy > len)
3106                                copy = len;
3107                        sg_set_page(&sg[elt], skb_frag_page(frag), copy,
3108                                        frag->page_offset+offset-start);
3109                        elt++;
3110                        if (!(len -= copy))
3111                                return elt;
3112                        offset += copy;
3113                }
3114                start = end;
3115        }
3116
3117        skb_walk_frags(skb, frag_iter) {
3118                int end;
3119
3120                WARN_ON(start > offset + len);
3121
3122                end = start + frag_iter->len;
3123                if ((copy = end - offset) > 0) {
3124                        if (copy > len)
3125                                copy = len;
3126                        elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start,
3127                                              copy);
3128                        if ((len -= copy) == 0)
3129                                return elt;
3130                        offset += copy;
3131                }
3132                start = end;
3133        }
3134        BUG_ON(len);
3135        return elt;
3136}
3137
3138int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3139{
3140        int nsg = __skb_to_sgvec(skb, sg, offset, len);
3141
3142        sg_mark_end(&sg[nsg - 1]);
3143
3144        return nsg;
3145}
3146EXPORT_SYMBOL_GPL(skb_to_sgvec);
3147
3148/**
3149 *      skb_cow_data - Check that a socket buffer's data buffers are writable
3150 *      @skb: The socket buffer to check.
3151 *      @tailbits: Amount of trailing space to be added
3152 *      @trailer: Returned pointer to the skb where the @tailbits space begins
3153 *
3154 *      Make sure that the data buffers attached to a socket buffer are
3155 *      writable. If they are not, private copies are made of the data buffers
3156 *      and the socket buffer is set to use these instead.
3157 *
3158 *      If @tailbits is given, make sure that there is space to write @tailbits
3159 *      bytes of data beyond current end of socket buffer.  @trailer will be
3160 *      set to point to the skb in which this space begins.
3161 *
3162 *      The number of scatterlist elements required to completely map the
3163 *      COW'd and extended socket buffer will be returned.
3164 */
3165int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
3166{
3167        int copyflag;
3168        int elt;
3169        struct sk_buff *skb1, **skb_p;
3170
3171        /* If skb is cloned or its head is paged, reallocate
3172         * head pulling out all the pages (pages are considered not writable
3173         * at the moment even if they are anonymous).
3174         */
3175        if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
3176            __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
3177                return -ENOMEM;
3178
3179        /* Easy case. Most of packets will go this way. */
3180        if (!skb_has_frag_list(skb)) {
3181                /* A little of trouble, not enough of space for trailer.
3182                 * This should not happen, when stack is tuned to generate
3183                 * good frames. OK, on miss we reallocate and reserve even more
3184                 * space, 128 bytes is fair. */
3185
3186                if (skb_tailroom(skb) < tailbits &&
3187                    pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
3188                        return -ENOMEM;
3189
3190                /* Voila! */
3191                *trailer = skb;
3192                return 1;
3193        }
3194
3195        /* Misery. We are in troubles, going to mincer fragments... */
3196
3197        elt = 1;
3198        skb_p = &skb_shinfo(skb)->frag_list;
3199        copyflag = 0;
3200
3201        while ((skb1 = *skb_p) != NULL) {
3202                int ntail = 0;
3203
3204                /* The fragment is partially pulled by someone,
3205                 * this can happen on input. Copy it and everything
3206                 * after it. */
3207
3208                if (skb_shared(skb1))
3209                        copyflag = 1;
3210
3211                /* If the skb is the last, worry about trailer. */
3212
3213                if (skb1->next == NULL && tailbits) {
3214                        if (skb_shinfo(skb1)->nr_frags ||
3215                            skb_has_frag_list(skb1) ||
3216                            skb_tailroom(skb1) < tailbits)
3217                                ntail = tailbits + 128;
3218                }
3219
3220                if (copyflag ||
3221                    skb_cloned(skb1) ||
3222                    ntail ||
3223                    skb_shinfo(skb1)->nr_frags ||
3224                    skb_has_frag_list(skb1)) {
3225                        struct sk_buff *skb2;
3226
3227                        /* Fuck, we are miserable poor guys... */
3228                        if (ntail == 0)
3229                                skb2 = skb_copy(skb1, GFP_ATOMIC);
3230                        else
3231                                skb2 = skb_copy_expand(skb1,
3232                                                       skb_headroom(skb1),
3233                                                       ntail,
3234                                                       GFP_ATOMIC);
3235                        if (unlikely(skb2 == NULL))
3236                                return -ENOMEM;
3237
3238                        if (skb1->sk)
3239                                skb_set_owner_w(skb2, skb1->sk);
3240
3241                        /* Looking around. Are we still alive?
3242                         * OK, link new skb, drop old one */
3243
3244                        skb2->next = skb1->next;
3245                        *skb_p = skb2;
3246                        kfree_skb(skb1);
3247                        skb1 = skb2;
3248                }
3249                elt++;
3250                *trailer = skb1;
3251                skb_p = &skb1->next;
3252        }
3253
3254        return elt;
3255}
3256EXPORT_SYMBOL_GPL(skb_cow_data);
3257
3258static void sock_rmem_free(struct sk_buff *skb)
3259{
3260        struct sock *sk = skb->sk;
3261
3262        atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
3263}
3264
3265/*
3266 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
3267 */
3268int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
3269{
3270        int len = skb->len;
3271
3272        if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
3273            (unsigned int)sk->sk_rcvbuf)
3274                return -ENOMEM;
3275
3276        skb_orphan(skb);
3277        skb->sk = sk;
3278        skb->destructor = sock_rmem_free;
3279        atomic_add(skb->truesize, &sk->sk_rmem_alloc);
3280
3281        /* before exiting rcu section, make sure dst is refcounted */
3282        skb_dst_force(skb);
3283
3284        skb_queue_tail(&sk->sk_error_queue, skb);
3285        if (!sock_flag(sk, SOCK_DEAD))
3286                sk->sk_data_ready(sk, len);
3287        return 0;
3288}
3289EXPORT_SYMBOL(sock_queue_err_skb);
3290
3291void skb_tstamp_tx(struct sk_buff *orig_skb,
3292                struct skb_shared_hwtstamps *hwtstamps)
3293{
3294        struct sock *sk = orig_skb->sk;
3295        struct sock_exterr_skb *serr;
3296        struct sk_buff *skb;
3297        int err;
3298
3299        if (!sk)
3300                return;
3301
3302        skb = skb_clone(orig_skb, GFP_ATOMIC);
3303        if (!skb)
3304                return;
3305
3306        if (hwtstamps) {
3307                *skb_hwtstamps(skb) =
3308                        *hwtstamps;
3309        } else {
3310                /*
3311                 * no hardware time stamps available,
3312                 * so keep the shared tx_flags and only
3313                 * store software time stamp
3314                 */
3315                skb->tstamp = ktime_get_real();
3316        }
3317
3318        serr = SKB_EXT_ERR(skb);
3319        memset(serr, 0, sizeof(*serr));
3320        serr->ee.ee_errno = ENOMSG;
3321        serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
3322
3323        err = sock_queue_err_skb(sk, skb);
3324
3325        if (err)
3326                kfree_skb(skb);
3327}
3328EXPORT_SYMBOL_GPL(skb_tstamp_tx);
3329
3330void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
3331{
3332        struct sock *sk = skb->sk;
3333        struct sock_exterr_skb *serr;
3334        int err;
3335
3336        skb->wifi_acked_valid = 1;
3337        skb->wifi_acked = acked;
3338
3339        serr = SKB_EXT_ERR(skb);
3340        memset(serr, 0, sizeof(*serr));
3341        serr->ee.ee_errno = ENOMSG;
3342        serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
3343
3344        err = sock_queue_err_skb(sk, skb);
3345        if (err)
3346                kfree_skb(skb);
3347}
3348EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
3349
3350
3351/**
3352 * skb_partial_csum_set - set up and verify partial csum values for packet
3353 * @skb: the skb to set
3354 * @start: the number of bytes after skb->data to start checksumming.
3355 * @off: the offset from start to place the checksum.
3356 *
3357 * For untrusted partially-checksummed packets, we need to make sure the values
3358 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
3359 *
3360 * This function checks and sets those values and skb->ip_summed: if this
3361 * returns false you should drop the packet.
3362 */
3363bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
3364{
3365        if (unlikely(start > skb_headlen(skb)) ||
3366            unlikely((int)start + off > skb_headlen(skb) - 2)) {
3367                net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n",
3368                                     start, off, skb_headlen(skb));
3369                return false;
3370        }
3371        skb->ip_summed = CHECKSUM_PARTIAL;
3372        skb->csum_start = skb_headroom(skb) + start;
3373        skb->csum_offset = off;
3374        return true;
3375}
3376EXPORT_SYMBOL_GPL(skb_partial_csum_set);
3377
3378void __skb_warn_lro_forwarding(const struct sk_buff *skb)
3379{
3380        net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
3381                             skb->dev->name);
3382}
3383EXPORT_SYMBOL(__skb_warn_lro_forwarding);
3384
3385void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
3386{
3387        if (head_stolen) {
3388                skb_release_head_state(skb);
3389                kmem_cache_free(skbuff_head_cache, skb);
3390        } else {
3391                __kfree_skb(skb);
3392        }
3393}
3394EXPORT_SYMBOL(kfree_skb_partial);
3395
3396/**
3397 * skb_try_coalesce - try to merge skb to prior one
3398 * @to: prior buffer
3399 * @from: buffer to add
3400 * @fragstolen: pointer to boolean
3401 * @delta_truesize: how much more was allocated than was requested
3402 */
3403bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
3404                      bool *fragstolen, int *delta_truesize)
3405{
3406        int i, delta, len = from->len;
3407
3408        *fragstolen = false;
3409
3410        if (skb_cloned(to))
3411                return false;
3412
3413        if (len <= skb_tailroom(to)) {
3414                BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
3415                *delta_truesize = 0;
3416                return true;
3417        }
3418
3419        if (skb_has_frag_list(to) || skb_has_frag_list(from))
3420                return false;
3421
3422        if (skb_headlen(from) != 0) {
3423                struct page *page;
3424                unsigned int offset;
3425
3426                if (skb_shinfo(to)->nr_frags +
3427                    skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
3428                        return false;
3429
3430                if (skb_head_is_locked(from))
3431                        return false;
3432
3433                delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
3434
3435                page = virt_to_head_page(from->head);
3436                offset = from->data - (unsigned char *)page_address(page);
3437
3438                skb_fill_page_desc(to, skb_shinfo(to)->nr_frags,
3439                                   page, offset, skb_headlen(from));
3440                *fragstolen = true;
3441        } else {
3442                if (skb_shinfo(to)->nr_frags +
3443                    skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS)
3444                        return false;
3445
3446                delta = from->truesize -
3447                        SKB_TRUESIZE(skb_end_pointer(from) - from->head);
3448        }
3449
3450        WARN_ON_ONCE(delta < len);
3451
3452        memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags,
3453               skb_shinfo(from)->frags,
3454               skb_shinfo(from)->nr_frags * sizeof(skb_frag_t));
3455        skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags;
3456
3457        if (!skb_cloned(from))
3458                skb_shinfo(from)->nr_frags = 0;
3459
3460        /* if the skb is not cloned this does nothing
3461         * since we set nr_frags to 0.
3462         */
3463        for (i = 0; i < skb_shinfo(from)->nr_frags; i++)
3464                skb_frag_ref(from, i);
3465
3466        to->truesize += delta;
3467        to->len += len;
3468        to->data_len += len;
3469
3470        *delta_truesize = delta;
3471        return true;
3472}
3473EXPORT_SYMBOL(skb_try_coalesce);
3474
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.