skbuff.c 110 KB

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