skbuff.c 87 KB

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