skbuff.c 87 KB

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