skbuff.c 100 KB

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