skbuff.c 82 KB

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