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