skbuff.c 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314
  1. /*
  2. * Routines having to do with the 'struct sk_buff' memory handlers.
  3. *
  4. * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>
  5. * Florian La Roche <rzsfl@rz.uni-sb.de>
  6. *
  7. * Fixes:
  8. * Alan Cox : Fixed the worst of the load
  9. * balancer bugs.
  10. * Dave Platt : Interrupt stacking fix.
  11. * Richard Kooijman : Timestamp fixes.
  12. * Alan Cox : Changed buffer format.
  13. * Alan Cox : destructor hook for AF_UNIX etc.
  14. * Linus Torvalds : Better skb_clone.
  15. * Alan Cox : Added skb_copy.
  16. * Alan Cox : Added all the changed routines Linus
  17. * only put in the headers
  18. * Ray VanTassle : Fixed --skb->lock in free
  19. * Alan Cox : skb_copy copy arp field
  20. * Andi Kleen : slabified it.
  21. * Robert Olsson : Removed skb_head_pool
  22. *
  23. * NOTE:
  24. * The __skb_ routines should be called with interrupts
  25. * disabled, or you better be *real* sure that the operation is atomic
  26. * with respect to whatever list is being frobbed (e.g. via lock_sock()
  27. * or via disabling bottom half handlers, etc).
  28. *
  29. * This program is free software; you can redistribute it and/or
  30. * modify it under the terms of the GNU General Public License
  31. * as published by the Free Software Foundation; either version
  32. * 2 of the License, or (at your option) any later version.
  33. */
  34. /*
  35. * The functions in this file will not compile correctly with gcc 2.4.x
  36. */
  37. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  38. #include <linux/module.h>
  39. #include <linux/types.h>
  40. #include <linux/kernel.h>
  41. #include <linux/kmemcheck.h>
  42. #include <linux/mm.h>
  43. #include <linux/interrupt.h>
  44. #include <linux/in.h>
  45. #include <linux/inet.h>
  46. #include <linux/slab.h>
  47. #include <linux/tcp.h>
  48. #include <linux/udp.h>
  49. #include <linux/netdevice.h>
  50. #ifdef CONFIG_NET_CLS_ACT
  51. #include <net/pkt_sched.h>
  52. #endif
  53. #include <linux/string.h>
  54. #include <linux/skbuff.h>
  55. #include <linux/splice.h>
  56. #include <linux/cache.h>
  57. #include <linux/rtnetlink.h>
  58. #include <linux/init.h>
  59. #include <linux/scatterlist.h>
  60. #include <linux/errqueue.h>
  61. #include <linux/prefetch.h>
  62. #include <net/protocol.h>
  63. #include <net/dst.h>
  64. #include <net/sock.h>
  65. #include <net/checksum.h>
  66. #include <net/xfrm.h>
  67. #include <asm/uaccess.h>
  68. #include <trace/events/skb.h>
  69. #include "kmap_skb.h"
  70. static struct kmem_cache *skbuff_head_cache __read_mostly;
  71. static struct kmem_cache *skbuff_fclone_cache __read_mostly;
  72. static void sock_pipe_buf_release(struct pipe_inode_info *pipe,
  73. struct pipe_buffer *buf)
  74. {
  75. put_page(buf->page);
  76. }
  77. static void sock_pipe_buf_get(struct pipe_inode_info *pipe,
  78. struct pipe_buffer *buf)
  79. {
  80. get_page(buf->page);
  81. }
  82. static int sock_pipe_buf_steal(struct pipe_inode_info *pipe,
  83. struct pipe_buffer *buf)
  84. {
  85. return 1;
  86. }
  87. /* Pipe buffer operations for a socket. */
  88. static const struct pipe_buf_operations sock_pipe_buf_ops = {
  89. .can_merge = 0,
  90. .map = generic_pipe_buf_map,
  91. .unmap = generic_pipe_buf_unmap,
  92. .confirm = generic_pipe_buf_confirm,
  93. .release = sock_pipe_buf_release,
  94. .steal = sock_pipe_buf_steal,
  95. .get = sock_pipe_buf_get,
  96. };
  97. /*
  98. * Keep out-of-line to prevent kernel bloat.
  99. * __builtin_return_address is not used because it is not always
  100. * reliable.
  101. */
  102. /**
  103. * skb_over_panic - private function
  104. * @skb: buffer
  105. * @sz: size
  106. * @here: address
  107. *
  108. * Out of line support code for skb_put(). Not user callable.
  109. */
  110. static void skb_over_panic(struct sk_buff *skb, int sz, void *here)
  111. {
  112. pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n",
  113. __func__, here, skb->len, sz, skb->head, skb->data,
  114. (unsigned long)skb->tail, (unsigned long)skb->end,
  115. skb->dev ? skb->dev->name : "<NULL>");
  116. BUG();
  117. }
  118. /**
  119. * skb_under_panic - private function
  120. * @skb: buffer
  121. * @sz: size
  122. * @here: address
  123. *
  124. * Out of line support code for skb_push(). Not user callable.
  125. */
  126. static void skb_under_panic(struct sk_buff *skb, int sz, void *here)
  127. {
  128. pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n",
  129. __func__, here, skb->len, sz, skb->head, skb->data,
  130. (unsigned long)skb->tail, (unsigned long)skb->end,
  131. skb->dev ? skb->dev->name : "<NULL>");
  132. BUG();
  133. }
  134. /* Allocate a new skbuff. We do this ourselves so we can fill in a few
  135. * 'private' fields and also do memory statistics to find all the
  136. * [BEEP] leaks.
  137. *
  138. */
  139. /**
  140. * __alloc_skb - allocate a network buffer
  141. * @size: size to allocate
  142. * @gfp_mask: allocation mask
  143. * @fclone: allocate from fclone cache instead of head cache
  144. * and allocate a cloned (child) skb
  145. * @node: numa node to allocate memory on
  146. *
  147. * Allocate a new &sk_buff. The returned buffer has no headroom and a
  148. * tail room of size bytes. The object has a reference count of one.
  149. * The return is the buffer. On a failure the return is %NULL.
  150. *
  151. * Buffers may only be allocated from interrupts using a @gfp_mask of
  152. * %GFP_ATOMIC.
  153. */
  154. struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
  155. int fclone, int node)
  156. {
  157. struct kmem_cache *cache;
  158. struct skb_shared_info *shinfo;
  159. struct sk_buff *skb;
  160. u8 *data;
  161. cache = fclone ? skbuff_fclone_cache : skbuff_head_cache;
  162. /* Get the HEAD */
  163. skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
  164. if (!skb)
  165. goto out;
  166. prefetchw(skb);
  167. /* We do our best to align skb_shared_info on a separate cache
  168. * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
  169. * aligned memory blocks, unless SLUB/SLAB debug is enabled.
  170. * Both skb->head and skb_shared_info are cache line aligned.
  171. */
  172. size = SKB_DATA_ALIGN(size);
  173. size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  174. data = kmalloc_node_track_caller(size, gfp_mask, node);
  175. if (unlikely(ZERO_OR_NULL_PTR(data)))
  176. goto nodata;
  177. /* kmalloc(size) might give us more room than requested.
  178. * Put skb_shared_info exactly at the end of allocated zone,
  179. * to allow max possible filling before reallocation.
  180. */
  181. size = SKB_WITH_OVERHEAD(ksize(data));
  182. prefetchw(data + size);
  183. /*
  184. * Only clear those fields we need to clear, not those that we will
  185. * actually initialise below. Hence, don't put any more fields after
  186. * the tail pointer in struct sk_buff!
  187. */
  188. memset(skb, 0, offsetof(struct sk_buff, tail));
  189. /* Account for allocated memory : skb + skb->head */
  190. skb->truesize = SKB_TRUESIZE(size);
  191. atomic_set(&skb->users, 1);
  192. skb->head = data;
  193. skb->data = data;
  194. skb_reset_tail_pointer(skb);
  195. skb->end = skb->tail + size;
  196. #ifdef NET_SKBUFF_DATA_USES_OFFSET
  197. skb->mac_header = ~0U;
  198. #endif
  199. /* make sure we initialize shinfo sequentially */
  200. shinfo = skb_shinfo(skb);
  201. memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
  202. atomic_set(&shinfo->dataref, 1);
  203. kmemcheck_annotate_variable(shinfo->destructor_arg);
  204. if (fclone) {
  205. struct sk_buff *child = skb + 1;
  206. atomic_t *fclone_ref = (atomic_t *) (child + 1);
  207. kmemcheck_annotate_bitfield(child, flags1);
  208. kmemcheck_annotate_bitfield(child, flags2);
  209. skb->fclone = SKB_FCLONE_ORIG;
  210. atomic_set(fclone_ref, 1);
  211. child->fclone = SKB_FCLONE_UNAVAILABLE;
  212. }
  213. out:
  214. return skb;
  215. nodata:
  216. kmem_cache_free(cache, skb);
  217. skb = NULL;
  218. goto out;
  219. }
  220. EXPORT_SYMBOL(__alloc_skb);
  221. /**
  222. * build_skb - build a network buffer
  223. * @data: data buffer provided by caller
  224. *
  225. * Allocate a new &sk_buff. Caller provides space holding head and
  226. * skb_shared_info. @data must have been allocated by kmalloc()
  227. * The return is the new skb buffer.
  228. * On a failure the return is %NULL, and @data is not freed.
  229. * Notes :
  230. * Before IO, driver allocates only data buffer where NIC put incoming frame
  231. * Driver should add room at head (NET_SKB_PAD) and
  232. * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
  233. * After IO, driver calls build_skb(), to allocate sk_buff and populate it
  234. * before giving packet to stack.
  235. * RX rings only contains data buffers, not full skbs.
  236. */
  237. struct sk_buff *build_skb(void *data)
  238. {
  239. struct skb_shared_info *shinfo;
  240. struct sk_buff *skb;
  241. unsigned int size;
  242. skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
  243. if (!skb)
  244. return NULL;
  245. size = ksize(data) - SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  246. memset(skb, 0, offsetof(struct sk_buff, tail));
  247. skb->truesize = SKB_TRUESIZE(size);
  248. atomic_set(&skb->users, 1);
  249. skb->head = data;
  250. skb->data = data;
  251. skb_reset_tail_pointer(skb);
  252. skb->end = skb->tail + size;
  253. #ifdef NET_SKBUFF_DATA_USES_OFFSET
  254. skb->mac_header = ~0U;
  255. #endif
  256. /* make sure we initialize shinfo sequentially */
  257. shinfo = skb_shinfo(skb);
  258. memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
  259. atomic_set(&shinfo->dataref, 1);
  260. kmemcheck_annotate_variable(shinfo->destructor_arg);
  261. return skb;
  262. }
  263. EXPORT_SYMBOL(build_skb);
  264. /**
  265. * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
  266. * @dev: network device to receive on
  267. * @length: length to allocate
  268. * @gfp_mask: get_free_pages mask, passed to alloc_skb
  269. *
  270. * Allocate a new &sk_buff and assign it a usage count of one. The
  271. * buffer has unspecified headroom built in. Users should allocate
  272. * the headroom they think they need without accounting for the
  273. * built in space. The built in space is used for optimisations.
  274. *
  275. * %NULL is returned if there is no free memory.
  276. */
  277. struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
  278. unsigned int length, gfp_t gfp_mask)
  279. {
  280. struct sk_buff *skb;
  281. skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask, 0, NUMA_NO_NODE);
  282. if (likely(skb)) {
  283. skb_reserve(skb, NET_SKB_PAD);
  284. skb->dev = dev;
  285. }
  286. return skb;
  287. }
  288. EXPORT_SYMBOL(__netdev_alloc_skb);
  289. void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
  290. int size, unsigned int truesize)
  291. {
  292. skb_fill_page_desc(skb, i, page, off, size);
  293. skb->len += size;
  294. skb->data_len += size;
  295. skb->truesize += truesize;
  296. }
  297. EXPORT_SYMBOL(skb_add_rx_frag);
  298. /**
  299. * dev_alloc_skb - allocate an skbuff for receiving
  300. * @length: length to allocate
  301. *
  302. * Allocate a new &sk_buff and assign it a usage count of one. The
  303. * buffer has unspecified headroom built in. Users should allocate
  304. * the headroom they think they need without accounting for the
  305. * built in space. The built in space is used for optimisations.
  306. *
  307. * %NULL is returned if there is no free memory. Although this function
  308. * allocates memory it can be called from an interrupt.
  309. */
  310. struct sk_buff *dev_alloc_skb(unsigned int length)
  311. {
  312. /*
  313. * There is more code here than it seems:
  314. * __dev_alloc_skb is an inline
  315. */
  316. return __dev_alloc_skb(length, GFP_ATOMIC);
  317. }
  318. EXPORT_SYMBOL(dev_alloc_skb);
  319. static void skb_drop_list(struct sk_buff **listp)
  320. {
  321. struct sk_buff *list = *listp;
  322. *listp = NULL;
  323. do {
  324. struct sk_buff *this = list;
  325. list = list->next;
  326. kfree_skb(this);
  327. } while (list);
  328. }
  329. static inline void skb_drop_fraglist(struct sk_buff *skb)
  330. {
  331. skb_drop_list(&skb_shinfo(skb)->frag_list);
  332. }
  333. static void skb_clone_fraglist(struct sk_buff *skb)
  334. {
  335. struct sk_buff *list;
  336. skb_walk_frags(skb, list)
  337. skb_get(list);
  338. }
  339. static void skb_release_data(struct sk_buff *skb)
  340. {
  341. if (!skb->cloned ||
  342. !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
  343. &skb_shinfo(skb)->dataref)) {
  344. if (skb_shinfo(skb)->nr_frags) {
  345. int i;
  346. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
  347. skb_frag_unref(skb, i);
  348. }
  349. /*
  350. * If skb buf is from userspace, we need to notify the caller
  351. * the lower device DMA has done;
  352. */
  353. if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
  354. struct ubuf_info *uarg;
  355. uarg = skb_shinfo(skb)->destructor_arg;
  356. if (uarg->callback)
  357. uarg->callback(uarg);
  358. }
  359. if (skb_has_frag_list(skb))
  360. skb_drop_fraglist(skb);
  361. kfree(skb->head);
  362. }
  363. }
  364. /*
  365. * Free an skbuff by memory without cleaning the state.
  366. */
  367. static void kfree_skbmem(struct sk_buff *skb)
  368. {
  369. struct sk_buff *other;
  370. atomic_t *fclone_ref;
  371. switch (skb->fclone) {
  372. case SKB_FCLONE_UNAVAILABLE:
  373. kmem_cache_free(skbuff_head_cache, skb);
  374. break;
  375. case SKB_FCLONE_ORIG:
  376. fclone_ref = (atomic_t *) (skb + 2);
  377. if (atomic_dec_and_test(fclone_ref))
  378. kmem_cache_free(skbuff_fclone_cache, skb);
  379. break;
  380. case SKB_FCLONE_CLONE:
  381. fclone_ref = (atomic_t *) (skb + 1);
  382. other = skb - 1;
  383. /* The clone portion is available for
  384. * fast-cloning again.
  385. */
  386. skb->fclone = SKB_FCLONE_UNAVAILABLE;
  387. if (atomic_dec_and_test(fclone_ref))
  388. kmem_cache_free(skbuff_fclone_cache, other);
  389. break;
  390. }
  391. }
  392. static void skb_release_head_state(struct sk_buff *skb)
  393. {
  394. skb_dst_drop(skb);
  395. #ifdef CONFIG_XFRM
  396. secpath_put(skb->sp);
  397. #endif
  398. if (skb->destructor) {
  399. WARN_ON(in_irq());
  400. skb->destructor(skb);
  401. }
  402. #if IS_ENABLED(CONFIG_NF_CONNTRACK)
  403. nf_conntrack_put(skb->nfct);
  404. #endif
  405. #ifdef NET_SKBUFF_NF_DEFRAG_NEEDED
  406. nf_conntrack_put_reasm(skb->nfct_reasm);
  407. #endif
  408. #ifdef CONFIG_BRIDGE_NETFILTER
  409. nf_bridge_put(skb->nf_bridge);
  410. #endif
  411. /* XXX: IS this still necessary? - JHS */
  412. #ifdef CONFIG_NET_SCHED
  413. skb->tc_index = 0;
  414. #ifdef CONFIG_NET_CLS_ACT
  415. skb->tc_verd = 0;
  416. #endif
  417. #endif
  418. }
  419. /* Free everything but the sk_buff shell. */
  420. static void skb_release_all(struct sk_buff *skb)
  421. {
  422. skb_release_head_state(skb);
  423. skb_release_data(skb);
  424. }
  425. /**
  426. * __kfree_skb - private function
  427. * @skb: buffer
  428. *
  429. * Free an sk_buff. Release anything attached to the buffer.
  430. * Clean the state. This is an internal helper function. Users should
  431. * always call kfree_skb
  432. */
  433. void __kfree_skb(struct sk_buff *skb)
  434. {
  435. skb_release_all(skb);
  436. kfree_skbmem(skb);
  437. }
  438. EXPORT_SYMBOL(__kfree_skb);
  439. /**
  440. * kfree_skb - free an sk_buff
  441. * @skb: buffer to free
  442. *
  443. * Drop a reference to the buffer and free it if the usage count has
  444. * hit zero.
  445. */
  446. void kfree_skb(struct sk_buff *skb)
  447. {
  448. if (unlikely(!skb))
  449. return;
  450. if (likely(atomic_read(&skb->users) == 1))
  451. smp_rmb();
  452. else if (likely(!atomic_dec_and_test(&skb->users)))
  453. return;
  454. trace_kfree_skb(skb, __builtin_return_address(0));
  455. __kfree_skb(skb);
  456. }
  457. EXPORT_SYMBOL(kfree_skb);
  458. /**
  459. * consume_skb - free an skbuff
  460. * @skb: buffer to free
  461. *
  462. * Drop a ref to the buffer and free it if the usage count has hit zero
  463. * Functions identically to kfree_skb, but kfree_skb assumes that the frame
  464. * is being dropped after a failure and notes that
  465. */
  466. void consume_skb(struct sk_buff *skb)
  467. {
  468. if (unlikely(!skb))
  469. return;
  470. if (likely(atomic_read(&skb->users) == 1))
  471. smp_rmb();
  472. else if (likely(!atomic_dec_and_test(&skb->users)))
  473. return;
  474. trace_consume_skb(skb);
  475. __kfree_skb(skb);
  476. }
  477. EXPORT_SYMBOL(consume_skb);
  478. /**
  479. * skb_recycle - clean up an skb for reuse
  480. * @skb: buffer
  481. *
  482. * Recycles the skb to be reused as a receive buffer. This
  483. * function does any necessary reference count dropping, and
  484. * cleans up the skbuff as if it just came from __alloc_skb().
  485. */
  486. void skb_recycle(struct sk_buff *skb)
  487. {
  488. struct skb_shared_info *shinfo;
  489. skb_release_head_state(skb);
  490. shinfo = skb_shinfo(skb);
  491. memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
  492. atomic_set(&shinfo->dataref, 1);
  493. memset(skb, 0, offsetof(struct sk_buff, tail));
  494. skb->data = skb->head + NET_SKB_PAD;
  495. skb_reset_tail_pointer(skb);
  496. }
  497. EXPORT_SYMBOL(skb_recycle);
  498. /**
  499. * skb_recycle_check - check if skb can be reused for receive
  500. * @skb: buffer
  501. * @skb_size: minimum receive buffer size
  502. *
  503. * Checks that the skb passed in is not shared or cloned, and
  504. * that it is linear and its head portion at least as large as
  505. * skb_size so that it can be recycled as a receive buffer.
  506. * If these conditions are met, this function does any necessary
  507. * reference count dropping and cleans up the skbuff as if it
  508. * just came from __alloc_skb().
  509. */
  510. bool skb_recycle_check(struct sk_buff *skb, int skb_size)
  511. {
  512. if (!skb_is_recycleable(skb, skb_size))
  513. return false;
  514. skb_recycle(skb);
  515. return true;
  516. }
  517. EXPORT_SYMBOL(skb_recycle_check);
  518. static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
  519. {
  520. new->tstamp = old->tstamp;
  521. new->dev = old->dev;
  522. new->transport_header = old->transport_header;
  523. new->network_header = old->network_header;
  524. new->mac_header = old->mac_header;
  525. skb_dst_copy(new, old);
  526. new->rxhash = old->rxhash;
  527. new->ooo_okay = old->ooo_okay;
  528. new->l4_rxhash = old->l4_rxhash;
  529. new->no_fcs = old->no_fcs;
  530. #ifdef CONFIG_XFRM
  531. new->sp = secpath_get(old->sp);
  532. #endif
  533. memcpy(new->cb, old->cb, sizeof(old->cb));
  534. new->csum = old->csum;
  535. new->local_df = old->local_df;
  536. new->pkt_type = old->pkt_type;
  537. new->ip_summed = old->ip_summed;
  538. skb_copy_queue_mapping(new, old);
  539. new->priority = old->priority;
  540. #if IS_ENABLED(CONFIG_IP_VS)
  541. new->ipvs_property = old->ipvs_property;
  542. #endif
  543. new->protocol = old->protocol;
  544. new->mark = old->mark;
  545. new->skb_iif = old->skb_iif;
  546. __nf_copy(new, old);
  547. #if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE)
  548. new->nf_trace = old->nf_trace;
  549. #endif
  550. #ifdef CONFIG_NET_SCHED
  551. new->tc_index = old->tc_index;
  552. #ifdef CONFIG_NET_CLS_ACT
  553. new->tc_verd = old->tc_verd;
  554. #endif
  555. #endif
  556. new->vlan_tci = old->vlan_tci;
  557. skb_copy_secmark(new, old);
  558. }
  559. /*
  560. * You should not add any new code to this function. Add it to
  561. * __copy_skb_header above instead.
  562. */
  563. static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
  564. {
  565. #define C(x) n->x = skb->x
  566. n->next = n->prev = NULL;
  567. n->sk = NULL;
  568. __copy_skb_header(n, skb);
  569. C(len);
  570. C(data_len);
  571. C(mac_len);
  572. n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
  573. n->cloned = 1;
  574. n->nohdr = 0;
  575. n->destructor = NULL;
  576. C(tail);
  577. C(end);
  578. C(head);
  579. C(data);
  580. C(truesize);
  581. atomic_set(&n->users, 1);
  582. atomic_inc(&(skb_shinfo(skb)->dataref));
  583. skb->cloned = 1;
  584. #ifdef CONFIG_IPV6_NDISC_NODETYPE
  585. C(ndisc_nodetype);
  586. #endif
  587. return n;
  588. #undef C
  589. }
  590. /**
  591. * skb_morph - morph one skb into another
  592. * @dst: the skb to receive the contents
  593. * @src: the skb to supply the contents
  594. *
  595. * This is identical to skb_clone except that the target skb is
  596. * supplied by the user.
  597. *
  598. * The target skb is returned upon exit.
  599. */
  600. struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
  601. {
  602. skb_release_all(dst);
  603. return __skb_clone(dst, src);
  604. }
  605. EXPORT_SYMBOL_GPL(skb_morph);
  606. /* skb_copy_ubufs - copy userspace skb frags buffers to kernel
  607. * @skb: the skb to modify
  608. * @gfp_mask: allocation priority
  609. *
  610. * This must be called on SKBTX_DEV_ZEROCOPY skb.
  611. * It will copy all frags into kernel and drop the reference
  612. * to userspace pages.
  613. *
  614. * If this function is called from an interrupt gfp_mask() must be
  615. * %GFP_ATOMIC.
  616. *
  617. * Returns 0 on success or a negative error code on failure
  618. * to allocate kernel memory to copy to.
  619. */
  620. int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
  621. {
  622. int i;
  623. int num_frags = skb_shinfo(skb)->nr_frags;
  624. struct page *page, *head = NULL;
  625. struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg;
  626. for (i = 0; i < num_frags; i++) {
  627. u8 *vaddr;
  628. skb_frag_t *f = &skb_shinfo(skb)->frags[i];
  629. page = alloc_page(GFP_ATOMIC);
  630. if (!page) {
  631. while (head) {
  632. struct page *next = (struct page *)head->private;
  633. put_page(head);
  634. head = next;
  635. }
  636. return -ENOMEM;
  637. }
  638. vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
  639. memcpy(page_address(page),
  640. vaddr + f->page_offset, skb_frag_size(f));
  641. kunmap_skb_frag(vaddr);
  642. page->private = (unsigned long)head;
  643. head = page;
  644. }
  645. /* skb frags release userspace buffers */
  646. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
  647. skb_frag_unref(skb, i);
  648. uarg->callback(uarg);
  649. /* skb frags point to kernel buffers */
  650. for (i = skb_shinfo(skb)->nr_frags; i > 0; i--) {
  651. __skb_fill_page_desc(skb, i-1, head, 0,
  652. skb_shinfo(skb)->frags[i - 1].size);
  653. head = (struct page *)head->private;
  654. }
  655. skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
  656. return 0;
  657. }
  658. EXPORT_SYMBOL_GPL(skb_copy_ubufs);
  659. /**
  660. * skb_clone - duplicate an sk_buff
  661. * @skb: buffer to clone
  662. * @gfp_mask: allocation priority
  663. *
  664. * Duplicate an &sk_buff. The new one is not owned by a socket. Both
  665. * copies share the same packet data but not structure. The new
  666. * buffer has a reference count of 1. If the allocation fails the
  667. * function returns %NULL otherwise the new buffer is returned.
  668. *
  669. * If this function is called from an interrupt gfp_mask() must be
  670. * %GFP_ATOMIC.
  671. */
  672. struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
  673. {
  674. struct sk_buff *n;
  675. if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
  676. if (skb_copy_ubufs(skb, gfp_mask))
  677. return NULL;
  678. }
  679. n = skb + 1;
  680. if (skb->fclone == SKB_FCLONE_ORIG &&
  681. n->fclone == SKB_FCLONE_UNAVAILABLE) {
  682. atomic_t *fclone_ref = (atomic_t *) (n + 1);
  683. n->fclone = SKB_FCLONE_CLONE;
  684. atomic_inc(fclone_ref);
  685. } else {
  686. n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
  687. if (!n)
  688. return NULL;
  689. kmemcheck_annotate_bitfield(n, flags1);
  690. kmemcheck_annotate_bitfield(n, flags2);
  691. n->fclone = SKB_FCLONE_UNAVAILABLE;
  692. }
  693. return __skb_clone(n, skb);
  694. }
  695. EXPORT_SYMBOL(skb_clone);
  696. static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
  697. {
  698. #ifndef NET_SKBUFF_DATA_USES_OFFSET
  699. /*
  700. * Shift between the two data areas in bytes
  701. */
  702. unsigned long offset = new->data - old->data;
  703. #endif
  704. __copy_skb_header(new, old);
  705. #ifndef NET_SKBUFF_DATA_USES_OFFSET
  706. /* {transport,network,mac}_header are relative to skb->head */
  707. new->transport_header += offset;
  708. new->network_header += offset;
  709. if (skb_mac_header_was_set(new))
  710. new->mac_header += offset;
  711. #endif
  712. skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
  713. skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
  714. skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
  715. }
  716. /**
  717. * skb_copy - create private copy of an sk_buff
  718. * @skb: buffer to copy
  719. * @gfp_mask: allocation priority
  720. *
  721. * Make a copy of both an &sk_buff and its data. This is used when the
  722. * caller wishes to modify the data and needs a private copy of the
  723. * data to alter. Returns %NULL on failure or the pointer to the buffer
  724. * on success. The returned buffer has a reference count of 1.
  725. *
  726. * As by-product this function converts non-linear &sk_buff to linear
  727. * one, so that &sk_buff becomes completely private and caller is allowed
  728. * to modify all the data of returned buffer. This means that this
  729. * function is not recommended for use in circumstances when only
  730. * header is going to be modified. Use pskb_copy() instead.
  731. */
  732. struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
  733. {
  734. int headerlen = skb_headroom(skb);
  735. unsigned int size = skb_end_offset(skb) + skb->data_len;
  736. struct sk_buff *n = alloc_skb(size, gfp_mask);
  737. if (!n)
  738. return NULL;
  739. /* Set the data pointer */
  740. skb_reserve(n, headerlen);
  741. /* Set the tail pointer and length */
  742. skb_put(n, skb->len);
  743. if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
  744. BUG();
  745. copy_skb_header(n, skb);
  746. return n;
  747. }
  748. EXPORT_SYMBOL(skb_copy);
  749. /**
  750. * __pskb_copy - create copy of an sk_buff with private head.
  751. * @skb: buffer to copy
  752. * @headroom: headroom of new skb
  753. * @gfp_mask: allocation priority
  754. *
  755. * Make a copy of both an &sk_buff and part of its data, located
  756. * in header. Fragmented data remain shared. This is used when
  757. * the caller wishes to modify only header of &sk_buff and needs
  758. * private copy of the header to alter. Returns %NULL on failure
  759. * or the pointer to the buffer on success.
  760. * The returned buffer has a reference count of 1.
  761. */
  762. struct sk_buff *__pskb_copy(struct sk_buff *skb, int headroom, gfp_t gfp_mask)
  763. {
  764. unsigned int size = skb_headlen(skb) + headroom;
  765. struct sk_buff *n = alloc_skb(size, gfp_mask);
  766. if (!n)
  767. goto out;
  768. /* Set the data pointer */
  769. skb_reserve(n, headroom);
  770. /* Set the tail pointer and length */
  771. skb_put(n, skb_headlen(skb));
  772. /* Copy the bytes */
  773. skb_copy_from_linear_data(skb, n->data, n->len);
  774. n->truesize += skb->data_len;
  775. n->data_len = skb->data_len;
  776. n->len = skb->len;
  777. if (skb_shinfo(skb)->nr_frags) {
  778. int i;
  779. if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
  780. if (skb_copy_ubufs(skb, gfp_mask)) {
  781. kfree_skb(n);
  782. n = NULL;
  783. goto out;
  784. }
  785. }
  786. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  787. skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
  788. skb_frag_ref(skb, i);
  789. }
  790. skb_shinfo(n)->nr_frags = i;
  791. }
  792. if (skb_has_frag_list(skb)) {
  793. skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
  794. skb_clone_fraglist(n);
  795. }
  796. copy_skb_header(n, skb);
  797. out:
  798. return n;
  799. }
  800. EXPORT_SYMBOL(__pskb_copy);
  801. /**
  802. * pskb_expand_head - reallocate header of &sk_buff
  803. * @skb: buffer to reallocate
  804. * @nhead: room to add at head
  805. * @ntail: room to add at tail
  806. * @gfp_mask: allocation priority
  807. *
  808. * Expands (or creates identical copy, if &nhead and &ntail are zero)
  809. * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
  810. * reference count of 1. Returns zero in the case of success or error,
  811. * if expansion failed. In the last case, &sk_buff is not changed.
  812. *
  813. * All the pointers pointing into skb header may change and must be
  814. * reloaded after call to this function.
  815. */
  816. int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
  817. gfp_t gfp_mask)
  818. {
  819. int i;
  820. u8 *data;
  821. int size = nhead + skb_end_offset(skb) + ntail;
  822. long off;
  823. bool fastpath;
  824. BUG_ON(nhead < 0);
  825. if (skb_shared(skb))
  826. BUG();
  827. size = SKB_DATA_ALIGN(size);
  828. /* Check if we can avoid taking references on fragments if we own
  829. * the last reference on skb->head. (see skb_release_data())
  830. */
  831. if (!skb->cloned)
  832. fastpath = true;
  833. else {
  834. int delta = skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1;
  835. fastpath = atomic_read(&skb_shinfo(skb)->dataref) == delta;
  836. }
  837. if (fastpath &&
  838. size + sizeof(struct skb_shared_info) <= ksize(skb->head)) {
  839. memmove(skb->head + size, skb_shinfo(skb),
  840. offsetof(struct skb_shared_info,
  841. frags[skb_shinfo(skb)->nr_frags]));
  842. memmove(skb->head + nhead, skb->head,
  843. skb_tail_pointer(skb) - skb->head);
  844. off = nhead;
  845. goto adjust_others;
  846. }
  847. data = kmalloc(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
  848. gfp_mask);
  849. if (!data)
  850. goto nodata;
  851. size = SKB_WITH_OVERHEAD(ksize(data));
  852. /* Copy only real data... and, alas, header. This should be
  853. * optimized for the cases when header is void.
  854. */
  855. memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
  856. memcpy((struct skb_shared_info *)(data + size),
  857. skb_shinfo(skb),
  858. offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
  859. if (fastpath) {
  860. kfree(skb->head);
  861. } else {
  862. /* copy this zero copy skb frags */
  863. if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
  864. if (skb_copy_ubufs(skb, gfp_mask))
  865. goto nofrags;
  866. }
  867. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
  868. skb_frag_ref(skb, i);
  869. if (skb_has_frag_list(skb))
  870. skb_clone_fraglist(skb);
  871. skb_release_data(skb);
  872. }
  873. off = (data + nhead) - skb->head;
  874. skb->head = data;
  875. adjust_others:
  876. skb->data += off;
  877. #ifdef NET_SKBUFF_DATA_USES_OFFSET
  878. skb->end = size;
  879. off = nhead;
  880. #else
  881. skb->end = skb->head + size;
  882. #endif
  883. /* {transport,network,mac}_header and tail are relative to skb->head */
  884. skb->tail += off;
  885. skb->transport_header += off;
  886. skb->network_header += off;
  887. if (skb_mac_header_was_set(skb))
  888. skb->mac_header += off;
  889. /* Only adjust this if it actually is csum_start rather than csum */
  890. if (skb->ip_summed == CHECKSUM_PARTIAL)
  891. skb->csum_start += nhead;
  892. skb->cloned = 0;
  893. skb->hdr_len = 0;
  894. skb->nohdr = 0;
  895. atomic_set(&skb_shinfo(skb)->dataref, 1);
  896. return 0;
  897. nofrags:
  898. kfree(data);
  899. nodata:
  900. return -ENOMEM;
  901. }
  902. EXPORT_SYMBOL(pskb_expand_head);
  903. /* Make private copy of skb with writable head and some headroom */
  904. struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
  905. {
  906. struct sk_buff *skb2;
  907. int delta = headroom - skb_headroom(skb);
  908. if (delta <= 0)
  909. skb2 = pskb_copy(skb, GFP_ATOMIC);
  910. else {
  911. skb2 = skb_clone(skb, GFP_ATOMIC);
  912. if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
  913. GFP_ATOMIC)) {
  914. kfree_skb(skb2);
  915. skb2 = NULL;
  916. }
  917. }
  918. return skb2;
  919. }
  920. EXPORT_SYMBOL(skb_realloc_headroom);
  921. /**
  922. * skb_copy_expand - copy and expand sk_buff
  923. * @skb: buffer to copy
  924. * @newheadroom: new free bytes at head
  925. * @newtailroom: new free bytes at tail
  926. * @gfp_mask: allocation priority
  927. *
  928. * Make a copy of both an &sk_buff and its data and while doing so
  929. * allocate additional space.
  930. *
  931. * This is used when the caller wishes to modify the data and needs a
  932. * private copy of the data to alter as well as more space for new fields.
  933. * Returns %NULL on failure or the pointer to the buffer
  934. * on success. The returned buffer has a reference count of 1.
  935. *
  936. * You must pass %GFP_ATOMIC as the allocation priority if this function
  937. * is called from an interrupt.
  938. */
  939. struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
  940. int newheadroom, int newtailroom,
  941. gfp_t gfp_mask)
  942. {
  943. /*
  944. * Allocate the copy buffer
  945. */
  946. struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
  947. gfp_mask);
  948. int oldheadroom = skb_headroom(skb);
  949. int head_copy_len, head_copy_off;
  950. int off;
  951. if (!n)
  952. return NULL;
  953. skb_reserve(n, newheadroom);
  954. /* Set the tail pointer and length */
  955. skb_put(n, skb->len);
  956. head_copy_len = oldheadroom;
  957. head_copy_off = 0;
  958. if (newheadroom <= head_copy_len)
  959. head_copy_len = newheadroom;
  960. else
  961. head_copy_off = newheadroom - head_copy_len;
  962. /* Copy the linear header and data. */
  963. if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
  964. skb->len + head_copy_len))
  965. BUG();
  966. copy_skb_header(n, skb);
  967. off = newheadroom - oldheadroom;
  968. if (n->ip_summed == CHECKSUM_PARTIAL)
  969. n->csum_start += off;
  970. #ifdef NET_SKBUFF_DATA_USES_OFFSET
  971. n->transport_header += off;
  972. n->network_header += off;
  973. if (skb_mac_header_was_set(skb))
  974. n->mac_header += off;
  975. #endif
  976. return n;
  977. }
  978. EXPORT_SYMBOL(skb_copy_expand);
  979. /**
  980. * skb_pad - zero pad the tail of an skb
  981. * @skb: buffer to pad
  982. * @pad: space to pad
  983. *
  984. * Ensure that a buffer is followed by a padding area that is zero
  985. * filled. Used by network drivers which may DMA or transfer data
  986. * beyond the buffer end onto the wire.
  987. *
  988. * May return error in out of memory cases. The skb is freed on error.
  989. */
  990. int skb_pad(struct sk_buff *skb, int pad)
  991. {
  992. int err;
  993. int ntail;
  994. /* If the skbuff is non linear tailroom is always zero.. */
  995. if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
  996. memset(skb->data+skb->len, 0, pad);
  997. return 0;
  998. }
  999. ntail = skb->data_len + pad - (skb->end - skb->tail);
  1000. if (likely(skb_cloned(skb) || ntail > 0)) {
  1001. err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
  1002. if (unlikely(err))
  1003. goto free_skb;
  1004. }
  1005. /* FIXME: The use of this function with non-linear skb's really needs
  1006. * to be audited.
  1007. */
  1008. err = skb_linearize(skb);
  1009. if (unlikely(err))
  1010. goto free_skb;
  1011. memset(skb->data + skb->len, 0, pad);
  1012. return 0;
  1013. free_skb:
  1014. kfree_skb(skb);
  1015. return err;
  1016. }
  1017. EXPORT_SYMBOL(skb_pad);
  1018. /**
  1019. * skb_put - add data to a buffer
  1020. * @skb: buffer to use
  1021. * @len: amount of data to add
  1022. *
  1023. * This function extends the used data area of the buffer. If this would
  1024. * exceed the total buffer size the kernel will panic. A pointer to the
  1025. * first byte of the extra data is returned.
  1026. */
  1027. unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
  1028. {
  1029. unsigned char *tmp = skb_tail_pointer(skb);
  1030. SKB_LINEAR_ASSERT(skb);
  1031. skb->tail += len;
  1032. skb->len += len;
  1033. if (unlikely(skb->tail > skb->end))
  1034. skb_over_panic(skb, len, __builtin_return_address(0));
  1035. return tmp;
  1036. }
  1037. EXPORT_SYMBOL(skb_put);
  1038. /**
  1039. * skb_push - add data to the start of a buffer
  1040. * @skb: buffer to use
  1041. * @len: amount of data to add
  1042. *
  1043. * This function extends the used data area of the buffer at the buffer
  1044. * start. If this would exceed the total buffer headroom the kernel will
  1045. * panic. A pointer to the first byte of the extra data is returned.
  1046. */
  1047. unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
  1048. {
  1049. skb->data -= len;
  1050. skb->len += len;
  1051. if (unlikely(skb->data<skb->head))
  1052. skb_under_panic(skb, len, __builtin_return_address(0));
  1053. return skb->data;
  1054. }
  1055. EXPORT_SYMBOL(skb_push);
  1056. /**
  1057. * skb_pull - remove data from the start of a buffer
  1058. * @skb: buffer to use
  1059. * @len: amount of data to remove
  1060. *
  1061. * This function removes data from the start of a buffer, returning
  1062. * the memory to the headroom. A pointer to the next data in the buffer
  1063. * is returned. Once the data has been pulled future pushes will overwrite
  1064. * the old data.
  1065. */
  1066. unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
  1067. {
  1068. return skb_pull_inline(skb, len);
  1069. }
  1070. EXPORT_SYMBOL(skb_pull);
  1071. /**
  1072. * skb_trim - remove end from a buffer
  1073. * @skb: buffer to alter
  1074. * @len: new length
  1075. *
  1076. * Cut the length of a buffer down by removing data from the tail. If
  1077. * the buffer is already under the length specified it is not modified.
  1078. * The skb must be linear.
  1079. */
  1080. void skb_trim(struct sk_buff *skb, unsigned int len)
  1081. {
  1082. if (skb->len > len)
  1083. __skb_trim(skb, len);
  1084. }
  1085. EXPORT_SYMBOL(skb_trim);
  1086. /* Trims skb to length len. It can change skb pointers.
  1087. */
  1088. int ___pskb_trim(struct sk_buff *skb, unsigned int len)
  1089. {
  1090. struct sk_buff **fragp;
  1091. struct sk_buff *frag;
  1092. int offset = skb_headlen(skb);
  1093. int nfrags = skb_shinfo(skb)->nr_frags;
  1094. int i;
  1095. int err;
  1096. if (skb_cloned(skb) &&
  1097. unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
  1098. return err;
  1099. i = 0;
  1100. if (offset >= len)
  1101. goto drop_pages;
  1102. for (; i < nfrags; i++) {
  1103. int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
  1104. if (end < len) {
  1105. offset = end;
  1106. continue;
  1107. }
  1108. skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
  1109. drop_pages:
  1110. skb_shinfo(skb)->nr_frags = i;
  1111. for (; i < nfrags; i++)
  1112. skb_frag_unref(skb, i);
  1113. if (skb_has_frag_list(skb))
  1114. skb_drop_fraglist(skb);
  1115. goto done;
  1116. }
  1117. for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
  1118. fragp = &frag->next) {
  1119. int end = offset + frag->len;
  1120. if (skb_shared(frag)) {
  1121. struct sk_buff *nfrag;
  1122. nfrag = skb_clone(frag, GFP_ATOMIC);
  1123. if (unlikely(!nfrag))
  1124. return -ENOMEM;
  1125. nfrag->next = frag->next;
  1126. kfree_skb(frag);
  1127. frag = nfrag;
  1128. *fragp = frag;
  1129. }
  1130. if (end < len) {
  1131. offset = end;
  1132. continue;
  1133. }
  1134. if (end > len &&
  1135. unlikely((err = pskb_trim(frag, len - offset))))
  1136. return err;
  1137. if (frag->next)
  1138. skb_drop_list(&frag->next);
  1139. break;
  1140. }
  1141. done:
  1142. if (len > skb_headlen(skb)) {
  1143. skb->data_len -= skb->len - len;
  1144. skb->len = len;
  1145. } else {
  1146. skb->len = len;
  1147. skb->data_len = 0;
  1148. skb_set_tail_pointer(skb, len);
  1149. }
  1150. return 0;
  1151. }
  1152. EXPORT_SYMBOL(___pskb_trim);
  1153. /**
  1154. * __pskb_pull_tail - advance tail of skb header
  1155. * @skb: buffer to reallocate
  1156. * @delta: number of bytes to advance tail
  1157. *
  1158. * The function makes a sense only on a fragmented &sk_buff,
  1159. * it expands header moving its tail forward and copying necessary
  1160. * data from fragmented part.
  1161. *
  1162. * &sk_buff MUST have reference count of 1.
  1163. *
  1164. * Returns %NULL (and &sk_buff does not change) if pull failed
  1165. * or value of new tail of skb in the case of success.
  1166. *
  1167. * All the pointers pointing into skb header may change and must be
  1168. * reloaded after call to this function.
  1169. */
  1170. /* Moves tail of skb head forward, copying data from fragmented part,
  1171. * when it is necessary.
  1172. * 1. It may fail due to malloc failure.
  1173. * 2. It may change skb pointers.
  1174. *
  1175. * It is pretty complicated. Luckily, it is called only in exceptional cases.
  1176. */
  1177. unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
  1178. {
  1179. /* If skb has not enough free space at tail, get new one
  1180. * plus 128 bytes for future expansions. If we have enough
  1181. * room at tail, reallocate without expansion only if skb is cloned.
  1182. */
  1183. int i, k, eat = (skb->tail + delta) - skb->end;
  1184. if (eat > 0 || skb_cloned(skb)) {
  1185. if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
  1186. GFP_ATOMIC))
  1187. return NULL;
  1188. }
  1189. if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
  1190. BUG();
  1191. /* Optimization: no fragments, no reasons to preestimate
  1192. * size of pulled pages. Superb.
  1193. */
  1194. if (!skb_has_frag_list(skb))
  1195. goto pull_pages;
  1196. /* Estimate size of pulled pages. */
  1197. eat = delta;
  1198. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1199. int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
  1200. if (size >= eat)
  1201. goto pull_pages;
  1202. eat -= size;
  1203. }
  1204. /* If we need update frag list, we are in troubles.
  1205. * Certainly, it possible to add an offset to skb data,
  1206. * but taking into account that pulling is expected to
  1207. * be very rare operation, it is worth to fight against
  1208. * further bloating skb head and crucify ourselves here instead.
  1209. * Pure masohism, indeed. 8)8)
  1210. */
  1211. if (eat) {
  1212. struct sk_buff *list = skb_shinfo(skb)->frag_list;
  1213. struct sk_buff *clone = NULL;
  1214. struct sk_buff *insp = NULL;
  1215. do {
  1216. BUG_ON(!list);
  1217. if (list->len <= eat) {
  1218. /* Eaten as whole. */
  1219. eat -= list->len;
  1220. list = list->next;
  1221. insp = list;
  1222. } else {
  1223. /* Eaten partially. */
  1224. if (skb_shared(list)) {
  1225. /* Sucks! We need to fork list. :-( */
  1226. clone = skb_clone(list, GFP_ATOMIC);
  1227. if (!clone)
  1228. return NULL;
  1229. insp = list->next;
  1230. list = clone;
  1231. } else {
  1232. /* This may be pulled without
  1233. * problems. */
  1234. insp = list;
  1235. }
  1236. if (!pskb_pull(list, eat)) {
  1237. kfree_skb(clone);
  1238. return NULL;
  1239. }
  1240. break;
  1241. }
  1242. } while (eat);
  1243. /* Free pulled out fragments. */
  1244. while ((list = skb_shinfo(skb)->frag_list) != insp) {
  1245. skb_shinfo(skb)->frag_list = list->next;
  1246. kfree_skb(list);
  1247. }
  1248. /* And insert new clone at head. */
  1249. if (clone) {
  1250. clone->next = list;
  1251. skb_shinfo(skb)->frag_list = clone;
  1252. }
  1253. }
  1254. /* Success! Now we may commit changes to skb data. */
  1255. pull_pages:
  1256. eat = delta;
  1257. k = 0;
  1258. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1259. int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
  1260. if (size <= eat) {
  1261. skb_frag_unref(skb, i);
  1262. eat -= size;
  1263. } else {
  1264. skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
  1265. if (eat) {
  1266. skb_shinfo(skb)->frags[k].page_offset += eat;
  1267. skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
  1268. eat = 0;
  1269. }
  1270. k++;
  1271. }
  1272. }
  1273. skb_shinfo(skb)->nr_frags = k;
  1274. skb->tail += delta;
  1275. skb->data_len -= delta;
  1276. return skb_tail_pointer(skb);
  1277. }
  1278. EXPORT_SYMBOL(__pskb_pull_tail);
  1279. /**
  1280. * skb_copy_bits - copy bits from skb to kernel buffer
  1281. * @skb: source skb
  1282. * @offset: offset in source
  1283. * @to: destination buffer
  1284. * @len: number of bytes to copy
  1285. *
  1286. * Copy the specified number of bytes from the source skb to the
  1287. * destination buffer.
  1288. *
  1289. * CAUTION ! :
  1290. * If its prototype is ever changed,
  1291. * check arch/{*}/net/{*}.S files,
  1292. * since it is called from BPF assembly code.
  1293. */
  1294. int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
  1295. {
  1296. int start = skb_headlen(skb);
  1297. struct sk_buff *frag_iter;
  1298. int i, copy;
  1299. if (offset > (int)skb->len - len)
  1300. goto fault;
  1301. /* Copy header. */
  1302. if ((copy = start - offset) > 0) {
  1303. if (copy > len)
  1304. copy = len;
  1305. skb_copy_from_linear_data_offset(skb, offset, to, copy);
  1306. if ((len -= copy) == 0)
  1307. return 0;
  1308. offset += copy;
  1309. to += copy;
  1310. }
  1311. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1312. int end;
  1313. WARN_ON(start > offset + len);
  1314. end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
  1315. if ((copy = end - offset) > 0) {
  1316. u8 *vaddr;
  1317. if (copy > len)
  1318. copy = len;
  1319. vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
  1320. memcpy(to,
  1321. vaddr + skb_shinfo(skb)->frags[i].page_offset+
  1322. offset - start, copy);
  1323. kunmap_skb_frag(vaddr);
  1324. if ((len -= copy) == 0)
  1325. return 0;
  1326. offset += copy;
  1327. to += copy;
  1328. }
  1329. start = end;
  1330. }
  1331. skb_walk_frags(skb, frag_iter) {
  1332. int end;
  1333. WARN_ON(start > offset + len);
  1334. end = start + frag_iter->len;
  1335. if ((copy = end - offset) > 0) {
  1336. if (copy > len)
  1337. copy = len;
  1338. if (skb_copy_bits(frag_iter, offset - start, to, copy))
  1339. goto fault;
  1340. if ((len -= copy) == 0)
  1341. return 0;
  1342. offset += copy;
  1343. to += copy;
  1344. }
  1345. start = end;
  1346. }
  1347. if (!len)
  1348. return 0;
  1349. fault:
  1350. return -EFAULT;
  1351. }
  1352. EXPORT_SYMBOL(skb_copy_bits);
  1353. /*
  1354. * Callback from splice_to_pipe(), if we need to release some pages
  1355. * at the end of the spd in case we error'ed out in filling the pipe.
  1356. */
  1357. static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
  1358. {
  1359. put_page(spd->pages[i]);
  1360. }
  1361. static inline struct page *linear_to_page(struct page *page, unsigned int *len,
  1362. unsigned int *offset,
  1363. struct sk_buff *skb, struct sock *sk)
  1364. {
  1365. struct page *p = sk->sk_sndmsg_page;
  1366. unsigned int off;
  1367. if (!p) {
  1368. new_page:
  1369. p = sk->sk_sndmsg_page = alloc_pages(sk->sk_allocation, 0);
  1370. if (!p)
  1371. return NULL;
  1372. off = sk->sk_sndmsg_off = 0;
  1373. /* hold one ref to this page until it's full */
  1374. } else {
  1375. unsigned int mlen;
  1376. off = sk->sk_sndmsg_off;
  1377. mlen = PAGE_SIZE - off;
  1378. if (mlen < 64 && mlen < *len) {
  1379. put_page(p);
  1380. goto new_page;
  1381. }
  1382. *len = min_t(unsigned int, *len, mlen);
  1383. }
  1384. memcpy(page_address(p) + off, page_address(page) + *offset, *len);
  1385. sk->sk_sndmsg_off += *len;
  1386. *offset = off;
  1387. get_page(p);
  1388. return p;
  1389. }
  1390. /*
  1391. * Fill page/offset/length into spd, if it can hold more pages.
  1392. */
  1393. static inline int spd_fill_page(struct splice_pipe_desc *spd,
  1394. struct pipe_inode_info *pipe, struct page *page,
  1395. unsigned int *len, unsigned int offset,
  1396. struct sk_buff *skb, int linear,
  1397. struct sock *sk)
  1398. {
  1399. if (unlikely(spd->nr_pages == pipe->buffers))
  1400. return 1;
  1401. if (linear) {
  1402. page = linear_to_page(page, len, &offset, skb, sk);
  1403. if (!page)
  1404. return 1;
  1405. } else
  1406. get_page(page);
  1407. spd->pages[spd->nr_pages] = page;
  1408. spd->partial[spd->nr_pages].len = *len;
  1409. spd->partial[spd->nr_pages].offset = offset;
  1410. spd->nr_pages++;
  1411. return 0;
  1412. }
  1413. static inline void __segment_seek(struct page **page, unsigned int *poff,
  1414. unsigned int *plen, unsigned int off)
  1415. {
  1416. unsigned long n;
  1417. *poff += off;
  1418. n = *poff / PAGE_SIZE;
  1419. if (n)
  1420. *page = nth_page(*page, n);
  1421. *poff = *poff % PAGE_SIZE;
  1422. *plen -= off;
  1423. }
  1424. static inline int __splice_segment(struct page *page, unsigned int poff,
  1425. unsigned int plen, unsigned int *off,
  1426. unsigned int *len, struct sk_buff *skb,
  1427. struct splice_pipe_desc *spd, int linear,
  1428. struct sock *sk,
  1429. struct pipe_inode_info *pipe)
  1430. {
  1431. if (!*len)
  1432. return 1;
  1433. /* skip this segment if already processed */
  1434. if (*off >= plen) {
  1435. *off -= plen;
  1436. return 0;
  1437. }
  1438. /* ignore any bits we already processed */
  1439. if (*off) {
  1440. __segment_seek(&page, &poff, &plen, *off);
  1441. *off = 0;
  1442. }
  1443. do {
  1444. unsigned int flen = min(*len, plen);
  1445. /* the linear region may spread across several pages */
  1446. flen = min_t(unsigned int, flen, PAGE_SIZE - poff);
  1447. if (spd_fill_page(spd, pipe, page, &flen, poff, skb, linear, sk))
  1448. return 1;
  1449. __segment_seek(&page, &poff, &plen, flen);
  1450. *len -= flen;
  1451. } while (*len && plen);
  1452. return 0;
  1453. }
  1454. /*
  1455. * Map linear and fragment data from the skb to spd. It reports failure if the
  1456. * pipe is full or if we already spliced the requested length.
  1457. */
  1458. static int __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
  1459. unsigned int *offset, unsigned int *len,
  1460. struct splice_pipe_desc *spd, struct sock *sk)
  1461. {
  1462. int seg;
  1463. /*
  1464. * map the linear part
  1465. */
  1466. if (__splice_segment(virt_to_page(skb->data),
  1467. (unsigned long) skb->data & (PAGE_SIZE - 1),
  1468. skb_headlen(skb),
  1469. offset, len, skb, spd, 1, sk, pipe))
  1470. return 1;
  1471. /*
  1472. * then map the fragments
  1473. */
  1474. for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
  1475. const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
  1476. if (__splice_segment(skb_frag_page(f),
  1477. f->page_offset, skb_frag_size(f),
  1478. offset, len, skb, spd, 0, sk, pipe))
  1479. return 1;
  1480. }
  1481. return 0;
  1482. }
  1483. /*
  1484. * Map data from the skb to a pipe. Should handle both the linear part,
  1485. * the fragments, and the frag list. It does NOT handle frag lists within
  1486. * the frag list, if such a thing exists. We'd probably need to recurse to
  1487. * handle that cleanly.
  1488. */
  1489. int skb_splice_bits(struct sk_buff *skb, unsigned int offset,
  1490. struct pipe_inode_info *pipe, unsigned int tlen,
  1491. unsigned int flags)
  1492. {
  1493. struct partial_page partial[PIPE_DEF_BUFFERS];
  1494. struct page *pages[PIPE_DEF_BUFFERS];
  1495. struct splice_pipe_desc spd = {
  1496. .pages = pages,
  1497. .partial = partial,
  1498. .nr_pages_max = MAX_SKB_FRAGS,
  1499. .flags = flags,
  1500. .ops = &sock_pipe_buf_ops,
  1501. .spd_release = sock_spd_release,
  1502. };
  1503. struct sk_buff *frag_iter;
  1504. struct sock *sk = skb->sk;
  1505. int ret = 0;
  1506. if (splice_grow_spd(pipe, &spd))
  1507. return -ENOMEM;
  1508. /*
  1509. * __skb_splice_bits() only fails if the output has no room left,
  1510. * so no point in going over the frag_list for the error case.
  1511. */
  1512. if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk))
  1513. goto done;
  1514. else if (!tlen)
  1515. goto done;
  1516. /*
  1517. * now see if we have a frag_list to map
  1518. */
  1519. skb_walk_frags(skb, frag_iter) {
  1520. if (!tlen)
  1521. break;
  1522. if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk))
  1523. break;
  1524. }
  1525. done:
  1526. if (spd.nr_pages) {
  1527. /*
  1528. * Drop the socket lock, otherwise we have reverse
  1529. * locking dependencies between sk_lock and i_mutex
  1530. * here as compared to sendfile(). We enter here
  1531. * with the socket lock held, and splice_to_pipe() will
  1532. * grab the pipe inode lock. For sendfile() emulation,
  1533. * we call into ->sendpage() with the i_mutex lock held
  1534. * and networking will grab the socket lock.
  1535. */
  1536. release_sock(sk);
  1537. ret = splice_to_pipe(pipe, &spd);
  1538. lock_sock(sk);
  1539. }
  1540. splice_shrink_spd(&spd);
  1541. return ret;
  1542. }
  1543. /**
  1544. * skb_store_bits - store bits from kernel buffer to skb
  1545. * @skb: destination buffer
  1546. * @offset: offset in destination
  1547. * @from: source buffer
  1548. * @len: number of bytes to copy
  1549. *
  1550. * Copy the specified number of bytes from the source buffer to the
  1551. * destination skb. This function handles all the messy bits of
  1552. * traversing fragment lists and such.
  1553. */
  1554. int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
  1555. {
  1556. int start = skb_headlen(skb);
  1557. struct sk_buff *frag_iter;
  1558. int i, copy;
  1559. if (offset > (int)skb->len - len)
  1560. goto fault;
  1561. if ((copy = start - offset) > 0) {
  1562. if (copy > len)
  1563. copy = len;
  1564. skb_copy_to_linear_data_offset(skb, offset, from, copy);
  1565. if ((len -= copy) == 0)
  1566. return 0;
  1567. offset += copy;
  1568. from += copy;
  1569. }
  1570. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1571. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  1572. int end;
  1573. WARN_ON(start > offset + len);
  1574. end = start + skb_frag_size(frag);
  1575. if ((copy = end - offset) > 0) {
  1576. u8 *vaddr;
  1577. if (copy > len)
  1578. copy = len;
  1579. vaddr = kmap_skb_frag(frag);
  1580. memcpy(vaddr + frag->page_offset + offset - start,
  1581. from, copy);
  1582. kunmap_skb_frag(vaddr);
  1583. if ((len -= copy) == 0)
  1584. return 0;
  1585. offset += copy;
  1586. from += copy;
  1587. }
  1588. start = end;
  1589. }
  1590. skb_walk_frags(skb, frag_iter) {
  1591. int end;
  1592. WARN_ON(start > offset + len);
  1593. end = start + frag_iter->len;
  1594. if ((copy = end - offset) > 0) {
  1595. if (copy > len)
  1596. copy = len;
  1597. if (skb_store_bits(frag_iter, offset - start,
  1598. from, copy))
  1599. goto fault;
  1600. if ((len -= copy) == 0)
  1601. return 0;
  1602. offset += copy;
  1603. from += copy;
  1604. }
  1605. start = end;
  1606. }
  1607. if (!len)
  1608. return 0;
  1609. fault:
  1610. return -EFAULT;
  1611. }
  1612. EXPORT_SYMBOL(skb_store_bits);
  1613. /* Checksum skb data. */
  1614. __wsum skb_checksum(const struct sk_buff *skb, int offset,
  1615. int len, __wsum csum)
  1616. {
  1617. int start = skb_headlen(skb);
  1618. int i, copy = start - offset;
  1619. struct sk_buff *frag_iter;
  1620. int pos = 0;
  1621. /* Checksum header. */
  1622. if (copy > 0) {
  1623. if (copy > len)
  1624. copy = len;
  1625. csum = csum_partial(skb->data + offset, copy, csum);
  1626. if ((len -= copy) == 0)
  1627. return csum;
  1628. offset += copy;
  1629. pos = copy;
  1630. }
  1631. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1632. int end;
  1633. WARN_ON(start > offset + len);
  1634. end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
  1635. if ((copy = end - offset) > 0) {
  1636. __wsum csum2;
  1637. u8 *vaddr;
  1638. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  1639. if (copy > len)
  1640. copy = len;
  1641. vaddr = kmap_skb_frag(frag);
  1642. csum2 = csum_partial(vaddr + frag->page_offset +
  1643. offset - start, copy, 0);
  1644. kunmap_skb_frag(vaddr);
  1645. csum = csum_block_add(csum, csum2, pos);
  1646. if (!(len -= copy))
  1647. return csum;
  1648. offset += copy;
  1649. pos += copy;
  1650. }
  1651. start = end;
  1652. }
  1653. skb_walk_frags(skb, frag_iter) {
  1654. int end;
  1655. WARN_ON(start > offset + len);
  1656. end = start + frag_iter->len;
  1657. if ((copy = end - offset) > 0) {
  1658. __wsum csum2;
  1659. if (copy > len)
  1660. copy = len;
  1661. csum2 = skb_checksum(frag_iter, offset - start,
  1662. copy, 0);
  1663. csum = csum_block_add(csum, csum2, pos);
  1664. if ((len -= copy) == 0)
  1665. return csum;
  1666. offset += copy;
  1667. pos += copy;
  1668. }
  1669. start = end;
  1670. }
  1671. BUG_ON(len);
  1672. return csum;
  1673. }
  1674. EXPORT_SYMBOL(skb_checksum);
  1675. /* Both of above in one bottle. */
  1676. __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
  1677. u8 *to, int len, __wsum csum)
  1678. {
  1679. int start = skb_headlen(skb);
  1680. int i, copy = start - offset;
  1681. struct sk_buff *frag_iter;
  1682. int pos = 0;
  1683. /* Copy header. */
  1684. if (copy > 0) {
  1685. if (copy > len)
  1686. copy = len;
  1687. csum = csum_partial_copy_nocheck(skb->data + offset, to,
  1688. copy, csum);
  1689. if ((len -= copy) == 0)
  1690. return csum;
  1691. offset += copy;
  1692. to += copy;
  1693. pos = copy;
  1694. }
  1695. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1696. int end;
  1697. WARN_ON(start > offset + len);
  1698. end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
  1699. if ((copy = end - offset) > 0) {
  1700. __wsum csum2;
  1701. u8 *vaddr;
  1702. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  1703. if (copy > len)
  1704. copy = len;
  1705. vaddr = kmap_skb_frag(frag);
  1706. csum2 = csum_partial_copy_nocheck(vaddr +
  1707. frag->page_offset +
  1708. offset - start, to,
  1709. copy, 0);
  1710. kunmap_skb_frag(vaddr);
  1711. csum = csum_block_add(csum, csum2, pos);
  1712. if (!(len -= copy))
  1713. return csum;
  1714. offset += copy;
  1715. to += copy;
  1716. pos += copy;
  1717. }
  1718. start = end;
  1719. }
  1720. skb_walk_frags(skb, frag_iter) {
  1721. __wsum csum2;
  1722. int end;
  1723. WARN_ON(start > offset + len);
  1724. end = start + frag_iter->len;
  1725. if ((copy = end - offset) > 0) {
  1726. if (copy > len)
  1727. copy = len;
  1728. csum2 = skb_copy_and_csum_bits(frag_iter,
  1729. offset - start,
  1730. to, copy, 0);
  1731. csum = csum_block_add(csum, csum2, pos);
  1732. if ((len -= copy) == 0)
  1733. return csum;
  1734. offset += copy;
  1735. to += copy;
  1736. pos += copy;
  1737. }
  1738. start = end;
  1739. }
  1740. BUG_ON(len);
  1741. return csum;
  1742. }
  1743. EXPORT_SYMBOL(skb_copy_and_csum_bits);
  1744. void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
  1745. {
  1746. __wsum csum;
  1747. long csstart;
  1748. if (skb->ip_summed == CHECKSUM_PARTIAL)
  1749. csstart = skb_checksum_start_offset(skb);
  1750. else
  1751. csstart = skb_headlen(skb);
  1752. BUG_ON(csstart > skb_headlen(skb));
  1753. skb_copy_from_linear_data(skb, to, csstart);
  1754. csum = 0;
  1755. if (csstart != skb->len)
  1756. csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
  1757. skb->len - csstart, 0);
  1758. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  1759. long csstuff = csstart + skb->csum_offset;
  1760. *((__sum16 *)(to + csstuff)) = csum_fold(csum);
  1761. }
  1762. }
  1763. EXPORT_SYMBOL(skb_copy_and_csum_dev);
  1764. /**
  1765. * skb_dequeue - remove from the head of the queue
  1766. * @list: list to dequeue from
  1767. *
  1768. * Remove the head of the list. The list lock is taken so the function
  1769. * may be used safely with other locking list functions. The head item is
  1770. * returned or %NULL if the list is empty.
  1771. */
  1772. struct sk_buff *skb_dequeue(struct sk_buff_head *list)
  1773. {
  1774. unsigned long flags;
  1775. struct sk_buff *result;
  1776. spin_lock_irqsave(&list->lock, flags);
  1777. result = __skb_dequeue(list);
  1778. spin_unlock_irqrestore(&list->lock, flags);
  1779. return result;
  1780. }
  1781. EXPORT_SYMBOL(skb_dequeue);
  1782. /**
  1783. * skb_dequeue_tail - remove from the tail of the queue
  1784. * @list: list to dequeue from
  1785. *
  1786. * Remove the tail of the list. The list lock is taken so the function
  1787. * may be used safely with other locking list functions. The tail item is
  1788. * returned or %NULL if the list is empty.
  1789. */
  1790. struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
  1791. {
  1792. unsigned long flags;
  1793. struct sk_buff *result;
  1794. spin_lock_irqsave(&list->lock, flags);
  1795. result = __skb_dequeue_tail(list);
  1796. spin_unlock_irqrestore(&list->lock, flags);
  1797. return result;
  1798. }
  1799. EXPORT_SYMBOL(skb_dequeue_tail);
  1800. /**
  1801. * skb_queue_purge - empty a list
  1802. * @list: list to empty
  1803. *
  1804. * Delete all buffers on an &sk_buff list. Each buffer is removed from
  1805. * the list and one reference dropped. This function takes the list
  1806. * lock and is atomic with respect to other list locking functions.
  1807. */
  1808. void skb_queue_purge(struct sk_buff_head *list)
  1809. {
  1810. struct sk_buff *skb;
  1811. while ((skb = skb_dequeue(list)) != NULL)
  1812. kfree_skb(skb);
  1813. }
  1814. EXPORT_SYMBOL(skb_queue_purge);
  1815. /**
  1816. * skb_queue_head - queue a buffer at the list head
  1817. * @list: list to use
  1818. * @newsk: buffer to queue
  1819. *
  1820. * Queue a buffer at the start of the list. This function takes the
  1821. * list lock and can be used safely with other locking &sk_buff functions
  1822. * safely.
  1823. *
  1824. * A buffer cannot be placed on two lists at the same time.
  1825. */
  1826. void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
  1827. {
  1828. unsigned long flags;
  1829. spin_lock_irqsave(&list->lock, flags);
  1830. __skb_queue_head(list, newsk);
  1831. spin_unlock_irqrestore(&list->lock, flags);
  1832. }
  1833. EXPORT_SYMBOL(skb_queue_head);
  1834. /**
  1835. * skb_queue_tail - queue a buffer at the list tail
  1836. * @list: list to use
  1837. * @newsk: buffer to queue
  1838. *
  1839. * Queue a buffer at the tail of the list. This function takes the
  1840. * list lock and can be used safely with other locking &sk_buff functions
  1841. * safely.
  1842. *
  1843. * A buffer cannot be placed on two lists at the same time.
  1844. */
  1845. void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
  1846. {
  1847. unsigned long flags;
  1848. spin_lock_irqsave(&list->lock, flags);
  1849. __skb_queue_tail(list, newsk);
  1850. spin_unlock_irqrestore(&list->lock, flags);
  1851. }
  1852. EXPORT_SYMBOL(skb_queue_tail);
  1853. /**
  1854. * skb_unlink - remove a buffer from a list
  1855. * @skb: buffer to remove
  1856. * @list: list to use
  1857. *
  1858. * Remove a packet from a list. The list locks are taken and this
  1859. * function is atomic with respect to other list locked calls
  1860. *
  1861. * You must know what list the SKB is on.
  1862. */
  1863. void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
  1864. {
  1865. unsigned long flags;
  1866. spin_lock_irqsave(&list->lock, flags);
  1867. __skb_unlink(skb, list);
  1868. spin_unlock_irqrestore(&list->lock, flags);
  1869. }
  1870. EXPORT_SYMBOL(skb_unlink);
  1871. /**
  1872. * skb_append - append a buffer
  1873. * @old: buffer to insert after
  1874. * @newsk: buffer to insert
  1875. * @list: list to use
  1876. *
  1877. * Place a packet after a given packet in a list. The list locks are taken
  1878. * and this function is atomic with respect to other list locked calls.
  1879. * A buffer cannot be placed on two lists at the same time.
  1880. */
  1881. void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
  1882. {
  1883. unsigned long flags;
  1884. spin_lock_irqsave(&list->lock, flags);
  1885. __skb_queue_after(list, old, newsk);
  1886. spin_unlock_irqrestore(&list->lock, flags);
  1887. }
  1888. EXPORT_SYMBOL(skb_append);
  1889. /**
  1890. * skb_insert - insert a buffer
  1891. * @old: buffer to insert before
  1892. * @newsk: buffer to insert
  1893. * @list: list to use
  1894. *
  1895. * Place a packet before a given packet in a list. The list locks are
  1896. * taken and this function is atomic with respect to other list locked
  1897. * calls.
  1898. *
  1899. * A buffer cannot be placed on two lists at the same time.
  1900. */
  1901. void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
  1902. {
  1903. unsigned long flags;
  1904. spin_lock_irqsave(&list->lock, flags);
  1905. __skb_insert(newsk, old->prev, old, list);
  1906. spin_unlock_irqrestore(&list->lock, flags);
  1907. }
  1908. EXPORT_SYMBOL(skb_insert);
  1909. static inline void skb_split_inside_header(struct sk_buff *skb,
  1910. struct sk_buff* skb1,
  1911. const u32 len, const int pos)
  1912. {
  1913. int i;
  1914. skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
  1915. pos - len);
  1916. /* And move data appendix as is. */
  1917. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
  1918. skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
  1919. skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
  1920. skb_shinfo(skb)->nr_frags = 0;
  1921. skb1->data_len = skb->data_len;
  1922. skb1->len += skb1->data_len;
  1923. skb->data_len = 0;
  1924. skb->len = len;
  1925. skb_set_tail_pointer(skb, len);
  1926. }
  1927. static inline void skb_split_no_header(struct sk_buff *skb,
  1928. struct sk_buff* skb1,
  1929. const u32 len, int pos)
  1930. {
  1931. int i, k = 0;
  1932. const int nfrags = skb_shinfo(skb)->nr_frags;
  1933. skb_shinfo(skb)->nr_frags = 0;
  1934. skb1->len = skb1->data_len = skb->len - len;
  1935. skb->len = len;
  1936. skb->data_len = len - pos;
  1937. for (i = 0; i < nfrags; i++) {
  1938. int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
  1939. if (pos + size > len) {
  1940. skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
  1941. if (pos < len) {
  1942. /* Split frag.
  1943. * We have two variants in this case:
  1944. * 1. Move all the frag to the second
  1945. * part, if it is possible. F.e.
  1946. * this approach is mandatory for TUX,
  1947. * where splitting is expensive.
  1948. * 2. Split is accurately. We make this.
  1949. */
  1950. skb_frag_ref(skb, i);
  1951. skb_shinfo(skb1)->frags[0].page_offset += len - pos;
  1952. skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
  1953. skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
  1954. skb_shinfo(skb)->nr_frags++;
  1955. }
  1956. k++;
  1957. } else
  1958. skb_shinfo(skb)->nr_frags++;
  1959. pos += size;
  1960. }
  1961. skb_shinfo(skb1)->nr_frags = k;
  1962. }
  1963. /**
  1964. * skb_split - Split fragmented skb to two parts at length len.
  1965. * @skb: the buffer to split
  1966. * @skb1: the buffer to receive the second part
  1967. * @len: new length for skb
  1968. */
  1969. void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
  1970. {
  1971. int pos = skb_headlen(skb);
  1972. if (len < pos) /* Split line is inside header. */
  1973. skb_split_inside_header(skb, skb1, len, pos);
  1974. else /* Second chunk has no header, nothing to copy. */
  1975. skb_split_no_header(skb, skb1, len, pos);
  1976. }
  1977. EXPORT_SYMBOL(skb_split);
  1978. /* Shifting from/to a cloned skb is a no-go.
  1979. *
  1980. * Caller cannot keep skb_shinfo related pointers past calling here!
  1981. */
  1982. static int skb_prepare_for_shift(struct sk_buff *skb)
  1983. {
  1984. return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
  1985. }
  1986. /**
  1987. * skb_shift - Shifts paged data partially from skb to another
  1988. * @tgt: buffer into which tail data gets added
  1989. * @skb: buffer from which the paged data comes from
  1990. * @shiftlen: shift up to this many bytes
  1991. *
  1992. * Attempts to shift up to shiftlen worth of bytes, which may be less than
  1993. * the length of the skb, from skb to tgt. Returns number bytes shifted.
  1994. * It's up to caller to free skb if everything was shifted.
  1995. *
  1996. * If @tgt runs out of frags, the whole operation is aborted.
  1997. *
  1998. * Skb cannot include anything else but paged data while tgt is allowed
  1999. * to have non-paged data as well.
  2000. *
  2001. * TODO: full sized shift could be optimized but that would need
  2002. * specialized skb free'er to handle frags without up-to-date nr_frags.
  2003. */
  2004. int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
  2005. {
  2006. int from, to, merge, todo;
  2007. struct skb_frag_struct *fragfrom, *fragto;
  2008. BUG_ON(shiftlen > skb->len);
  2009. BUG_ON(skb_headlen(skb)); /* Would corrupt stream */
  2010. todo = shiftlen;
  2011. from = 0;
  2012. to = skb_shinfo(tgt)->nr_frags;
  2013. fragfrom = &skb_shinfo(skb)->frags[from];
  2014. /* Actual merge is delayed until the point when we know we can
  2015. * commit all, so that we don't have to undo partial changes
  2016. */
  2017. if (!to ||
  2018. !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
  2019. fragfrom->page_offset)) {
  2020. merge = -1;
  2021. } else {
  2022. merge = to - 1;
  2023. todo -= skb_frag_size(fragfrom);
  2024. if (todo < 0) {
  2025. if (skb_prepare_for_shift(skb) ||
  2026. skb_prepare_for_shift(tgt))
  2027. return 0;
  2028. /* All previous frag pointers might be stale! */
  2029. fragfrom = &skb_shinfo(skb)->frags[from];
  2030. fragto = &skb_shinfo(tgt)->frags[merge];
  2031. skb_frag_size_add(fragto, shiftlen);
  2032. skb_frag_size_sub(fragfrom, shiftlen);
  2033. fragfrom->page_offset += shiftlen;
  2034. goto onlymerged;
  2035. }
  2036. from++;
  2037. }
  2038. /* Skip full, not-fitting skb to avoid expensive operations */
  2039. if ((shiftlen == skb->len) &&
  2040. (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
  2041. return 0;
  2042. if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
  2043. return 0;
  2044. while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
  2045. if (to == MAX_SKB_FRAGS)
  2046. return 0;
  2047. fragfrom = &skb_shinfo(skb)->frags[from];
  2048. fragto = &skb_shinfo(tgt)->frags[to];
  2049. if (todo >= skb_frag_size(fragfrom)) {
  2050. *fragto = *fragfrom;
  2051. todo -= skb_frag_size(fragfrom);
  2052. from++;
  2053. to++;
  2054. } else {
  2055. __skb_frag_ref(fragfrom);
  2056. fragto->page = fragfrom->page;
  2057. fragto->page_offset = fragfrom->page_offset;
  2058. skb_frag_size_set(fragto, todo);
  2059. fragfrom->page_offset += todo;
  2060. skb_frag_size_sub(fragfrom, todo);
  2061. todo = 0;
  2062. to++;
  2063. break;
  2064. }
  2065. }
  2066. /* Ready to "commit" this state change to tgt */
  2067. skb_shinfo(tgt)->nr_frags = to;
  2068. if (merge >= 0) {
  2069. fragfrom = &skb_shinfo(skb)->frags[0];
  2070. fragto = &skb_shinfo(tgt)->frags[merge];
  2071. skb_frag_size_add(fragto, skb_frag_size(fragfrom));
  2072. __skb_frag_unref(fragfrom);
  2073. }
  2074. /* Reposition in the original skb */
  2075. to = 0;
  2076. while (from < skb_shinfo(skb)->nr_frags)
  2077. skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
  2078. skb_shinfo(skb)->nr_frags = to;
  2079. BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
  2080. onlymerged:
  2081. /* Most likely the tgt won't ever need its checksum anymore, skb on
  2082. * the other hand might need it if it needs to be resent
  2083. */
  2084. tgt->ip_summed = CHECKSUM_PARTIAL;
  2085. skb->ip_summed = CHECKSUM_PARTIAL;
  2086. /* Yak, is it really working this way? Some helper please? */
  2087. skb->len -= shiftlen;
  2088. skb->data_len -= shiftlen;
  2089. skb->truesize -= shiftlen;
  2090. tgt->len += shiftlen;
  2091. tgt->data_len += shiftlen;
  2092. tgt->truesize += shiftlen;
  2093. return shiftlen;
  2094. }
  2095. /**
  2096. * skb_prepare_seq_read - Prepare a sequential read of skb data
  2097. * @skb: the buffer to read
  2098. * @from: lower offset of data to be read
  2099. * @to: upper offset of data to be read
  2100. * @st: state variable
  2101. *
  2102. * Initializes the specified state variable. Must be called before
  2103. * invoking skb_seq_read() for the first time.
  2104. */
  2105. void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
  2106. unsigned int to, struct skb_seq_state *st)
  2107. {
  2108. st->lower_offset = from;
  2109. st->upper_offset = to;
  2110. st->root_skb = st->cur_skb = skb;
  2111. st->frag_idx = st->stepped_offset = 0;
  2112. st->frag_data = NULL;
  2113. }
  2114. EXPORT_SYMBOL(skb_prepare_seq_read);
  2115. /**
  2116. * skb_seq_read - Sequentially read skb data
  2117. * @consumed: number of bytes consumed by the caller so far
  2118. * @data: destination pointer for data to be returned
  2119. * @st: state variable
  2120. *
  2121. * Reads a block of skb data at &consumed relative to the
  2122. * lower offset specified to skb_prepare_seq_read(). Assigns
  2123. * the head of the data block to &data and returns the length
  2124. * of the block or 0 if the end of the skb data or the upper
  2125. * offset has been reached.
  2126. *
  2127. * The caller is not required to consume all of the data
  2128. * returned, i.e. &consumed is typically set to the number
  2129. * of bytes already consumed and the next call to
  2130. * skb_seq_read() will return the remaining part of the block.
  2131. *
  2132. * Note 1: The size of each block of data returned can be arbitrary,
  2133. * this limitation is the cost for zerocopy seqeuental
  2134. * reads of potentially non linear data.
  2135. *
  2136. * Note 2: Fragment lists within fragments are not implemented
  2137. * at the moment, state->root_skb could be replaced with
  2138. * a stack for this purpose.
  2139. */
  2140. unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
  2141. struct skb_seq_state *st)
  2142. {
  2143. unsigned int block_limit, abs_offset = consumed + st->lower_offset;
  2144. skb_frag_t *frag;
  2145. if (unlikely(abs_offset >= st->upper_offset))
  2146. return 0;
  2147. next_skb:
  2148. block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
  2149. if (abs_offset < block_limit && !st->frag_data) {
  2150. *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
  2151. return block_limit - abs_offset;
  2152. }
  2153. if (st->frag_idx == 0 && !st->frag_data)
  2154. st->stepped_offset += skb_headlen(st->cur_skb);
  2155. while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
  2156. frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
  2157. block_limit = skb_frag_size(frag) + st->stepped_offset;
  2158. if (abs_offset < block_limit) {
  2159. if (!st->frag_data)
  2160. st->frag_data = kmap_skb_frag(frag);
  2161. *data = (u8 *) st->frag_data + frag->page_offset +
  2162. (abs_offset - st->stepped_offset);
  2163. return block_limit - abs_offset;
  2164. }
  2165. if (st->frag_data) {
  2166. kunmap_skb_frag(st->frag_data);
  2167. st->frag_data = NULL;
  2168. }
  2169. st->frag_idx++;
  2170. st->stepped_offset += skb_frag_size(frag);
  2171. }
  2172. if (st->frag_data) {
  2173. kunmap_skb_frag(st->frag_data);
  2174. st->frag_data = NULL;
  2175. }
  2176. if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
  2177. st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
  2178. st->frag_idx = 0;
  2179. goto next_skb;
  2180. } else if (st->cur_skb->next) {
  2181. st->cur_skb = st->cur_skb->next;
  2182. st->frag_idx = 0;
  2183. goto next_skb;
  2184. }
  2185. return 0;
  2186. }
  2187. EXPORT_SYMBOL(skb_seq_read);
  2188. /**
  2189. * skb_abort_seq_read - Abort a sequential read of skb data
  2190. * @st: state variable
  2191. *
  2192. * Must be called if skb_seq_read() was not called until it
  2193. * returned 0.
  2194. */
  2195. void skb_abort_seq_read(struct skb_seq_state *st)
  2196. {
  2197. if (st->frag_data)
  2198. kunmap_skb_frag(st->frag_data);
  2199. }
  2200. EXPORT_SYMBOL(skb_abort_seq_read);
  2201. #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
  2202. static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
  2203. struct ts_config *conf,
  2204. struct ts_state *state)
  2205. {
  2206. return skb_seq_read(offset, text, TS_SKB_CB(state));
  2207. }
  2208. static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
  2209. {
  2210. skb_abort_seq_read(TS_SKB_CB(state));
  2211. }
  2212. /**
  2213. * skb_find_text - Find a text pattern in skb data
  2214. * @skb: the buffer to look in
  2215. * @from: search offset
  2216. * @to: search limit
  2217. * @config: textsearch configuration
  2218. * @state: uninitialized textsearch state variable
  2219. *
  2220. * Finds a pattern in the skb data according to the specified
  2221. * textsearch configuration. Use textsearch_next() to retrieve
  2222. * subsequent occurrences of the pattern. Returns the offset
  2223. * to the first occurrence or UINT_MAX if no match was found.
  2224. */
  2225. unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
  2226. unsigned int to, struct ts_config *config,
  2227. struct ts_state *state)
  2228. {
  2229. unsigned int ret;
  2230. config->get_next_block = skb_ts_get_next_block;
  2231. config->finish = skb_ts_finish;
  2232. skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
  2233. ret = textsearch_find(config, state);
  2234. return (ret <= to - from ? ret : UINT_MAX);
  2235. }
  2236. EXPORT_SYMBOL(skb_find_text);
  2237. /**
  2238. * skb_append_datato_frags: - append the user data to a skb
  2239. * @sk: sock structure
  2240. * @skb: skb structure to be appened with user data.
  2241. * @getfrag: call back function to be used for getting the user data
  2242. * @from: pointer to user message iov
  2243. * @length: length of the iov message
  2244. *
  2245. * Description: This procedure append the user data in the fragment part
  2246. * of the skb if any page alloc fails user this procedure returns -ENOMEM
  2247. */
  2248. int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
  2249. int (*getfrag)(void *from, char *to, int offset,
  2250. int len, int odd, struct sk_buff *skb),
  2251. void *from, int length)
  2252. {
  2253. int frg_cnt = 0;
  2254. skb_frag_t *frag = NULL;
  2255. struct page *page = NULL;
  2256. int copy, left;
  2257. int offset = 0;
  2258. int ret;
  2259. do {
  2260. /* Return error if we don't have space for new frag */
  2261. frg_cnt = skb_shinfo(skb)->nr_frags;
  2262. if (frg_cnt >= MAX_SKB_FRAGS)
  2263. return -EFAULT;
  2264. /* allocate a new page for next frag */
  2265. page = alloc_pages(sk->sk_allocation, 0);
  2266. /* If alloc_page fails just return failure and caller will
  2267. * free previous allocated pages by doing kfree_skb()
  2268. */
  2269. if (page == NULL)
  2270. return -ENOMEM;
  2271. /* initialize the next frag */
  2272. skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
  2273. skb->truesize += PAGE_SIZE;
  2274. atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
  2275. /* get the new initialized frag */
  2276. frg_cnt = skb_shinfo(skb)->nr_frags;
  2277. frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
  2278. /* copy the user data to page */
  2279. left = PAGE_SIZE - frag->page_offset;
  2280. copy = (length > left)? left : length;
  2281. ret = getfrag(from, skb_frag_address(frag) + skb_frag_size(frag),
  2282. offset, copy, 0, skb);
  2283. if (ret < 0)
  2284. return -EFAULT;
  2285. /* copy was successful so update the size parameters */
  2286. skb_frag_size_add(frag, copy);
  2287. skb->len += copy;
  2288. skb->data_len += copy;
  2289. offset += copy;
  2290. length -= copy;
  2291. } while (length > 0);
  2292. return 0;
  2293. }
  2294. EXPORT_SYMBOL(skb_append_datato_frags);
  2295. /**
  2296. * skb_pull_rcsum - pull skb and update receive checksum
  2297. * @skb: buffer to update
  2298. * @len: length of data pulled
  2299. *
  2300. * This function performs an skb_pull on the packet and updates
  2301. * the CHECKSUM_COMPLETE checksum. It should be used on
  2302. * receive path processing instead of skb_pull unless you know
  2303. * that the checksum difference is zero (e.g., a valid IP header)
  2304. * or you are setting ip_summed to CHECKSUM_NONE.
  2305. */
  2306. unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
  2307. {
  2308. BUG_ON(len > skb->len);
  2309. skb->len -= len;
  2310. BUG_ON(skb->len < skb->data_len);
  2311. skb_postpull_rcsum(skb, skb->data, len);
  2312. return skb->data += len;
  2313. }
  2314. EXPORT_SYMBOL_GPL(skb_pull_rcsum);
  2315. /**
  2316. * skb_segment - Perform protocol segmentation on skb.
  2317. * @skb: buffer to segment
  2318. * @features: features for the output path (see dev->features)
  2319. *
  2320. * This function performs segmentation on the given skb. It returns
  2321. * a pointer to the first in a list of new skbs for the segments.
  2322. * In case of error it returns ERR_PTR(err).
  2323. */
  2324. struct sk_buff *skb_segment(struct sk_buff *skb, netdev_features_t features)
  2325. {
  2326. struct sk_buff *segs = NULL;
  2327. struct sk_buff *tail = NULL;
  2328. struct sk_buff *fskb = skb_shinfo(skb)->frag_list;
  2329. unsigned int mss = skb_shinfo(skb)->gso_size;
  2330. unsigned int doffset = skb->data - skb_mac_header(skb);
  2331. unsigned int offset = doffset;
  2332. unsigned int headroom;
  2333. unsigned int len;
  2334. int sg = !!(features & NETIF_F_SG);
  2335. int nfrags = skb_shinfo(skb)->nr_frags;
  2336. int err = -ENOMEM;
  2337. int i = 0;
  2338. int pos;
  2339. __skb_push(skb, doffset);
  2340. headroom = skb_headroom(skb);
  2341. pos = skb_headlen(skb);
  2342. do {
  2343. struct sk_buff *nskb;
  2344. skb_frag_t *frag;
  2345. int hsize;
  2346. int size;
  2347. len = skb->len - offset;
  2348. if (len > mss)
  2349. len = mss;
  2350. hsize = skb_headlen(skb) - offset;
  2351. if (hsize < 0)
  2352. hsize = 0;
  2353. if (hsize > len || !sg)
  2354. hsize = len;
  2355. if (!hsize && i >= nfrags) {
  2356. BUG_ON(fskb->len != len);
  2357. pos += len;
  2358. nskb = skb_clone(fskb, GFP_ATOMIC);
  2359. fskb = fskb->next;
  2360. if (unlikely(!nskb))
  2361. goto err;
  2362. hsize = skb_end_offset(nskb);
  2363. if (skb_cow_head(nskb, doffset + headroom)) {
  2364. kfree_skb(nskb);
  2365. goto err;
  2366. }
  2367. nskb->truesize += skb_end_offset(nskb) - hsize;
  2368. skb_release_head_state(nskb);
  2369. __skb_push(nskb, doffset);
  2370. } else {
  2371. nskb = alloc_skb(hsize + doffset + headroom,
  2372. GFP_ATOMIC);
  2373. if (unlikely(!nskb))
  2374. goto err;
  2375. skb_reserve(nskb, headroom);
  2376. __skb_put(nskb, doffset);
  2377. }
  2378. if (segs)
  2379. tail->next = nskb;
  2380. else
  2381. segs = nskb;
  2382. tail = nskb;
  2383. __copy_skb_header(nskb, skb);
  2384. /* nskb and skb might have different headroom */
  2385. if (nskb->ip_summed == CHECKSUM_PARTIAL)
  2386. nskb->csum_start += skb_headroom(nskb) - headroom;
  2387. skb_reset_mac_header(nskb);
  2388. skb_set_network_header(nskb, skb->mac_len);
  2389. nskb->transport_header = (nskb->network_header +
  2390. skb_network_header_len(skb));
  2391. skb_reset_mac_len(nskb);
  2392. skb_copy_from_linear_data(skb, nskb->data, doffset);
  2393. if (fskb != skb_shinfo(skb)->frag_list)
  2394. continue;
  2395. if (!sg) {
  2396. nskb->ip_summed = CHECKSUM_NONE;
  2397. nskb->csum = skb_copy_and_csum_bits(skb, offset,
  2398. skb_put(nskb, len),
  2399. len, 0);
  2400. continue;
  2401. }
  2402. frag = skb_shinfo(nskb)->frags;
  2403. skb_copy_from_linear_data_offset(skb, offset,
  2404. skb_put(nskb, hsize), hsize);
  2405. while (pos < offset + len && i < nfrags) {
  2406. if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC)))
  2407. goto err;
  2408. *frag = skb_shinfo(skb)->frags[i];
  2409. __skb_frag_ref(frag);
  2410. size = skb_frag_size(frag);
  2411. if (pos < offset) {
  2412. frag->page_offset += offset - pos;
  2413. skb_frag_size_sub(frag, offset - pos);
  2414. }
  2415. skb_shinfo(nskb)->nr_frags++;
  2416. if (pos + size <= offset + len) {
  2417. i++;
  2418. pos += size;
  2419. } else {
  2420. skb_frag_size_sub(frag, pos + size - (offset + len));
  2421. goto skip_fraglist;
  2422. }
  2423. frag++;
  2424. }
  2425. if (pos < offset + len) {
  2426. struct sk_buff *fskb2 = fskb;
  2427. BUG_ON(pos + fskb->len != offset + len);
  2428. pos += fskb->len;
  2429. fskb = fskb->next;
  2430. if (fskb2->next) {
  2431. fskb2 = skb_clone(fskb2, GFP_ATOMIC);
  2432. if (!fskb2)
  2433. goto err;
  2434. } else
  2435. skb_get(fskb2);
  2436. SKB_FRAG_ASSERT(nskb);
  2437. skb_shinfo(nskb)->frag_list = fskb2;
  2438. }
  2439. skip_fraglist:
  2440. nskb->data_len = len - hsize;
  2441. nskb->len += nskb->data_len;
  2442. nskb->truesize += nskb->data_len;
  2443. } while ((offset += len) < skb->len);
  2444. return segs;
  2445. err:
  2446. while ((skb = segs)) {
  2447. segs = skb->next;
  2448. kfree_skb(skb);
  2449. }
  2450. return ERR_PTR(err);
  2451. }
  2452. EXPORT_SYMBOL_GPL(skb_segment);
  2453. int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
  2454. {
  2455. struct sk_buff *p = *head;
  2456. struct sk_buff *nskb;
  2457. struct skb_shared_info *skbinfo = skb_shinfo(skb);
  2458. struct skb_shared_info *pinfo = skb_shinfo(p);
  2459. unsigned int headroom;
  2460. unsigned int len = skb_gro_len(skb);
  2461. unsigned int offset = skb_gro_offset(skb);
  2462. unsigned int headlen = skb_headlen(skb);
  2463. if (p->len + len >= 65536)
  2464. return -E2BIG;
  2465. if (pinfo->frag_list)
  2466. goto merge;
  2467. else if (headlen <= offset) {
  2468. skb_frag_t *frag;
  2469. skb_frag_t *frag2;
  2470. int i = skbinfo->nr_frags;
  2471. int nr_frags = pinfo->nr_frags + i;
  2472. offset -= headlen;
  2473. if (nr_frags > MAX_SKB_FRAGS)
  2474. return -E2BIG;
  2475. pinfo->nr_frags = nr_frags;
  2476. skbinfo->nr_frags = 0;
  2477. frag = pinfo->frags + nr_frags;
  2478. frag2 = skbinfo->frags + i;
  2479. do {
  2480. *--frag = *--frag2;
  2481. } while (--i);
  2482. frag->page_offset += offset;
  2483. skb_frag_size_sub(frag, offset);
  2484. skb->truesize -= skb->data_len;
  2485. skb->len -= skb->data_len;
  2486. skb->data_len = 0;
  2487. NAPI_GRO_CB(skb)->free = 1;
  2488. goto done;
  2489. } else if (skb_gro_len(p) != pinfo->gso_size)
  2490. return -E2BIG;
  2491. headroom = skb_headroom(p);
  2492. nskb = alloc_skb(headroom + skb_gro_offset(p), GFP_ATOMIC);
  2493. if (unlikely(!nskb))
  2494. return -ENOMEM;
  2495. __copy_skb_header(nskb, p);
  2496. nskb->mac_len = p->mac_len;
  2497. skb_reserve(nskb, headroom);
  2498. __skb_put(nskb, skb_gro_offset(p));
  2499. skb_set_mac_header(nskb, skb_mac_header(p) - p->data);
  2500. skb_set_network_header(nskb, skb_network_offset(p));
  2501. skb_set_transport_header(nskb, skb_transport_offset(p));
  2502. __skb_pull(p, skb_gro_offset(p));
  2503. memcpy(skb_mac_header(nskb), skb_mac_header(p),
  2504. p->data - skb_mac_header(p));
  2505. *NAPI_GRO_CB(nskb) = *NAPI_GRO_CB(p);
  2506. skb_shinfo(nskb)->frag_list = p;
  2507. skb_shinfo(nskb)->gso_size = pinfo->gso_size;
  2508. pinfo->gso_size = 0;
  2509. skb_header_release(p);
  2510. nskb->prev = p;
  2511. nskb->data_len += p->len;
  2512. nskb->truesize += p->truesize;
  2513. nskb->len += p->len;
  2514. *head = nskb;
  2515. nskb->next = p->next;
  2516. p->next = NULL;
  2517. p = nskb;
  2518. merge:
  2519. p->truesize += skb->truesize - len;
  2520. if (offset > headlen) {
  2521. unsigned int eat = offset - headlen;
  2522. skbinfo->frags[0].page_offset += eat;
  2523. skb_frag_size_sub(&skbinfo->frags[0], eat);
  2524. skb->data_len -= eat;
  2525. skb->len -= eat;
  2526. offset = headlen;
  2527. }
  2528. __skb_pull(skb, offset);
  2529. p->prev->next = skb;
  2530. p->prev = skb;
  2531. skb_header_release(skb);
  2532. done:
  2533. NAPI_GRO_CB(p)->count++;
  2534. p->data_len += len;
  2535. p->truesize += len;
  2536. p->len += len;
  2537. NAPI_GRO_CB(skb)->same_flow = 1;
  2538. return 0;
  2539. }
  2540. EXPORT_SYMBOL_GPL(skb_gro_receive);
  2541. void __init skb_init(void)
  2542. {
  2543. skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
  2544. sizeof(struct sk_buff),
  2545. 0,
  2546. SLAB_HWCACHE_ALIGN|SLAB_PANIC,
  2547. NULL);
  2548. skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
  2549. (2*sizeof(struct sk_buff)) +
  2550. sizeof(atomic_t),
  2551. 0,
  2552. SLAB_HWCACHE_ALIGN|SLAB_PANIC,
  2553. NULL);
  2554. }
  2555. /**
  2556. * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
  2557. * @skb: Socket buffer containing the buffers to be mapped
  2558. * @sg: The scatter-gather list to map into
  2559. * @offset: The offset into the buffer's contents to start mapping
  2560. * @len: Length of buffer space to be mapped
  2561. *
  2562. * Fill the specified scatter-gather list with mappings/pointers into a
  2563. * region of the buffer space attached to a socket buffer.
  2564. */
  2565. static int
  2566. __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
  2567. {
  2568. int start = skb_headlen(skb);
  2569. int i, copy = start - offset;
  2570. struct sk_buff *frag_iter;
  2571. int elt = 0;
  2572. if (copy > 0) {
  2573. if (copy > len)
  2574. copy = len;
  2575. sg_set_buf(sg, skb->data + offset, copy);
  2576. elt++;
  2577. if ((len -= copy) == 0)
  2578. return elt;
  2579. offset += copy;
  2580. }
  2581. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  2582. int end;
  2583. WARN_ON(start > offset + len);
  2584. end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
  2585. if ((copy = end - offset) > 0) {
  2586. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  2587. if (copy > len)
  2588. copy = len;
  2589. sg_set_page(&sg[elt], skb_frag_page(frag), copy,
  2590. frag->page_offset+offset-start);
  2591. elt++;
  2592. if (!(len -= copy))
  2593. return elt;
  2594. offset += copy;
  2595. }
  2596. start = end;
  2597. }
  2598. skb_walk_frags(skb, frag_iter) {
  2599. int end;
  2600. WARN_ON(start > offset + len);
  2601. end = start + frag_iter->len;
  2602. if ((copy = end - offset) > 0) {
  2603. if (copy > len)
  2604. copy = len;
  2605. elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start,
  2606. copy);
  2607. if ((len -= copy) == 0)
  2608. return elt;
  2609. offset += copy;
  2610. }
  2611. start = end;
  2612. }
  2613. BUG_ON(len);
  2614. return elt;
  2615. }
  2616. int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
  2617. {
  2618. int nsg = __skb_to_sgvec(skb, sg, offset, len);
  2619. sg_mark_end(&sg[nsg - 1]);
  2620. return nsg;
  2621. }
  2622. EXPORT_SYMBOL_GPL(skb_to_sgvec);
  2623. /**
  2624. * skb_cow_data - Check that a socket buffer's data buffers are writable
  2625. * @skb: The socket buffer to check.
  2626. * @tailbits: Amount of trailing space to be added
  2627. * @trailer: Returned pointer to the skb where the @tailbits space begins
  2628. *
  2629. * Make sure that the data buffers attached to a socket buffer are
  2630. * writable. If they are not, private copies are made of the data buffers
  2631. * and the socket buffer is set to use these instead.
  2632. *
  2633. * If @tailbits is given, make sure that there is space to write @tailbits
  2634. * bytes of data beyond current end of socket buffer. @trailer will be
  2635. * set to point to the skb in which this space begins.
  2636. *
  2637. * The number of scatterlist elements required to completely map the
  2638. * COW'd and extended socket buffer will be returned.
  2639. */
  2640. int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
  2641. {
  2642. int copyflag;
  2643. int elt;
  2644. struct sk_buff *skb1, **skb_p;
  2645. /* If skb is cloned or its head is paged, reallocate
  2646. * head pulling out all the pages (pages are considered not writable
  2647. * at the moment even if they are anonymous).
  2648. */
  2649. if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
  2650. __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
  2651. return -ENOMEM;
  2652. /* Easy case. Most of packets will go this way. */
  2653. if (!skb_has_frag_list(skb)) {
  2654. /* A little of trouble, not enough of space for trailer.
  2655. * This should not happen, when stack is tuned to generate
  2656. * good frames. OK, on miss we reallocate and reserve even more
  2657. * space, 128 bytes is fair. */
  2658. if (skb_tailroom(skb) < tailbits &&
  2659. pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
  2660. return -ENOMEM;
  2661. /* Voila! */
  2662. *trailer = skb;
  2663. return 1;
  2664. }
  2665. /* Misery. We are in troubles, going to mincer fragments... */
  2666. elt = 1;
  2667. skb_p = &skb_shinfo(skb)->frag_list;
  2668. copyflag = 0;
  2669. while ((skb1 = *skb_p) != NULL) {
  2670. int ntail = 0;
  2671. /* The fragment is partially pulled by someone,
  2672. * this can happen on input. Copy it and everything
  2673. * after it. */
  2674. if (skb_shared(skb1))
  2675. copyflag = 1;
  2676. /* If the skb is the last, worry about trailer. */
  2677. if (skb1->next == NULL && tailbits) {
  2678. if (skb_shinfo(skb1)->nr_frags ||
  2679. skb_has_frag_list(skb1) ||
  2680. skb_tailroom(skb1) < tailbits)
  2681. ntail = tailbits + 128;
  2682. }
  2683. if (copyflag ||
  2684. skb_cloned(skb1) ||
  2685. ntail ||
  2686. skb_shinfo(skb1)->nr_frags ||
  2687. skb_has_frag_list(skb1)) {
  2688. struct sk_buff *skb2;
  2689. /* Fuck, we are miserable poor guys... */
  2690. if (ntail == 0)
  2691. skb2 = skb_copy(skb1, GFP_ATOMIC);
  2692. else
  2693. skb2 = skb_copy_expand(skb1,
  2694. skb_headroom(skb1),
  2695. ntail,
  2696. GFP_ATOMIC);
  2697. if (unlikely(skb2 == NULL))
  2698. return -ENOMEM;
  2699. if (skb1->sk)
  2700. skb_set_owner_w(skb2, skb1->sk);
  2701. /* Looking around. Are we still alive?
  2702. * OK, link new skb, drop old one */
  2703. skb2->next = skb1->next;
  2704. *skb_p = skb2;
  2705. kfree_skb(skb1);
  2706. skb1 = skb2;
  2707. }
  2708. elt++;
  2709. *trailer = skb1;
  2710. skb_p = &skb1->next;
  2711. }
  2712. return elt;
  2713. }
  2714. EXPORT_SYMBOL_GPL(skb_cow_data);
  2715. static void sock_rmem_free(struct sk_buff *skb)
  2716. {
  2717. struct sock *sk = skb->sk;
  2718. atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
  2719. }
  2720. /*
  2721. * Note: We dont mem charge error packets (no sk_forward_alloc changes)
  2722. */
  2723. int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
  2724. {
  2725. int len = skb->len;
  2726. if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
  2727. (unsigned int)sk->sk_rcvbuf)
  2728. return -ENOMEM;
  2729. skb_orphan(skb);
  2730. skb->sk = sk;
  2731. skb->destructor = sock_rmem_free;
  2732. atomic_add(skb->truesize, &sk->sk_rmem_alloc);
  2733. /* before exiting rcu section, make sure dst is refcounted */
  2734. skb_dst_force(skb);
  2735. skb_queue_tail(&sk->sk_error_queue, skb);
  2736. if (!sock_flag(sk, SOCK_DEAD))
  2737. sk->sk_data_ready(sk, len);
  2738. return 0;
  2739. }
  2740. EXPORT_SYMBOL(sock_queue_err_skb);
  2741. void skb_tstamp_tx(struct sk_buff *orig_skb,
  2742. struct skb_shared_hwtstamps *hwtstamps)
  2743. {
  2744. struct sock *sk = orig_skb->sk;
  2745. struct sock_exterr_skb *serr;
  2746. struct sk_buff *skb;
  2747. int err;
  2748. if (!sk)
  2749. return;
  2750. skb = skb_clone(orig_skb, GFP_ATOMIC);
  2751. if (!skb)
  2752. return;
  2753. if (hwtstamps) {
  2754. *skb_hwtstamps(skb) =
  2755. *hwtstamps;
  2756. } else {
  2757. /*
  2758. * no hardware time stamps available,
  2759. * so keep the shared tx_flags and only
  2760. * store software time stamp
  2761. */
  2762. skb->tstamp = ktime_get_real();
  2763. }
  2764. serr = SKB_EXT_ERR(skb);
  2765. memset(serr, 0, sizeof(*serr));
  2766. serr->ee.ee_errno = ENOMSG;
  2767. serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
  2768. err = sock_queue_err_skb(sk, skb);
  2769. if (err)
  2770. kfree_skb(skb);
  2771. }
  2772. EXPORT_SYMBOL_GPL(skb_tstamp_tx);
  2773. void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
  2774. {
  2775. struct sock *sk = skb->sk;
  2776. struct sock_exterr_skb *serr;
  2777. int err;
  2778. skb->wifi_acked_valid = 1;
  2779. skb->wifi_acked = acked;
  2780. serr = SKB_EXT_ERR(skb);
  2781. memset(serr, 0, sizeof(*serr));
  2782. serr->ee.ee_errno = ENOMSG;
  2783. serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
  2784. err = sock_queue_err_skb(sk, skb);
  2785. if (err)
  2786. kfree_skb(skb);
  2787. }
  2788. EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
  2789. /**
  2790. * skb_partial_csum_set - set up and verify partial csum values for packet
  2791. * @skb: the skb to set
  2792. * @start: the number of bytes after skb->data to start checksumming.
  2793. * @off: the offset from start to place the checksum.
  2794. *
  2795. * For untrusted partially-checksummed packets, we need to make sure the values
  2796. * for skb->csum_start and skb->csum_offset are valid so we don't oops.
  2797. *
  2798. * This function checks and sets those values and skb->ip_summed: if this
  2799. * returns false you should drop the packet.
  2800. */
  2801. bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
  2802. {
  2803. if (unlikely(start > skb_headlen(skb)) ||
  2804. unlikely((int)start + off > skb_headlen(skb) - 2)) {
  2805. net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n",
  2806. start, off, skb_headlen(skb));
  2807. return false;
  2808. }
  2809. skb->ip_summed = CHECKSUM_PARTIAL;
  2810. skb->csum_start = skb_headroom(skb) + start;
  2811. skb->csum_offset = off;
  2812. return true;
  2813. }
  2814. EXPORT_SYMBOL_GPL(skb_partial_csum_set);
  2815. void __skb_warn_lro_forwarding(const struct sk_buff *skb)
  2816. {
  2817. net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
  2818. skb->dev->name);
  2819. }
  2820. EXPORT_SYMBOL(__skb_warn_lro_forwarding);
  2821. /**
  2822. * skb_gso_transport_seglen - Return length of individual segments of a gso packet
  2823. *
  2824. * @skb: GSO skb
  2825. *
  2826. * skb_gso_transport_seglen is used to determine the real size of the
  2827. * individual segments, including Layer4 headers (TCP/UDP).
  2828. *
  2829. * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
  2830. */
  2831. unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
  2832. {
  2833. const struct skb_shared_info *shinfo = skb_shinfo(skb);
  2834. if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
  2835. return tcp_hdrlen(skb) + shinfo->gso_size;
  2836. /* UFO sets gso_size to the size of the fragmentation
  2837. * payload, i.e. the size of the L4 (UDP) header is already
  2838. * accounted for.
  2839. */
  2840. return shinfo->gso_size;
  2841. }
  2842. EXPORT_SYMBOL_GPL(skb_gso_transport_seglen);