skbuff.h 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792
  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/compiler.h>
  17. #include <linux/time.h>
  18. #include <linux/cache.h>
  19. #include <asm/atomic.h>
  20. #include <asm/types.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/net.h>
  23. #include <linux/textsearch.h>
  24. #include <net/checksum.h>
  25. #include <linux/rcupdate.h>
  26. #include <linux/dmaengine.h>
  27. #include <linux/hrtimer.h>
  28. #define HAVE_ALLOC_SKB /* For the drivers to know */
  29. #define HAVE_ALIGNABLE_SKB /* Ditto 8) */
  30. /* Don't change this without changing skb_csum_unnecessary! */
  31. #define CHECKSUM_NONE 0
  32. #define CHECKSUM_UNNECESSARY 1
  33. #define CHECKSUM_COMPLETE 2
  34. #define CHECKSUM_PARTIAL 3
  35. #define SKB_DATA_ALIGN(X) (((X) + (SMP_CACHE_BYTES - 1)) & \
  36. ~(SMP_CACHE_BYTES - 1))
  37. #define SKB_WITH_OVERHEAD(X) \
  38. (((X) - sizeof(struct skb_shared_info)) & \
  39. ~(SMP_CACHE_BYTES - 1))
  40. #define SKB_MAX_ORDER(X, ORDER) \
  41. SKB_WITH_OVERHEAD((PAGE_SIZE << (ORDER)) - (X))
  42. #define SKB_MAX_HEAD(X) (SKB_MAX_ORDER((X), 0))
  43. #define SKB_MAX_ALLOC (SKB_MAX_ORDER(0, 2))
  44. /* A. Checksumming of received packets by device.
  45. *
  46. * NONE: device failed to checksum this packet.
  47. * skb->csum is undefined.
  48. *
  49. * UNNECESSARY: device parsed packet and wouldbe verified checksum.
  50. * skb->csum is undefined.
  51. * It is bad option, but, unfortunately, many of vendors do this.
  52. * Apparently with secret goal to sell you new device, when you
  53. * will add new protocol to your host. F.e. IPv6. 8)
  54. *
  55. * COMPLETE: the most generic way. Device supplied checksum of _all_
  56. * the packet as seen by netif_rx in skb->csum.
  57. * NOTE: Even if device supports only some protocols, but
  58. * is able to produce some skb->csum, it MUST use COMPLETE,
  59. * not UNNECESSARY.
  60. *
  61. * PARTIAL: identical to the case for output below. This may occur
  62. * on a packet received directly from another Linux OS, e.g.,
  63. * a virtualised Linux kernel on the same host. The packet can
  64. * be treated in the same way as UNNECESSARY except that on
  65. * output (i.e., forwarding) the checksum must be filled in
  66. * by the OS or the hardware.
  67. *
  68. * B. Checksumming on output.
  69. *
  70. * NONE: skb is checksummed by protocol or csum is not required.
  71. *
  72. * PARTIAL: device is required to csum packet as seen by hard_start_xmit
  73. * from skb->csum_start to the end and to record the checksum
  74. * at skb->csum_start + skb->csum_offset.
  75. *
  76. * Device must show its capabilities in dev->features, set
  77. * at device setup time.
  78. * NETIF_F_HW_CSUM - it is clever device, it is able to checksum
  79. * everything.
  80. * NETIF_F_NO_CSUM - loopback or reliable single hop media.
  81. * NETIF_F_IP_CSUM - device is dumb. It is able to csum only
  82. * TCP/UDP over IPv4. Sigh. Vendors like this
  83. * way by an unknown reason. Though, see comment above
  84. * about CHECKSUM_UNNECESSARY. 8)
  85. * NETIF_F_IPV6_CSUM about as dumb as the last one but does IPv6 instead.
  86. *
  87. * Any questions? No questions, good. --ANK
  88. */
  89. struct net_device;
  90. struct scatterlist;
  91. #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
  92. struct nf_conntrack {
  93. atomic_t use;
  94. };
  95. #endif
  96. #ifdef CONFIG_BRIDGE_NETFILTER
  97. struct nf_bridge_info {
  98. atomic_t use;
  99. struct net_device *physindev;
  100. struct net_device *physoutdev;
  101. #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
  102. struct net_device *netoutdev;
  103. #endif
  104. unsigned int mask;
  105. unsigned long data[32 / sizeof(unsigned long)];
  106. };
  107. #endif
  108. struct sk_buff_head {
  109. /* These two members must be first. */
  110. struct sk_buff *next;
  111. struct sk_buff *prev;
  112. __u32 qlen;
  113. spinlock_t lock;
  114. };
  115. struct sk_buff;
  116. /* To allow 64K frame to be packed as single skb without frag_list */
  117. #define MAX_SKB_FRAGS (65536/PAGE_SIZE + 2)
  118. typedef struct skb_frag_struct skb_frag_t;
  119. struct skb_frag_struct {
  120. struct page *page;
  121. __u32 page_offset;
  122. __u32 size;
  123. };
  124. /* This data is invariant across clones and lives at
  125. * the end of the header data, ie. at skb->end.
  126. */
  127. struct skb_shared_info {
  128. atomic_t dataref;
  129. unsigned short nr_frags;
  130. unsigned short gso_size;
  131. /* Warning: this field is not always filled in (UFO)! */
  132. unsigned short gso_segs;
  133. unsigned short gso_type;
  134. __be32 ip6_frag_id;
  135. struct sk_buff *frag_list;
  136. skb_frag_t frags[MAX_SKB_FRAGS];
  137. };
  138. /* We divide dataref into two halves. The higher 16 bits hold references
  139. * to the payload part of skb->data. The lower 16 bits hold references to
  140. * the entire skb->data. A clone of a headerless skb holds the length of
  141. * the header in skb->hdr_len.
  142. *
  143. * All users must obey the rule that the skb->data reference count must be
  144. * greater than or equal to the payload reference count.
  145. *
  146. * Holding a reference to the payload part means that the user does not
  147. * care about modifications to the header part of skb->data.
  148. */
  149. #define SKB_DATAREF_SHIFT 16
  150. #define SKB_DATAREF_MASK ((1 << SKB_DATAREF_SHIFT) - 1)
  151. enum {
  152. SKB_FCLONE_UNAVAILABLE,
  153. SKB_FCLONE_ORIG,
  154. SKB_FCLONE_CLONE,
  155. };
  156. enum {
  157. SKB_GSO_TCPV4 = 1 << 0,
  158. SKB_GSO_UDP = 1 << 1,
  159. /* This indicates the skb is from an untrusted source. */
  160. SKB_GSO_DODGY = 1 << 2,
  161. /* This indicates the tcp segment has CWR set. */
  162. SKB_GSO_TCP_ECN = 1 << 3,
  163. SKB_GSO_TCPV6 = 1 << 4,
  164. };
  165. #if BITS_PER_LONG > 32
  166. #define NET_SKBUFF_DATA_USES_OFFSET 1
  167. #endif
  168. #ifdef NET_SKBUFF_DATA_USES_OFFSET
  169. typedef unsigned int sk_buff_data_t;
  170. #else
  171. typedef unsigned char *sk_buff_data_t;
  172. #endif
  173. /**
  174. * struct sk_buff - socket buffer
  175. * @next: Next buffer in list
  176. * @prev: Previous buffer in list
  177. * @sk: Socket we are owned by
  178. * @tstamp: Time we arrived
  179. * @dev: Device we arrived on/are leaving by
  180. * @transport_header: Transport layer header
  181. * @network_header: Network layer header
  182. * @mac_header: Link layer header
  183. * @dst: destination entry
  184. * @sp: the security path, used for xfrm
  185. * @cb: Control buffer. Free for use by every layer. Put private vars here
  186. * @len: Length of actual data
  187. * @data_len: Data length
  188. * @mac_len: Length of link layer header
  189. * @hdr_len: writable header length of cloned skb
  190. * @csum: Checksum (must include start/offset pair)
  191. * @csum_start: Offset from skb->head where checksumming should start
  192. * @csum_offset: Offset from csum_start where checksum should be stored
  193. * @local_df: allow local fragmentation
  194. * @cloned: Head may be cloned (check refcnt to be sure)
  195. * @nohdr: Payload reference only, must not modify header
  196. * @pkt_type: Packet class
  197. * @fclone: skbuff clone status
  198. * @ip_summed: Driver fed us an IP checksum
  199. * @priority: Packet queueing priority
  200. * @users: User count - see {datagram,tcp}.c
  201. * @protocol: Packet protocol from driver
  202. * @truesize: Buffer size
  203. * @head: Head of buffer
  204. * @data: Data head pointer
  205. * @tail: Tail pointer
  206. * @end: End pointer
  207. * @destructor: Destruct function
  208. * @mark: Generic packet mark
  209. * @nfct: Associated connection, if any
  210. * @ipvs_property: skbuff is owned by ipvs
  211. * @nf_trace: netfilter packet trace flag
  212. * @nfctinfo: Relationship of this skb to the connection
  213. * @nfct_reasm: netfilter conntrack re-assembly pointer
  214. * @nf_bridge: Saved data about a bridged frame - see br_netfilter.c
  215. * @iif: ifindex of device we arrived on
  216. * @queue_mapping: Queue mapping for multiqueue devices
  217. * @tc_index: Traffic control index
  218. * @tc_verd: traffic control verdict
  219. * @dma_cookie: a cookie to one of several possible DMA operations
  220. * done by skb DMA functions
  221. * @secmark: security marking
  222. */
  223. struct sk_buff {
  224. /* These two members must be first. */
  225. struct sk_buff *next;
  226. struct sk_buff *prev;
  227. struct sock *sk;
  228. ktime_t tstamp;
  229. struct net_device *dev;
  230. struct dst_entry *dst;
  231. struct sec_path *sp;
  232. /*
  233. * This is the control buffer. It is free to use for every
  234. * layer. Please put your private variables there. If you
  235. * want to keep them across layers you have to do a skb_clone()
  236. * first. This is owned by whoever has the skb queued ATM.
  237. */
  238. char cb[48];
  239. unsigned int len,
  240. data_len;
  241. __u16 mac_len,
  242. hdr_len;
  243. union {
  244. __wsum csum;
  245. struct {
  246. __u16 csum_start;
  247. __u16 csum_offset;
  248. };
  249. };
  250. __u32 priority;
  251. __u8 local_df:1,
  252. cloned:1,
  253. ip_summed:2,
  254. nohdr:1,
  255. nfctinfo:3;
  256. __u8 pkt_type:3,
  257. fclone:2,
  258. ipvs_property:1,
  259. nf_trace:1;
  260. __be16 protocol;
  261. void (*destructor)(struct sk_buff *skb);
  262. #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
  263. struct nf_conntrack *nfct;
  264. struct sk_buff *nfct_reasm;
  265. #endif
  266. #ifdef CONFIG_BRIDGE_NETFILTER
  267. struct nf_bridge_info *nf_bridge;
  268. #endif
  269. int iif;
  270. __u16 queue_mapping;
  271. #ifdef CONFIG_NET_SCHED
  272. __u16 tc_index; /* traffic control index */
  273. #ifdef CONFIG_NET_CLS_ACT
  274. __u16 tc_verd; /* traffic control verdict */
  275. #endif
  276. #endif
  277. /* 2 byte hole */
  278. #ifdef CONFIG_NET_DMA
  279. dma_cookie_t dma_cookie;
  280. #endif
  281. #ifdef CONFIG_NETWORK_SECMARK
  282. __u32 secmark;
  283. #endif
  284. __u32 mark;
  285. sk_buff_data_t transport_header;
  286. sk_buff_data_t network_header;
  287. sk_buff_data_t mac_header;
  288. /* These elements must be at the end, see alloc_skb() for details. */
  289. sk_buff_data_t tail;
  290. sk_buff_data_t end;
  291. unsigned char *head,
  292. *data;
  293. unsigned int truesize;
  294. atomic_t users;
  295. };
  296. #ifdef __KERNEL__
  297. /*
  298. * Handling routines are only of interest to the kernel
  299. */
  300. #include <linux/slab.h>
  301. #include <asm/system.h>
  302. extern void kfree_skb(struct sk_buff *skb);
  303. extern void __kfree_skb(struct sk_buff *skb);
  304. extern struct sk_buff *__alloc_skb(unsigned int size,
  305. gfp_t priority, int fclone, int node);
  306. static inline struct sk_buff *alloc_skb(unsigned int size,
  307. gfp_t priority)
  308. {
  309. return __alloc_skb(size, priority, 0, -1);
  310. }
  311. static inline struct sk_buff *alloc_skb_fclone(unsigned int size,
  312. gfp_t priority)
  313. {
  314. return __alloc_skb(size, priority, 1, -1);
  315. }
  316. extern void kfree_skbmem(struct sk_buff *skb);
  317. extern struct sk_buff *skb_clone(struct sk_buff *skb,
  318. gfp_t priority);
  319. extern struct sk_buff *skb_copy(const struct sk_buff *skb,
  320. gfp_t priority);
  321. extern struct sk_buff *pskb_copy(struct sk_buff *skb,
  322. gfp_t gfp_mask);
  323. extern int pskb_expand_head(struct sk_buff *skb,
  324. int nhead, int ntail,
  325. gfp_t gfp_mask);
  326. extern struct sk_buff *skb_realloc_headroom(struct sk_buff *skb,
  327. unsigned int headroom);
  328. extern struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
  329. int newheadroom, int newtailroom,
  330. gfp_t priority);
  331. extern int skb_to_sgvec(struct sk_buff *skb,
  332. struct scatterlist *sg, int offset,
  333. int len);
  334. extern int skb_cow_data(struct sk_buff *skb, int tailbits,
  335. struct sk_buff **trailer);
  336. extern int skb_pad(struct sk_buff *skb, int pad);
  337. #define dev_kfree_skb(a) kfree_skb(a)
  338. extern void skb_over_panic(struct sk_buff *skb, int len,
  339. void *here);
  340. extern void skb_under_panic(struct sk_buff *skb, int len,
  341. void *here);
  342. extern void skb_truesize_bug(struct sk_buff *skb);
  343. static inline void skb_truesize_check(struct sk_buff *skb)
  344. {
  345. if (unlikely((int)skb->truesize < sizeof(struct sk_buff) + skb->len))
  346. skb_truesize_bug(skb);
  347. }
  348. extern int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
  349. int getfrag(void *from, char *to, int offset,
  350. int len,int odd, struct sk_buff *skb),
  351. void *from, int length);
  352. struct skb_seq_state
  353. {
  354. __u32 lower_offset;
  355. __u32 upper_offset;
  356. __u32 frag_idx;
  357. __u32 stepped_offset;
  358. struct sk_buff *root_skb;
  359. struct sk_buff *cur_skb;
  360. __u8 *frag_data;
  361. };
  362. extern void skb_prepare_seq_read(struct sk_buff *skb,
  363. unsigned int from, unsigned int to,
  364. struct skb_seq_state *st);
  365. extern unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
  366. struct skb_seq_state *st);
  367. extern void skb_abort_seq_read(struct skb_seq_state *st);
  368. extern unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
  369. unsigned int to, struct ts_config *config,
  370. struct ts_state *state);
  371. #ifdef NET_SKBUFF_DATA_USES_OFFSET
  372. static inline unsigned char *skb_end_pointer(const struct sk_buff *skb)
  373. {
  374. return skb->head + skb->end;
  375. }
  376. #else
  377. static inline unsigned char *skb_end_pointer(const struct sk_buff *skb)
  378. {
  379. return skb->end;
  380. }
  381. #endif
  382. /* Internal */
  383. #define skb_shinfo(SKB) ((struct skb_shared_info *)(skb_end_pointer(SKB)))
  384. /**
  385. * skb_queue_empty - check if a queue is empty
  386. * @list: queue head
  387. *
  388. * Returns true if the queue is empty, false otherwise.
  389. */
  390. static inline int skb_queue_empty(const struct sk_buff_head *list)
  391. {
  392. return list->next == (struct sk_buff *)list;
  393. }
  394. /**
  395. * skb_get - reference buffer
  396. * @skb: buffer to reference
  397. *
  398. * Makes another reference to a socket buffer and returns a pointer
  399. * to the buffer.
  400. */
  401. static inline struct sk_buff *skb_get(struct sk_buff *skb)
  402. {
  403. atomic_inc(&skb->users);
  404. return skb;
  405. }
  406. /*
  407. * If users == 1, we are the only owner and are can avoid redundant
  408. * atomic change.
  409. */
  410. /**
  411. * skb_cloned - is the buffer a clone
  412. * @skb: buffer to check
  413. *
  414. * Returns true if the buffer was generated with skb_clone() and is
  415. * one of multiple shared copies of the buffer. Cloned buffers are
  416. * shared data so must not be written to under normal circumstances.
  417. */
  418. static inline int skb_cloned(const struct sk_buff *skb)
  419. {
  420. return skb->cloned &&
  421. (atomic_read(&skb_shinfo(skb)->dataref) & SKB_DATAREF_MASK) != 1;
  422. }
  423. /**
  424. * skb_header_cloned - is the header a clone
  425. * @skb: buffer to check
  426. *
  427. * Returns true if modifying the header part of the buffer requires
  428. * the data to be copied.
  429. */
  430. static inline int skb_header_cloned(const struct sk_buff *skb)
  431. {
  432. int dataref;
  433. if (!skb->cloned)
  434. return 0;
  435. dataref = atomic_read(&skb_shinfo(skb)->dataref);
  436. dataref = (dataref & SKB_DATAREF_MASK) - (dataref >> SKB_DATAREF_SHIFT);
  437. return dataref != 1;
  438. }
  439. /**
  440. * skb_header_release - release reference to header
  441. * @skb: buffer to operate on
  442. *
  443. * Drop a reference to the header part of the buffer. This is done
  444. * by acquiring a payload reference. You must not read from the header
  445. * part of skb->data after this.
  446. */
  447. static inline void skb_header_release(struct sk_buff *skb)
  448. {
  449. BUG_ON(skb->nohdr);
  450. skb->nohdr = 1;
  451. atomic_add(1 << SKB_DATAREF_SHIFT, &skb_shinfo(skb)->dataref);
  452. }
  453. /**
  454. * skb_shared - is the buffer shared
  455. * @skb: buffer to check
  456. *
  457. * Returns true if more than one person has a reference to this
  458. * buffer.
  459. */
  460. static inline int skb_shared(const struct sk_buff *skb)
  461. {
  462. return atomic_read(&skb->users) != 1;
  463. }
  464. /**
  465. * skb_share_check - check if buffer is shared and if so clone it
  466. * @skb: buffer to check
  467. * @pri: priority for memory allocation
  468. *
  469. * If the buffer is shared the buffer is cloned and the old copy
  470. * drops a reference. A new clone with a single reference is returned.
  471. * If the buffer is not shared the original buffer is returned. When
  472. * being called from interrupt status or with spinlocks held pri must
  473. * be GFP_ATOMIC.
  474. *
  475. * NULL is returned on a memory allocation failure.
  476. */
  477. static inline struct sk_buff *skb_share_check(struct sk_buff *skb,
  478. gfp_t pri)
  479. {
  480. might_sleep_if(pri & __GFP_WAIT);
  481. if (skb_shared(skb)) {
  482. struct sk_buff *nskb = skb_clone(skb, pri);
  483. kfree_skb(skb);
  484. skb = nskb;
  485. }
  486. return skb;
  487. }
  488. /*
  489. * Copy shared buffers into a new sk_buff. We effectively do COW on
  490. * packets to handle cases where we have a local reader and forward
  491. * and a couple of other messy ones. The normal one is tcpdumping
  492. * a packet thats being forwarded.
  493. */
  494. /**
  495. * skb_unshare - make a copy of a shared buffer
  496. * @skb: buffer to check
  497. * @pri: priority for memory allocation
  498. *
  499. * If the socket buffer is a clone then this function creates a new
  500. * copy of the data, drops a reference count on the old copy and returns
  501. * the new copy with the reference count at 1. If the buffer is not a clone
  502. * the original buffer is returned. When called with a spinlock held or
  503. * from interrupt state @pri must be %GFP_ATOMIC
  504. *
  505. * %NULL is returned on a memory allocation failure.
  506. */
  507. static inline struct sk_buff *skb_unshare(struct sk_buff *skb,
  508. gfp_t pri)
  509. {
  510. might_sleep_if(pri & __GFP_WAIT);
  511. if (skb_cloned(skb)) {
  512. struct sk_buff *nskb = skb_copy(skb, pri);
  513. kfree_skb(skb); /* Free our shared copy */
  514. skb = nskb;
  515. }
  516. return skb;
  517. }
  518. /**
  519. * skb_peek
  520. * @list_: list to peek at
  521. *
  522. * Peek an &sk_buff. Unlike most other operations you _MUST_
  523. * be careful with this one. A peek leaves the buffer on the
  524. * list and someone else may run off with it. You must hold
  525. * the appropriate locks or have a private queue to do this.
  526. *
  527. * Returns %NULL for an empty list or a pointer to the head element.
  528. * The reference count is not incremented and the reference is therefore
  529. * volatile. Use with caution.
  530. */
  531. static inline struct sk_buff *skb_peek(struct sk_buff_head *list_)
  532. {
  533. struct sk_buff *list = ((struct sk_buff *)list_)->next;
  534. if (list == (struct sk_buff *)list_)
  535. list = NULL;
  536. return list;
  537. }
  538. /**
  539. * skb_peek_tail
  540. * @list_: list to peek at
  541. *
  542. * Peek an &sk_buff. Unlike most other operations you _MUST_
  543. * be careful with this one. A peek leaves the buffer on the
  544. * list and someone else may run off with it. You must hold
  545. * the appropriate locks or have a private queue to do this.
  546. *
  547. * Returns %NULL for an empty list or a pointer to the tail element.
  548. * The reference count is not incremented and the reference is therefore
  549. * volatile. Use with caution.
  550. */
  551. static inline struct sk_buff *skb_peek_tail(struct sk_buff_head *list_)
  552. {
  553. struct sk_buff *list = ((struct sk_buff *)list_)->prev;
  554. if (list == (struct sk_buff *)list_)
  555. list = NULL;
  556. return list;
  557. }
  558. /**
  559. * skb_queue_len - get queue length
  560. * @list_: list to measure
  561. *
  562. * Return the length of an &sk_buff queue.
  563. */
  564. static inline __u32 skb_queue_len(const struct sk_buff_head *list_)
  565. {
  566. return list_->qlen;
  567. }
  568. /*
  569. * This function creates a split out lock class for each invocation;
  570. * this is needed for now since a whole lot of users of the skb-queue
  571. * infrastructure in drivers have different locking usage (in hardirq)
  572. * than the networking core (in softirq only). In the long run either the
  573. * network layer or drivers should need annotation to consolidate the
  574. * main types of usage into 3 classes.
  575. */
  576. static inline void skb_queue_head_init(struct sk_buff_head *list)
  577. {
  578. spin_lock_init(&list->lock);
  579. list->prev = list->next = (struct sk_buff *)list;
  580. list->qlen = 0;
  581. }
  582. static inline void skb_queue_head_init_class(struct sk_buff_head *list,
  583. struct lock_class_key *class)
  584. {
  585. skb_queue_head_init(list);
  586. lockdep_set_class(&list->lock, class);
  587. }
  588. /*
  589. * Insert an sk_buff at the start of a list.
  590. *
  591. * The "__skb_xxxx()" functions are the non-atomic ones that
  592. * can only be called with interrupts disabled.
  593. */
  594. /**
  595. * __skb_queue_after - queue a buffer at the list head
  596. * @list: list to use
  597. * @prev: place after this buffer
  598. * @newsk: buffer to queue
  599. *
  600. * Queue a buffer int the middle of a list. This function takes no locks
  601. * and you must therefore hold required locks before calling it.
  602. *
  603. * A buffer cannot be placed on two lists at the same time.
  604. */
  605. static inline void __skb_queue_after(struct sk_buff_head *list,
  606. struct sk_buff *prev,
  607. struct sk_buff *newsk)
  608. {
  609. struct sk_buff *next;
  610. list->qlen++;
  611. next = prev->next;
  612. newsk->next = next;
  613. newsk->prev = prev;
  614. next->prev = prev->next = newsk;
  615. }
  616. /**
  617. * __skb_queue_head - queue a buffer at the list head
  618. * @list: list to use
  619. * @newsk: buffer to queue
  620. *
  621. * Queue a buffer at the start of a list. This function takes no locks
  622. * and you must therefore hold required locks before calling it.
  623. *
  624. * A buffer cannot be placed on two lists at the same time.
  625. */
  626. extern void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk);
  627. static inline void __skb_queue_head(struct sk_buff_head *list,
  628. struct sk_buff *newsk)
  629. {
  630. __skb_queue_after(list, (struct sk_buff *)list, newsk);
  631. }
  632. /**
  633. * __skb_queue_tail - queue a buffer at the list tail
  634. * @list: list to use
  635. * @newsk: buffer to queue
  636. *
  637. * Queue a buffer at the end of a list. This function takes no locks
  638. * and you must therefore hold required locks before calling it.
  639. *
  640. * A buffer cannot be placed on two lists at the same time.
  641. */
  642. extern void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk);
  643. static inline void __skb_queue_tail(struct sk_buff_head *list,
  644. struct sk_buff *newsk)
  645. {
  646. struct sk_buff *prev, *next;
  647. list->qlen++;
  648. next = (struct sk_buff *)list;
  649. prev = next->prev;
  650. newsk->next = next;
  651. newsk->prev = prev;
  652. next->prev = prev->next = newsk;
  653. }
  654. /**
  655. * __skb_dequeue - remove from the head of the queue
  656. * @list: list to dequeue from
  657. *
  658. * Remove the head of the list. This function does not take any locks
  659. * so must be used with appropriate locks held only. The head item is
  660. * returned or %NULL if the list is empty.
  661. */
  662. extern struct sk_buff *skb_dequeue(struct sk_buff_head *list);
  663. static inline struct sk_buff *__skb_dequeue(struct sk_buff_head *list)
  664. {
  665. struct sk_buff *next, *prev, *result;
  666. prev = (struct sk_buff *) list;
  667. next = prev->next;
  668. result = NULL;
  669. if (next != prev) {
  670. result = next;
  671. next = next->next;
  672. list->qlen--;
  673. next->prev = prev;
  674. prev->next = next;
  675. result->next = result->prev = NULL;
  676. }
  677. return result;
  678. }
  679. /*
  680. * Insert a packet on a list.
  681. */
  682. extern void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list);
  683. static inline void __skb_insert(struct sk_buff *newsk,
  684. struct sk_buff *prev, struct sk_buff *next,
  685. struct sk_buff_head *list)
  686. {
  687. newsk->next = next;
  688. newsk->prev = prev;
  689. next->prev = prev->next = newsk;
  690. list->qlen++;
  691. }
  692. /*
  693. * Place a packet after a given packet in a list.
  694. */
  695. extern void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list);
  696. static inline void __skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
  697. {
  698. __skb_insert(newsk, old, old->next, list);
  699. }
  700. /*
  701. * remove sk_buff from list. _Must_ be called atomically, and with
  702. * the list known..
  703. */
  704. extern void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list);
  705. static inline void __skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
  706. {
  707. struct sk_buff *next, *prev;
  708. list->qlen--;
  709. next = skb->next;
  710. prev = skb->prev;
  711. skb->next = skb->prev = NULL;
  712. next->prev = prev;
  713. prev->next = next;
  714. }
  715. /* XXX: more streamlined implementation */
  716. /**
  717. * __skb_dequeue_tail - remove from the tail of the queue
  718. * @list: list to dequeue from
  719. *
  720. * Remove the tail of the list. This function does not take any locks
  721. * so must be used with appropriate locks held only. The tail item is
  722. * returned or %NULL if the list is empty.
  723. */
  724. extern struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list);
  725. static inline struct sk_buff *__skb_dequeue_tail(struct sk_buff_head *list)
  726. {
  727. struct sk_buff *skb = skb_peek_tail(list);
  728. if (skb)
  729. __skb_unlink(skb, list);
  730. return skb;
  731. }
  732. static inline int skb_is_nonlinear(const struct sk_buff *skb)
  733. {
  734. return skb->data_len;
  735. }
  736. static inline unsigned int skb_headlen(const struct sk_buff *skb)
  737. {
  738. return skb->len - skb->data_len;
  739. }
  740. static inline int skb_pagelen(const struct sk_buff *skb)
  741. {
  742. int i, len = 0;
  743. for (i = (int)skb_shinfo(skb)->nr_frags - 1; i >= 0; i--)
  744. len += skb_shinfo(skb)->frags[i].size;
  745. return len + skb_headlen(skb);
  746. }
  747. static inline void skb_fill_page_desc(struct sk_buff *skb, int i,
  748. struct page *page, int off, int size)
  749. {
  750. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  751. frag->page = page;
  752. frag->page_offset = off;
  753. frag->size = size;
  754. skb_shinfo(skb)->nr_frags = i + 1;
  755. }
  756. #define SKB_PAGE_ASSERT(skb) BUG_ON(skb_shinfo(skb)->nr_frags)
  757. #define SKB_FRAG_ASSERT(skb) BUG_ON(skb_shinfo(skb)->frag_list)
  758. #define SKB_LINEAR_ASSERT(skb) BUG_ON(skb_is_nonlinear(skb))
  759. #ifdef NET_SKBUFF_DATA_USES_OFFSET
  760. static inline unsigned char *skb_tail_pointer(const struct sk_buff *skb)
  761. {
  762. return skb->head + skb->tail;
  763. }
  764. static inline void skb_reset_tail_pointer(struct sk_buff *skb)
  765. {
  766. skb->tail = skb->data - skb->head;
  767. }
  768. static inline void skb_set_tail_pointer(struct sk_buff *skb, const int offset)
  769. {
  770. skb_reset_tail_pointer(skb);
  771. skb->tail += offset;
  772. }
  773. #else /* NET_SKBUFF_DATA_USES_OFFSET */
  774. static inline unsigned char *skb_tail_pointer(const struct sk_buff *skb)
  775. {
  776. return skb->tail;
  777. }
  778. static inline void skb_reset_tail_pointer(struct sk_buff *skb)
  779. {
  780. skb->tail = skb->data;
  781. }
  782. static inline void skb_set_tail_pointer(struct sk_buff *skb, const int offset)
  783. {
  784. skb->tail = skb->data + offset;
  785. }
  786. #endif /* NET_SKBUFF_DATA_USES_OFFSET */
  787. /*
  788. * Add data to an sk_buff
  789. */
  790. static inline unsigned char *__skb_put(struct sk_buff *skb, unsigned int len)
  791. {
  792. unsigned char *tmp = skb_tail_pointer(skb);
  793. SKB_LINEAR_ASSERT(skb);
  794. skb->tail += len;
  795. skb->len += len;
  796. return tmp;
  797. }
  798. /**
  799. * skb_put - add data to a buffer
  800. * @skb: buffer to use
  801. * @len: amount of data to add
  802. *
  803. * This function extends the used data area of the buffer. If this would
  804. * exceed the total buffer size the kernel will panic. A pointer to the
  805. * first byte of the extra data is returned.
  806. */
  807. static inline unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
  808. {
  809. unsigned char *tmp = skb_tail_pointer(skb);
  810. SKB_LINEAR_ASSERT(skb);
  811. skb->tail += len;
  812. skb->len += len;
  813. if (unlikely(skb->tail > skb->end))
  814. skb_over_panic(skb, len, current_text_addr());
  815. return tmp;
  816. }
  817. static inline unsigned char *__skb_push(struct sk_buff *skb, unsigned int len)
  818. {
  819. skb->data -= len;
  820. skb->len += len;
  821. return skb->data;
  822. }
  823. /**
  824. * skb_push - add data to the start of a buffer
  825. * @skb: buffer to use
  826. * @len: amount of data to add
  827. *
  828. * This function extends the used data area of the buffer at the buffer
  829. * start. If this would exceed the total buffer headroom the kernel will
  830. * panic. A pointer to the first byte of the extra data is returned.
  831. */
  832. static inline unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
  833. {
  834. skb->data -= len;
  835. skb->len += len;
  836. if (unlikely(skb->data<skb->head))
  837. skb_under_panic(skb, len, current_text_addr());
  838. return skb->data;
  839. }
  840. static inline unsigned char *__skb_pull(struct sk_buff *skb, unsigned int len)
  841. {
  842. skb->len -= len;
  843. BUG_ON(skb->len < skb->data_len);
  844. return skb->data += len;
  845. }
  846. /**
  847. * skb_pull - remove data from the start of a buffer
  848. * @skb: buffer to use
  849. * @len: amount of data to remove
  850. *
  851. * This function removes data from the start of a buffer, returning
  852. * the memory to the headroom. A pointer to the next data in the buffer
  853. * is returned. Once the data has been pulled future pushes will overwrite
  854. * the old data.
  855. */
  856. static inline unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
  857. {
  858. return unlikely(len > skb->len) ? NULL : __skb_pull(skb, len);
  859. }
  860. extern unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta);
  861. static inline unsigned char *__pskb_pull(struct sk_buff *skb, unsigned int len)
  862. {
  863. if (len > skb_headlen(skb) &&
  864. !__pskb_pull_tail(skb, len-skb_headlen(skb)))
  865. return NULL;
  866. skb->len -= len;
  867. return skb->data += len;
  868. }
  869. static inline unsigned char *pskb_pull(struct sk_buff *skb, unsigned int len)
  870. {
  871. return unlikely(len > skb->len) ? NULL : __pskb_pull(skb, len);
  872. }
  873. static inline int pskb_may_pull(struct sk_buff *skb, unsigned int len)
  874. {
  875. if (likely(len <= skb_headlen(skb)))
  876. return 1;
  877. if (unlikely(len > skb->len))
  878. return 0;
  879. return __pskb_pull_tail(skb, len-skb_headlen(skb)) != NULL;
  880. }
  881. /**
  882. * skb_headroom - bytes at buffer head
  883. * @skb: buffer to check
  884. *
  885. * Return the number of bytes of free space at the head of an &sk_buff.
  886. */
  887. static inline int skb_headroom(const struct sk_buff *skb)
  888. {
  889. return skb->data - skb->head;
  890. }
  891. /**
  892. * skb_tailroom - bytes at buffer end
  893. * @skb: buffer to check
  894. *
  895. * Return the number of bytes of free space at the tail of an sk_buff
  896. */
  897. static inline int skb_tailroom(const struct sk_buff *skb)
  898. {
  899. return skb_is_nonlinear(skb) ? 0 : skb->end - skb->tail;
  900. }
  901. /**
  902. * skb_reserve - adjust headroom
  903. * @skb: buffer to alter
  904. * @len: bytes to move
  905. *
  906. * Increase the headroom of an empty &sk_buff by reducing the tail
  907. * room. This is only allowed for an empty buffer.
  908. */
  909. static inline void skb_reserve(struct sk_buff *skb, int len)
  910. {
  911. skb->data += len;
  912. skb->tail += len;
  913. }
  914. #ifdef NET_SKBUFF_DATA_USES_OFFSET
  915. static inline unsigned char *skb_transport_header(const struct sk_buff *skb)
  916. {
  917. return skb->head + skb->transport_header;
  918. }
  919. static inline void skb_reset_transport_header(struct sk_buff *skb)
  920. {
  921. skb->transport_header = skb->data - skb->head;
  922. }
  923. static inline void skb_set_transport_header(struct sk_buff *skb,
  924. const int offset)
  925. {
  926. skb_reset_transport_header(skb);
  927. skb->transport_header += offset;
  928. }
  929. static inline unsigned char *skb_network_header(const struct sk_buff *skb)
  930. {
  931. return skb->head + skb->network_header;
  932. }
  933. static inline void skb_reset_network_header(struct sk_buff *skb)
  934. {
  935. skb->network_header = skb->data - skb->head;
  936. }
  937. static inline void skb_set_network_header(struct sk_buff *skb, const int offset)
  938. {
  939. skb_reset_network_header(skb);
  940. skb->network_header += offset;
  941. }
  942. static inline unsigned char *skb_mac_header(const struct sk_buff *skb)
  943. {
  944. return skb->head + skb->mac_header;
  945. }
  946. static inline int skb_mac_header_was_set(const struct sk_buff *skb)
  947. {
  948. return skb->mac_header != ~0U;
  949. }
  950. static inline void skb_reset_mac_header(struct sk_buff *skb)
  951. {
  952. skb->mac_header = skb->data - skb->head;
  953. }
  954. static inline void skb_set_mac_header(struct sk_buff *skb, const int offset)
  955. {
  956. skb_reset_mac_header(skb);
  957. skb->mac_header += offset;
  958. }
  959. #else /* NET_SKBUFF_DATA_USES_OFFSET */
  960. static inline unsigned char *skb_transport_header(const struct sk_buff *skb)
  961. {
  962. return skb->transport_header;
  963. }
  964. static inline void skb_reset_transport_header(struct sk_buff *skb)
  965. {
  966. skb->transport_header = skb->data;
  967. }
  968. static inline void skb_set_transport_header(struct sk_buff *skb,
  969. const int offset)
  970. {
  971. skb->transport_header = skb->data + offset;
  972. }
  973. static inline unsigned char *skb_network_header(const struct sk_buff *skb)
  974. {
  975. return skb->network_header;
  976. }
  977. static inline void skb_reset_network_header(struct sk_buff *skb)
  978. {
  979. skb->network_header = skb->data;
  980. }
  981. static inline void skb_set_network_header(struct sk_buff *skb, const int offset)
  982. {
  983. skb->network_header = skb->data + offset;
  984. }
  985. static inline unsigned char *skb_mac_header(const struct sk_buff *skb)
  986. {
  987. return skb->mac_header;
  988. }
  989. static inline int skb_mac_header_was_set(const struct sk_buff *skb)
  990. {
  991. return skb->mac_header != NULL;
  992. }
  993. static inline void skb_reset_mac_header(struct sk_buff *skb)
  994. {
  995. skb->mac_header = skb->data;
  996. }
  997. static inline void skb_set_mac_header(struct sk_buff *skb, const int offset)
  998. {
  999. skb->mac_header = skb->data + offset;
  1000. }
  1001. #endif /* NET_SKBUFF_DATA_USES_OFFSET */
  1002. static inline int skb_transport_offset(const struct sk_buff *skb)
  1003. {
  1004. return skb_transport_header(skb) - skb->data;
  1005. }
  1006. static inline u32 skb_network_header_len(const struct sk_buff *skb)
  1007. {
  1008. return skb->transport_header - skb->network_header;
  1009. }
  1010. static inline int skb_network_offset(const struct sk_buff *skb)
  1011. {
  1012. return skb_network_header(skb) - skb->data;
  1013. }
  1014. /*
  1015. * CPUs often take a performance hit when accessing unaligned memory
  1016. * locations. The actual performance hit varies, it can be small if the
  1017. * hardware handles it or large if we have to take an exception and fix it
  1018. * in software.
  1019. *
  1020. * Since an ethernet header is 14 bytes network drivers often end up with
  1021. * the IP header at an unaligned offset. The IP header can be aligned by
  1022. * shifting the start of the packet by 2 bytes. Drivers should do this
  1023. * with:
  1024. *
  1025. * skb_reserve(NET_IP_ALIGN);
  1026. *
  1027. * The downside to this alignment of the IP header is that the DMA is now
  1028. * unaligned. On some architectures the cost of an unaligned DMA is high
  1029. * and this cost outweighs the gains made by aligning the IP header.
  1030. *
  1031. * Since this trade off varies between architectures, we allow NET_IP_ALIGN
  1032. * to be overridden.
  1033. */
  1034. #ifndef NET_IP_ALIGN
  1035. #define NET_IP_ALIGN 2
  1036. #endif
  1037. /*
  1038. * The networking layer reserves some headroom in skb data (via
  1039. * dev_alloc_skb). This is used to avoid having to reallocate skb data when
  1040. * the header has to grow. In the default case, if the header has to grow
  1041. * 16 bytes or less we avoid the reallocation.
  1042. *
  1043. * Unfortunately this headroom changes the DMA alignment of the resulting
  1044. * network packet. As for NET_IP_ALIGN, this unaligned DMA is expensive
  1045. * on some architectures. An architecture can override this value,
  1046. * perhaps setting it to a cacheline in size (since that will maintain
  1047. * cacheline alignment of the DMA). It must be a power of 2.
  1048. *
  1049. * Various parts of the networking layer expect at least 16 bytes of
  1050. * headroom, you should not reduce this.
  1051. */
  1052. #ifndef NET_SKB_PAD
  1053. #define NET_SKB_PAD 16
  1054. #endif
  1055. extern int ___pskb_trim(struct sk_buff *skb, unsigned int len);
  1056. static inline void __skb_trim(struct sk_buff *skb, unsigned int len)
  1057. {
  1058. if (unlikely(skb->data_len)) {
  1059. WARN_ON(1);
  1060. return;
  1061. }
  1062. skb->len = len;
  1063. skb_set_tail_pointer(skb, len);
  1064. }
  1065. /**
  1066. * skb_trim - remove end from a buffer
  1067. * @skb: buffer to alter
  1068. * @len: new length
  1069. *
  1070. * Cut the length of a buffer down by removing data from the tail. If
  1071. * the buffer is already under the length specified it is not modified.
  1072. * The skb must be linear.
  1073. */
  1074. static inline void skb_trim(struct sk_buff *skb, unsigned int len)
  1075. {
  1076. if (skb->len > len)
  1077. __skb_trim(skb, len);
  1078. }
  1079. static inline int __pskb_trim(struct sk_buff *skb, unsigned int len)
  1080. {
  1081. if (skb->data_len)
  1082. return ___pskb_trim(skb, len);
  1083. __skb_trim(skb, len);
  1084. return 0;
  1085. }
  1086. static inline int pskb_trim(struct sk_buff *skb, unsigned int len)
  1087. {
  1088. return (len < skb->len) ? __pskb_trim(skb, len) : 0;
  1089. }
  1090. /**
  1091. * pskb_trim_unique - remove end from a paged unique (not cloned) buffer
  1092. * @skb: buffer to alter
  1093. * @len: new length
  1094. *
  1095. * This is identical to pskb_trim except that the caller knows that
  1096. * the skb is not cloned so we should never get an error due to out-
  1097. * of-memory.
  1098. */
  1099. static inline void pskb_trim_unique(struct sk_buff *skb, unsigned int len)
  1100. {
  1101. int err = pskb_trim(skb, len);
  1102. BUG_ON(err);
  1103. }
  1104. /**
  1105. * skb_orphan - orphan a buffer
  1106. * @skb: buffer to orphan
  1107. *
  1108. * If a buffer currently has an owner then we call the owner's
  1109. * destructor function and make the @skb unowned. The buffer continues
  1110. * to exist but is no longer charged to its former owner.
  1111. */
  1112. static inline void skb_orphan(struct sk_buff *skb)
  1113. {
  1114. if (skb->destructor)
  1115. skb->destructor(skb);
  1116. skb->destructor = NULL;
  1117. skb->sk = NULL;
  1118. }
  1119. /**
  1120. * __skb_queue_purge - empty a list
  1121. * @list: list to empty
  1122. *
  1123. * Delete all buffers on an &sk_buff list. Each buffer is removed from
  1124. * the list and one reference dropped. This function does not take the
  1125. * list lock and the caller must hold the relevant locks to use it.
  1126. */
  1127. extern void skb_queue_purge(struct sk_buff_head *list);
  1128. static inline void __skb_queue_purge(struct sk_buff_head *list)
  1129. {
  1130. struct sk_buff *skb;
  1131. while ((skb = __skb_dequeue(list)) != NULL)
  1132. kfree_skb(skb);
  1133. }
  1134. /**
  1135. * __dev_alloc_skb - allocate an skbuff for receiving
  1136. * @length: length to allocate
  1137. * @gfp_mask: get_free_pages mask, passed to alloc_skb
  1138. *
  1139. * Allocate a new &sk_buff and assign it a usage count of one. The
  1140. * buffer has unspecified headroom built in. Users should allocate
  1141. * the headroom they think they need without accounting for the
  1142. * built in space. The built in space is used for optimisations.
  1143. *
  1144. * %NULL is returned if there is no free memory.
  1145. */
  1146. static inline struct sk_buff *__dev_alloc_skb(unsigned int length,
  1147. gfp_t gfp_mask)
  1148. {
  1149. struct sk_buff *skb = alloc_skb(length + NET_SKB_PAD, gfp_mask);
  1150. if (likely(skb))
  1151. skb_reserve(skb, NET_SKB_PAD);
  1152. return skb;
  1153. }
  1154. /**
  1155. * dev_alloc_skb - allocate an skbuff for receiving
  1156. * @length: length to allocate
  1157. *
  1158. * Allocate a new &sk_buff and assign it a usage count of one. The
  1159. * buffer has unspecified headroom built in. Users should allocate
  1160. * the headroom they think they need without accounting for the
  1161. * built in space. The built in space is used for optimisations.
  1162. *
  1163. * %NULL is returned if there is no free memory. Although this function
  1164. * allocates memory it can be called from an interrupt.
  1165. */
  1166. static inline struct sk_buff *dev_alloc_skb(unsigned int length)
  1167. {
  1168. return __dev_alloc_skb(length, GFP_ATOMIC);
  1169. }
  1170. extern struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
  1171. unsigned int length, gfp_t gfp_mask);
  1172. /**
  1173. * netdev_alloc_skb - allocate an skbuff for rx on a specific device
  1174. * @dev: network device to receive on
  1175. * @length: length to allocate
  1176. *
  1177. * Allocate a new &sk_buff and assign it a usage count of one. The
  1178. * buffer has unspecified headroom built in. Users should allocate
  1179. * the headroom they think they need without accounting for the
  1180. * built in space. The built in space is used for optimisations.
  1181. *
  1182. * %NULL is returned if there is no free memory. Although this function
  1183. * allocates memory it can be called from an interrupt.
  1184. */
  1185. static inline struct sk_buff *netdev_alloc_skb(struct net_device *dev,
  1186. unsigned int length)
  1187. {
  1188. return __netdev_alloc_skb(dev, length, GFP_ATOMIC);
  1189. }
  1190. /**
  1191. * skb_clone_writable - is the header of a clone writable
  1192. * @skb: buffer to check
  1193. * @len: length up to which to write
  1194. *
  1195. * Returns true if modifying the header part of the cloned buffer
  1196. * does not requires the data to be copied.
  1197. */
  1198. static inline int skb_clone_writable(struct sk_buff *skb, int len)
  1199. {
  1200. return !skb_header_cloned(skb) &&
  1201. skb_headroom(skb) + len <= skb->hdr_len;
  1202. }
  1203. static inline int __skb_cow(struct sk_buff *skb, unsigned int headroom,
  1204. int cloned)
  1205. {
  1206. int delta = 0;
  1207. if (headroom < NET_SKB_PAD)
  1208. headroom = NET_SKB_PAD;
  1209. if (headroom > skb_headroom(skb))
  1210. delta = headroom - skb_headroom(skb);
  1211. if (delta || cloned)
  1212. return pskb_expand_head(skb, ALIGN(delta, NET_SKB_PAD), 0,
  1213. GFP_ATOMIC);
  1214. return 0;
  1215. }
  1216. /**
  1217. * skb_cow - copy header of skb when it is required
  1218. * @skb: buffer to cow
  1219. * @headroom: needed headroom
  1220. *
  1221. * If the skb passed lacks sufficient headroom or its data part
  1222. * is shared, data is reallocated. If reallocation fails, an error
  1223. * is returned and original skb is not changed.
  1224. *
  1225. * The result is skb with writable area skb->head...skb->tail
  1226. * and at least @headroom of space at head.
  1227. */
  1228. static inline int skb_cow(struct sk_buff *skb, unsigned int headroom)
  1229. {
  1230. return __skb_cow(skb, headroom, skb_cloned(skb));
  1231. }
  1232. /**
  1233. * skb_cow_head - skb_cow but only making the head writable
  1234. * @skb: buffer to cow
  1235. * @headroom: needed headroom
  1236. *
  1237. * This function is identical to skb_cow except that we replace the
  1238. * skb_cloned check by skb_header_cloned. It should be used when
  1239. * you only need to push on some header and do not need to modify
  1240. * the data.
  1241. */
  1242. static inline int skb_cow_head(struct sk_buff *skb, unsigned int headroom)
  1243. {
  1244. return __skb_cow(skb, headroom, skb_header_cloned(skb));
  1245. }
  1246. /**
  1247. * skb_padto - pad an skbuff up to a minimal size
  1248. * @skb: buffer to pad
  1249. * @len: minimal length
  1250. *
  1251. * Pads up a buffer to ensure the trailing bytes exist and are
  1252. * blanked. If the buffer already contains sufficient data it
  1253. * is untouched. Otherwise it is extended. Returns zero on
  1254. * success. The skb is freed on error.
  1255. */
  1256. static inline int skb_padto(struct sk_buff *skb, unsigned int len)
  1257. {
  1258. unsigned int size = skb->len;
  1259. if (likely(size >= len))
  1260. return 0;
  1261. return skb_pad(skb, len-size);
  1262. }
  1263. static inline int skb_add_data(struct sk_buff *skb,
  1264. char __user *from, int copy)
  1265. {
  1266. const int off = skb->len;
  1267. if (skb->ip_summed == CHECKSUM_NONE) {
  1268. int err = 0;
  1269. __wsum csum = csum_and_copy_from_user(from, skb_put(skb, copy),
  1270. copy, 0, &err);
  1271. if (!err) {
  1272. skb->csum = csum_block_add(skb->csum, csum, off);
  1273. return 0;
  1274. }
  1275. } else if (!copy_from_user(skb_put(skb, copy), from, copy))
  1276. return 0;
  1277. __skb_trim(skb, off);
  1278. return -EFAULT;
  1279. }
  1280. static inline int skb_can_coalesce(struct sk_buff *skb, int i,
  1281. struct page *page, int off)
  1282. {
  1283. if (i) {
  1284. struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i - 1];
  1285. return page == frag->page &&
  1286. off == frag->page_offset + frag->size;
  1287. }
  1288. return 0;
  1289. }
  1290. static inline int __skb_linearize(struct sk_buff *skb)
  1291. {
  1292. return __pskb_pull_tail(skb, skb->data_len) ? 0 : -ENOMEM;
  1293. }
  1294. /**
  1295. * skb_linearize - convert paged skb to linear one
  1296. * @skb: buffer to linarize
  1297. *
  1298. * If there is no free memory -ENOMEM is returned, otherwise zero
  1299. * is returned and the old skb data released.
  1300. */
  1301. static inline int skb_linearize(struct sk_buff *skb)
  1302. {
  1303. return skb_is_nonlinear(skb) ? __skb_linearize(skb) : 0;
  1304. }
  1305. /**
  1306. * skb_linearize_cow - make sure skb is linear and writable
  1307. * @skb: buffer to process
  1308. *
  1309. * If there is no free memory -ENOMEM is returned, otherwise zero
  1310. * is returned and the old skb data released.
  1311. */
  1312. static inline int skb_linearize_cow(struct sk_buff *skb)
  1313. {
  1314. return skb_is_nonlinear(skb) || skb_cloned(skb) ?
  1315. __skb_linearize(skb) : 0;
  1316. }
  1317. /**
  1318. * skb_postpull_rcsum - update checksum for received skb after pull
  1319. * @skb: buffer to update
  1320. * @start: start of data before pull
  1321. * @len: length of data pulled
  1322. *
  1323. * After doing a pull on a received packet, you need to call this to
  1324. * update the CHECKSUM_COMPLETE checksum, or set ip_summed to
  1325. * CHECKSUM_NONE so that it can be recomputed from scratch.
  1326. */
  1327. static inline void skb_postpull_rcsum(struct sk_buff *skb,
  1328. const void *start, unsigned int len)
  1329. {
  1330. if (skb->ip_summed == CHECKSUM_COMPLETE)
  1331. skb->csum = csum_sub(skb->csum, csum_partial(start, len, 0));
  1332. }
  1333. unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len);
  1334. /**
  1335. * pskb_trim_rcsum - trim received skb and update checksum
  1336. * @skb: buffer to trim
  1337. * @len: new length
  1338. *
  1339. * This is exactly the same as pskb_trim except that it ensures the
  1340. * checksum of received packets are still valid after the operation.
  1341. */
  1342. static inline int pskb_trim_rcsum(struct sk_buff *skb, unsigned int len)
  1343. {
  1344. if (likely(len >= skb->len))
  1345. return 0;
  1346. if (skb->ip_summed == CHECKSUM_COMPLETE)
  1347. skb->ip_summed = CHECKSUM_NONE;
  1348. return __pskb_trim(skb, len);
  1349. }
  1350. #define skb_queue_walk(queue, skb) \
  1351. for (skb = (queue)->next; \
  1352. prefetch(skb->next), (skb != (struct sk_buff *)(queue)); \
  1353. skb = skb->next)
  1354. #define skb_queue_walk_safe(queue, skb, tmp) \
  1355. for (skb = (queue)->next, tmp = skb->next; \
  1356. skb != (struct sk_buff *)(queue); \
  1357. skb = tmp, tmp = skb->next)
  1358. #define skb_queue_reverse_walk(queue, skb) \
  1359. for (skb = (queue)->prev; \
  1360. prefetch(skb->prev), (skb != (struct sk_buff *)(queue)); \
  1361. skb = skb->prev)
  1362. extern struct sk_buff *skb_recv_datagram(struct sock *sk, unsigned flags,
  1363. int noblock, int *err);
  1364. extern unsigned int datagram_poll(struct file *file, struct socket *sock,
  1365. struct poll_table_struct *wait);
  1366. extern int skb_copy_datagram_iovec(const struct sk_buff *from,
  1367. int offset, struct iovec *to,
  1368. int size);
  1369. extern int skb_copy_and_csum_datagram_iovec(struct sk_buff *skb,
  1370. int hlen,
  1371. struct iovec *iov);
  1372. extern void skb_free_datagram(struct sock *sk, struct sk_buff *skb);
  1373. extern void skb_kill_datagram(struct sock *sk, struct sk_buff *skb,
  1374. unsigned int flags);
  1375. extern __wsum skb_checksum(const struct sk_buff *skb, int offset,
  1376. int len, __wsum csum);
  1377. extern int skb_copy_bits(const struct sk_buff *skb, int offset,
  1378. void *to, int len);
  1379. extern int skb_store_bits(struct sk_buff *skb, int offset,
  1380. const void *from, int len);
  1381. extern __wsum skb_copy_and_csum_bits(const struct sk_buff *skb,
  1382. int offset, u8 *to, int len,
  1383. __wsum csum);
  1384. extern void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to);
  1385. extern void skb_split(struct sk_buff *skb,
  1386. struct sk_buff *skb1, const u32 len);
  1387. extern struct sk_buff *skb_segment(struct sk_buff *skb, int features);
  1388. static inline void *skb_header_pointer(const struct sk_buff *skb, int offset,
  1389. int len, void *buffer)
  1390. {
  1391. int hlen = skb_headlen(skb);
  1392. if (hlen - offset >= len)
  1393. return skb->data + offset;
  1394. if (skb_copy_bits(skb, offset, buffer, len) < 0)
  1395. return NULL;
  1396. return buffer;
  1397. }
  1398. static inline void skb_copy_from_linear_data(const struct sk_buff *skb,
  1399. void *to,
  1400. const unsigned int len)
  1401. {
  1402. memcpy(to, skb->data, len);
  1403. }
  1404. static inline void skb_copy_from_linear_data_offset(const struct sk_buff *skb,
  1405. const int offset, void *to,
  1406. const unsigned int len)
  1407. {
  1408. memcpy(to, skb->data + offset, len);
  1409. }
  1410. static inline void skb_copy_to_linear_data(struct sk_buff *skb,
  1411. const void *from,
  1412. const unsigned int len)
  1413. {
  1414. memcpy(skb->data, from, len);
  1415. }
  1416. static inline void skb_copy_to_linear_data_offset(struct sk_buff *skb,
  1417. const int offset,
  1418. const void *from,
  1419. const unsigned int len)
  1420. {
  1421. memcpy(skb->data + offset, from, len);
  1422. }
  1423. extern void skb_init(void);
  1424. /**
  1425. * skb_get_timestamp - get timestamp from a skb
  1426. * @skb: skb to get stamp from
  1427. * @stamp: pointer to struct timeval to store stamp in
  1428. *
  1429. * Timestamps are stored in the skb as offsets to a base timestamp.
  1430. * This function converts the offset back to a struct timeval and stores
  1431. * it in stamp.
  1432. */
  1433. static inline void skb_get_timestamp(const struct sk_buff *skb, struct timeval *stamp)
  1434. {
  1435. *stamp = ktime_to_timeval(skb->tstamp);
  1436. }
  1437. static inline void __net_timestamp(struct sk_buff *skb)
  1438. {
  1439. skb->tstamp = ktime_get_real();
  1440. }
  1441. static inline ktime_t net_timedelta(ktime_t t)
  1442. {
  1443. return ktime_sub(ktime_get_real(), t);
  1444. }
  1445. static inline ktime_t net_invalid_timestamp(void)
  1446. {
  1447. return ktime_set(0, 0);
  1448. }
  1449. extern __sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len);
  1450. extern __sum16 __skb_checksum_complete(struct sk_buff *skb);
  1451. static inline int skb_csum_unnecessary(const struct sk_buff *skb)
  1452. {
  1453. return skb->ip_summed & CHECKSUM_UNNECESSARY;
  1454. }
  1455. /**
  1456. * skb_checksum_complete - Calculate checksum of an entire packet
  1457. * @skb: packet to process
  1458. *
  1459. * This function calculates the checksum over the entire packet plus
  1460. * the value of skb->csum. The latter can be used to supply the
  1461. * checksum of a pseudo header as used by TCP/UDP. It returns the
  1462. * checksum.
  1463. *
  1464. * For protocols that contain complete checksums such as ICMP/TCP/UDP,
  1465. * this function can be used to verify that checksum on received
  1466. * packets. In that case the function should return zero if the
  1467. * checksum is correct. In particular, this function will return zero
  1468. * if skb->ip_summed is CHECKSUM_UNNECESSARY which indicates that the
  1469. * hardware has already verified the correctness of the checksum.
  1470. */
  1471. static inline __sum16 skb_checksum_complete(struct sk_buff *skb)
  1472. {
  1473. return skb_csum_unnecessary(skb) ?
  1474. 0 : __skb_checksum_complete(skb);
  1475. }
  1476. #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
  1477. extern void nf_conntrack_destroy(struct nf_conntrack *nfct);
  1478. static inline void nf_conntrack_put(struct nf_conntrack *nfct)
  1479. {
  1480. if (nfct && atomic_dec_and_test(&nfct->use))
  1481. nf_conntrack_destroy(nfct);
  1482. }
  1483. static inline void nf_conntrack_get(struct nf_conntrack *nfct)
  1484. {
  1485. if (nfct)
  1486. atomic_inc(&nfct->use);
  1487. }
  1488. static inline void nf_conntrack_get_reasm(struct sk_buff *skb)
  1489. {
  1490. if (skb)
  1491. atomic_inc(&skb->users);
  1492. }
  1493. static inline void nf_conntrack_put_reasm(struct sk_buff *skb)
  1494. {
  1495. if (skb)
  1496. kfree_skb(skb);
  1497. }
  1498. #endif
  1499. #ifdef CONFIG_BRIDGE_NETFILTER
  1500. static inline void nf_bridge_put(struct nf_bridge_info *nf_bridge)
  1501. {
  1502. if (nf_bridge && atomic_dec_and_test(&nf_bridge->use))
  1503. kfree(nf_bridge);
  1504. }
  1505. static inline void nf_bridge_get(struct nf_bridge_info *nf_bridge)
  1506. {
  1507. if (nf_bridge)
  1508. atomic_inc(&nf_bridge->use);
  1509. }
  1510. #endif /* CONFIG_BRIDGE_NETFILTER */
  1511. static inline void nf_reset(struct sk_buff *skb)
  1512. {
  1513. #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
  1514. nf_conntrack_put(skb->nfct);
  1515. skb->nfct = NULL;
  1516. nf_conntrack_put_reasm(skb->nfct_reasm);
  1517. skb->nfct_reasm = NULL;
  1518. #endif
  1519. #ifdef CONFIG_BRIDGE_NETFILTER
  1520. nf_bridge_put(skb->nf_bridge);
  1521. skb->nf_bridge = NULL;
  1522. #endif
  1523. }
  1524. /* Note: This doesn't put any conntrack and bridge info in dst. */
  1525. static inline void __nf_copy(struct sk_buff *dst, const struct sk_buff *src)
  1526. {
  1527. #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
  1528. dst->nfct = src->nfct;
  1529. nf_conntrack_get(src->nfct);
  1530. dst->nfctinfo = src->nfctinfo;
  1531. dst->nfct_reasm = src->nfct_reasm;
  1532. nf_conntrack_get_reasm(src->nfct_reasm);
  1533. #endif
  1534. #ifdef CONFIG_BRIDGE_NETFILTER
  1535. dst->nf_bridge = src->nf_bridge;
  1536. nf_bridge_get(src->nf_bridge);
  1537. #endif
  1538. }
  1539. static inline void nf_copy(struct sk_buff *dst, const struct sk_buff *src)
  1540. {
  1541. #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
  1542. nf_conntrack_put(dst->nfct);
  1543. nf_conntrack_put_reasm(dst->nfct_reasm);
  1544. #endif
  1545. #ifdef CONFIG_BRIDGE_NETFILTER
  1546. nf_bridge_put(dst->nf_bridge);
  1547. #endif
  1548. __nf_copy(dst, src);
  1549. }
  1550. #ifdef CONFIG_NETWORK_SECMARK
  1551. static inline void skb_copy_secmark(struct sk_buff *to, const struct sk_buff *from)
  1552. {
  1553. to->secmark = from->secmark;
  1554. }
  1555. static inline void skb_init_secmark(struct sk_buff *skb)
  1556. {
  1557. skb->secmark = 0;
  1558. }
  1559. #else
  1560. static inline void skb_copy_secmark(struct sk_buff *to, const struct sk_buff *from)
  1561. { }
  1562. static inline void skb_init_secmark(struct sk_buff *skb)
  1563. { }
  1564. #endif
  1565. static inline void skb_set_queue_mapping(struct sk_buff *skb, u16 queue_mapping)
  1566. {
  1567. #ifdef CONFIG_NETDEVICES_MULTIQUEUE
  1568. skb->queue_mapping = queue_mapping;
  1569. #endif
  1570. }
  1571. static inline void skb_copy_queue_mapping(struct sk_buff *to, const struct sk_buff *from)
  1572. {
  1573. #ifdef CONFIG_NETDEVICES_MULTIQUEUE
  1574. to->queue_mapping = from->queue_mapping;
  1575. #endif
  1576. }
  1577. static inline int skb_is_gso(const struct sk_buff *skb)
  1578. {
  1579. return skb_shinfo(skb)->gso_size;
  1580. }
  1581. static inline void skb_forward_csum(struct sk_buff *skb)
  1582. {
  1583. /* Unfortunately we don't support this one. Any brave souls? */
  1584. if (skb->ip_summed == CHECKSUM_COMPLETE)
  1585. skb->ip_summed = CHECKSUM_NONE;
  1586. }
  1587. #endif /* __KERNEL__ */
  1588. #endif /* _LINUX_SKBUFF_H */