PageRenderTime 72ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/include/linux/skbuff.h

https://github.com/airy09/android_kernel_sony_apq8064
C Header | 2564 lines | 1412 code | 298 blank | 854 comment | 112 complexity | 0fe0a09de4fab852acdab926de7f7a76 MD5 | raw file
Possible License(s): GPL-2.0
  1. /*
  2. * Definitions for the 'struct sk_buff' memory handlers.
  3. *
  4. * Authors:
  5. * Alan Cox, <gw4pts@gw4pts.ampr.org>
  6. * Florian La Roche, <rzsfl@rz.uni-sb.de>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version
  11. * 2 of the License, or (at your option) any later version.
  12. */
  13. #ifndef _LINUX_SKBUFF_H
  14. #define _LINUX_SKBUFF_H
  15. #include <linux/kernel.h>
  16. #include <linux/kmemcheck.h>
  17. #include <linux/compiler.h>
  18. #include <linux/time.h>
  19. #include <linux/bug.h>
  20. #include <linux/cache.h>
  21. #include <linux/atomic.h>
  22. #include <asm/types.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/net.h>
  25. #include <linux/textsearch.h>
  26. #include <net/checksum.h>
  27. #include <linux/rcupdate.h>
  28. #include <linux/dmaengine.h>
  29. #include <linux/hrtimer.h>
  30. #include <linux/dma-mapping.h>
  31. #include <linux/netdev_features.h>
  32. /* Don't change this without changing skb_csum_unnecessary! */
  33. #define CHECKSUM_NONE 0
  34. #define CHECKSUM_UNNECESSARY 1
  35. #define CHECKSUM_COMPLETE 2
  36. #define CHECKSUM_PARTIAL 3
  37. #define SKB_DATA_ALIGN(X) (((X) + (SMP_CACHE_BYTES - 1)) & \
  38. ~(SMP_CACHE_BYTES - 1))
  39. #define SKB_WITH_OVERHEAD(X) \
  40. ((X) - SKB_DATA_ALIGN(sizeof(struct skb_shared_info)))
  41. #define SKB_MAX_ORDER(X, ORDER) \
  42. SKB_WITH_OVERHEAD((PAGE_SIZE << (ORDER)) - (X))
  43. #define SKB_MAX_HEAD(X) (SKB_MAX_ORDER((X), 0))
  44. #define SKB_MAX_ALLOC (SKB_MAX_ORDER(0, 2))
  45. /* return minimum truesize of one skb containing X bytes of data */
  46. #define SKB_TRUESIZE(X) ((X) + \
  47. SKB_DATA_ALIGN(sizeof(struct sk_buff)) + \
  48. SKB_DATA_ALIGN(sizeof(struct skb_shared_info)))
  49. /* A. Checksumming of received packets by device.
  50. *
  51. * NONE: device failed to checksum this packet.
  52. * skb->csum is undefined.
  53. *
  54. * UNNECESSARY: device parsed packet and wouldbe verified checksum.
  55. * skb->csum is undefined.
  56. * It is bad option, but, unfortunately, many of vendors do this.
  57. * Apparently with secret goal to sell you new device, when you
  58. * will add new protocol to your host. F.e. IPv6. 8)
  59. *
  60. * COMPLETE: the most generic way. Device supplied checksum of _all_
  61. * the packet as seen by netif_rx in skb->csum.
  62. * NOTE: Even if device supports only some protocols, but
  63. * is able to produce some skb->csum, it MUST use COMPLETE,
  64. * not UNNECESSARY.
  65. *
  66. * PARTIAL: identical to the case for output below. This may occur
  67. * on a packet received directly from another Linux OS, e.g.,
  68. * a virtualised Linux kernel on the same host. The packet can
  69. * be treated in the same way as UNNECESSARY except that on
  70. * output (i.e., forwarding) the checksum must be filled in
  71. * by the OS or the hardware.
  72. *
  73. * B. Checksumming on output.
  74. *
  75. * NONE: skb is checksummed by protocol or csum is not required.
  76. *
  77. * PARTIAL: device is required to csum packet as seen by hard_start_xmit
  78. * from skb->csum_start to the end and to record the checksum
  79. * at skb->csum_start + skb->csum_offset.
  80. *
  81. * Device must show its capabilities in dev->features, set
  82. * at device setup time.
  83. * NETIF_F_HW_CSUM - it is clever device, it is able to checksum
  84. * everything.
  85. * NETIF_F_IP_CSUM - device is dumb. It is able to csum only
  86. * TCP/UDP over IPv4. Sigh. Vendors like this
  87. * way by an unknown reason. Though, see comment above
  88. * about CHECKSUM_UNNECESSARY. 8)
  89. * NETIF_F_IPV6_CSUM about as dumb as the last one but does IPv6 instead.
  90. *
  91. * UNNECESSARY: device will do per protocol specific csum. Protocol drivers
  92. * that do not want net to perform the checksum calculation should use
  93. * this flag in their outgoing skbs.
  94. * NETIF_F_FCOE_CRC this indicates the device can do FCoE FC CRC
  95. * offload. Correspondingly, the FCoE protocol driver
  96. * stack should use CHECKSUM_UNNECESSARY.
  97. *
  98. * Any questions? No questions, good. --ANK
  99. */
  100. struct net_device;
  101. struct scatterlist;
  102. struct pipe_inode_info;
  103. #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
  104. struct nf_conntrack {
  105. atomic_t use;
  106. };
  107. #endif
  108. #ifdef CONFIG_BRIDGE_NETFILTER
  109. struct nf_bridge_info {
  110. atomic_t use;
  111. struct net_device *physindev;
  112. struct net_device *physoutdev;
  113. unsigned int mask;
  114. unsigned long data[32 / sizeof(unsigned long)];
  115. };
  116. #endif
  117. struct sk_buff_head {
  118. /* These two members must be first. */
  119. struct sk_buff *next;
  120. struct sk_buff *prev;
  121. __u32 qlen;
  122. spinlock_t lock;
  123. };
  124. struct sk_buff;
  125. /* To allow 64K frame to be packed as single skb without frag_list we
  126. * require 64K/PAGE_SIZE pages plus 1 additional page to allow for
  127. * buffers which do not start on a page boundary.
  128. *
  129. * Since GRO uses frags we allocate at least 16 regardless of page
  130. * size.
  131. */
  132. #if (65536/PAGE_SIZE + 1) < 16
  133. #define MAX_SKB_FRAGS 16UL
  134. #else
  135. #define MAX_SKB_FRAGS (65536/PAGE_SIZE + 1)
  136. #endif
  137. typedef struct skb_frag_struct skb_frag_t;
  138. struct skb_frag_struct {
  139. struct {
  140. struct page *p;
  141. } page;
  142. #if (BITS_PER_LONG > 32) || (PAGE_SIZE >= 65536)
  143. __u32 page_offset;
  144. __u32 size;
  145. #else
  146. __u16 page_offset;
  147. __u16 size;
  148. #endif
  149. };
  150. static inline unsigned int skb_frag_size(const skb_frag_t *frag)
  151. {
  152. return frag->size;
  153. }
  154. static inline void skb_frag_size_set(skb_frag_t *frag, unsigned int size)
  155. {
  156. frag->size = size;
  157. }
  158. static inline void skb_frag_size_add(skb_frag_t *frag, int delta)
  159. {
  160. frag->size += delta;
  161. }
  162. static inline void skb_frag_size_sub(skb_frag_t *frag, int delta)
  163. {
  164. frag->size -= delta;
  165. }
  166. #define HAVE_HW_TIME_STAMP
  167. /**
  168. * struct skb_shared_hwtstamps - hardware time stamps
  169. * @hwtstamp: hardware time stamp transformed into duration
  170. * since arbitrary point in time
  171. * @syststamp: hwtstamp transformed to system time base
  172. *
  173. * Software time stamps generated by ktime_get_real() are stored in
  174. * skb->tstamp. The relation between the different kinds of time
  175. * stamps is as follows:
  176. *
  177. * syststamp and tstamp can be compared against each other in
  178. * arbitrary combinations. The accuracy of a
  179. * syststamp/tstamp/"syststamp from other device" comparison is
  180. * limited by the accuracy of the transformation into system time
  181. * base. This depends on the device driver and its underlying
  182. * hardware.
  183. *
  184. * hwtstamps can only be compared against other hwtstamps from
  185. * the same device.
  186. *
  187. * This structure is attached to packets as part of the
  188. * &skb_shared_info. Use skb_hwtstamps() to get a pointer.
  189. */
  190. struct skb_shared_hwtstamps {
  191. ktime_t hwtstamp;
  192. ktime_t syststamp;
  193. };
  194. /* Definitions for tx_flags in struct skb_shared_info */
  195. enum {
  196. /* generate hardware time stamp */
  197. SKBTX_HW_TSTAMP = 1 << 0,
  198. /* generate software time stamp */
  199. SKBTX_SW_TSTAMP = 1 << 1,
  200. /* device driver is going to provide hardware time stamp */
  201. SKBTX_IN_PROGRESS = 1 << 2,
  202. /* ensure the originating sk reference is available on driver level */
  203. SKBTX_DRV_NEEDS_SK_REF = 1 << 3,
  204. /* device driver supports TX zero-copy buffers */
  205. SKBTX_DEV_ZEROCOPY = 1 << 4,
  206. /* generate wifi status information (where possible) */
  207. SKBTX_WIFI_STATUS = 1 << 5,
  208. };
  209. /*
  210. * The callback notifies userspace to release buffers when skb DMA is done in
  211. * lower device, the skb last reference should be 0 when calling this.
  212. * The ctx field is used to track device context.
  213. * The desc field is used to track userspace buffer index.
  214. */
  215. struct ubuf_info {
  216. void (*callback)(struct ubuf_info *);
  217. void *ctx;
  218. unsigned long desc;
  219. };
  220. /* This data is invariant across clones and lives at
  221. * the end of the header data, ie. at skb->end.
  222. */
  223. struct skb_shared_info {
  224. unsigned char nr_frags;
  225. __u8 tx_flags;
  226. unsigned short gso_size;
  227. /* Warning: this field is not always filled in (UFO)! */
  228. unsigned short gso_segs;
  229. unsigned short gso_type;
  230. struct sk_buff *frag_list;
  231. struct skb_shared_hwtstamps hwtstamps;
  232. __be32 ip6_frag_id;
  233. /*
  234. * Warning : all fields before dataref are cleared in __alloc_skb()
  235. */
  236. atomic_t dataref;
  237. /* Intermediate layers must ensure that destructor_arg
  238. * remains valid until skb destructor */
  239. void * destructor_arg;
  240. /* must be last field, see pskb_expand_head() */
  241. skb_frag_t frags[MAX_SKB_FRAGS];
  242. };
  243. /* We divide dataref into two halves. The higher 16 bits hold references
  244. * to the payload part of skb->data. The lower 16 bits hold references to
  245. * the entire skb->data. A clone of a headerless skb holds the length of
  246. * the header in skb->hdr_len.
  247. *
  248. * All users must obey the rule that the skb->data reference count must be
  249. * greater than or equal to the payload reference count.
  250. *
  251. * Holding a reference to the payload part means that the user does not
  252. * care about modifications to the header part of skb->data.
  253. */
  254. #define SKB_DATAREF_SHIFT 16
  255. #define SKB_DATAREF_MASK ((1 << SKB_DATAREF_SHIFT) - 1)
  256. enum {
  257. SKB_FCLONE_UNAVAILABLE,
  258. SKB_FCLONE_ORIG,
  259. SKB_FCLONE_CLONE,
  260. };
  261. enum {
  262. SKB_GSO_TCPV4 = 1 << 0,
  263. SKB_GSO_UDP = 1 << 1,
  264. /* This indicates the skb is from an untrusted source. */
  265. SKB_GSO_DODGY = 1 << 2,
  266. /* This indicates the tcp segment has CWR set. */
  267. SKB_GSO_TCP_ECN = 1 << 3,
  268. SKB_GSO_TCPV6 = 1 << 4,
  269. SKB_GSO_FCOE = 1 << 5,
  270. };
  271. #if BITS_PER_LONG > 32
  272. #define NET_SKBUFF_DATA_USES_OFFSET 1
  273. #endif
  274. #ifdef NET_SKBUFF_DATA_USES_OFFSET
  275. typedef unsigned int sk_buff_data_t;
  276. #else
  277. typedef unsigned char *sk_buff_data_t;
  278. #endif
  279. #if defined(CONFIG_NF_DEFRAG_IPV4) || defined(CONFIG_NF_DEFRAG_IPV4_MODULE) || \
  280. defined(CONFIG_NF_DEFRAG_IPV6) || defined(CONFIG_NF_DEFRAG_IPV6_MODULE)
  281. #define NET_SKBUFF_NF_DEFRAG_NEEDED 1
  282. #endif
  283. /**
  284. * struct sk_buff - socket buffer
  285. * @next: Next buffer in list
  286. * @prev: Previous buffer in list
  287. * @tstamp: Time we arrived
  288. * @sk: Socket we are owned by
  289. * @dev: Device we arrived on/are leaving by
  290. * @cb: Control buffer. Free for use by every layer. Put private vars here
  291. * @_skb_refdst: destination entry (with norefcount bit)
  292. * @sp: the security path, used for xfrm
  293. * @len: Length of actual data
  294. * @data_len: Data length
  295. * @mac_len: Length of link layer header
  296. * @hdr_len: writable header length of cloned skb
  297. * @csum: Checksum (must include start/offset pair)
  298. * @csum_start: Offset from skb->head where checksumming should start
  299. * @csum_offset: Offset from csum_start where checksum should be stored
  300. * @priority: Packet queueing priority
  301. * @local_df: allow local fragmentation
  302. * @cloned: Head may be cloned (check refcnt to be sure)
  303. * @ip_summed: Driver fed us an IP checksum
  304. * @nohdr: Payload reference only, must not modify header
  305. * @nfctinfo: Relationship of this skb to the connection
  306. * @pkt_type: Packet class
  307. * @fclone: skbuff clone status
  308. * @ipvs_property: skbuff is owned by ipvs
  309. * @peeked: this packet has been seen already, so stats have been
  310. * done for it, don't do them again
  311. * @nf_trace: netfilter packet trace flag
  312. * @protocol: Packet protocol from driver
  313. * @destructor: Destruct function
  314. * @nfct: Associated connection, if any
  315. * @nfct_reasm: netfilter conntrack re-assembly pointer
  316. * @nf_bridge: Saved data about a bridged frame - see br_netfilter.c
  317. * @skb_iif: ifindex of device we arrived on
  318. * @tc_index: Traffic control index
  319. * @tc_verd: traffic control verdict
  320. * @rxhash: the packet hash computed on receive
  321. * @queue_mapping: Queue mapping for multiqueue devices
  322. * @ndisc_nodetype: router type (from link layer)
  323. * @ooo_okay: allow the mapping of a socket to a queue to be changed
  324. * @l4_rxhash: indicate rxhash is a canonical 4-tuple hash over transport
  325. * ports.
  326. * @wifi_acked_valid: wifi_acked was set
  327. * @wifi_acked: whether frame was acked on wifi or not
  328. * @no_fcs: Request NIC to treat last 4 bytes as Ethernet FCS
  329. * @dma_cookie: a cookie to one of several possible DMA operations
  330. * done by skb DMA functions
  331. * @secmark: security marking
  332. * @mark: Generic packet mark
  333. * @dropcount: total number of sk_receive_queue overflows
  334. * @vlan_tci: vlan tag control information
  335. * @transport_header: Transport layer header
  336. * @network_header: Network layer header
  337. * @mac_header: Link layer header
  338. * @tail: Tail pointer
  339. * @end: End pointer
  340. * @head: Head of buffer
  341. * @data: Data head pointer
  342. * @truesize: Buffer size
  343. * @users: User count - see {datagram,tcp}.c
  344. */
  345. struct sk_buff {
  346. /* These two members must be first. */
  347. struct sk_buff *next;
  348. struct sk_buff *prev;
  349. ktime_t tstamp;
  350. struct sock *sk;
  351. struct net_device *dev;
  352. /*
  353. * This is the control buffer. It is free to use for every
  354. * layer. Please put your private variables there. If you
  355. * want to keep them across layers you have to do a skb_clone()
  356. * first. This is owned by whoever has the skb queued ATM.
  357. */
  358. char cb[48] __aligned(8);
  359. unsigned long _skb_refdst;
  360. #ifdef CONFIG_XFRM
  361. struct sec_path *sp;
  362. #endif
  363. unsigned int len,
  364. data_len;
  365. __u16 mac_len,
  366. hdr_len;
  367. union {
  368. __wsum csum;
  369. struct {
  370. __u16 csum_start;
  371. __u16 csum_offset;
  372. };
  373. };
  374. __u32 priority;
  375. kmemcheck_bitfield_begin(flags1);
  376. __u8 local_df:1,
  377. cloned:1,
  378. ip_summed:2,
  379. nohdr:1,
  380. nfctinfo:3;
  381. __u8 pkt_type:3,
  382. fclone:2,
  383. ipvs_property:1,
  384. peeked:1,
  385. nf_trace:1;
  386. kmemcheck_bitfield_end(flags1);
  387. __be16 protocol;
  388. void (*destructor)(struct sk_buff *skb);
  389. #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
  390. struct nf_conntrack *nfct;
  391. #endif
  392. #ifdef NET_SKBUFF_NF_DEFRAG_NEEDED
  393. struct sk_buff *nfct_reasm;
  394. #endif
  395. #ifdef CONFIG_BRIDGE_NETFILTER
  396. struct nf_bridge_info *nf_bridge;
  397. #endif
  398. int skb_iif;
  399. __u32 rxhash;
  400. __u16 vlan_tci;
  401. #ifdef CONFIG_NET_SCHED
  402. __u16 tc_index; /* traffic control index */
  403. #ifdef CONFIG_NET_CLS_ACT
  404. __u16 tc_verd; /* traffic control verdict */
  405. #endif
  406. #endif
  407. __u16 queue_mapping;
  408. kmemcheck_bitfield_begin(flags2);
  409. #ifdef CONFIG_IPV6_NDISC_NODETYPE
  410. __u8 ndisc_nodetype:2;
  411. #endif
  412. __u8 ooo_okay:1;
  413. __u8 l4_rxhash:1;
  414. __u8 wifi_acked_valid:1;
  415. __u8 wifi_acked:1;
  416. __u8 no_fcs:1;
  417. /* 9/11 bit hole (depending on ndisc_nodetype presence) */
  418. kmemcheck_bitfield_end(flags2);
  419. #ifdef CONFIG_NET_DMA
  420. dma_cookie_t dma_cookie;
  421. #endif
  422. #ifdef CONFIG_NETWORK_SECMARK
  423. __u32 secmark;
  424. #endif
  425. union {
  426. __u32 mark;
  427. __u32 dropcount;
  428. __u32 avail_size;
  429. };
  430. sk_buff_data_t transport_header;
  431. sk_buff_data_t network_header;
  432. sk_buff_data_t mac_header;
  433. /* These elements must be at the end, see alloc_skb() for details. */
  434. sk_buff_data_t tail;
  435. sk_buff_data_t end;
  436. unsigned char *head,
  437. *data;
  438. unsigned int truesize;
  439. atomic_t users;
  440. };
  441. #ifdef __KERNEL__
  442. /*
  443. * Handling routines are only of interest to the kernel
  444. */
  445. #include <linux/slab.h>
  446. /*
  447. * skb might have a dst pointer attached, refcounted or not.
  448. * _skb_refdst low order bit is set if refcount was _not_ taken
  449. */
  450. #define SKB_DST_NOREF 1UL
  451. #define SKB_DST_PTRMASK ~(SKB_DST_NOREF)
  452. /**
  453. * skb_dst - returns skb dst_entry
  454. * @skb: buffer
  455. *
  456. * Returns skb dst_entry, regardless of reference taken or not.
  457. */
  458. static inline struct dst_entry *skb_dst(const struct sk_buff *skb)
  459. {
  460. /* If refdst was not refcounted, check we still are in a
  461. * rcu_read_lock section
  462. */
  463. WARN_ON((skb->_skb_refdst & SKB_DST_NOREF) &&
  464. !rcu_read_lock_held() &&
  465. !rcu_read_lock_bh_held());
  466. return (struct dst_entry *)(skb->_skb_refdst & SKB_DST_PTRMASK);
  467. }
  468. /**
  469. * skb_dst_set - sets skb dst
  470. * @skb: buffer
  471. * @dst: dst entry
  472. *
  473. * Sets skb dst, assuming a reference was taken on dst and should
  474. * be released by skb_dst_drop()
  475. */
  476. static inline void skb_dst_set(struct sk_buff *skb, struct dst_entry *dst)
  477. {
  478. skb->_skb_refdst = (unsigned long)dst;
  479. }
  480. extern void skb_dst_set_noref(struct sk_buff *skb, struct dst_entry *dst);
  481. /**
  482. * skb_dst_is_noref - Test if skb dst isn't refcounted
  483. * @skb: buffer
  484. */
  485. static inline bool skb_dst_is_noref(const struct sk_buff *skb)
  486. {
  487. return (skb->_skb_refdst & SKB_DST_NOREF) && skb_dst(skb);
  488. }
  489. static inline struct rtable *skb_rtable(const struct sk_buff *skb)
  490. {
  491. return (struct rtable *)skb_dst(skb);
  492. }
  493. extern void kfree_skb(struct sk_buff *skb);
  494. extern void consume_skb(struct sk_buff *skb);
  495. extern void __kfree_skb(struct sk_buff *skb);
  496. extern struct sk_buff *__alloc_skb(unsigned int size,
  497. gfp_t priority, int fclone, int node);
  498. extern struct sk_buff *build_skb(void *data);
  499. static inline struct sk_buff *alloc_skb(unsigned int size,
  500. gfp_t priority)
  501. {
  502. return __alloc_skb(size, priority, 0, NUMA_NO_NODE);
  503. }
  504. static inline struct sk_buff *alloc_skb_fclone(unsigned int size,
  505. gfp_t priority)
  506. {
  507. return __alloc_skb(size, priority, 1, NUMA_NO_NODE);
  508. }
  509. extern void skb_recycle(struct sk_buff *skb);
  510. extern bool skb_recycle_check(struct sk_buff *skb, int skb_size);
  511. extern struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src);
  512. extern int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask);
  513. extern struct sk_buff *skb_clone(struct sk_buff *skb,
  514. gfp_t priority);
  515. extern struct sk_buff *skb_copy(const struct sk_buff *skb,
  516. gfp_t priority);
  517. extern struct sk_buff *__pskb_copy(struct sk_buff *skb,
  518. int headroom, gfp_t gfp_mask);
  519. extern int pskb_expand_head(struct sk_buff *skb,
  520. int nhead, int ntail,
  521. gfp_t gfp_mask);
  522. extern struct sk_buff *skb_realloc_headroom(struct sk_buff *skb,
  523. unsigned int headroom);
  524. extern struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
  525. int newheadroom, int newtailroom,
  526. gfp_t priority);
  527. extern int skb_to_sgvec(struct sk_buff *skb,
  528. struct scatterlist *sg, int offset,
  529. int len);
  530. extern int skb_cow_data(struct sk_buff *skb, int tailbits,
  531. struct sk_buff **trailer);
  532. extern int skb_pad(struct sk_buff *skb, int pad);
  533. #define dev_kfree_skb(a) consume_skb(a)
  534. extern int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
  535. int getfrag(void *from, char *to, int offset,
  536. int len,int odd, struct sk_buff *skb),
  537. void *from, int length);
  538. struct skb_seq_state {
  539. __u32 lower_offset;
  540. __u32 upper_offset;
  541. __u32 frag_idx;
  542. __u32 stepped_offset;
  543. struct sk_buff *root_skb;
  544. struct sk_buff *cur_skb;
  545. __u8 *frag_data;
  546. };
  547. extern void skb_prepare_seq_read(struct sk_buff *skb,
  548. unsigned int from, unsigned int to,
  549. struct skb_seq_state *st);
  550. extern unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
  551. struct skb_seq_state *st);
  552. extern void skb_abort_seq_read(struct skb_seq_state *st);
  553. extern unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
  554. unsigned int to, struct ts_config *config,
  555. struct ts_state *state);
  556. extern void __skb_get_rxhash(struct sk_buff *skb);
  557. static inline __u32 skb_get_rxhash(struct sk_buff *skb)
  558. {
  559. if (!skb->rxhash)
  560. __skb_get_rxhash(skb);
  561. return skb->rxhash;
  562. }
  563. #ifdef NET_SKBUFF_DATA_USES_OFFSET
  564. static inline unsigned char *skb_end_pointer(const struct sk_buff *skb)
  565. {
  566. return skb->head + skb->end;
  567. }
  568. #else
  569. static inline unsigned char *skb_end_pointer(const struct sk_buff *skb)
  570. {
  571. return skb->end;
  572. }
  573. #endif
  574. /* Internal */
  575. #define skb_shinfo(SKB) ((struct skb_shared_info *)(skb_end_pointer(SKB)))
  576. static inline struct skb_shared_hwtstamps *skb_hwtstamps(struct sk_buff *skb)
  577. {
  578. return &skb_shinfo(skb)->hwtstamps;
  579. }
  580. /**
  581. * skb_queue_empty - check if a queue is empty
  582. * @list: queue head
  583. *
  584. * Returns true if the queue is empty, false otherwise.
  585. */
  586. static inline int skb_queue_empty(const struct sk_buff_head *list)
  587. {
  588. return list->next == (struct sk_buff *)list;
  589. }
  590. /**
  591. * skb_queue_is_last - check if skb is the last entry in the queue
  592. * @list: queue head
  593. * @skb: buffer
  594. *
  595. * Returns true if @skb is the last buffer on the list.
  596. */
  597. static inline bool skb_queue_is_last(const struct sk_buff_head *list,
  598. const struct sk_buff *skb)
  599. {
  600. return skb->next == (struct sk_buff *)list;
  601. }
  602. /**
  603. * skb_queue_is_first - check if skb is the first entry in the queue
  604. * @list: queue head
  605. * @skb: buffer
  606. *
  607. * Returns true if @skb is the first buffer on the list.
  608. */
  609. static inline bool skb_queue_is_first(const struct sk_buff_head *list,
  610. const struct sk_buff *skb)
  611. {
  612. return skb->prev == (struct sk_buff *)list;
  613. }
  614. /**
  615. * skb_queue_next - return the next packet in the queue
  616. * @list: queue head
  617. * @skb: current buffer
  618. *
  619. * Return the next packet in @list after @skb. It is only valid to
  620. * call this if skb_queue_is_last() evaluates to false.
  621. */
  622. static inline struct sk_buff *skb_queue_next(const struct sk_buff_head *list,
  623. const struct sk_buff *skb)
  624. {
  625. /* This BUG_ON may seem severe, but if we just return then we
  626. * are going to dereference garbage.
  627. */
  628. BUG_ON(skb_queue_is_last(list, skb));
  629. return skb->next;
  630. }
  631. /**
  632. * skb_queue_prev - return the prev packet in the queue
  633. * @list: queue head
  634. * @skb: current buffer
  635. *
  636. * Return the prev packet in @list before @skb. It is only valid to
  637. * call this if skb_queue_is_first() evaluates to false.
  638. */
  639. static inline struct sk_buff *skb_queue_prev(const struct sk_buff_head *list,
  640. const struct sk_buff *skb)
  641. {
  642. /* This BUG_ON may seem severe, but if we just return then we
  643. * are going to dereference garbage.
  644. */
  645. BUG_ON(skb_queue_is_first(list, skb));
  646. return skb->prev;
  647. }
  648. /**
  649. * skb_get - reference buffer
  650. * @skb: buffer to reference
  651. *
  652. * Makes another reference to a socket buffer and returns a pointer
  653. * to the buffer.
  654. */
  655. static inline struct sk_buff *skb_get(struct sk_buff *skb)
  656. {
  657. atomic_inc(&skb->users);
  658. return skb;
  659. }
  660. /*
  661. * If users == 1, we are the only owner and are can avoid redundant
  662. * atomic change.
  663. */
  664. /**
  665. * skb_cloned - is the buffer a clone
  666. * @skb: buffer to check
  667. *
  668. * Returns true if the buffer was generated with skb_clone() and is
  669. * one of multiple shared copies of the buffer. Cloned buffers are
  670. * shared data so must not be written to under normal circumstances.
  671. */
  672. static inline int skb_cloned(const struct sk_buff *skb)
  673. {
  674. return skb->cloned &&
  675. (atomic_read(&skb_shinfo(skb)->dataref) & SKB_DATAREF_MASK) != 1;
  676. }
  677. /**
  678. * skb_header_cloned - is the header a clone
  679. * @skb: buffer to check
  680. *
  681. * Returns true if modifying the header part of the buffer requires
  682. * the data to be copied.
  683. */
  684. static inline int skb_header_cloned(const struct sk_buff *skb)
  685. {
  686. int dataref;
  687. if (!skb->cloned)
  688. return 0;
  689. dataref = atomic_read(&skb_shinfo(skb)->dataref);
  690. dataref = (dataref & SKB_DATAREF_MASK) - (dataref >> SKB_DATAREF_SHIFT);
  691. return dataref != 1;
  692. }
  693. /**
  694. * skb_header_release - release reference to header
  695. * @skb: buffer to operate on
  696. *
  697. * Drop a reference to the header part of the buffer. This is done
  698. * by acquiring a payload reference. You must not read from the header
  699. * part of skb->data after this.
  700. */
  701. static inline void skb_header_release(struct sk_buff *skb)
  702. {
  703. BUG_ON(skb->nohdr);
  704. skb->nohdr = 1;
  705. atomic_add(1 << SKB_DATAREF_SHIFT, &skb_shinfo(skb)->dataref);
  706. }
  707. /**
  708. * skb_shared - is the buffer shared
  709. * @skb: buffer to check
  710. *
  711. * Returns true if more than one person has a reference to this
  712. * buffer.
  713. */
  714. static inline int skb_shared(const struct sk_buff *skb)
  715. {
  716. return atomic_read(&skb->users) != 1;
  717. }
  718. /**
  719. * skb_share_check - check if buffer is shared and if so clone it
  720. * @skb: buffer to check
  721. * @pri: priority for memory allocation
  722. *
  723. * If the buffer is shared the buffer is cloned and the old copy
  724. * drops a reference. A new clone with a single reference is returned.
  725. * If the buffer is not shared the original buffer is returned. When
  726. * being called from interrupt status or with spinlocks held pri must
  727. * be GFP_ATOMIC.
  728. *
  729. * NULL is returned on a memory allocation failure.
  730. */
  731. static inline struct sk_buff *skb_share_check(struct sk_buff *skb,
  732. gfp_t pri)
  733. {
  734. might_sleep_if(pri & __GFP_WAIT);
  735. if (skb_shared(skb)) {
  736. struct sk_buff *nskb = skb_clone(skb, pri);
  737. kfree_skb(skb);
  738. skb = nskb;
  739. }
  740. return skb;
  741. }
  742. /*
  743. * Copy shared buffers into a new sk_buff. We effectively do COW on
  744. * packets to handle cases where we have a local reader and forward
  745. * and a couple of other messy ones. The normal one is tcpdumping
  746. * a packet thats being forwarded.
  747. */
  748. /**
  749. * skb_unshare - make a copy of a shared buffer
  750. * @skb: buffer to check
  751. * @pri: priority for memory allocation
  752. *
  753. * If the socket buffer is a clone then this function creates a new
  754. * copy of the data, drops a reference count on the old copy and returns
  755. * the new copy with the reference count at 1. If the buffer is not a clone
  756. * the original buffer is returned. When called with a spinlock held or
  757. * from interrupt state @pri must be %GFP_ATOMIC
  758. *
  759. * %NULL is returned on a memory allocation failure.
  760. */
  761. static inline struct sk_buff *skb_unshare(struct sk_buff *skb,
  762. gfp_t pri)
  763. {
  764. might_sleep_if(pri & __GFP_WAIT);
  765. if (skb_cloned(skb)) {
  766. struct sk_buff *nskb = skb_copy(skb, pri);
  767. kfree_skb(skb); /* Free our shared copy */
  768. skb = nskb;
  769. }
  770. return skb;
  771. }
  772. /**
  773. * skb_peek - peek at the head of an &sk_buff_head
  774. * @list_: list to peek at
  775. *
  776. * Peek an &sk_buff. Unlike most other operations you _MUST_
  777. * be careful with this one. A peek leaves the buffer on the
  778. * list and someone else may run off with it. You must hold
  779. * the appropriate locks or have a private queue to do this.
  780. *
  781. * Returns %NULL for an empty list or a pointer to the head element.
  782. * The reference count is not incremented and the reference is therefore
  783. * volatile. Use with caution.
  784. */
  785. static inline struct sk_buff *skb_peek(const struct sk_buff_head *list_)
  786. {
  787. struct sk_buff *list = ((const struct sk_buff *)list_)->next;
  788. if (list == (struct sk_buff *)list_)
  789. list = NULL;
  790. return list;
  791. }
  792. /**
  793. * skb_peek_next - peek skb following the given one from a queue
  794. * @skb: skb to start from
  795. * @list_: list to peek at
  796. *
  797. * Returns %NULL when the end of the list is met or a pointer to the
  798. * next element. The reference count is not incremented and the
  799. * reference is therefore volatile. Use with caution.
  800. */
  801. static inline struct sk_buff *skb_peek_next(struct sk_buff *skb,
  802. const struct sk_buff_head *list_)
  803. {
  804. struct sk_buff *next = skb->next;
  805. if (next == (struct sk_buff *)list_)
  806. next = NULL;
  807. return next;
  808. }
  809. /**
  810. * skb_peek_tail - peek at the tail of an &sk_buff_head
  811. * @list_: list to peek at
  812. *
  813. * Peek an &sk_buff. Unlike most other operations you _MUST_
  814. * be careful with this one. A peek leaves the buffer on the
  815. * list and someone else may run off with it. You must hold
  816. * the appropriate locks or have a private queue to do this.
  817. *
  818. * Returns %NULL for an empty list or a pointer to the tail element.
  819. * The reference count is not incremented and the reference is therefore
  820. * volatile. Use with caution.
  821. */
  822. static inline struct sk_buff *skb_peek_tail(const struct sk_buff_head *list_)
  823. {
  824. struct sk_buff *list = ((const struct sk_buff *)list_)->prev;
  825. if (list == (struct sk_buff *)list_)
  826. list = NULL;
  827. return list;
  828. }
  829. /**
  830. * skb_queue_len - get queue length
  831. * @list_: list to measure
  832. *
  833. * Return the length of an &sk_buff queue.
  834. */
  835. static inline __u32 skb_queue_len(const struct sk_buff_head *list_)
  836. {
  837. return list_->qlen;
  838. }
  839. /**
  840. * __skb_queue_head_init - initialize non-spinlock portions of sk_buff_head
  841. * @list: queue to initialize
  842. *
  843. * This initializes only the list and queue length aspects of
  844. * an sk_buff_head object. This allows to initialize the list
  845. * aspects of an sk_buff_head without reinitializing things like
  846. * the spinlock. It can also be used for on-stack sk_buff_head
  847. * objects where the spinlock is known to not be used.
  848. */
  849. static inline void __skb_queue_head_init(struct sk_buff_head *list)
  850. {
  851. list->prev = list->next = (struct sk_buff *)list;
  852. list->qlen = 0;
  853. }
  854. /*
  855. * This function creates a split out lock class for each invocation;
  856. * this is needed for now since a whole lot of users of the skb-queue
  857. * infrastructure in drivers have different locking usage (in hardirq)
  858. * than the networking core (in softirq only). In the long run either the
  859. * network layer or drivers should need annotation to consolidate the
  860. * main types of usage into 3 classes.
  861. */
  862. static inline void skb_queue_head_init(struct sk_buff_head *list)
  863. {
  864. spin_lock_init(&list->lock);
  865. __skb_queue_head_init(list);
  866. }
  867. static inline void skb_queue_head_init_class(struct sk_buff_head *list,
  868. struct lock_class_key *class)
  869. {
  870. skb_queue_head_init(list);
  871. lockdep_set_class(&list->lock, class);
  872. }
  873. /*
  874. * Insert an sk_buff on a list.
  875. *
  876. * The "__skb_xxxx()" functions are the non-atomic ones that
  877. * can only be called with interrupts disabled.
  878. */
  879. extern void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list);
  880. static inline void __skb_insert(struct sk_buff *newsk,
  881. struct sk_buff *prev, struct sk_buff *next,
  882. struct sk_buff_head *list)
  883. {
  884. newsk->next = next;
  885. newsk->prev = prev;
  886. next->prev = prev->next = newsk;
  887. list->qlen++;
  888. }
  889. static inline void __skb_queue_splice(const struct sk_buff_head *list,
  890. struct sk_buff *prev,
  891. struct sk_buff *next)
  892. {
  893. struct sk_buff *first = list->next;
  894. struct sk_buff *last = list->prev;
  895. first->prev = prev;
  896. prev->next = first;
  897. last->next = next;
  898. next->prev = last;
  899. }
  900. /**
  901. * skb_queue_splice - join two skb lists, this is designed for stacks
  902. * @list: the new list to add
  903. * @head: the place to add it in the first list
  904. */
  905. static inline void skb_queue_splice(const struct sk_buff_head *list,
  906. struct sk_buff_head *head)
  907. {
  908. if (!skb_queue_empty(list)) {
  909. __skb_queue_splice(list, (struct sk_buff *) head, head->next);
  910. head->qlen += list->qlen;
  911. }
  912. }
  913. /**
  914. * skb_queue_splice_init - join two skb lists and reinitialise the emptied list
  915. * @list: the new list to add
  916. * @head: the place to add it in the first list
  917. *
  918. * The list at @list is reinitialised
  919. */
  920. static inline void skb_queue_splice_init(struct sk_buff_head *list,
  921. struct sk_buff_head *head)
  922. {
  923. if (!skb_queue_empty(list)) {
  924. __skb_queue_splice(list, (struct sk_buff *) head, head->next);
  925. head->qlen += list->qlen;
  926. __skb_queue_head_init(list);
  927. }
  928. }
  929. /**
  930. * skb_queue_splice_tail - join two skb lists, each list being a queue
  931. * @list: the new list to add
  932. * @head: the place to add it in the first list
  933. */
  934. static inline void skb_queue_splice_tail(const struct sk_buff_head *list,
  935. struct sk_buff_head *head)
  936. {
  937. if (!skb_queue_empty(list)) {
  938. __skb_queue_splice(list, head->prev, (struct sk_buff *) head);
  939. head->qlen += list->qlen;
  940. }
  941. }
  942. /**
  943. * skb_queue_splice_tail_init - join two skb lists and reinitialise the emptied list
  944. * @list: the new list to add
  945. * @head: the place to add it in the first list
  946. *
  947. * Each of the lists is a queue.
  948. * The list at @list is reinitialised
  949. */
  950. static inline void skb_queue_splice_tail_init(struct sk_buff_head *list,
  951. struct sk_buff_head *head)
  952. {
  953. if (!skb_queue_empty(list)) {
  954. __skb_queue_splice(list, head->prev, (struct sk_buff *) head);
  955. head->qlen += list->qlen;
  956. __skb_queue_head_init(list);
  957. }
  958. }
  959. /**
  960. * __skb_queue_after - queue a buffer at the list head
  961. * @list: list to use
  962. * @prev: place after this buffer
  963. * @newsk: buffer to queue
  964. *
  965. * Queue a buffer int the middle of a list. This function takes no locks
  966. * and you must therefore hold required locks before calling it.
  967. *
  968. * A buffer cannot be placed on two lists at the same time.
  969. */
  970. static inline void __skb_queue_after(struct sk_buff_head *list,
  971. struct sk_buff *prev,
  972. struct sk_buff *newsk)
  973. {
  974. __skb_insert(newsk, prev, prev->next, list);
  975. }
  976. extern void skb_append(struct sk_buff *old, struct sk_buff *newsk,
  977. struct sk_buff_head *list);
  978. static inline void __skb_queue_before(struct sk_buff_head *list,
  979. struct sk_buff *next,
  980. struct sk_buff *newsk)
  981. {
  982. __skb_insert(newsk, next->prev, next, list);
  983. }
  984. /**
  985. * __skb_queue_head - queue a buffer at the list head
  986. * @list: list to use
  987. * @newsk: buffer to queue
  988. *
  989. * Queue a buffer at the start of a list. This function takes no locks
  990. * and you must therefore hold required locks before calling it.
  991. *
  992. * A buffer cannot be placed on two lists at the same time.
  993. */
  994. extern void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk);
  995. static inline void __skb_queue_head(struct sk_buff_head *list,
  996. struct sk_buff *newsk)
  997. {
  998. __skb_queue_after(list, (struct sk_buff *)list, newsk);
  999. }
  1000. /**
  1001. * __skb_queue_tail - queue a buffer at the list tail
  1002. * @list: list to use
  1003. * @newsk: buffer to queue
  1004. *
  1005. * Queue a buffer at the end of a list. This function takes no locks
  1006. * and you must therefore hold required locks before calling it.
  1007. *
  1008. * A buffer cannot be placed on two lists at the same time.
  1009. */
  1010. extern void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk);
  1011. static inline void __skb_queue_tail(struct sk_buff_head *list,
  1012. struct sk_buff *newsk)
  1013. {
  1014. __skb_queue_before(list, (struct sk_buff *)list, newsk);
  1015. }
  1016. /*
  1017. * remove sk_buff from list. _Must_ be called atomically, and with
  1018. * the list known..
  1019. */
  1020. extern void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list);
  1021. static inline void __skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
  1022. {
  1023. struct sk_buff *next, *prev;
  1024. list->qlen--;
  1025. next = skb->next;
  1026. prev = skb->prev;
  1027. skb->next = skb->prev = NULL;
  1028. next->prev = prev;
  1029. prev->next = next;
  1030. }
  1031. /**
  1032. * __skb_dequeue - remove from the head of the queue
  1033. * @list: list to dequeue from
  1034. *
  1035. * Remove the head of the list. This function does not take any locks
  1036. * so must be used with appropriate locks held only. The head item is
  1037. * returned or %NULL if the list is empty.
  1038. */
  1039. extern struct sk_buff *skb_dequeue(struct sk_buff_head *list);
  1040. static inline struct sk_buff *__skb_dequeue(struct sk_buff_head *list)
  1041. {
  1042. struct sk_buff *skb = skb_peek(list);
  1043. if (skb)
  1044. __skb_unlink(skb, list);
  1045. return skb;
  1046. }
  1047. /**
  1048. * __skb_dequeue_tail - remove from the tail of the queue
  1049. * @list: list to dequeue from
  1050. *
  1051. * Remove the tail of the list. This function does not take any locks
  1052. * so must be used with appropriate locks held only. The tail item is
  1053. * returned or %NULL if the list is empty.
  1054. */
  1055. extern struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list);
  1056. static inline struct sk_buff *__skb_dequeue_tail(struct sk_buff_head *list)
  1057. {
  1058. struct sk_buff *skb = skb_peek_tail(list);
  1059. if (skb)
  1060. __skb_unlink(skb, list);
  1061. return skb;
  1062. }
  1063. static inline bool skb_is_nonlinear(const struct sk_buff *skb)
  1064. {
  1065. return skb->data_len;
  1066. }
  1067. static inline unsigned int skb_headlen(const struct sk_buff *skb)
  1068. {
  1069. return skb->len - skb->data_len;
  1070. }
  1071. static inline int skb_pagelen(const struct sk_buff *skb)
  1072. {
  1073. int i, len = 0;
  1074. for (i = (int)skb_shinfo(skb)->nr_frags - 1; i >= 0; i--)
  1075. len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
  1076. return len + skb_headlen(skb);
  1077. }
  1078. /**
  1079. * __skb_fill_page_desc - initialise a paged fragment in an skb
  1080. * @skb: buffer containing fragment to be initialised
  1081. * @i: paged fragment index to initialise
  1082. * @page: the page to use for this fragment
  1083. * @off: the offset to the data with @page
  1084. * @size: the length of the data
  1085. *
  1086. * Initialises the @i'th fragment of @skb to point to &size bytes at
  1087. * offset @off within @page.
  1088. *
  1089. * Does not take any additional reference on the fragment.
  1090. */
  1091. static inline void __skb_fill_page_desc(struct sk_buff *skb, int i,
  1092. struct page *page, int off, int size)
  1093. {
  1094. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  1095. frag->page.p = page;
  1096. frag->page_offset = off;
  1097. skb_frag_size_set(frag, size);
  1098. }
  1099. /**
  1100. * skb_fill_page_desc - initialise a paged fragment in an skb
  1101. * @skb: buffer containing fragment to be initialised
  1102. * @i: paged fragment index to initialise
  1103. * @page: the page to use for this fragment
  1104. * @off: the offset to the data with @page
  1105. * @size: the length of the data
  1106. *
  1107. * As per __skb_fill_page_desc() -- initialises the @i'th fragment of
  1108. * @skb to point to &size bytes at offset @off within @page. In
  1109. * addition updates @skb such that @i is the last fragment.
  1110. *
  1111. * Does not take any additional reference on the fragment.
  1112. */
  1113. static inline void skb_fill_page_desc(struct sk_buff *skb, int i,
  1114. struct page *page, int off, int size)
  1115. {
  1116. __skb_fill_page_desc(skb, i, page, off, size);
  1117. skb_shinfo(skb)->nr_frags = i + 1;
  1118. }
  1119. extern void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page,
  1120. int off, int size, unsigned int truesize);
  1121. #define SKB_PAGE_ASSERT(skb) BUG_ON(skb_shinfo(skb)->nr_frags)
  1122. #define SKB_FRAG_ASSERT(skb) BUG_ON(skb_has_frag_list(skb))
  1123. #define SKB_LINEAR_ASSERT(skb) BUG_ON(skb_is_nonlinear(skb))
  1124. #ifdef NET_SKBUFF_DATA_USES_OFFSET
  1125. static inline unsigned char *skb_tail_pointer(const struct sk_buff *skb)
  1126. {
  1127. return skb->head + skb->tail;
  1128. }
  1129. static inline void skb_reset_tail_pointer(struct sk_buff *skb)
  1130. {
  1131. skb->tail = skb->data - skb->head;
  1132. }
  1133. static inline void skb_set_tail_pointer(struct sk_buff *skb, const int offset)
  1134. {
  1135. skb_reset_tail_pointer(skb);
  1136. skb->tail += offset;
  1137. }
  1138. #else /* NET_SKBUFF_DATA_USES_OFFSET */
  1139. static inline unsigned char *skb_tail_pointer(const struct sk_buff *skb)
  1140. {
  1141. return skb->tail;
  1142. }
  1143. static inline void skb_reset_tail_pointer(struct sk_buff *skb)
  1144. {
  1145. skb->tail = skb->data;
  1146. }
  1147. static inline void skb_set_tail_pointer(struct sk_buff *skb, const int offset)
  1148. {
  1149. skb->tail = skb->data + offset;
  1150. }
  1151. #endif /* NET_SKBUFF_DATA_USES_OFFSET */
  1152. /*
  1153. * Add data to an sk_buff
  1154. */
  1155. extern unsigned char *skb_put(struct sk_buff *skb, unsigned int len);
  1156. static inline unsigned char *__skb_put(struct sk_buff *skb, unsigned int len)
  1157. {
  1158. unsigned char *tmp = skb_tail_pointer(skb);
  1159. SKB_LINEAR_ASSERT(skb);
  1160. skb->tail += len;
  1161. skb->len += len;
  1162. return tmp;
  1163. }
  1164. extern unsigned char *skb_push(struct sk_buff *skb, unsigned int len);
  1165. static inline unsigned char *__skb_push(struct sk_buff *skb, unsigned int len)
  1166. {
  1167. skb->data -= len;
  1168. skb->len += len;
  1169. return skb->data;
  1170. }
  1171. extern unsigned char *skb_pull(struct sk_buff *skb, unsigned int len);
  1172. static inline unsigned char *__skb_pull(struct sk_buff *skb, unsigned int len)
  1173. {
  1174. skb->len -= len;
  1175. BUG_ON(skb->len < skb->data_len);
  1176. return skb->data += len;
  1177. }
  1178. static inline unsigned char *skb_pull_inline(struct sk_buff *skb, unsigned int len)
  1179. {
  1180. return unlikely(len > skb->len) ? NULL : __skb_pull(skb, len);
  1181. }
  1182. extern unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta);
  1183. static inline unsigned char *__pskb_pull(struct sk_buff *skb, unsigned int len)
  1184. {
  1185. if (len > skb_headlen(skb) &&
  1186. !__pskb_pull_tail(skb, len - skb_headlen(skb)))
  1187. return NULL;
  1188. skb->len -= len;
  1189. return skb->data += len;
  1190. }
  1191. static inline unsigned char *pskb_pull(struct sk_buff *skb, unsigned int len)
  1192. {
  1193. return unlikely(len > skb->len) ? NULL : __pskb_pull(skb, len);
  1194. }
  1195. static inline int pskb_may_pull(struct sk_buff *skb, unsigned int len)
  1196. {
  1197. if (likely(len <= skb_headlen(skb)))
  1198. return 1;
  1199. if (unlikely(len > skb->len))
  1200. return 0;
  1201. return __pskb_pull_tail(skb, len - skb_headlen(skb)) != NULL;
  1202. }
  1203. /**
  1204. * skb_headroom - bytes at buffer head
  1205. * @skb: buffer to check
  1206. *
  1207. * Return the number of bytes of free space at the head of an &sk_buff.
  1208. */
  1209. static inline unsigned int skb_headroom(const struct sk_buff *skb)
  1210. {
  1211. return skb->data - skb->head;
  1212. }
  1213. /**
  1214. * skb_tailroom - bytes at buffer end
  1215. * @skb: buffer to check
  1216. *
  1217. * Return the number of bytes of free space at the tail of an sk_buff
  1218. */
  1219. static inline int skb_tailroom(const struct sk_buff *skb)
  1220. {
  1221. return skb_is_nonlinear(skb) ? 0 : skb->end - skb->tail;
  1222. }
  1223. /**
  1224. * skb_availroom - bytes at buffer end
  1225. * @skb: buffer to check
  1226. *
  1227. * Return the number of bytes of free space at the tail of an sk_buff
  1228. * allocated by sk_stream_alloc()
  1229. */
  1230. static inline int skb_availroom(const struct sk_buff *skb)
  1231. {
  1232. return skb_is_nonlinear(skb) ? 0 : skb->avail_size - skb->len;
  1233. }
  1234. /**
  1235. * skb_reserve - adjust headroom
  1236. * @skb: buffer to alter
  1237. * @len: bytes to move
  1238. *
  1239. * Increase the headroom of an empty &sk_buff by reducing the tail
  1240. * room. This is only allowed for an empty buffer.
  1241. */
  1242. static inline void skb_reserve(struct sk_buff *skb, int len)
  1243. {
  1244. skb->data += len;
  1245. skb->tail += len;
  1246. }
  1247. static inline void skb_reset_mac_len(struct sk_buff *skb)
  1248. {
  1249. skb->mac_len = skb->network_header - skb->mac_header;
  1250. }
  1251. #ifdef NET_SKBUFF_DATA_USES_OFFSET
  1252. static inline unsigned char *skb_transport_header(const struct sk_buff *skb)
  1253. {
  1254. return skb->head + skb->transport_header;
  1255. }
  1256. static inline void skb_reset_transport_header(struct sk_buff *skb)
  1257. {
  1258. skb->transport_header = skb->data - skb->head;
  1259. }
  1260. static inline void skb_set_transport_header(struct sk_buff *skb,
  1261. const int offset)
  1262. {
  1263. skb_reset_transport_header(skb);
  1264. skb->transport_header += offset;
  1265. }
  1266. static inline unsigned char *skb_network_header(const struct sk_buff *skb)
  1267. {
  1268. return skb->head + skb->network_header;
  1269. }
  1270. static inline void skb_reset_network_header(struct sk_buff *skb)
  1271. {
  1272. skb->network_header = skb->data - skb->head;
  1273. }
  1274. static inline void skb_set_network_header(struct sk_buff *skb, const int offset)
  1275. {
  1276. skb_reset_network_header(skb);
  1277. skb->network_header += offset;
  1278. }
  1279. static inline unsigned char *skb_mac_header(const struct sk_buff *skb)
  1280. {
  1281. return skb->head + skb->mac_header;
  1282. }
  1283. static inline int skb_mac_header_was_set(const struct sk_buff *skb)
  1284. {
  1285. return skb->mac_header != ~0U;
  1286. }
  1287. static inline void skb_reset_mac_header(struct sk_buff *skb)
  1288. {
  1289. skb->mac_header = skb->data - skb->head;
  1290. }
  1291. static inline void skb_set_mac_header(struct sk_buff *skb, const int offset)
  1292. {
  1293. skb_reset_mac_header(skb);
  1294. skb->mac_header += offset;
  1295. }
  1296. #else /* NET_SKBUFF_DATA_USES_OFFSET */
  1297. static inline unsigned char *skb_transport_header(const struct sk_buff *skb)
  1298. {
  1299. return skb->transport_header;
  1300. }
  1301. static inline void skb_reset_transport_header(struct sk_buff *skb)
  1302. {
  1303. skb->transport_header = skb->data;
  1304. }
  1305. static inline void skb_set_transport_header(struct sk_buff *skb,
  1306. const int offset)
  1307. {
  1308. skb->transport_header = skb->data + offset;
  1309. }
  1310. static inline unsigned char *skb_network_header(const struct sk_buff *skb)
  1311. {
  1312. return skb->network_header;
  1313. }
  1314. static inline void skb_reset_network_header(struct sk_buff *skb)
  1315. {
  1316. skb->network_header = skb->data;
  1317. }
  1318. static inline void skb_set_network_header(struct sk_buff *skb, const int offset)
  1319. {
  1320. skb->network_header = skb->data + offset;
  1321. }
  1322. static inline unsigned char *skb_mac_header(const struct sk_buff *skb)
  1323. {
  1324. return skb->mac_header;
  1325. }
  1326. static inline int skb_mac_header_was_set(const struct sk_buff *skb)
  1327. {
  1328. return skb->mac_header != NULL;
  1329. }
  1330. static inline void skb_reset_mac_header(struct sk_buff *skb)
  1331. {
  1332. skb->mac_header = skb->data;
  1333. }
  1334. static inline void skb_set_mac_header(struct sk_buff *skb, const int offset)
  1335. {
  1336. skb->mac_header = skb->data + offset;
  1337. }
  1338. #endif /* NET_SKBUFF_DATA_USES_OFFSET */
  1339. static inline void skb_mac_header_rebuild(struct sk_buff *skb)
  1340. {
  1341. if (skb_mac_header_was_set(skb)) {
  1342. const unsigned char *old_mac = skb_mac_header(skb);
  1343. skb_set_mac_header(skb, -skb->mac_len);
  1344. memmove(skb_mac_header(skb), old_mac, skb->mac_len);
  1345. }
  1346. }
  1347. static inline int skb_checksum_start_offset(const struct sk_buff *skb)
  1348. {
  1349. return skb->csum_start - skb_headroom(skb);
  1350. }
  1351. static inline int skb_transport_offset(const struct sk_buff *skb)
  1352. {
  1353. return skb_transport_header(skb) - skb->data;
  1354. }
  1355. static inline u32 skb_network_header_len(const struct sk_buff *skb)
  1356. {
  1357. return skb->transport_header - skb->network_header;
  1358. }
  1359. static inline int skb_network_offset(const struct sk_buff *skb)
  1360. {
  1361. return skb_network_header(skb) - skb->data;
  1362. }
  1363. static inline int pskb_network_may_pull(struct sk_buff *skb, unsigned int len)
  1364. {
  1365. return pskb_may_pull(skb, skb_network_offset(skb) + len);
  1366. }
  1367. /*
  1368. * CPUs often take a performance hit when accessing unaligned memory
  1369. * locations. The actual performance hit varies, it can be small if the
  1370. * hardware handles it or large if we have to take an exception and fix it
  1371. * in software.
  1372. *
  1373. * Since an ethernet header is 14 bytes network drivers often end up with
  1374. * the IP header at an unaligned offset. The IP header can be aligned by
  1375. * shifting the start of the packet by 2 bytes. Drivers should do this
  1376. * with:
  1377. *
  1378. * skb_reserve(skb, NET_IP_ALIGN);
  1379. *
  1380. * The downside to this alignment of the IP header is that the DMA is now
  1381. * unaligned. On some architectures the cost of an unaligned DMA is high
  1382. * and this cost outweighs the gains made by aligning the IP header.
  1383. *
  1384. * Since this trade off varies between architectures, we allow NET_IP_ALIGN
  1385. * to be overridden.
  1386. */
  1387. #ifndef NET_IP_ALIGN
  1388. #define NET_IP_ALIGN 2
  1389. #endif
  1390. /*
  1391. * The networking layer reserves some headroom in skb data (via
  1392. * dev_alloc_skb). This is used to avoid having to reallocate skb data when
  1393. * the header has to grow. In the default case, if the header has to grow
  1394. * 32 bytes or less we avoid the reallocation.
  1395. *
  1396. * Unfortunately this headroom changes the DMA alignment of the resulting
  1397. * network packet. As for NET_IP_ALIGN, this unaligned DMA is expensive
  1398. * on some architectures. An architecture can override this value,
  1399. * perhaps setting it to a cacheline in size (since that will maintain
  1400. * cacheline alignment of the DMA). It must be a power of 2.
  1401. *
  1402. * Various parts of the networking layer expect at least 32 bytes of
  1403. * headroom, you should not reduce this.
  1404. *
  1405. * Using max(32, L1_CACHE_BYTES) makes sense (especially with RPS)
  1406. * to reduce average number of cache lines per packet.
  1407. * get_rps_cpus() for example only access one 64 bytes aligned block :
  1408. * NET_IP_ALIGN(2) + ethernet_header(14) + IP_header(20/40) + ports(8)
  1409. */
  1410. #ifndef NET_SKB_PAD
  1411. #define NET_SKB_PAD max(32, L1_CACHE_BYTES)
  1412. #endif
  1413. extern int ___pskb_trim(struct sk_buff *skb, unsigned int len);
  1414. static inline void __skb_trim(struct sk_buff *skb, unsigned int len)
  1415. {
  1416. if (unlikely(skb_is_nonlinear(skb))) {
  1417. WARN_ON(1);
  1418. return;
  1419. }
  1420. skb->len = len;
  1421. skb_set_tail_pointer(skb, len);
  1422. }
  1423. extern void skb_trim(struct sk_buff *skb, unsigned int len);
  1424. static inline int __pskb_trim(struct sk_buff *skb, unsigned int len)
  1425. {
  1426. if (skb->data_len)
  1427. return ___pskb_trim(skb, len);
  1428. __skb_trim(skb, len);
  1429. return 0;
  1430. }
  1431. static inline int pskb_trim(struct sk_buff *skb, unsigned int len)
  1432. {
  1433. return (len < skb->len) ? __pskb_trim(skb, len) : 0;
  1434. }
  1435. /**
  1436. * pskb_trim_unique - remove end from a paged unique (not cloned) buffer
  1437. * @skb: buffer to alter
  1438. * @len: new length
  1439. *
  1440. * This is identical to pskb_trim except that the caller knows that
  1441. * the skb is not cloned so we should never get an error due to out-
  1442. * of-memory.
  1443. */
  1444. static inline void pskb_trim_unique(struct sk_buff *skb, unsigned int len)
  1445. {
  1446. int err = pskb_trim(skb, len);
  1447. BUG_ON(err);
  1448. }
  1449. /**
  1450. * skb_orphan - orphan a buffer
  1451. * @skb: buffer to orphan
  1452. *
  1453. * If a buffer currently has an owner then we call the owner's
  1454. * destructor function and make the @skb unowned. The buffer continues
  1455. * to exist but is no longer charged to its former owner.
  1456. */
  1457. static inline void skb_orphan(struct sk_buff *skb)
  1458. {
  1459. if (skb->destructor)
  1460. skb->destructor(skb);
  1461. skb->destructor = NULL;
  1462. skb->sk = NULL;
  1463. }
  1464. /**
  1465. * __skb_queue_purge - empty a list
  1466. * @list: list to empty
  1467. *
  1468. * Delete all buffers on an &sk_buff list. Each buffer is removed from
  1469. * the list and one reference dropped. This function does not take the
  1470. * list lock and the caller must hold the relevant locks to use it.
  1471. */
  1472. extern void skb_queue_purge(struct sk_buff_head *list);
  1473. static inline void __skb_queue_purge(struct sk_buff_head *list)
  1474. {
  1475. struct sk_buff *skb;
  1476. while ((skb = __skb_dequeue(list)) != NULL)
  1477. kfree_skb(skb);
  1478. }
  1479. /**
  1480. * __dev_alloc_skb - allocate an skbuff for receiving
  1481. * @length: length to allocate
  1482. * @gfp_mask: get_free_pages mask, passed to alloc_skb
  1483. *
  1484. * Allocate a new &sk_buff and assign it a usage count of one. The
  1485. * buffer has unspecified headroom built in. Users should allocate
  1486. * the headroom they think they need without accounting for the
  1487. * built in space. The built in space is used for optimisations.
  1488. *
  1489. * %NULL is returned if there is no free memory.
  1490. */
  1491. static inline struct sk_buff *__dev_alloc_skb(unsigned int length,
  1492. gfp_t gfp_mask)
  1493. {
  1494. struct sk_buff *skb = alloc_skb(length + NET_SKB_PAD, gfp_mask);
  1495. if (likely(skb))
  1496. skb_reserve(skb, NET_SKB_PAD);
  1497. return skb;
  1498. }
  1499. extern struct sk_buff *dev_alloc_skb(unsigned int length);
  1500. extern struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
  1501. unsigned int length, gfp_t gfp_mask);
  1502. /**
  1503. * netdev_alloc_skb - allocate an skbuff for rx on a specific device
  1504. * @dev: network device to receive on
  1505. * @length: length to allocate
  1506. *
  1507. * Allocate a new &sk_buff and assign it a usage count of one. The
  1508. * buffer has unspecified headroom built in. Users should allocate
  1509. * the headroom they think they need without accounting for the
  1510. * built in space. The built in space is used for optimisations.
  1511. *
  1512. * %NULL is returned if there is no free memory. Although this function
  1513. * allocates memory it can be called from an interrupt.
  1514. */
  1515. static inline struct sk_buff *netdev_alloc_skb(struct net_device *dev,
  1516. unsigned int length)
  1517. {
  1518. return __netdev_alloc_skb(dev, length, GFP_ATOMIC);
  1519. }
  1520. static inline struct sk_buff *__netdev_alloc_skb_ip_align(struct net_device *dev,
  1521. unsigned int length, gfp_t gfp)
  1522. {
  1523. struct sk_buff *skb = __netdev_alloc_skb(dev, length + NET_IP_ALIGN, gfp);
  1524. if (NET_IP_ALIGN && skb)
  1525. skb_reserve(skb, NET_IP_ALIGN);
  1526. return skb;
  1527. }
  1528. static inline struct sk_buff *netdev_alloc_skb_ip_align(struct net_device *dev,
  1529. unsigned int length)
  1530. {
  1531. return __netdev_alloc_skb_ip_align(dev, length, GFP_ATOMIC);
  1532. }
  1533. /**
  1534. * skb_frag_page - retrieve the page refered to by a paged fragment
  1535. * @frag: the paged fragment
  1536. *
  1537. * Returns the &struct page associated with @frag.
  1538. */
  1539. static inline struct page *skb_frag_page(const skb_frag_t *frag)
  1540. {
  1541. return frag->page.p;
  1542. }
  1543. /**
  1544. * __skb_frag_ref - take an addition reference on a paged fragment.
  1545. * @frag: the paged fragment
  1546. *
  1547. * Takes an additional reference on the paged fragment @frag.
  1548. */
  1549. static inline void __skb_frag_ref(skb_frag_t *frag)
  1550. {
  1551. get_page(skb_frag_page(frag));
  1552. }
  1553. /**
  1554. * skb_frag_ref - take an addition reference on a paged fragment of an skb.
  1555. * @skb: the buffer
  1556. * @f: the fragment offset.
  1557. *
  1558. * Takes an additional reference on the @f'th paged fragment of @skb.
  1559. */
  1560. static inline void skb_frag_ref(struct sk_buff *skb, int f)
  1561. {
  1562. __skb_frag_ref(&skb_shinfo(skb)->frags[f]);
  1563. }
  1564. /**
  1565. * __skb_frag_unref - release a reference on a paged fragment.
  1566. * @frag: the paged fragment
  1567. *
  1568. * Releases a reference on the paged fragment @frag.
  1569. */
  1570. static inline void __skb_frag_unref(skb_frag_t *frag)
  1571. {
  1572. put_page(skb_frag_page(frag));
  1573. }
  1574. /**
  1575. * skb_frag_unref - release a reference on a paged fragment of an skb.
  1576. * @skb: the buffer
  1577. * @f: the fragment offset
  1578. *
  1579. * Releases a reference on the @f'th paged fragment of @skb.
  1580. */
  1581. static inline void skb_frag_unref(struct sk_buff *skb, int f)
  1582. {
  1583. __skb_frag_unref(&skb_shinfo(skb)->frags[f]);
  1584. }
  1585. /**
  1586. * skb_frag_address - gets the address of the data contained in a paged fragment
  1587. * @frag: the paged fragment buffer
  1588. *
  1589. * Returns the address of the data within @frag. The page must already
  1590. * be mapped.
  1591. */
  1592. static inline void *skb_frag_address(const skb_frag_t *frag)
  1593. {
  1594. return page_address(skb_frag_page(frag)) + frag->page_offset;
  1595. }
  1596. /**
  1597. * skb_frag_address_safe - gets the address of the data contained in a paged fragment
  1598. * @frag: the paged fragment buffer
  1599. *
  1600. * Returns the address of the data within @frag. Checks that the page
  1601. * is mapped and returns %NULL otherwise.
  1602. */
  1603. static inline void *skb_frag_address_safe(const skb_frag_t *frag)
  1604. {
  1605. void *ptr = page_address(skb_frag_page(frag));
  1606. if (unlikely(!ptr))
  1607. return NULL;
  1608. return ptr + frag->page_offset;
  1609. }
  1610. /**
  1611. * __skb_frag_set_page - sets the page contained in a paged fragment
  1612. * @frag: the paged fragment
  1613. * @page: the page to set
  1614. *
  1615. * Sets the fragment @frag to contain @page.
  1616. */
  1617. static inline void __skb_frag_set_page(skb_frag_t *frag, struct page *page)
  1618. {
  1619. frag->page.p = page;
  1620. }
  1621. /**
  1622. * skb_frag_set_page - sets the page contained in a paged fragment of an skb
  1623. * @skb: the buffer
  1624. * @f: the fragment offset
  1625. * @page: the page to set
  1626. *
  1627. * Sets the @f'th fragment of @skb to contain @page.
  1628. */
  1629. static inline void skb_frag_set_page(struct sk_buff *skb, int f,
  1630. struct page *page)
  1631. {
  1632. __skb_frag_set_page(&skb_shinfo(skb)->frags[f], page);
  1633. }
  1634. /**
  1635. * skb_frag_dma_map - maps a paged fragment via the DMA API
  1636. * @dev: the device to map the fragment to
  1637. * @frag: the paged fragment to map
  1638. * @offset: the offset within the fragment (starting at the
  1639. * fragment's own offset)
  1640. * @size: the number of bytes to map
  1641. * @dir: the direction of the mapping (%PCI_DMA_*)
  1642. *
  1643. * Maps the page associated with @frag to @device.
  1644. */
  1645. static inline dma_addr_t skb_frag_dma_map(struct device *dev,
  1646. const skb_frag_t *frag,
  1647. size_t offset, size_t size,
  1648. enum dma_data_direction dir)
  1649. {
  1650. return dma_map_page(dev, skb_frag_page(frag),
  1651. frag->page_offset + offset, size, dir);
  1652. }
  1653. static inline struct sk_buff *pskb_copy(struct sk_buff *skb,
  1654. gfp_t gfp_mask)
  1655. {
  1656. return __pskb_copy(skb, skb_headroom(skb), gfp_mask);
  1657. }
  1658. /**
  1659. * skb_clone_writable - is the header of a clone writable
  1660. * @skb: buffer to check
  1661. * @len: length up to which to write
  1662. *
  1663. * Returns true if modifying the header part of the cloned buffer
  1664. * does not requires the data to be copied.
  1665. */
  1666. static inline int skb_clone_writable(const struct sk_buff *skb, unsigned int len)
  1667. {
  1668. return !skb_header_cloned(skb) &&
  1669. skb_headroom(skb) + len <= skb->hdr_len;
  1670. }
  1671. static inline int __skb_cow(struct sk_buff *skb, unsigned int headroom,
  1672. int cloned)
  1673. {
  1674. int delta = 0;
  1675. if (headroom < NET_SKB_PAD)
  1676. headroom = NET_SKB_PAD;
  1677. if (headroom > skb_headroom(skb))
  1678. delta = headroom - skb_headroom(skb);
  1679. if (delta || cloned)
  1680. return pskb_expand_head(skb, ALIGN(delta, NET_SKB_PAD), 0,
  1681. GFP_ATOMIC);
  1682. return 0;
  1683. }
  1684. /**
  1685. * skb_cow - copy header of skb when it is required
  1686. * @skb: buffer to cow
  1687. * @headroom: needed headroom
  1688. *
  1689. * If the skb passed lacks sufficient headroom or its data part
  1690. * is shared, data is reallocated. If reallocation fails, an error
  1691. * is returned and original skb is not changed.
  1692. *
  1693. * The result is skb with writable area skb->head...skb->tail
  1694. * and at least @headroom of space at head.
  1695. */
  1696. static inline int skb_cow(struct sk_buff *skb, unsigned int headroom)
  1697. {
  1698. return __skb_cow(skb, headroom, skb_cloned(skb));
  1699. }
  1700. /**
  1701. * skb_cow_head - skb_cow but only making the head writable
  1702. * @skb: buffer to cow
  1703. * @headroom: needed headroom
  1704. *
  1705. * This function is identical to skb_cow except that we replace the
  1706. * skb_cloned check by skb_header_cloned. It should be used when
  1707. * you only need to push on some header and do not need to modify
  1708. * the data.
  1709. */
  1710. static inline int skb_cow_head(struct sk_buff *skb, unsigned int headroom)
  1711. {
  1712. return __skb_cow(skb, headroom, skb_header_cloned(skb));
  1713. }
  1714. /**
  1715. * skb_padto - pad an skbuff up to a minimal size
  1716. * @skb: buffer to pad
  1717. * @len: minimal length
  1718. *
  1719. * Pads up a buffer to ensure the trailing bytes exist and are
  1720. * blanked. If the buffer already contains sufficient data it
  1721. * is untouched. Otherwise it is extended. Returns zero on
  1722. * success. The skb is freed on error.
  1723. */
  1724. static inline int skb_padto(struct sk_buff *skb, unsigned int len)
  1725. {
  1726. unsigned int size = skb->len;
  1727. if (likely(size >= len))
  1728. return 0;
  1729. return skb_pad(skb, len - size);
  1730. }
  1731. static inline int skb_add_data(struct sk_buff *skb,
  1732. char __user *from, int copy)
  1733. {
  1734. const int off = skb->len;
  1735. if (skb->ip_summed == CHECKSUM_NONE) {
  1736. int err = 0;
  1737. __wsum csum = csum_and_copy_from_user(from, skb_put(skb, copy),
  1738. copy, 0, &err);
  1739. if (!err) {
  1740. skb->csum = csum_block_add(skb->csum, csum, off);
  1741. return 0;
  1742. }
  1743. } else if (!copy_from_user(skb_put(skb, copy), from, copy))
  1744. return 0;
  1745. __skb_trim(skb, off);
  1746. return -EFAULT;
  1747. }
  1748. static inline int skb_can_coalesce(struct sk_buff *skb, int i,
  1749. const struct page *page, int off)
  1750. {
  1751. if (i) {
  1752. const struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i - 1];
  1753. return page == skb_frag_page(frag) &&
  1754. off == frag->page_offset + skb_frag_size(frag);
  1755. }
  1756. return 0;
  1757. }
  1758. static inline int __skb_linearize(struct sk_buff *skb)
  1759. {
  1760. return __pskb_pull_tail(skb, skb->data_len) ? 0 : -ENOMEM;
  1761. }
  1762. /**
  1763. * skb_linearize - convert paged skb to linear one
  1764. * @skb: buffer to linarize
  1765. *
  1766. * If there is no free memory -ENOMEM is returned, otherwise zero
  1767. * is returned and the old skb data released.
  1768. */
  1769. static inline int skb_linearize(struct sk_buff *skb)
  1770. {
  1771. return skb_is_nonlinear(skb) ? __skb_linearize(skb) : 0;
  1772. }
  1773. /**
  1774. * skb_linearize_cow - make sure skb is linear and writable
  1775. * @skb: buffer to process
  1776. *
  1777. * If there is no free memory -ENOMEM is returned, otherwise zero
  1778. * is returned and the old skb data released.
  1779. */
  1780. static inline int skb_linearize_cow(struct sk_buff *skb)
  1781. {
  1782. return skb_is_nonlinear(skb) || skb_cloned(skb) ?
  1783. __skb_linearize(skb) : 0;
  1784. }
  1785. /**
  1786. * skb_postpull_rcsum - update checksum for received skb after pull
  1787. * @skb: buffer to update
  1788. * @start: start of data before pull
  1789. * @len: length of data pulled
  1790. *
  1791. * After doing a pull on a received packet, you need to call this to
  1792. * update the CHECKSUM_COMPLETE checksum, or set ip_summed to
  1793. * CHECKSUM_NONE so that it can be recomputed from scratch.
  1794. */
  1795. static inline void skb_postpull_rcsum(struct sk_buff *skb,
  1796. const void *start, unsigned int len)
  1797. {
  1798. if (skb->ip_summed == CHECKSUM_COMPLETE)
  1799. skb->csum = csum_sub(skb->csum, csum_partial(start, len, 0));
  1800. }
  1801. unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len);
  1802. /**
  1803. * pskb_trim_rcsum - trim received skb and update checksum
  1804. * @skb: buffer to trim
  1805. * @len: new length
  1806. *
  1807. * This is exactly the same as pskb_trim except that it ensures the
  1808. * checksum of received packets are still valid after the operation.
  1809. */
  1810. static inline int pskb_trim_rcsum(struct sk_buff *skb, unsigned int len)
  1811. {
  1812. if (likely(len >= skb->len))
  1813. return 0;
  1814. if (skb->ip_summed == CHECKSUM_COMPLETE)
  1815. skb->ip_summed = CHECKSUM_NONE;
  1816. return __pskb_trim(skb, len);
  1817. }
  1818. #define skb_queue_walk(queue, skb) \
  1819. for (skb = (queue)->next; \
  1820. skb != (struct sk_buff *)(queue); \
  1821. skb = skb->next)
  1822. #define skb_queue_walk_safe(queue, skb, tmp) \
  1823. for (skb = (queue)->next, tmp = skb->next; \
  1824. skb != (struct sk_buff *)(queue); \
  1825. skb = tmp, tmp = skb->next)
  1826. #define skb_queue_walk_from(queue, skb) \
  1827. for (; skb != (struct sk_buff *)(queue); \
  1828. skb = skb->next)
  1829. #define skb_queue_walk_from_safe(queue, skb, tmp) \
  1830. for (tmp = skb->next; \
  1831. skb != (struct sk_buff *)(queue); \
  1832. skb = tmp, tmp = skb->next)
  1833. #define skb_queue_reverse_walk(queue, skb) \
  1834. for (skb = (queue)->prev; \
  1835. skb != (struct sk_buff *)(queue); \
  1836. skb = skb->prev)
  1837. #define skb_queue_reverse_walk_safe(queue, skb, tmp) \
  1838. for (skb = (queue)->prev, tmp = skb->prev; \
  1839. skb != (struct sk_buff *)(queue); \
  1840. skb = tmp, tmp = skb->prev)
  1841. #define skb_queue_reverse_walk_from_safe(queue, skb, tmp) \
  1842. for (tmp = skb->prev; \
  1843. skb != (struct sk_buff *)(queue); \
  1844. skb = tmp, tmp = skb->prev)
  1845. static inline bool skb_has_frag_list(const struct sk_buff *skb)
  1846. {
  1847. return skb_shinfo(skb)->frag_list != NULL;
  1848. }
  1849. static inline void skb_frag_list_init(struct sk_buff *skb)
  1850. {
  1851. skb_shinfo(skb)->frag_list = NULL;
  1852. }
  1853. static inline void skb_frag_add_head(struct sk_buff *skb, struct sk_buff *frag)
  1854. {
  1855. frag->next = skb_shinfo(skb)->frag_list;
  1856. skb_shinfo(skb)->frag_list = frag;
  1857. }
  1858. #define skb_walk_frags(skb, iter) \
  1859. for (iter = skb_shinfo(skb)->frag_list; iter; iter = iter->next)
  1860. extern struct sk_buff *__skb_recv_datagram(struct sock *sk, unsigned flags,
  1861. int *peeked, int *off, int *err);
  1862. extern struct sk_buff *skb_recv_datagram(struct sock *sk, unsigned flags,
  1863. int noblock, int *err);
  1864. extern unsigned int datagram_poll(struct file *file, struct socket *sock,
  1865. struct poll_table_struct *wait);
  1866. extern int skb_copy_datagram_iovec(const struct sk_buff *from,
  1867. int offset, struct iovec *to,
  1868. int size);
  1869. extern int skb_copy_and_csum_datagram_iovec(struct sk_buff *skb,
  1870. int hlen,
  1871. struct iovec *iov);
  1872. extern int skb_copy_datagram_from_iovec(struct sk_buff *skb,
  1873. int offset,
  1874. const struct iovec *from,
  1875. int from_offset,
  1876. int len);
  1877. extern int skb_copy_datagram_const_iovec(const struct sk_buff *from,
  1878. int offset,
  1879. const struct iovec *to,
  1880. int to_offset,
  1881. int size);
  1882. extern void skb_free_datagram(struct sock *sk, struct sk_buff *skb);
  1883. extern void skb_free_datagram_locked(struct sock *sk,
  1884. struct sk_buff *skb);
  1885. extern int skb_kill_datagram(struct sock *sk, struct sk_buff *skb,
  1886. unsigned int flags);
  1887. extern __wsum skb_checksum(const struct sk_buff *skb, int offset,
  1888. int len, __wsum csum);
  1889. extern int skb_copy_bits(const struct sk_buff *skb, int offset,
  1890. void *to, int len);
  1891. extern int skb_store_bits(struct sk_buff *skb, int offset,
  1892. const void *from, int len);
  1893. extern __wsum skb_copy_and_csum_bits(const struct sk_buff *skb,
  1894. int offset, u8 *to, int len,
  1895. __wsum csum);
  1896. extern int skb_splice_bits(struct sk_buff *skb,
  1897. unsigned int offset,
  1898. struct pipe_inode_info *pipe,
  1899. unsigned int len,
  1900. unsigned int flags);
  1901. extern void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to);
  1902. extern void skb_split(struct sk_buff *skb,
  1903. struct sk_buff *skb1, const u32 len);
  1904. extern int skb_shift(struct sk_buff *tgt, struct sk_buff *skb,
  1905. int shiftlen);
  1906. extern struct sk_buff *skb_segment(struct sk_buff *skb,
  1907. netdev_features_t features);
  1908. static inline void *skb_header_pointer(const struct sk_buff *skb, int offset,
  1909. int len, void *buffer)
  1910. {
  1911. int hlen = skb_headlen(skb);
  1912. if (hlen - offset >= len)
  1913. return skb->data + offset;
  1914. if (skb_copy_bits(skb, offset, buffer, len) < 0)
  1915. return NULL;
  1916. return buffer;
  1917. }
  1918. static inline void skb_copy_from_linear_data(const struct sk_buff *skb,
  1919. void *to,
  1920. const unsigned int len)
  1921. {
  1922. memcpy(to, skb->data, len);
  1923. }
  1924. static inline void skb_copy_from_linear_data_offset(const struct sk_buff *skb,
  1925. const int offset, void *to,
  1926. const unsigned int len)
  1927. {
  1928. memcpy(to, skb->data + offset, len);
  1929. }
  1930. static inline void skb_copy_to_linear_data(struct sk_buff *skb,
  1931. const void *from,
  1932. const unsigned int len)
  1933. {
  1934. memcpy(skb->data, from, len);
  1935. }
  1936. static inline void skb_copy_to_linear_data_offset(struct sk_buff *skb,
  1937. const int offset,
  1938. const void *from,
  1939. const unsigned int len)
  1940. {
  1941. memcpy(skb->data + offset, from, len);
  1942. }
  1943. extern void skb_init(void);
  1944. static inline ktime_t skb_get_ktime(const struct sk_buff *skb)
  1945. {
  1946. return skb->tstamp;
  1947. }
  1948. /**
  1949. * skb_get_timestamp - get timestamp from a skb
  1950. * @skb: skb to get stamp from
  1951. * @stamp: pointer to struct timeval to store stamp in
  1952. *
  1953. * Timestamps are stored in the skb as offsets to a base timestamp.
  1954. * This function converts the offset back to a struct timeval and stores
  1955. * it in stamp.
  1956. */
  1957. static inline void skb_get_timestamp(const struct sk_buff *skb,
  1958. struct timeval *stamp)
  1959. {
  1960. *stamp = ktime_to_timeval(skb->tstamp);
  1961. }
  1962. static inline void skb_get_timestampns(const struct sk_buff *skb,
  1963. struct timespec *stamp)
  1964. {
  1965. *stamp = ktime_to_timespec(skb->tstamp);
  1966. }
  1967. static inline void __net_timestamp(struct sk_buff *skb)
  1968. {
  1969. skb->tstamp = ktime_get_real();
  1970. }
  1971. static inline ktime_t net_timedelta(ktime_t t)
  1972. {
  1973. return ktime_sub(ktime_get_real(), t);
  1974. }
  1975. static inline ktime_t net_invalid_timestamp(void)
  1976. {
  1977. return ktime_set(0, 0);
  1978. }
  1979. extern void skb_timestamping_init(void);
  1980. #ifdef CONFIG_NETWORK_PHY_TIMESTAMPING
  1981. extern void skb_clone_tx_timestamp(struct sk_buff *skb);
  1982. extern bool skb_defer_rx_timestamp(struct sk_buff *skb);
  1983. #else /* CONFIG_NETWORK_PHY_TIMESTAMPING */
  1984. static inline void skb_clone_tx_timestamp(struct sk_buff *skb)
  1985. {
  1986. }
  1987. static inline bool skb_defer_rx_timestamp(struct sk_buff *skb)
  1988. {
  1989. return false;
  1990. }
  1991. #endif /* !CONFIG_NETWORK_PHY_TIMESTAMPING */
  1992. /**
  1993. * skb_complete_tx_timestamp() - deliver cloned skb with tx timestamps
  1994. *
  1995. * PHY drivers may accept clones of transmitted packets for
  1996. * timestamping via their phy_driver.txtstamp method. These drivers
  1997. * must call this function to return the skb back to the stack, with
  1998. * or without a timestamp.
  1999. *
  2000. * @skb: clone of the the original outgoing packet
  2001. * @hwtstamps: hardware time stamps, may be NULL if not available
  2002. *
  2003. */
  2004. void skb_complete_tx_timestamp(struct sk_buff *skb,
  2005. struct skb_shared_hwtstamps *hwtstamps);
  2006. /**
  2007. * skb_tstamp_tx - queue clone of skb with send time stamps
  2008. * @orig_skb: the original outgoing packet
  2009. * @hwtstamps: hardware time stamps, may be NULL if not available
  2010. *
  2011. * If the skb has a socket associated, then this function clones the
  2012. * skb (thus sharing the actual data and optional structures), stores
  2013. * the optional hardware time stamping information (if non NULL) or
  2014. * generates a software time stamp (otherwise), then queues the clone
  2015. * to the error queue of the socket. Errors are silently ignored.
  2016. */
  2017. extern void skb_tstamp_tx(struct sk_buff *orig_skb,
  2018. struct skb_shared_hwtstamps *hwtstamps);
  2019. static inline void sw_tx_timestamp(struct sk_buff *skb)
  2020. {
  2021. if (skb_shinfo(skb)->tx_flags & SKBTX_SW_TSTAMP &&
  2022. !(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS))
  2023. skb_tstamp_tx(skb, NULL);
  2024. }
  2025. /**
  2026. * skb_tx_timestamp() - Driver hook for transmit timestamping
  2027. *
  2028. * Ethernet MAC Drivers should call this function in their hard_xmit()
  2029. * function immediately before giving the sk_buff to the MAC hardware.
  2030. *
  2031. * @skb: A socket buffer.
  2032. */
  2033. static inline void skb_tx_timestamp(struct sk_buff *skb)
  2034. {
  2035. skb_clone_tx_timestamp(skb);
  2036. sw_tx_timestamp(skb);
  2037. }
  2038. /**
  2039. * skb_complete_wifi_ack - deliver skb with wifi status
  2040. *
  2041. * @skb: the original outgoing packet
  2042. * @acked: ack status
  2043. *
  2044. */
  2045. void skb_complete_wifi_ack(struct sk_buff *skb, bool acked);
  2046. extern __sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len);
  2047. extern __sum16 __skb_checksum_complete(struct sk_buff *skb);
  2048. static inline int skb_csum_unnecessary(const struct sk_buff *skb)
  2049. {
  2050. return skb->ip_summed & CHECKSUM_UNNECESSARY;
  2051. }
  2052. /**
  2053. * skb_checksum_complete - Calculate checksum of an entire packet
  2054. * @skb: packet to process
  2055. *
  2056. * This function calculates the checksum over the entire packet plus
  2057. * the value of skb->csum. The latter can be used to supply the
  2058. * checksum of a pseudo header as used by TCP/UDP. It returns the
  2059. * checksum.
  2060. *
  2061. * For protocols that contain complete checksums such as ICMP/TCP/UDP,
  2062. * this function can be used to verify that checksum on received
  2063. * packets. In that case the function should return zero if the
  2064. * checksum is correct. In particular, this function will return zero
  2065. * if skb->ip_summed is CHECKSUM_UNNECESSARY which indicates that the
  2066. * hardware has already verified the correctness of the checksum.
  2067. */
  2068. static inline __sum16 skb_checksum_complete(struct sk_buff *skb)
  2069. {
  2070. return skb_csum_unnecessary(skb) ?
  2071. 0 : __skb_checksum_complete(skb);
  2072. }
  2073. #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
  2074. extern void nf_conntrack_destroy(struct nf_conntrack *nfct);
  2075. static inline void nf_conntrack_put(struct nf_conntrack *nfct)
  2076. {
  2077. if (nfct && atomic_dec_and_test(&nfct->use))
  2078. nf_conntrack_destroy(nfct);
  2079. }
  2080. static inline void nf_conntrack_get(struct nf_conntrack *nfct)
  2081. {
  2082. if (nfct)
  2083. atomic_inc(&nfct->use);
  2084. }
  2085. #endif
  2086. #ifdef NET_SKBUFF_NF_DEFRAG_NEEDED
  2087. static inline void nf_conntrack_get_reasm(struct sk_buff *skb)
  2088. {
  2089. if (skb)
  2090. atomic_inc(&skb->users);
  2091. }
  2092. static inline void nf_conntrack_put_reasm(struct sk_buff *skb)
  2093. {
  2094. if (skb)
  2095. kfree_skb(skb);
  2096. }
  2097. #endif
  2098. #ifdef CONFIG_BRIDGE_NETFILTER
  2099. static inline void nf_bridge_put(struct nf_bridge_info *nf_bridge)
  2100. {
  2101. if (nf_bridge && atomic_dec_and_test(&nf_bridge->use))
  2102. kfree(nf_bridge);
  2103. }
  2104. static inline void nf_bridge_get(struct nf_bridge_info *nf_bridge)
  2105. {
  2106. if (nf_bridge)
  2107. atomic_inc(&nf_bridge->use);
  2108. }
  2109. #endif /* CONFIG_BRIDGE_NETFILTER */
  2110. static inline void nf_reset(struct sk_buff *skb)
  2111. {
  2112. #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
  2113. nf_conntrack_put(skb->nfct);
  2114. skb->nfct = NULL;
  2115. #endif
  2116. #ifdef NET_SKBUFF_NF_DEFRAG_NEEDED
  2117. nf_conntrack_put_reasm(skb->nfct_reasm);
  2118. skb->nfct_reasm = NULL;
  2119. #endif
  2120. #ifdef CONFIG_BRIDGE_NETFILTER
  2121. nf_bridge_put(skb->nf_bridge);
  2122. skb->nf_bridge = NULL;
  2123. #endif
  2124. }
  2125. /* Note: This doesn't put any conntrack and bridge info in dst. */
  2126. static inline void __nf_copy(struct sk_buff *dst, const struct sk_buff *src)
  2127. {
  2128. #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
  2129. dst->nfct = src->nfct;
  2130. nf_conntrack_get(src->nfct);
  2131. dst->nfctinfo = src->nfctinfo;
  2132. #endif
  2133. #ifdef NET_SKBUFF_NF_DEFRAG_NEEDED
  2134. dst->nfct_reasm = src->nfct_reasm;
  2135. nf_conntrack_get_reasm(src->nfct_reasm);
  2136. #endif
  2137. #ifdef CONFIG_BRIDGE_NETFILTER
  2138. dst->nf_bridge = src->nf_bridge;
  2139. nf_bridge_get(src->nf_bridge);
  2140. #endif
  2141. }
  2142. static inline void nf_copy(struct sk_buff *dst, const struct sk_buff *src)
  2143. {
  2144. #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
  2145. nf_conntrack_put(dst->nfct);
  2146. #endif
  2147. #ifdef NET_SKBUFF_NF_DEFRAG_NEEDED
  2148. nf_conntrack_put_reasm(dst->nfct_reasm);
  2149. #endif
  2150. #ifdef CONFIG_BRIDGE_NETFILTER
  2151. nf_bridge_put(dst->nf_bridge);
  2152. #endif
  2153. __nf_copy(dst, src);
  2154. }
  2155. #ifdef CONFIG_NETWORK_SECMARK
  2156. static inline void skb_copy_secmark(struct sk_buff *to, const struct sk_buff *from)
  2157. {
  2158. to->secmark = from->secmark;
  2159. }
  2160. static inline void skb_init_secmark(struct sk_buff *skb)
  2161. {
  2162. skb->secmark = 0;
  2163. }
  2164. #else
  2165. static inline void skb_copy_secmark(struct sk_buff *to, const struct sk_buff *from)
  2166. { }
  2167. static inline void skb_init_secmark(struct sk_buff *skb)
  2168. { }
  2169. #endif
  2170. static inline void skb_set_queue_mapping(struct sk_buff *skb, u16 queue_mapping)
  2171. {
  2172. skb->queue_mapping = queue_mapping;
  2173. }
  2174. static inline u16 skb_get_queue_mapping(const struct sk_buff *skb)
  2175. {
  2176. return skb->queue_mapping;
  2177. }
  2178. static inline void skb_copy_queue_mapping(struct sk_buff *to, const struct sk_buff *from)
  2179. {
  2180. to->queue_mapping = from->queue_mapping;
  2181. }
  2182. static inline void skb_record_rx_queue(struct sk_buff *skb, u16 rx_queue)
  2183. {
  2184. skb->queue_mapping = rx_queue + 1;
  2185. }
  2186. static inline u16 skb_get_rx_queue(const struct sk_buff *skb)
  2187. {
  2188. return skb->queue_mapping - 1;
  2189. }
  2190. static inline bool skb_rx_queue_recorded(const struct sk_buff *skb)
  2191. {
  2192. return skb->queue_mapping != 0;
  2193. }
  2194. extern u16 __skb_tx_hash(const struct net_device *dev,
  2195. const struct sk_buff *skb,
  2196. unsigned int num_tx_queues);
  2197. #ifdef CONFIG_XFRM
  2198. static inline struct sec_path *skb_sec_path(struct sk_buff *skb)
  2199. {
  2200. return skb->sp;
  2201. }
  2202. #else
  2203. static inline struct sec_path *skb_sec_path(struct sk_buff *skb)
  2204. {
  2205. return NULL;
  2206. }
  2207. #endif
  2208. static inline bool skb_is_gso(const struct sk_buff *skb)
  2209. {
  2210. return skb_shinfo(skb)->gso_size;
  2211. }
  2212. static inline bool skb_is_gso_v6(const struct sk_buff *skb)
  2213. {
  2214. return skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6;
  2215. }
  2216. extern void __skb_warn_lro_forwarding(const struct sk_buff *skb);
  2217. static inline bool skb_warn_if_lro(const struct sk_buff *skb)
  2218. {
  2219. /* LRO sets gso_size but not gso_type, whereas if GSO is really
  2220. * wanted then gso_type will be set. */
  2221. const struct skb_shared_info *shinfo = skb_shinfo(skb);
  2222. if (skb_is_nonlinear(skb) && shinfo->gso_size != 0 &&
  2223. unlikely(shinfo->gso_type == 0)) {
  2224. __skb_warn_lro_forwarding(skb);
  2225. return true;
  2226. }
  2227. return false;
  2228. }
  2229. static inline void skb_forward_csum(struct sk_buff *skb)
  2230. {
  2231. /* Unfortunately we don't support this one. Any brave souls? */
  2232. if (skb->ip_summed == CHECKSUM_COMPLETE)
  2233. skb->ip_summed = CHECKSUM_NONE;
  2234. }
  2235. /**
  2236. * skb_checksum_none_assert - make sure skb ip_summed is CHECKSUM_NONE
  2237. * @skb: skb to check
  2238. *
  2239. * fresh skbs have their ip_summed set to CHECKSUM_NONE.
  2240. * Instead of forcing ip_summed to CHECKSUM_NONE, we can
  2241. * use this helper, to document places where we make this assertion.
  2242. */
  2243. static inline void skb_checksum_none_assert(const struct sk_buff *skb)
  2244. {
  2245. #ifdef DEBUG
  2246. BUG_ON(skb->ip_summed != CHECKSUM_NONE);
  2247. #endif
  2248. }
  2249. bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off);
  2250. static inline bool skb_is_recycleable(const struct sk_buff *skb, int skb_size)
  2251. {
  2252. if (irqs_disabled())
  2253. return false;
  2254. if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY)
  2255. return false;
  2256. if (skb_is_nonlinear(skb) || skb->fclone != SKB_FCLONE_UNAVAILABLE)
  2257. return false;
  2258. skb_size = SKB_DATA_ALIGN(skb_size + NET_SKB_PAD);
  2259. if (skb_end_pointer(skb) - skb->head < skb_size)
  2260. return false;
  2261. if (skb_shared(skb) || skb_cloned(skb))
  2262. return false;
  2263. return true;
  2264. }
  2265. #endif /* __KERNEL__ */
  2266. #endif /* _LINUX_SKBUFF_H */