skbuff.c 82 KB

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