skbuff.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037
  1. /*
  2. * Routines having to do with the 'struct sk_buff' memory handlers.
  3. *
  4. * Authors: Alan Cox <iiitac@pyr.swan.ac.uk>
  5. * Florian La Roche <rzsfl@rz.uni-sb.de>
  6. *
  7. * Version: $Id: skbuff.c,v 1.90 2001/11/07 05:56:19 davem Exp $
  8. *
  9. * Fixes:
  10. * Alan Cox : Fixed the worst of the load
  11. * balancer bugs.
  12. * Dave Platt : Interrupt stacking fix.
  13. * Richard Kooijman : Timestamp fixes.
  14. * Alan Cox : Changed buffer format.
  15. * Alan Cox : destructor hook for AF_UNIX etc.
  16. * Linus Torvalds : Better skb_clone.
  17. * Alan Cox : Added skb_copy.
  18. * Alan Cox : Added all the changed routines Linus
  19. * only put in the headers
  20. * Ray VanTassle : Fixed --skb->lock in free
  21. * Alan Cox : skb_copy copy arp field
  22. * Andi Kleen : slabified it.
  23. * Robert Olsson : Removed skb_head_pool
  24. *
  25. * NOTE:
  26. * The __skb_ routines should be called with interrupts
  27. * disabled, or you better be *real* sure that the operation is atomic
  28. * with respect to whatever list is being frobbed (e.g. via lock_sock()
  29. * or via disabling bottom half handlers, etc).
  30. *
  31. * This program is free software; you can redistribute it and/or
  32. * modify it under the terms of the GNU General Public License
  33. * as published by the Free Software Foundation; either version
  34. * 2 of the License, or (at your option) any later version.
  35. */
  36. /*
  37. * The functions in this file will not compile correctly with gcc 2.4.x
  38. */
  39. #include <linux/module.h>
  40. #include <linux/types.h>
  41. #include <linux/kernel.h>
  42. #include <linux/mm.h>
  43. #include <linux/interrupt.h>
  44. #include <linux/in.h>
  45. #include <linux/inet.h>
  46. #include <linux/slab.h>
  47. #include <linux/netdevice.h>
  48. #ifdef CONFIG_NET_CLS_ACT
  49. #include <net/pkt_sched.h>
  50. #endif
  51. #include <linux/string.h>
  52. #include <linux/skbuff.h>
  53. #include <linux/cache.h>
  54. #include <linux/rtnetlink.h>
  55. #include <linux/init.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. /*
  67. * Keep out-of-line to prevent kernel bloat.
  68. * __builtin_return_address is not used because it is not always
  69. * reliable.
  70. */
  71. /**
  72. * skb_over_panic - private function
  73. * @skb: buffer
  74. * @sz: size
  75. * @here: address
  76. *
  77. * Out of line support code for skb_put(). Not user callable.
  78. */
  79. void skb_over_panic(struct sk_buff *skb, int sz, void *here)
  80. {
  81. printk(KERN_EMERG "skb_over_panic: text:%p len:%d put:%d head:%p "
  82. "data:%p tail:%#lx end:%#lx dev:%s\n",
  83. here, skb->len, sz, skb->head, skb->data,
  84. (unsigned long)skb->tail, (unsigned long)skb->end,
  85. skb->dev ? skb->dev->name : "<NULL>");
  86. BUG();
  87. }
  88. /**
  89. * skb_under_panic - private function
  90. * @skb: buffer
  91. * @sz: size
  92. * @here: address
  93. *
  94. * Out of line support code for skb_push(). Not user callable.
  95. */
  96. void skb_under_panic(struct sk_buff *skb, int sz, void *here)
  97. {
  98. printk(KERN_EMERG "skb_under_panic: text:%p len:%d put:%d head:%p "
  99. "data:%p tail:%#lx end:%#lx dev:%s\n",
  100. here, skb->len, sz, skb->head, skb->data,
  101. (unsigned long)skb->tail, (unsigned long)skb->end,
  102. skb->dev ? skb->dev->name : "<NULL>");
  103. BUG();
  104. }
  105. void skb_truesize_bug(struct sk_buff *skb)
  106. {
  107. printk(KERN_ERR "SKB BUG: Invalid truesize (%u) "
  108. "len=%u, sizeof(sk_buff)=%Zd\n",
  109. skb->truesize, skb->len, sizeof(struct sk_buff));
  110. }
  111. EXPORT_SYMBOL(skb_truesize_bug);
  112. /* Allocate a new skbuff. We do this ourselves so we can fill in a few
  113. * 'private' fields and also do memory statistics to find all the
  114. * [BEEP] leaks.
  115. *
  116. */
  117. /**
  118. * __alloc_skb - allocate a network buffer
  119. * @size: size to allocate
  120. * @gfp_mask: allocation mask
  121. * @fclone: allocate from fclone cache instead of head cache
  122. * and allocate a cloned (child) skb
  123. * @node: numa node to allocate memory on
  124. *
  125. * Allocate a new &sk_buff. The returned buffer has no headroom and a
  126. * tail room of size bytes. The object has a reference count of one.
  127. * The return is the buffer. On a failure the return is %NULL.
  128. *
  129. * Buffers may only be allocated from interrupts using a @gfp_mask of
  130. * %GFP_ATOMIC.
  131. */
  132. struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
  133. int fclone, int node)
  134. {
  135. struct kmem_cache *cache;
  136. struct skb_shared_info *shinfo;
  137. struct sk_buff *skb;
  138. u8 *data;
  139. cache = fclone ? skbuff_fclone_cache : skbuff_head_cache;
  140. /* Get the HEAD */
  141. skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
  142. if (!skb)
  143. goto out;
  144. size = SKB_DATA_ALIGN(size);
  145. data = kmalloc_node_track_caller(size + sizeof(struct skb_shared_info),
  146. gfp_mask, node);
  147. if (!data)
  148. goto nodata;
  149. /*
  150. * See comment in sk_buff definition, just before the 'tail' member
  151. */
  152. memset(skb, 0, offsetof(struct sk_buff, tail));
  153. skb->truesize = size + sizeof(struct sk_buff);
  154. atomic_set(&skb->users, 1);
  155. skb->head = data;
  156. skb->data = data;
  157. skb_reset_tail_pointer(skb);
  158. skb->end = skb->tail + size;
  159. /* make sure we initialize shinfo sequentially */
  160. shinfo = skb_shinfo(skb);
  161. atomic_set(&shinfo->dataref, 1);
  162. shinfo->nr_frags = 0;
  163. shinfo->gso_size = 0;
  164. shinfo->gso_segs = 0;
  165. shinfo->gso_type = 0;
  166. shinfo->ip6_frag_id = 0;
  167. shinfo->frag_list = NULL;
  168. if (fclone) {
  169. struct sk_buff *child = skb + 1;
  170. atomic_t *fclone_ref = (atomic_t *) (child + 1);
  171. skb->fclone = SKB_FCLONE_ORIG;
  172. atomic_set(fclone_ref, 1);
  173. child->fclone = SKB_FCLONE_UNAVAILABLE;
  174. }
  175. out:
  176. return skb;
  177. nodata:
  178. kmem_cache_free(cache, skb);
  179. skb = NULL;
  180. goto out;
  181. }
  182. /**
  183. * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
  184. * @dev: network device to receive on
  185. * @length: length to allocate
  186. * @gfp_mask: get_free_pages mask, passed to alloc_skb
  187. *
  188. * Allocate a new &sk_buff and assign it a usage count of one. The
  189. * buffer has unspecified headroom built in. Users should allocate
  190. * the headroom they think they need without accounting for the
  191. * built in space. The built in space is used for optimisations.
  192. *
  193. * %NULL is returned if there is no free memory.
  194. */
  195. struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
  196. unsigned int length, gfp_t gfp_mask)
  197. {
  198. int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
  199. struct sk_buff *skb;
  200. skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask, 0, node);
  201. if (likely(skb)) {
  202. skb_reserve(skb, NET_SKB_PAD);
  203. skb->dev = dev;
  204. }
  205. return skb;
  206. }
  207. static void skb_drop_list(struct sk_buff **listp)
  208. {
  209. struct sk_buff *list = *listp;
  210. *listp = NULL;
  211. do {
  212. struct sk_buff *this = list;
  213. list = list->next;
  214. kfree_skb(this);
  215. } while (list);
  216. }
  217. static inline void skb_drop_fraglist(struct sk_buff *skb)
  218. {
  219. skb_drop_list(&skb_shinfo(skb)->frag_list);
  220. }
  221. static void skb_clone_fraglist(struct sk_buff *skb)
  222. {
  223. struct sk_buff *list;
  224. for (list = skb_shinfo(skb)->frag_list; list; list = list->next)
  225. skb_get(list);
  226. }
  227. static void skb_release_data(struct sk_buff *skb)
  228. {
  229. if (!skb->cloned ||
  230. !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
  231. &skb_shinfo(skb)->dataref)) {
  232. if (skb_shinfo(skb)->nr_frags) {
  233. int i;
  234. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
  235. put_page(skb_shinfo(skb)->frags[i].page);
  236. }
  237. if (skb_shinfo(skb)->frag_list)
  238. skb_drop_fraglist(skb);
  239. kfree(skb->head);
  240. }
  241. }
  242. /*
  243. * Free an skbuff by memory without cleaning the state.
  244. */
  245. void kfree_skbmem(struct sk_buff *skb)
  246. {
  247. struct sk_buff *other;
  248. atomic_t *fclone_ref;
  249. skb_release_data(skb);
  250. switch (skb->fclone) {
  251. case SKB_FCLONE_UNAVAILABLE:
  252. kmem_cache_free(skbuff_head_cache, skb);
  253. break;
  254. case SKB_FCLONE_ORIG:
  255. fclone_ref = (atomic_t *) (skb + 2);
  256. if (atomic_dec_and_test(fclone_ref))
  257. kmem_cache_free(skbuff_fclone_cache, skb);
  258. break;
  259. case SKB_FCLONE_CLONE:
  260. fclone_ref = (atomic_t *) (skb + 1);
  261. other = skb - 1;
  262. /* The clone portion is available for
  263. * fast-cloning again.
  264. */
  265. skb->fclone = SKB_FCLONE_UNAVAILABLE;
  266. if (atomic_dec_and_test(fclone_ref))
  267. kmem_cache_free(skbuff_fclone_cache, other);
  268. break;
  269. };
  270. }
  271. /**
  272. * __kfree_skb - private function
  273. * @skb: buffer
  274. *
  275. * Free an sk_buff. Release anything attached to the buffer.
  276. * Clean the state. This is an internal helper function. Users should
  277. * always call kfree_skb
  278. */
  279. void __kfree_skb(struct sk_buff *skb)
  280. {
  281. dst_release(skb->dst);
  282. #ifdef CONFIG_XFRM
  283. secpath_put(skb->sp);
  284. #endif
  285. if (skb->destructor) {
  286. WARN_ON(in_irq());
  287. skb->destructor(skb);
  288. }
  289. #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
  290. nf_conntrack_put(skb->nfct);
  291. nf_conntrack_put_reasm(skb->nfct_reasm);
  292. #endif
  293. #ifdef CONFIG_BRIDGE_NETFILTER
  294. nf_bridge_put(skb->nf_bridge);
  295. #endif
  296. /* XXX: IS this still necessary? - JHS */
  297. #ifdef CONFIG_NET_SCHED
  298. skb->tc_index = 0;
  299. #ifdef CONFIG_NET_CLS_ACT
  300. skb->tc_verd = 0;
  301. #endif
  302. #endif
  303. kfree_skbmem(skb);
  304. }
  305. /**
  306. * kfree_skb - free an sk_buff
  307. * @skb: buffer to free
  308. *
  309. * Drop a reference to the buffer and free it if the usage count has
  310. * hit zero.
  311. */
  312. void kfree_skb(struct sk_buff *skb)
  313. {
  314. if (unlikely(!skb))
  315. return;
  316. if (likely(atomic_read(&skb->users) == 1))
  317. smp_rmb();
  318. else if (likely(!atomic_dec_and_test(&skb->users)))
  319. return;
  320. __kfree_skb(skb);
  321. }
  322. /**
  323. * skb_clone - duplicate an sk_buff
  324. * @skb: buffer to clone
  325. * @gfp_mask: allocation priority
  326. *
  327. * Duplicate an &sk_buff. The new one is not owned by a socket. Both
  328. * copies share the same packet data but not structure. The new
  329. * buffer has a reference count of 1. If the allocation fails the
  330. * function returns %NULL otherwise the new buffer is returned.
  331. *
  332. * If this function is called from an interrupt gfp_mask() must be
  333. * %GFP_ATOMIC.
  334. */
  335. struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
  336. {
  337. struct sk_buff *n;
  338. n = skb + 1;
  339. if (skb->fclone == SKB_FCLONE_ORIG &&
  340. n->fclone == SKB_FCLONE_UNAVAILABLE) {
  341. atomic_t *fclone_ref = (atomic_t *) (n + 1);
  342. n->fclone = SKB_FCLONE_CLONE;
  343. atomic_inc(fclone_ref);
  344. } else {
  345. n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
  346. if (!n)
  347. return NULL;
  348. n->fclone = SKB_FCLONE_UNAVAILABLE;
  349. }
  350. #define C(x) n->x = skb->x
  351. n->next = n->prev = NULL;
  352. n->sk = NULL;
  353. C(tstamp);
  354. C(dev);
  355. C(transport_header);
  356. C(network_header);
  357. C(mac_header);
  358. C(dst);
  359. dst_clone(skb->dst);
  360. C(sp);
  361. #ifdef CONFIG_INET
  362. secpath_get(skb->sp);
  363. #endif
  364. memcpy(n->cb, skb->cb, sizeof(skb->cb));
  365. C(len);
  366. C(data_len);
  367. C(mac_len);
  368. C(csum);
  369. C(local_df);
  370. n->cloned = 1;
  371. n->nohdr = 0;
  372. C(pkt_type);
  373. C(ip_summed);
  374. C(priority);
  375. #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
  376. C(ipvs_property);
  377. #endif
  378. C(protocol);
  379. n->destructor = NULL;
  380. C(mark);
  381. __nf_copy(n, skb);
  382. #ifdef CONFIG_NET_SCHED
  383. C(tc_index);
  384. #ifdef CONFIG_NET_CLS_ACT
  385. n->tc_verd = SET_TC_VERD(skb->tc_verd,0);
  386. n->tc_verd = CLR_TC_OK2MUNGE(n->tc_verd);
  387. n->tc_verd = CLR_TC_MUNGED(n->tc_verd);
  388. C(iif);
  389. #endif
  390. skb_copy_secmark(n, skb);
  391. #endif
  392. C(truesize);
  393. atomic_set(&n->users, 1);
  394. C(head);
  395. C(data);
  396. C(tail);
  397. C(end);
  398. atomic_inc(&(skb_shinfo(skb)->dataref));
  399. skb->cloned = 1;
  400. return n;
  401. }
  402. static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
  403. {
  404. #ifndef NET_SKBUFF_DATA_USES_OFFSET
  405. /*
  406. * Shift between the two data areas in bytes
  407. */
  408. unsigned long offset = new->data - old->data;
  409. #endif
  410. new->sk = NULL;
  411. new->dev = old->dev;
  412. new->priority = old->priority;
  413. new->protocol = old->protocol;
  414. new->dst = dst_clone(old->dst);
  415. #ifdef CONFIG_INET
  416. new->sp = secpath_get(old->sp);
  417. #endif
  418. new->transport_header = old->transport_header;
  419. new->network_header = old->network_header;
  420. new->mac_header = old->mac_header;
  421. #ifndef NET_SKBUFF_DATA_USES_OFFSET
  422. /* {transport,network,mac}_header are relative to skb->head */
  423. new->transport_header += offset;
  424. new->network_header += offset;
  425. new->mac_header += offset;
  426. #endif
  427. memcpy(new->cb, old->cb, sizeof(old->cb));
  428. new->local_df = old->local_df;
  429. new->fclone = SKB_FCLONE_UNAVAILABLE;
  430. new->pkt_type = old->pkt_type;
  431. new->tstamp = old->tstamp;
  432. new->destructor = NULL;
  433. new->mark = old->mark;
  434. __nf_copy(new, old);
  435. #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
  436. new->ipvs_property = old->ipvs_property;
  437. #endif
  438. #ifdef CONFIG_NET_SCHED
  439. #ifdef CONFIG_NET_CLS_ACT
  440. new->tc_verd = old->tc_verd;
  441. #endif
  442. new->tc_index = old->tc_index;
  443. #endif
  444. skb_copy_secmark(new, old);
  445. atomic_set(&new->users, 1);
  446. skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
  447. skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
  448. skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
  449. }
  450. /**
  451. * skb_copy - create private copy of an sk_buff
  452. * @skb: buffer to copy
  453. * @gfp_mask: allocation priority
  454. *
  455. * Make a copy of both an &sk_buff and its data. This is used when the
  456. * caller wishes to modify the data and needs a private copy of the
  457. * data to alter. Returns %NULL on failure or the pointer to the buffer
  458. * on success. The returned buffer has a reference count of 1.
  459. *
  460. * As by-product this function converts non-linear &sk_buff to linear
  461. * one, so that &sk_buff becomes completely private and caller is allowed
  462. * to modify all the data of returned buffer. This means that this
  463. * function is not recommended for use in circumstances when only
  464. * header is going to be modified. Use pskb_copy() instead.
  465. */
  466. struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
  467. {
  468. int headerlen = skb->data - skb->head;
  469. /*
  470. * Allocate the copy buffer
  471. */
  472. struct sk_buff *n;
  473. #ifdef NET_SKBUFF_DATA_USES_OFFSET
  474. n = alloc_skb(skb->end + skb->data_len, gfp_mask);
  475. #else
  476. n = alloc_skb(skb->end - skb->head + skb->data_len, gfp_mask);
  477. #endif
  478. if (!n)
  479. return NULL;
  480. /* Set the data pointer */
  481. skb_reserve(n, headerlen);
  482. /* Set the tail pointer and length */
  483. skb_put(n, skb->len);
  484. n->csum = skb->csum;
  485. n->ip_summed = skb->ip_summed;
  486. if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
  487. BUG();
  488. copy_skb_header(n, skb);
  489. return n;
  490. }
  491. /**
  492. * pskb_copy - create copy of an sk_buff with private head.
  493. * @skb: buffer to copy
  494. * @gfp_mask: allocation priority
  495. *
  496. * Make a copy of both an &sk_buff and part of its data, located
  497. * in header. Fragmented data remain shared. This is used when
  498. * the caller wishes to modify only header of &sk_buff and needs
  499. * private copy of the header to alter. Returns %NULL on failure
  500. * or the pointer to the buffer on success.
  501. * The returned buffer has a reference count of 1.
  502. */
  503. struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask)
  504. {
  505. /*
  506. * Allocate the copy buffer
  507. */
  508. struct sk_buff *n;
  509. #ifdef NET_SKBUFF_DATA_USES_OFFSET
  510. n = alloc_skb(skb->end, gfp_mask);
  511. #else
  512. n = alloc_skb(skb->end - skb->head, gfp_mask);
  513. #endif
  514. if (!n)
  515. goto out;
  516. /* Set the data pointer */
  517. skb_reserve(n, skb->data - skb->head);
  518. /* Set the tail pointer and length */
  519. skb_put(n, skb_headlen(skb));
  520. /* Copy the bytes */
  521. memcpy(n->data, skb->data, n->len);
  522. n->csum = skb->csum;
  523. n->ip_summed = skb->ip_summed;
  524. n->truesize += skb->data_len;
  525. n->data_len = skb->data_len;
  526. n->len = skb->len;
  527. if (skb_shinfo(skb)->nr_frags) {
  528. int i;
  529. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  530. skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
  531. get_page(skb_shinfo(n)->frags[i].page);
  532. }
  533. skb_shinfo(n)->nr_frags = i;
  534. }
  535. if (skb_shinfo(skb)->frag_list) {
  536. skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
  537. skb_clone_fraglist(n);
  538. }
  539. copy_skb_header(n, skb);
  540. out:
  541. return n;
  542. }
  543. /**
  544. * pskb_expand_head - reallocate header of &sk_buff
  545. * @skb: buffer to reallocate
  546. * @nhead: room to add at head
  547. * @ntail: room to add at tail
  548. * @gfp_mask: allocation priority
  549. *
  550. * Expands (or creates identical copy, if &nhead and &ntail are zero)
  551. * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
  552. * reference count of 1. Returns zero in the case of success or error,
  553. * if expansion failed. In the last case, &sk_buff is not changed.
  554. *
  555. * All the pointers pointing into skb header may change and must be
  556. * reloaded after call to this function.
  557. */
  558. int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
  559. gfp_t gfp_mask)
  560. {
  561. int i;
  562. u8 *data;
  563. #ifdef NET_SKBUFF_DATA_USES_OFFSET
  564. int size = nhead + skb->end + ntail;
  565. #else
  566. int size = nhead + (skb->end - skb->head) + ntail;
  567. #endif
  568. long off;
  569. if (skb_shared(skb))
  570. BUG();
  571. size = SKB_DATA_ALIGN(size);
  572. data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
  573. if (!data)
  574. goto nodata;
  575. /* Copy only real data... and, alas, header. This should be
  576. * optimized for the cases when header is void. */
  577. memcpy(data + nhead, skb->head,
  578. #ifdef NET_SKBUFF_DATA_USES_OFFSET
  579. skb->tail);
  580. #else
  581. skb->tail - skb->head);
  582. #endif
  583. memcpy(data + size, skb_end_pointer(skb),
  584. sizeof(struct skb_shared_info));
  585. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
  586. get_page(skb_shinfo(skb)->frags[i].page);
  587. if (skb_shinfo(skb)->frag_list)
  588. skb_clone_fraglist(skb);
  589. skb_release_data(skb);
  590. off = (data + nhead) - skb->head;
  591. skb->head = data;
  592. skb->data += off;
  593. #ifdef NET_SKBUFF_DATA_USES_OFFSET
  594. skb->end = size;
  595. #else
  596. skb->end = skb->head + size;
  597. /* {transport,network,mac}_header and tail are relative to skb->head */
  598. skb->tail += off;
  599. skb->transport_header += off;
  600. skb->network_header += off;
  601. skb->mac_header += off;
  602. #endif
  603. skb->cloned = 0;
  604. skb->nohdr = 0;
  605. atomic_set(&skb_shinfo(skb)->dataref, 1);
  606. return 0;
  607. nodata:
  608. return -ENOMEM;
  609. }
  610. /* Make private copy of skb with writable head and some headroom */
  611. struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
  612. {
  613. struct sk_buff *skb2;
  614. int delta = headroom - skb_headroom(skb);
  615. if (delta <= 0)
  616. skb2 = pskb_copy(skb, GFP_ATOMIC);
  617. else {
  618. skb2 = skb_clone(skb, GFP_ATOMIC);
  619. if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
  620. GFP_ATOMIC)) {
  621. kfree_skb(skb2);
  622. skb2 = NULL;
  623. }
  624. }
  625. return skb2;
  626. }
  627. /**
  628. * skb_copy_expand - copy and expand sk_buff
  629. * @skb: buffer to copy
  630. * @newheadroom: new free bytes at head
  631. * @newtailroom: new free bytes at tail
  632. * @gfp_mask: allocation priority
  633. *
  634. * Make a copy of both an &sk_buff and its data and while doing so
  635. * allocate additional space.
  636. *
  637. * This is used when the caller wishes to modify the data and needs a
  638. * private copy of the data to alter as well as more space for new fields.
  639. * Returns %NULL on failure or the pointer to the buffer
  640. * on success. The returned buffer has a reference count of 1.
  641. *
  642. * You must pass %GFP_ATOMIC as the allocation priority if this function
  643. * is called from an interrupt.
  644. *
  645. * BUG ALERT: ip_summed is not copied. Why does this work? Is it used
  646. * only by netfilter in the cases when checksum is recalculated? --ANK
  647. */
  648. struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
  649. int newheadroom, int newtailroom,
  650. gfp_t gfp_mask)
  651. {
  652. /*
  653. * Allocate the copy buffer
  654. */
  655. struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
  656. gfp_mask);
  657. int head_copy_len, head_copy_off;
  658. if (!n)
  659. return NULL;
  660. skb_reserve(n, newheadroom);
  661. /* Set the tail pointer and length */
  662. skb_put(n, skb->len);
  663. head_copy_len = skb_headroom(skb);
  664. head_copy_off = 0;
  665. if (newheadroom <= head_copy_len)
  666. head_copy_len = newheadroom;
  667. else
  668. head_copy_off = newheadroom - head_copy_len;
  669. /* Copy the linear header and data. */
  670. if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
  671. skb->len + head_copy_len))
  672. BUG();
  673. copy_skb_header(n, skb);
  674. return n;
  675. }
  676. /**
  677. * skb_pad - zero pad the tail of an skb
  678. * @skb: buffer to pad
  679. * @pad: space to pad
  680. *
  681. * Ensure that a buffer is followed by a padding area that is zero
  682. * filled. Used by network drivers which may DMA or transfer data
  683. * beyond the buffer end onto the wire.
  684. *
  685. * May return error in out of memory cases. The skb is freed on error.
  686. */
  687. int skb_pad(struct sk_buff *skb, int pad)
  688. {
  689. int err;
  690. int ntail;
  691. /* If the skbuff is non linear tailroom is always zero.. */
  692. if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
  693. memset(skb->data+skb->len, 0, pad);
  694. return 0;
  695. }
  696. ntail = skb->data_len + pad - (skb->end - skb->tail);
  697. if (likely(skb_cloned(skb) || ntail > 0)) {
  698. err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
  699. if (unlikely(err))
  700. goto free_skb;
  701. }
  702. /* FIXME: The use of this function with non-linear skb's really needs
  703. * to be audited.
  704. */
  705. err = skb_linearize(skb);
  706. if (unlikely(err))
  707. goto free_skb;
  708. memset(skb->data + skb->len, 0, pad);
  709. return 0;
  710. free_skb:
  711. kfree_skb(skb);
  712. return err;
  713. }
  714. /* Trims skb to length len. It can change skb pointers.
  715. */
  716. int ___pskb_trim(struct sk_buff *skb, unsigned int len)
  717. {
  718. struct sk_buff **fragp;
  719. struct sk_buff *frag;
  720. int offset = skb_headlen(skb);
  721. int nfrags = skb_shinfo(skb)->nr_frags;
  722. int i;
  723. int err;
  724. if (skb_cloned(skb) &&
  725. unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
  726. return err;
  727. i = 0;
  728. if (offset >= len)
  729. goto drop_pages;
  730. for (; i < nfrags; i++) {
  731. int end = offset + skb_shinfo(skb)->frags[i].size;
  732. if (end < len) {
  733. offset = end;
  734. continue;
  735. }
  736. skb_shinfo(skb)->frags[i++].size = len - offset;
  737. drop_pages:
  738. skb_shinfo(skb)->nr_frags = i;
  739. for (; i < nfrags; i++)
  740. put_page(skb_shinfo(skb)->frags[i].page);
  741. if (skb_shinfo(skb)->frag_list)
  742. skb_drop_fraglist(skb);
  743. goto done;
  744. }
  745. for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
  746. fragp = &frag->next) {
  747. int end = offset + frag->len;
  748. if (skb_shared(frag)) {
  749. struct sk_buff *nfrag;
  750. nfrag = skb_clone(frag, GFP_ATOMIC);
  751. if (unlikely(!nfrag))
  752. return -ENOMEM;
  753. nfrag->next = frag->next;
  754. kfree_skb(frag);
  755. frag = nfrag;
  756. *fragp = frag;
  757. }
  758. if (end < len) {
  759. offset = end;
  760. continue;
  761. }
  762. if (end > len &&
  763. unlikely((err = pskb_trim(frag, len - offset))))
  764. return err;
  765. if (frag->next)
  766. skb_drop_list(&frag->next);
  767. break;
  768. }
  769. done:
  770. if (len > skb_headlen(skb)) {
  771. skb->data_len -= skb->len - len;
  772. skb->len = len;
  773. } else {
  774. skb->len = len;
  775. skb->data_len = 0;
  776. skb_set_tail_pointer(skb, len);
  777. }
  778. return 0;
  779. }
  780. /**
  781. * __pskb_pull_tail - advance tail of skb header
  782. * @skb: buffer to reallocate
  783. * @delta: number of bytes to advance tail
  784. *
  785. * The function makes a sense only on a fragmented &sk_buff,
  786. * it expands header moving its tail forward and copying necessary
  787. * data from fragmented part.
  788. *
  789. * &sk_buff MUST have reference count of 1.
  790. *
  791. * Returns %NULL (and &sk_buff does not change) if pull failed
  792. * or value of new tail of skb in the case of success.
  793. *
  794. * All the pointers pointing into skb header may change and must be
  795. * reloaded after call to this function.
  796. */
  797. /* Moves tail of skb head forward, copying data from fragmented part,
  798. * when it is necessary.
  799. * 1. It may fail due to malloc failure.
  800. * 2. It may change skb pointers.
  801. *
  802. * It is pretty complicated. Luckily, it is called only in exceptional cases.
  803. */
  804. unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
  805. {
  806. /* If skb has not enough free space at tail, get new one
  807. * plus 128 bytes for future expansions. If we have enough
  808. * room at tail, reallocate without expansion only if skb is cloned.
  809. */
  810. int i, k, eat = (skb->tail + delta) - skb->end;
  811. if (eat > 0 || skb_cloned(skb)) {
  812. if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
  813. GFP_ATOMIC))
  814. return NULL;
  815. }
  816. if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
  817. BUG();
  818. /* Optimization: no fragments, no reasons to preestimate
  819. * size of pulled pages. Superb.
  820. */
  821. if (!skb_shinfo(skb)->frag_list)
  822. goto pull_pages;
  823. /* Estimate size of pulled pages. */
  824. eat = delta;
  825. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  826. if (skb_shinfo(skb)->frags[i].size >= eat)
  827. goto pull_pages;
  828. eat -= skb_shinfo(skb)->frags[i].size;
  829. }
  830. /* If we need update frag list, we are in troubles.
  831. * Certainly, it possible to add an offset to skb data,
  832. * but taking into account that pulling is expected to
  833. * be very rare operation, it is worth to fight against
  834. * further bloating skb head and crucify ourselves here instead.
  835. * Pure masohism, indeed. 8)8)
  836. */
  837. if (eat) {
  838. struct sk_buff *list = skb_shinfo(skb)->frag_list;
  839. struct sk_buff *clone = NULL;
  840. struct sk_buff *insp = NULL;
  841. do {
  842. BUG_ON(!list);
  843. if (list->len <= eat) {
  844. /* Eaten as whole. */
  845. eat -= list->len;
  846. list = list->next;
  847. insp = list;
  848. } else {
  849. /* Eaten partially. */
  850. if (skb_shared(list)) {
  851. /* Sucks! We need to fork list. :-( */
  852. clone = skb_clone(list, GFP_ATOMIC);
  853. if (!clone)
  854. return NULL;
  855. insp = list->next;
  856. list = clone;
  857. } else {
  858. /* This may be pulled without
  859. * problems. */
  860. insp = list;
  861. }
  862. if (!pskb_pull(list, eat)) {
  863. if (clone)
  864. kfree_skb(clone);
  865. return NULL;
  866. }
  867. break;
  868. }
  869. } while (eat);
  870. /* Free pulled out fragments. */
  871. while ((list = skb_shinfo(skb)->frag_list) != insp) {
  872. skb_shinfo(skb)->frag_list = list->next;
  873. kfree_skb(list);
  874. }
  875. /* And insert new clone at head. */
  876. if (clone) {
  877. clone->next = list;
  878. skb_shinfo(skb)->frag_list = clone;
  879. }
  880. }
  881. /* Success! Now we may commit changes to skb data. */
  882. pull_pages:
  883. eat = delta;
  884. k = 0;
  885. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  886. if (skb_shinfo(skb)->frags[i].size <= eat) {
  887. put_page(skb_shinfo(skb)->frags[i].page);
  888. eat -= skb_shinfo(skb)->frags[i].size;
  889. } else {
  890. skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
  891. if (eat) {
  892. skb_shinfo(skb)->frags[k].page_offset += eat;
  893. skb_shinfo(skb)->frags[k].size -= eat;
  894. eat = 0;
  895. }
  896. k++;
  897. }
  898. }
  899. skb_shinfo(skb)->nr_frags = k;
  900. skb->tail += delta;
  901. skb->data_len -= delta;
  902. return skb_tail_pointer(skb);
  903. }
  904. /* Copy some data bits from skb to kernel buffer. */
  905. int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
  906. {
  907. int i, copy;
  908. int start = skb_headlen(skb);
  909. if (offset > (int)skb->len - len)
  910. goto fault;
  911. /* Copy header. */
  912. if ((copy = start - offset) > 0) {
  913. if (copy > len)
  914. copy = len;
  915. memcpy(to, skb->data + offset, copy);
  916. if ((len -= copy) == 0)
  917. return 0;
  918. offset += copy;
  919. to += copy;
  920. }
  921. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  922. int end;
  923. BUG_TRAP(start <= offset + len);
  924. end = start + skb_shinfo(skb)->frags[i].size;
  925. if ((copy = end - offset) > 0) {
  926. u8 *vaddr;
  927. if (copy > len)
  928. copy = len;
  929. vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
  930. memcpy(to,
  931. vaddr + skb_shinfo(skb)->frags[i].page_offset+
  932. offset - start, copy);
  933. kunmap_skb_frag(vaddr);
  934. if ((len -= copy) == 0)
  935. return 0;
  936. offset += copy;
  937. to += copy;
  938. }
  939. start = end;
  940. }
  941. if (skb_shinfo(skb)->frag_list) {
  942. struct sk_buff *list = skb_shinfo(skb)->frag_list;
  943. for (; list; list = list->next) {
  944. int end;
  945. BUG_TRAP(start <= offset + len);
  946. end = start + list->len;
  947. if ((copy = end - offset) > 0) {
  948. if (copy > len)
  949. copy = len;
  950. if (skb_copy_bits(list, offset - start,
  951. to, copy))
  952. goto fault;
  953. if ((len -= copy) == 0)
  954. return 0;
  955. offset += copy;
  956. to += copy;
  957. }
  958. start = end;
  959. }
  960. }
  961. if (!len)
  962. return 0;
  963. fault:
  964. return -EFAULT;
  965. }
  966. /**
  967. * skb_store_bits - store bits from kernel buffer to skb
  968. * @skb: destination buffer
  969. * @offset: offset in destination
  970. * @from: source buffer
  971. * @len: number of bytes to copy
  972. *
  973. * Copy the specified number of bytes from the source buffer to the
  974. * destination skb. This function handles all the messy bits of
  975. * traversing fragment lists and such.
  976. */
  977. int skb_store_bits(const struct sk_buff *skb, int offset, void *from, int len)
  978. {
  979. int i, copy;
  980. int start = skb_headlen(skb);
  981. if (offset > (int)skb->len - len)
  982. goto fault;
  983. if ((copy = start - offset) > 0) {
  984. if (copy > len)
  985. copy = len;
  986. memcpy(skb->data + offset, from, copy);
  987. if ((len -= copy) == 0)
  988. return 0;
  989. offset += copy;
  990. from += copy;
  991. }
  992. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  993. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  994. int end;
  995. BUG_TRAP(start <= offset + len);
  996. end = start + frag->size;
  997. if ((copy = end - offset) > 0) {
  998. u8 *vaddr;
  999. if (copy > len)
  1000. copy = len;
  1001. vaddr = kmap_skb_frag(frag);
  1002. memcpy(vaddr + frag->page_offset + offset - start,
  1003. from, copy);
  1004. kunmap_skb_frag(vaddr);
  1005. if ((len -= copy) == 0)
  1006. return 0;
  1007. offset += copy;
  1008. from += copy;
  1009. }
  1010. start = end;
  1011. }
  1012. if (skb_shinfo(skb)->frag_list) {
  1013. struct sk_buff *list = skb_shinfo(skb)->frag_list;
  1014. for (; list; list = list->next) {
  1015. int end;
  1016. BUG_TRAP(start <= offset + len);
  1017. end = start + list->len;
  1018. if ((copy = end - offset) > 0) {
  1019. if (copy > len)
  1020. copy = len;
  1021. if (skb_store_bits(list, offset - start,
  1022. from, copy))
  1023. goto fault;
  1024. if ((len -= copy) == 0)
  1025. return 0;
  1026. offset += copy;
  1027. from += copy;
  1028. }
  1029. start = end;
  1030. }
  1031. }
  1032. if (!len)
  1033. return 0;
  1034. fault:
  1035. return -EFAULT;
  1036. }
  1037. EXPORT_SYMBOL(skb_store_bits);
  1038. /* Checksum skb data. */
  1039. __wsum skb_checksum(const struct sk_buff *skb, int offset,
  1040. int len, __wsum csum)
  1041. {
  1042. int start = skb_headlen(skb);
  1043. int i, copy = start - offset;
  1044. int pos = 0;
  1045. /* Checksum header. */
  1046. if (copy > 0) {
  1047. if (copy > len)
  1048. copy = len;
  1049. csum = csum_partial(skb->data + offset, copy, csum);
  1050. if ((len -= copy) == 0)
  1051. return csum;
  1052. offset += copy;
  1053. pos = copy;
  1054. }
  1055. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1056. int end;
  1057. BUG_TRAP(start <= offset + len);
  1058. end = start + skb_shinfo(skb)->frags[i].size;
  1059. if ((copy = end - offset) > 0) {
  1060. __wsum csum2;
  1061. u8 *vaddr;
  1062. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  1063. if (copy > len)
  1064. copy = len;
  1065. vaddr = kmap_skb_frag(frag);
  1066. csum2 = csum_partial(vaddr + frag->page_offset +
  1067. offset - start, copy, 0);
  1068. kunmap_skb_frag(vaddr);
  1069. csum = csum_block_add(csum, csum2, pos);
  1070. if (!(len -= copy))
  1071. return csum;
  1072. offset += copy;
  1073. pos += copy;
  1074. }
  1075. start = end;
  1076. }
  1077. if (skb_shinfo(skb)->frag_list) {
  1078. struct sk_buff *list = skb_shinfo(skb)->frag_list;
  1079. for (; list; list = list->next) {
  1080. int end;
  1081. BUG_TRAP(start <= offset + len);
  1082. end = start + list->len;
  1083. if ((copy = end - offset) > 0) {
  1084. __wsum csum2;
  1085. if (copy > len)
  1086. copy = len;
  1087. csum2 = skb_checksum(list, offset - start,
  1088. copy, 0);
  1089. csum = csum_block_add(csum, csum2, pos);
  1090. if ((len -= copy) == 0)
  1091. return csum;
  1092. offset += copy;
  1093. pos += copy;
  1094. }
  1095. start = end;
  1096. }
  1097. }
  1098. BUG_ON(len);
  1099. return csum;
  1100. }
  1101. /* Both of above in one bottle. */
  1102. __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
  1103. u8 *to, int len, __wsum csum)
  1104. {
  1105. int start = skb_headlen(skb);
  1106. int i, copy = start - offset;
  1107. int pos = 0;
  1108. /* Copy header. */
  1109. if (copy > 0) {
  1110. if (copy > len)
  1111. copy = len;
  1112. csum = csum_partial_copy_nocheck(skb->data + offset, to,
  1113. copy, csum);
  1114. if ((len -= copy) == 0)
  1115. return csum;
  1116. offset += copy;
  1117. to += copy;
  1118. pos = copy;
  1119. }
  1120. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1121. int end;
  1122. BUG_TRAP(start <= offset + len);
  1123. end = start + skb_shinfo(skb)->frags[i].size;
  1124. if ((copy = end - offset) > 0) {
  1125. __wsum csum2;
  1126. u8 *vaddr;
  1127. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  1128. if (copy > len)
  1129. copy = len;
  1130. vaddr = kmap_skb_frag(frag);
  1131. csum2 = csum_partial_copy_nocheck(vaddr +
  1132. frag->page_offset +
  1133. offset - start, to,
  1134. copy, 0);
  1135. kunmap_skb_frag(vaddr);
  1136. csum = csum_block_add(csum, csum2, pos);
  1137. if (!(len -= copy))
  1138. return csum;
  1139. offset += copy;
  1140. to += copy;
  1141. pos += copy;
  1142. }
  1143. start = end;
  1144. }
  1145. if (skb_shinfo(skb)->frag_list) {
  1146. struct sk_buff *list = skb_shinfo(skb)->frag_list;
  1147. for (; list; list = list->next) {
  1148. __wsum csum2;
  1149. int end;
  1150. BUG_TRAP(start <= offset + len);
  1151. end = start + list->len;
  1152. if ((copy = end - offset) > 0) {
  1153. if (copy > len)
  1154. copy = len;
  1155. csum2 = skb_copy_and_csum_bits(list,
  1156. offset - start,
  1157. to, copy, 0);
  1158. csum = csum_block_add(csum, csum2, pos);
  1159. if ((len -= copy) == 0)
  1160. return csum;
  1161. offset += copy;
  1162. to += copy;
  1163. pos += copy;
  1164. }
  1165. start = end;
  1166. }
  1167. }
  1168. BUG_ON(len);
  1169. return csum;
  1170. }
  1171. void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
  1172. {
  1173. __wsum csum;
  1174. long csstart;
  1175. if (skb->ip_summed == CHECKSUM_PARTIAL)
  1176. csstart = skb_transport_offset(skb);
  1177. else
  1178. csstart = skb_headlen(skb);
  1179. BUG_ON(csstart > skb_headlen(skb));
  1180. memcpy(to, skb->data, csstart);
  1181. csum = 0;
  1182. if (csstart != skb->len)
  1183. csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
  1184. skb->len - csstart, 0);
  1185. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  1186. long csstuff = csstart + skb->csum_offset;
  1187. *((__sum16 *)(to + csstuff)) = csum_fold(csum);
  1188. }
  1189. }
  1190. /**
  1191. * skb_dequeue - remove from the head of the queue
  1192. * @list: list to dequeue from
  1193. *
  1194. * Remove the head of the list. The list lock is taken so the function
  1195. * may be used safely with other locking list functions. The head item is
  1196. * returned or %NULL if the list is empty.
  1197. */
  1198. struct sk_buff *skb_dequeue(struct sk_buff_head *list)
  1199. {
  1200. unsigned long flags;
  1201. struct sk_buff *result;
  1202. spin_lock_irqsave(&list->lock, flags);
  1203. result = __skb_dequeue(list);
  1204. spin_unlock_irqrestore(&list->lock, flags);
  1205. return result;
  1206. }
  1207. /**
  1208. * skb_dequeue_tail - remove from the tail of the queue
  1209. * @list: list to dequeue from
  1210. *
  1211. * Remove the tail of the list. The list lock is taken so the function
  1212. * may be used safely with other locking list functions. The tail item is
  1213. * returned or %NULL if the list is empty.
  1214. */
  1215. struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
  1216. {
  1217. unsigned long flags;
  1218. struct sk_buff *result;
  1219. spin_lock_irqsave(&list->lock, flags);
  1220. result = __skb_dequeue_tail(list);
  1221. spin_unlock_irqrestore(&list->lock, flags);
  1222. return result;
  1223. }
  1224. /**
  1225. * skb_queue_purge - empty a list
  1226. * @list: list to empty
  1227. *
  1228. * Delete all buffers on an &sk_buff list. Each buffer is removed from
  1229. * the list and one reference dropped. This function takes the list
  1230. * lock and is atomic with respect to other list locking functions.
  1231. */
  1232. void skb_queue_purge(struct sk_buff_head *list)
  1233. {
  1234. struct sk_buff *skb;
  1235. while ((skb = skb_dequeue(list)) != NULL)
  1236. kfree_skb(skb);
  1237. }
  1238. /**
  1239. * skb_queue_head - queue a buffer at the list head
  1240. * @list: list to use
  1241. * @newsk: buffer to queue
  1242. *
  1243. * Queue a buffer at the start of the list. This function takes the
  1244. * list lock and can be used safely with other locking &sk_buff functions
  1245. * safely.
  1246. *
  1247. * A buffer cannot be placed on two lists at the same time.
  1248. */
  1249. void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
  1250. {
  1251. unsigned long flags;
  1252. spin_lock_irqsave(&list->lock, flags);
  1253. __skb_queue_head(list, newsk);
  1254. spin_unlock_irqrestore(&list->lock, flags);
  1255. }
  1256. /**
  1257. * skb_queue_tail - queue a buffer at the list tail
  1258. * @list: list to use
  1259. * @newsk: buffer to queue
  1260. *
  1261. * Queue a buffer at the tail of the list. This function takes the
  1262. * list lock and can be used safely with other locking &sk_buff functions
  1263. * safely.
  1264. *
  1265. * A buffer cannot be placed on two lists at the same time.
  1266. */
  1267. void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
  1268. {
  1269. unsigned long flags;
  1270. spin_lock_irqsave(&list->lock, flags);
  1271. __skb_queue_tail(list, newsk);
  1272. spin_unlock_irqrestore(&list->lock, flags);
  1273. }
  1274. /**
  1275. * skb_unlink - remove a buffer from a list
  1276. * @skb: buffer to remove
  1277. * @list: list to use
  1278. *
  1279. * Remove a packet from a list. The list locks are taken and this
  1280. * function is atomic with respect to other list locked calls
  1281. *
  1282. * You must know what list the SKB is on.
  1283. */
  1284. void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
  1285. {
  1286. unsigned long flags;
  1287. spin_lock_irqsave(&list->lock, flags);
  1288. __skb_unlink(skb, list);
  1289. spin_unlock_irqrestore(&list->lock, flags);
  1290. }
  1291. /**
  1292. * skb_append - append a buffer
  1293. * @old: buffer to insert after
  1294. * @newsk: buffer to insert
  1295. * @list: list to use
  1296. *
  1297. * Place a packet after a given packet in a list. The list locks are taken
  1298. * and this function is atomic with respect to other list locked calls.
  1299. * A buffer cannot be placed on two lists at the same time.
  1300. */
  1301. void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
  1302. {
  1303. unsigned long flags;
  1304. spin_lock_irqsave(&list->lock, flags);
  1305. __skb_append(old, newsk, list);
  1306. spin_unlock_irqrestore(&list->lock, flags);
  1307. }
  1308. /**
  1309. * skb_insert - insert a buffer
  1310. * @old: buffer to insert before
  1311. * @newsk: buffer to insert
  1312. * @list: list to use
  1313. *
  1314. * Place a packet before a given packet in a list. The list locks are
  1315. * taken and this function is atomic with respect to other list locked
  1316. * calls.
  1317. *
  1318. * A buffer cannot be placed on two lists at the same time.
  1319. */
  1320. void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
  1321. {
  1322. unsigned long flags;
  1323. spin_lock_irqsave(&list->lock, flags);
  1324. __skb_insert(newsk, old->prev, old, list);
  1325. spin_unlock_irqrestore(&list->lock, flags);
  1326. }
  1327. static inline void skb_split_inside_header(struct sk_buff *skb,
  1328. struct sk_buff* skb1,
  1329. const u32 len, const int pos)
  1330. {
  1331. int i;
  1332. memcpy(skb_put(skb1, pos - len), skb->data + len, pos - len);
  1333. /* And move data appendix as is. */
  1334. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
  1335. skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
  1336. skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
  1337. skb_shinfo(skb)->nr_frags = 0;
  1338. skb1->data_len = skb->data_len;
  1339. skb1->len += skb1->data_len;
  1340. skb->data_len = 0;
  1341. skb->len = len;
  1342. skb_set_tail_pointer(skb, len);
  1343. }
  1344. static inline void skb_split_no_header(struct sk_buff *skb,
  1345. struct sk_buff* skb1,
  1346. const u32 len, int pos)
  1347. {
  1348. int i, k = 0;
  1349. const int nfrags = skb_shinfo(skb)->nr_frags;
  1350. skb_shinfo(skb)->nr_frags = 0;
  1351. skb1->len = skb1->data_len = skb->len - len;
  1352. skb->len = len;
  1353. skb->data_len = len - pos;
  1354. for (i = 0; i < nfrags; i++) {
  1355. int size = skb_shinfo(skb)->frags[i].size;
  1356. if (pos + size > len) {
  1357. skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
  1358. if (pos < len) {
  1359. /* Split frag.
  1360. * We have two variants in this case:
  1361. * 1. Move all the frag to the second
  1362. * part, if it is possible. F.e.
  1363. * this approach is mandatory for TUX,
  1364. * where splitting is expensive.
  1365. * 2. Split is accurately. We make this.
  1366. */
  1367. get_page(skb_shinfo(skb)->frags[i].page);
  1368. skb_shinfo(skb1)->frags[0].page_offset += len - pos;
  1369. skb_shinfo(skb1)->frags[0].size -= len - pos;
  1370. skb_shinfo(skb)->frags[i].size = len - pos;
  1371. skb_shinfo(skb)->nr_frags++;
  1372. }
  1373. k++;
  1374. } else
  1375. skb_shinfo(skb)->nr_frags++;
  1376. pos += size;
  1377. }
  1378. skb_shinfo(skb1)->nr_frags = k;
  1379. }
  1380. /**
  1381. * skb_split - Split fragmented skb to two parts at length len.
  1382. * @skb: the buffer to split
  1383. * @skb1: the buffer to receive the second part
  1384. * @len: new length for skb
  1385. */
  1386. void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
  1387. {
  1388. int pos = skb_headlen(skb);
  1389. if (len < pos) /* Split line is inside header. */
  1390. skb_split_inside_header(skb, skb1, len, pos);
  1391. else /* Second chunk has no header, nothing to copy. */
  1392. skb_split_no_header(skb, skb1, len, pos);
  1393. }
  1394. /**
  1395. * skb_prepare_seq_read - Prepare a sequential read of skb data
  1396. * @skb: the buffer to read
  1397. * @from: lower offset of data to be read
  1398. * @to: upper offset of data to be read
  1399. * @st: state variable
  1400. *
  1401. * Initializes the specified state variable. Must be called before
  1402. * invoking skb_seq_read() for the first time.
  1403. */
  1404. void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
  1405. unsigned int to, struct skb_seq_state *st)
  1406. {
  1407. st->lower_offset = from;
  1408. st->upper_offset = to;
  1409. st->root_skb = st->cur_skb = skb;
  1410. st->frag_idx = st->stepped_offset = 0;
  1411. st->frag_data = NULL;
  1412. }
  1413. /**
  1414. * skb_seq_read - Sequentially read skb data
  1415. * @consumed: number of bytes consumed by the caller so far
  1416. * @data: destination pointer for data to be returned
  1417. * @st: state variable
  1418. *
  1419. * Reads a block of skb data at &consumed relative to the
  1420. * lower offset specified to skb_prepare_seq_read(). Assigns
  1421. * the head of the data block to &data and returns the length
  1422. * of the block or 0 if the end of the skb data or the upper
  1423. * offset has been reached.
  1424. *
  1425. * The caller is not required to consume all of the data
  1426. * returned, i.e. &consumed is typically set to the number
  1427. * of bytes already consumed and the next call to
  1428. * skb_seq_read() will return the remaining part of the block.
  1429. *
  1430. * Note: The size of each block of data returned can be arbitary,
  1431. * this limitation is the cost for zerocopy seqeuental
  1432. * reads of potentially non linear data.
  1433. *
  1434. * Note: Fragment lists within fragments are not implemented
  1435. * at the moment, state->root_skb could be replaced with
  1436. * a stack for this purpose.
  1437. */
  1438. unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
  1439. struct skb_seq_state *st)
  1440. {
  1441. unsigned int block_limit, abs_offset = consumed + st->lower_offset;
  1442. skb_frag_t *frag;
  1443. if (unlikely(abs_offset >= st->upper_offset))
  1444. return 0;
  1445. next_skb:
  1446. block_limit = skb_headlen(st->cur_skb);
  1447. if (abs_offset < block_limit) {
  1448. *data = st->cur_skb->data + abs_offset;
  1449. return block_limit - abs_offset;
  1450. }
  1451. if (st->frag_idx == 0 && !st->frag_data)
  1452. st->stepped_offset += skb_headlen(st->cur_skb);
  1453. while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
  1454. frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
  1455. block_limit = frag->size + st->stepped_offset;
  1456. if (abs_offset < block_limit) {
  1457. if (!st->frag_data)
  1458. st->frag_data = kmap_skb_frag(frag);
  1459. *data = (u8 *) st->frag_data + frag->page_offset +
  1460. (abs_offset - st->stepped_offset);
  1461. return block_limit - abs_offset;
  1462. }
  1463. if (st->frag_data) {
  1464. kunmap_skb_frag(st->frag_data);
  1465. st->frag_data = NULL;
  1466. }
  1467. st->frag_idx++;
  1468. st->stepped_offset += frag->size;
  1469. }
  1470. if (st->cur_skb->next) {
  1471. st->cur_skb = st->cur_skb->next;
  1472. st->frag_idx = 0;
  1473. goto next_skb;
  1474. } else if (st->root_skb == st->cur_skb &&
  1475. skb_shinfo(st->root_skb)->frag_list) {
  1476. st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
  1477. goto next_skb;
  1478. }
  1479. return 0;
  1480. }
  1481. /**
  1482. * skb_abort_seq_read - Abort a sequential read of skb data
  1483. * @st: state variable
  1484. *
  1485. * Must be called if skb_seq_read() was not called until it
  1486. * returned 0.
  1487. */
  1488. void skb_abort_seq_read(struct skb_seq_state *st)
  1489. {
  1490. if (st->frag_data)
  1491. kunmap_skb_frag(st->frag_data);
  1492. }
  1493. #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
  1494. static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
  1495. struct ts_config *conf,
  1496. struct ts_state *state)
  1497. {
  1498. return skb_seq_read(offset, text, TS_SKB_CB(state));
  1499. }
  1500. static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
  1501. {
  1502. skb_abort_seq_read(TS_SKB_CB(state));
  1503. }
  1504. /**
  1505. * skb_find_text - Find a text pattern in skb data
  1506. * @skb: the buffer to look in
  1507. * @from: search offset
  1508. * @to: search limit
  1509. * @config: textsearch configuration
  1510. * @state: uninitialized textsearch state variable
  1511. *
  1512. * Finds a pattern in the skb data according to the specified
  1513. * textsearch configuration. Use textsearch_next() to retrieve
  1514. * subsequent occurrences of the pattern. Returns the offset
  1515. * to the first occurrence or UINT_MAX if no match was found.
  1516. */
  1517. unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
  1518. unsigned int to, struct ts_config *config,
  1519. struct ts_state *state)
  1520. {
  1521. unsigned int ret;
  1522. config->get_next_block = skb_ts_get_next_block;
  1523. config->finish = skb_ts_finish;
  1524. skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
  1525. ret = textsearch_find(config, state);
  1526. return (ret <= to - from ? ret : UINT_MAX);
  1527. }
  1528. /**
  1529. * skb_append_datato_frags: - append the user data to a skb
  1530. * @sk: sock structure
  1531. * @skb: skb structure to be appened with user data.
  1532. * @getfrag: call back function to be used for getting the user data
  1533. * @from: pointer to user message iov
  1534. * @length: length of the iov message
  1535. *
  1536. * Description: This procedure append the user data in the fragment part
  1537. * of the skb if any page alloc fails user this procedure returns -ENOMEM
  1538. */
  1539. int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
  1540. int (*getfrag)(void *from, char *to, int offset,
  1541. int len, int odd, struct sk_buff *skb),
  1542. void *from, int length)
  1543. {
  1544. int frg_cnt = 0;
  1545. skb_frag_t *frag = NULL;
  1546. struct page *page = NULL;
  1547. int copy, left;
  1548. int offset = 0;
  1549. int ret;
  1550. do {
  1551. /* Return error if we don't have space for new frag */
  1552. frg_cnt = skb_shinfo(skb)->nr_frags;
  1553. if (frg_cnt >= MAX_SKB_FRAGS)
  1554. return -EFAULT;
  1555. /* allocate a new page for next frag */
  1556. page = alloc_pages(sk->sk_allocation, 0);
  1557. /* If alloc_page fails just return failure and caller will
  1558. * free previous allocated pages by doing kfree_skb()
  1559. */
  1560. if (page == NULL)
  1561. return -ENOMEM;
  1562. /* initialize the next frag */
  1563. sk->sk_sndmsg_page = page;
  1564. sk->sk_sndmsg_off = 0;
  1565. skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
  1566. skb->truesize += PAGE_SIZE;
  1567. atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
  1568. /* get the new initialized frag */
  1569. frg_cnt = skb_shinfo(skb)->nr_frags;
  1570. frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
  1571. /* copy the user data to page */
  1572. left = PAGE_SIZE - frag->page_offset;
  1573. copy = (length > left)? left : length;
  1574. ret = getfrag(from, (page_address(frag->page) +
  1575. frag->page_offset + frag->size),
  1576. offset, copy, 0, skb);
  1577. if (ret < 0)
  1578. return -EFAULT;
  1579. /* copy was successful so update the size parameters */
  1580. sk->sk_sndmsg_off += copy;
  1581. frag->size += copy;
  1582. skb->len += copy;
  1583. skb->data_len += copy;
  1584. offset += copy;
  1585. length -= copy;
  1586. } while (length > 0);
  1587. return 0;
  1588. }
  1589. /**
  1590. * skb_pull_rcsum - pull skb and update receive checksum
  1591. * @skb: buffer to update
  1592. * @start: start of data before pull
  1593. * @len: length of data pulled
  1594. *
  1595. * This function performs an skb_pull on the packet and updates
  1596. * update the CHECKSUM_COMPLETE checksum. It should be used on
  1597. * receive path processing instead of skb_pull unless you know
  1598. * that the checksum difference is zero (e.g., a valid IP header)
  1599. * or you are setting ip_summed to CHECKSUM_NONE.
  1600. */
  1601. unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
  1602. {
  1603. BUG_ON(len > skb->len);
  1604. skb->len -= len;
  1605. BUG_ON(skb->len < skb->data_len);
  1606. skb_postpull_rcsum(skb, skb->data, len);
  1607. return skb->data += len;
  1608. }
  1609. EXPORT_SYMBOL_GPL(skb_pull_rcsum);
  1610. /**
  1611. * skb_segment - Perform protocol segmentation on skb.
  1612. * @skb: buffer to segment
  1613. * @features: features for the output path (see dev->features)
  1614. *
  1615. * This function performs segmentation on the given skb. It returns
  1616. * the segment at the given position. It returns NULL if there are
  1617. * no more segments to generate, or when an error is encountered.
  1618. */
  1619. struct sk_buff *skb_segment(struct sk_buff *skb, int features)
  1620. {
  1621. struct sk_buff *segs = NULL;
  1622. struct sk_buff *tail = NULL;
  1623. unsigned int mss = skb_shinfo(skb)->gso_size;
  1624. unsigned int doffset = skb->data - skb_mac_header(skb);
  1625. unsigned int offset = doffset;
  1626. unsigned int headroom;
  1627. unsigned int len;
  1628. int sg = features & NETIF_F_SG;
  1629. int nfrags = skb_shinfo(skb)->nr_frags;
  1630. int err = -ENOMEM;
  1631. int i = 0;
  1632. int pos;
  1633. __skb_push(skb, doffset);
  1634. headroom = skb_headroom(skb);
  1635. pos = skb_headlen(skb);
  1636. do {
  1637. struct sk_buff *nskb;
  1638. skb_frag_t *frag;
  1639. int hsize;
  1640. int k;
  1641. int size;
  1642. len = skb->len - offset;
  1643. if (len > mss)
  1644. len = mss;
  1645. hsize = skb_headlen(skb) - offset;
  1646. if (hsize < 0)
  1647. hsize = 0;
  1648. if (hsize > len || !sg)
  1649. hsize = len;
  1650. nskb = alloc_skb(hsize + doffset + headroom, GFP_ATOMIC);
  1651. if (unlikely(!nskb))
  1652. goto err;
  1653. if (segs)
  1654. tail->next = nskb;
  1655. else
  1656. segs = nskb;
  1657. tail = nskb;
  1658. nskb->dev = skb->dev;
  1659. nskb->priority = skb->priority;
  1660. nskb->protocol = skb->protocol;
  1661. nskb->dst = dst_clone(skb->dst);
  1662. memcpy(nskb->cb, skb->cb, sizeof(skb->cb));
  1663. nskb->pkt_type = skb->pkt_type;
  1664. nskb->mac_len = skb->mac_len;
  1665. skb_reserve(nskb, headroom);
  1666. skb_reset_mac_header(nskb);
  1667. skb_set_network_header(nskb, skb->mac_len);
  1668. nskb->transport_header = (nskb->network_header +
  1669. skb_network_header_len(skb));
  1670. memcpy(skb_put(nskb, doffset), skb->data, doffset);
  1671. if (!sg) {
  1672. nskb->csum = skb_copy_and_csum_bits(skb, offset,
  1673. skb_put(nskb, len),
  1674. len, 0);
  1675. continue;
  1676. }
  1677. frag = skb_shinfo(nskb)->frags;
  1678. k = 0;
  1679. nskb->ip_summed = CHECKSUM_PARTIAL;
  1680. nskb->csum = skb->csum;
  1681. memcpy(skb_put(nskb, hsize), skb->data + offset, hsize);
  1682. while (pos < offset + len) {
  1683. BUG_ON(i >= nfrags);
  1684. *frag = skb_shinfo(skb)->frags[i];
  1685. get_page(frag->page);
  1686. size = frag->size;
  1687. if (pos < offset) {
  1688. frag->page_offset += offset - pos;
  1689. frag->size -= offset - pos;
  1690. }
  1691. k++;
  1692. if (pos + size <= offset + len) {
  1693. i++;
  1694. pos += size;
  1695. } else {
  1696. frag->size -= pos + size - (offset + len);
  1697. break;
  1698. }
  1699. frag++;
  1700. }
  1701. skb_shinfo(nskb)->nr_frags = k;
  1702. nskb->data_len = len - hsize;
  1703. nskb->len += nskb->data_len;
  1704. nskb->truesize += nskb->data_len;
  1705. } while ((offset += len) < skb->len);
  1706. return segs;
  1707. err:
  1708. while ((skb = segs)) {
  1709. segs = skb->next;
  1710. kfree_skb(skb);
  1711. }
  1712. return ERR_PTR(err);
  1713. }
  1714. EXPORT_SYMBOL_GPL(skb_segment);
  1715. void __init skb_init(void)
  1716. {
  1717. skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
  1718. sizeof(struct sk_buff),
  1719. 0,
  1720. SLAB_HWCACHE_ALIGN|SLAB_PANIC,
  1721. NULL, NULL);
  1722. skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
  1723. (2*sizeof(struct sk_buff)) +
  1724. sizeof(atomic_t),
  1725. 0,
  1726. SLAB_HWCACHE_ALIGN|SLAB_PANIC,
  1727. NULL, NULL);
  1728. }
  1729. EXPORT_SYMBOL(___pskb_trim);
  1730. EXPORT_SYMBOL(__kfree_skb);
  1731. EXPORT_SYMBOL(kfree_skb);
  1732. EXPORT_SYMBOL(__pskb_pull_tail);
  1733. EXPORT_SYMBOL(__alloc_skb);
  1734. EXPORT_SYMBOL(__netdev_alloc_skb);
  1735. EXPORT_SYMBOL(pskb_copy);
  1736. EXPORT_SYMBOL(pskb_expand_head);
  1737. EXPORT_SYMBOL(skb_checksum);
  1738. EXPORT_SYMBOL(skb_clone);
  1739. EXPORT_SYMBOL(skb_clone_fraglist);
  1740. EXPORT_SYMBOL(skb_copy);
  1741. EXPORT_SYMBOL(skb_copy_and_csum_bits);
  1742. EXPORT_SYMBOL(skb_copy_and_csum_dev);
  1743. EXPORT_SYMBOL(skb_copy_bits);
  1744. EXPORT_SYMBOL(skb_copy_expand);
  1745. EXPORT_SYMBOL(skb_over_panic);
  1746. EXPORT_SYMBOL(skb_pad);
  1747. EXPORT_SYMBOL(skb_realloc_headroom);
  1748. EXPORT_SYMBOL(skb_under_panic);
  1749. EXPORT_SYMBOL(skb_dequeue);
  1750. EXPORT_SYMBOL(skb_dequeue_tail);
  1751. EXPORT_SYMBOL(skb_insert);
  1752. EXPORT_SYMBOL(skb_queue_purge);
  1753. EXPORT_SYMBOL(skb_queue_head);
  1754. EXPORT_SYMBOL(skb_queue_tail);
  1755. EXPORT_SYMBOL(skb_unlink);
  1756. EXPORT_SYMBOL(skb_append);
  1757. EXPORT_SYMBOL(skb_split);
  1758. EXPORT_SYMBOL(skb_prepare_seq_read);
  1759. EXPORT_SYMBOL(skb_seq_read);
  1760. EXPORT_SYMBOL(skb_abort_seq_read);
  1761. EXPORT_SYMBOL(skb_find_text);
  1762. EXPORT_SYMBOL(skb_append_datato_frags);