skbuff.c 75 KB

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