skbuff.c 90 KB

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