skbuff.c 87 KB

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