skbuff.c 75 KB

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