skbuff.c 88 KB

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