skbuff.c 85 KB

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