skbuff.c 110 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418
  1. /*
  2. * Routines having to do with the 'struct sk_buff' memory handlers.
  3. *
  4. * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>
  5. * Florian La Roche <rzsfl@rz.uni-sb.de>
  6. *
  7. * Fixes:
  8. * Alan Cox : Fixed the worst of the load
  9. * balancer bugs.
  10. * Dave Platt : Interrupt stacking fix.
  11. * Richard Kooijman : Timestamp fixes.
  12. * Alan Cox : Changed buffer format.
  13. * Alan Cox : destructor hook for AF_UNIX etc.
  14. * Linus Torvalds : Better skb_clone.
  15. * Alan Cox : Added skb_copy.
  16. * Alan Cox : Added all the changed routines Linus
  17. * only put in the headers
  18. * Ray VanTassle : Fixed --skb->lock in free
  19. * Alan Cox : skb_copy copy arp field
  20. * Andi Kleen : slabified it.
  21. * Robert Olsson : Removed skb_head_pool
  22. *
  23. * NOTE:
  24. * The __skb_ routines should be called with interrupts
  25. * disabled, or you better be *real* sure that the operation is atomic
  26. * with respect to whatever list is being frobbed (e.g. via lock_sock()
  27. * or via disabling bottom half handlers, etc).
  28. *
  29. * This program is free software; you can redistribute it and/or
  30. * modify it under the terms of the GNU General Public License
  31. * as published by the Free Software Foundation; either version
  32. * 2 of the License, or (at your option) any later version.
  33. */
  34. /*
  35. * The functions in this file will not compile correctly with gcc 2.4.x
  36. */
  37. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  38. #include <linux/module.h>
  39. #include <linux/types.h>
  40. #include <linux/kernel.h>
  41. #include <linux/kmemcheck.h>
  42. #include <linux/mm.h>
  43. #include <linux/interrupt.h>
  44. #include <linux/in.h>
  45. #include <linux/inet.h>
  46. #include <linux/slab.h>
  47. #include <linux/tcp.h>
  48. #include <linux/udp.h>
  49. #include <linux/netdevice.h>
  50. #ifdef CONFIG_NET_CLS_ACT
  51. #include <net/pkt_sched.h>
  52. #endif
  53. #include <linux/string.h>
  54. #include <linux/skbuff.h>
  55. #include <linux/splice.h>
  56. #include <linux/cache.h>
  57. #include <linux/rtnetlink.h>
  58. #include <linux/init.h>
  59. #include <linux/scatterlist.h>
  60. #include <linux/errqueue.h>
  61. #include <linux/prefetch.h>
  62. #include <linux/if_vlan.h>
  63. #include <net/protocol.h>
  64. #include <net/dst.h>
  65. #include <net/sock.h>
  66. #include <net/checksum.h>
  67. #include <net/ip6_checksum.h>
  68. #include <net/xfrm.h>
  69. #include <asm/uaccess.h>
  70. #include <trace/events/skb.h>
  71. #include <linux/highmem.h>
  72. struct kmem_cache *skbuff_head_cache __read_mostly;
  73. static struct kmem_cache *skbuff_fclone_cache __read_mostly;
  74. /**
  75. * skb_panic - private function for out-of-line support
  76. * @skb: buffer
  77. * @sz: size
  78. * @addr: address
  79. * @msg: skb_over_panic or skb_under_panic
  80. *
  81. * Out-of-line support for skb_put() and skb_push().
  82. * Called via the wrapper skb_over_panic() or skb_under_panic().
  83. * Keep out of line to prevent kernel bloat.
  84. * __builtin_return_address is not used because it is not always reliable.
  85. */
  86. static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr,
  87. const char msg[])
  88. {
  89. pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n",
  90. msg, addr, skb->len, sz, skb->head, skb->data,
  91. (unsigned long)skb->tail, (unsigned long)skb->end,
  92. skb->dev ? skb->dev->name : "<NULL>");
  93. BUG();
  94. }
  95. static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr)
  96. {
  97. skb_panic(skb, sz, addr, __func__);
  98. }
  99. static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr)
  100. {
  101. skb_panic(skb, sz, addr, __func__);
  102. }
  103. /*
  104. * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
  105. * the caller if emergency pfmemalloc reserves are being used. If it is and
  106. * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
  107. * may be used. Otherwise, the packet data may be discarded until enough
  108. * memory is free
  109. */
  110. #define kmalloc_reserve(size, gfp, node, pfmemalloc) \
  111. __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc)
  112. static void *__kmalloc_reserve(size_t size, gfp_t flags, int node,
  113. unsigned long ip, bool *pfmemalloc)
  114. {
  115. void *obj;
  116. bool ret_pfmemalloc = false;
  117. /*
  118. * Try a regular allocation, when that fails and we're not entitled
  119. * to the reserves, fail.
  120. */
  121. obj = kmalloc_node_track_caller(size,
  122. flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
  123. node);
  124. if (obj || !(gfp_pfmemalloc_allowed(flags)))
  125. goto out;
  126. /* Try again but now we are using pfmemalloc reserves */
  127. ret_pfmemalloc = true;
  128. obj = kmalloc_node_track_caller(size, flags, node);
  129. out:
  130. if (pfmemalloc)
  131. *pfmemalloc = ret_pfmemalloc;
  132. return obj;
  133. }
  134. /* Allocate a new skbuff. We do this ourselves so we can fill in a few
  135. * 'private' fields and also do memory statistics to find all the
  136. * [BEEP] leaks.
  137. *
  138. */
  139. struct sk_buff *__alloc_skb_head(gfp_t gfp_mask, int node)
  140. {
  141. struct sk_buff *skb;
  142. /* Get the HEAD */
  143. skb = kmem_cache_alloc_node(skbuff_head_cache,
  144. gfp_mask & ~__GFP_DMA, node);
  145. if (!skb)
  146. goto out;
  147. /*
  148. * Only clear those fields we need to clear, not those that we will
  149. * actually initialise below. Hence, don't put any more fields after
  150. * the tail pointer in struct sk_buff!
  151. */
  152. memset(skb, 0, offsetof(struct sk_buff, tail));
  153. skb->head = NULL;
  154. skb->truesize = sizeof(struct sk_buff);
  155. atomic_set(&skb->users, 1);
  156. skb->mac_header = (typeof(skb->mac_header))~0U;
  157. out:
  158. return skb;
  159. }
  160. /**
  161. * __alloc_skb - allocate a network buffer
  162. * @size: size to allocate
  163. * @gfp_mask: allocation mask
  164. * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
  165. * instead of head cache and allocate a cloned (child) skb.
  166. * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
  167. * allocations in case the data is required for writeback
  168. * @node: numa node to allocate memory on
  169. *
  170. * Allocate a new &sk_buff. The returned buffer has no headroom and a
  171. * tail room of at least size bytes. The object has a reference count
  172. * of one. The return is the buffer. On a failure the return is %NULL.
  173. *
  174. * Buffers may only be allocated from interrupts using a @gfp_mask of
  175. * %GFP_ATOMIC.
  176. */
  177. struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
  178. int flags, int node)
  179. {
  180. struct kmem_cache *cache;
  181. struct skb_shared_info *shinfo;
  182. struct sk_buff *skb;
  183. u8 *data;
  184. bool pfmemalloc;
  185. cache = (flags & SKB_ALLOC_FCLONE)
  186. ? skbuff_fclone_cache : skbuff_head_cache;
  187. if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX))
  188. gfp_mask |= __GFP_MEMALLOC;
  189. /* Get the HEAD */
  190. skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
  191. if (!skb)
  192. goto out;
  193. prefetchw(skb);
  194. /* We do our best to align skb_shared_info on a separate cache
  195. * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
  196. * aligned memory blocks, unless SLUB/SLAB debug is enabled.
  197. * Both skb->head and skb_shared_info are cache line aligned.
  198. */
  199. size = SKB_DATA_ALIGN(size);
  200. size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  201. data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc);
  202. if (!data)
  203. goto nodata;
  204. /* kmalloc(size) might give us more room than requested.
  205. * Put skb_shared_info exactly at the end of allocated zone,
  206. * to allow max possible filling before reallocation.
  207. */
  208. size = SKB_WITH_OVERHEAD(ksize(data));
  209. prefetchw(data + size);
  210. /*
  211. * Only clear those fields we need to clear, not those that we will
  212. * actually initialise below. Hence, don't put any more fields after
  213. * the tail pointer in struct sk_buff!
  214. */
  215. memset(skb, 0, offsetof(struct sk_buff, tail));
  216. /* Account for allocated memory : skb + skb->head */
  217. skb->truesize = SKB_TRUESIZE(size);
  218. skb->pfmemalloc = pfmemalloc;
  219. atomic_set(&skb->users, 1);
  220. skb->head = data;
  221. skb->data = data;
  222. skb_reset_tail_pointer(skb);
  223. skb->end = skb->tail + size;
  224. skb->mac_header = (typeof(skb->mac_header))~0U;
  225. skb->transport_header = (typeof(skb->transport_header))~0U;
  226. /* make sure we initialize shinfo sequentially */
  227. shinfo = skb_shinfo(skb);
  228. memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
  229. atomic_set(&shinfo->dataref, 1);
  230. kmemcheck_annotate_variable(shinfo->destructor_arg);
  231. if (flags & SKB_ALLOC_FCLONE) {
  232. struct sk_buff_fclones *fclones;
  233. fclones = container_of(skb, struct sk_buff_fclones, skb1);
  234. kmemcheck_annotate_bitfield(&fclones->skb2, flags1);
  235. skb->fclone = SKB_FCLONE_ORIG;
  236. atomic_set(&fclones->fclone_ref, 1);
  237. fclones->skb2.fclone = SKB_FCLONE_CLONE;
  238. fclones->skb2.pfmemalloc = pfmemalloc;
  239. }
  240. out:
  241. return skb;
  242. nodata:
  243. kmem_cache_free(cache, skb);
  244. skb = NULL;
  245. goto out;
  246. }
  247. EXPORT_SYMBOL(__alloc_skb);
  248. /**
  249. * build_skb - build a network buffer
  250. * @data: data buffer provided by caller
  251. * @frag_size: size of fragment, or 0 if head was kmalloced
  252. *
  253. * Allocate a new &sk_buff. Caller provides space holding head and
  254. * skb_shared_info. @data must have been allocated by kmalloc() only if
  255. * @frag_size is 0, otherwise data should come from the page allocator.
  256. * The return is the new skb buffer.
  257. * On a failure the return is %NULL, and @data is not freed.
  258. * Notes :
  259. * Before IO, driver allocates only data buffer where NIC put incoming frame
  260. * Driver should add room at head (NET_SKB_PAD) and
  261. * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
  262. * After IO, driver calls build_skb(), to allocate sk_buff and populate it
  263. * before giving packet to stack.
  264. * RX rings only contains data buffers, not full skbs.
  265. */
  266. struct sk_buff *build_skb(void *data, unsigned int frag_size)
  267. {
  268. struct skb_shared_info *shinfo;
  269. struct sk_buff *skb;
  270. unsigned int size = frag_size ? : ksize(data);
  271. skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
  272. if (!skb)
  273. return NULL;
  274. size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  275. memset(skb, 0, offsetof(struct sk_buff, tail));
  276. skb->truesize = SKB_TRUESIZE(size);
  277. skb->head_frag = frag_size != 0;
  278. atomic_set(&skb->users, 1);
  279. skb->head = data;
  280. skb->data = data;
  281. skb_reset_tail_pointer(skb);
  282. skb->end = skb->tail + size;
  283. skb->mac_header = (typeof(skb->mac_header))~0U;
  284. skb->transport_header = (typeof(skb->transport_header))~0U;
  285. /* make sure we initialize shinfo sequentially */
  286. shinfo = skb_shinfo(skb);
  287. memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
  288. atomic_set(&shinfo->dataref, 1);
  289. kmemcheck_annotate_variable(shinfo->destructor_arg);
  290. return skb;
  291. }
  292. EXPORT_SYMBOL(build_skb);
  293. struct netdev_alloc_cache {
  294. struct page_frag frag;
  295. /* we maintain a pagecount bias, so that we dont dirty cache line
  296. * containing page->_count every time we allocate a fragment.
  297. */
  298. unsigned int pagecnt_bias;
  299. };
  300. static DEFINE_PER_CPU(struct netdev_alloc_cache, netdev_alloc_cache);
  301. static DEFINE_PER_CPU(struct netdev_alloc_cache, napi_alloc_cache);
  302. static struct page *__page_frag_refill(struct netdev_alloc_cache *nc,
  303. gfp_t gfp_mask)
  304. {
  305. const unsigned int order = NETDEV_FRAG_PAGE_MAX_ORDER;
  306. struct page *page = NULL;
  307. gfp_t gfp = gfp_mask;
  308. if (order) {
  309. gfp_mask |= __GFP_COMP | __GFP_NOWARN | __GFP_NORETRY;
  310. page = alloc_pages_node(NUMA_NO_NODE, gfp_mask, order);
  311. nc->frag.size = PAGE_SIZE << (page ? order : 0);
  312. }
  313. if (unlikely(!page))
  314. page = alloc_pages_node(NUMA_NO_NODE, gfp, 0);
  315. nc->frag.page = page;
  316. return page;
  317. }
  318. static void *__alloc_page_frag(struct netdev_alloc_cache __percpu *cache,
  319. unsigned int fragsz, gfp_t gfp_mask)
  320. {
  321. struct netdev_alloc_cache *nc = this_cpu_ptr(cache);
  322. struct page *page = nc->frag.page;
  323. unsigned int size;
  324. int offset;
  325. if (unlikely(!page)) {
  326. refill:
  327. page = __page_frag_refill(nc, gfp_mask);
  328. if (!page)
  329. return NULL;
  330. /* if size can vary use frag.size else just use PAGE_SIZE */
  331. size = NETDEV_FRAG_PAGE_MAX_ORDER ? nc->frag.size : PAGE_SIZE;
  332. /* Even if we own the page, we do not use atomic_set().
  333. * This would break get_page_unless_zero() users.
  334. */
  335. atomic_add(size - 1, &page->_count);
  336. /* reset page count bias and offset to start of new frag */
  337. nc->pagecnt_bias = size;
  338. nc->frag.offset = size;
  339. }
  340. offset = nc->frag.offset - fragsz;
  341. if (unlikely(offset < 0)) {
  342. if (!atomic_sub_and_test(nc->pagecnt_bias, &page->_count))
  343. goto refill;
  344. /* if size can vary use frag.size else just use PAGE_SIZE */
  345. size = NETDEV_FRAG_PAGE_MAX_ORDER ? nc->frag.size : PAGE_SIZE;
  346. /* OK, page count is 0, we can safely set it */
  347. atomic_set(&page->_count, size);
  348. /* reset page count bias and offset to start of new frag */
  349. nc->pagecnt_bias = size;
  350. offset = size - fragsz;
  351. }
  352. nc->pagecnt_bias--;
  353. nc->frag.offset = offset;
  354. return page_address(page) + offset;
  355. }
  356. static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
  357. {
  358. unsigned long flags;
  359. void *data;
  360. local_irq_save(flags);
  361. data = __alloc_page_frag(&netdev_alloc_cache, fragsz, gfp_mask);
  362. local_irq_restore(flags);
  363. return data;
  364. }
  365. /**
  366. * netdev_alloc_frag - allocate a page fragment
  367. * @fragsz: fragment size
  368. *
  369. * Allocates a frag from a page for receive buffer.
  370. * Uses GFP_ATOMIC allocations.
  371. */
  372. void *netdev_alloc_frag(unsigned int fragsz)
  373. {
  374. return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
  375. }
  376. EXPORT_SYMBOL(netdev_alloc_frag);
  377. static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
  378. {
  379. return __alloc_page_frag(&napi_alloc_cache, fragsz, gfp_mask);
  380. }
  381. void *napi_alloc_frag(unsigned int fragsz)
  382. {
  383. return __napi_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
  384. }
  385. EXPORT_SYMBOL(napi_alloc_frag);
  386. /**
  387. * __alloc_rx_skb - allocate an skbuff for rx
  388. * @length: length to allocate
  389. * @gfp_mask: get_free_pages mask, passed to alloc_skb
  390. * @flags: If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
  391. * allocations in case we have to fallback to __alloc_skb()
  392. * If SKB_ALLOC_NAPI is set, page fragment will be allocated
  393. * from napi_cache instead of netdev_cache.
  394. *
  395. * Allocate a new &sk_buff and assign it a usage count of one. The
  396. * buffer has unspecified headroom built in. Users should allocate
  397. * the headroom they think they need without accounting for the
  398. * built in space. The built in space is used for optimisations.
  399. *
  400. * %NULL is returned if there is no free memory.
  401. */
  402. static struct sk_buff *__alloc_rx_skb(unsigned int length, gfp_t gfp_mask,
  403. int flags)
  404. {
  405. struct sk_buff *skb = NULL;
  406. unsigned int fragsz = SKB_DATA_ALIGN(length) +
  407. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  408. if (fragsz <= PAGE_SIZE && !(gfp_mask & (__GFP_WAIT | GFP_DMA))) {
  409. void *data;
  410. if (sk_memalloc_socks())
  411. gfp_mask |= __GFP_MEMALLOC;
  412. data = (flags & SKB_ALLOC_NAPI) ?
  413. __napi_alloc_frag(fragsz, gfp_mask) :
  414. __netdev_alloc_frag(fragsz, gfp_mask);
  415. if (likely(data)) {
  416. skb = build_skb(data, fragsz);
  417. if (unlikely(!skb))
  418. put_page(virt_to_head_page(data));
  419. }
  420. } else {
  421. skb = __alloc_skb(length, gfp_mask,
  422. SKB_ALLOC_RX, NUMA_NO_NODE);
  423. }
  424. return skb;
  425. }
  426. /**
  427. * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
  428. * @dev: network device to receive on
  429. * @length: length to allocate
  430. * @gfp_mask: get_free_pages mask, passed to alloc_skb
  431. *
  432. * Allocate a new &sk_buff and assign it a usage count of one. The
  433. * buffer has NET_SKB_PAD headroom built in. Users should allocate
  434. * the headroom they think they need without accounting for the
  435. * built in space. The built in space is used for optimisations.
  436. *
  437. * %NULL is returned if there is no free memory.
  438. */
  439. struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
  440. unsigned int length, gfp_t gfp_mask)
  441. {
  442. struct sk_buff *skb;
  443. length += NET_SKB_PAD;
  444. skb = __alloc_rx_skb(length, gfp_mask, 0);
  445. if (likely(skb)) {
  446. skb_reserve(skb, NET_SKB_PAD);
  447. skb->dev = dev;
  448. }
  449. return skb;
  450. }
  451. EXPORT_SYMBOL(__netdev_alloc_skb);
  452. /**
  453. * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance
  454. * @napi: napi instance this buffer was allocated for
  455. * @length: length to allocate
  456. * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages
  457. *
  458. * Allocate a new sk_buff for use in NAPI receive. This buffer will
  459. * attempt to allocate the head from a special reserved region used
  460. * only for NAPI Rx allocation. By doing this we can save several
  461. * CPU cycles by avoiding having to disable and re-enable IRQs.
  462. *
  463. * %NULL is returned if there is no free memory.
  464. */
  465. struct sk_buff *__napi_alloc_skb(struct napi_struct *napi,
  466. unsigned int length, gfp_t gfp_mask)
  467. {
  468. struct sk_buff *skb;
  469. length += NET_SKB_PAD + NET_IP_ALIGN;
  470. skb = __alloc_rx_skb(length, gfp_mask, SKB_ALLOC_NAPI);
  471. if (likely(skb)) {
  472. skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
  473. skb->dev = napi->dev;
  474. }
  475. return skb;
  476. }
  477. EXPORT_SYMBOL(__napi_alloc_skb);
  478. void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
  479. int size, unsigned int truesize)
  480. {
  481. skb_fill_page_desc(skb, i, page, off, size);
  482. skb->len += size;
  483. skb->data_len += size;
  484. skb->truesize += truesize;
  485. }
  486. EXPORT_SYMBOL(skb_add_rx_frag);
  487. void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
  488. unsigned int truesize)
  489. {
  490. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  491. skb_frag_size_add(frag, size);
  492. skb->len += size;
  493. skb->data_len += size;
  494. skb->truesize += truesize;
  495. }
  496. EXPORT_SYMBOL(skb_coalesce_rx_frag);
  497. static void skb_drop_list(struct sk_buff **listp)
  498. {
  499. kfree_skb_list(*listp);
  500. *listp = NULL;
  501. }
  502. static inline void skb_drop_fraglist(struct sk_buff *skb)
  503. {
  504. skb_drop_list(&skb_shinfo(skb)->frag_list);
  505. }
  506. static void skb_clone_fraglist(struct sk_buff *skb)
  507. {
  508. struct sk_buff *list;
  509. skb_walk_frags(skb, list)
  510. skb_get(list);
  511. }
  512. static void skb_free_head(struct sk_buff *skb)
  513. {
  514. if (skb->head_frag)
  515. put_page(virt_to_head_page(skb->head));
  516. else
  517. kfree(skb->head);
  518. }
  519. static void skb_release_data(struct sk_buff *skb)
  520. {
  521. struct skb_shared_info *shinfo = skb_shinfo(skb);
  522. int i;
  523. if (skb->cloned &&
  524. atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
  525. &shinfo->dataref))
  526. return;
  527. for (i = 0; i < shinfo->nr_frags; i++)
  528. __skb_frag_unref(&shinfo->frags[i]);
  529. /*
  530. * If skb buf is from userspace, we need to notify the caller
  531. * the lower device DMA has done;
  532. */
  533. if (shinfo->tx_flags & SKBTX_DEV_ZEROCOPY) {
  534. struct ubuf_info *uarg;
  535. uarg = shinfo->destructor_arg;
  536. if (uarg->callback)
  537. uarg->callback(uarg, true);
  538. }
  539. if (shinfo->frag_list)
  540. kfree_skb_list(shinfo->frag_list);
  541. skb_free_head(skb);
  542. }
  543. /*
  544. * Free an skbuff by memory without cleaning the state.
  545. */
  546. static void kfree_skbmem(struct sk_buff *skb)
  547. {
  548. struct sk_buff_fclones *fclones;
  549. switch (skb->fclone) {
  550. case SKB_FCLONE_UNAVAILABLE:
  551. kmem_cache_free(skbuff_head_cache, skb);
  552. return;
  553. case SKB_FCLONE_ORIG:
  554. fclones = container_of(skb, struct sk_buff_fclones, skb1);
  555. /* We usually free the clone (TX completion) before original skb
  556. * This test would have no chance to be true for the clone,
  557. * while here, branch prediction will be good.
  558. */
  559. if (atomic_read(&fclones->fclone_ref) == 1)
  560. goto fastpath;
  561. break;
  562. default: /* SKB_FCLONE_CLONE */
  563. fclones = container_of(skb, struct sk_buff_fclones, skb2);
  564. break;
  565. }
  566. if (!atomic_dec_and_test(&fclones->fclone_ref))
  567. return;
  568. fastpath:
  569. kmem_cache_free(skbuff_fclone_cache, fclones);
  570. }
  571. static void skb_release_head_state(struct sk_buff *skb)
  572. {
  573. skb_dst_drop(skb);
  574. #ifdef CONFIG_XFRM
  575. secpath_put(skb->sp);
  576. #endif
  577. if (skb->destructor) {
  578. WARN_ON(in_irq());
  579. skb->destructor(skb);
  580. }
  581. #if IS_ENABLED(CONFIG_NF_CONNTRACK)
  582. nf_conntrack_put(skb->nfct);
  583. #endif
  584. #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
  585. nf_bridge_put(skb->nf_bridge);
  586. #endif
  587. }
  588. /* Free everything but the sk_buff shell. */
  589. static void skb_release_all(struct sk_buff *skb)
  590. {
  591. skb_release_head_state(skb);
  592. if (likely(skb->head))
  593. skb_release_data(skb);
  594. }
  595. /**
  596. * __kfree_skb - private function
  597. * @skb: buffer
  598. *
  599. * Free an sk_buff. Release anything attached to the buffer.
  600. * Clean the state. This is an internal helper function. Users should
  601. * always call kfree_skb
  602. */
  603. void __kfree_skb(struct sk_buff *skb)
  604. {
  605. skb_release_all(skb);
  606. kfree_skbmem(skb);
  607. }
  608. EXPORT_SYMBOL(__kfree_skb);
  609. /**
  610. * kfree_skb - free an sk_buff
  611. * @skb: buffer to free
  612. *
  613. * Drop a reference to the buffer and free it if the usage count has
  614. * hit zero.
  615. */
  616. void kfree_skb(struct sk_buff *skb)
  617. {
  618. if (unlikely(!skb))
  619. return;
  620. if (likely(atomic_read(&skb->users) == 1))
  621. smp_rmb();
  622. else if (likely(!atomic_dec_and_test(&skb->users)))
  623. return;
  624. trace_kfree_skb(skb, __builtin_return_address(0));
  625. __kfree_skb(skb);
  626. }
  627. EXPORT_SYMBOL(kfree_skb);
  628. void kfree_skb_list(struct sk_buff *segs)
  629. {
  630. while (segs) {
  631. struct sk_buff *next = segs->next;
  632. kfree_skb(segs);
  633. segs = next;
  634. }
  635. }
  636. EXPORT_SYMBOL(kfree_skb_list);
  637. /**
  638. * skb_tx_error - report an sk_buff xmit error
  639. * @skb: buffer that triggered an error
  640. *
  641. * Report xmit error if a device callback is tracking this skb.
  642. * skb must be freed afterwards.
  643. */
  644. void skb_tx_error(struct sk_buff *skb)
  645. {
  646. if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
  647. struct ubuf_info *uarg;
  648. uarg = skb_shinfo(skb)->destructor_arg;
  649. if (uarg->callback)
  650. uarg->callback(uarg, false);
  651. skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
  652. }
  653. }
  654. EXPORT_SYMBOL(skb_tx_error);
  655. /**
  656. * consume_skb - free an skbuff
  657. * @skb: buffer to free
  658. *
  659. * Drop a ref to the buffer and free it if the usage count has hit zero
  660. * Functions identically to kfree_skb, but kfree_skb assumes that the frame
  661. * is being dropped after a failure and notes that
  662. */
  663. void consume_skb(struct sk_buff *skb)
  664. {
  665. if (unlikely(!skb))
  666. return;
  667. if (likely(atomic_read(&skb->users) == 1))
  668. smp_rmb();
  669. else if (likely(!atomic_dec_and_test(&skb->users)))
  670. return;
  671. trace_consume_skb(skb);
  672. __kfree_skb(skb);
  673. }
  674. EXPORT_SYMBOL(consume_skb);
  675. /* Make sure a field is enclosed inside headers_start/headers_end section */
  676. #define CHECK_SKB_FIELD(field) \
  677. BUILD_BUG_ON(offsetof(struct sk_buff, field) < \
  678. offsetof(struct sk_buff, headers_start)); \
  679. BUILD_BUG_ON(offsetof(struct sk_buff, field) > \
  680. offsetof(struct sk_buff, headers_end)); \
  681. static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
  682. {
  683. new->tstamp = old->tstamp;
  684. /* We do not copy old->sk */
  685. new->dev = old->dev;
  686. memcpy(new->cb, old->cb, sizeof(old->cb));
  687. skb_dst_copy(new, old);
  688. #ifdef CONFIG_XFRM
  689. new->sp = secpath_get(old->sp);
  690. #endif
  691. __nf_copy(new, old, false);
  692. /* Note : this field could be in headers_start/headers_end section
  693. * It is not yet because we do not want to have a 16 bit hole
  694. */
  695. new->queue_mapping = old->queue_mapping;
  696. memcpy(&new->headers_start, &old->headers_start,
  697. offsetof(struct sk_buff, headers_end) -
  698. offsetof(struct sk_buff, headers_start));
  699. CHECK_SKB_FIELD(protocol);
  700. CHECK_SKB_FIELD(csum);
  701. CHECK_SKB_FIELD(hash);
  702. CHECK_SKB_FIELD(priority);
  703. CHECK_SKB_FIELD(skb_iif);
  704. CHECK_SKB_FIELD(vlan_proto);
  705. CHECK_SKB_FIELD(vlan_tci);
  706. CHECK_SKB_FIELD(transport_header);
  707. CHECK_SKB_FIELD(network_header);
  708. CHECK_SKB_FIELD(mac_header);
  709. CHECK_SKB_FIELD(inner_protocol);
  710. CHECK_SKB_FIELD(inner_transport_header);
  711. CHECK_SKB_FIELD(inner_network_header);
  712. CHECK_SKB_FIELD(inner_mac_header);
  713. CHECK_SKB_FIELD(mark);
  714. #ifdef CONFIG_NETWORK_SECMARK
  715. CHECK_SKB_FIELD(secmark);
  716. #endif
  717. #ifdef CONFIG_NET_RX_BUSY_POLL
  718. CHECK_SKB_FIELD(napi_id);
  719. #endif
  720. #ifdef CONFIG_NET_SCHED
  721. CHECK_SKB_FIELD(tc_index);
  722. #ifdef CONFIG_NET_CLS_ACT
  723. CHECK_SKB_FIELD(tc_verd);
  724. #endif
  725. #endif
  726. }
  727. /*
  728. * You should not add any new code to this function. Add it to
  729. * __copy_skb_header above instead.
  730. */
  731. static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
  732. {
  733. #define C(x) n->x = skb->x
  734. n->next = n->prev = NULL;
  735. n->sk = NULL;
  736. __copy_skb_header(n, skb);
  737. C(len);
  738. C(data_len);
  739. C(mac_len);
  740. n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
  741. n->cloned = 1;
  742. n->nohdr = 0;
  743. n->destructor = NULL;
  744. C(tail);
  745. C(end);
  746. C(head);
  747. C(head_frag);
  748. C(data);
  749. C(truesize);
  750. atomic_set(&n->users, 1);
  751. atomic_inc(&(skb_shinfo(skb)->dataref));
  752. skb->cloned = 1;
  753. return n;
  754. #undef C
  755. }
  756. /**
  757. * skb_morph - morph one skb into another
  758. * @dst: the skb to receive the contents
  759. * @src: the skb to supply the contents
  760. *
  761. * This is identical to skb_clone except that the target skb is
  762. * supplied by the user.
  763. *
  764. * The target skb is returned upon exit.
  765. */
  766. struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
  767. {
  768. skb_release_all(dst);
  769. return __skb_clone(dst, src);
  770. }
  771. EXPORT_SYMBOL_GPL(skb_morph);
  772. /**
  773. * skb_copy_ubufs - copy userspace skb frags buffers to kernel
  774. * @skb: the skb to modify
  775. * @gfp_mask: allocation priority
  776. *
  777. * This must be called on SKBTX_DEV_ZEROCOPY skb.
  778. * It will copy all frags into kernel and drop the reference
  779. * to userspace pages.
  780. *
  781. * If this function is called from an interrupt gfp_mask() must be
  782. * %GFP_ATOMIC.
  783. *
  784. * Returns 0 on success or a negative error code on failure
  785. * to allocate kernel memory to copy to.
  786. */
  787. int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
  788. {
  789. int i;
  790. int num_frags = skb_shinfo(skb)->nr_frags;
  791. struct page *page, *head = NULL;
  792. struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg;
  793. for (i = 0; i < num_frags; i++) {
  794. u8 *vaddr;
  795. skb_frag_t *f = &skb_shinfo(skb)->frags[i];
  796. page = alloc_page(gfp_mask);
  797. if (!page) {
  798. while (head) {
  799. struct page *next = (struct page *)page_private(head);
  800. put_page(head);
  801. head = next;
  802. }
  803. return -ENOMEM;
  804. }
  805. vaddr = kmap_atomic(skb_frag_page(f));
  806. memcpy(page_address(page),
  807. vaddr + f->page_offset, skb_frag_size(f));
  808. kunmap_atomic(vaddr);
  809. set_page_private(page, (unsigned long)head);
  810. head = page;
  811. }
  812. /* skb frags release userspace buffers */
  813. for (i = 0; i < num_frags; i++)
  814. skb_frag_unref(skb, i);
  815. uarg->callback(uarg, false);
  816. /* skb frags point to kernel buffers */
  817. for (i = num_frags - 1; i >= 0; i--) {
  818. __skb_fill_page_desc(skb, i, head, 0,
  819. skb_shinfo(skb)->frags[i].size);
  820. head = (struct page *)page_private(head);
  821. }
  822. skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
  823. return 0;
  824. }
  825. EXPORT_SYMBOL_GPL(skb_copy_ubufs);
  826. /**
  827. * skb_clone - duplicate an sk_buff
  828. * @skb: buffer to clone
  829. * @gfp_mask: allocation priority
  830. *
  831. * Duplicate an &sk_buff. The new one is not owned by a socket. Both
  832. * copies share the same packet data but not structure. The new
  833. * buffer has a reference count of 1. If the allocation fails the
  834. * function returns %NULL otherwise the new buffer is returned.
  835. *
  836. * If this function is called from an interrupt gfp_mask() must be
  837. * %GFP_ATOMIC.
  838. */
  839. struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
  840. {
  841. struct sk_buff_fclones *fclones = container_of(skb,
  842. struct sk_buff_fclones,
  843. skb1);
  844. struct sk_buff *n;
  845. if (skb_orphan_frags(skb, gfp_mask))
  846. return NULL;
  847. if (skb->fclone == SKB_FCLONE_ORIG &&
  848. atomic_read(&fclones->fclone_ref) == 1) {
  849. n = &fclones->skb2;
  850. atomic_set(&fclones->fclone_ref, 2);
  851. } else {
  852. if (skb_pfmemalloc(skb))
  853. gfp_mask |= __GFP_MEMALLOC;
  854. n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
  855. if (!n)
  856. return NULL;
  857. kmemcheck_annotate_bitfield(n, flags1);
  858. n->fclone = SKB_FCLONE_UNAVAILABLE;
  859. }
  860. return __skb_clone(n, skb);
  861. }
  862. EXPORT_SYMBOL(skb_clone);
  863. static void skb_headers_offset_update(struct sk_buff *skb, int off)
  864. {
  865. /* Only adjust this if it actually is csum_start rather than csum */
  866. if (skb->ip_summed == CHECKSUM_PARTIAL)
  867. skb->csum_start += off;
  868. /* {transport,network,mac}_header and tail are relative to skb->head */
  869. skb->transport_header += off;
  870. skb->network_header += off;
  871. if (skb_mac_header_was_set(skb))
  872. skb->mac_header += off;
  873. skb->inner_transport_header += off;
  874. skb->inner_network_header += off;
  875. skb->inner_mac_header += off;
  876. }
  877. static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
  878. {
  879. __copy_skb_header(new, old);
  880. skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
  881. skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
  882. skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
  883. }
  884. static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
  885. {
  886. if (skb_pfmemalloc(skb))
  887. return SKB_ALLOC_RX;
  888. return 0;
  889. }
  890. /**
  891. * skb_copy - create private copy of an sk_buff
  892. * @skb: buffer to copy
  893. * @gfp_mask: allocation priority
  894. *
  895. * Make a copy of both an &sk_buff and its data. This is used when the
  896. * caller wishes to modify the data and needs a private copy of the
  897. * data to alter. Returns %NULL on failure or the pointer to the buffer
  898. * on success. The returned buffer has a reference count of 1.
  899. *
  900. * As by-product this function converts non-linear &sk_buff to linear
  901. * one, so that &sk_buff becomes completely private and caller is allowed
  902. * to modify all the data of returned buffer. This means that this
  903. * function is not recommended for use in circumstances when only
  904. * header is going to be modified. Use pskb_copy() instead.
  905. */
  906. struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
  907. {
  908. int headerlen = skb_headroom(skb);
  909. unsigned int size = skb_end_offset(skb) + skb->data_len;
  910. struct sk_buff *n = __alloc_skb(size, gfp_mask,
  911. skb_alloc_rx_flag(skb), NUMA_NO_NODE);
  912. if (!n)
  913. return NULL;
  914. /* Set the data pointer */
  915. skb_reserve(n, headerlen);
  916. /* Set the tail pointer and length */
  917. skb_put(n, skb->len);
  918. if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
  919. BUG();
  920. copy_skb_header(n, skb);
  921. return n;
  922. }
  923. EXPORT_SYMBOL(skb_copy);
  924. /**
  925. * __pskb_copy_fclone - create copy of an sk_buff with private head.
  926. * @skb: buffer to copy
  927. * @headroom: headroom of new skb
  928. * @gfp_mask: allocation priority
  929. * @fclone: if true allocate the copy of the skb from the fclone
  930. * cache instead of the head cache; it is recommended to set this
  931. * to true for the cases where the copy will likely be cloned
  932. *
  933. * Make a copy of both an &sk_buff and part of its data, located
  934. * in header. Fragmented data remain shared. This is used when
  935. * the caller wishes to modify only header of &sk_buff and needs
  936. * private copy of the header to alter. Returns %NULL on failure
  937. * or the pointer to the buffer on success.
  938. * The returned buffer has a reference count of 1.
  939. */
  940. struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
  941. gfp_t gfp_mask, bool fclone)
  942. {
  943. unsigned int size = skb_headlen(skb) + headroom;
  944. int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
  945. struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
  946. if (!n)
  947. goto out;
  948. /* Set the data pointer */
  949. skb_reserve(n, headroom);
  950. /* Set the tail pointer and length */
  951. skb_put(n, skb_headlen(skb));
  952. /* Copy the bytes */
  953. skb_copy_from_linear_data(skb, n->data, n->len);
  954. n->truesize += skb->data_len;
  955. n->data_len = skb->data_len;
  956. n->len = skb->len;
  957. if (skb_shinfo(skb)->nr_frags) {
  958. int i;
  959. if (skb_orphan_frags(skb, gfp_mask)) {
  960. kfree_skb(n);
  961. n = NULL;
  962. goto out;
  963. }
  964. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  965. skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
  966. skb_frag_ref(skb, i);
  967. }
  968. skb_shinfo(n)->nr_frags = i;
  969. }
  970. if (skb_has_frag_list(skb)) {
  971. skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
  972. skb_clone_fraglist(n);
  973. }
  974. copy_skb_header(n, skb);
  975. out:
  976. return n;
  977. }
  978. EXPORT_SYMBOL(__pskb_copy_fclone);
  979. /**
  980. * pskb_expand_head - reallocate header of &sk_buff
  981. * @skb: buffer to reallocate
  982. * @nhead: room to add at head
  983. * @ntail: room to add at tail
  984. * @gfp_mask: allocation priority
  985. *
  986. * Expands (or creates identical copy, if @nhead and @ntail are zero)
  987. * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
  988. * reference count of 1. Returns zero in the case of success or error,
  989. * if expansion failed. In the last case, &sk_buff is not changed.
  990. *
  991. * All the pointers pointing into skb header may change and must be
  992. * reloaded after call to this function.
  993. */
  994. int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
  995. gfp_t gfp_mask)
  996. {
  997. int i;
  998. u8 *data;
  999. int size = nhead + skb_end_offset(skb) + ntail;
  1000. long off;
  1001. BUG_ON(nhead < 0);
  1002. if (skb_shared(skb))
  1003. BUG();
  1004. size = SKB_DATA_ALIGN(size);
  1005. if (skb_pfmemalloc(skb))
  1006. gfp_mask |= __GFP_MEMALLOC;
  1007. data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
  1008. gfp_mask, NUMA_NO_NODE, NULL);
  1009. if (!data)
  1010. goto nodata;
  1011. size = SKB_WITH_OVERHEAD(ksize(data));
  1012. /* Copy only real data... and, alas, header. This should be
  1013. * optimized for the cases when header is void.
  1014. */
  1015. memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
  1016. memcpy((struct skb_shared_info *)(data + size),
  1017. skb_shinfo(skb),
  1018. offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
  1019. /*
  1020. * if shinfo is shared we must drop the old head gracefully, but if it
  1021. * is not we can just drop the old head and let the existing refcount
  1022. * be since all we did is relocate the values
  1023. */
  1024. if (skb_cloned(skb)) {
  1025. /* copy this zero copy skb frags */
  1026. if (skb_orphan_frags(skb, gfp_mask))
  1027. goto nofrags;
  1028. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
  1029. skb_frag_ref(skb, i);
  1030. if (skb_has_frag_list(skb))
  1031. skb_clone_fraglist(skb);
  1032. skb_release_data(skb);
  1033. } else {
  1034. skb_free_head(skb);
  1035. }
  1036. off = (data + nhead) - skb->head;
  1037. skb->head = data;
  1038. skb->head_frag = 0;
  1039. skb->data += off;
  1040. #ifdef NET_SKBUFF_DATA_USES_OFFSET
  1041. skb->end = size;
  1042. off = nhead;
  1043. #else
  1044. skb->end = skb->head + size;
  1045. #endif
  1046. skb->tail += off;
  1047. skb_headers_offset_update(skb, nhead);
  1048. skb->cloned = 0;
  1049. skb->hdr_len = 0;
  1050. skb->nohdr = 0;
  1051. atomic_set(&skb_shinfo(skb)->dataref, 1);
  1052. return 0;
  1053. nofrags:
  1054. kfree(data);
  1055. nodata:
  1056. return -ENOMEM;
  1057. }
  1058. EXPORT_SYMBOL(pskb_expand_head);
  1059. /* Make private copy of skb with writable head and some headroom */
  1060. struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
  1061. {
  1062. struct sk_buff *skb2;
  1063. int delta = headroom - skb_headroom(skb);
  1064. if (delta <= 0)
  1065. skb2 = pskb_copy(skb, GFP_ATOMIC);
  1066. else {
  1067. skb2 = skb_clone(skb, GFP_ATOMIC);
  1068. if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
  1069. GFP_ATOMIC)) {
  1070. kfree_skb(skb2);
  1071. skb2 = NULL;
  1072. }
  1073. }
  1074. return skb2;
  1075. }
  1076. EXPORT_SYMBOL(skb_realloc_headroom);
  1077. /**
  1078. * skb_copy_expand - copy and expand sk_buff
  1079. * @skb: buffer to copy
  1080. * @newheadroom: new free bytes at head
  1081. * @newtailroom: new free bytes at tail
  1082. * @gfp_mask: allocation priority
  1083. *
  1084. * Make a copy of both an &sk_buff and its data and while doing so
  1085. * allocate additional space.
  1086. *
  1087. * This is used when the caller wishes to modify the data and needs a
  1088. * private copy of the data to alter as well as more space for new fields.
  1089. * Returns %NULL on failure or the pointer to the buffer
  1090. * on success. The returned buffer has a reference count of 1.
  1091. *
  1092. * You must pass %GFP_ATOMIC as the allocation priority if this function
  1093. * is called from an interrupt.
  1094. */
  1095. struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
  1096. int newheadroom, int newtailroom,
  1097. gfp_t gfp_mask)
  1098. {
  1099. /*
  1100. * Allocate the copy buffer
  1101. */
  1102. struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
  1103. gfp_mask, skb_alloc_rx_flag(skb),
  1104. NUMA_NO_NODE);
  1105. int oldheadroom = skb_headroom(skb);
  1106. int head_copy_len, head_copy_off;
  1107. if (!n)
  1108. return NULL;
  1109. skb_reserve(n, newheadroom);
  1110. /* Set the tail pointer and length */
  1111. skb_put(n, skb->len);
  1112. head_copy_len = oldheadroom;
  1113. head_copy_off = 0;
  1114. if (newheadroom <= head_copy_len)
  1115. head_copy_len = newheadroom;
  1116. else
  1117. head_copy_off = newheadroom - head_copy_len;
  1118. /* Copy the linear header and data. */
  1119. if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
  1120. skb->len + head_copy_len))
  1121. BUG();
  1122. copy_skb_header(n, skb);
  1123. skb_headers_offset_update(n, newheadroom - oldheadroom);
  1124. return n;
  1125. }
  1126. EXPORT_SYMBOL(skb_copy_expand);
  1127. /**
  1128. * skb_pad - zero pad the tail of an skb
  1129. * @skb: buffer to pad
  1130. * @pad: space to pad
  1131. *
  1132. * Ensure that a buffer is followed by a padding area that is zero
  1133. * filled. Used by network drivers which may DMA or transfer data
  1134. * beyond the buffer end onto the wire.
  1135. *
  1136. * May return error in out of memory cases. The skb is freed on error.
  1137. */
  1138. int skb_pad(struct sk_buff *skb, int pad)
  1139. {
  1140. int err;
  1141. int ntail;
  1142. /* If the skbuff is non linear tailroom is always zero.. */
  1143. if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
  1144. memset(skb->data+skb->len, 0, pad);
  1145. return 0;
  1146. }
  1147. ntail = skb->data_len + pad - (skb->end - skb->tail);
  1148. if (likely(skb_cloned(skb) || ntail > 0)) {
  1149. err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
  1150. if (unlikely(err))
  1151. goto free_skb;
  1152. }
  1153. /* FIXME: The use of this function with non-linear skb's really needs
  1154. * to be audited.
  1155. */
  1156. err = skb_linearize(skb);
  1157. if (unlikely(err))
  1158. goto free_skb;
  1159. memset(skb->data + skb->len, 0, pad);
  1160. return 0;
  1161. free_skb:
  1162. kfree_skb(skb);
  1163. return err;
  1164. }
  1165. EXPORT_SYMBOL(skb_pad);
  1166. /**
  1167. * pskb_put - add data to the tail of a potentially fragmented buffer
  1168. * @skb: start of the buffer to use
  1169. * @tail: tail fragment of the buffer to use
  1170. * @len: amount of data to add
  1171. *
  1172. * This function extends the used data area of the potentially
  1173. * fragmented buffer. @tail must be the last fragment of @skb -- or
  1174. * @skb itself. If this would exceed the total buffer size the kernel
  1175. * will panic. A pointer to the first byte of the extra data is
  1176. * returned.
  1177. */
  1178. unsigned char *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
  1179. {
  1180. if (tail != skb) {
  1181. skb->data_len += len;
  1182. skb->len += len;
  1183. }
  1184. return skb_put(tail, len);
  1185. }
  1186. EXPORT_SYMBOL_GPL(pskb_put);
  1187. /**
  1188. * skb_put - add data to a buffer
  1189. * @skb: buffer to use
  1190. * @len: amount of data to add
  1191. *
  1192. * This function extends the used data area of the buffer. If this would
  1193. * exceed the total buffer size the kernel will panic. A pointer to the
  1194. * first byte of the extra data is returned.
  1195. */
  1196. unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
  1197. {
  1198. unsigned char *tmp = skb_tail_pointer(skb);
  1199. SKB_LINEAR_ASSERT(skb);
  1200. skb->tail += len;
  1201. skb->len += len;
  1202. if (unlikely(skb->tail > skb->end))
  1203. skb_over_panic(skb, len, __builtin_return_address(0));
  1204. return tmp;
  1205. }
  1206. EXPORT_SYMBOL(skb_put);
  1207. /**
  1208. * skb_push - add data to the start of a buffer
  1209. * @skb: buffer to use
  1210. * @len: amount of data to add
  1211. *
  1212. * This function extends the used data area of the buffer at the buffer
  1213. * start. If this would exceed the total buffer headroom the kernel will
  1214. * panic. A pointer to the first byte of the extra data is returned.
  1215. */
  1216. unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
  1217. {
  1218. skb->data -= len;
  1219. skb->len += len;
  1220. if (unlikely(skb->data<skb->head))
  1221. skb_under_panic(skb, len, __builtin_return_address(0));
  1222. return skb->data;
  1223. }
  1224. EXPORT_SYMBOL(skb_push);
  1225. /**
  1226. * skb_pull - remove data from the start of a buffer
  1227. * @skb: buffer to use
  1228. * @len: amount of data to remove
  1229. *
  1230. * This function removes data from the start of a buffer, returning
  1231. * the memory to the headroom. A pointer to the next data in the buffer
  1232. * is returned. Once the data has been pulled future pushes will overwrite
  1233. * the old data.
  1234. */
  1235. unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
  1236. {
  1237. return skb_pull_inline(skb, len);
  1238. }
  1239. EXPORT_SYMBOL(skb_pull);
  1240. /**
  1241. * skb_trim - remove end from a buffer
  1242. * @skb: buffer to alter
  1243. * @len: new length
  1244. *
  1245. * Cut the length of a buffer down by removing data from the tail. If
  1246. * the buffer is already under the length specified it is not modified.
  1247. * The skb must be linear.
  1248. */
  1249. void skb_trim(struct sk_buff *skb, unsigned int len)
  1250. {
  1251. if (skb->len > len)
  1252. __skb_trim(skb, len);
  1253. }
  1254. EXPORT_SYMBOL(skb_trim);
  1255. /* Trims skb to length len. It can change skb pointers.
  1256. */
  1257. int ___pskb_trim(struct sk_buff *skb, unsigned int len)
  1258. {
  1259. struct sk_buff **fragp;
  1260. struct sk_buff *frag;
  1261. int offset = skb_headlen(skb);
  1262. int nfrags = skb_shinfo(skb)->nr_frags;
  1263. int i;
  1264. int err;
  1265. if (skb_cloned(skb) &&
  1266. unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
  1267. return err;
  1268. i = 0;
  1269. if (offset >= len)
  1270. goto drop_pages;
  1271. for (; i < nfrags; i++) {
  1272. int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
  1273. if (end < len) {
  1274. offset = end;
  1275. continue;
  1276. }
  1277. skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
  1278. drop_pages:
  1279. skb_shinfo(skb)->nr_frags = i;
  1280. for (; i < nfrags; i++)
  1281. skb_frag_unref(skb, i);
  1282. if (skb_has_frag_list(skb))
  1283. skb_drop_fraglist(skb);
  1284. goto done;
  1285. }
  1286. for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
  1287. fragp = &frag->next) {
  1288. int end = offset + frag->len;
  1289. if (skb_shared(frag)) {
  1290. struct sk_buff *nfrag;
  1291. nfrag = skb_clone(frag, GFP_ATOMIC);
  1292. if (unlikely(!nfrag))
  1293. return -ENOMEM;
  1294. nfrag->next = frag->next;
  1295. consume_skb(frag);
  1296. frag = nfrag;
  1297. *fragp = frag;
  1298. }
  1299. if (end < len) {
  1300. offset = end;
  1301. continue;
  1302. }
  1303. if (end > len &&
  1304. unlikely((err = pskb_trim(frag, len - offset))))
  1305. return err;
  1306. if (frag->next)
  1307. skb_drop_list(&frag->next);
  1308. break;
  1309. }
  1310. done:
  1311. if (len > skb_headlen(skb)) {
  1312. skb->data_len -= skb->len - len;
  1313. skb->len = len;
  1314. } else {
  1315. skb->len = len;
  1316. skb->data_len = 0;
  1317. skb_set_tail_pointer(skb, len);
  1318. }
  1319. return 0;
  1320. }
  1321. EXPORT_SYMBOL(___pskb_trim);
  1322. /**
  1323. * __pskb_pull_tail - advance tail of skb header
  1324. * @skb: buffer to reallocate
  1325. * @delta: number of bytes to advance tail
  1326. *
  1327. * The function makes a sense only on a fragmented &sk_buff,
  1328. * it expands header moving its tail forward and copying necessary
  1329. * data from fragmented part.
  1330. *
  1331. * &sk_buff MUST have reference count of 1.
  1332. *
  1333. * Returns %NULL (and &sk_buff does not change) if pull failed
  1334. * or value of new tail of skb in the case of success.
  1335. *
  1336. * All the pointers pointing into skb header may change and must be
  1337. * reloaded after call to this function.
  1338. */
  1339. /* Moves tail of skb head forward, copying data from fragmented part,
  1340. * when it is necessary.
  1341. * 1. It may fail due to malloc failure.
  1342. * 2. It may change skb pointers.
  1343. *
  1344. * It is pretty complicated. Luckily, it is called only in exceptional cases.
  1345. */
  1346. unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
  1347. {
  1348. /* If skb has not enough free space at tail, get new one
  1349. * plus 128 bytes for future expansions. If we have enough
  1350. * room at tail, reallocate without expansion only if skb is cloned.
  1351. */
  1352. int i, k, eat = (skb->tail + delta) - skb->end;
  1353. if (eat > 0 || skb_cloned(skb)) {
  1354. if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
  1355. GFP_ATOMIC))
  1356. return NULL;
  1357. }
  1358. if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
  1359. BUG();
  1360. /* Optimization: no fragments, no reasons to preestimate
  1361. * size of pulled pages. Superb.
  1362. */
  1363. if (!skb_has_frag_list(skb))
  1364. goto pull_pages;
  1365. /* Estimate size of pulled pages. */
  1366. eat = delta;
  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. goto pull_pages;
  1371. eat -= size;
  1372. }
  1373. /* If we need update frag list, we are in troubles.
  1374. * Certainly, it possible to add an offset to skb data,
  1375. * but taking into account that pulling is expected to
  1376. * be very rare operation, it is worth to fight against
  1377. * further bloating skb head and crucify ourselves here instead.
  1378. * Pure masohism, indeed. 8)8)
  1379. */
  1380. if (eat) {
  1381. struct sk_buff *list = skb_shinfo(skb)->frag_list;
  1382. struct sk_buff *clone = NULL;
  1383. struct sk_buff *insp = NULL;
  1384. do {
  1385. BUG_ON(!list);
  1386. if (list->len <= eat) {
  1387. /* Eaten as whole. */
  1388. eat -= list->len;
  1389. list = list->next;
  1390. insp = list;
  1391. } else {
  1392. /* Eaten partially. */
  1393. if (skb_shared(list)) {
  1394. /* Sucks! We need to fork list. :-( */
  1395. clone = skb_clone(list, GFP_ATOMIC);
  1396. if (!clone)
  1397. return NULL;
  1398. insp = list->next;
  1399. list = clone;
  1400. } else {
  1401. /* This may be pulled without
  1402. * problems. */
  1403. insp = list;
  1404. }
  1405. if (!pskb_pull(list, eat)) {
  1406. kfree_skb(clone);
  1407. return NULL;
  1408. }
  1409. break;
  1410. }
  1411. } while (eat);
  1412. /* Free pulled out fragments. */
  1413. while ((list = skb_shinfo(skb)->frag_list) != insp) {
  1414. skb_shinfo(skb)->frag_list = list->next;
  1415. kfree_skb(list);
  1416. }
  1417. /* And insert new clone at head. */
  1418. if (clone) {
  1419. clone->next = list;
  1420. skb_shinfo(skb)->frag_list = clone;
  1421. }
  1422. }
  1423. /* Success! Now we may commit changes to skb data. */
  1424. pull_pages:
  1425. eat = delta;
  1426. k = 0;
  1427. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1428. int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
  1429. if (size <= eat) {
  1430. skb_frag_unref(skb, i);
  1431. eat -= size;
  1432. } else {
  1433. skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
  1434. if (eat) {
  1435. skb_shinfo(skb)->frags[k].page_offset += eat;
  1436. skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
  1437. eat = 0;
  1438. }
  1439. k++;
  1440. }
  1441. }
  1442. skb_shinfo(skb)->nr_frags = k;
  1443. skb->tail += delta;
  1444. skb->data_len -= delta;
  1445. return skb_tail_pointer(skb);
  1446. }
  1447. EXPORT_SYMBOL(__pskb_pull_tail);
  1448. /**
  1449. * skb_copy_bits - copy bits from skb to kernel buffer
  1450. * @skb: source skb
  1451. * @offset: offset in source
  1452. * @to: destination buffer
  1453. * @len: number of bytes to copy
  1454. *
  1455. * Copy the specified number of bytes from the source skb to the
  1456. * destination buffer.
  1457. *
  1458. * CAUTION ! :
  1459. * If its prototype is ever changed,
  1460. * check arch/{*}/net/{*}.S files,
  1461. * since it is called from BPF assembly code.
  1462. */
  1463. int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
  1464. {
  1465. int start = skb_headlen(skb);
  1466. struct sk_buff *frag_iter;
  1467. int i, copy;
  1468. if (offset > (int)skb->len - len)
  1469. goto fault;
  1470. /* Copy header. */
  1471. if ((copy = start - offset) > 0) {
  1472. if (copy > len)
  1473. copy = len;
  1474. skb_copy_from_linear_data_offset(skb, offset, to, copy);
  1475. if ((len -= copy) == 0)
  1476. return 0;
  1477. offset += copy;
  1478. to += copy;
  1479. }
  1480. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1481. int end;
  1482. skb_frag_t *f = &skb_shinfo(skb)->frags[i];
  1483. WARN_ON(start > offset + len);
  1484. end = start + skb_frag_size(f);
  1485. if ((copy = end - offset) > 0) {
  1486. u8 *vaddr;
  1487. if (copy > len)
  1488. copy = len;
  1489. vaddr = kmap_atomic(skb_frag_page(f));
  1490. memcpy(to,
  1491. vaddr + f->page_offset + offset - start,
  1492. copy);
  1493. kunmap_atomic(vaddr);
  1494. if ((len -= copy) == 0)
  1495. return 0;
  1496. offset += copy;
  1497. to += copy;
  1498. }
  1499. start = end;
  1500. }
  1501. skb_walk_frags(skb, frag_iter) {
  1502. int end;
  1503. WARN_ON(start > offset + len);
  1504. end = start + frag_iter->len;
  1505. if ((copy = end - offset) > 0) {
  1506. if (copy > len)
  1507. copy = len;
  1508. if (skb_copy_bits(frag_iter, offset - start, to, copy))
  1509. goto fault;
  1510. if ((len -= copy) == 0)
  1511. return 0;
  1512. offset += copy;
  1513. to += copy;
  1514. }
  1515. start = end;
  1516. }
  1517. if (!len)
  1518. return 0;
  1519. fault:
  1520. return -EFAULT;
  1521. }
  1522. EXPORT_SYMBOL(skb_copy_bits);
  1523. /*
  1524. * Callback from splice_to_pipe(), if we need to release some pages
  1525. * at the end of the spd in case we error'ed out in filling the pipe.
  1526. */
  1527. static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
  1528. {
  1529. put_page(spd->pages[i]);
  1530. }
  1531. static struct page *linear_to_page(struct page *page, unsigned int *len,
  1532. unsigned int *offset,
  1533. struct sock *sk)
  1534. {
  1535. struct page_frag *pfrag = sk_page_frag(sk);
  1536. if (!sk_page_frag_refill(sk, pfrag))
  1537. return NULL;
  1538. *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
  1539. memcpy(page_address(pfrag->page) + pfrag->offset,
  1540. page_address(page) + *offset, *len);
  1541. *offset = pfrag->offset;
  1542. pfrag->offset += *len;
  1543. return pfrag->page;
  1544. }
  1545. static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
  1546. struct page *page,
  1547. unsigned int offset)
  1548. {
  1549. return spd->nr_pages &&
  1550. spd->pages[spd->nr_pages - 1] == page &&
  1551. (spd->partial[spd->nr_pages - 1].offset +
  1552. spd->partial[spd->nr_pages - 1].len == offset);
  1553. }
  1554. /*
  1555. * Fill page/offset/length into spd, if it can hold more pages.
  1556. */
  1557. static bool spd_fill_page(struct splice_pipe_desc *spd,
  1558. struct pipe_inode_info *pipe, struct page *page,
  1559. unsigned int *len, unsigned int offset,
  1560. bool linear,
  1561. struct sock *sk)
  1562. {
  1563. if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
  1564. return true;
  1565. if (linear) {
  1566. page = linear_to_page(page, len, &offset, sk);
  1567. if (!page)
  1568. return true;
  1569. }
  1570. if (spd_can_coalesce(spd, page, offset)) {
  1571. spd->partial[spd->nr_pages - 1].len += *len;
  1572. return false;
  1573. }
  1574. get_page(page);
  1575. spd->pages[spd->nr_pages] = page;
  1576. spd->partial[spd->nr_pages].len = *len;
  1577. spd->partial[spd->nr_pages].offset = offset;
  1578. spd->nr_pages++;
  1579. return false;
  1580. }
  1581. static bool __splice_segment(struct page *page, unsigned int poff,
  1582. unsigned int plen, unsigned int *off,
  1583. unsigned int *len,
  1584. struct splice_pipe_desc *spd, bool linear,
  1585. struct sock *sk,
  1586. struct pipe_inode_info *pipe)
  1587. {
  1588. if (!*len)
  1589. return true;
  1590. /* skip this segment if already processed */
  1591. if (*off >= plen) {
  1592. *off -= plen;
  1593. return false;
  1594. }
  1595. /* ignore any bits we already processed */
  1596. poff += *off;
  1597. plen -= *off;
  1598. *off = 0;
  1599. do {
  1600. unsigned int flen = min(*len, plen);
  1601. if (spd_fill_page(spd, pipe, page, &flen, poff,
  1602. linear, sk))
  1603. return true;
  1604. poff += flen;
  1605. plen -= flen;
  1606. *len -= flen;
  1607. } while (*len && plen);
  1608. return false;
  1609. }
  1610. /*
  1611. * Map linear and fragment data from the skb to spd. It reports true if the
  1612. * pipe is full or if we already spliced the requested length.
  1613. */
  1614. static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
  1615. unsigned int *offset, unsigned int *len,
  1616. struct splice_pipe_desc *spd, struct sock *sk)
  1617. {
  1618. int seg;
  1619. /* map the linear part :
  1620. * If skb->head_frag is set, this 'linear' part is backed by a
  1621. * fragment, and if the head is not shared with any clones then
  1622. * we can avoid a copy since we own the head portion of this page.
  1623. */
  1624. if (__splice_segment(virt_to_page(skb->data),
  1625. (unsigned long) skb->data & (PAGE_SIZE - 1),
  1626. skb_headlen(skb),
  1627. offset, len, spd,
  1628. skb_head_is_locked(skb),
  1629. sk, pipe))
  1630. return true;
  1631. /*
  1632. * then map the fragments
  1633. */
  1634. for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
  1635. const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
  1636. if (__splice_segment(skb_frag_page(f),
  1637. f->page_offset, skb_frag_size(f),
  1638. offset, len, spd, false, sk, pipe))
  1639. return true;
  1640. }
  1641. return false;
  1642. }
  1643. /*
  1644. * Map data from the skb to a pipe. Should handle both the linear part,
  1645. * the fragments, and the frag list. It does NOT handle frag lists within
  1646. * the frag list, if such a thing exists. We'd probably need to recurse to
  1647. * handle that cleanly.
  1648. */
  1649. int skb_splice_bits(struct sk_buff *skb, unsigned int offset,
  1650. struct pipe_inode_info *pipe, unsigned int tlen,
  1651. unsigned int flags)
  1652. {
  1653. struct partial_page partial[MAX_SKB_FRAGS];
  1654. struct page *pages[MAX_SKB_FRAGS];
  1655. struct splice_pipe_desc spd = {
  1656. .pages = pages,
  1657. .partial = partial,
  1658. .nr_pages_max = MAX_SKB_FRAGS,
  1659. .flags = flags,
  1660. .ops = &nosteal_pipe_buf_ops,
  1661. .spd_release = sock_spd_release,
  1662. };
  1663. struct sk_buff *frag_iter;
  1664. struct sock *sk = skb->sk;
  1665. int ret = 0;
  1666. /*
  1667. * __skb_splice_bits() only fails if the output has no room left,
  1668. * so no point in going over the frag_list for the error case.
  1669. */
  1670. if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk))
  1671. goto done;
  1672. else if (!tlen)
  1673. goto done;
  1674. /*
  1675. * now see if we have a frag_list to map
  1676. */
  1677. skb_walk_frags(skb, frag_iter) {
  1678. if (!tlen)
  1679. break;
  1680. if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk))
  1681. break;
  1682. }
  1683. done:
  1684. if (spd.nr_pages) {
  1685. /*
  1686. * Drop the socket lock, otherwise we have reverse
  1687. * locking dependencies between sk_lock and i_mutex
  1688. * here as compared to sendfile(). We enter here
  1689. * with the socket lock held, and splice_to_pipe() will
  1690. * grab the pipe inode lock. For sendfile() emulation,
  1691. * we call into ->sendpage() with the i_mutex lock held
  1692. * and networking will grab the socket lock.
  1693. */
  1694. release_sock(sk);
  1695. ret = splice_to_pipe(pipe, &spd);
  1696. lock_sock(sk);
  1697. }
  1698. return ret;
  1699. }
  1700. /**
  1701. * skb_store_bits - store bits from kernel buffer to skb
  1702. * @skb: destination buffer
  1703. * @offset: offset in destination
  1704. * @from: source buffer
  1705. * @len: number of bytes to copy
  1706. *
  1707. * Copy the specified number of bytes from the source buffer to the
  1708. * destination skb. This function handles all the messy bits of
  1709. * traversing fragment lists and such.
  1710. */
  1711. int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
  1712. {
  1713. int start = skb_headlen(skb);
  1714. struct sk_buff *frag_iter;
  1715. int i, copy;
  1716. if (offset > (int)skb->len - len)
  1717. goto fault;
  1718. if ((copy = start - offset) > 0) {
  1719. if (copy > len)
  1720. copy = len;
  1721. skb_copy_to_linear_data_offset(skb, offset, from, copy);
  1722. if ((len -= copy) == 0)
  1723. return 0;
  1724. offset += copy;
  1725. from += copy;
  1726. }
  1727. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1728. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  1729. int end;
  1730. WARN_ON(start > offset + len);
  1731. end = start + skb_frag_size(frag);
  1732. if ((copy = end - offset) > 0) {
  1733. u8 *vaddr;
  1734. if (copy > len)
  1735. copy = len;
  1736. vaddr = kmap_atomic(skb_frag_page(frag));
  1737. memcpy(vaddr + frag->page_offset + offset - start,
  1738. from, copy);
  1739. kunmap_atomic(vaddr);
  1740. if ((len -= copy) == 0)
  1741. return 0;
  1742. offset += copy;
  1743. from += copy;
  1744. }
  1745. start = end;
  1746. }
  1747. skb_walk_frags(skb, frag_iter) {
  1748. int end;
  1749. WARN_ON(start > offset + len);
  1750. end = start + frag_iter->len;
  1751. if ((copy = end - offset) > 0) {
  1752. if (copy > len)
  1753. copy = len;
  1754. if (skb_store_bits(frag_iter, offset - start,
  1755. from, copy))
  1756. goto fault;
  1757. if ((len -= copy) == 0)
  1758. return 0;
  1759. offset += copy;
  1760. from += copy;
  1761. }
  1762. start = end;
  1763. }
  1764. if (!len)
  1765. return 0;
  1766. fault:
  1767. return -EFAULT;
  1768. }
  1769. EXPORT_SYMBOL(skb_store_bits);
  1770. /* Checksum skb data. */
  1771. __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
  1772. __wsum csum, const struct skb_checksum_ops *ops)
  1773. {
  1774. int start = skb_headlen(skb);
  1775. int i, copy = start - offset;
  1776. struct sk_buff *frag_iter;
  1777. int pos = 0;
  1778. /* Checksum header. */
  1779. if (copy > 0) {
  1780. if (copy > len)
  1781. copy = len;
  1782. csum = ops->update(skb->data + offset, copy, csum);
  1783. if ((len -= copy) == 0)
  1784. return csum;
  1785. offset += copy;
  1786. pos = copy;
  1787. }
  1788. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1789. int end;
  1790. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  1791. WARN_ON(start > offset + len);
  1792. end = start + skb_frag_size(frag);
  1793. if ((copy = end - offset) > 0) {
  1794. __wsum csum2;
  1795. u8 *vaddr;
  1796. if (copy > len)
  1797. copy = len;
  1798. vaddr = kmap_atomic(skb_frag_page(frag));
  1799. csum2 = ops->update(vaddr + frag->page_offset +
  1800. offset - start, copy, 0);
  1801. kunmap_atomic(vaddr);
  1802. csum = ops->combine(csum, csum2, pos, copy);
  1803. if (!(len -= copy))
  1804. return csum;
  1805. offset += copy;
  1806. pos += copy;
  1807. }
  1808. start = end;
  1809. }
  1810. skb_walk_frags(skb, frag_iter) {
  1811. int end;
  1812. WARN_ON(start > offset + len);
  1813. end = start + frag_iter->len;
  1814. if ((copy = end - offset) > 0) {
  1815. __wsum csum2;
  1816. if (copy > len)
  1817. copy = len;
  1818. csum2 = __skb_checksum(frag_iter, offset - start,
  1819. copy, 0, ops);
  1820. csum = ops->combine(csum, csum2, pos, copy);
  1821. if ((len -= copy) == 0)
  1822. return csum;
  1823. offset += copy;
  1824. pos += copy;
  1825. }
  1826. start = end;
  1827. }
  1828. BUG_ON(len);
  1829. return csum;
  1830. }
  1831. EXPORT_SYMBOL(__skb_checksum);
  1832. __wsum skb_checksum(const struct sk_buff *skb, int offset,
  1833. int len, __wsum csum)
  1834. {
  1835. const struct skb_checksum_ops ops = {
  1836. .update = csum_partial_ext,
  1837. .combine = csum_block_add_ext,
  1838. };
  1839. return __skb_checksum(skb, offset, len, csum, &ops);
  1840. }
  1841. EXPORT_SYMBOL(skb_checksum);
  1842. /* Both of above in one bottle. */
  1843. __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
  1844. u8 *to, int len, __wsum csum)
  1845. {
  1846. int start = skb_headlen(skb);
  1847. int i, copy = start - offset;
  1848. struct sk_buff *frag_iter;
  1849. int pos = 0;
  1850. /* Copy header. */
  1851. if (copy > 0) {
  1852. if (copy > len)
  1853. copy = len;
  1854. csum = csum_partial_copy_nocheck(skb->data + offset, to,
  1855. copy, csum);
  1856. if ((len -= copy) == 0)
  1857. return csum;
  1858. offset += copy;
  1859. to += copy;
  1860. pos = copy;
  1861. }
  1862. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1863. int end;
  1864. WARN_ON(start > offset + len);
  1865. end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
  1866. if ((copy = end - offset) > 0) {
  1867. __wsum csum2;
  1868. u8 *vaddr;
  1869. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  1870. if (copy > len)
  1871. copy = len;
  1872. vaddr = kmap_atomic(skb_frag_page(frag));
  1873. csum2 = csum_partial_copy_nocheck(vaddr +
  1874. frag->page_offset +
  1875. offset - start, to,
  1876. copy, 0);
  1877. kunmap_atomic(vaddr);
  1878. csum = csum_block_add(csum, csum2, pos);
  1879. if (!(len -= copy))
  1880. return csum;
  1881. offset += copy;
  1882. to += copy;
  1883. pos += copy;
  1884. }
  1885. start = end;
  1886. }
  1887. skb_walk_frags(skb, frag_iter) {
  1888. __wsum csum2;
  1889. int end;
  1890. WARN_ON(start > offset + len);
  1891. end = start + frag_iter->len;
  1892. if ((copy = end - offset) > 0) {
  1893. if (copy > len)
  1894. copy = len;
  1895. csum2 = skb_copy_and_csum_bits(frag_iter,
  1896. offset - start,
  1897. to, copy, 0);
  1898. csum = csum_block_add(csum, csum2, pos);
  1899. if ((len -= copy) == 0)
  1900. return csum;
  1901. offset += copy;
  1902. to += copy;
  1903. pos += copy;
  1904. }
  1905. start = end;
  1906. }
  1907. BUG_ON(len);
  1908. return csum;
  1909. }
  1910. EXPORT_SYMBOL(skb_copy_and_csum_bits);
  1911. /**
  1912. * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
  1913. * @from: source buffer
  1914. *
  1915. * Calculates the amount of linear headroom needed in the 'to' skb passed
  1916. * into skb_zerocopy().
  1917. */
  1918. unsigned int
  1919. skb_zerocopy_headlen(const struct sk_buff *from)
  1920. {
  1921. unsigned int hlen = 0;
  1922. if (!from->head_frag ||
  1923. skb_headlen(from) < L1_CACHE_BYTES ||
  1924. skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
  1925. hlen = skb_headlen(from);
  1926. if (skb_has_frag_list(from))
  1927. hlen = from->len;
  1928. return hlen;
  1929. }
  1930. EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
  1931. /**
  1932. * skb_zerocopy - Zero copy skb to skb
  1933. * @to: destination buffer
  1934. * @from: source buffer
  1935. * @len: number of bytes to copy from source buffer
  1936. * @hlen: size of linear headroom in destination buffer
  1937. *
  1938. * Copies up to `len` bytes from `from` to `to` by creating references
  1939. * to the frags in the source buffer.
  1940. *
  1941. * The `hlen` as calculated by skb_zerocopy_headlen() specifies the
  1942. * headroom in the `to` buffer.
  1943. *
  1944. * Return value:
  1945. * 0: everything is OK
  1946. * -ENOMEM: couldn't orphan frags of @from due to lack of memory
  1947. * -EFAULT: skb_copy_bits() found some problem with skb geometry
  1948. */
  1949. int
  1950. skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
  1951. {
  1952. int i, j = 0;
  1953. int plen = 0; /* length of skb->head fragment */
  1954. int ret;
  1955. struct page *page;
  1956. unsigned int offset;
  1957. BUG_ON(!from->head_frag && !hlen);
  1958. /* dont bother with small payloads */
  1959. if (len <= skb_tailroom(to))
  1960. return skb_copy_bits(from, 0, skb_put(to, len), len);
  1961. if (hlen) {
  1962. ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
  1963. if (unlikely(ret))
  1964. return ret;
  1965. len -= hlen;
  1966. } else {
  1967. plen = min_t(int, skb_headlen(from), len);
  1968. if (plen) {
  1969. page = virt_to_head_page(from->head);
  1970. offset = from->data - (unsigned char *)page_address(page);
  1971. __skb_fill_page_desc(to, 0, page, offset, plen);
  1972. get_page(page);
  1973. j = 1;
  1974. len -= plen;
  1975. }
  1976. }
  1977. to->truesize += len + plen;
  1978. to->len += len + plen;
  1979. to->data_len += len + plen;
  1980. if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
  1981. skb_tx_error(from);
  1982. return -ENOMEM;
  1983. }
  1984. for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
  1985. if (!len)
  1986. break;
  1987. skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
  1988. skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len);
  1989. len -= skb_shinfo(to)->frags[j].size;
  1990. skb_frag_ref(to, j);
  1991. j++;
  1992. }
  1993. skb_shinfo(to)->nr_frags = j;
  1994. return 0;
  1995. }
  1996. EXPORT_SYMBOL_GPL(skb_zerocopy);
  1997. void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
  1998. {
  1999. __wsum csum;
  2000. long csstart;
  2001. if (skb->ip_summed == CHECKSUM_PARTIAL)
  2002. csstart = skb_checksum_start_offset(skb);
  2003. else
  2004. csstart = skb_headlen(skb);
  2005. BUG_ON(csstart > skb_headlen(skb));
  2006. skb_copy_from_linear_data(skb, to, csstart);
  2007. csum = 0;
  2008. if (csstart != skb->len)
  2009. csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
  2010. skb->len - csstart, 0);
  2011. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  2012. long csstuff = csstart + skb->csum_offset;
  2013. *((__sum16 *)(to + csstuff)) = csum_fold(csum);
  2014. }
  2015. }
  2016. EXPORT_SYMBOL(skb_copy_and_csum_dev);
  2017. /**
  2018. * skb_dequeue - remove from the head of the queue
  2019. * @list: list to dequeue from
  2020. *
  2021. * Remove the head of the list. The list lock is taken so the function
  2022. * may be used safely with other locking list functions. The head item is
  2023. * returned or %NULL if the list is empty.
  2024. */
  2025. struct sk_buff *skb_dequeue(struct sk_buff_head *list)
  2026. {
  2027. unsigned long flags;
  2028. struct sk_buff *result;
  2029. spin_lock_irqsave(&list->lock, flags);
  2030. result = __skb_dequeue(list);
  2031. spin_unlock_irqrestore(&list->lock, flags);
  2032. return result;
  2033. }
  2034. EXPORT_SYMBOL(skb_dequeue);
  2035. /**
  2036. * skb_dequeue_tail - remove from the tail of the queue
  2037. * @list: list to dequeue from
  2038. *
  2039. * Remove the tail of the list. The list lock is taken so the function
  2040. * may be used safely with other locking list functions. The tail item is
  2041. * returned or %NULL if the list is empty.
  2042. */
  2043. struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
  2044. {
  2045. unsigned long flags;
  2046. struct sk_buff *result;
  2047. spin_lock_irqsave(&list->lock, flags);
  2048. result = __skb_dequeue_tail(list);
  2049. spin_unlock_irqrestore(&list->lock, flags);
  2050. return result;
  2051. }
  2052. EXPORT_SYMBOL(skb_dequeue_tail);
  2053. /**
  2054. * skb_queue_purge - empty a list
  2055. * @list: list to empty
  2056. *
  2057. * Delete all buffers on an &sk_buff list. Each buffer is removed from
  2058. * the list and one reference dropped. This function takes the list
  2059. * lock and is atomic with respect to other list locking functions.
  2060. */
  2061. void skb_queue_purge(struct sk_buff_head *list)
  2062. {
  2063. struct sk_buff *skb;
  2064. while ((skb = skb_dequeue(list)) != NULL)
  2065. kfree_skb(skb);
  2066. }
  2067. EXPORT_SYMBOL(skb_queue_purge);
  2068. /**
  2069. * skb_queue_head - queue a buffer at the list head
  2070. * @list: list to use
  2071. * @newsk: buffer to queue
  2072. *
  2073. * Queue a buffer at the start of the list. This function takes the
  2074. * list lock and can be used safely with other locking &sk_buff functions
  2075. * safely.
  2076. *
  2077. * A buffer cannot be placed on two lists at the same time.
  2078. */
  2079. void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
  2080. {
  2081. unsigned long flags;
  2082. spin_lock_irqsave(&list->lock, flags);
  2083. __skb_queue_head(list, newsk);
  2084. spin_unlock_irqrestore(&list->lock, flags);
  2085. }
  2086. EXPORT_SYMBOL(skb_queue_head);
  2087. /**
  2088. * skb_queue_tail - queue a buffer at the list tail
  2089. * @list: list to use
  2090. * @newsk: buffer to queue
  2091. *
  2092. * Queue a buffer at the tail of the list. This function takes the
  2093. * list lock and can be used safely with other locking &sk_buff functions
  2094. * safely.
  2095. *
  2096. * A buffer cannot be placed on two lists at the same time.
  2097. */
  2098. void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
  2099. {
  2100. unsigned long flags;
  2101. spin_lock_irqsave(&list->lock, flags);
  2102. __skb_queue_tail(list, newsk);
  2103. spin_unlock_irqrestore(&list->lock, flags);
  2104. }
  2105. EXPORT_SYMBOL(skb_queue_tail);
  2106. /**
  2107. * skb_unlink - remove a buffer from a list
  2108. * @skb: buffer to remove
  2109. * @list: list to use
  2110. *
  2111. * Remove a packet from a list. The list locks are taken and this
  2112. * function is atomic with respect to other list locked calls
  2113. *
  2114. * You must know what list the SKB is on.
  2115. */
  2116. void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
  2117. {
  2118. unsigned long flags;
  2119. spin_lock_irqsave(&list->lock, flags);
  2120. __skb_unlink(skb, list);
  2121. spin_unlock_irqrestore(&list->lock, flags);
  2122. }
  2123. EXPORT_SYMBOL(skb_unlink);
  2124. /**
  2125. * skb_append - append a buffer
  2126. * @old: buffer to insert after
  2127. * @newsk: buffer to insert
  2128. * @list: list to use
  2129. *
  2130. * Place a packet after a given packet in a list. The list locks are taken
  2131. * and this function is atomic with respect to other list locked calls.
  2132. * A buffer cannot be placed on two lists at the same time.
  2133. */
  2134. void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
  2135. {
  2136. unsigned long flags;
  2137. spin_lock_irqsave(&list->lock, flags);
  2138. __skb_queue_after(list, old, newsk);
  2139. spin_unlock_irqrestore(&list->lock, flags);
  2140. }
  2141. EXPORT_SYMBOL(skb_append);
  2142. /**
  2143. * skb_insert - insert a buffer
  2144. * @old: buffer to insert before
  2145. * @newsk: buffer to insert
  2146. * @list: list to use
  2147. *
  2148. * Place a packet before a given packet in a list. The list locks are
  2149. * taken and this function is atomic with respect to other list locked
  2150. * calls.
  2151. *
  2152. * A buffer cannot be placed on two lists at the same time.
  2153. */
  2154. void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
  2155. {
  2156. unsigned long flags;
  2157. spin_lock_irqsave(&list->lock, flags);
  2158. __skb_insert(newsk, old->prev, old, list);
  2159. spin_unlock_irqrestore(&list->lock, flags);
  2160. }
  2161. EXPORT_SYMBOL(skb_insert);
  2162. static inline void skb_split_inside_header(struct sk_buff *skb,
  2163. struct sk_buff* skb1,
  2164. const u32 len, const int pos)
  2165. {
  2166. int i;
  2167. skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
  2168. pos - len);
  2169. /* And move data appendix as is. */
  2170. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
  2171. skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
  2172. skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
  2173. skb_shinfo(skb)->nr_frags = 0;
  2174. skb1->data_len = skb->data_len;
  2175. skb1->len += skb1->data_len;
  2176. skb->data_len = 0;
  2177. skb->len = len;
  2178. skb_set_tail_pointer(skb, len);
  2179. }
  2180. static inline void skb_split_no_header(struct sk_buff *skb,
  2181. struct sk_buff* skb1,
  2182. const u32 len, int pos)
  2183. {
  2184. int i, k = 0;
  2185. const int nfrags = skb_shinfo(skb)->nr_frags;
  2186. skb_shinfo(skb)->nr_frags = 0;
  2187. skb1->len = skb1->data_len = skb->len - len;
  2188. skb->len = len;
  2189. skb->data_len = len - pos;
  2190. for (i = 0; i < nfrags; i++) {
  2191. int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
  2192. if (pos + size > len) {
  2193. skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
  2194. if (pos < len) {
  2195. /* Split frag.
  2196. * We have two variants in this case:
  2197. * 1. Move all the frag to the second
  2198. * part, if it is possible. F.e.
  2199. * this approach is mandatory for TUX,
  2200. * where splitting is expensive.
  2201. * 2. Split is accurately. We make this.
  2202. */
  2203. skb_frag_ref(skb, i);
  2204. skb_shinfo(skb1)->frags[0].page_offset += len - pos;
  2205. skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
  2206. skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
  2207. skb_shinfo(skb)->nr_frags++;
  2208. }
  2209. k++;
  2210. } else
  2211. skb_shinfo(skb)->nr_frags++;
  2212. pos += size;
  2213. }
  2214. skb_shinfo(skb1)->nr_frags = k;
  2215. }
  2216. /**
  2217. * skb_split - Split fragmented skb to two parts at length len.
  2218. * @skb: the buffer to split
  2219. * @skb1: the buffer to receive the second part
  2220. * @len: new length for skb
  2221. */
  2222. void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
  2223. {
  2224. int pos = skb_headlen(skb);
  2225. skb_shinfo(skb1)->tx_flags = skb_shinfo(skb)->tx_flags & SKBTX_SHARED_FRAG;
  2226. if (len < pos) /* Split line is inside header. */
  2227. skb_split_inside_header(skb, skb1, len, pos);
  2228. else /* Second chunk has no header, nothing to copy. */
  2229. skb_split_no_header(skb, skb1, len, pos);
  2230. }
  2231. EXPORT_SYMBOL(skb_split);
  2232. /* Shifting from/to a cloned skb is a no-go.
  2233. *
  2234. * Caller cannot keep skb_shinfo related pointers past calling here!
  2235. */
  2236. static int skb_prepare_for_shift(struct sk_buff *skb)
  2237. {
  2238. return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
  2239. }
  2240. /**
  2241. * skb_shift - Shifts paged data partially from skb to another
  2242. * @tgt: buffer into which tail data gets added
  2243. * @skb: buffer from which the paged data comes from
  2244. * @shiftlen: shift up to this many bytes
  2245. *
  2246. * Attempts to shift up to shiftlen worth of bytes, which may be less than
  2247. * the length of the skb, from skb to tgt. Returns number bytes shifted.
  2248. * It's up to caller to free skb if everything was shifted.
  2249. *
  2250. * If @tgt runs out of frags, the whole operation is aborted.
  2251. *
  2252. * Skb cannot include anything else but paged data while tgt is allowed
  2253. * to have non-paged data as well.
  2254. *
  2255. * TODO: full sized shift could be optimized but that would need
  2256. * specialized skb free'er to handle frags without up-to-date nr_frags.
  2257. */
  2258. int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
  2259. {
  2260. int from, to, merge, todo;
  2261. struct skb_frag_struct *fragfrom, *fragto;
  2262. BUG_ON(shiftlen > skb->len);
  2263. BUG_ON(skb_headlen(skb)); /* Would corrupt stream */
  2264. todo = shiftlen;
  2265. from = 0;
  2266. to = skb_shinfo(tgt)->nr_frags;
  2267. fragfrom = &skb_shinfo(skb)->frags[from];
  2268. /* Actual merge is delayed until the point when we know we can
  2269. * commit all, so that we don't have to undo partial changes
  2270. */
  2271. if (!to ||
  2272. !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
  2273. fragfrom->page_offset)) {
  2274. merge = -1;
  2275. } else {
  2276. merge = to - 1;
  2277. todo -= skb_frag_size(fragfrom);
  2278. if (todo < 0) {
  2279. if (skb_prepare_for_shift(skb) ||
  2280. skb_prepare_for_shift(tgt))
  2281. return 0;
  2282. /* All previous frag pointers might be stale! */
  2283. fragfrom = &skb_shinfo(skb)->frags[from];
  2284. fragto = &skb_shinfo(tgt)->frags[merge];
  2285. skb_frag_size_add(fragto, shiftlen);
  2286. skb_frag_size_sub(fragfrom, shiftlen);
  2287. fragfrom->page_offset += shiftlen;
  2288. goto onlymerged;
  2289. }
  2290. from++;
  2291. }
  2292. /* Skip full, not-fitting skb to avoid expensive operations */
  2293. if ((shiftlen == skb->len) &&
  2294. (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
  2295. return 0;
  2296. if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
  2297. return 0;
  2298. while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
  2299. if (to == MAX_SKB_FRAGS)
  2300. return 0;
  2301. fragfrom = &skb_shinfo(skb)->frags[from];
  2302. fragto = &skb_shinfo(tgt)->frags[to];
  2303. if (todo >= skb_frag_size(fragfrom)) {
  2304. *fragto = *fragfrom;
  2305. todo -= skb_frag_size(fragfrom);
  2306. from++;
  2307. to++;
  2308. } else {
  2309. __skb_frag_ref(fragfrom);
  2310. fragto->page = fragfrom->page;
  2311. fragto->page_offset = fragfrom->page_offset;
  2312. skb_frag_size_set(fragto, todo);
  2313. fragfrom->page_offset += todo;
  2314. skb_frag_size_sub(fragfrom, todo);
  2315. todo = 0;
  2316. to++;
  2317. break;
  2318. }
  2319. }
  2320. /* Ready to "commit" this state change to tgt */
  2321. skb_shinfo(tgt)->nr_frags = to;
  2322. if (merge >= 0) {
  2323. fragfrom = &skb_shinfo(skb)->frags[0];
  2324. fragto = &skb_shinfo(tgt)->frags[merge];
  2325. skb_frag_size_add(fragto, skb_frag_size(fragfrom));
  2326. __skb_frag_unref(fragfrom);
  2327. }
  2328. /* Reposition in the original skb */
  2329. to = 0;
  2330. while (from < skb_shinfo(skb)->nr_frags)
  2331. skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
  2332. skb_shinfo(skb)->nr_frags = to;
  2333. BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
  2334. onlymerged:
  2335. /* Most likely the tgt won't ever need its checksum anymore, skb on
  2336. * the other hand might need it if it needs to be resent
  2337. */
  2338. tgt->ip_summed = CHECKSUM_PARTIAL;
  2339. skb->ip_summed = CHECKSUM_PARTIAL;
  2340. /* Yak, is it really working this way? Some helper please? */
  2341. skb->len -= shiftlen;
  2342. skb->data_len -= shiftlen;
  2343. skb->truesize -= shiftlen;
  2344. tgt->len += shiftlen;
  2345. tgt->data_len += shiftlen;
  2346. tgt->truesize += shiftlen;
  2347. return shiftlen;
  2348. }
  2349. /**
  2350. * skb_prepare_seq_read - Prepare a sequential read of skb data
  2351. * @skb: the buffer to read
  2352. * @from: lower offset of data to be read
  2353. * @to: upper offset of data to be read
  2354. * @st: state variable
  2355. *
  2356. * Initializes the specified state variable. Must be called before
  2357. * invoking skb_seq_read() for the first time.
  2358. */
  2359. void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
  2360. unsigned int to, struct skb_seq_state *st)
  2361. {
  2362. st->lower_offset = from;
  2363. st->upper_offset = to;
  2364. st->root_skb = st->cur_skb = skb;
  2365. st->frag_idx = st->stepped_offset = 0;
  2366. st->frag_data = NULL;
  2367. }
  2368. EXPORT_SYMBOL(skb_prepare_seq_read);
  2369. /**
  2370. * skb_seq_read - Sequentially read skb data
  2371. * @consumed: number of bytes consumed by the caller so far
  2372. * @data: destination pointer for data to be returned
  2373. * @st: state variable
  2374. *
  2375. * Reads a block of skb data at @consumed relative to the
  2376. * lower offset specified to skb_prepare_seq_read(). Assigns
  2377. * the head of the data block to @data and returns the length
  2378. * of the block or 0 if the end of the skb data or the upper
  2379. * offset has been reached.
  2380. *
  2381. * The caller is not required to consume all of the data
  2382. * returned, i.e. @consumed is typically set to the number
  2383. * of bytes already consumed and the next call to
  2384. * skb_seq_read() will return the remaining part of the block.
  2385. *
  2386. * Note 1: The size of each block of data returned can be arbitrary,
  2387. * this limitation is the cost for zerocopy sequential
  2388. * reads of potentially non linear data.
  2389. *
  2390. * Note 2: Fragment lists within fragments are not implemented
  2391. * at the moment, state->root_skb could be replaced with
  2392. * a stack for this purpose.
  2393. */
  2394. unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
  2395. struct skb_seq_state *st)
  2396. {
  2397. unsigned int block_limit, abs_offset = consumed + st->lower_offset;
  2398. skb_frag_t *frag;
  2399. if (unlikely(abs_offset >= st->upper_offset)) {
  2400. if (st->frag_data) {
  2401. kunmap_atomic(st->frag_data);
  2402. st->frag_data = NULL;
  2403. }
  2404. return 0;
  2405. }
  2406. next_skb:
  2407. block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
  2408. if (abs_offset < block_limit && !st->frag_data) {
  2409. *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
  2410. return block_limit - abs_offset;
  2411. }
  2412. if (st->frag_idx == 0 && !st->frag_data)
  2413. st->stepped_offset += skb_headlen(st->cur_skb);
  2414. while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
  2415. frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
  2416. block_limit = skb_frag_size(frag) + st->stepped_offset;
  2417. if (abs_offset < block_limit) {
  2418. if (!st->frag_data)
  2419. st->frag_data = kmap_atomic(skb_frag_page(frag));
  2420. *data = (u8 *) st->frag_data + frag->page_offset +
  2421. (abs_offset - st->stepped_offset);
  2422. return block_limit - abs_offset;
  2423. }
  2424. if (st->frag_data) {
  2425. kunmap_atomic(st->frag_data);
  2426. st->frag_data = NULL;
  2427. }
  2428. st->frag_idx++;
  2429. st->stepped_offset += skb_frag_size(frag);
  2430. }
  2431. if (st->frag_data) {
  2432. kunmap_atomic(st->frag_data);
  2433. st->frag_data = NULL;
  2434. }
  2435. if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
  2436. st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
  2437. st->frag_idx = 0;
  2438. goto next_skb;
  2439. } else if (st->cur_skb->next) {
  2440. st->cur_skb = st->cur_skb->next;
  2441. st->frag_idx = 0;
  2442. goto next_skb;
  2443. }
  2444. return 0;
  2445. }
  2446. EXPORT_SYMBOL(skb_seq_read);
  2447. /**
  2448. * skb_abort_seq_read - Abort a sequential read of skb data
  2449. * @st: state variable
  2450. *
  2451. * Must be called if skb_seq_read() was not called until it
  2452. * returned 0.
  2453. */
  2454. void skb_abort_seq_read(struct skb_seq_state *st)
  2455. {
  2456. if (st->frag_data)
  2457. kunmap_atomic(st->frag_data);
  2458. }
  2459. EXPORT_SYMBOL(skb_abort_seq_read);
  2460. #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
  2461. static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
  2462. struct ts_config *conf,
  2463. struct ts_state *state)
  2464. {
  2465. return skb_seq_read(offset, text, TS_SKB_CB(state));
  2466. }
  2467. static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
  2468. {
  2469. skb_abort_seq_read(TS_SKB_CB(state));
  2470. }
  2471. /**
  2472. * skb_find_text - Find a text pattern in skb data
  2473. * @skb: the buffer to look in
  2474. * @from: search offset
  2475. * @to: search limit
  2476. * @config: textsearch configuration
  2477. * @state: uninitialized textsearch state variable
  2478. *
  2479. * Finds a pattern in the skb data according to the specified
  2480. * textsearch configuration. Use textsearch_next() to retrieve
  2481. * subsequent occurrences of the pattern. Returns the offset
  2482. * to the first occurrence or UINT_MAX if no match was found.
  2483. */
  2484. unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
  2485. unsigned int to, struct ts_config *config,
  2486. struct ts_state *state)
  2487. {
  2488. unsigned int ret;
  2489. config->get_next_block = skb_ts_get_next_block;
  2490. config->finish = skb_ts_finish;
  2491. skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
  2492. ret = textsearch_find(config, state);
  2493. return (ret <= to - from ? ret : UINT_MAX);
  2494. }
  2495. EXPORT_SYMBOL(skb_find_text);
  2496. /**
  2497. * skb_append_datato_frags - append the user data to a skb
  2498. * @sk: sock structure
  2499. * @skb: skb structure to be appended with user data.
  2500. * @getfrag: call back function to be used for getting the user data
  2501. * @from: pointer to user message iov
  2502. * @length: length of the iov message
  2503. *
  2504. * Description: This procedure append the user data in the fragment part
  2505. * of the skb if any page alloc fails user this procedure returns -ENOMEM
  2506. */
  2507. int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
  2508. int (*getfrag)(void *from, char *to, int offset,
  2509. int len, int odd, struct sk_buff *skb),
  2510. void *from, int length)
  2511. {
  2512. int frg_cnt = skb_shinfo(skb)->nr_frags;
  2513. int copy;
  2514. int offset = 0;
  2515. int ret;
  2516. struct page_frag *pfrag = &current->task_frag;
  2517. do {
  2518. /* Return error if we don't have space for new frag */
  2519. if (frg_cnt >= MAX_SKB_FRAGS)
  2520. return -EMSGSIZE;
  2521. if (!sk_page_frag_refill(sk, pfrag))
  2522. return -ENOMEM;
  2523. /* copy the user data to page */
  2524. copy = min_t(int, length, pfrag->size - pfrag->offset);
  2525. ret = getfrag(from, page_address(pfrag->page) + pfrag->offset,
  2526. offset, copy, 0, skb);
  2527. if (ret < 0)
  2528. return -EFAULT;
  2529. /* copy was successful so update the size parameters */
  2530. skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset,
  2531. copy);
  2532. frg_cnt++;
  2533. pfrag->offset += copy;
  2534. get_page(pfrag->page);
  2535. skb->truesize += copy;
  2536. atomic_add(copy, &sk->sk_wmem_alloc);
  2537. skb->len += copy;
  2538. skb->data_len += copy;
  2539. offset += copy;
  2540. length -= copy;
  2541. } while (length > 0);
  2542. return 0;
  2543. }
  2544. EXPORT_SYMBOL(skb_append_datato_frags);
  2545. /**
  2546. * skb_pull_rcsum - pull skb and update receive checksum
  2547. * @skb: buffer to update
  2548. * @len: length of data pulled
  2549. *
  2550. * This function performs an skb_pull on the packet and updates
  2551. * the CHECKSUM_COMPLETE checksum. It should be used on
  2552. * receive path processing instead of skb_pull unless you know
  2553. * that the checksum difference is zero (e.g., a valid IP header)
  2554. * or you are setting ip_summed to CHECKSUM_NONE.
  2555. */
  2556. unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
  2557. {
  2558. BUG_ON(len > skb->len);
  2559. skb->len -= len;
  2560. BUG_ON(skb->len < skb->data_len);
  2561. skb_postpull_rcsum(skb, skb->data, len);
  2562. return skb->data += len;
  2563. }
  2564. EXPORT_SYMBOL_GPL(skb_pull_rcsum);
  2565. /**
  2566. * skb_segment - Perform protocol segmentation on skb.
  2567. * @head_skb: buffer to segment
  2568. * @features: features for the output path (see dev->features)
  2569. *
  2570. * This function performs segmentation on the given skb. It returns
  2571. * a pointer to the first in a list of new skbs for the segments.
  2572. * In case of error it returns ERR_PTR(err).
  2573. */
  2574. struct sk_buff *skb_segment(struct sk_buff *head_skb,
  2575. netdev_features_t features)
  2576. {
  2577. struct sk_buff *segs = NULL;
  2578. struct sk_buff *tail = NULL;
  2579. struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
  2580. skb_frag_t *frag = skb_shinfo(head_skb)->frags;
  2581. unsigned int mss = skb_shinfo(head_skb)->gso_size;
  2582. unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
  2583. struct sk_buff *frag_skb = head_skb;
  2584. unsigned int offset = doffset;
  2585. unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
  2586. unsigned int headroom;
  2587. unsigned int len;
  2588. __be16 proto;
  2589. bool csum;
  2590. int sg = !!(features & NETIF_F_SG);
  2591. int nfrags = skb_shinfo(head_skb)->nr_frags;
  2592. int err = -ENOMEM;
  2593. int i = 0;
  2594. int pos;
  2595. int dummy;
  2596. __skb_push(head_skb, doffset);
  2597. proto = skb_network_protocol(head_skb, &dummy);
  2598. if (unlikely(!proto))
  2599. return ERR_PTR(-EINVAL);
  2600. csum = !head_skb->encap_hdr_csum &&
  2601. !!can_checksum_protocol(features, proto);
  2602. headroom = skb_headroom(head_skb);
  2603. pos = skb_headlen(head_skb);
  2604. do {
  2605. struct sk_buff *nskb;
  2606. skb_frag_t *nskb_frag;
  2607. int hsize;
  2608. int size;
  2609. len = head_skb->len - offset;
  2610. if (len > mss)
  2611. len = mss;
  2612. hsize = skb_headlen(head_skb) - offset;
  2613. if (hsize < 0)
  2614. hsize = 0;
  2615. if (hsize > len || !sg)
  2616. hsize = len;
  2617. if (!hsize && i >= nfrags && skb_headlen(list_skb) &&
  2618. (skb_headlen(list_skb) == len || sg)) {
  2619. BUG_ON(skb_headlen(list_skb) > len);
  2620. i = 0;
  2621. nfrags = skb_shinfo(list_skb)->nr_frags;
  2622. frag = skb_shinfo(list_skb)->frags;
  2623. frag_skb = list_skb;
  2624. pos += skb_headlen(list_skb);
  2625. while (pos < offset + len) {
  2626. BUG_ON(i >= nfrags);
  2627. size = skb_frag_size(frag);
  2628. if (pos + size > offset + len)
  2629. break;
  2630. i++;
  2631. pos += size;
  2632. frag++;
  2633. }
  2634. nskb = skb_clone(list_skb, GFP_ATOMIC);
  2635. list_skb = list_skb->next;
  2636. if (unlikely(!nskb))
  2637. goto err;
  2638. if (unlikely(pskb_trim(nskb, len))) {
  2639. kfree_skb(nskb);
  2640. goto err;
  2641. }
  2642. hsize = skb_end_offset(nskb);
  2643. if (skb_cow_head(nskb, doffset + headroom)) {
  2644. kfree_skb(nskb);
  2645. goto err;
  2646. }
  2647. nskb->truesize += skb_end_offset(nskb) - hsize;
  2648. skb_release_head_state(nskb);
  2649. __skb_push(nskb, doffset);
  2650. } else {
  2651. nskb = __alloc_skb(hsize + doffset + headroom,
  2652. GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
  2653. NUMA_NO_NODE);
  2654. if (unlikely(!nskb))
  2655. goto err;
  2656. skb_reserve(nskb, headroom);
  2657. __skb_put(nskb, doffset);
  2658. }
  2659. if (segs)
  2660. tail->next = nskb;
  2661. else
  2662. segs = nskb;
  2663. tail = nskb;
  2664. __copy_skb_header(nskb, head_skb);
  2665. skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
  2666. skb_reset_mac_len(nskb);
  2667. skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
  2668. nskb->data - tnl_hlen,
  2669. doffset + tnl_hlen);
  2670. if (nskb->len == len + doffset)
  2671. goto perform_csum_check;
  2672. if (!sg && !nskb->remcsum_offload) {
  2673. nskb->ip_summed = CHECKSUM_NONE;
  2674. nskb->csum = skb_copy_and_csum_bits(head_skb, offset,
  2675. skb_put(nskb, len),
  2676. len, 0);
  2677. SKB_GSO_CB(nskb)->csum_start =
  2678. skb_headroom(nskb) + doffset;
  2679. continue;
  2680. }
  2681. nskb_frag = skb_shinfo(nskb)->frags;
  2682. skb_copy_from_linear_data_offset(head_skb, offset,
  2683. skb_put(nskb, hsize), hsize);
  2684. skb_shinfo(nskb)->tx_flags = skb_shinfo(head_skb)->tx_flags &
  2685. SKBTX_SHARED_FRAG;
  2686. while (pos < offset + len) {
  2687. if (i >= nfrags) {
  2688. BUG_ON(skb_headlen(list_skb));
  2689. i = 0;
  2690. nfrags = skb_shinfo(list_skb)->nr_frags;
  2691. frag = skb_shinfo(list_skb)->frags;
  2692. frag_skb = list_skb;
  2693. BUG_ON(!nfrags);
  2694. list_skb = list_skb->next;
  2695. }
  2696. if (unlikely(skb_shinfo(nskb)->nr_frags >=
  2697. MAX_SKB_FRAGS)) {
  2698. net_warn_ratelimited(
  2699. "skb_segment: too many frags: %u %u\n",
  2700. pos, mss);
  2701. goto err;
  2702. }
  2703. if (unlikely(skb_orphan_frags(frag_skb, GFP_ATOMIC)))
  2704. goto err;
  2705. *nskb_frag = *frag;
  2706. __skb_frag_ref(nskb_frag);
  2707. size = skb_frag_size(nskb_frag);
  2708. if (pos < offset) {
  2709. nskb_frag->page_offset += offset - pos;
  2710. skb_frag_size_sub(nskb_frag, offset - pos);
  2711. }
  2712. skb_shinfo(nskb)->nr_frags++;
  2713. if (pos + size <= offset + len) {
  2714. i++;
  2715. frag++;
  2716. pos += size;
  2717. } else {
  2718. skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
  2719. goto skip_fraglist;
  2720. }
  2721. nskb_frag++;
  2722. }
  2723. skip_fraglist:
  2724. nskb->data_len = len - hsize;
  2725. nskb->len += nskb->data_len;
  2726. nskb->truesize += nskb->data_len;
  2727. perform_csum_check:
  2728. if (!csum && !nskb->remcsum_offload) {
  2729. nskb->csum = skb_checksum(nskb, doffset,
  2730. nskb->len - doffset, 0);
  2731. nskb->ip_summed = CHECKSUM_NONE;
  2732. SKB_GSO_CB(nskb)->csum_start =
  2733. skb_headroom(nskb) + doffset;
  2734. }
  2735. } while ((offset += len) < head_skb->len);
  2736. /* Some callers want to get the end of the list.
  2737. * Put it in segs->prev to avoid walking the list.
  2738. * (see validate_xmit_skb_list() for example)
  2739. */
  2740. segs->prev = tail;
  2741. /* Following permits correct backpressure, for protocols
  2742. * using skb_set_owner_w().
  2743. * Idea is to tranfert ownership from head_skb to last segment.
  2744. */
  2745. if (head_skb->destructor == sock_wfree) {
  2746. swap(tail->truesize, head_skb->truesize);
  2747. swap(tail->destructor, head_skb->destructor);
  2748. swap(tail->sk, head_skb->sk);
  2749. }
  2750. return segs;
  2751. err:
  2752. kfree_skb_list(segs);
  2753. return ERR_PTR(err);
  2754. }
  2755. EXPORT_SYMBOL_GPL(skb_segment);
  2756. int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
  2757. {
  2758. struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
  2759. unsigned int offset = skb_gro_offset(skb);
  2760. unsigned int headlen = skb_headlen(skb);
  2761. struct sk_buff *nskb, *lp, *p = *head;
  2762. unsigned int len = skb_gro_len(skb);
  2763. unsigned int delta_truesize;
  2764. unsigned int headroom;
  2765. if (unlikely(p->len + len >= 65536))
  2766. return -E2BIG;
  2767. lp = NAPI_GRO_CB(p)->last;
  2768. pinfo = skb_shinfo(lp);
  2769. if (headlen <= offset) {
  2770. skb_frag_t *frag;
  2771. skb_frag_t *frag2;
  2772. int i = skbinfo->nr_frags;
  2773. int nr_frags = pinfo->nr_frags + i;
  2774. if (nr_frags > MAX_SKB_FRAGS)
  2775. goto merge;
  2776. offset -= headlen;
  2777. pinfo->nr_frags = nr_frags;
  2778. skbinfo->nr_frags = 0;
  2779. frag = pinfo->frags + nr_frags;
  2780. frag2 = skbinfo->frags + i;
  2781. do {
  2782. *--frag = *--frag2;
  2783. } while (--i);
  2784. frag->page_offset += offset;
  2785. skb_frag_size_sub(frag, offset);
  2786. /* all fragments truesize : remove (head size + sk_buff) */
  2787. delta_truesize = skb->truesize -
  2788. SKB_TRUESIZE(skb_end_offset(skb));
  2789. skb->truesize -= skb->data_len;
  2790. skb->len -= skb->data_len;
  2791. skb->data_len = 0;
  2792. NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
  2793. goto done;
  2794. } else if (skb->head_frag) {
  2795. int nr_frags = pinfo->nr_frags;
  2796. skb_frag_t *frag = pinfo->frags + nr_frags;
  2797. struct page *page = virt_to_head_page(skb->head);
  2798. unsigned int first_size = headlen - offset;
  2799. unsigned int first_offset;
  2800. if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
  2801. goto merge;
  2802. first_offset = skb->data -
  2803. (unsigned char *)page_address(page) +
  2804. offset;
  2805. pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
  2806. frag->page.p = page;
  2807. frag->page_offset = first_offset;
  2808. skb_frag_size_set(frag, first_size);
  2809. memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
  2810. /* We dont need to clear skbinfo->nr_frags here */
  2811. delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
  2812. NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
  2813. goto done;
  2814. }
  2815. /* switch back to head shinfo */
  2816. pinfo = skb_shinfo(p);
  2817. if (pinfo->frag_list)
  2818. goto merge;
  2819. if (skb_gro_len(p) != pinfo->gso_size)
  2820. return -E2BIG;
  2821. headroom = skb_headroom(p);
  2822. nskb = alloc_skb(headroom + skb_gro_offset(p), GFP_ATOMIC);
  2823. if (unlikely(!nskb))
  2824. return -ENOMEM;
  2825. __copy_skb_header(nskb, p);
  2826. nskb->mac_len = p->mac_len;
  2827. skb_reserve(nskb, headroom);
  2828. __skb_put(nskb, skb_gro_offset(p));
  2829. skb_set_mac_header(nskb, skb_mac_header(p) - p->data);
  2830. skb_set_network_header(nskb, skb_network_offset(p));
  2831. skb_set_transport_header(nskb, skb_transport_offset(p));
  2832. __skb_pull(p, skb_gro_offset(p));
  2833. memcpy(skb_mac_header(nskb), skb_mac_header(p),
  2834. p->data - skb_mac_header(p));
  2835. skb_shinfo(nskb)->frag_list = p;
  2836. skb_shinfo(nskb)->gso_size = pinfo->gso_size;
  2837. pinfo->gso_size = 0;
  2838. __skb_header_release(p);
  2839. NAPI_GRO_CB(nskb)->last = p;
  2840. nskb->data_len += p->len;
  2841. nskb->truesize += p->truesize;
  2842. nskb->len += p->len;
  2843. *head = nskb;
  2844. nskb->next = p->next;
  2845. p->next = NULL;
  2846. p = nskb;
  2847. merge:
  2848. delta_truesize = skb->truesize;
  2849. if (offset > headlen) {
  2850. unsigned int eat = offset - headlen;
  2851. skbinfo->frags[0].page_offset += eat;
  2852. skb_frag_size_sub(&skbinfo->frags[0], eat);
  2853. skb->data_len -= eat;
  2854. skb->len -= eat;
  2855. offset = headlen;
  2856. }
  2857. __skb_pull(skb, offset);
  2858. if (NAPI_GRO_CB(p)->last == p)
  2859. skb_shinfo(p)->frag_list = skb;
  2860. else
  2861. NAPI_GRO_CB(p)->last->next = skb;
  2862. NAPI_GRO_CB(p)->last = skb;
  2863. __skb_header_release(skb);
  2864. lp = p;
  2865. done:
  2866. NAPI_GRO_CB(p)->count++;
  2867. p->data_len += len;
  2868. p->truesize += delta_truesize;
  2869. p->len += len;
  2870. if (lp != p) {
  2871. lp->data_len += len;
  2872. lp->truesize += delta_truesize;
  2873. lp->len += len;
  2874. }
  2875. NAPI_GRO_CB(skb)->same_flow = 1;
  2876. return 0;
  2877. }
  2878. void __init skb_init(void)
  2879. {
  2880. skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
  2881. sizeof(struct sk_buff),
  2882. 0,
  2883. SLAB_HWCACHE_ALIGN|SLAB_PANIC,
  2884. NULL);
  2885. skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
  2886. sizeof(struct sk_buff_fclones),
  2887. 0,
  2888. SLAB_HWCACHE_ALIGN|SLAB_PANIC,
  2889. NULL);
  2890. }
  2891. /**
  2892. * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
  2893. * @skb: Socket buffer containing the buffers to be mapped
  2894. * @sg: The scatter-gather list to map into
  2895. * @offset: The offset into the buffer's contents to start mapping
  2896. * @len: Length of buffer space to be mapped
  2897. *
  2898. * Fill the specified scatter-gather list with mappings/pointers into a
  2899. * region of the buffer space attached to a socket buffer.
  2900. */
  2901. static int
  2902. __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
  2903. {
  2904. int start = skb_headlen(skb);
  2905. int i, copy = start - offset;
  2906. struct sk_buff *frag_iter;
  2907. int elt = 0;
  2908. if (copy > 0) {
  2909. if (copy > len)
  2910. copy = len;
  2911. sg_set_buf(sg, skb->data + offset, copy);
  2912. elt++;
  2913. if ((len -= copy) == 0)
  2914. return elt;
  2915. offset += copy;
  2916. }
  2917. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  2918. int end;
  2919. WARN_ON(start > offset + len);
  2920. end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
  2921. if ((copy = end - offset) > 0) {
  2922. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  2923. if (copy > len)
  2924. copy = len;
  2925. sg_set_page(&sg[elt], skb_frag_page(frag), copy,
  2926. frag->page_offset+offset-start);
  2927. elt++;
  2928. if (!(len -= copy))
  2929. return elt;
  2930. offset += copy;
  2931. }
  2932. start = end;
  2933. }
  2934. skb_walk_frags(skb, frag_iter) {
  2935. int end;
  2936. WARN_ON(start > offset + len);
  2937. end = start + frag_iter->len;
  2938. if ((copy = end - offset) > 0) {
  2939. if (copy > len)
  2940. copy = len;
  2941. elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start,
  2942. copy);
  2943. if ((len -= copy) == 0)
  2944. return elt;
  2945. offset += copy;
  2946. }
  2947. start = end;
  2948. }
  2949. BUG_ON(len);
  2950. return elt;
  2951. }
  2952. /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
  2953. * sglist without mark the sg which contain last skb data as the end.
  2954. * So the caller can mannipulate sg list as will when padding new data after
  2955. * the first call without calling sg_unmark_end to expend sg list.
  2956. *
  2957. * Scenario to use skb_to_sgvec_nomark:
  2958. * 1. sg_init_table
  2959. * 2. skb_to_sgvec_nomark(payload1)
  2960. * 3. skb_to_sgvec_nomark(payload2)
  2961. *
  2962. * This is equivalent to:
  2963. * 1. sg_init_table
  2964. * 2. skb_to_sgvec(payload1)
  2965. * 3. sg_unmark_end
  2966. * 4. skb_to_sgvec(payload2)
  2967. *
  2968. * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
  2969. * is more preferable.
  2970. */
  2971. int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
  2972. int offset, int len)
  2973. {
  2974. return __skb_to_sgvec(skb, sg, offset, len);
  2975. }
  2976. EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
  2977. int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
  2978. {
  2979. int nsg = __skb_to_sgvec(skb, sg, offset, len);
  2980. sg_mark_end(&sg[nsg - 1]);
  2981. return nsg;
  2982. }
  2983. EXPORT_SYMBOL_GPL(skb_to_sgvec);
  2984. /**
  2985. * skb_cow_data - Check that a socket buffer's data buffers are writable
  2986. * @skb: The socket buffer to check.
  2987. * @tailbits: Amount of trailing space to be added
  2988. * @trailer: Returned pointer to the skb where the @tailbits space begins
  2989. *
  2990. * Make sure that the data buffers attached to a socket buffer are
  2991. * writable. If they are not, private copies are made of the data buffers
  2992. * and the socket buffer is set to use these instead.
  2993. *
  2994. * If @tailbits is given, make sure that there is space to write @tailbits
  2995. * bytes of data beyond current end of socket buffer. @trailer will be
  2996. * set to point to the skb in which this space begins.
  2997. *
  2998. * The number of scatterlist elements required to completely map the
  2999. * COW'd and extended socket buffer will be returned.
  3000. */
  3001. int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
  3002. {
  3003. int copyflag;
  3004. int elt;
  3005. struct sk_buff *skb1, **skb_p;
  3006. /* If skb is cloned or its head is paged, reallocate
  3007. * head pulling out all the pages (pages are considered not writable
  3008. * at the moment even if they are anonymous).
  3009. */
  3010. if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
  3011. __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
  3012. return -ENOMEM;
  3013. /* Easy case. Most of packets will go this way. */
  3014. if (!skb_has_frag_list(skb)) {
  3015. /* A little of trouble, not enough of space for trailer.
  3016. * This should not happen, when stack is tuned to generate
  3017. * good frames. OK, on miss we reallocate and reserve even more
  3018. * space, 128 bytes is fair. */
  3019. if (skb_tailroom(skb) < tailbits &&
  3020. pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
  3021. return -ENOMEM;
  3022. /* Voila! */
  3023. *trailer = skb;
  3024. return 1;
  3025. }
  3026. /* Misery. We are in troubles, going to mincer fragments... */
  3027. elt = 1;
  3028. skb_p = &skb_shinfo(skb)->frag_list;
  3029. copyflag = 0;
  3030. while ((skb1 = *skb_p) != NULL) {
  3031. int ntail = 0;
  3032. /* The fragment is partially pulled by someone,
  3033. * this can happen on input. Copy it and everything
  3034. * after it. */
  3035. if (skb_shared(skb1))
  3036. copyflag = 1;
  3037. /* If the skb is the last, worry about trailer. */
  3038. if (skb1->next == NULL && tailbits) {
  3039. if (skb_shinfo(skb1)->nr_frags ||
  3040. skb_has_frag_list(skb1) ||
  3041. skb_tailroom(skb1) < tailbits)
  3042. ntail = tailbits + 128;
  3043. }
  3044. if (copyflag ||
  3045. skb_cloned(skb1) ||
  3046. ntail ||
  3047. skb_shinfo(skb1)->nr_frags ||
  3048. skb_has_frag_list(skb1)) {
  3049. struct sk_buff *skb2;
  3050. /* Fuck, we are miserable poor guys... */
  3051. if (ntail == 0)
  3052. skb2 = skb_copy(skb1, GFP_ATOMIC);
  3053. else
  3054. skb2 = skb_copy_expand(skb1,
  3055. skb_headroom(skb1),
  3056. ntail,
  3057. GFP_ATOMIC);
  3058. if (unlikely(skb2 == NULL))
  3059. return -ENOMEM;
  3060. if (skb1->sk)
  3061. skb_set_owner_w(skb2, skb1->sk);
  3062. /* Looking around. Are we still alive?
  3063. * OK, link new skb, drop old one */
  3064. skb2->next = skb1->next;
  3065. *skb_p = skb2;
  3066. kfree_skb(skb1);
  3067. skb1 = skb2;
  3068. }
  3069. elt++;
  3070. *trailer = skb1;
  3071. skb_p = &skb1->next;
  3072. }
  3073. return elt;
  3074. }
  3075. EXPORT_SYMBOL_GPL(skb_cow_data);
  3076. static void sock_rmem_free(struct sk_buff *skb)
  3077. {
  3078. struct sock *sk = skb->sk;
  3079. atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
  3080. }
  3081. /*
  3082. * Note: We dont mem charge error packets (no sk_forward_alloc changes)
  3083. */
  3084. int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
  3085. {
  3086. if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
  3087. (unsigned int)sk->sk_rcvbuf)
  3088. return -ENOMEM;
  3089. skb_orphan(skb);
  3090. skb->sk = sk;
  3091. skb->destructor = sock_rmem_free;
  3092. atomic_add(skb->truesize, &sk->sk_rmem_alloc);
  3093. /* before exiting rcu section, make sure dst is refcounted */
  3094. skb_dst_force(skb);
  3095. skb_queue_tail(&sk->sk_error_queue, skb);
  3096. if (!sock_flag(sk, SOCK_DEAD))
  3097. sk->sk_data_ready(sk);
  3098. return 0;
  3099. }
  3100. EXPORT_SYMBOL(sock_queue_err_skb);
  3101. struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
  3102. {
  3103. struct sk_buff_head *q = &sk->sk_error_queue;
  3104. struct sk_buff *skb, *skb_next;
  3105. int err = 0;
  3106. spin_lock_bh(&q->lock);
  3107. skb = __skb_dequeue(q);
  3108. if (skb && (skb_next = skb_peek(q)))
  3109. err = SKB_EXT_ERR(skb_next)->ee.ee_errno;
  3110. spin_unlock_bh(&q->lock);
  3111. sk->sk_err = err;
  3112. if (err)
  3113. sk->sk_error_report(sk);
  3114. return skb;
  3115. }
  3116. EXPORT_SYMBOL(sock_dequeue_err_skb);
  3117. /**
  3118. * skb_clone_sk - create clone of skb, and take reference to socket
  3119. * @skb: the skb to clone
  3120. *
  3121. * This function creates a clone of a buffer that holds a reference on
  3122. * sk_refcnt. Buffers created via this function are meant to be
  3123. * returned using sock_queue_err_skb, or free via kfree_skb.
  3124. *
  3125. * When passing buffers allocated with this function to sock_queue_err_skb
  3126. * it is necessary to wrap the call with sock_hold/sock_put in order to
  3127. * prevent the socket from being released prior to being enqueued on
  3128. * the sk_error_queue.
  3129. */
  3130. struct sk_buff *skb_clone_sk(struct sk_buff *skb)
  3131. {
  3132. struct sock *sk = skb->sk;
  3133. struct sk_buff *clone;
  3134. if (!sk || !atomic_inc_not_zero(&sk->sk_refcnt))
  3135. return NULL;
  3136. clone = skb_clone(skb, GFP_ATOMIC);
  3137. if (!clone) {
  3138. sock_put(sk);
  3139. return NULL;
  3140. }
  3141. clone->sk = sk;
  3142. clone->destructor = sock_efree;
  3143. return clone;
  3144. }
  3145. EXPORT_SYMBOL(skb_clone_sk);
  3146. static void __skb_complete_tx_timestamp(struct sk_buff *skb,
  3147. struct sock *sk,
  3148. int tstype)
  3149. {
  3150. struct sock_exterr_skb *serr;
  3151. int err;
  3152. serr = SKB_EXT_ERR(skb);
  3153. memset(serr, 0, sizeof(*serr));
  3154. serr->ee.ee_errno = ENOMSG;
  3155. serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
  3156. serr->ee.ee_info = tstype;
  3157. if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
  3158. serr->ee.ee_data = skb_shinfo(skb)->tskey;
  3159. if (sk->sk_protocol == IPPROTO_TCP)
  3160. serr->ee.ee_data -= sk->sk_tskey;
  3161. }
  3162. err = sock_queue_err_skb(sk, skb);
  3163. if (err)
  3164. kfree_skb(skb);
  3165. }
  3166. void skb_complete_tx_timestamp(struct sk_buff *skb,
  3167. struct skb_shared_hwtstamps *hwtstamps)
  3168. {
  3169. struct sock *sk = skb->sk;
  3170. /* take a reference to prevent skb_orphan() from freeing the socket */
  3171. sock_hold(sk);
  3172. *skb_hwtstamps(skb) = *hwtstamps;
  3173. __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND);
  3174. sock_put(sk);
  3175. }
  3176. EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
  3177. void __skb_tstamp_tx(struct sk_buff *orig_skb,
  3178. struct skb_shared_hwtstamps *hwtstamps,
  3179. struct sock *sk, int tstype)
  3180. {
  3181. struct sk_buff *skb;
  3182. if (!sk)
  3183. return;
  3184. if (hwtstamps)
  3185. *skb_hwtstamps(orig_skb) = *hwtstamps;
  3186. else
  3187. orig_skb->tstamp = ktime_get_real();
  3188. skb = skb_clone(orig_skb, GFP_ATOMIC);
  3189. if (!skb)
  3190. return;
  3191. __skb_complete_tx_timestamp(skb, sk, tstype);
  3192. }
  3193. EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
  3194. void skb_tstamp_tx(struct sk_buff *orig_skb,
  3195. struct skb_shared_hwtstamps *hwtstamps)
  3196. {
  3197. return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk,
  3198. SCM_TSTAMP_SND);
  3199. }
  3200. EXPORT_SYMBOL_GPL(skb_tstamp_tx);
  3201. void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
  3202. {
  3203. struct sock *sk = skb->sk;
  3204. struct sock_exterr_skb *serr;
  3205. int err;
  3206. skb->wifi_acked_valid = 1;
  3207. skb->wifi_acked = acked;
  3208. serr = SKB_EXT_ERR(skb);
  3209. memset(serr, 0, sizeof(*serr));
  3210. serr->ee.ee_errno = ENOMSG;
  3211. serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
  3212. /* take a reference to prevent skb_orphan() from freeing the socket */
  3213. sock_hold(sk);
  3214. err = sock_queue_err_skb(sk, skb);
  3215. if (err)
  3216. kfree_skb(skb);
  3217. sock_put(sk);
  3218. }
  3219. EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
  3220. /**
  3221. * skb_partial_csum_set - set up and verify partial csum values for packet
  3222. * @skb: the skb to set
  3223. * @start: the number of bytes after skb->data to start checksumming.
  3224. * @off: the offset from start to place the checksum.
  3225. *
  3226. * For untrusted partially-checksummed packets, we need to make sure the values
  3227. * for skb->csum_start and skb->csum_offset are valid so we don't oops.
  3228. *
  3229. * This function checks and sets those values and skb->ip_summed: if this
  3230. * returns false you should drop the packet.
  3231. */
  3232. bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
  3233. {
  3234. if (unlikely(start > skb_headlen(skb)) ||
  3235. unlikely((int)start + off > skb_headlen(skb) - 2)) {
  3236. net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n",
  3237. start, off, skb_headlen(skb));
  3238. return false;
  3239. }
  3240. skb->ip_summed = CHECKSUM_PARTIAL;
  3241. skb->csum_start = skb_headroom(skb) + start;
  3242. skb->csum_offset = off;
  3243. skb_set_transport_header(skb, start);
  3244. return true;
  3245. }
  3246. EXPORT_SYMBOL_GPL(skb_partial_csum_set);
  3247. static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
  3248. unsigned int max)
  3249. {
  3250. if (skb_headlen(skb) >= len)
  3251. return 0;
  3252. /* If we need to pullup then pullup to the max, so we
  3253. * won't need to do it again.
  3254. */
  3255. if (max > skb->len)
  3256. max = skb->len;
  3257. if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
  3258. return -ENOMEM;
  3259. if (skb_headlen(skb) < len)
  3260. return -EPROTO;
  3261. return 0;
  3262. }
  3263. #define MAX_TCP_HDR_LEN (15 * 4)
  3264. static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
  3265. typeof(IPPROTO_IP) proto,
  3266. unsigned int off)
  3267. {
  3268. switch (proto) {
  3269. int err;
  3270. case IPPROTO_TCP:
  3271. err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
  3272. off + MAX_TCP_HDR_LEN);
  3273. if (!err && !skb_partial_csum_set(skb, off,
  3274. offsetof(struct tcphdr,
  3275. check)))
  3276. err = -EPROTO;
  3277. return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
  3278. case IPPROTO_UDP:
  3279. err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
  3280. off + sizeof(struct udphdr));
  3281. if (!err && !skb_partial_csum_set(skb, off,
  3282. offsetof(struct udphdr,
  3283. check)))
  3284. err = -EPROTO;
  3285. return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
  3286. }
  3287. return ERR_PTR(-EPROTO);
  3288. }
  3289. /* This value should be large enough to cover a tagged ethernet header plus
  3290. * maximally sized IP and TCP or UDP headers.
  3291. */
  3292. #define MAX_IP_HDR_LEN 128
  3293. static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
  3294. {
  3295. unsigned int off;
  3296. bool fragment;
  3297. __sum16 *csum;
  3298. int err;
  3299. fragment = false;
  3300. err = skb_maybe_pull_tail(skb,
  3301. sizeof(struct iphdr),
  3302. MAX_IP_HDR_LEN);
  3303. if (err < 0)
  3304. goto out;
  3305. if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF))
  3306. fragment = true;
  3307. off = ip_hdrlen(skb);
  3308. err = -EPROTO;
  3309. if (fragment)
  3310. goto out;
  3311. csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
  3312. if (IS_ERR(csum))
  3313. return PTR_ERR(csum);
  3314. if (recalculate)
  3315. *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
  3316. ip_hdr(skb)->daddr,
  3317. skb->len - off,
  3318. ip_hdr(skb)->protocol, 0);
  3319. err = 0;
  3320. out:
  3321. return err;
  3322. }
  3323. /* This value should be large enough to cover a tagged ethernet header plus
  3324. * an IPv6 header, all options, and a maximal TCP or UDP header.
  3325. */
  3326. #define MAX_IPV6_HDR_LEN 256
  3327. #define OPT_HDR(type, skb, off) \
  3328. (type *)(skb_network_header(skb) + (off))
  3329. static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
  3330. {
  3331. int err;
  3332. u8 nexthdr;
  3333. unsigned int off;
  3334. unsigned int len;
  3335. bool fragment;
  3336. bool done;
  3337. __sum16 *csum;
  3338. fragment = false;
  3339. done = false;
  3340. off = sizeof(struct ipv6hdr);
  3341. err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
  3342. if (err < 0)
  3343. goto out;
  3344. nexthdr = ipv6_hdr(skb)->nexthdr;
  3345. len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
  3346. while (off <= len && !done) {
  3347. switch (nexthdr) {
  3348. case IPPROTO_DSTOPTS:
  3349. case IPPROTO_HOPOPTS:
  3350. case IPPROTO_ROUTING: {
  3351. struct ipv6_opt_hdr *hp;
  3352. err = skb_maybe_pull_tail(skb,
  3353. off +
  3354. sizeof(struct ipv6_opt_hdr),
  3355. MAX_IPV6_HDR_LEN);
  3356. if (err < 0)
  3357. goto out;
  3358. hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
  3359. nexthdr = hp->nexthdr;
  3360. off += ipv6_optlen(hp);
  3361. break;
  3362. }
  3363. case IPPROTO_AH: {
  3364. struct ip_auth_hdr *hp;
  3365. err = skb_maybe_pull_tail(skb,
  3366. off +
  3367. sizeof(struct ip_auth_hdr),
  3368. MAX_IPV6_HDR_LEN);
  3369. if (err < 0)
  3370. goto out;
  3371. hp = OPT_HDR(struct ip_auth_hdr, skb, off);
  3372. nexthdr = hp->nexthdr;
  3373. off += ipv6_authlen(hp);
  3374. break;
  3375. }
  3376. case IPPROTO_FRAGMENT: {
  3377. struct frag_hdr *hp;
  3378. err = skb_maybe_pull_tail(skb,
  3379. off +
  3380. sizeof(struct frag_hdr),
  3381. MAX_IPV6_HDR_LEN);
  3382. if (err < 0)
  3383. goto out;
  3384. hp = OPT_HDR(struct frag_hdr, skb, off);
  3385. if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
  3386. fragment = true;
  3387. nexthdr = hp->nexthdr;
  3388. off += sizeof(struct frag_hdr);
  3389. break;
  3390. }
  3391. default:
  3392. done = true;
  3393. break;
  3394. }
  3395. }
  3396. err = -EPROTO;
  3397. if (!done || fragment)
  3398. goto out;
  3399. csum = skb_checksum_setup_ip(skb, nexthdr, off);
  3400. if (IS_ERR(csum))
  3401. return PTR_ERR(csum);
  3402. if (recalculate)
  3403. *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
  3404. &ipv6_hdr(skb)->daddr,
  3405. skb->len - off, nexthdr, 0);
  3406. err = 0;
  3407. out:
  3408. return err;
  3409. }
  3410. /**
  3411. * skb_checksum_setup - set up partial checksum offset
  3412. * @skb: the skb to set up
  3413. * @recalculate: if true the pseudo-header checksum will be recalculated
  3414. */
  3415. int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
  3416. {
  3417. int err;
  3418. switch (skb->protocol) {
  3419. case htons(ETH_P_IP):
  3420. err = skb_checksum_setup_ipv4(skb, recalculate);
  3421. break;
  3422. case htons(ETH_P_IPV6):
  3423. err = skb_checksum_setup_ipv6(skb, recalculate);
  3424. break;
  3425. default:
  3426. err = -EPROTO;
  3427. break;
  3428. }
  3429. return err;
  3430. }
  3431. EXPORT_SYMBOL(skb_checksum_setup);
  3432. void __skb_warn_lro_forwarding(const struct sk_buff *skb)
  3433. {
  3434. net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
  3435. skb->dev->name);
  3436. }
  3437. EXPORT_SYMBOL(__skb_warn_lro_forwarding);
  3438. void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
  3439. {
  3440. if (head_stolen) {
  3441. skb_release_head_state(skb);
  3442. kmem_cache_free(skbuff_head_cache, skb);
  3443. } else {
  3444. __kfree_skb(skb);
  3445. }
  3446. }
  3447. EXPORT_SYMBOL(kfree_skb_partial);
  3448. /**
  3449. * skb_try_coalesce - try to merge skb to prior one
  3450. * @to: prior buffer
  3451. * @from: buffer to add
  3452. * @fragstolen: pointer to boolean
  3453. * @delta_truesize: how much more was allocated than was requested
  3454. */
  3455. bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
  3456. bool *fragstolen, int *delta_truesize)
  3457. {
  3458. int i, delta, len = from->len;
  3459. *fragstolen = false;
  3460. if (skb_cloned(to))
  3461. return false;
  3462. if (len <= skb_tailroom(to)) {
  3463. if (len)
  3464. BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
  3465. *delta_truesize = 0;
  3466. return true;
  3467. }
  3468. if (skb_has_frag_list(to) || skb_has_frag_list(from))
  3469. return false;
  3470. if (skb_headlen(from) != 0) {
  3471. struct page *page;
  3472. unsigned int offset;
  3473. if (skb_shinfo(to)->nr_frags +
  3474. skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
  3475. return false;
  3476. if (skb_head_is_locked(from))
  3477. return false;
  3478. delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
  3479. page = virt_to_head_page(from->head);
  3480. offset = from->data - (unsigned char *)page_address(page);
  3481. skb_fill_page_desc(to, skb_shinfo(to)->nr_frags,
  3482. page, offset, skb_headlen(from));
  3483. *fragstolen = true;
  3484. } else {
  3485. if (skb_shinfo(to)->nr_frags +
  3486. skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS)
  3487. return false;
  3488. delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
  3489. }
  3490. WARN_ON_ONCE(delta < len);
  3491. memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags,
  3492. skb_shinfo(from)->frags,
  3493. skb_shinfo(from)->nr_frags * sizeof(skb_frag_t));
  3494. skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags;
  3495. if (!skb_cloned(from))
  3496. skb_shinfo(from)->nr_frags = 0;
  3497. /* if the skb is not cloned this does nothing
  3498. * since we set nr_frags to 0.
  3499. */
  3500. for (i = 0; i < skb_shinfo(from)->nr_frags; i++)
  3501. skb_frag_ref(from, i);
  3502. to->truesize += delta;
  3503. to->len += len;
  3504. to->data_len += len;
  3505. *delta_truesize = delta;
  3506. return true;
  3507. }
  3508. EXPORT_SYMBOL(skb_try_coalesce);
  3509. /**
  3510. * skb_scrub_packet - scrub an skb
  3511. *
  3512. * @skb: buffer to clean
  3513. * @xnet: packet is crossing netns
  3514. *
  3515. * skb_scrub_packet can be used after encapsulating or decapsulting a packet
  3516. * into/from a tunnel. Some information have to be cleared during these
  3517. * operations.
  3518. * skb_scrub_packet can also be used to clean a skb before injecting it in
  3519. * another namespace (@xnet == true). We have to clear all information in the
  3520. * skb that could impact namespace isolation.
  3521. */
  3522. void skb_scrub_packet(struct sk_buff *skb, bool xnet)
  3523. {
  3524. if (xnet)
  3525. skb_orphan(skb);
  3526. skb->tstamp.tv64 = 0;
  3527. skb->pkt_type = PACKET_HOST;
  3528. skb->skb_iif = 0;
  3529. skb->ignore_df = 0;
  3530. skb_dst_drop(skb);
  3531. skb->mark = 0;
  3532. skb_init_secmark(skb);
  3533. secpath_reset(skb);
  3534. nf_reset(skb);
  3535. nf_reset_trace(skb);
  3536. }
  3537. EXPORT_SYMBOL_GPL(skb_scrub_packet);
  3538. /**
  3539. * skb_gso_transport_seglen - Return length of individual segments of a gso packet
  3540. *
  3541. * @skb: GSO skb
  3542. *
  3543. * skb_gso_transport_seglen is used to determine the real size of the
  3544. * individual segments, including Layer4 headers (TCP/UDP).
  3545. *
  3546. * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
  3547. */
  3548. unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
  3549. {
  3550. const struct skb_shared_info *shinfo = skb_shinfo(skb);
  3551. unsigned int thlen = 0;
  3552. if (skb->encapsulation) {
  3553. thlen = skb_inner_transport_header(skb) -
  3554. skb_transport_header(skb);
  3555. if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
  3556. thlen += inner_tcp_hdrlen(skb);
  3557. } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
  3558. thlen = tcp_hdrlen(skb);
  3559. }
  3560. /* UFO sets gso_size to the size of the fragmentation
  3561. * payload, i.e. the size of the L4 (UDP) header is already
  3562. * accounted for.
  3563. */
  3564. return thlen + shinfo->gso_size;
  3565. }
  3566. EXPORT_SYMBOL_GPL(skb_gso_transport_seglen);
  3567. static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
  3568. {
  3569. if (skb_cow(skb, skb_headroom(skb)) < 0) {
  3570. kfree_skb(skb);
  3571. return NULL;
  3572. }
  3573. memmove(skb->data - ETH_HLEN, skb->data - VLAN_ETH_HLEN, 2 * ETH_ALEN);
  3574. skb->mac_header += VLAN_HLEN;
  3575. return skb;
  3576. }
  3577. struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
  3578. {
  3579. struct vlan_hdr *vhdr;
  3580. u16 vlan_tci;
  3581. if (unlikely(skb_vlan_tag_present(skb))) {
  3582. /* vlan_tci is already set-up so leave this for another time */
  3583. return skb;
  3584. }
  3585. skb = skb_share_check(skb, GFP_ATOMIC);
  3586. if (unlikely(!skb))
  3587. goto err_free;
  3588. if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
  3589. goto err_free;
  3590. vhdr = (struct vlan_hdr *)skb->data;
  3591. vlan_tci = ntohs(vhdr->h_vlan_TCI);
  3592. __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
  3593. skb_pull_rcsum(skb, VLAN_HLEN);
  3594. vlan_set_encap_proto(skb, vhdr);
  3595. skb = skb_reorder_vlan_header(skb);
  3596. if (unlikely(!skb))
  3597. goto err_free;
  3598. skb_reset_network_header(skb);
  3599. skb_reset_transport_header(skb);
  3600. skb_reset_mac_len(skb);
  3601. return skb;
  3602. err_free:
  3603. kfree_skb(skb);
  3604. return NULL;
  3605. }
  3606. EXPORT_SYMBOL(skb_vlan_untag);
  3607. int skb_ensure_writable(struct sk_buff *skb, int write_len)
  3608. {
  3609. if (!pskb_may_pull(skb, write_len))
  3610. return -ENOMEM;
  3611. if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
  3612. return 0;
  3613. return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
  3614. }
  3615. EXPORT_SYMBOL(skb_ensure_writable);
  3616. /* remove VLAN header from packet and update csum accordingly. */
  3617. static int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci)
  3618. {
  3619. struct vlan_hdr *vhdr;
  3620. unsigned int offset = skb->data - skb_mac_header(skb);
  3621. int err;
  3622. __skb_push(skb, offset);
  3623. err = skb_ensure_writable(skb, VLAN_ETH_HLEN);
  3624. if (unlikely(err))
  3625. goto pull;
  3626. skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
  3627. vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
  3628. *vlan_tci = ntohs(vhdr->h_vlan_TCI);
  3629. memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
  3630. __skb_pull(skb, VLAN_HLEN);
  3631. vlan_set_encap_proto(skb, vhdr);
  3632. skb->mac_header += VLAN_HLEN;
  3633. if (skb_network_offset(skb) < ETH_HLEN)
  3634. skb_set_network_header(skb, ETH_HLEN);
  3635. skb_reset_mac_len(skb);
  3636. pull:
  3637. __skb_pull(skb, offset);
  3638. return err;
  3639. }
  3640. int skb_vlan_pop(struct sk_buff *skb)
  3641. {
  3642. u16 vlan_tci;
  3643. __be16 vlan_proto;
  3644. int err;
  3645. if (likely(skb_vlan_tag_present(skb))) {
  3646. skb->vlan_tci = 0;
  3647. } else {
  3648. if (unlikely((skb->protocol != htons(ETH_P_8021Q) &&
  3649. skb->protocol != htons(ETH_P_8021AD)) ||
  3650. skb->len < VLAN_ETH_HLEN))
  3651. return 0;
  3652. err = __skb_vlan_pop(skb, &vlan_tci);
  3653. if (err)
  3654. return err;
  3655. }
  3656. /* move next vlan tag to hw accel tag */
  3657. if (likely((skb->protocol != htons(ETH_P_8021Q) &&
  3658. skb->protocol != htons(ETH_P_8021AD)) ||
  3659. skb->len < VLAN_ETH_HLEN))
  3660. return 0;
  3661. vlan_proto = skb->protocol;
  3662. err = __skb_vlan_pop(skb, &vlan_tci);
  3663. if (unlikely(err))
  3664. return err;
  3665. __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
  3666. return 0;
  3667. }
  3668. EXPORT_SYMBOL(skb_vlan_pop);
  3669. int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
  3670. {
  3671. if (skb_vlan_tag_present(skb)) {
  3672. unsigned int offset = skb->data - skb_mac_header(skb);
  3673. int err;
  3674. /* __vlan_insert_tag expect skb->data pointing to mac header.
  3675. * So change skb->data before calling it and change back to
  3676. * original position later
  3677. */
  3678. __skb_push(skb, offset);
  3679. err = __vlan_insert_tag(skb, skb->vlan_proto,
  3680. skb_vlan_tag_get(skb));
  3681. if (err)
  3682. return err;
  3683. skb->protocol = skb->vlan_proto;
  3684. skb->mac_len += VLAN_HLEN;
  3685. __skb_pull(skb, offset);
  3686. if (skb->ip_summed == CHECKSUM_COMPLETE)
  3687. skb->csum = csum_add(skb->csum, csum_partial(skb->data
  3688. + (2 * ETH_ALEN), VLAN_HLEN, 0));
  3689. }
  3690. __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
  3691. return 0;
  3692. }
  3693. EXPORT_SYMBOL(skb_vlan_push);
  3694. /**
  3695. * alloc_skb_with_frags - allocate skb with page frags
  3696. *
  3697. * @header_len: size of linear part
  3698. * @data_len: needed length in frags
  3699. * @max_page_order: max page order desired.
  3700. * @errcode: pointer to error code if any
  3701. * @gfp_mask: allocation mask
  3702. *
  3703. * This can be used to allocate a paged skb, given a maximal order for frags.
  3704. */
  3705. struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
  3706. unsigned long data_len,
  3707. int max_page_order,
  3708. int *errcode,
  3709. gfp_t gfp_mask)
  3710. {
  3711. int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
  3712. unsigned long chunk;
  3713. struct sk_buff *skb;
  3714. struct page *page;
  3715. gfp_t gfp_head;
  3716. int i;
  3717. *errcode = -EMSGSIZE;
  3718. /* Note this test could be relaxed, if we succeed to allocate
  3719. * high order pages...
  3720. */
  3721. if (npages > MAX_SKB_FRAGS)
  3722. return NULL;
  3723. gfp_head = gfp_mask;
  3724. if (gfp_head & __GFP_WAIT)
  3725. gfp_head |= __GFP_REPEAT;
  3726. *errcode = -ENOBUFS;
  3727. skb = alloc_skb(header_len, gfp_head);
  3728. if (!skb)
  3729. return NULL;
  3730. skb->truesize += npages << PAGE_SHIFT;
  3731. for (i = 0; npages > 0; i++) {
  3732. int order = max_page_order;
  3733. while (order) {
  3734. if (npages >= 1 << order) {
  3735. page = alloc_pages(gfp_mask |
  3736. __GFP_COMP |
  3737. __GFP_NOWARN |
  3738. __GFP_NORETRY,
  3739. order);
  3740. if (page)
  3741. goto fill_page;
  3742. /* Do not retry other high order allocations */
  3743. order = 1;
  3744. max_page_order = 0;
  3745. }
  3746. order--;
  3747. }
  3748. page = alloc_page(gfp_mask);
  3749. if (!page)
  3750. goto failure;
  3751. fill_page:
  3752. chunk = min_t(unsigned long, data_len,
  3753. PAGE_SIZE << order);
  3754. skb_fill_page_desc(skb, i, page, 0, chunk);
  3755. data_len -= chunk;
  3756. npages -= 1 << order;
  3757. }
  3758. return skb;
  3759. failure:
  3760. kfree_skb(skb);
  3761. return NULL;
  3762. }
  3763. EXPORT_SYMBOL(alloc_skb_with_frags);