skbuff.h 62 KB

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