skbuff.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200
  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 <linux/scatterlist.h>
  57. #include <net/protocol.h>
  58. #include <net/dst.h>
  59. #include <net/sock.h>
  60. #include <net/checksum.h>
  61. #include <net/xfrm.h>
  62. #include <asm/uaccess.h>
  63. #include <asm/system.h>
  64. #include "kmap_skb.h"
  65. static struct kmem_cache *skbuff_head_cache __read_mostly;
  66. static struct kmem_cache *skbuff_fclone_cache __read_mostly;
  67. /*
  68. * Keep out-of-line to prevent kernel bloat.
  69. * __builtin_return_address is not used because it is not always
  70. * reliable.
  71. */
  72. /**
  73. * skb_over_panic - private function
  74. * @skb: buffer
  75. * @sz: size
  76. * @here: address
  77. *
  78. * Out of line support code for skb_put(). Not user callable.
  79. */
  80. void skb_over_panic(struct sk_buff *skb, int sz, void *here)
  81. {
  82. printk(KERN_EMERG "skb_over_panic: text:%p len:%d put:%d head:%p "
  83. "data:%p tail:%#lx end:%#lx dev:%s\n",
  84. here, skb->len, sz, skb->head, skb->data,
  85. (unsigned long)skb->tail, (unsigned long)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:%#lx end:%#lx dev:%s\n",
  101. here, skb->len, sz, skb->head, skb->data,
  102. (unsigned long)skb->tail, (unsigned long)skb->end,
  103. skb->dev ? skb->dev->name : "<NULL>");
  104. BUG();
  105. }
  106. void skb_truesize_bug(struct sk_buff *skb)
  107. {
  108. printk(KERN_ERR "SKB BUG: Invalid truesize (%u) "
  109. "len=%u, sizeof(sk_buff)=%Zd\n",
  110. skb->truesize, skb->len, sizeof(struct sk_buff));
  111. }
  112. EXPORT_SYMBOL(skb_truesize_bug);
  113. /* Allocate a new skbuff. We do this ourselves so we can fill in a few
  114. * 'private' fields and also do memory statistics to find all the
  115. * [BEEP] leaks.
  116. *
  117. */
  118. /**
  119. * __alloc_skb - allocate a network buffer
  120. * @size: size to allocate
  121. * @gfp_mask: allocation mask
  122. * @fclone: allocate from fclone cache instead of head cache
  123. * and allocate a cloned (child) skb
  124. * @node: numa node to allocate memory on
  125. *
  126. * Allocate a new &sk_buff. The returned buffer has no headroom and a
  127. * tail room of size bytes. The object has a reference count of one.
  128. * The return is the buffer. On a failure the return is %NULL.
  129. *
  130. * Buffers may only be allocated from interrupts using a @gfp_mask of
  131. * %GFP_ATOMIC.
  132. */
  133. struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
  134. int fclone, int node)
  135. {
  136. struct kmem_cache *cache;
  137. struct skb_shared_info *shinfo;
  138. struct sk_buff *skb;
  139. u8 *data;
  140. cache = fclone ? skbuff_fclone_cache : skbuff_head_cache;
  141. /* Get the HEAD */
  142. skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
  143. if (!skb)
  144. goto out;
  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. #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
  291. nf_conntrack_put(skb->nfct);
  292. nf_conntrack_put_reasm(skb->nfct_reasm);
  293. #endif
  294. #ifdef CONFIG_BRIDGE_NETFILTER
  295. nf_bridge_put(skb->nf_bridge);
  296. #endif
  297. /* XXX: IS this still necessary? - JHS */
  298. #ifdef CONFIG_NET_SCHED
  299. skb->tc_index = 0;
  300. #ifdef CONFIG_NET_CLS_ACT
  301. skb->tc_verd = 0;
  302. #endif
  303. #endif
  304. kfree_skbmem(skb);
  305. }
  306. /**
  307. * kfree_skb - free an sk_buff
  308. * @skb: buffer to free
  309. *
  310. * Drop a reference to the buffer and free it if the usage count has
  311. * hit zero.
  312. */
  313. void kfree_skb(struct sk_buff *skb)
  314. {
  315. if (unlikely(!skb))
  316. return;
  317. if (likely(atomic_read(&skb->users) == 1))
  318. smp_rmb();
  319. else if (likely(!atomic_dec_and_test(&skb->users)))
  320. return;
  321. __kfree_skb(skb);
  322. }
  323. /**
  324. * skb_clone - duplicate an sk_buff
  325. * @skb: buffer to clone
  326. * @gfp_mask: allocation priority
  327. *
  328. * Duplicate an &sk_buff. The new one is not owned by a socket. Both
  329. * copies share the same packet data but not structure. The new
  330. * buffer has a reference count of 1. If the allocation fails the
  331. * function returns %NULL otherwise the new buffer is returned.
  332. *
  333. * If this function is called from an interrupt gfp_mask() must be
  334. * %GFP_ATOMIC.
  335. */
  336. struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
  337. {
  338. struct sk_buff *n;
  339. n = skb + 1;
  340. if (skb->fclone == SKB_FCLONE_ORIG &&
  341. n->fclone == SKB_FCLONE_UNAVAILABLE) {
  342. atomic_t *fclone_ref = (atomic_t *) (n + 1);
  343. n->fclone = SKB_FCLONE_CLONE;
  344. atomic_inc(fclone_ref);
  345. } else {
  346. n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
  347. if (!n)
  348. return NULL;
  349. n->fclone = SKB_FCLONE_UNAVAILABLE;
  350. }
  351. #define C(x) n->x = skb->x
  352. n->next = n->prev = NULL;
  353. n->sk = NULL;
  354. C(tstamp);
  355. C(dev);
  356. C(transport_header);
  357. C(network_header);
  358. C(mac_header);
  359. C(dst);
  360. dst_clone(skb->dst);
  361. C(sp);
  362. #ifdef CONFIG_INET
  363. secpath_get(skb->sp);
  364. #endif
  365. memcpy(n->cb, skb->cb, sizeof(skb->cb));
  366. C(len);
  367. C(data_len);
  368. C(mac_len);
  369. C(csum);
  370. C(local_df);
  371. n->cloned = 1;
  372. n->nohdr = 0;
  373. C(pkt_type);
  374. C(ip_summed);
  375. C(priority);
  376. #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
  377. C(ipvs_property);
  378. #endif
  379. C(protocol);
  380. n->destructor = NULL;
  381. C(mark);
  382. __nf_copy(n, skb);
  383. #ifdef CONFIG_NET_SCHED
  384. C(tc_index);
  385. #ifdef CONFIG_NET_CLS_ACT
  386. n->tc_verd = SET_TC_VERD(skb->tc_verd,0);
  387. n->tc_verd = CLR_TC_OK2MUNGE(n->tc_verd);
  388. n->tc_verd = CLR_TC_MUNGED(n->tc_verd);
  389. C(iif);
  390. #endif
  391. skb_copy_secmark(n, skb);
  392. #endif
  393. C(truesize);
  394. atomic_set(&n->users, 1);
  395. C(head);
  396. C(data);
  397. C(tail);
  398. C(end);
  399. atomic_inc(&(skb_shinfo(skb)->dataref));
  400. skb->cloned = 1;
  401. return n;
  402. }
  403. static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
  404. {
  405. #ifndef NET_SKBUFF_DATA_USES_OFFSET
  406. /*
  407. * Shift between the two data areas in bytes
  408. */
  409. unsigned long offset = new->data - old->data;
  410. #endif
  411. new->sk = NULL;
  412. new->dev = old->dev;
  413. new->priority = old->priority;
  414. new->protocol = old->protocol;
  415. new->dst = dst_clone(old->dst);
  416. #ifdef CONFIG_INET
  417. new->sp = secpath_get(old->sp);
  418. #endif
  419. new->transport_header = old->transport_header;
  420. new->network_header = old->network_header;
  421. new->mac_header = old->mac_header;
  422. #ifndef NET_SKBUFF_DATA_USES_OFFSET
  423. /* {transport,network,mac}_header are relative to skb->head */
  424. new->transport_header += offset;
  425. new->network_header += offset;
  426. new->mac_header += offset;
  427. #endif
  428. memcpy(new->cb, old->cb, sizeof(old->cb));
  429. new->local_df = old->local_df;
  430. new->fclone = SKB_FCLONE_UNAVAILABLE;
  431. new->pkt_type = old->pkt_type;
  432. new->tstamp = old->tstamp;
  433. new->destructor = NULL;
  434. new->mark = old->mark;
  435. __nf_copy(new, old);
  436. #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
  437. new->ipvs_property = old->ipvs_property;
  438. #endif
  439. #ifdef CONFIG_NET_SCHED
  440. #ifdef CONFIG_NET_CLS_ACT
  441. new->tc_verd = old->tc_verd;
  442. #endif
  443. new->tc_index = old->tc_index;
  444. #endif
  445. skb_copy_secmark(new, old);
  446. atomic_set(&new->users, 1);
  447. skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
  448. skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
  449. skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
  450. }
  451. /**
  452. * skb_copy - create private copy of an sk_buff
  453. * @skb: buffer to copy
  454. * @gfp_mask: allocation priority
  455. *
  456. * Make a copy of both an &sk_buff and its data. This is used when the
  457. * caller wishes to modify the data and needs a private copy of the
  458. * data to alter. Returns %NULL on failure or the pointer to the buffer
  459. * on success. The returned buffer has a reference count of 1.
  460. *
  461. * As by-product this function converts non-linear &sk_buff to linear
  462. * one, so that &sk_buff becomes completely private and caller is allowed
  463. * to modify all the data of returned buffer. This means that this
  464. * function is not recommended for use in circumstances when only
  465. * header is going to be modified. Use pskb_copy() instead.
  466. */
  467. struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
  468. {
  469. int headerlen = skb->data - skb->head;
  470. /*
  471. * Allocate the copy buffer
  472. */
  473. struct sk_buff *n;
  474. #ifdef NET_SKBUFF_DATA_USES_OFFSET
  475. n = alloc_skb(skb->end + skb->data_len, gfp_mask);
  476. #else
  477. n = alloc_skb(skb->end - skb->head + skb->data_len, gfp_mask);
  478. #endif
  479. if (!n)
  480. return NULL;
  481. /* Set the data pointer */
  482. skb_reserve(n, headerlen);
  483. /* Set the tail pointer and length */
  484. skb_put(n, skb->len);
  485. n->csum = skb->csum;
  486. n->ip_summed = skb->ip_summed;
  487. if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
  488. BUG();
  489. copy_skb_header(n, skb);
  490. return n;
  491. }
  492. /**
  493. * pskb_copy - create copy of an sk_buff with private head.
  494. * @skb: buffer to copy
  495. * @gfp_mask: allocation priority
  496. *
  497. * Make a copy of both an &sk_buff and part of its data, located
  498. * in header. Fragmented data remain shared. This is used when
  499. * the caller wishes to modify only header of &sk_buff and needs
  500. * private copy of the header to alter. Returns %NULL on failure
  501. * or the pointer to the buffer on success.
  502. * The returned buffer has a reference count of 1.
  503. */
  504. struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask)
  505. {
  506. /*
  507. * Allocate the copy buffer
  508. */
  509. struct sk_buff *n;
  510. #ifdef NET_SKBUFF_DATA_USES_OFFSET
  511. n = alloc_skb(skb->end, gfp_mask);
  512. #else
  513. n = alloc_skb(skb->end - skb->head, gfp_mask);
  514. #endif
  515. if (!n)
  516. goto out;
  517. /* Set the data pointer */
  518. skb_reserve(n, skb->data - skb->head);
  519. /* Set the tail pointer and length */
  520. skb_put(n, skb_headlen(skb));
  521. /* Copy the bytes */
  522. skb_copy_from_linear_data(skb, n->data, n->len);
  523. n->csum = skb->csum;
  524. n->ip_summed = skb->ip_summed;
  525. n->truesize += skb->data_len;
  526. n->data_len = skb->data_len;
  527. n->len = skb->len;
  528. if (skb_shinfo(skb)->nr_frags) {
  529. int i;
  530. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  531. skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
  532. get_page(skb_shinfo(n)->frags[i].page);
  533. }
  534. skb_shinfo(n)->nr_frags = i;
  535. }
  536. if (skb_shinfo(skb)->frag_list) {
  537. skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
  538. skb_clone_fraglist(n);
  539. }
  540. copy_skb_header(n, skb);
  541. out:
  542. return n;
  543. }
  544. /**
  545. * pskb_expand_head - reallocate header of &sk_buff
  546. * @skb: buffer to reallocate
  547. * @nhead: room to add at head
  548. * @ntail: room to add at tail
  549. * @gfp_mask: allocation priority
  550. *
  551. * Expands (or creates identical copy, if &nhead and &ntail are zero)
  552. * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
  553. * reference count of 1. Returns zero in the case of success or error,
  554. * if expansion failed. In the last case, &sk_buff is not changed.
  555. *
  556. * All the pointers pointing into skb header may change and must be
  557. * reloaded after call to this function.
  558. */
  559. int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
  560. gfp_t gfp_mask)
  561. {
  562. int i;
  563. u8 *data;
  564. #ifdef NET_SKBUFF_DATA_USES_OFFSET
  565. int size = nhead + skb->end + ntail;
  566. #else
  567. int size = nhead + (skb->end - skb->head) + ntail;
  568. #endif
  569. long off;
  570. if (skb_shared(skb))
  571. BUG();
  572. size = SKB_DATA_ALIGN(size);
  573. data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
  574. if (!data)
  575. goto nodata;
  576. /* Copy only real data... and, alas, header. This should be
  577. * optimized for the cases when header is void. */
  578. memcpy(data + nhead, skb->head,
  579. #ifdef NET_SKBUFF_DATA_USES_OFFSET
  580. skb->tail);
  581. #else
  582. skb->tail - skb->head);
  583. #endif
  584. memcpy(data + size, skb_end_pointer(skb),
  585. sizeof(struct skb_shared_info));
  586. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
  587. get_page(skb_shinfo(skb)->frags[i].page);
  588. if (skb_shinfo(skb)->frag_list)
  589. skb_clone_fraglist(skb);
  590. skb_release_data(skb);
  591. off = (data + nhead) - skb->head;
  592. skb->head = data;
  593. skb->data += off;
  594. #ifdef NET_SKBUFF_DATA_USES_OFFSET
  595. skb->end = size;
  596. off = nhead;
  597. #else
  598. skb->end = skb->head + size;
  599. #endif
  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. skb->cloned = 0;
  606. skb->nohdr = 0;
  607. atomic_set(&skb_shinfo(skb)->dataref, 1);
  608. return 0;
  609. nodata:
  610. return -ENOMEM;
  611. }
  612. /* Make private copy of skb with writable head and some headroom */
  613. struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
  614. {
  615. struct sk_buff *skb2;
  616. int delta = headroom - skb_headroom(skb);
  617. if (delta <= 0)
  618. skb2 = pskb_copy(skb, GFP_ATOMIC);
  619. else {
  620. skb2 = skb_clone(skb, GFP_ATOMIC);
  621. if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
  622. GFP_ATOMIC)) {
  623. kfree_skb(skb2);
  624. skb2 = NULL;
  625. }
  626. }
  627. return skb2;
  628. }
  629. /**
  630. * skb_copy_expand - copy and expand sk_buff
  631. * @skb: buffer to copy
  632. * @newheadroom: new free bytes at head
  633. * @newtailroom: new free bytes at tail
  634. * @gfp_mask: allocation priority
  635. *
  636. * Make a copy of both an &sk_buff and its data and while doing so
  637. * allocate additional space.
  638. *
  639. * This is used when the caller wishes to modify the data and needs a
  640. * private copy of the data to alter as well as more space for new fields.
  641. * Returns %NULL on failure or the pointer to the buffer
  642. * on success. The returned buffer has a reference count of 1.
  643. *
  644. * You must pass %GFP_ATOMIC as the allocation priority if this function
  645. * is called from an interrupt.
  646. *
  647. * BUG ALERT: ip_summed is not copied. Why does this work? Is it used
  648. * only by netfilter in the cases when checksum is recalculated? --ANK
  649. */
  650. struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
  651. int newheadroom, int newtailroom,
  652. gfp_t gfp_mask)
  653. {
  654. /*
  655. * Allocate the copy buffer
  656. */
  657. struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
  658. gfp_mask);
  659. int oldheadroom = skb_headroom(skb);
  660. int head_copy_len, head_copy_off;
  661. int off = 0;
  662. if (!n)
  663. return NULL;
  664. skb_reserve(n, newheadroom);
  665. /* Set the tail pointer and length */
  666. skb_put(n, skb->len);
  667. head_copy_len = oldheadroom;
  668. head_copy_off = 0;
  669. if (newheadroom <= head_copy_len)
  670. head_copy_len = newheadroom;
  671. else
  672. head_copy_off = newheadroom - head_copy_len;
  673. /* Copy the linear header and data. */
  674. if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
  675. skb->len + head_copy_len))
  676. BUG();
  677. copy_skb_header(n, skb);
  678. #ifdef NET_SKBUFF_DATA_USES_OFFSET
  679. off = newheadroom - oldheadroom;
  680. #endif
  681. n->transport_header += off;
  682. n->network_header += off;
  683. n->mac_header += off;
  684. return n;
  685. }
  686. /**
  687. * skb_pad - zero pad the tail of an skb
  688. * @skb: buffer to pad
  689. * @pad: space to pad
  690. *
  691. * Ensure that a buffer is followed by a padding area that is zero
  692. * filled. Used by network drivers which may DMA or transfer data
  693. * beyond the buffer end onto the wire.
  694. *
  695. * May return error in out of memory cases. The skb is freed on error.
  696. */
  697. int skb_pad(struct sk_buff *skb, int pad)
  698. {
  699. int err;
  700. int ntail;
  701. /* If the skbuff is non linear tailroom is always zero.. */
  702. if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
  703. memset(skb->data+skb->len, 0, pad);
  704. return 0;
  705. }
  706. ntail = skb->data_len + pad - (skb->end - skb->tail);
  707. if (likely(skb_cloned(skb) || ntail > 0)) {
  708. err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
  709. if (unlikely(err))
  710. goto free_skb;
  711. }
  712. /* FIXME: The use of this function with non-linear skb's really needs
  713. * to be audited.
  714. */
  715. err = skb_linearize(skb);
  716. if (unlikely(err))
  717. goto free_skb;
  718. memset(skb->data + skb->len, 0, pad);
  719. return 0;
  720. free_skb:
  721. kfree_skb(skb);
  722. return err;
  723. }
  724. /* Trims skb to length len. It can change skb pointers.
  725. */
  726. int ___pskb_trim(struct sk_buff *skb, unsigned int len)
  727. {
  728. struct sk_buff **fragp;
  729. struct sk_buff *frag;
  730. int offset = skb_headlen(skb);
  731. int nfrags = skb_shinfo(skb)->nr_frags;
  732. int i;
  733. int err;
  734. if (skb_cloned(skb) &&
  735. unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
  736. return err;
  737. i = 0;
  738. if (offset >= len)
  739. goto drop_pages;
  740. for (; i < nfrags; i++) {
  741. int end = offset + skb_shinfo(skb)->frags[i].size;
  742. if (end < len) {
  743. offset = end;
  744. continue;
  745. }
  746. skb_shinfo(skb)->frags[i++].size = len - offset;
  747. drop_pages:
  748. skb_shinfo(skb)->nr_frags = i;
  749. for (; i < nfrags; i++)
  750. put_page(skb_shinfo(skb)->frags[i].page);
  751. if (skb_shinfo(skb)->frag_list)
  752. skb_drop_fraglist(skb);
  753. goto done;
  754. }
  755. for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
  756. fragp = &frag->next) {
  757. int end = offset + frag->len;
  758. if (skb_shared(frag)) {
  759. struct sk_buff *nfrag;
  760. nfrag = skb_clone(frag, GFP_ATOMIC);
  761. if (unlikely(!nfrag))
  762. return -ENOMEM;
  763. nfrag->next = frag->next;
  764. kfree_skb(frag);
  765. frag = nfrag;
  766. *fragp = frag;
  767. }
  768. if (end < len) {
  769. offset = end;
  770. continue;
  771. }
  772. if (end > len &&
  773. unlikely((err = pskb_trim(frag, len - offset))))
  774. return err;
  775. if (frag->next)
  776. skb_drop_list(&frag->next);
  777. break;
  778. }
  779. done:
  780. if (len > skb_headlen(skb)) {
  781. skb->data_len -= skb->len - len;
  782. skb->len = len;
  783. } else {
  784. skb->len = len;
  785. skb->data_len = 0;
  786. skb_set_tail_pointer(skb, len);
  787. }
  788. return 0;
  789. }
  790. /**
  791. * __pskb_pull_tail - advance tail of skb header
  792. * @skb: buffer to reallocate
  793. * @delta: number of bytes to advance tail
  794. *
  795. * The function makes a sense only on a fragmented &sk_buff,
  796. * it expands header moving its tail forward and copying necessary
  797. * data from fragmented part.
  798. *
  799. * &sk_buff MUST have reference count of 1.
  800. *
  801. * Returns %NULL (and &sk_buff does not change) if pull failed
  802. * or value of new tail of skb in the case of success.
  803. *
  804. * All the pointers pointing into skb header may change and must be
  805. * reloaded after call to this function.
  806. */
  807. /* Moves tail of skb head forward, copying data from fragmented part,
  808. * when it is necessary.
  809. * 1. It may fail due to malloc failure.
  810. * 2. It may change skb pointers.
  811. *
  812. * It is pretty complicated. Luckily, it is called only in exceptional cases.
  813. */
  814. unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
  815. {
  816. /* If skb has not enough free space at tail, get new one
  817. * plus 128 bytes for future expansions. If we have enough
  818. * room at tail, reallocate without expansion only if skb is cloned.
  819. */
  820. int i, k, eat = (skb->tail + delta) - skb->end;
  821. if (eat > 0 || skb_cloned(skb)) {
  822. if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
  823. GFP_ATOMIC))
  824. return NULL;
  825. }
  826. if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
  827. BUG();
  828. /* Optimization: no fragments, no reasons to preestimate
  829. * size of pulled pages. Superb.
  830. */
  831. if (!skb_shinfo(skb)->frag_list)
  832. goto pull_pages;
  833. /* Estimate size of pulled pages. */
  834. eat = delta;
  835. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  836. if (skb_shinfo(skb)->frags[i].size >= eat)
  837. goto pull_pages;
  838. eat -= skb_shinfo(skb)->frags[i].size;
  839. }
  840. /* If we need update frag list, we are in troubles.
  841. * Certainly, it possible to add an offset to skb data,
  842. * but taking into account that pulling is expected to
  843. * be very rare operation, it is worth to fight against
  844. * further bloating skb head and crucify ourselves here instead.
  845. * Pure masohism, indeed. 8)8)
  846. */
  847. if (eat) {
  848. struct sk_buff *list = skb_shinfo(skb)->frag_list;
  849. struct sk_buff *clone = NULL;
  850. struct sk_buff *insp = NULL;
  851. do {
  852. BUG_ON(!list);
  853. if (list->len <= eat) {
  854. /* Eaten as whole. */
  855. eat -= list->len;
  856. list = list->next;
  857. insp = list;
  858. } else {
  859. /* Eaten partially. */
  860. if (skb_shared(list)) {
  861. /* Sucks! We need to fork list. :-( */
  862. clone = skb_clone(list, GFP_ATOMIC);
  863. if (!clone)
  864. return NULL;
  865. insp = list->next;
  866. list = clone;
  867. } else {
  868. /* This may be pulled without
  869. * problems. */
  870. insp = list;
  871. }
  872. if (!pskb_pull(list, eat)) {
  873. if (clone)
  874. kfree_skb(clone);
  875. return NULL;
  876. }
  877. break;
  878. }
  879. } while (eat);
  880. /* Free pulled out fragments. */
  881. while ((list = skb_shinfo(skb)->frag_list) != insp) {
  882. skb_shinfo(skb)->frag_list = list->next;
  883. kfree_skb(list);
  884. }
  885. /* And insert new clone at head. */
  886. if (clone) {
  887. clone->next = list;
  888. skb_shinfo(skb)->frag_list = clone;
  889. }
  890. }
  891. /* Success! Now we may commit changes to skb data. */
  892. pull_pages:
  893. eat = delta;
  894. k = 0;
  895. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  896. if (skb_shinfo(skb)->frags[i].size <= eat) {
  897. put_page(skb_shinfo(skb)->frags[i].page);
  898. eat -= skb_shinfo(skb)->frags[i].size;
  899. } else {
  900. skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
  901. if (eat) {
  902. skb_shinfo(skb)->frags[k].page_offset += eat;
  903. skb_shinfo(skb)->frags[k].size -= eat;
  904. eat = 0;
  905. }
  906. k++;
  907. }
  908. }
  909. skb_shinfo(skb)->nr_frags = k;
  910. skb->tail += delta;
  911. skb->data_len -= delta;
  912. return skb_tail_pointer(skb);
  913. }
  914. /* Copy some data bits from skb to kernel buffer. */
  915. int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
  916. {
  917. int i, copy;
  918. int end = skb_headlen(skb);
  919. if (offset > (int)skb->len - len)
  920. goto fault;
  921. /* Copy header. */
  922. if ((copy = end - offset) > 0) {
  923. if (copy > len)
  924. copy = len;
  925. skb_copy_from_linear_data_offset(skb, offset, to, copy);
  926. if ((len -= copy) == 0)
  927. return 0;
  928. offset += copy;
  929. to += copy;
  930. }
  931. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  932. BUG_TRAP(len >= 0);
  933. end = offset + skb_shinfo(skb)->frags[i].size;
  934. if ((copy = end - offset) > 0) {
  935. u8 *vaddr;
  936. if (copy > len)
  937. copy = len;
  938. vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
  939. memcpy(to,
  940. vaddr + skb_shinfo(skb)->frags[i].page_offset,
  941. copy);
  942. kunmap_skb_frag(vaddr);
  943. if ((len -= copy) == 0)
  944. return 0;
  945. offset += copy;
  946. to += copy;
  947. }
  948. }
  949. if (skb_shinfo(skb)->frag_list) {
  950. struct sk_buff *list = skb_shinfo(skb)->frag_list;
  951. for (; list; list = list->next) {
  952. BUG_TRAP(len >= 0);
  953. end = offset + list->len;
  954. if ((copy = end - offset) > 0) {
  955. if (copy > len)
  956. copy = len;
  957. if (skb_copy_bits(list, 0, to, copy))
  958. goto fault;
  959. if ((len -= copy) == 0)
  960. return 0;
  961. offset += copy;
  962. to += copy;
  963. }
  964. }
  965. }
  966. if (!len)
  967. return 0;
  968. fault:
  969. return -EFAULT;
  970. }
  971. /**
  972. * skb_store_bits - store bits from kernel buffer to skb
  973. * @skb: destination buffer
  974. * @offset: offset in destination
  975. * @from: source buffer
  976. * @len: number of bytes to copy
  977. *
  978. * Copy the specified number of bytes from the source buffer to the
  979. * destination skb. This function handles all the messy bits of
  980. * traversing fragment lists and such.
  981. */
  982. int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
  983. {
  984. int i, copy;
  985. int end = skb_headlen(skb);
  986. if (offset > (int)skb->len - len)
  987. goto fault;
  988. if ((copy = end - offset) > 0) {
  989. if (copy > len)
  990. copy = len;
  991. skb_copy_to_linear_data_offset(skb, offset, from, copy);
  992. if ((len -= copy) == 0)
  993. return 0;
  994. offset += copy;
  995. from += copy;
  996. }
  997. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  998. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  999. BUG_TRAP(len >= 0);
  1000. end = offset + frag->size;
  1001. if ((copy = end - offset) > 0) {
  1002. u8 *vaddr;
  1003. if (copy > len)
  1004. copy = len;
  1005. vaddr = kmap_skb_frag(frag);
  1006. memcpy(vaddr + frag->page_offset, from, copy);
  1007. kunmap_skb_frag(vaddr);
  1008. if ((len -= copy) == 0)
  1009. return 0;
  1010. offset += copy;
  1011. from += copy;
  1012. }
  1013. }
  1014. if (skb_shinfo(skb)->frag_list) {
  1015. struct sk_buff *list = skb_shinfo(skb)->frag_list;
  1016. for (; list; list = list->next) {
  1017. BUG_TRAP(len >= 0);
  1018. end = offset + list->len;
  1019. if ((copy = end - offset) > 0) {
  1020. if (copy > len)
  1021. copy = len;
  1022. if (skb_store_bits(list, 0, from, copy))
  1023. goto fault;
  1024. if ((len -= copy) == 0)
  1025. return 0;
  1026. offset += copy;
  1027. from += copy;
  1028. }
  1029. }
  1030. }
  1031. if (!len)
  1032. return 0;
  1033. fault:
  1034. return -EFAULT;
  1035. }
  1036. EXPORT_SYMBOL(skb_store_bits);
  1037. /* Checksum skb data. */
  1038. __wsum skb_checksum(const struct sk_buff *skb, int offset,
  1039. int len, __wsum csum)
  1040. {
  1041. int end = skb_headlen(skb);
  1042. int i, copy = end - offset;
  1043. int pos = 0;
  1044. /* Checksum header. */
  1045. if (copy > 0) {
  1046. if (copy > len)
  1047. copy = len;
  1048. csum = csum_partial(skb->data + offset, copy, csum);
  1049. if ((len -= copy) == 0)
  1050. return csum;
  1051. offset += copy;
  1052. pos = copy;
  1053. }
  1054. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1055. BUG_TRAP(len >= 0);
  1056. end = offset + skb_shinfo(skb)->frags[i].size;
  1057. if ((copy = end - offset) > 0) {
  1058. __wsum csum2;
  1059. u8 *vaddr;
  1060. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  1061. if (copy > len)
  1062. copy = len;
  1063. vaddr = kmap_skb_frag(frag);
  1064. csum2 = csum_partial(vaddr + frag->page_offset,
  1065. copy, 0);
  1066. kunmap_skb_frag(vaddr);
  1067. csum = csum_block_add(csum, csum2, pos);
  1068. if (!(len -= copy))
  1069. return csum;
  1070. offset += copy;
  1071. pos += copy;
  1072. }
  1073. }
  1074. if (skb_shinfo(skb)->frag_list) {
  1075. struct sk_buff *list = skb_shinfo(skb)->frag_list;
  1076. for (; list; list = list->next) {
  1077. BUG_TRAP(len >= 0);
  1078. end = offset + list->len;
  1079. if ((copy = end - offset) > 0) {
  1080. __wsum csum2;
  1081. if (copy > len)
  1082. copy = len;
  1083. csum2 = skb_checksum(list, 0, copy, 0);
  1084. csum = csum_block_add(csum, csum2, pos);
  1085. if ((len -= copy) == 0)
  1086. return csum;
  1087. offset += copy;
  1088. pos += copy;
  1089. }
  1090. }
  1091. }
  1092. BUG_ON(len);
  1093. return csum;
  1094. }
  1095. /* Both of above in one bottle. */
  1096. __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
  1097. u8 *to, int len, __wsum csum)
  1098. {
  1099. int end = skb_headlen(skb);
  1100. int i, copy = end - offset;
  1101. int pos = 0;
  1102. /* Copy header. */
  1103. if (copy > 0) {
  1104. if (copy > len)
  1105. copy = len;
  1106. csum = csum_partial_copy_nocheck(skb->data + offset, to,
  1107. copy, csum);
  1108. if ((len -= copy) == 0)
  1109. return csum;
  1110. offset += copy;
  1111. to += copy;
  1112. pos = copy;
  1113. }
  1114. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1115. BUG_TRAP(len >= 0);
  1116. end = offset + skb_shinfo(skb)->frags[i].size;
  1117. if ((copy = end - offset) > 0) {
  1118. __wsum csum2;
  1119. u8 *vaddr;
  1120. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  1121. if (copy > len)
  1122. copy = len;
  1123. vaddr = kmap_skb_frag(frag);
  1124. csum2 = csum_partial_copy_nocheck(vaddr +
  1125. frag->page_offset,
  1126. to, copy, 0);
  1127. kunmap_skb_frag(vaddr);
  1128. csum = csum_block_add(csum, csum2, pos);
  1129. if (!(len -= copy))
  1130. return csum;
  1131. offset += copy;
  1132. to += copy;
  1133. pos += copy;
  1134. }
  1135. }
  1136. if (skb_shinfo(skb)->frag_list) {
  1137. struct sk_buff *list = skb_shinfo(skb)->frag_list;
  1138. for (; list; list = list->next) {
  1139. __wsum csum2;
  1140. BUG_TRAP(len >= 0);
  1141. end = offset + list->len;
  1142. if ((copy = end - offset) > 0) {
  1143. if (copy > len)
  1144. copy = len;
  1145. csum2 = skb_copy_and_csum_bits(list, 0,
  1146. to, copy, 0);
  1147. csum = csum_block_add(csum, csum2, pos);
  1148. if ((len -= copy) == 0)
  1149. return csum;
  1150. offset += copy;
  1151. to += copy;
  1152. pos += copy;
  1153. }
  1154. }
  1155. }
  1156. BUG_ON(len);
  1157. return csum;
  1158. }
  1159. void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
  1160. {
  1161. __wsum csum;
  1162. long csstart;
  1163. if (skb->ip_summed == CHECKSUM_PARTIAL)
  1164. csstart = skb->csum_start - skb_headroom(skb);
  1165. else
  1166. csstart = skb_headlen(skb);
  1167. BUG_ON(csstart > skb_headlen(skb));
  1168. skb_copy_from_linear_data(skb, to, csstart);
  1169. csum = 0;
  1170. if (csstart != skb->len)
  1171. csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
  1172. skb->len - csstart, 0);
  1173. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  1174. long csstuff = csstart + skb->csum_offset;
  1175. *((__sum16 *)(to + csstuff)) = csum_fold(csum);
  1176. }
  1177. }
  1178. /**
  1179. * skb_dequeue - remove from the head of the queue
  1180. * @list: list to dequeue from
  1181. *
  1182. * Remove the head of the list. The list lock is taken so the function
  1183. * may be used safely with other locking list functions. The head item is
  1184. * returned or %NULL if the list is empty.
  1185. */
  1186. struct sk_buff *skb_dequeue(struct sk_buff_head *list)
  1187. {
  1188. unsigned long flags;
  1189. struct sk_buff *result;
  1190. spin_lock_irqsave(&list->lock, flags);
  1191. result = __skb_dequeue(list);
  1192. spin_unlock_irqrestore(&list->lock, flags);
  1193. return result;
  1194. }
  1195. /**
  1196. * skb_dequeue_tail - remove from the tail of the queue
  1197. * @list: list to dequeue from
  1198. *
  1199. * Remove the tail of the list. The list lock is taken so the function
  1200. * may be used safely with other locking list functions. The tail item is
  1201. * returned or %NULL if the list is empty.
  1202. */
  1203. struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
  1204. {
  1205. unsigned long flags;
  1206. struct sk_buff *result;
  1207. spin_lock_irqsave(&list->lock, flags);
  1208. result = __skb_dequeue_tail(list);
  1209. spin_unlock_irqrestore(&list->lock, flags);
  1210. return result;
  1211. }
  1212. /**
  1213. * skb_queue_purge - empty a list
  1214. * @list: list to empty
  1215. *
  1216. * Delete all buffers on an &sk_buff list. Each buffer is removed from
  1217. * the list and one reference dropped. This function takes the list
  1218. * lock and is atomic with respect to other list locking functions.
  1219. */
  1220. void skb_queue_purge(struct sk_buff_head *list)
  1221. {
  1222. struct sk_buff *skb;
  1223. while ((skb = skb_dequeue(list)) != NULL)
  1224. kfree_skb(skb);
  1225. }
  1226. /**
  1227. * skb_queue_head - queue a buffer at the list head
  1228. * @list: list to use
  1229. * @newsk: buffer to queue
  1230. *
  1231. * Queue a buffer at the start of the list. This function takes the
  1232. * list lock and can be used safely with other locking &sk_buff functions
  1233. * safely.
  1234. *
  1235. * A buffer cannot be placed on two lists at the same time.
  1236. */
  1237. void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
  1238. {
  1239. unsigned long flags;
  1240. spin_lock_irqsave(&list->lock, flags);
  1241. __skb_queue_head(list, newsk);
  1242. spin_unlock_irqrestore(&list->lock, flags);
  1243. }
  1244. /**
  1245. * skb_queue_tail - queue a buffer at the list tail
  1246. * @list: list to use
  1247. * @newsk: buffer to queue
  1248. *
  1249. * Queue a buffer at the tail of the list. This function takes the
  1250. * list lock and can be used safely with other locking &sk_buff functions
  1251. * safely.
  1252. *
  1253. * A buffer cannot be placed on two lists at the same time.
  1254. */
  1255. void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
  1256. {
  1257. unsigned long flags;
  1258. spin_lock_irqsave(&list->lock, flags);
  1259. __skb_queue_tail(list, newsk);
  1260. spin_unlock_irqrestore(&list->lock, flags);
  1261. }
  1262. /**
  1263. * skb_unlink - remove a buffer from a list
  1264. * @skb: buffer to remove
  1265. * @list: list to use
  1266. *
  1267. * Remove a packet from a list. The list locks are taken and this
  1268. * function is atomic with respect to other list locked calls
  1269. *
  1270. * You must know what list the SKB is on.
  1271. */
  1272. void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
  1273. {
  1274. unsigned long flags;
  1275. spin_lock_irqsave(&list->lock, flags);
  1276. __skb_unlink(skb, list);
  1277. spin_unlock_irqrestore(&list->lock, flags);
  1278. }
  1279. /**
  1280. * skb_append - append a buffer
  1281. * @old: buffer to insert after
  1282. * @newsk: buffer to insert
  1283. * @list: list to use
  1284. *
  1285. * Place a packet after a given packet in a list. The list locks are taken
  1286. * and this function is atomic with respect to other list locked calls.
  1287. * A buffer cannot be placed on two lists at the same time.
  1288. */
  1289. void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
  1290. {
  1291. unsigned long flags;
  1292. spin_lock_irqsave(&list->lock, flags);
  1293. __skb_append(old, newsk, list);
  1294. spin_unlock_irqrestore(&list->lock, flags);
  1295. }
  1296. /**
  1297. * skb_insert - insert a buffer
  1298. * @old: buffer to insert before
  1299. * @newsk: buffer to insert
  1300. * @list: list to use
  1301. *
  1302. * Place a packet before a given packet in a list. The list locks are
  1303. * taken and this function is atomic with respect to other list locked
  1304. * calls.
  1305. *
  1306. * A buffer cannot be placed on two lists at the same time.
  1307. */
  1308. void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
  1309. {
  1310. unsigned long flags;
  1311. spin_lock_irqsave(&list->lock, flags);
  1312. __skb_insert(newsk, old->prev, old, list);
  1313. spin_unlock_irqrestore(&list->lock, flags);
  1314. }
  1315. static inline void skb_split_inside_header(struct sk_buff *skb,
  1316. struct sk_buff* skb1,
  1317. const u32 len, const int pos)
  1318. {
  1319. int i;
  1320. skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
  1321. pos - len);
  1322. /* And move data appendix as is. */
  1323. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
  1324. skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
  1325. skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
  1326. skb_shinfo(skb)->nr_frags = 0;
  1327. skb1->data_len = skb->data_len;
  1328. skb1->len += skb1->data_len;
  1329. skb->data_len = 0;
  1330. skb->len = len;
  1331. skb_set_tail_pointer(skb, len);
  1332. }
  1333. static inline void skb_split_no_header(struct sk_buff *skb,
  1334. struct sk_buff* skb1,
  1335. const u32 len, int pos)
  1336. {
  1337. int i, k = 0;
  1338. const int nfrags = skb_shinfo(skb)->nr_frags;
  1339. skb_shinfo(skb)->nr_frags = 0;
  1340. skb1->len = skb1->data_len = skb->len - len;
  1341. skb->len = len;
  1342. skb->data_len = len - pos;
  1343. for (i = 0; i < nfrags; i++) {
  1344. int size = skb_shinfo(skb)->frags[i].size;
  1345. if (pos + size > len) {
  1346. skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
  1347. if (pos < len) {
  1348. /* Split frag.
  1349. * We have two variants in this case:
  1350. * 1. Move all the frag to the second
  1351. * part, if it is possible. F.e.
  1352. * this approach is mandatory for TUX,
  1353. * where splitting is expensive.
  1354. * 2. Split is accurately. We make this.
  1355. */
  1356. get_page(skb_shinfo(skb)->frags[i].page);
  1357. skb_shinfo(skb1)->frags[0].page_offset += len - pos;
  1358. skb_shinfo(skb1)->frags[0].size -= len - pos;
  1359. skb_shinfo(skb)->frags[i].size = len - pos;
  1360. skb_shinfo(skb)->nr_frags++;
  1361. }
  1362. k++;
  1363. } else
  1364. skb_shinfo(skb)->nr_frags++;
  1365. pos += size;
  1366. }
  1367. skb_shinfo(skb1)->nr_frags = k;
  1368. }
  1369. /**
  1370. * skb_split - Split fragmented skb to two parts at length len.
  1371. * @skb: the buffer to split
  1372. * @skb1: the buffer to receive the second part
  1373. * @len: new length for skb
  1374. */
  1375. void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
  1376. {
  1377. int pos = skb_headlen(skb);
  1378. if (len < pos) /* Split line is inside header. */
  1379. skb_split_inside_header(skb, skb1, len, pos);
  1380. else /* Second chunk has no header, nothing to copy. */
  1381. skb_split_no_header(skb, skb1, len, pos);
  1382. }
  1383. /**
  1384. * skb_prepare_seq_read - Prepare a sequential read of skb data
  1385. * @skb: the buffer to read
  1386. * @from: lower offset of data to be read
  1387. * @to: upper offset of data to be read
  1388. * @st: state variable
  1389. *
  1390. * Initializes the specified state variable. Must be called before
  1391. * invoking skb_seq_read() for the first time.
  1392. */
  1393. void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
  1394. unsigned int to, struct skb_seq_state *st)
  1395. {
  1396. st->lower_offset = from;
  1397. st->upper_offset = to;
  1398. st->root_skb = st->cur_skb = skb;
  1399. st->frag_idx = st->stepped_offset = 0;
  1400. st->frag_data = NULL;
  1401. }
  1402. /**
  1403. * skb_seq_read - Sequentially read skb data
  1404. * @consumed: number of bytes consumed by the caller so far
  1405. * @data: destination pointer for data to be returned
  1406. * @st: state variable
  1407. *
  1408. * Reads a block of skb data at &consumed relative to the
  1409. * lower offset specified to skb_prepare_seq_read(). Assigns
  1410. * the head of the data block to &data and returns the length
  1411. * of the block or 0 if the end of the skb data or the upper
  1412. * offset has been reached.
  1413. *
  1414. * The caller is not required to consume all of the data
  1415. * returned, i.e. &consumed is typically set to the number
  1416. * of bytes already consumed and the next call to
  1417. * skb_seq_read() will return the remaining part of the block.
  1418. *
  1419. * Note: The size of each block of data returned can be arbitary,
  1420. * this limitation is the cost for zerocopy seqeuental
  1421. * reads of potentially non linear data.
  1422. *
  1423. * Note: Fragment lists within fragments are not implemented
  1424. * at the moment, state->root_skb could be replaced with
  1425. * a stack for this purpose.
  1426. */
  1427. unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
  1428. struct skb_seq_state *st)
  1429. {
  1430. unsigned int block_limit, abs_offset = consumed + st->lower_offset;
  1431. skb_frag_t *frag;
  1432. if (unlikely(abs_offset >= st->upper_offset))
  1433. return 0;
  1434. next_skb:
  1435. block_limit = skb_headlen(st->cur_skb);
  1436. if (abs_offset < block_limit) {
  1437. *data = st->cur_skb->data + abs_offset;
  1438. return block_limit - abs_offset;
  1439. }
  1440. if (st->frag_idx == 0 && !st->frag_data)
  1441. st->stepped_offset += skb_headlen(st->cur_skb);
  1442. while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
  1443. frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
  1444. block_limit = frag->size + st->stepped_offset;
  1445. if (abs_offset < block_limit) {
  1446. if (!st->frag_data)
  1447. st->frag_data = kmap_skb_frag(frag);
  1448. *data = (u8 *) st->frag_data + frag->page_offset +
  1449. (abs_offset - st->stepped_offset);
  1450. return block_limit - abs_offset;
  1451. }
  1452. if (st->frag_data) {
  1453. kunmap_skb_frag(st->frag_data);
  1454. st->frag_data = NULL;
  1455. }
  1456. st->frag_idx++;
  1457. st->stepped_offset += frag->size;
  1458. }
  1459. if (st->cur_skb->next) {
  1460. st->cur_skb = st->cur_skb->next;
  1461. st->frag_idx = 0;
  1462. goto next_skb;
  1463. } else if (st->root_skb == st->cur_skb &&
  1464. skb_shinfo(st->root_skb)->frag_list) {
  1465. st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
  1466. goto next_skb;
  1467. }
  1468. return 0;
  1469. }
  1470. /**
  1471. * skb_abort_seq_read - Abort a sequential read of skb data
  1472. * @st: state variable
  1473. *
  1474. * Must be called if skb_seq_read() was not called until it
  1475. * returned 0.
  1476. */
  1477. void skb_abort_seq_read(struct skb_seq_state *st)
  1478. {
  1479. if (st->frag_data)
  1480. kunmap_skb_frag(st->frag_data);
  1481. }
  1482. #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
  1483. static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
  1484. struct ts_config *conf,
  1485. struct ts_state *state)
  1486. {
  1487. return skb_seq_read(offset, text, TS_SKB_CB(state));
  1488. }
  1489. static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
  1490. {
  1491. skb_abort_seq_read(TS_SKB_CB(state));
  1492. }
  1493. /**
  1494. * skb_find_text - Find a text pattern in skb data
  1495. * @skb: the buffer to look in
  1496. * @from: search offset
  1497. * @to: search limit
  1498. * @config: textsearch configuration
  1499. * @state: uninitialized textsearch state variable
  1500. *
  1501. * Finds a pattern in the skb data according to the specified
  1502. * textsearch configuration. Use textsearch_next() to retrieve
  1503. * subsequent occurrences of the pattern. Returns the offset
  1504. * to the first occurrence or UINT_MAX if no match was found.
  1505. */
  1506. unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
  1507. unsigned int to, struct ts_config *config,
  1508. struct ts_state *state)
  1509. {
  1510. unsigned int ret;
  1511. config->get_next_block = skb_ts_get_next_block;
  1512. config->finish = skb_ts_finish;
  1513. skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
  1514. ret = textsearch_find(config, state);
  1515. return (ret <= to - from ? ret : UINT_MAX);
  1516. }
  1517. /**
  1518. * skb_append_datato_frags: - append the user data to a skb
  1519. * @sk: sock structure
  1520. * @skb: skb structure to be appened with user data.
  1521. * @getfrag: call back function to be used for getting the user data
  1522. * @from: pointer to user message iov
  1523. * @length: length of the iov message
  1524. *
  1525. * Description: This procedure append the user data in the fragment part
  1526. * of the skb if any page alloc fails user this procedure returns -ENOMEM
  1527. */
  1528. int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
  1529. int (*getfrag)(void *from, char *to, int offset,
  1530. int len, int odd, struct sk_buff *skb),
  1531. void *from, int length)
  1532. {
  1533. int frg_cnt = 0;
  1534. skb_frag_t *frag = NULL;
  1535. struct page *page = NULL;
  1536. int copy, left;
  1537. int offset = 0;
  1538. int ret;
  1539. do {
  1540. /* Return error if we don't have space for new frag */
  1541. frg_cnt = skb_shinfo(skb)->nr_frags;
  1542. if (frg_cnt >= MAX_SKB_FRAGS)
  1543. return -EFAULT;
  1544. /* allocate a new page for next frag */
  1545. page = alloc_pages(sk->sk_allocation, 0);
  1546. /* If alloc_page fails just return failure and caller will
  1547. * free previous allocated pages by doing kfree_skb()
  1548. */
  1549. if (page == NULL)
  1550. return -ENOMEM;
  1551. /* initialize the next frag */
  1552. sk->sk_sndmsg_page = page;
  1553. sk->sk_sndmsg_off = 0;
  1554. skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
  1555. skb->truesize += PAGE_SIZE;
  1556. atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
  1557. /* get the new initialized frag */
  1558. frg_cnt = skb_shinfo(skb)->nr_frags;
  1559. frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
  1560. /* copy the user data to page */
  1561. left = PAGE_SIZE - frag->page_offset;
  1562. copy = (length > left)? left : length;
  1563. ret = getfrag(from, (page_address(frag->page) +
  1564. frag->page_offset + frag->size),
  1565. offset, copy, 0, skb);
  1566. if (ret < 0)
  1567. return -EFAULT;
  1568. /* copy was successful so update the size parameters */
  1569. sk->sk_sndmsg_off += copy;
  1570. frag->size += copy;
  1571. skb->len += copy;
  1572. skb->data_len += copy;
  1573. offset += copy;
  1574. length -= copy;
  1575. } while (length > 0);
  1576. return 0;
  1577. }
  1578. /**
  1579. * skb_pull_rcsum - pull skb and update receive checksum
  1580. * @skb: buffer to update
  1581. * @start: start of data before pull
  1582. * @len: length of data pulled
  1583. *
  1584. * This function performs an skb_pull on the packet and updates
  1585. * update the CHECKSUM_COMPLETE checksum. It should be used on
  1586. * receive path processing instead of skb_pull unless you know
  1587. * that the checksum difference is zero (e.g., a valid IP header)
  1588. * or you are setting ip_summed to CHECKSUM_NONE.
  1589. */
  1590. unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
  1591. {
  1592. BUG_ON(len > skb->len);
  1593. skb->len -= len;
  1594. BUG_ON(skb->len < skb->data_len);
  1595. skb_postpull_rcsum(skb, skb->data, len);
  1596. return skb->data += len;
  1597. }
  1598. EXPORT_SYMBOL_GPL(skb_pull_rcsum);
  1599. /**
  1600. * skb_segment - Perform protocol segmentation on skb.
  1601. * @skb: buffer to segment
  1602. * @features: features for the output path (see dev->features)
  1603. *
  1604. * This function performs segmentation on the given skb. It returns
  1605. * the segment at the given position. It returns NULL if there are
  1606. * no more segments to generate, or when an error is encountered.
  1607. */
  1608. struct sk_buff *skb_segment(struct sk_buff *skb, int features)
  1609. {
  1610. struct sk_buff *segs = NULL;
  1611. struct sk_buff *tail = NULL;
  1612. unsigned int mss = skb_shinfo(skb)->gso_size;
  1613. unsigned int doffset = skb->data - skb_mac_header(skb);
  1614. unsigned int offset = doffset;
  1615. unsigned int headroom;
  1616. unsigned int len;
  1617. int sg = features & NETIF_F_SG;
  1618. int nfrags = skb_shinfo(skb)->nr_frags;
  1619. int err = -ENOMEM;
  1620. int i = 0;
  1621. int pos;
  1622. __skb_push(skb, doffset);
  1623. headroom = skb_headroom(skb);
  1624. pos = skb_headlen(skb);
  1625. do {
  1626. struct sk_buff *nskb;
  1627. skb_frag_t *frag;
  1628. int hsize;
  1629. int k;
  1630. int size;
  1631. len = skb->len - offset;
  1632. if (len > mss)
  1633. len = mss;
  1634. hsize = skb_headlen(skb) - offset;
  1635. if (hsize < 0)
  1636. hsize = 0;
  1637. if (hsize > len || !sg)
  1638. hsize = len;
  1639. nskb = alloc_skb(hsize + doffset + headroom, GFP_ATOMIC);
  1640. if (unlikely(!nskb))
  1641. goto err;
  1642. if (segs)
  1643. tail->next = nskb;
  1644. else
  1645. segs = nskb;
  1646. tail = nskb;
  1647. nskb->dev = skb->dev;
  1648. nskb->priority = skb->priority;
  1649. nskb->protocol = skb->protocol;
  1650. nskb->dst = dst_clone(skb->dst);
  1651. memcpy(nskb->cb, skb->cb, sizeof(skb->cb));
  1652. nskb->pkt_type = skb->pkt_type;
  1653. nskb->mac_len = skb->mac_len;
  1654. skb_reserve(nskb, headroom);
  1655. skb_reset_mac_header(nskb);
  1656. skb_set_network_header(nskb, skb->mac_len);
  1657. nskb->transport_header = (nskb->network_header +
  1658. skb_network_header_len(skb));
  1659. skb_copy_from_linear_data(skb, skb_put(nskb, doffset),
  1660. doffset);
  1661. if (!sg) {
  1662. nskb->csum = skb_copy_and_csum_bits(skb, offset,
  1663. skb_put(nskb, len),
  1664. len, 0);
  1665. continue;
  1666. }
  1667. frag = skb_shinfo(nskb)->frags;
  1668. k = 0;
  1669. nskb->ip_summed = CHECKSUM_PARTIAL;
  1670. nskb->csum = skb->csum;
  1671. skb_copy_from_linear_data_offset(skb, offset,
  1672. skb_put(nskb, hsize), 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(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|SLAB_PANIC,
  1712. NULL, NULL);
  1713. skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
  1714. (2*sizeof(struct sk_buff)) +
  1715. sizeof(atomic_t),
  1716. 0,
  1717. SLAB_HWCACHE_ALIGN|SLAB_PANIC,
  1718. NULL, NULL);
  1719. }
  1720. /**
  1721. * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
  1722. * @skb: Socket buffer containing the buffers to be mapped
  1723. * @sg: The scatter-gather list to map into
  1724. * @offset: The offset into the buffer's contents to start mapping
  1725. * @len: Length of buffer space to be mapped
  1726. *
  1727. * Fill the specified scatter-gather list with mappings/pointers into a
  1728. * region of the buffer space attached to a socket buffer.
  1729. */
  1730. int
  1731. skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
  1732. {
  1733. int end = skb_headlen(skb);
  1734. int i, copy = end - offset;
  1735. int elt = 0;
  1736. if (copy > 0) {
  1737. if (copy > len)
  1738. copy = len;
  1739. sg[elt].page = virt_to_page(skb->data + offset);
  1740. sg[elt].offset = (unsigned long)(skb->data + offset) % PAGE_SIZE;
  1741. sg[elt].length = copy;
  1742. elt++;
  1743. if ((len -= copy) == 0)
  1744. return elt;
  1745. offset += copy;
  1746. }
  1747. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1748. BUG_TRAP(len >= 0);
  1749. end = offset + skb_shinfo(skb)->frags[i].size;
  1750. if ((copy = end - offset) > 0) {
  1751. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  1752. if (copy > len)
  1753. copy = len;
  1754. sg[elt].page = frag->page;
  1755. sg[elt].offset = frag->page_offset;
  1756. sg[elt].length = copy;
  1757. elt++;
  1758. if (!(len -= copy))
  1759. return elt;
  1760. offset += copy;
  1761. }
  1762. }
  1763. if (skb_shinfo(skb)->frag_list) {
  1764. struct sk_buff *list = skb_shinfo(skb)->frag_list;
  1765. for (; list; list = list->next) {
  1766. BUG_TRAP(len >= 0);
  1767. end = offset + list->len;
  1768. if ((copy = end - offset) > 0) {
  1769. if (copy > len)
  1770. copy = len;
  1771. elt += skb_to_sgvec(list, sg+elt, 0, copy);
  1772. if ((len -= copy) == 0)
  1773. return elt;
  1774. offset += copy;
  1775. }
  1776. }
  1777. }
  1778. BUG_ON(len);
  1779. return elt;
  1780. }
  1781. /**
  1782. * skb_cow_data - Check that a socket buffer's data buffers are writable
  1783. * @skb: The socket buffer to check.
  1784. * @tailbits: Amount of trailing space to be added
  1785. * @trailer: Returned pointer to the skb where the @tailbits space begins
  1786. *
  1787. * Make sure that the data buffers attached to a socket buffer are
  1788. * writable. If they are not, private copies are made of the data buffers
  1789. * and the socket buffer is set to use these instead.
  1790. *
  1791. * If @tailbits is given, make sure that there is space to write @tailbits
  1792. * bytes of data beyond current end of socket buffer. @trailer will be
  1793. * set to point to the skb in which this space begins.
  1794. *
  1795. * The number of scatterlist elements required to completely map the
  1796. * COW'd and extended socket buffer will be returned.
  1797. */
  1798. int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
  1799. {
  1800. int copyflag;
  1801. int elt;
  1802. struct sk_buff *skb1, **skb_p;
  1803. /* If skb is cloned or its head is paged, reallocate
  1804. * head pulling out all the pages (pages are considered not writable
  1805. * at the moment even if they are anonymous).
  1806. */
  1807. if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
  1808. __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
  1809. return -ENOMEM;
  1810. /* Easy case. Most of packets will go this way. */
  1811. if (!skb_shinfo(skb)->frag_list) {
  1812. /* A little of trouble, not enough of space for trailer.
  1813. * This should not happen, when stack is tuned to generate
  1814. * good frames. OK, on miss we reallocate and reserve even more
  1815. * space, 128 bytes is fair. */
  1816. if (skb_tailroom(skb) < tailbits &&
  1817. pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
  1818. return -ENOMEM;
  1819. /* Voila! */
  1820. *trailer = skb;
  1821. return 1;
  1822. }
  1823. /* Misery. We are in troubles, going to mincer fragments... */
  1824. elt = 1;
  1825. skb_p = &skb_shinfo(skb)->frag_list;
  1826. copyflag = 0;
  1827. while ((skb1 = *skb_p) != NULL) {
  1828. int ntail = 0;
  1829. /* The fragment is partially pulled by someone,
  1830. * this can happen on input. Copy it and everything
  1831. * after it. */
  1832. if (skb_shared(skb1))
  1833. copyflag = 1;
  1834. /* If the skb is the last, worry about trailer. */
  1835. if (skb1->next == NULL && tailbits) {
  1836. if (skb_shinfo(skb1)->nr_frags ||
  1837. skb_shinfo(skb1)->frag_list ||
  1838. skb_tailroom(skb1) < tailbits)
  1839. ntail = tailbits + 128;
  1840. }
  1841. if (copyflag ||
  1842. skb_cloned(skb1) ||
  1843. ntail ||
  1844. skb_shinfo(skb1)->nr_frags ||
  1845. skb_shinfo(skb1)->frag_list) {
  1846. struct sk_buff *skb2;
  1847. /* Fuck, we are miserable poor guys... */
  1848. if (ntail == 0)
  1849. skb2 = skb_copy(skb1, GFP_ATOMIC);
  1850. else
  1851. skb2 = skb_copy_expand(skb1,
  1852. skb_headroom(skb1),
  1853. ntail,
  1854. GFP_ATOMIC);
  1855. if (unlikely(skb2 == NULL))
  1856. return -ENOMEM;
  1857. if (skb1->sk)
  1858. skb_set_owner_w(skb2, skb1->sk);
  1859. /* Looking around. Are we still alive?
  1860. * OK, link new skb, drop old one */
  1861. skb2->next = skb1->next;
  1862. *skb_p = skb2;
  1863. kfree_skb(skb1);
  1864. skb1 = skb2;
  1865. }
  1866. elt++;
  1867. *trailer = skb1;
  1868. skb_p = &skb1->next;
  1869. }
  1870. return elt;
  1871. }
  1872. EXPORT_SYMBOL(___pskb_trim);
  1873. EXPORT_SYMBOL(__kfree_skb);
  1874. EXPORT_SYMBOL(kfree_skb);
  1875. EXPORT_SYMBOL(__pskb_pull_tail);
  1876. EXPORT_SYMBOL(__alloc_skb);
  1877. EXPORT_SYMBOL(__netdev_alloc_skb);
  1878. EXPORT_SYMBOL(pskb_copy);
  1879. EXPORT_SYMBOL(pskb_expand_head);
  1880. EXPORT_SYMBOL(skb_checksum);
  1881. EXPORT_SYMBOL(skb_clone);
  1882. EXPORT_SYMBOL(skb_clone_fraglist);
  1883. EXPORT_SYMBOL(skb_copy);
  1884. EXPORT_SYMBOL(skb_copy_and_csum_bits);
  1885. EXPORT_SYMBOL(skb_copy_and_csum_dev);
  1886. EXPORT_SYMBOL(skb_copy_bits);
  1887. EXPORT_SYMBOL(skb_copy_expand);
  1888. EXPORT_SYMBOL(skb_over_panic);
  1889. EXPORT_SYMBOL(skb_pad);
  1890. EXPORT_SYMBOL(skb_realloc_headroom);
  1891. EXPORT_SYMBOL(skb_under_panic);
  1892. EXPORT_SYMBOL(skb_dequeue);
  1893. EXPORT_SYMBOL(skb_dequeue_tail);
  1894. EXPORT_SYMBOL(skb_insert);
  1895. EXPORT_SYMBOL(skb_queue_purge);
  1896. EXPORT_SYMBOL(skb_queue_head);
  1897. EXPORT_SYMBOL(skb_queue_tail);
  1898. EXPORT_SYMBOL(skb_unlink);
  1899. EXPORT_SYMBOL(skb_append);
  1900. EXPORT_SYMBOL(skb_split);
  1901. EXPORT_SYMBOL(skb_prepare_seq_read);
  1902. EXPORT_SYMBOL(skb_seq_read);
  1903. EXPORT_SYMBOL(skb_abort_seq_read);
  1904. EXPORT_SYMBOL(skb_find_text);
  1905. EXPORT_SYMBOL(skb_append_datato_frags);
  1906. EXPORT_SYMBOL_GPL(skb_to_sgvec);
  1907. EXPORT_SYMBOL_GPL(skb_cow_data);