skbuff.c 50 KB

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