skbuff.c 74 KB

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