skbuff.c 81 KB

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