skbuff.c 75 KB

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