skbuff.c 86 KB

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