skbuff.c 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435
  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 | GFP_DMA))) {
  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. .nr_pages_max = MAX_SKB_FRAGS,
  1535. .flags = flags,
  1536. .ops = &sock_pipe_buf_ops,
  1537. .spd_release = sock_spd_release,
  1538. };
  1539. struct sk_buff *frag_iter;
  1540. struct sock *sk = skb->sk;
  1541. int ret = 0;
  1542. /*
  1543. * __skb_splice_bits() only fails if the output has no room left,
  1544. * so no point in going over the frag_list for the error case.
  1545. */
  1546. if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk))
  1547. goto done;
  1548. else if (!tlen)
  1549. goto done;
  1550. /*
  1551. * now see if we have a frag_list to map
  1552. */
  1553. skb_walk_frags(skb, frag_iter) {
  1554. if (!tlen)
  1555. break;
  1556. if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk))
  1557. break;
  1558. }
  1559. done:
  1560. if (spd.nr_pages) {
  1561. /*
  1562. * Drop the socket lock, otherwise we have reverse
  1563. * locking dependencies between sk_lock and i_mutex
  1564. * here as compared to sendfile(). We enter here
  1565. * with the socket lock held, and splice_to_pipe() will
  1566. * grab the pipe inode lock. For sendfile() emulation,
  1567. * we call into ->sendpage() with the i_mutex lock held
  1568. * and networking will grab the socket lock.
  1569. */
  1570. release_sock(sk);
  1571. ret = splice_to_pipe(pipe, &spd);
  1572. lock_sock(sk);
  1573. }
  1574. return ret;
  1575. }
  1576. /**
  1577. * skb_store_bits - store bits from kernel buffer to skb
  1578. * @skb: destination buffer
  1579. * @offset: offset in destination
  1580. * @from: source buffer
  1581. * @len: number of bytes to copy
  1582. *
  1583. * Copy the specified number of bytes from the source buffer to the
  1584. * destination skb. This function handles all the messy bits of
  1585. * traversing fragment lists and such.
  1586. */
  1587. int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
  1588. {
  1589. int start = skb_headlen(skb);
  1590. struct sk_buff *frag_iter;
  1591. int i, copy;
  1592. if (offset > (int)skb->len - len)
  1593. goto fault;
  1594. if ((copy = start - offset) > 0) {
  1595. if (copy > len)
  1596. copy = len;
  1597. skb_copy_to_linear_data_offset(skb, offset, from, copy);
  1598. if ((len -= copy) == 0)
  1599. return 0;
  1600. offset += copy;
  1601. from += copy;
  1602. }
  1603. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1604. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  1605. int end;
  1606. WARN_ON(start > offset + len);
  1607. end = start + skb_frag_size(frag);
  1608. if ((copy = end - offset) > 0) {
  1609. u8 *vaddr;
  1610. if (copy > len)
  1611. copy = len;
  1612. vaddr = kmap_atomic(skb_frag_page(frag));
  1613. memcpy(vaddr + frag->page_offset + offset - start,
  1614. from, copy);
  1615. kunmap_atomic(vaddr);
  1616. if ((len -= copy) == 0)
  1617. return 0;
  1618. offset += copy;
  1619. from += copy;
  1620. }
  1621. start = end;
  1622. }
  1623. skb_walk_frags(skb, frag_iter) {
  1624. int end;
  1625. WARN_ON(start > offset + len);
  1626. end = start + frag_iter->len;
  1627. if ((copy = end - offset) > 0) {
  1628. if (copy > len)
  1629. copy = len;
  1630. if (skb_store_bits(frag_iter, offset - start,
  1631. from, copy))
  1632. goto fault;
  1633. if ((len -= copy) == 0)
  1634. return 0;
  1635. offset += copy;
  1636. from += copy;
  1637. }
  1638. start = end;
  1639. }
  1640. if (!len)
  1641. return 0;
  1642. fault:
  1643. return -EFAULT;
  1644. }
  1645. EXPORT_SYMBOL(skb_store_bits);
  1646. /* Checksum skb data. */
  1647. __wsum skb_checksum(const struct sk_buff *skb, int offset,
  1648. int len, __wsum csum)
  1649. {
  1650. int start = skb_headlen(skb);
  1651. int i, copy = start - offset;
  1652. struct sk_buff *frag_iter;
  1653. int pos = 0;
  1654. /* Checksum header. */
  1655. if (copy > 0) {
  1656. if (copy > len)
  1657. copy = len;
  1658. csum = csum_partial(skb->data + offset, copy, csum);
  1659. if ((len -= copy) == 0)
  1660. return csum;
  1661. offset += copy;
  1662. pos = copy;
  1663. }
  1664. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1665. int end;
  1666. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  1667. WARN_ON(start > offset + len);
  1668. end = start + skb_frag_size(frag);
  1669. if ((copy = end - offset) > 0) {
  1670. __wsum csum2;
  1671. u8 *vaddr;
  1672. if (copy > len)
  1673. copy = len;
  1674. vaddr = kmap_atomic(skb_frag_page(frag));
  1675. csum2 = csum_partial(vaddr + frag->page_offset +
  1676. offset - start, copy, 0);
  1677. kunmap_atomic(vaddr);
  1678. csum = csum_block_add(csum, csum2, pos);
  1679. if (!(len -= copy))
  1680. return csum;
  1681. offset += copy;
  1682. pos += copy;
  1683. }
  1684. start = end;
  1685. }
  1686. skb_walk_frags(skb, frag_iter) {
  1687. int end;
  1688. WARN_ON(start > offset + len);
  1689. end = start + frag_iter->len;
  1690. if ((copy = end - offset) > 0) {
  1691. __wsum csum2;
  1692. if (copy > len)
  1693. copy = len;
  1694. csum2 = skb_checksum(frag_iter, offset - start,
  1695. copy, 0);
  1696. csum = csum_block_add(csum, csum2, pos);
  1697. if ((len -= copy) == 0)
  1698. return csum;
  1699. offset += copy;
  1700. pos += copy;
  1701. }
  1702. start = end;
  1703. }
  1704. BUG_ON(len);
  1705. return csum;
  1706. }
  1707. EXPORT_SYMBOL(skb_checksum);
  1708. /* Both of above in one bottle. */
  1709. __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
  1710. u8 *to, int len, __wsum csum)
  1711. {
  1712. int start = skb_headlen(skb);
  1713. int i, copy = start - offset;
  1714. struct sk_buff *frag_iter;
  1715. int pos = 0;
  1716. /* Copy header. */
  1717. if (copy > 0) {
  1718. if (copy > len)
  1719. copy = len;
  1720. csum = csum_partial_copy_nocheck(skb->data + offset, to,
  1721. copy, csum);
  1722. if ((len -= copy) == 0)
  1723. return csum;
  1724. offset += copy;
  1725. to += copy;
  1726. pos = copy;
  1727. }
  1728. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1729. int end;
  1730. WARN_ON(start > offset + len);
  1731. end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
  1732. if ((copy = end - offset) > 0) {
  1733. __wsum csum2;
  1734. u8 *vaddr;
  1735. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  1736. if (copy > len)
  1737. copy = len;
  1738. vaddr = kmap_atomic(skb_frag_page(frag));
  1739. csum2 = csum_partial_copy_nocheck(vaddr +
  1740. frag->page_offset +
  1741. offset - start, to,
  1742. copy, 0);
  1743. kunmap_atomic(vaddr);
  1744. csum = csum_block_add(csum, csum2, pos);
  1745. if (!(len -= copy))
  1746. return csum;
  1747. offset += copy;
  1748. to += copy;
  1749. pos += copy;
  1750. }
  1751. start = end;
  1752. }
  1753. skb_walk_frags(skb, frag_iter) {
  1754. __wsum csum2;
  1755. int end;
  1756. WARN_ON(start > offset + len);
  1757. end = start + frag_iter->len;
  1758. if ((copy = end - offset) > 0) {
  1759. if (copy > len)
  1760. copy = len;
  1761. csum2 = skb_copy_and_csum_bits(frag_iter,
  1762. offset - start,
  1763. to, copy, 0);
  1764. csum = csum_block_add(csum, csum2, pos);
  1765. if ((len -= copy) == 0)
  1766. return csum;
  1767. offset += copy;
  1768. to += copy;
  1769. pos += copy;
  1770. }
  1771. start = end;
  1772. }
  1773. BUG_ON(len);
  1774. return csum;
  1775. }
  1776. EXPORT_SYMBOL(skb_copy_and_csum_bits);
  1777. void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
  1778. {
  1779. __wsum csum;
  1780. long csstart;
  1781. if (skb->ip_summed == CHECKSUM_PARTIAL)
  1782. csstart = skb_checksum_start_offset(skb);
  1783. else
  1784. csstart = skb_headlen(skb);
  1785. BUG_ON(csstart > skb_headlen(skb));
  1786. skb_copy_from_linear_data(skb, to, csstart);
  1787. csum = 0;
  1788. if (csstart != skb->len)
  1789. csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
  1790. skb->len - csstart, 0);
  1791. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  1792. long csstuff = csstart + skb->csum_offset;
  1793. *((__sum16 *)(to + csstuff)) = csum_fold(csum);
  1794. }
  1795. }
  1796. EXPORT_SYMBOL(skb_copy_and_csum_dev);
  1797. /**
  1798. * skb_dequeue - remove from the head of the queue
  1799. * @list: list to dequeue from
  1800. *
  1801. * Remove the head of the list. The list lock is taken so the function
  1802. * may be used safely with other locking list functions. The head item is
  1803. * returned or %NULL if the list is empty.
  1804. */
  1805. struct sk_buff *skb_dequeue(struct sk_buff_head *list)
  1806. {
  1807. unsigned long flags;
  1808. struct sk_buff *result;
  1809. spin_lock_irqsave(&list->lock, flags);
  1810. result = __skb_dequeue(list);
  1811. spin_unlock_irqrestore(&list->lock, flags);
  1812. return result;
  1813. }
  1814. EXPORT_SYMBOL(skb_dequeue);
  1815. /**
  1816. * skb_dequeue_tail - remove from the tail of the queue
  1817. * @list: list to dequeue from
  1818. *
  1819. * Remove the tail of the list. The list lock is taken so the function
  1820. * may be used safely with other locking list functions. The tail item is
  1821. * returned or %NULL if the list is empty.
  1822. */
  1823. struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
  1824. {
  1825. unsigned long flags;
  1826. struct sk_buff *result;
  1827. spin_lock_irqsave(&list->lock, flags);
  1828. result = __skb_dequeue_tail(list);
  1829. spin_unlock_irqrestore(&list->lock, flags);
  1830. return result;
  1831. }
  1832. EXPORT_SYMBOL(skb_dequeue_tail);
  1833. /**
  1834. * skb_queue_purge - empty a list
  1835. * @list: list to empty
  1836. *
  1837. * Delete all buffers on an &sk_buff list. Each buffer is removed from
  1838. * the list and one reference dropped. This function takes the list
  1839. * lock and is atomic with respect to other list locking functions.
  1840. */
  1841. void skb_queue_purge(struct sk_buff_head *list)
  1842. {
  1843. struct sk_buff *skb;
  1844. while ((skb = skb_dequeue(list)) != NULL)
  1845. kfree_skb(skb);
  1846. }
  1847. EXPORT_SYMBOL(skb_queue_purge);
  1848. /**
  1849. * skb_queue_head - queue a buffer at the list head
  1850. * @list: list to use
  1851. * @newsk: buffer to queue
  1852. *
  1853. * Queue a buffer at the start of the list. This function takes the
  1854. * list lock and can be used safely with other locking &sk_buff functions
  1855. * safely.
  1856. *
  1857. * A buffer cannot be placed on two lists at the same time.
  1858. */
  1859. void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
  1860. {
  1861. unsigned long flags;
  1862. spin_lock_irqsave(&list->lock, flags);
  1863. __skb_queue_head(list, newsk);
  1864. spin_unlock_irqrestore(&list->lock, flags);
  1865. }
  1866. EXPORT_SYMBOL(skb_queue_head);
  1867. /**
  1868. * skb_queue_tail - queue a buffer at the list tail
  1869. * @list: list to use
  1870. * @newsk: buffer to queue
  1871. *
  1872. * Queue a buffer at the tail of the list. This function takes the
  1873. * list lock and can be used safely with other locking &sk_buff functions
  1874. * safely.
  1875. *
  1876. * A buffer cannot be placed on two lists at the same time.
  1877. */
  1878. void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
  1879. {
  1880. unsigned long flags;
  1881. spin_lock_irqsave(&list->lock, flags);
  1882. __skb_queue_tail(list, newsk);
  1883. spin_unlock_irqrestore(&list->lock, flags);
  1884. }
  1885. EXPORT_SYMBOL(skb_queue_tail);
  1886. /**
  1887. * skb_unlink - remove a buffer from a list
  1888. * @skb: buffer to remove
  1889. * @list: list to use
  1890. *
  1891. * Remove a packet from a list. The list locks are taken and this
  1892. * function is atomic with respect to other list locked calls
  1893. *
  1894. * You must know what list the SKB is on.
  1895. */
  1896. void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
  1897. {
  1898. unsigned long flags;
  1899. spin_lock_irqsave(&list->lock, flags);
  1900. __skb_unlink(skb, list);
  1901. spin_unlock_irqrestore(&list->lock, flags);
  1902. }
  1903. EXPORT_SYMBOL(skb_unlink);
  1904. /**
  1905. * skb_append - append a buffer
  1906. * @old: buffer to insert after
  1907. * @newsk: buffer to insert
  1908. * @list: list to use
  1909. *
  1910. * Place a packet after a given packet in a list. The list locks are taken
  1911. * and this function is atomic with respect to other list locked calls.
  1912. * A buffer cannot be placed on two lists at the same time.
  1913. */
  1914. void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
  1915. {
  1916. unsigned long flags;
  1917. spin_lock_irqsave(&list->lock, flags);
  1918. __skb_queue_after(list, old, newsk);
  1919. spin_unlock_irqrestore(&list->lock, flags);
  1920. }
  1921. EXPORT_SYMBOL(skb_append);
  1922. /**
  1923. * skb_insert - insert a buffer
  1924. * @old: buffer to insert before
  1925. * @newsk: buffer to insert
  1926. * @list: list to use
  1927. *
  1928. * Place a packet before a given packet in a list. The list locks are
  1929. * taken and this function is atomic with respect to other list locked
  1930. * calls.
  1931. *
  1932. * A buffer cannot be placed on two lists at the same time.
  1933. */
  1934. void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
  1935. {
  1936. unsigned long flags;
  1937. spin_lock_irqsave(&list->lock, flags);
  1938. __skb_insert(newsk, old->prev, old, list);
  1939. spin_unlock_irqrestore(&list->lock, flags);
  1940. }
  1941. EXPORT_SYMBOL(skb_insert);
  1942. static inline void skb_split_inside_header(struct sk_buff *skb,
  1943. struct sk_buff* skb1,
  1944. const u32 len, const int pos)
  1945. {
  1946. int i;
  1947. skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
  1948. pos - len);
  1949. /* And move data appendix as is. */
  1950. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
  1951. skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
  1952. skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
  1953. skb_shinfo(skb)->nr_frags = 0;
  1954. skb1->data_len = skb->data_len;
  1955. skb1->len += skb1->data_len;
  1956. skb->data_len = 0;
  1957. skb->len = len;
  1958. skb_set_tail_pointer(skb, len);
  1959. }
  1960. static inline void skb_split_no_header(struct sk_buff *skb,
  1961. struct sk_buff* skb1,
  1962. const u32 len, int pos)
  1963. {
  1964. int i, k = 0;
  1965. const int nfrags = skb_shinfo(skb)->nr_frags;
  1966. skb_shinfo(skb)->nr_frags = 0;
  1967. skb1->len = skb1->data_len = skb->len - len;
  1968. skb->len = len;
  1969. skb->data_len = len - pos;
  1970. for (i = 0; i < nfrags; i++) {
  1971. int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
  1972. if (pos + size > len) {
  1973. skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
  1974. if (pos < len) {
  1975. /* Split frag.
  1976. * We have two variants in this case:
  1977. * 1. Move all the frag to the second
  1978. * part, if it is possible. F.e.
  1979. * this approach is mandatory for TUX,
  1980. * where splitting is expensive.
  1981. * 2. Split is accurately. We make this.
  1982. */
  1983. skb_frag_ref(skb, i);
  1984. skb_shinfo(skb1)->frags[0].page_offset += len - pos;
  1985. skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
  1986. skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
  1987. skb_shinfo(skb)->nr_frags++;
  1988. }
  1989. k++;
  1990. } else
  1991. skb_shinfo(skb)->nr_frags++;
  1992. pos += size;
  1993. }
  1994. skb_shinfo(skb1)->nr_frags = k;
  1995. }
  1996. /**
  1997. * skb_split - Split fragmented skb to two parts at length len.
  1998. * @skb: the buffer to split
  1999. * @skb1: the buffer to receive the second part
  2000. * @len: new length for skb
  2001. */
  2002. void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
  2003. {
  2004. int pos = skb_headlen(skb);
  2005. if (len < pos) /* Split line is inside header. */
  2006. skb_split_inside_header(skb, skb1, len, pos);
  2007. else /* Second chunk has no header, nothing to copy. */
  2008. skb_split_no_header(skb, skb1, len, pos);
  2009. }
  2010. EXPORT_SYMBOL(skb_split);
  2011. /* Shifting from/to a cloned skb is a no-go.
  2012. *
  2013. * Caller cannot keep skb_shinfo related pointers past calling here!
  2014. */
  2015. static int skb_prepare_for_shift(struct sk_buff *skb)
  2016. {
  2017. return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
  2018. }
  2019. /**
  2020. * skb_shift - Shifts paged data partially from skb to another
  2021. * @tgt: buffer into which tail data gets added
  2022. * @skb: buffer from which the paged data comes from
  2023. * @shiftlen: shift up to this many bytes
  2024. *
  2025. * Attempts to shift up to shiftlen worth of bytes, which may be less than
  2026. * the length of the skb, from skb to tgt. Returns number bytes shifted.
  2027. * It's up to caller to free skb if everything was shifted.
  2028. *
  2029. * If @tgt runs out of frags, the whole operation is aborted.
  2030. *
  2031. * Skb cannot include anything else but paged data while tgt is allowed
  2032. * to have non-paged data as well.
  2033. *
  2034. * TODO: full sized shift could be optimized but that would need
  2035. * specialized skb free'er to handle frags without up-to-date nr_frags.
  2036. */
  2037. int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
  2038. {
  2039. int from, to, merge, todo;
  2040. struct skb_frag_struct *fragfrom, *fragto;
  2041. BUG_ON(shiftlen > skb->len);
  2042. BUG_ON(skb_headlen(skb)); /* Would corrupt stream */
  2043. todo = shiftlen;
  2044. from = 0;
  2045. to = skb_shinfo(tgt)->nr_frags;
  2046. fragfrom = &skb_shinfo(skb)->frags[from];
  2047. /* Actual merge is delayed until the point when we know we can
  2048. * commit all, so that we don't have to undo partial changes
  2049. */
  2050. if (!to ||
  2051. !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
  2052. fragfrom->page_offset)) {
  2053. merge = -1;
  2054. } else {
  2055. merge = to - 1;
  2056. todo -= skb_frag_size(fragfrom);
  2057. if (todo < 0) {
  2058. if (skb_prepare_for_shift(skb) ||
  2059. skb_prepare_for_shift(tgt))
  2060. return 0;
  2061. /* All previous frag pointers might be stale! */
  2062. fragfrom = &skb_shinfo(skb)->frags[from];
  2063. fragto = &skb_shinfo(tgt)->frags[merge];
  2064. skb_frag_size_add(fragto, shiftlen);
  2065. skb_frag_size_sub(fragfrom, shiftlen);
  2066. fragfrom->page_offset += shiftlen;
  2067. goto onlymerged;
  2068. }
  2069. from++;
  2070. }
  2071. /* Skip full, not-fitting skb to avoid expensive operations */
  2072. if ((shiftlen == skb->len) &&
  2073. (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
  2074. return 0;
  2075. if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
  2076. return 0;
  2077. while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
  2078. if (to == MAX_SKB_FRAGS)
  2079. return 0;
  2080. fragfrom = &skb_shinfo(skb)->frags[from];
  2081. fragto = &skb_shinfo(tgt)->frags[to];
  2082. if (todo >= skb_frag_size(fragfrom)) {
  2083. *fragto = *fragfrom;
  2084. todo -= skb_frag_size(fragfrom);
  2085. from++;
  2086. to++;
  2087. } else {
  2088. __skb_frag_ref(fragfrom);
  2089. fragto->page = fragfrom->page;
  2090. fragto->page_offset = fragfrom->page_offset;
  2091. skb_frag_size_set(fragto, todo);
  2092. fragfrom->page_offset += todo;
  2093. skb_frag_size_sub(fragfrom, todo);
  2094. todo = 0;
  2095. to++;
  2096. break;
  2097. }
  2098. }
  2099. /* Ready to "commit" this state change to tgt */
  2100. skb_shinfo(tgt)->nr_frags = to;
  2101. if (merge >= 0) {
  2102. fragfrom = &skb_shinfo(skb)->frags[0];
  2103. fragto = &skb_shinfo(tgt)->frags[merge];
  2104. skb_frag_size_add(fragto, skb_frag_size(fragfrom));
  2105. __skb_frag_unref(fragfrom);
  2106. }
  2107. /* Reposition in the original skb */
  2108. to = 0;
  2109. while (from < skb_shinfo(skb)->nr_frags)
  2110. skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
  2111. skb_shinfo(skb)->nr_frags = to;
  2112. BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
  2113. onlymerged:
  2114. /* Most likely the tgt won't ever need its checksum anymore, skb on
  2115. * the other hand might need it if it needs to be resent
  2116. */
  2117. tgt->ip_summed = CHECKSUM_PARTIAL;
  2118. skb->ip_summed = CHECKSUM_PARTIAL;
  2119. /* Yak, is it really working this way? Some helper please? */
  2120. skb->len -= shiftlen;
  2121. skb->data_len -= shiftlen;
  2122. skb->truesize -= shiftlen;
  2123. tgt->len += shiftlen;
  2124. tgt->data_len += shiftlen;
  2125. tgt->truesize += shiftlen;
  2126. return shiftlen;
  2127. }
  2128. /**
  2129. * skb_prepare_seq_read - Prepare a sequential read of skb data
  2130. * @skb: the buffer to read
  2131. * @from: lower offset of data to be read
  2132. * @to: upper offset of data to be read
  2133. * @st: state variable
  2134. *
  2135. * Initializes the specified state variable. Must be called before
  2136. * invoking skb_seq_read() for the first time.
  2137. */
  2138. void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
  2139. unsigned int to, struct skb_seq_state *st)
  2140. {
  2141. st->lower_offset = from;
  2142. st->upper_offset = to;
  2143. st->root_skb = st->cur_skb = skb;
  2144. st->frag_idx = st->stepped_offset = 0;
  2145. st->frag_data = NULL;
  2146. }
  2147. EXPORT_SYMBOL(skb_prepare_seq_read);
  2148. /**
  2149. * skb_seq_read - Sequentially read skb data
  2150. * @consumed: number of bytes consumed by the caller so far
  2151. * @data: destination pointer for data to be returned
  2152. * @st: state variable
  2153. *
  2154. * Reads a block of skb data at &consumed relative to the
  2155. * lower offset specified to skb_prepare_seq_read(). Assigns
  2156. * the head of the data block to &data and returns the length
  2157. * of the block or 0 if the end of the skb data or the upper
  2158. * offset has been reached.
  2159. *
  2160. * The caller is not required to consume all of the data
  2161. * returned, i.e. &consumed is typically set to the number
  2162. * of bytes already consumed and the next call to
  2163. * skb_seq_read() will return the remaining part of the block.
  2164. *
  2165. * Note 1: The size of each block of data returned can be arbitrary,
  2166. * this limitation is the cost for zerocopy seqeuental
  2167. * reads of potentially non linear data.
  2168. *
  2169. * Note 2: Fragment lists within fragments are not implemented
  2170. * at the moment, state->root_skb could be replaced with
  2171. * a stack for this purpose.
  2172. */
  2173. unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
  2174. struct skb_seq_state *st)
  2175. {
  2176. unsigned int block_limit, abs_offset = consumed + st->lower_offset;
  2177. skb_frag_t *frag;
  2178. if (unlikely(abs_offset >= st->upper_offset))
  2179. return 0;
  2180. next_skb:
  2181. block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
  2182. if (abs_offset < block_limit && !st->frag_data) {
  2183. *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
  2184. return block_limit - abs_offset;
  2185. }
  2186. if (st->frag_idx == 0 && !st->frag_data)
  2187. st->stepped_offset += skb_headlen(st->cur_skb);
  2188. while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
  2189. frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
  2190. block_limit = skb_frag_size(frag) + st->stepped_offset;
  2191. if (abs_offset < block_limit) {
  2192. if (!st->frag_data)
  2193. st->frag_data = kmap_atomic(skb_frag_page(frag));
  2194. *data = (u8 *) st->frag_data + frag->page_offset +
  2195. (abs_offset - st->stepped_offset);
  2196. return block_limit - abs_offset;
  2197. }
  2198. if (st->frag_data) {
  2199. kunmap_atomic(st->frag_data);
  2200. st->frag_data = NULL;
  2201. }
  2202. st->frag_idx++;
  2203. st->stepped_offset += skb_frag_size(frag);
  2204. }
  2205. if (st->frag_data) {
  2206. kunmap_atomic(st->frag_data);
  2207. st->frag_data = NULL;
  2208. }
  2209. if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
  2210. st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
  2211. st->frag_idx = 0;
  2212. goto next_skb;
  2213. } else if (st->cur_skb->next) {
  2214. st->cur_skb = st->cur_skb->next;
  2215. st->frag_idx = 0;
  2216. goto next_skb;
  2217. }
  2218. return 0;
  2219. }
  2220. EXPORT_SYMBOL(skb_seq_read);
  2221. /**
  2222. * skb_abort_seq_read - Abort a sequential read of skb data
  2223. * @st: state variable
  2224. *
  2225. * Must be called if skb_seq_read() was not called until it
  2226. * returned 0.
  2227. */
  2228. void skb_abort_seq_read(struct skb_seq_state *st)
  2229. {
  2230. if (st->frag_data)
  2231. kunmap_atomic(st->frag_data);
  2232. }
  2233. EXPORT_SYMBOL(skb_abort_seq_read);
  2234. #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
  2235. static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
  2236. struct ts_config *conf,
  2237. struct ts_state *state)
  2238. {
  2239. return skb_seq_read(offset, text, TS_SKB_CB(state));
  2240. }
  2241. static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
  2242. {
  2243. skb_abort_seq_read(TS_SKB_CB(state));
  2244. }
  2245. /**
  2246. * skb_find_text - Find a text pattern in skb data
  2247. * @skb: the buffer to look in
  2248. * @from: search offset
  2249. * @to: search limit
  2250. * @config: textsearch configuration
  2251. * @state: uninitialized textsearch state variable
  2252. *
  2253. * Finds a pattern in the skb data according to the specified
  2254. * textsearch configuration. Use textsearch_next() to retrieve
  2255. * subsequent occurrences of the pattern. Returns the offset
  2256. * to the first occurrence or UINT_MAX if no match was found.
  2257. */
  2258. unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
  2259. unsigned int to, struct ts_config *config,
  2260. struct ts_state *state)
  2261. {
  2262. unsigned int ret;
  2263. config->get_next_block = skb_ts_get_next_block;
  2264. config->finish = skb_ts_finish;
  2265. skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
  2266. ret = textsearch_find(config, state);
  2267. return (ret <= to - from ? ret : UINT_MAX);
  2268. }
  2269. EXPORT_SYMBOL(skb_find_text);
  2270. /**
  2271. * skb_append_datato_frags: - append the user data to a skb
  2272. * @sk: sock structure
  2273. * @skb: skb structure to be appened with user data.
  2274. * @getfrag: call back function to be used for getting the user data
  2275. * @from: pointer to user message iov
  2276. * @length: length of the iov message
  2277. *
  2278. * Description: This procedure append the user data in the fragment part
  2279. * of the skb if any page alloc fails user this procedure returns -ENOMEM
  2280. */
  2281. int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
  2282. int (*getfrag)(void *from, char *to, int offset,
  2283. int len, int odd, struct sk_buff *skb),
  2284. void *from, int length)
  2285. {
  2286. int frg_cnt = 0;
  2287. skb_frag_t *frag = NULL;
  2288. struct page *page = NULL;
  2289. int copy, left;
  2290. int offset = 0;
  2291. int ret;
  2292. do {
  2293. /* Return error if we don't have space for new frag */
  2294. frg_cnt = skb_shinfo(skb)->nr_frags;
  2295. if (frg_cnt >= MAX_SKB_FRAGS)
  2296. return -EFAULT;
  2297. /* allocate a new page for next frag */
  2298. page = alloc_pages(sk->sk_allocation, 0);
  2299. /* If alloc_page fails just return failure and caller will
  2300. * free previous allocated pages by doing kfree_skb()
  2301. */
  2302. if (page == NULL)
  2303. return -ENOMEM;
  2304. /* initialize the next frag */
  2305. skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
  2306. skb->truesize += PAGE_SIZE;
  2307. atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
  2308. /* get the new initialized frag */
  2309. frg_cnt = skb_shinfo(skb)->nr_frags;
  2310. frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
  2311. /* copy the user data to page */
  2312. left = PAGE_SIZE - frag->page_offset;
  2313. copy = (length > left)? left : length;
  2314. ret = getfrag(from, skb_frag_address(frag) + skb_frag_size(frag),
  2315. offset, copy, 0, skb);
  2316. if (ret < 0)
  2317. return -EFAULT;
  2318. /* copy was successful so update the size parameters */
  2319. skb_frag_size_add(frag, copy);
  2320. skb->len += copy;
  2321. skb->data_len += copy;
  2322. offset += copy;
  2323. length -= copy;
  2324. } while (length > 0);
  2325. return 0;
  2326. }
  2327. EXPORT_SYMBOL(skb_append_datato_frags);
  2328. /**
  2329. * skb_pull_rcsum - pull skb and update receive checksum
  2330. * @skb: buffer to update
  2331. * @len: length of data pulled
  2332. *
  2333. * This function performs an skb_pull on the packet and updates
  2334. * the CHECKSUM_COMPLETE checksum. It should be used on
  2335. * receive path processing instead of skb_pull unless you know
  2336. * that the checksum difference is zero (e.g., a valid IP header)
  2337. * or you are setting ip_summed to CHECKSUM_NONE.
  2338. */
  2339. unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
  2340. {
  2341. BUG_ON(len > skb->len);
  2342. skb->len -= len;
  2343. BUG_ON(skb->len < skb->data_len);
  2344. skb_postpull_rcsum(skb, skb->data, len);
  2345. return skb->data += len;
  2346. }
  2347. EXPORT_SYMBOL_GPL(skb_pull_rcsum);
  2348. /**
  2349. * skb_segment - Perform protocol segmentation on skb.
  2350. * @skb: buffer to segment
  2351. * @features: features for the output path (see dev->features)
  2352. *
  2353. * This function performs segmentation on the given skb. It returns
  2354. * a pointer to the first in a list of new skbs for the segments.
  2355. * In case of error it returns ERR_PTR(err).
  2356. */
  2357. struct sk_buff *skb_segment(struct sk_buff *skb, netdev_features_t features)
  2358. {
  2359. struct sk_buff *segs = NULL;
  2360. struct sk_buff *tail = NULL;
  2361. struct sk_buff *fskb = skb_shinfo(skb)->frag_list;
  2362. unsigned int mss = skb_shinfo(skb)->gso_size;
  2363. unsigned int doffset = skb->data - skb_mac_header(skb);
  2364. unsigned int offset = doffset;
  2365. unsigned int headroom;
  2366. unsigned int len;
  2367. int sg = !!(features & NETIF_F_SG);
  2368. int nfrags = skb_shinfo(skb)->nr_frags;
  2369. int err = -ENOMEM;
  2370. int i = 0;
  2371. int pos;
  2372. __skb_push(skb, doffset);
  2373. headroom = skb_headroom(skb);
  2374. pos = skb_headlen(skb);
  2375. do {
  2376. struct sk_buff *nskb;
  2377. skb_frag_t *frag;
  2378. int hsize;
  2379. int size;
  2380. len = skb->len - offset;
  2381. if (len > mss)
  2382. len = mss;
  2383. hsize = skb_headlen(skb) - offset;
  2384. if (hsize < 0)
  2385. hsize = 0;
  2386. if (hsize > len || !sg)
  2387. hsize = len;
  2388. if (!hsize && i >= nfrags) {
  2389. BUG_ON(fskb->len != len);
  2390. pos += len;
  2391. nskb = skb_clone(fskb, GFP_ATOMIC);
  2392. fskb = fskb->next;
  2393. if (unlikely(!nskb))
  2394. goto err;
  2395. hsize = skb_end_offset(nskb);
  2396. if (skb_cow_head(nskb, doffset + headroom)) {
  2397. kfree_skb(nskb);
  2398. goto err;
  2399. }
  2400. nskb->truesize += skb_end_offset(nskb) - hsize;
  2401. skb_release_head_state(nskb);
  2402. __skb_push(nskb, doffset);
  2403. } else {
  2404. nskb = alloc_skb(hsize + doffset + headroom,
  2405. GFP_ATOMIC);
  2406. if (unlikely(!nskb))
  2407. goto err;
  2408. skb_reserve(nskb, headroom);
  2409. __skb_put(nskb, doffset);
  2410. }
  2411. if (segs)
  2412. tail->next = nskb;
  2413. else
  2414. segs = nskb;
  2415. tail = nskb;
  2416. __copy_skb_header(nskb, skb);
  2417. nskb->mac_len = skb->mac_len;
  2418. /* nskb and skb might have different headroom */
  2419. if (nskb->ip_summed == CHECKSUM_PARTIAL)
  2420. nskb->csum_start += skb_headroom(nskb) - headroom;
  2421. skb_reset_mac_header(nskb);
  2422. skb_set_network_header(nskb, skb->mac_len);
  2423. nskb->transport_header = (nskb->network_header +
  2424. skb_network_header_len(skb));
  2425. skb_copy_from_linear_data(skb, nskb->data, doffset);
  2426. if (fskb != skb_shinfo(skb)->frag_list)
  2427. continue;
  2428. if (!sg) {
  2429. nskb->ip_summed = CHECKSUM_NONE;
  2430. nskb->csum = skb_copy_and_csum_bits(skb, offset,
  2431. skb_put(nskb, len),
  2432. len, 0);
  2433. continue;
  2434. }
  2435. frag = skb_shinfo(nskb)->frags;
  2436. skb_copy_from_linear_data_offset(skb, offset,
  2437. skb_put(nskb, hsize), hsize);
  2438. while (pos < offset + len && i < nfrags) {
  2439. *frag = skb_shinfo(skb)->frags[i];
  2440. __skb_frag_ref(frag);
  2441. size = skb_frag_size(frag);
  2442. if (pos < offset) {
  2443. frag->page_offset += offset - pos;
  2444. skb_frag_size_sub(frag, offset - pos);
  2445. }
  2446. skb_shinfo(nskb)->nr_frags++;
  2447. if (pos + size <= offset + len) {
  2448. i++;
  2449. pos += size;
  2450. } else {
  2451. skb_frag_size_sub(frag, pos + size - (offset + len));
  2452. goto skip_fraglist;
  2453. }
  2454. frag++;
  2455. }
  2456. if (pos < offset + len) {
  2457. struct sk_buff *fskb2 = fskb;
  2458. BUG_ON(pos + fskb->len != offset + len);
  2459. pos += fskb->len;
  2460. fskb = fskb->next;
  2461. if (fskb2->next) {
  2462. fskb2 = skb_clone(fskb2, GFP_ATOMIC);
  2463. if (!fskb2)
  2464. goto err;
  2465. } else
  2466. skb_get(fskb2);
  2467. SKB_FRAG_ASSERT(nskb);
  2468. skb_shinfo(nskb)->frag_list = fskb2;
  2469. }
  2470. skip_fraglist:
  2471. nskb->data_len = len - hsize;
  2472. nskb->len += nskb->data_len;
  2473. nskb->truesize += nskb->data_len;
  2474. } while ((offset += len) < skb->len);
  2475. return segs;
  2476. err:
  2477. while ((skb = segs)) {
  2478. segs = skb->next;
  2479. kfree_skb(skb);
  2480. }
  2481. return ERR_PTR(err);
  2482. }
  2483. EXPORT_SYMBOL_GPL(skb_segment);
  2484. int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
  2485. {
  2486. struct sk_buff *p = *head;
  2487. struct sk_buff *nskb;
  2488. struct skb_shared_info *skbinfo = skb_shinfo(skb);
  2489. struct skb_shared_info *pinfo = skb_shinfo(p);
  2490. unsigned int headroom;
  2491. unsigned int len = skb_gro_len(skb);
  2492. unsigned int offset = skb_gro_offset(skb);
  2493. unsigned int headlen = skb_headlen(skb);
  2494. unsigned int delta_truesize;
  2495. if (p->len + len >= 65536)
  2496. return -E2BIG;
  2497. if (pinfo->frag_list)
  2498. goto merge;
  2499. else if (headlen <= offset) {
  2500. skb_frag_t *frag;
  2501. skb_frag_t *frag2;
  2502. int i = skbinfo->nr_frags;
  2503. int nr_frags = pinfo->nr_frags + i;
  2504. offset -= headlen;
  2505. if (nr_frags > MAX_SKB_FRAGS)
  2506. return -E2BIG;
  2507. pinfo->nr_frags = nr_frags;
  2508. skbinfo->nr_frags = 0;
  2509. frag = pinfo->frags + nr_frags;
  2510. frag2 = skbinfo->frags + i;
  2511. do {
  2512. *--frag = *--frag2;
  2513. } while (--i);
  2514. frag->page_offset += offset;
  2515. skb_frag_size_sub(frag, offset);
  2516. /* all fragments truesize : remove (head size + sk_buff) */
  2517. delta_truesize = skb->truesize -
  2518. SKB_TRUESIZE(skb_end_offset(skb));
  2519. skb->truesize -= skb->data_len;
  2520. skb->len -= skb->data_len;
  2521. skb->data_len = 0;
  2522. NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
  2523. goto done;
  2524. } else if (skb->head_frag) {
  2525. int nr_frags = pinfo->nr_frags;
  2526. skb_frag_t *frag = pinfo->frags + nr_frags;
  2527. struct page *page = virt_to_head_page(skb->head);
  2528. unsigned int first_size = headlen - offset;
  2529. unsigned int first_offset;
  2530. if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
  2531. return -E2BIG;
  2532. first_offset = skb->data -
  2533. (unsigned char *)page_address(page) +
  2534. offset;
  2535. pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
  2536. frag->page.p = page;
  2537. frag->page_offset = first_offset;
  2538. skb_frag_size_set(frag, first_size);
  2539. memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
  2540. /* We dont need to clear skbinfo->nr_frags here */
  2541. delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
  2542. NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
  2543. goto done;
  2544. } else if (skb_gro_len(p) != pinfo->gso_size)
  2545. return -E2BIG;
  2546. headroom = skb_headroom(p);
  2547. nskb = alloc_skb(headroom + skb_gro_offset(p), GFP_ATOMIC);
  2548. if (unlikely(!nskb))
  2549. return -ENOMEM;
  2550. __copy_skb_header(nskb, p);
  2551. nskb->mac_len = p->mac_len;
  2552. skb_reserve(nskb, headroom);
  2553. __skb_put(nskb, skb_gro_offset(p));
  2554. skb_set_mac_header(nskb, skb_mac_header(p) - p->data);
  2555. skb_set_network_header(nskb, skb_network_offset(p));
  2556. skb_set_transport_header(nskb, skb_transport_offset(p));
  2557. __skb_pull(p, skb_gro_offset(p));
  2558. memcpy(skb_mac_header(nskb), skb_mac_header(p),
  2559. p->data - skb_mac_header(p));
  2560. *NAPI_GRO_CB(nskb) = *NAPI_GRO_CB(p);
  2561. skb_shinfo(nskb)->frag_list = p;
  2562. skb_shinfo(nskb)->gso_size = pinfo->gso_size;
  2563. pinfo->gso_size = 0;
  2564. skb_header_release(p);
  2565. nskb->prev = p;
  2566. nskb->data_len += p->len;
  2567. nskb->truesize += p->truesize;
  2568. nskb->len += p->len;
  2569. *head = nskb;
  2570. nskb->next = p->next;
  2571. p->next = NULL;
  2572. p = nskb;
  2573. merge:
  2574. delta_truesize = skb->truesize;
  2575. if (offset > headlen) {
  2576. unsigned int eat = offset - headlen;
  2577. skbinfo->frags[0].page_offset += eat;
  2578. skb_frag_size_sub(&skbinfo->frags[0], eat);
  2579. skb->data_len -= eat;
  2580. skb->len -= eat;
  2581. offset = headlen;
  2582. }
  2583. __skb_pull(skb, offset);
  2584. p->prev->next = skb;
  2585. p->prev = skb;
  2586. skb_header_release(skb);
  2587. done:
  2588. NAPI_GRO_CB(p)->count++;
  2589. p->data_len += len;
  2590. p->truesize += delta_truesize;
  2591. p->len += len;
  2592. NAPI_GRO_CB(skb)->same_flow = 1;
  2593. return 0;
  2594. }
  2595. EXPORT_SYMBOL_GPL(skb_gro_receive);
  2596. void __init skb_init(void)
  2597. {
  2598. skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
  2599. sizeof(struct sk_buff),
  2600. 0,
  2601. SLAB_HWCACHE_ALIGN|SLAB_PANIC,
  2602. NULL);
  2603. skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
  2604. (2*sizeof(struct sk_buff)) +
  2605. sizeof(atomic_t),
  2606. 0,
  2607. SLAB_HWCACHE_ALIGN|SLAB_PANIC,
  2608. NULL);
  2609. }
  2610. /**
  2611. * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
  2612. * @skb: Socket buffer containing the buffers to be mapped
  2613. * @sg: The scatter-gather list to map into
  2614. * @offset: The offset into the buffer's contents to start mapping
  2615. * @len: Length of buffer space to be mapped
  2616. *
  2617. * Fill the specified scatter-gather list with mappings/pointers into a
  2618. * region of the buffer space attached to a socket buffer.
  2619. */
  2620. static int
  2621. __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
  2622. {
  2623. int start = skb_headlen(skb);
  2624. int i, copy = start - offset;
  2625. struct sk_buff *frag_iter;
  2626. int elt = 0;
  2627. if (copy > 0) {
  2628. if (copy > len)
  2629. copy = len;
  2630. sg_set_buf(sg, skb->data + offset, copy);
  2631. elt++;
  2632. if ((len -= copy) == 0)
  2633. return elt;
  2634. offset += copy;
  2635. }
  2636. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  2637. int end;
  2638. WARN_ON(start > offset + len);
  2639. end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
  2640. if ((copy = end - offset) > 0) {
  2641. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  2642. if (copy > len)
  2643. copy = len;
  2644. sg_set_page(&sg[elt], skb_frag_page(frag), copy,
  2645. frag->page_offset+offset-start);
  2646. elt++;
  2647. if (!(len -= copy))
  2648. return elt;
  2649. offset += copy;
  2650. }
  2651. start = end;
  2652. }
  2653. skb_walk_frags(skb, frag_iter) {
  2654. int end;
  2655. WARN_ON(start > offset + len);
  2656. end = start + frag_iter->len;
  2657. if ((copy = end - offset) > 0) {
  2658. if (copy > len)
  2659. copy = len;
  2660. elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start,
  2661. copy);
  2662. if ((len -= copy) == 0)
  2663. return elt;
  2664. offset += copy;
  2665. }
  2666. start = end;
  2667. }
  2668. BUG_ON(len);
  2669. return elt;
  2670. }
  2671. int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
  2672. {
  2673. int nsg = __skb_to_sgvec(skb, sg, offset, len);
  2674. sg_mark_end(&sg[nsg - 1]);
  2675. return nsg;
  2676. }
  2677. EXPORT_SYMBOL_GPL(skb_to_sgvec);
  2678. /**
  2679. * skb_cow_data - Check that a socket buffer's data buffers are writable
  2680. * @skb: The socket buffer to check.
  2681. * @tailbits: Amount of trailing space to be added
  2682. * @trailer: Returned pointer to the skb where the @tailbits space begins
  2683. *
  2684. * Make sure that the data buffers attached to a socket buffer are
  2685. * writable. If they are not, private copies are made of the data buffers
  2686. * and the socket buffer is set to use these instead.
  2687. *
  2688. * If @tailbits is given, make sure that there is space to write @tailbits
  2689. * bytes of data beyond current end of socket buffer. @trailer will be
  2690. * set to point to the skb in which this space begins.
  2691. *
  2692. * The number of scatterlist elements required to completely map the
  2693. * COW'd and extended socket buffer will be returned.
  2694. */
  2695. int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
  2696. {
  2697. int copyflag;
  2698. int elt;
  2699. struct sk_buff *skb1, **skb_p;
  2700. /* If skb is cloned or its head is paged, reallocate
  2701. * head pulling out all the pages (pages are considered not writable
  2702. * at the moment even if they are anonymous).
  2703. */
  2704. if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
  2705. __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
  2706. return -ENOMEM;
  2707. /* Easy case. Most of packets will go this way. */
  2708. if (!skb_has_frag_list(skb)) {
  2709. /* A little of trouble, not enough of space for trailer.
  2710. * This should not happen, when stack is tuned to generate
  2711. * good frames. OK, on miss we reallocate and reserve even more
  2712. * space, 128 bytes is fair. */
  2713. if (skb_tailroom(skb) < tailbits &&
  2714. pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
  2715. return -ENOMEM;
  2716. /* Voila! */
  2717. *trailer = skb;
  2718. return 1;
  2719. }
  2720. /* Misery. We are in troubles, going to mincer fragments... */
  2721. elt = 1;
  2722. skb_p = &skb_shinfo(skb)->frag_list;
  2723. copyflag = 0;
  2724. while ((skb1 = *skb_p) != NULL) {
  2725. int ntail = 0;
  2726. /* The fragment is partially pulled by someone,
  2727. * this can happen on input. Copy it and everything
  2728. * after it. */
  2729. if (skb_shared(skb1))
  2730. copyflag = 1;
  2731. /* If the skb is the last, worry about trailer. */
  2732. if (skb1->next == NULL && tailbits) {
  2733. if (skb_shinfo(skb1)->nr_frags ||
  2734. skb_has_frag_list(skb1) ||
  2735. skb_tailroom(skb1) < tailbits)
  2736. ntail = tailbits + 128;
  2737. }
  2738. if (copyflag ||
  2739. skb_cloned(skb1) ||
  2740. ntail ||
  2741. skb_shinfo(skb1)->nr_frags ||
  2742. skb_has_frag_list(skb1)) {
  2743. struct sk_buff *skb2;
  2744. /* Fuck, we are miserable poor guys... */
  2745. if (ntail == 0)
  2746. skb2 = skb_copy(skb1, GFP_ATOMIC);
  2747. else
  2748. skb2 = skb_copy_expand(skb1,
  2749. skb_headroom(skb1),
  2750. ntail,
  2751. GFP_ATOMIC);
  2752. if (unlikely(skb2 == NULL))
  2753. return -ENOMEM;
  2754. if (skb1->sk)
  2755. skb_set_owner_w(skb2, skb1->sk);
  2756. /* Looking around. Are we still alive?
  2757. * OK, link new skb, drop old one */
  2758. skb2->next = skb1->next;
  2759. *skb_p = skb2;
  2760. kfree_skb(skb1);
  2761. skb1 = skb2;
  2762. }
  2763. elt++;
  2764. *trailer = skb1;
  2765. skb_p = &skb1->next;
  2766. }
  2767. return elt;
  2768. }
  2769. EXPORT_SYMBOL_GPL(skb_cow_data);
  2770. static void sock_rmem_free(struct sk_buff *skb)
  2771. {
  2772. struct sock *sk = skb->sk;
  2773. atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
  2774. }
  2775. /*
  2776. * Note: We dont mem charge error packets (no sk_forward_alloc changes)
  2777. */
  2778. int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
  2779. {
  2780. int len = skb->len;
  2781. if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
  2782. (unsigned int)sk->sk_rcvbuf)
  2783. return -ENOMEM;
  2784. skb_orphan(skb);
  2785. skb->sk = sk;
  2786. skb->destructor = sock_rmem_free;
  2787. atomic_add(skb->truesize, &sk->sk_rmem_alloc);
  2788. /* before exiting rcu section, make sure dst is refcounted */
  2789. skb_dst_force(skb);
  2790. skb_queue_tail(&sk->sk_error_queue, skb);
  2791. if (!sock_flag(sk, SOCK_DEAD))
  2792. sk->sk_data_ready(sk, len);
  2793. return 0;
  2794. }
  2795. EXPORT_SYMBOL(sock_queue_err_skb);
  2796. void skb_tstamp_tx(struct sk_buff *orig_skb,
  2797. struct skb_shared_hwtstamps *hwtstamps)
  2798. {
  2799. struct sock *sk = orig_skb->sk;
  2800. struct sock_exterr_skb *serr;
  2801. struct sk_buff *skb;
  2802. int err;
  2803. if (!sk)
  2804. return;
  2805. skb = skb_clone(orig_skb, GFP_ATOMIC);
  2806. if (!skb)
  2807. return;
  2808. if (hwtstamps) {
  2809. *skb_hwtstamps(skb) =
  2810. *hwtstamps;
  2811. } else {
  2812. /*
  2813. * no hardware time stamps available,
  2814. * so keep the shared tx_flags and only
  2815. * store software time stamp
  2816. */
  2817. skb->tstamp = ktime_get_real();
  2818. }
  2819. serr = SKB_EXT_ERR(skb);
  2820. memset(serr, 0, sizeof(*serr));
  2821. serr->ee.ee_errno = ENOMSG;
  2822. serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
  2823. err = sock_queue_err_skb(sk, skb);
  2824. if (err)
  2825. kfree_skb(skb);
  2826. }
  2827. EXPORT_SYMBOL_GPL(skb_tstamp_tx);
  2828. void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
  2829. {
  2830. struct sock *sk = skb->sk;
  2831. struct sock_exterr_skb *serr;
  2832. int err;
  2833. skb->wifi_acked_valid = 1;
  2834. skb->wifi_acked = acked;
  2835. serr = SKB_EXT_ERR(skb);
  2836. memset(serr, 0, sizeof(*serr));
  2837. serr->ee.ee_errno = ENOMSG;
  2838. serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
  2839. err = sock_queue_err_skb(sk, skb);
  2840. if (err)
  2841. kfree_skb(skb);
  2842. }
  2843. EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
  2844. /**
  2845. * skb_partial_csum_set - set up and verify partial csum values for packet
  2846. * @skb: the skb to set
  2847. * @start: the number of bytes after skb->data to start checksumming.
  2848. * @off: the offset from start to place the checksum.
  2849. *
  2850. * For untrusted partially-checksummed packets, we need to make sure the values
  2851. * for skb->csum_start and skb->csum_offset are valid so we don't oops.
  2852. *
  2853. * This function checks and sets those values and skb->ip_summed: if this
  2854. * returns false you should drop the packet.
  2855. */
  2856. bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
  2857. {
  2858. if (unlikely(start > skb_headlen(skb)) ||
  2859. unlikely((int)start + off > skb_headlen(skb) - 2)) {
  2860. net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n",
  2861. start, off, skb_headlen(skb));
  2862. return false;
  2863. }
  2864. skb->ip_summed = CHECKSUM_PARTIAL;
  2865. skb->csum_start = skb_headroom(skb) + start;
  2866. skb->csum_offset = off;
  2867. return true;
  2868. }
  2869. EXPORT_SYMBOL_GPL(skb_partial_csum_set);
  2870. void __skb_warn_lro_forwarding(const struct sk_buff *skb)
  2871. {
  2872. net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
  2873. skb->dev->name);
  2874. }
  2875. EXPORT_SYMBOL(__skb_warn_lro_forwarding);
  2876. void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
  2877. {
  2878. if (head_stolen)
  2879. kmem_cache_free(skbuff_head_cache, skb);
  2880. else
  2881. __kfree_skb(skb);
  2882. }
  2883. EXPORT_SYMBOL(kfree_skb_partial);
  2884. /**
  2885. * skb_try_coalesce - try to merge skb to prior one
  2886. * @to: prior buffer
  2887. * @from: buffer to add
  2888. * @fragstolen: pointer to boolean
  2889. * @delta_truesize: how much more was allocated than was requested
  2890. */
  2891. bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
  2892. bool *fragstolen, int *delta_truesize)
  2893. {
  2894. int i, delta, len = from->len;
  2895. *fragstolen = false;
  2896. if (skb_cloned(to))
  2897. return false;
  2898. if (len <= skb_tailroom(to)) {
  2899. BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
  2900. *delta_truesize = 0;
  2901. return true;
  2902. }
  2903. if (skb_has_frag_list(to) || skb_has_frag_list(from))
  2904. return false;
  2905. if (skb_headlen(from) != 0) {
  2906. struct page *page;
  2907. unsigned int offset;
  2908. if (skb_shinfo(to)->nr_frags +
  2909. skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
  2910. return false;
  2911. if (skb_head_is_locked(from))
  2912. return false;
  2913. delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
  2914. page = virt_to_head_page(from->head);
  2915. offset = from->data - (unsigned char *)page_address(page);
  2916. skb_fill_page_desc(to, skb_shinfo(to)->nr_frags,
  2917. page, offset, skb_headlen(from));
  2918. *fragstolen = true;
  2919. } else {
  2920. if (skb_shinfo(to)->nr_frags +
  2921. skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS)
  2922. return false;
  2923. delta = from->truesize -
  2924. SKB_TRUESIZE(skb_end_pointer(from) - from->head);
  2925. }
  2926. WARN_ON_ONCE(delta < len);
  2927. memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags,
  2928. skb_shinfo(from)->frags,
  2929. skb_shinfo(from)->nr_frags * sizeof(skb_frag_t));
  2930. skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags;
  2931. if (!skb_cloned(from))
  2932. skb_shinfo(from)->nr_frags = 0;
  2933. /* if the skb is cloned this does nothing since we set nr_frags to 0 */
  2934. for (i = 0; i < skb_shinfo(from)->nr_frags; i++)
  2935. skb_frag_ref(from, i);
  2936. to->truesize += delta;
  2937. to->len += len;
  2938. to->data_len += len;
  2939. *delta_truesize = delta;
  2940. return true;
  2941. }
  2942. EXPORT_SYMBOL(skb_try_coalesce);