skbuff.c 105 KB

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