skbuff.c 86 KB

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