skbuff.c 74 KB

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