skbuff.c 81 KB

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