skbuff.c 65 KB

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