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