skbuff.c 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063
  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. data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
  686. if (!data)
  687. goto nodata;
  688. /* Copy only real data... and, alas, header. This should be
  689. * optimized for the cases when header is void.
  690. */
  691. memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
  692. memcpy((struct skb_shared_info *)(data + size),
  693. skb_shinfo(skb),
  694. offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
  695. /* Check if we can avoid taking references on fragments if we own
  696. * the last reference on skb->head. (see skb_release_data())
  697. */
  698. if (!skb->cloned)
  699. fastpath = true;
  700. else {
  701. int delta = skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1;
  702. fastpath = atomic_read(&skb_shinfo(skb)->dataref) == delta;
  703. }
  704. if (fastpath) {
  705. kfree(skb->head);
  706. } else {
  707. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
  708. get_page(skb_shinfo(skb)->frags[i].page);
  709. if (skb_has_frag_list(skb))
  710. skb_clone_fraglist(skb);
  711. skb_release_data(skb);
  712. }
  713. off = (data + nhead) - skb->head;
  714. skb->head = data;
  715. skb->data += off;
  716. #ifdef NET_SKBUFF_DATA_USES_OFFSET
  717. skb->end = size;
  718. off = nhead;
  719. #else
  720. skb->end = skb->head + size;
  721. #endif
  722. /* {transport,network,mac}_header and tail are relative to skb->head */
  723. skb->tail += off;
  724. skb->transport_header += off;
  725. skb->network_header += off;
  726. if (skb_mac_header_was_set(skb))
  727. skb->mac_header += off;
  728. /* Only adjust this if it actually is csum_start rather than csum */
  729. if (skb->ip_summed == CHECKSUM_PARTIAL)
  730. skb->csum_start += nhead;
  731. skb->cloned = 0;
  732. skb->hdr_len = 0;
  733. skb->nohdr = 0;
  734. atomic_set(&skb_shinfo(skb)->dataref, 1);
  735. return 0;
  736. nodata:
  737. return -ENOMEM;
  738. }
  739. EXPORT_SYMBOL(pskb_expand_head);
  740. /* Make private copy of skb with writable head and some headroom */
  741. struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
  742. {
  743. struct sk_buff *skb2;
  744. int delta = headroom - skb_headroom(skb);
  745. if (delta <= 0)
  746. skb2 = pskb_copy(skb, GFP_ATOMIC);
  747. else {
  748. skb2 = skb_clone(skb, GFP_ATOMIC);
  749. if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
  750. GFP_ATOMIC)) {
  751. kfree_skb(skb2);
  752. skb2 = NULL;
  753. }
  754. }
  755. return skb2;
  756. }
  757. EXPORT_SYMBOL(skb_realloc_headroom);
  758. /**
  759. * skb_copy_expand - copy and expand sk_buff
  760. * @skb: buffer to copy
  761. * @newheadroom: new free bytes at head
  762. * @newtailroom: new free bytes at tail
  763. * @gfp_mask: allocation priority
  764. *
  765. * Make a copy of both an &sk_buff and its data and while doing so
  766. * allocate additional space.
  767. *
  768. * This is used when the caller wishes to modify the data and needs a
  769. * private copy of the data to alter as well as more space for new fields.
  770. * Returns %NULL on failure or the pointer to the buffer
  771. * on success. The returned buffer has a reference count of 1.
  772. *
  773. * You must pass %GFP_ATOMIC as the allocation priority if this function
  774. * is called from an interrupt.
  775. */
  776. struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
  777. int newheadroom, int newtailroom,
  778. gfp_t gfp_mask)
  779. {
  780. /*
  781. * Allocate the copy buffer
  782. */
  783. struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
  784. gfp_mask);
  785. int oldheadroom = skb_headroom(skb);
  786. int head_copy_len, head_copy_off;
  787. int off;
  788. if (!n)
  789. return NULL;
  790. skb_reserve(n, newheadroom);
  791. /* Set the tail pointer and length */
  792. skb_put(n, skb->len);
  793. head_copy_len = oldheadroom;
  794. head_copy_off = 0;
  795. if (newheadroom <= head_copy_len)
  796. head_copy_len = newheadroom;
  797. else
  798. head_copy_off = newheadroom - head_copy_len;
  799. /* Copy the linear header and data. */
  800. if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
  801. skb->len + head_copy_len))
  802. BUG();
  803. copy_skb_header(n, skb);
  804. off = newheadroom - oldheadroom;
  805. if (n->ip_summed == CHECKSUM_PARTIAL)
  806. n->csum_start += off;
  807. #ifdef NET_SKBUFF_DATA_USES_OFFSET
  808. n->transport_header += off;
  809. n->network_header += off;
  810. if (skb_mac_header_was_set(skb))
  811. n->mac_header += off;
  812. #endif
  813. return n;
  814. }
  815. EXPORT_SYMBOL(skb_copy_expand);
  816. /**
  817. * skb_pad - zero pad the tail of an skb
  818. * @skb: buffer to pad
  819. * @pad: space to pad
  820. *
  821. * Ensure that a buffer is followed by a padding area that is zero
  822. * filled. Used by network drivers which may DMA or transfer data
  823. * beyond the buffer end onto the wire.
  824. *
  825. * May return error in out of memory cases. The skb is freed on error.
  826. */
  827. int skb_pad(struct sk_buff *skb, int pad)
  828. {
  829. int err;
  830. int ntail;
  831. /* If the skbuff is non linear tailroom is always zero.. */
  832. if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
  833. memset(skb->data+skb->len, 0, pad);
  834. return 0;
  835. }
  836. ntail = skb->data_len + pad - (skb->end - skb->tail);
  837. if (likely(skb_cloned(skb) || ntail > 0)) {
  838. err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
  839. if (unlikely(err))
  840. goto free_skb;
  841. }
  842. /* FIXME: The use of this function with non-linear skb's really needs
  843. * to be audited.
  844. */
  845. err = skb_linearize(skb);
  846. if (unlikely(err))
  847. goto free_skb;
  848. memset(skb->data + skb->len, 0, pad);
  849. return 0;
  850. free_skb:
  851. kfree_skb(skb);
  852. return err;
  853. }
  854. EXPORT_SYMBOL(skb_pad);
  855. /**
  856. * skb_put - add data to a buffer
  857. * @skb: buffer to use
  858. * @len: amount of data to add
  859. *
  860. * This function extends the used data area of the buffer. If this would
  861. * exceed the total buffer size the kernel will panic. A pointer to the
  862. * first byte of the extra data is returned.
  863. */
  864. unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
  865. {
  866. unsigned char *tmp = skb_tail_pointer(skb);
  867. SKB_LINEAR_ASSERT(skb);
  868. skb->tail += len;
  869. skb->len += len;
  870. if (unlikely(skb->tail > skb->end))
  871. skb_over_panic(skb, len, __builtin_return_address(0));
  872. return tmp;
  873. }
  874. EXPORT_SYMBOL(skb_put);
  875. /**
  876. * skb_push - add data to the start of a buffer
  877. * @skb: buffer to use
  878. * @len: amount of data to add
  879. *
  880. * This function extends the used data area of the buffer at the buffer
  881. * start. If this would exceed the total buffer headroom the kernel will
  882. * panic. A pointer to the first byte of the extra data is returned.
  883. */
  884. unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
  885. {
  886. skb->data -= len;
  887. skb->len += len;
  888. if (unlikely(skb->data<skb->head))
  889. skb_under_panic(skb, len, __builtin_return_address(0));
  890. return skb->data;
  891. }
  892. EXPORT_SYMBOL(skb_push);
  893. /**
  894. * skb_pull - remove data from the start of a buffer
  895. * @skb: buffer to use
  896. * @len: amount of data to remove
  897. *
  898. * This function removes data from the start of a buffer, returning
  899. * the memory to the headroom. A pointer to the next data in the buffer
  900. * is returned. Once the data has been pulled future pushes will overwrite
  901. * the old data.
  902. */
  903. unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
  904. {
  905. return skb_pull_inline(skb, len);
  906. }
  907. EXPORT_SYMBOL(skb_pull);
  908. /**
  909. * skb_trim - remove end from a buffer
  910. * @skb: buffer to alter
  911. * @len: new length
  912. *
  913. * Cut the length of a buffer down by removing data from the tail. If
  914. * the buffer is already under the length specified it is not modified.
  915. * The skb must be linear.
  916. */
  917. void skb_trim(struct sk_buff *skb, unsigned int len)
  918. {
  919. if (skb->len > len)
  920. __skb_trim(skb, len);
  921. }
  922. EXPORT_SYMBOL(skb_trim);
  923. /* Trims skb to length len. It can change skb pointers.
  924. */
  925. int ___pskb_trim(struct sk_buff *skb, unsigned int len)
  926. {
  927. struct sk_buff **fragp;
  928. struct sk_buff *frag;
  929. int offset = skb_headlen(skb);
  930. int nfrags = skb_shinfo(skb)->nr_frags;
  931. int i;
  932. int err;
  933. if (skb_cloned(skb) &&
  934. unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
  935. return err;
  936. i = 0;
  937. if (offset >= len)
  938. goto drop_pages;
  939. for (; i < nfrags; i++) {
  940. int end = offset + skb_shinfo(skb)->frags[i].size;
  941. if (end < len) {
  942. offset = end;
  943. continue;
  944. }
  945. skb_shinfo(skb)->frags[i++].size = len - offset;
  946. drop_pages:
  947. skb_shinfo(skb)->nr_frags = i;
  948. for (; i < nfrags; i++)
  949. put_page(skb_shinfo(skb)->frags[i].page);
  950. if (skb_has_frag_list(skb))
  951. skb_drop_fraglist(skb);
  952. goto done;
  953. }
  954. for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
  955. fragp = &frag->next) {
  956. int end = offset + frag->len;
  957. if (skb_shared(frag)) {
  958. struct sk_buff *nfrag;
  959. nfrag = skb_clone(frag, GFP_ATOMIC);
  960. if (unlikely(!nfrag))
  961. return -ENOMEM;
  962. nfrag->next = frag->next;
  963. kfree_skb(frag);
  964. frag = nfrag;
  965. *fragp = frag;
  966. }
  967. if (end < len) {
  968. offset = end;
  969. continue;
  970. }
  971. if (end > len &&
  972. unlikely((err = pskb_trim(frag, len - offset))))
  973. return err;
  974. if (frag->next)
  975. skb_drop_list(&frag->next);
  976. break;
  977. }
  978. done:
  979. if (len > skb_headlen(skb)) {
  980. skb->data_len -= skb->len - len;
  981. skb->len = len;
  982. } else {
  983. skb->len = len;
  984. skb->data_len = 0;
  985. skb_set_tail_pointer(skb, len);
  986. }
  987. return 0;
  988. }
  989. EXPORT_SYMBOL(___pskb_trim);
  990. /**
  991. * __pskb_pull_tail - advance tail of skb header
  992. * @skb: buffer to reallocate
  993. * @delta: number of bytes to advance tail
  994. *
  995. * The function makes a sense only on a fragmented &sk_buff,
  996. * it expands header moving its tail forward and copying necessary
  997. * data from fragmented part.
  998. *
  999. * &sk_buff MUST have reference count of 1.
  1000. *
  1001. * Returns %NULL (and &sk_buff does not change) if pull failed
  1002. * or value of new tail of skb in the case of success.
  1003. *
  1004. * All the pointers pointing into skb header may change and must be
  1005. * reloaded after call to this function.
  1006. */
  1007. /* Moves tail of skb head forward, copying data from fragmented part,
  1008. * when it is necessary.
  1009. * 1. It may fail due to malloc failure.
  1010. * 2. It may change skb pointers.
  1011. *
  1012. * It is pretty complicated. Luckily, it is called only in exceptional cases.
  1013. */
  1014. unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
  1015. {
  1016. /* If skb has not enough free space at tail, get new one
  1017. * plus 128 bytes for future expansions. If we have enough
  1018. * room at tail, reallocate without expansion only if skb is cloned.
  1019. */
  1020. int i, k, eat = (skb->tail + delta) - skb->end;
  1021. if (eat > 0 || skb_cloned(skb)) {
  1022. if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
  1023. GFP_ATOMIC))
  1024. return NULL;
  1025. }
  1026. if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
  1027. BUG();
  1028. /* Optimization: no fragments, no reasons to preestimate
  1029. * size of pulled pages. Superb.
  1030. */
  1031. if (!skb_has_frag_list(skb))
  1032. goto pull_pages;
  1033. /* Estimate size of pulled pages. */
  1034. eat = delta;
  1035. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1036. if (skb_shinfo(skb)->frags[i].size >= eat)
  1037. goto pull_pages;
  1038. eat -= skb_shinfo(skb)->frags[i].size;
  1039. }
  1040. /* If we need update frag list, we are in troubles.
  1041. * Certainly, it possible to add an offset to skb data,
  1042. * but taking into account that pulling is expected to
  1043. * be very rare operation, it is worth to fight against
  1044. * further bloating skb head and crucify ourselves here instead.
  1045. * Pure masohism, indeed. 8)8)
  1046. */
  1047. if (eat) {
  1048. struct sk_buff *list = skb_shinfo(skb)->frag_list;
  1049. struct sk_buff *clone = NULL;
  1050. struct sk_buff *insp = NULL;
  1051. do {
  1052. BUG_ON(!list);
  1053. if (list->len <= eat) {
  1054. /* Eaten as whole. */
  1055. eat -= list->len;
  1056. list = list->next;
  1057. insp = list;
  1058. } else {
  1059. /* Eaten partially. */
  1060. if (skb_shared(list)) {
  1061. /* Sucks! We need to fork list. :-( */
  1062. clone = skb_clone(list, GFP_ATOMIC);
  1063. if (!clone)
  1064. return NULL;
  1065. insp = list->next;
  1066. list = clone;
  1067. } else {
  1068. /* This may be pulled without
  1069. * problems. */
  1070. insp = list;
  1071. }
  1072. if (!pskb_pull(list, eat)) {
  1073. kfree_skb(clone);
  1074. return NULL;
  1075. }
  1076. break;
  1077. }
  1078. } while (eat);
  1079. /* Free pulled out fragments. */
  1080. while ((list = skb_shinfo(skb)->frag_list) != insp) {
  1081. skb_shinfo(skb)->frag_list = list->next;
  1082. kfree_skb(list);
  1083. }
  1084. /* And insert new clone at head. */
  1085. if (clone) {
  1086. clone->next = list;
  1087. skb_shinfo(skb)->frag_list = clone;
  1088. }
  1089. }
  1090. /* Success! Now we may commit changes to skb data. */
  1091. pull_pages:
  1092. eat = delta;
  1093. k = 0;
  1094. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1095. if (skb_shinfo(skb)->frags[i].size <= eat) {
  1096. put_page(skb_shinfo(skb)->frags[i].page);
  1097. eat -= skb_shinfo(skb)->frags[i].size;
  1098. } else {
  1099. skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
  1100. if (eat) {
  1101. skb_shinfo(skb)->frags[k].page_offset += eat;
  1102. skb_shinfo(skb)->frags[k].size -= eat;
  1103. eat = 0;
  1104. }
  1105. k++;
  1106. }
  1107. }
  1108. skb_shinfo(skb)->nr_frags = k;
  1109. skb->tail += delta;
  1110. skb->data_len -= delta;
  1111. return skb_tail_pointer(skb);
  1112. }
  1113. EXPORT_SYMBOL(__pskb_pull_tail);
  1114. /* Copy some data bits from skb to kernel buffer. */
  1115. int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
  1116. {
  1117. int start = skb_headlen(skb);
  1118. struct sk_buff *frag_iter;
  1119. int i, copy;
  1120. if (offset > (int)skb->len - len)
  1121. goto fault;
  1122. /* Copy header. */
  1123. if ((copy = start - offset) > 0) {
  1124. if (copy > len)
  1125. copy = len;
  1126. skb_copy_from_linear_data_offset(skb, offset, to, copy);
  1127. if ((len -= copy) == 0)
  1128. return 0;
  1129. offset += copy;
  1130. to += copy;
  1131. }
  1132. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1133. int end;
  1134. WARN_ON(start > offset + len);
  1135. end = start + skb_shinfo(skb)->frags[i].size;
  1136. if ((copy = end - offset) > 0) {
  1137. u8 *vaddr;
  1138. if (copy > len)
  1139. copy = len;
  1140. vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
  1141. memcpy(to,
  1142. vaddr + skb_shinfo(skb)->frags[i].page_offset+
  1143. offset - start, copy);
  1144. kunmap_skb_frag(vaddr);
  1145. if ((len -= copy) == 0)
  1146. return 0;
  1147. offset += copy;
  1148. to += copy;
  1149. }
  1150. start = end;
  1151. }
  1152. skb_walk_frags(skb, frag_iter) {
  1153. int end;
  1154. WARN_ON(start > offset + len);
  1155. end = start + frag_iter->len;
  1156. if ((copy = end - offset) > 0) {
  1157. if (copy > len)
  1158. copy = len;
  1159. if (skb_copy_bits(frag_iter, offset - start, to, copy))
  1160. goto fault;
  1161. if ((len -= copy) == 0)
  1162. return 0;
  1163. offset += copy;
  1164. to += copy;
  1165. }
  1166. start = end;
  1167. }
  1168. if (!len)
  1169. return 0;
  1170. fault:
  1171. return -EFAULT;
  1172. }
  1173. EXPORT_SYMBOL(skb_copy_bits);
  1174. /*
  1175. * Callback from splice_to_pipe(), if we need to release some pages
  1176. * at the end of the spd in case we error'ed out in filling the pipe.
  1177. */
  1178. static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
  1179. {
  1180. put_page(spd->pages[i]);
  1181. }
  1182. static inline struct page *linear_to_page(struct page *page, unsigned int *len,
  1183. unsigned int *offset,
  1184. struct sk_buff *skb, struct sock *sk)
  1185. {
  1186. struct page *p = sk->sk_sndmsg_page;
  1187. unsigned int off;
  1188. if (!p) {
  1189. new_page:
  1190. p = sk->sk_sndmsg_page = alloc_pages(sk->sk_allocation, 0);
  1191. if (!p)
  1192. return NULL;
  1193. off = sk->sk_sndmsg_off = 0;
  1194. /* hold one ref to this page until it's full */
  1195. } else {
  1196. unsigned int mlen;
  1197. off = sk->sk_sndmsg_off;
  1198. mlen = PAGE_SIZE - off;
  1199. if (mlen < 64 && mlen < *len) {
  1200. put_page(p);
  1201. goto new_page;
  1202. }
  1203. *len = min_t(unsigned int, *len, mlen);
  1204. }
  1205. memcpy(page_address(p) + off, page_address(page) + *offset, *len);
  1206. sk->sk_sndmsg_off += *len;
  1207. *offset = off;
  1208. get_page(p);
  1209. return p;
  1210. }
  1211. /*
  1212. * Fill page/offset/length into spd, if it can hold more pages.
  1213. */
  1214. static inline int spd_fill_page(struct splice_pipe_desc *spd,
  1215. struct pipe_inode_info *pipe, struct page *page,
  1216. unsigned int *len, unsigned int offset,
  1217. struct sk_buff *skb, int linear,
  1218. struct sock *sk)
  1219. {
  1220. if (unlikely(spd->nr_pages == pipe->buffers))
  1221. return 1;
  1222. if (linear) {
  1223. page = linear_to_page(page, len, &offset, skb, sk);
  1224. if (!page)
  1225. return 1;
  1226. } else
  1227. get_page(page);
  1228. spd->pages[spd->nr_pages] = page;
  1229. spd->partial[spd->nr_pages].len = *len;
  1230. spd->partial[spd->nr_pages].offset = offset;
  1231. spd->nr_pages++;
  1232. return 0;
  1233. }
  1234. static inline void __segment_seek(struct page **page, unsigned int *poff,
  1235. unsigned int *plen, unsigned int off)
  1236. {
  1237. unsigned long n;
  1238. *poff += off;
  1239. n = *poff / PAGE_SIZE;
  1240. if (n)
  1241. *page = nth_page(*page, n);
  1242. *poff = *poff % PAGE_SIZE;
  1243. *plen -= off;
  1244. }
  1245. static inline int __splice_segment(struct page *page, unsigned int poff,
  1246. unsigned int plen, unsigned int *off,
  1247. unsigned int *len, struct sk_buff *skb,
  1248. struct splice_pipe_desc *spd, int linear,
  1249. struct sock *sk,
  1250. struct pipe_inode_info *pipe)
  1251. {
  1252. if (!*len)
  1253. return 1;
  1254. /* skip this segment if already processed */
  1255. if (*off >= plen) {
  1256. *off -= plen;
  1257. return 0;
  1258. }
  1259. /* ignore any bits we already processed */
  1260. if (*off) {
  1261. __segment_seek(&page, &poff, &plen, *off);
  1262. *off = 0;
  1263. }
  1264. do {
  1265. unsigned int flen = min(*len, plen);
  1266. /* the linear region may spread across several pages */
  1267. flen = min_t(unsigned int, flen, PAGE_SIZE - poff);
  1268. if (spd_fill_page(spd, pipe, page, &flen, poff, skb, linear, sk))
  1269. return 1;
  1270. __segment_seek(&page, &poff, &plen, flen);
  1271. *len -= flen;
  1272. } while (*len && plen);
  1273. return 0;
  1274. }
  1275. /*
  1276. * Map linear and fragment data from the skb to spd. It reports failure if the
  1277. * pipe is full or if we already spliced the requested length.
  1278. */
  1279. static int __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
  1280. unsigned int *offset, unsigned int *len,
  1281. struct splice_pipe_desc *spd, struct sock *sk)
  1282. {
  1283. int seg;
  1284. /*
  1285. * map the linear part
  1286. */
  1287. if (__splice_segment(virt_to_page(skb->data),
  1288. (unsigned long) skb->data & (PAGE_SIZE - 1),
  1289. skb_headlen(skb),
  1290. offset, len, skb, spd, 1, sk, pipe))
  1291. return 1;
  1292. /*
  1293. * then map the fragments
  1294. */
  1295. for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
  1296. const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
  1297. if (__splice_segment(f->page, f->page_offset, f->size,
  1298. offset, len, skb, spd, 0, sk, pipe))
  1299. return 1;
  1300. }
  1301. return 0;
  1302. }
  1303. /*
  1304. * Map data from the skb to a pipe. Should handle both the linear part,
  1305. * the fragments, and the frag list. It does NOT handle frag lists within
  1306. * the frag list, if such a thing exists. We'd probably need to recurse to
  1307. * handle that cleanly.
  1308. */
  1309. int skb_splice_bits(struct sk_buff *skb, unsigned int offset,
  1310. struct pipe_inode_info *pipe, unsigned int tlen,
  1311. unsigned int flags)
  1312. {
  1313. struct partial_page partial[PIPE_DEF_BUFFERS];
  1314. struct page *pages[PIPE_DEF_BUFFERS];
  1315. struct splice_pipe_desc spd = {
  1316. .pages = pages,
  1317. .partial = partial,
  1318. .flags = flags,
  1319. .ops = &sock_pipe_buf_ops,
  1320. .spd_release = sock_spd_release,
  1321. };
  1322. struct sk_buff *frag_iter;
  1323. struct sock *sk = skb->sk;
  1324. int ret = 0;
  1325. if (splice_grow_spd(pipe, &spd))
  1326. return -ENOMEM;
  1327. /*
  1328. * __skb_splice_bits() only fails if the output has no room left,
  1329. * so no point in going over the frag_list for the error case.
  1330. */
  1331. if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk))
  1332. goto done;
  1333. else if (!tlen)
  1334. goto done;
  1335. /*
  1336. * now see if we have a frag_list to map
  1337. */
  1338. skb_walk_frags(skb, frag_iter) {
  1339. if (!tlen)
  1340. break;
  1341. if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk))
  1342. break;
  1343. }
  1344. done:
  1345. if (spd.nr_pages) {
  1346. /*
  1347. * Drop the socket lock, otherwise we have reverse
  1348. * locking dependencies between sk_lock and i_mutex
  1349. * here as compared to sendfile(). We enter here
  1350. * with the socket lock held, and splice_to_pipe() will
  1351. * grab the pipe inode lock. For sendfile() emulation,
  1352. * we call into ->sendpage() with the i_mutex lock held
  1353. * and networking will grab the socket lock.
  1354. */
  1355. release_sock(sk);
  1356. ret = splice_to_pipe(pipe, &spd);
  1357. lock_sock(sk);
  1358. }
  1359. splice_shrink_spd(pipe, &spd);
  1360. return ret;
  1361. }
  1362. /**
  1363. * skb_store_bits - store bits from kernel buffer to skb
  1364. * @skb: destination buffer
  1365. * @offset: offset in destination
  1366. * @from: source buffer
  1367. * @len: number of bytes to copy
  1368. *
  1369. * Copy the specified number of bytes from the source buffer to the
  1370. * destination skb. This function handles all the messy bits of
  1371. * traversing fragment lists and such.
  1372. */
  1373. int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
  1374. {
  1375. int start = skb_headlen(skb);
  1376. struct sk_buff *frag_iter;
  1377. int i, copy;
  1378. if (offset > (int)skb->len - len)
  1379. goto fault;
  1380. if ((copy = start - offset) > 0) {
  1381. if (copy > len)
  1382. copy = len;
  1383. skb_copy_to_linear_data_offset(skb, offset, from, copy);
  1384. if ((len -= copy) == 0)
  1385. return 0;
  1386. offset += copy;
  1387. from += copy;
  1388. }
  1389. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1390. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  1391. int end;
  1392. WARN_ON(start > offset + len);
  1393. end = start + frag->size;
  1394. if ((copy = end - offset) > 0) {
  1395. u8 *vaddr;
  1396. if (copy > len)
  1397. copy = len;
  1398. vaddr = kmap_skb_frag(frag);
  1399. memcpy(vaddr + frag->page_offset + offset - start,
  1400. from, copy);
  1401. kunmap_skb_frag(vaddr);
  1402. if ((len -= copy) == 0)
  1403. return 0;
  1404. offset += copy;
  1405. from += copy;
  1406. }
  1407. start = end;
  1408. }
  1409. skb_walk_frags(skb, frag_iter) {
  1410. int end;
  1411. WARN_ON(start > offset + len);
  1412. end = start + frag_iter->len;
  1413. if ((copy = end - offset) > 0) {
  1414. if (copy > len)
  1415. copy = len;
  1416. if (skb_store_bits(frag_iter, offset - start,
  1417. from, copy))
  1418. goto fault;
  1419. if ((len -= copy) == 0)
  1420. return 0;
  1421. offset += copy;
  1422. from += copy;
  1423. }
  1424. start = end;
  1425. }
  1426. if (!len)
  1427. return 0;
  1428. fault:
  1429. return -EFAULT;
  1430. }
  1431. EXPORT_SYMBOL(skb_store_bits);
  1432. /* Checksum skb data. */
  1433. __wsum skb_checksum(const struct sk_buff *skb, int offset,
  1434. int len, __wsum csum)
  1435. {
  1436. int start = skb_headlen(skb);
  1437. int i, copy = start - offset;
  1438. struct sk_buff *frag_iter;
  1439. int pos = 0;
  1440. /* Checksum header. */
  1441. if (copy > 0) {
  1442. if (copy > len)
  1443. copy = len;
  1444. csum = csum_partial(skb->data + offset, copy, csum);
  1445. if ((len -= copy) == 0)
  1446. return csum;
  1447. offset += copy;
  1448. pos = copy;
  1449. }
  1450. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1451. int end;
  1452. WARN_ON(start > offset + len);
  1453. end = start + skb_shinfo(skb)->frags[i].size;
  1454. if ((copy = end - offset) > 0) {
  1455. __wsum csum2;
  1456. u8 *vaddr;
  1457. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  1458. if (copy > len)
  1459. copy = len;
  1460. vaddr = kmap_skb_frag(frag);
  1461. csum2 = csum_partial(vaddr + frag->page_offset +
  1462. offset - start, copy, 0);
  1463. kunmap_skb_frag(vaddr);
  1464. csum = csum_block_add(csum, csum2, pos);
  1465. if (!(len -= copy))
  1466. return csum;
  1467. offset += copy;
  1468. pos += copy;
  1469. }
  1470. start = end;
  1471. }
  1472. skb_walk_frags(skb, frag_iter) {
  1473. int end;
  1474. WARN_ON(start > offset + len);
  1475. end = start + frag_iter->len;
  1476. if ((copy = end - offset) > 0) {
  1477. __wsum csum2;
  1478. if (copy > len)
  1479. copy = len;
  1480. csum2 = skb_checksum(frag_iter, offset - start,
  1481. copy, 0);
  1482. csum = csum_block_add(csum, csum2, pos);
  1483. if ((len -= copy) == 0)
  1484. return csum;
  1485. offset += copy;
  1486. pos += copy;
  1487. }
  1488. start = end;
  1489. }
  1490. BUG_ON(len);
  1491. return csum;
  1492. }
  1493. EXPORT_SYMBOL(skb_checksum);
  1494. /* Both of above in one bottle. */
  1495. __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
  1496. u8 *to, int len, __wsum csum)
  1497. {
  1498. int start = skb_headlen(skb);
  1499. int i, copy = start - offset;
  1500. struct sk_buff *frag_iter;
  1501. int pos = 0;
  1502. /* Copy header. */
  1503. if (copy > 0) {
  1504. if (copy > len)
  1505. copy = len;
  1506. csum = csum_partial_copy_nocheck(skb->data + offset, to,
  1507. copy, csum);
  1508. if ((len -= copy) == 0)
  1509. return csum;
  1510. offset += copy;
  1511. to += copy;
  1512. pos = copy;
  1513. }
  1514. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1515. int end;
  1516. WARN_ON(start > offset + len);
  1517. end = start + skb_shinfo(skb)->frags[i].size;
  1518. if ((copy = end - offset) > 0) {
  1519. __wsum csum2;
  1520. u8 *vaddr;
  1521. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  1522. if (copy > len)
  1523. copy = len;
  1524. vaddr = kmap_skb_frag(frag);
  1525. csum2 = csum_partial_copy_nocheck(vaddr +
  1526. frag->page_offset +
  1527. offset - start, to,
  1528. copy, 0);
  1529. kunmap_skb_frag(vaddr);
  1530. csum = csum_block_add(csum, csum2, pos);
  1531. if (!(len -= copy))
  1532. return csum;
  1533. offset += copy;
  1534. to += copy;
  1535. pos += copy;
  1536. }
  1537. start = end;
  1538. }
  1539. skb_walk_frags(skb, frag_iter) {
  1540. __wsum csum2;
  1541. int end;
  1542. WARN_ON(start > offset + len);
  1543. end = start + frag_iter->len;
  1544. if ((copy = end - offset) > 0) {
  1545. if (copy > len)
  1546. copy = len;
  1547. csum2 = skb_copy_and_csum_bits(frag_iter,
  1548. offset - start,
  1549. to, copy, 0);
  1550. csum = csum_block_add(csum, csum2, pos);
  1551. if ((len -= copy) == 0)
  1552. return csum;
  1553. offset += copy;
  1554. to += copy;
  1555. pos += copy;
  1556. }
  1557. start = end;
  1558. }
  1559. BUG_ON(len);
  1560. return csum;
  1561. }
  1562. EXPORT_SYMBOL(skb_copy_and_csum_bits);
  1563. void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
  1564. {
  1565. __wsum csum;
  1566. long csstart;
  1567. if (skb->ip_summed == CHECKSUM_PARTIAL)
  1568. csstart = skb->csum_start - skb_headroom(skb);
  1569. else
  1570. csstart = skb_headlen(skb);
  1571. BUG_ON(csstart > skb_headlen(skb));
  1572. skb_copy_from_linear_data(skb, to, csstart);
  1573. csum = 0;
  1574. if (csstart != skb->len)
  1575. csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
  1576. skb->len - csstart, 0);
  1577. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  1578. long csstuff = csstart + skb->csum_offset;
  1579. *((__sum16 *)(to + csstuff)) = csum_fold(csum);
  1580. }
  1581. }
  1582. EXPORT_SYMBOL(skb_copy_and_csum_dev);
  1583. /**
  1584. * skb_dequeue - remove from the head of the queue
  1585. * @list: list to dequeue from
  1586. *
  1587. * Remove the head of the list. The list lock is taken so the function
  1588. * may be used safely with other locking list functions. The head item is
  1589. * returned or %NULL if the list is empty.
  1590. */
  1591. struct sk_buff *skb_dequeue(struct sk_buff_head *list)
  1592. {
  1593. unsigned long flags;
  1594. struct sk_buff *result;
  1595. spin_lock_irqsave(&list->lock, flags);
  1596. result = __skb_dequeue(list);
  1597. spin_unlock_irqrestore(&list->lock, flags);
  1598. return result;
  1599. }
  1600. EXPORT_SYMBOL(skb_dequeue);
  1601. /**
  1602. * skb_dequeue_tail - remove from the tail of the queue
  1603. * @list: list to dequeue from
  1604. *
  1605. * Remove the tail of the list. The list lock is taken so the function
  1606. * may be used safely with other locking list functions. The tail item is
  1607. * returned or %NULL if the list is empty.
  1608. */
  1609. struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
  1610. {
  1611. unsigned long flags;
  1612. struct sk_buff *result;
  1613. spin_lock_irqsave(&list->lock, flags);
  1614. result = __skb_dequeue_tail(list);
  1615. spin_unlock_irqrestore(&list->lock, flags);
  1616. return result;
  1617. }
  1618. EXPORT_SYMBOL(skb_dequeue_tail);
  1619. /**
  1620. * skb_queue_purge - empty a list
  1621. * @list: list to empty
  1622. *
  1623. * Delete all buffers on an &sk_buff list. Each buffer is removed from
  1624. * the list and one reference dropped. This function takes the list
  1625. * lock and is atomic with respect to other list locking functions.
  1626. */
  1627. void skb_queue_purge(struct sk_buff_head *list)
  1628. {
  1629. struct sk_buff *skb;
  1630. while ((skb = skb_dequeue(list)) != NULL)
  1631. kfree_skb(skb);
  1632. }
  1633. EXPORT_SYMBOL(skb_queue_purge);
  1634. /**
  1635. * skb_queue_head - queue a buffer at the list head
  1636. * @list: list to use
  1637. * @newsk: buffer to queue
  1638. *
  1639. * Queue a buffer at the start of the list. This function takes the
  1640. * list lock and can be used safely with other locking &sk_buff functions
  1641. * safely.
  1642. *
  1643. * A buffer cannot be placed on two lists at the same time.
  1644. */
  1645. void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
  1646. {
  1647. unsigned long flags;
  1648. spin_lock_irqsave(&list->lock, flags);
  1649. __skb_queue_head(list, newsk);
  1650. spin_unlock_irqrestore(&list->lock, flags);
  1651. }
  1652. EXPORT_SYMBOL(skb_queue_head);
  1653. /**
  1654. * skb_queue_tail - queue a buffer at the list tail
  1655. * @list: list to use
  1656. * @newsk: buffer to queue
  1657. *
  1658. * Queue a buffer at the tail of the list. This function takes the
  1659. * list lock and can be used safely with other locking &sk_buff functions
  1660. * safely.
  1661. *
  1662. * A buffer cannot be placed on two lists at the same time.
  1663. */
  1664. void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
  1665. {
  1666. unsigned long flags;
  1667. spin_lock_irqsave(&list->lock, flags);
  1668. __skb_queue_tail(list, newsk);
  1669. spin_unlock_irqrestore(&list->lock, flags);
  1670. }
  1671. EXPORT_SYMBOL(skb_queue_tail);
  1672. /**
  1673. * skb_unlink - remove a buffer from a list
  1674. * @skb: buffer to remove
  1675. * @list: list to use
  1676. *
  1677. * Remove a packet from a list. The list locks are taken and this
  1678. * function is atomic with respect to other list locked calls
  1679. *
  1680. * You must know what list the SKB is on.
  1681. */
  1682. void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
  1683. {
  1684. unsigned long flags;
  1685. spin_lock_irqsave(&list->lock, flags);
  1686. __skb_unlink(skb, list);
  1687. spin_unlock_irqrestore(&list->lock, flags);
  1688. }
  1689. EXPORT_SYMBOL(skb_unlink);
  1690. /**
  1691. * skb_append - append a buffer
  1692. * @old: buffer to insert after
  1693. * @newsk: buffer to insert
  1694. * @list: list to use
  1695. *
  1696. * Place a packet after a given packet in a list. The list locks are taken
  1697. * and this function is atomic with respect to other list locked calls.
  1698. * A buffer cannot be placed on two lists at the same time.
  1699. */
  1700. void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
  1701. {
  1702. unsigned long flags;
  1703. spin_lock_irqsave(&list->lock, flags);
  1704. __skb_queue_after(list, old, newsk);
  1705. spin_unlock_irqrestore(&list->lock, flags);
  1706. }
  1707. EXPORT_SYMBOL(skb_append);
  1708. /**
  1709. * skb_insert - insert a buffer
  1710. * @old: buffer to insert before
  1711. * @newsk: buffer to insert
  1712. * @list: list to use
  1713. *
  1714. * Place a packet before a given packet in a list. The list locks are
  1715. * taken and this function is atomic with respect to other list locked
  1716. * calls.
  1717. *
  1718. * A buffer cannot be placed on two lists at the same time.
  1719. */
  1720. void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
  1721. {
  1722. unsigned long flags;
  1723. spin_lock_irqsave(&list->lock, flags);
  1724. __skb_insert(newsk, old->prev, old, list);
  1725. spin_unlock_irqrestore(&list->lock, flags);
  1726. }
  1727. EXPORT_SYMBOL(skb_insert);
  1728. static inline void skb_split_inside_header(struct sk_buff *skb,
  1729. struct sk_buff* skb1,
  1730. const u32 len, const int pos)
  1731. {
  1732. int i;
  1733. skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
  1734. pos - len);
  1735. /* And move data appendix as is. */
  1736. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
  1737. skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
  1738. skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
  1739. skb_shinfo(skb)->nr_frags = 0;
  1740. skb1->data_len = skb->data_len;
  1741. skb1->len += skb1->data_len;
  1742. skb->data_len = 0;
  1743. skb->len = len;
  1744. skb_set_tail_pointer(skb, len);
  1745. }
  1746. static inline void skb_split_no_header(struct sk_buff *skb,
  1747. struct sk_buff* skb1,
  1748. const u32 len, int pos)
  1749. {
  1750. int i, k = 0;
  1751. const int nfrags = skb_shinfo(skb)->nr_frags;
  1752. skb_shinfo(skb)->nr_frags = 0;
  1753. skb1->len = skb1->data_len = skb->len - len;
  1754. skb->len = len;
  1755. skb->data_len = len - pos;
  1756. for (i = 0; i < nfrags; i++) {
  1757. int size = skb_shinfo(skb)->frags[i].size;
  1758. if (pos + size > len) {
  1759. skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
  1760. if (pos < len) {
  1761. /* Split frag.
  1762. * We have two variants in this case:
  1763. * 1. Move all the frag to the second
  1764. * part, if it is possible. F.e.
  1765. * this approach is mandatory for TUX,
  1766. * where splitting is expensive.
  1767. * 2. Split is accurately. We make this.
  1768. */
  1769. get_page(skb_shinfo(skb)->frags[i].page);
  1770. skb_shinfo(skb1)->frags[0].page_offset += len - pos;
  1771. skb_shinfo(skb1)->frags[0].size -= len - pos;
  1772. skb_shinfo(skb)->frags[i].size = len - pos;
  1773. skb_shinfo(skb)->nr_frags++;
  1774. }
  1775. k++;
  1776. } else
  1777. skb_shinfo(skb)->nr_frags++;
  1778. pos += size;
  1779. }
  1780. skb_shinfo(skb1)->nr_frags = k;
  1781. }
  1782. /**
  1783. * skb_split - Split fragmented skb to two parts at length len.
  1784. * @skb: the buffer to split
  1785. * @skb1: the buffer to receive the second part
  1786. * @len: new length for skb
  1787. */
  1788. void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
  1789. {
  1790. int pos = skb_headlen(skb);
  1791. if (len < pos) /* Split line is inside header. */
  1792. skb_split_inside_header(skb, skb1, len, pos);
  1793. else /* Second chunk has no header, nothing to copy. */
  1794. skb_split_no_header(skb, skb1, len, pos);
  1795. }
  1796. EXPORT_SYMBOL(skb_split);
  1797. /* Shifting from/to a cloned skb is a no-go.
  1798. *
  1799. * Caller cannot keep skb_shinfo related pointers past calling here!
  1800. */
  1801. static int skb_prepare_for_shift(struct sk_buff *skb)
  1802. {
  1803. return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
  1804. }
  1805. /**
  1806. * skb_shift - Shifts paged data partially from skb to another
  1807. * @tgt: buffer into which tail data gets added
  1808. * @skb: buffer from which the paged data comes from
  1809. * @shiftlen: shift up to this many bytes
  1810. *
  1811. * Attempts to shift up to shiftlen worth of bytes, which may be less than
  1812. * the length of the skb, from tgt to skb. Returns number bytes shifted.
  1813. * It's up to caller to free skb if everything was shifted.
  1814. *
  1815. * If @tgt runs out of frags, the whole operation is aborted.
  1816. *
  1817. * Skb cannot include anything else but paged data while tgt is allowed
  1818. * to have non-paged data as well.
  1819. *
  1820. * TODO: full sized shift could be optimized but that would need
  1821. * specialized skb free'er to handle frags without up-to-date nr_frags.
  1822. */
  1823. int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
  1824. {
  1825. int from, to, merge, todo;
  1826. struct skb_frag_struct *fragfrom, *fragto;
  1827. BUG_ON(shiftlen > skb->len);
  1828. BUG_ON(skb_headlen(skb)); /* Would corrupt stream */
  1829. todo = shiftlen;
  1830. from = 0;
  1831. to = skb_shinfo(tgt)->nr_frags;
  1832. fragfrom = &skb_shinfo(skb)->frags[from];
  1833. /* Actual merge is delayed until the point when we know we can
  1834. * commit all, so that we don't have to undo partial changes
  1835. */
  1836. if (!to ||
  1837. !skb_can_coalesce(tgt, to, fragfrom->page, fragfrom->page_offset)) {
  1838. merge = -1;
  1839. } else {
  1840. merge = to - 1;
  1841. todo -= fragfrom->size;
  1842. if (todo < 0) {
  1843. if (skb_prepare_for_shift(skb) ||
  1844. skb_prepare_for_shift(tgt))
  1845. return 0;
  1846. /* All previous frag pointers might be stale! */
  1847. fragfrom = &skb_shinfo(skb)->frags[from];
  1848. fragto = &skb_shinfo(tgt)->frags[merge];
  1849. fragto->size += shiftlen;
  1850. fragfrom->size -= shiftlen;
  1851. fragfrom->page_offset += shiftlen;
  1852. goto onlymerged;
  1853. }
  1854. from++;
  1855. }
  1856. /* Skip full, not-fitting skb to avoid expensive operations */
  1857. if ((shiftlen == skb->len) &&
  1858. (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
  1859. return 0;
  1860. if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
  1861. return 0;
  1862. while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
  1863. if (to == MAX_SKB_FRAGS)
  1864. return 0;
  1865. fragfrom = &skb_shinfo(skb)->frags[from];
  1866. fragto = &skb_shinfo(tgt)->frags[to];
  1867. if (todo >= fragfrom->size) {
  1868. *fragto = *fragfrom;
  1869. todo -= fragfrom->size;
  1870. from++;
  1871. to++;
  1872. } else {
  1873. get_page(fragfrom->page);
  1874. fragto->page = fragfrom->page;
  1875. fragto->page_offset = fragfrom->page_offset;
  1876. fragto->size = todo;
  1877. fragfrom->page_offset += todo;
  1878. fragfrom->size -= todo;
  1879. todo = 0;
  1880. to++;
  1881. break;
  1882. }
  1883. }
  1884. /* Ready to "commit" this state change to tgt */
  1885. skb_shinfo(tgt)->nr_frags = to;
  1886. if (merge >= 0) {
  1887. fragfrom = &skb_shinfo(skb)->frags[0];
  1888. fragto = &skb_shinfo(tgt)->frags[merge];
  1889. fragto->size += fragfrom->size;
  1890. put_page(fragfrom->page);
  1891. }
  1892. /* Reposition in the original skb */
  1893. to = 0;
  1894. while (from < skb_shinfo(skb)->nr_frags)
  1895. skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
  1896. skb_shinfo(skb)->nr_frags = to;
  1897. BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
  1898. onlymerged:
  1899. /* Most likely the tgt won't ever need its checksum anymore, skb on
  1900. * the other hand might need it if it needs to be resent
  1901. */
  1902. tgt->ip_summed = CHECKSUM_PARTIAL;
  1903. skb->ip_summed = CHECKSUM_PARTIAL;
  1904. /* Yak, is it really working this way? Some helper please? */
  1905. skb->len -= shiftlen;
  1906. skb->data_len -= shiftlen;
  1907. skb->truesize -= shiftlen;
  1908. tgt->len += shiftlen;
  1909. tgt->data_len += shiftlen;
  1910. tgt->truesize += shiftlen;
  1911. return shiftlen;
  1912. }
  1913. /**
  1914. * skb_prepare_seq_read - Prepare a sequential read of skb data
  1915. * @skb: the buffer to read
  1916. * @from: lower offset of data to be read
  1917. * @to: upper offset of data to be read
  1918. * @st: state variable
  1919. *
  1920. * Initializes the specified state variable. Must be called before
  1921. * invoking skb_seq_read() for the first time.
  1922. */
  1923. void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
  1924. unsigned int to, struct skb_seq_state *st)
  1925. {
  1926. st->lower_offset = from;
  1927. st->upper_offset = to;
  1928. st->root_skb = st->cur_skb = skb;
  1929. st->frag_idx = st->stepped_offset = 0;
  1930. st->frag_data = NULL;
  1931. }
  1932. EXPORT_SYMBOL(skb_prepare_seq_read);
  1933. /**
  1934. * skb_seq_read - Sequentially read skb data
  1935. * @consumed: number of bytes consumed by the caller so far
  1936. * @data: destination pointer for data to be returned
  1937. * @st: state variable
  1938. *
  1939. * Reads a block of skb data at &consumed relative to the
  1940. * lower offset specified to skb_prepare_seq_read(). Assigns
  1941. * the head of the data block to &data and returns the length
  1942. * of the block or 0 if the end of the skb data or the upper
  1943. * offset has been reached.
  1944. *
  1945. * The caller is not required to consume all of the data
  1946. * returned, i.e. &consumed is typically set to the number
  1947. * of bytes already consumed and the next call to
  1948. * skb_seq_read() will return the remaining part of the block.
  1949. *
  1950. * Note 1: The size of each block of data returned can be arbitary,
  1951. * this limitation is the cost for zerocopy seqeuental
  1952. * reads of potentially non linear data.
  1953. *
  1954. * Note 2: Fragment lists within fragments are not implemented
  1955. * at the moment, state->root_skb could be replaced with
  1956. * a stack for this purpose.
  1957. */
  1958. unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
  1959. struct skb_seq_state *st)
  1960. {
  1961. unsigned int block_limit, abs_offset = consumed + st->lower_offset;
  1962. skb_frag_t *frag;
  1963. if (unlikely(abs_offset >= st->upper_offset))
  1964. return 0;
  1965. next_skb:
  1966. block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
  1967. if (abs_offset < block_limit && !st->frag_data) {
  1968. *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
  1969. return block_limit - abs_offset;
  1970. }
  1971. if (st->frag_idx == 0 && !st->frag_data)
  1972. st->stepped_offset += skb_headlen(st->cur_skb);
  1973. while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
  1974. frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
  1975. block_limit = frag->size + st->stepped_offset;
  1976. if (abs_offset < block_limit) {
  1977. if (!st->frag_data)
  1978. st->frag_data = kmap_skb_frag(frag);
  1979. *data = (u8 *) st->frag_data + frag->page_offset +
  1980. (abs_offset - st->stepped_offset);
  1981. return block_limit - abs_offset;
  1982. }
  1983. if (st->frag_data) {
  1984. kunmap_skb_frag(st->frag_data);
  1985. st->frag_data = NULL;
  1986. }
  1987. st->frag_idx++;
  1988. st->stepped_offset += frag->size;
  1989. }
  1990. if (st->frag_data) {
  1991. kunmap_skb_frag(st->frag_data);
  1992. st->frag_data = NULL;
  1993. }
  1994. if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
  1995. st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
  1996. st->frag_idx = 0;
  1997. goto next_skb;
  1998. } else if (st->cur_skb->next) {
  1999. st->cur_skb = st->cur_skb->next;
  2000. st->frag_idx = 0;
  2001. goto next_skb;
  2002. }
  2003. return 0;
  2004. }
  2005. EXPORT_SYMBOL(skb_seq_read);
  2006. /**
  2007. * skb_abort_seq_read - Abort a sequential read of skb data
  2008. * @st: state variable
  2009. *
  2010. * Must be called if skb_seq_read() was not called until it
  2011. * returned 0.
  2012. */
  2013. void skb_abort_seq_read(struct skb_seq_state *st)
  2014. {
  2015. if (st->frag_data)
  2016. kunmap_skb_frag(st->frag_data);
  2017. }
  2018. EXPORT_SYMBOL(skb_abort_seq_read);
  2019. #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
  2020. static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
  2021. struct ts_config *conf,
  2022. struct ts_state *state)
  2023. {
  2024. return skb_seq_read(offset, text, TS_SKB_CB(state));
  2025. }
  2026. static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
  2027. {
  2028. skb_abort_seq_read(TS_SKB_CB(state));
  2029. }
  2030. /**
  2031. * skb_find_text - Find a text pattern in skb data
  2032. * @skb: the buffer to look in
  2033. * @from: search offset
  2034. * @to: search limit
  2035. * @config: textsearch configuration
  2036. * @state: uninitialized textsearch state variable
  2037. *
  2038. * Finds a pattern in the skb data according to the specified
  2039. * textsearch configuration. Use textsearch_next() to retrieve
  2040. * subsequent occurrences of the pattern. Returns the offset
  2041. * to the first occurrence or UINT_MAX if no match was found.
  2042. */
  2043. unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
  2044. unsigned int to, struct ts_config *config,
  2045. struct ts_state *state)
  2046. {
  2047. unsigned int ret;
  2048. config->get_next_block = skb_ts_get_next_block;
  2049. config->finish = skb_ts_finish;
  2050. skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
  2051. ret = textsearch_find(config, state);
  2052. return (ret <= to - from ? ret : UINT_MAX);
  2053. }
  2054. EXPORT_SYMBOL(skb_find_text);
  2055. /**
  2056. * skb_append_datato_frags: - append the user data to a skb
  2057. * @sk: sock structure
  2058. * @skb: skb structure to be appened with user data.
  2059. * @getfrag: call back function to be used for getting the user data
  2060. * @from: pointer to user message iov
  2061. * @length: length of the iov message
  2062. *
  2063. * Description: This procedure append the user data in the fragment part
  2064. * of the skb if any page alloc fails user this procedure returns -ENOMEM
  2065. */
  2066. int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
  2067. int (*getfrag)(void *from, char *to, int offset,
  2068. int len, int odd, struct sk_buff *skb),
  2069. void *from, int length)
  2070. {
  2071. int frg_cnt = 0;
  2072. skb_frag_t *frag = NULL;
  2073. struct page *page = NULL;
  2074. int copy, left;
  2075. int offset = 0;
  2076. int ret;
  2077. do {
  2078. /* Return error if we don't have space for new frag */
  2079. frg_cnt = skb_shinfo(skb)->nr_frags;
  2080. if (frg_cnt >= MAX_SKB_FRAGS)
  2081. return -EFAULT;
  2082. /* allocate a new page for next frag */
  2083. page = alloc_pages(sk->sk_allocation, 0);
  2084. /* If alloc_page fails just return failure and caller will
  2085. * free previous allocated pages by doing kfree_skb()
  2086. */
  2087. if (page == NULL)
  2088. return -ENOMEM;
  2089. /* initialize the next frag */
  2090. sk->sk_sndmsg_page = page;
  2091. sk->sk_sndmsg_off = 0;
  2092. skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
  2093. skb->truesize += PAGE_SIZE;
  2094. atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
  2095. /* get the new initialized frag */
  2096. frg_cnt = skb_shinfo(skb)->nr_frags;
  2097. frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
  2098. /* copy the user data to page */
  2099. left = PAGE_SIZE - frag->page_offset;
  2100. copy = (length > left)? left : length;
  2101. ret = getfrag(from, (page_address(frag->page) +
  2102. frag->page_offset + frag->size),
  2103. offset, copy, 0, skb);
  2104. if (ret < 0)
  2105. return -EFAULT;
  2106. /* copy was successful so update the size parameters */
  2107. sk->sk_sndmsg_off += copy;
  2108. frag->size += copy;
  2109. skb->len += copy;
  2110. skb->data_len += copy;
  2111. offset += copy;
  2112. length -= copy;
  2113. } while (length > 0);
  2114. return 0;
  2115. }
  2116. EXPORT_SYMBOL(skb_append_datato_frags);
  2117. /**
  2118. * skb_pull_rcsum - pull skb and update receive checksum
  2119. * @skb: buffer to update
  2120. * @len: length of data pulled
  2121. *
  2122. * This function performs an skb_pull on the packet and updates
  2123. * the CHECKSUM_COMPLETE checksum. It should be used on
  2124. * receive path processing instead of skb_pull unless you know
  2125. * that the checksum difference is zero (e.g., a valid IP header)
  2126. * or you are setting ip_summed to CHECKSUM_NONE.
  2127. */
  2128. unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
  2129. {
  2130. BUG_ON(len > skb->len);
  2131. skb->len -= len;
  2132. BUG_ON(skb->len < skb->data_len);
  2133. skb_postpull_rcsum(skb, skb->data, len);
  2134. return skb->data += len;
  2135. }
  2136. EXPORT_SYMBOL_GPL(skb_pull_rcsum);
  2137. /**
  2138. * skb_segment - Perform protocol segmentation on skb.
  2139. * @skb: buffer to segment
  2140. * @features: features for the output path (see dev->features)
  2141. *
  2142. * This function performs segmentation on the given skb. It returns
  2143. * a pointer to the first in a list of new skbs for the segments.
  2144. * In case of error it returns ERR_PTR(err).
  2145. */
  2146. struct sk_buff *skb_segment(struct sk_buff *skb, int features)
  2147. {
  2148. struct sk_buff *segs = NULL;
  2149. struct sk_buff *tail = NULL;
  2150. struct sk_buff *fskb = skb_shinfo(skb)->frag_list;
  2151. unsigned int mss = skb_shinfo(skb)->gso_size;
  2152. unsigned int doffset = skb->data - skb_mac_header(skb);
  2153. unsigned int offset = doffset;
  2154. unsigned int headroom;
  2155. unsigned int len;
  2156. int sg = features & NETIF_F_SG;
  2157. int nfrags = skb_shinfo(skb)->nr_frags;
  2158. int err = -ENOMEM;
  2159. int i = 0;
  2160. int pos;
  2161. __skb_push(skb, doffset);
  2162. headroom = skb_headroom(skb);
  2163. pos = skb_headlen(skb);
  2164. do {
  2165. struct sk_buff *nskb;
  2166. skb_frag_t *frag;
  2167. int hsize;
  2168. int size;
  2169. len = skb->len - offset;
  2170. if (len > mss)
  2171. len = mss;
  2172. hsize = skb_headlen(skb) - offset;
  2173. if (hsize < 0)
  2174. hsize = 0;
  2175. if (hsize > len || !sg)
  2176. hsize = len;
  2177. if (!hsize && i >= nfrags) {
  2178. BUG_ON(fskb->len != len);
  2179. pos += len;
  2180. nskb = skb_clone(fskb, GFP_ATOMIC);
  2181. fskb = fskb->next;
  2182. if (unlikely(!nskb))
  2183. goto err;
  2184. hsize = skb_end_pointer(nskb) - nskb->head;
  2185. if (skb_cow_head(nskb, doffset + headroom)) {
  2186. kfree_skb(nskb);
  2187. goto err;
  2188. }
  2189. nskb->truesize += skb_end_pointer(nskb) - nskb->head -
  2190. hsize;
  2191. skb_release_head_state(nskb);
  2192. __skb_push(nskb, doffset);
  2193. } else {
  2194. nskb = alloc_skb(hsize + doffset + headroom,
  2195. GFP_ATOMIC);
  2196. if (unlikely(!nskb))
  2197. goto err;
  2198. skb_reserve(nskb, headroom);
  2199. __skb_put(nskb, doffset);
  2200. }
  2201. if (segs)
  2202. tail->next = nskb;
  2203. else
  2204. segs = nskb;
  2205. tail = nskb;
  2206. __copy_skb_header(nskb, skb);
  2207. nskb->mac_len = skb->mac_len;
  2208. /* nskb and skb might have different headroom */
  2209. if (nskb->ip_summed == CHECKSUM_PARTIAL)
  2210. nskb->csum_start += skb_headroom(nskb) - headroom;
  2211. skb_reset_mac_header(nskb);
  2212. skb_set_network_header(nskb, skb->mac_len);
  2213. nskb->transport_header = (nskb->network_header +
  2214. skb_network_header_len(skb));
  2215. skb_copy_from_linear_data(skb, nskb->data, doffset);
  2216. if (fskb != skb_shinfo(skb)->frag_list)
  2217. continue;
  2218. if (!sg) {
  2219. nskb->ip_summed = CHECKSUM_NONE;
  2220. nskb->csum = skb_copy_and_csum_bits(skb, offset,
  2221. skb_put(nskb, len),
  2222. len, 0);
  2223. continue;
  2224. }
  2225. frag = skb_shinfo(nskb)->frags;
  2226. skb_copy_from_linear_data_offset(skb, offset,
  2227. skb_put(nskb, hsize), hsize);
  2228. while (pos < offset + len && i < nfrags) {
  2229. *frag = skb_shinfo(skb)->frags[i];
  2230. get_page(frag->page);
  2231. size = frag->size;
  2232. if (pos < offset) {
  2233. frag->page_offset += offset - pos;
  2234. frag->size -= offset - pos;
  2235. }
  2236. skb_shinfo(nskb)->nr_frags++;
  2237. if (pos + size <= offset + len) {
  2238. i++;
  2239. pos += size;
  2240. } else {
  2241. frag->size -= pos + size - (offset + len);
  2242. goto skip_fraglist;
  2243. }
  2244. frag++;
  2245. }
  2246. if (pos < offset + len) {
  2247. struct sk_buff *fskb2 = fskb;
  2248. BUG_ON(pos + fskb->len != offset + len);
  2249. pos += fskb->len;
  2250. fskb = fskb->next;
  2251. if (fskb2->next) {
  2252. fskb2 = skb_clone(fskb2, GFP_ATOMIC);
  2253. if (!fskb2)
  2254. goto err;
  2255. } else
  2256. skb_get(fskb2);
  2257. SKB_FRAG_ASSERT(nskb);
  2258. skb_shinfo(nskb)->frag_list = fskb2;
  2259. }
  2260. skip_fraglist:
  2261. nskb->data_len = len - hsize;
  2262. nskb->len += nskb->data_len;
  2263. nskb->truesize += nskb->data_len;
  2264. } while ((offset += len) < skb->len);
  2265. return segs;
  2266. err:
  2267. while ((skb = segs)) {
  2268. segs = skb->next;
  2269. kfree_skb(skb);
  2270. }
  2271. return ERR_PTR(err);
  2272. }
  2273. EXPORT_SYMBOL_GPL(skb_segment);
  2274. int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
  2275. {
  2276. struct sk_buff *p = *head;
  2277. struct sk_buff *nskb;
  2278. struct skb_shared_info *skbinfo = skb_shinfo(skb);
  2279. struct skb_shared_info *pinfo = skb_shinfo(p);
  2280. unsigned int headroom;
  2281. unsigned int len = skb_gro_len(skb);
  2282. unsigned int offset = skb_gro_offset(skb);
  2283. unsigned int headlen = skb_headlen(skb);
  2284. if (p->len + len >= 65536)
  2285. return -E2BIG;
  2286. if (pinfo->frag_list)
  2287. goto merge;
  2288. else if (headlen <= offset) {
  2289. skb_frag_t *frag;
  2290. skb_frag_t *frag2;
  2291. int i = skbinfo->nr_frags;
  2292. int nr_frags = pinfo->nr_frags + i;
  2293. offset -= headlen;
  2294. if (nr_frags > MAX_SKB_FRAGS)
  2295. return -E2BIG;
  2296. pinfo->nr_frags = nr_frags;
  2297. skbinfo->nr_frags = 0;
  2298. frag = pinfo->frags + nr_frags;
  2299. frag2 = skbinfo->frags + i;
  2300. do {
  2301. *--frag = *--frag2;
  2302. } while (--i);
  2303. frag->page_offset += offset;
  2304. frag->size -= offset;
  2305. skb->truesize -= skb->data_len;
  2306. skb->len -= skb->data_len;
  2307. skb->data_len = 0;
  2308. NAPI_GRO_CB(skb)->free = 1;
  2309. goto done;
  2310. } else if (skb_gro_len(p) != pinfo->gso_size)
  2311. return -E2BIG;
  2312. headroom = skb_headroom(p);
  2313. nskb = alloc_skb(headroom + skb_gro_offset(p), GFP_ATOMIC);
  2314. if (unlikely(!nskb))
  2315. return -ENOMEM;
  2316. __copy_skb_header(nskb, p);
  2317. nskb->mac_len = p->mac_len;
  2318. skb_reserve(nskb, headroom);
  2319. __skb_put(nskb, skb_gro_offset(p));
  2320. skb_set_mac_header(nskb, skb_mac_header(p) - p->data);
  2321. skb_set_network_header(nskb, skb_network_offset(p));
  2322. skb_set_transport_header(nskb, skb_transport_offset(p));
  2323. __skb_pull(p, skb_gro_offset(p));
  2324. memcpy(skb_mac_header(nskb), skb_mac_header(p),
  2325. p->data - skb_mac_header(p));
  2326. *NAPI_GRO_CB(nskb) = *NAPI_GRO_CB(p);
  2327. skb_shinfo(nskb)->frag_list = p;
  2328. skb_shinfo(nskb)->gso_size = pinfo->gso_size;
  2329. pinfo->gso_size = 0;
  2330. skb_header_release(p);
  2331. nskb->prev = p;
  2332. nskb->data_len += p->len;
  2333. nskb->truesize += p->len;
  2334. nskb->len += p->len;
  2335. *head = nskb;
  2336. nskb->next = p->next;
  2337. p->next = NULL;
  2338. p = nskb;
  2339. merge:
  2340. if (offset > headlen) {
  2341. skbinfo->frags[0].page_offset += offset - headlen;
  2342. skbinfo->frags[0].size -= offset - headlen;
  2343. offset = headlen;
  2344. }
  2345. __skb_pull(skb, offset);
  2346. p->prev->next = skb;
  2347. p->prev = skb;
  2348. skb_header_release(skb);
  2349. done:
  2350. NAPI_GRO_CB(p)->count++;
  2351. p->data_len += len;
  2352. p->truesize += len;
  2353. p->len += len;
  2354. NAPI_GRO_CB(skb)->same_flow = 1;
  2355. return 0;
  2356. }
  2357. EXPORT_SYMBOL_GPL(skb_gro_receive);
  2358. void __init skb_init(void)
  2359. {
  2360. skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
  2361. sizeof(struct sk_buff),
  2362. 0,
  2363. SLAB_HWCACHE_ALIGN|SLAB_PANIC,
  2364. NULL);
  2365. skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
  2366. (2*sizeof(struct sk_buff)) +
  2367. sizeof(atomic_t),
  2368. 0,
  2369. SLAB_HWCACHE_ALIGN|SLAB_PANIC,
  2370. NULL);
  2371. }
  2372. /**
  2373. * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
  2374. * @skb: Socket buffer containing the buffers to be mapped
  2375. * @sg: The scatter-gather list to map into
  2376. * @offset: The offset into the buffer's contents to start mapping
  2377. * @len: Length of buffer space to be mapped
  2378. *
  2379. * Fill the specified scatter-gather list with mappings/pointers into a
  2380. * region of the buffer space attached to a socket buffer.
  2381. */
  2382. static int
  2383. __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
  2384. {
  2385. int start = skb_headlen(skb);
  2386. int i, copy = start - offset;
  2387. struct sk_buff *frag_iter;
  2388. int elt = 0;
  2389. if (copy > 0) {
  2390. if (copy > len)
  2391. copy = len;
  2392. sg_set_buf(sg, skb->data + offset, copy);
  2393. elt++;
  2394. if ((len -= copy) == 0)
  2395. return elt;
  2396. offset += copy;
  2397. }
  2398. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  2399. int end;
  2400. WARN_ON(start > offset + len);
  2401. end = start + skb_shinfo(skb)->frags[i].size;
  2402. if ((copy = end - offset) > 0) {
  2403. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  2404. if (copy > len)
  2405. copy = len;
  2406. sg_set_page(&sg[elt], frag->page, copy,
  2407. frag->page_offset+offset-start);
  2408. elt++;
  2409. if (!(len -= copy))
  2410. return elt;
  2411. offset += copy;
  2412. }
  2413. start = end;
  2414. }
  2415. skb_walk_frags(skb, frag_iter) {
  2416. int end;
  2417. WARN_ON(start > offset + len);
  2418. end = start + frag_iter->len;
  2419. if ((copy = end - offset) > 0) {
  2420. if (copy > len)
  2421. copy = len;
  2422. elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start,
  2423. copy);
  2424. if ((len -= copy) == 0)
  2425. return elt;
  2426. offset += copy;
  2427. }
  2428. start = end;
  2429. }
  2430. BUG_ON(len);
  2431. return elt;
  2432. }
  2433. int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
  2434. {
  2435. int nsg = __skb_to_sgvec(skb, sg, offset, len);
  2436. sg_mark_end(&sg[nsg - 1]);
  2437. return nsg;
  2438. }
  2439. EXPORT_SYMBOL_GPL(skb_to_sgvec);
  2440. /**
  2441. * skb_cow_data - Check that a socket buffer's data buffers are writable
  2442. * @skb: The socket buffer to check.
  2443. * @tailbits: Amount of trailing space to be added
  2444. * @trailer: Returned pointer to the skb where the @tailbits space begins
  2445. *
  2446. * Make sure that the data buffers attached to a socket buffer are
  2447. * writable. If they are not, private copies are made of the data buffers
  2448. * and the socket buffer is set to use these instead.
  2449. *
  2450. * If @tailbits is given, make sure that there is space to write @tailbits
  2451. * bytes of data beyond current end of socket buffer. @trailer will be
  2452. * set to point to the skb in which this space begins.
  2453. *
  2454. * The number of scatterlist elements required to completely map the
  2455. * COW'd and extended socket buffer will be returned.
  2456. */
  2457. int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
  2458. {
  2459. int copyflag;
  2460. int elt;
  2461. struct sk_buff *skb1, **skb_p;
  2462. /* If skb is cloned or its head is paged, reallocate
  2463. * head pulling out all the pages (pages are considered not writable
  2464. * at the moment even if they are anonymous).
  2465. */
  2466. if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
  2467. __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
  2468. return -ENOMEM;
  2469. /* Easy case. Most of packets will go this way. */
  2470. if (!skb_has_frag_list(skb)) {
  2471. /* A little of trouble, not enough of space for trailer.
  2472. * This should not happen, when stack is tuned to generate
  2473. * good frames. OK, on miss we reallocate and reserve even more
  2474. * space, 128 bytes is fair. */
  2475. if (skb_tailroom(skb) < tailbits &&
  2476. pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
  2477. return -ENOMEM;
  2478. /* Voila! */
  2479. *trailer = skb;
  2480. return 1;
  2481. }
  2482. /* Misery. We are in troubles, going to mincer fragments... */
  2483. elt = 1;
  2484. skb_p = &skb_shinfo(skb)->frag_list;
  2485. copyflag = 0;
  2486. while ((skb1 = *skb_p) != NULL) {
  2487. int ntail = 0;
  2488. /* The fragment is partially pulled by someone,
  2489. * this can happen on input. Copy it and everything
  2490. * after it. */
  2491. if (skb_shared(skb1))
  2492. copyflag = 1;
  2493. /* If the skb is the last, worry about trailer. */
  2494. if (skb1->next == NULL && tailbits) {
  2495. if (skb_shinfo(skb1)->nr_frags ||
  2496. skb_has_frag_list(skb1) ||
  2497. skb_tailroom(skb1) < tailbits)
  2498. ntail = tailbits + 128;
  2499. }
  2500. if (copyflag ||
  2501. skb_cloned(skb1) ||
  2502. ntail ||
  2503. skb_shinfo(skb1)->nr_frags ||
  2504. skb_has_frag_list(skb1)) {
  2505. struct sk_buff *skb2;
  2506. /* Fuck, we are miserable poor guys... */
  2507. if (ntail == 0)
  2508. skb2 = skb_copy(skb1, GFP_ATOMIC);
  2509. else
  2510. skb2 = skb_copy_expand(skb1,
  2511. skb_headroom(skb1),
  2512. ntail,
  2513. GFP_ATOMIC);
  2514. if (unlikely(skb2 == NULL))
  2515. return -ENOMEM;
  2516. if (skb1->sk)
  2517. skb_set_owner_w(skb2, skb1->sk);
  2518. /* Looking around. Are we still alive?
  2519. * OK, link new skb, drop old one */
  2520. skb2->next = skb1->next;
  2521. *skb_p = skb2;
  2522. kfree_skb(skb1);
  2523. skb1 = skb2;
  2524. }
  2525. elt++;
  2526. *trailer = skb1;
  2527. skb_p = &skb1->next;
  2528. }
  2529. return elt;
  2530. }
  2531. EXPORT_SYMBOL_GPL(skb_cow_data);
  2532. static void sock_rmem_free(struct sk_buff *skb)
  2533. {
  2534. struct sock *sk = skb->sk;
  2535. atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
  2536. }
  2537. /*
  2538. * Note: We dont mem charge error packets (no sk_forward_alloc changes)
  2539. */
  2540. int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
  2541. {
  2542. if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
  2543. (unsigned)sk->sk_rcvbuf)
  2544. return -ENOMEM;
  2545. skb_orphan(skb);
  2546. skb->sk = sk;
  2547. skb->destructor = sock_rmem_free;
  2548. atomic_add(skb->truesize, &sk->sk_rmem_alloc);
  2549. skb_queue_tail(&sk->sk_error_queue, skb);
  2550. if (!sock_flag(sk, SOCK_DEAD))
  2551. sk->sk_data_ready(sk, skb->len);
  2552. return 0;
  2553. }
  2554. EXPORT_SYMBOL(sock_queue_err_skb);
  2555. void skb_tstamp_tx(struct sk_buff *orig_skb,
  2556. struct skb_shared_hwtstamps *hwtstamps)
  2557. {
  2558. struct sock *sk = orig_skb->sk;
  2559. struct sock_exterr_skb *serr;
  2560. struct sk_buff *skb;
  2561. int err;
  2562. if (!sk)
  2563. return;
  2564. skb = skb_clone(orig_skb, GFP_ATOMIC);
  2565. if (!skb)
  2566. return;
  2567. if (hwtstamps) {
  2568. *skb_hwtstamps(skb) =
  2569. *hwtstamps;
  2570. } else {
  2571. /*
  2572. * no hardware time stamps available,
  2573. * so keep the shared tx_flags and only
  2574. * store software time stamp
  2575. */
  2576. skb->tstamp = ktime_get_real();
  2577. }
  2578. serr = SKB_EXT_ERR(skb);
  2579. memset(serr, 0, sizeof(*serr));
  2580. serr->ee.ee_errno = ENOMSG;
  2581. serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
  2582. err = sock_queue_err_skb(sk, skb);
  2583. if (err)
  2584. kfree_skb(skb);
  2585. }
  2586. EXPORT_SYMBOL_GPL(skb_tstamp_tx);
  2587. /**
  2588. * skb_partial_csum_set - set up and verify partial csum values for packet
  2589. * @skb: the skb to set
  2590. * @start: the number of bytes after skb->data to start checksumming.
  2591. * @off: the offset from start to place the checksum.
  2592. *
  2593. * For untrusted partially-checksummed packets, we need to make sure the values
  2594. * for skb->csum_start and skb->csum_offset are valid so we don't oops.
  2595. *
  2596. * This function checks and sets those values and skb->ip_summed: if this
  2597. * returns false you should drop the packet.
  2598. */
  2599. bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
  2600. {
  2601. if (unlikely(start > skb_headlen(skb)) ||
  2602. unlikely((int)start + off > skb_headlen(skb) - 2)) {
  2603. if (net_ratelimit())
  2604. printk(KERN_WARNING
  2605. "bad partial csum: csum=%u/%u len=%u\n",
  2606. start, off, skb_headlen(skb));
  2607. return false;
  2608. }
  2609. skb->ip_summed = CHECKSUM_PARTIAL;
  2610. skb->csum_start = skb_headroom(skb) + start;
  2611. skb->csum_offset = off;
  2612. return true;
  2613. }
  2614. EXPORT_SYMBOL_GPL(skb_partial_csum_set);
  2615. void __skb_warn_lro_forwarding(const struct sk_buff *skb)
  2616. {
  2617. if (net_ratelimit())
  2618. pr_warning("%s: received packets cannot be forwarded"
  2619. " while LRO is enabled\n", skb->dev->name);
  2620. }
  2621. EXPORT_SYMBOL(__skb_warn_lro_forwarding);