skbuff.h 62 KB

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