skbuff.c 49 KB

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