skbuff.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236
  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 start = skb_headlen(skb);
  919. if (offset > (int)skb->len - len)
  920. goto fault;
  921. /* Copy header. */
  922. if ((copy = start - 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. int end;
  933. BUG_TRAP(start <= offset + len);
  934. end = start + skb_shinfo(skb)->frags[i].size;
  935. if ((copy = end - offset) > 0) {
  936. u8 *vaddr;
  937. if (copy > len)
  938. copy = len;
  939. vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
  940. memcpy(to,
  941. vaddr + skb_shinfo(skb)->frags[i].page_offset+
  942. offset - start, copy);
  943. kunmap_skb_frag(vaddr);
  944. if ((len -= copy) == 0)
  945. return 0;
  946. offset += copy;
  947. to += copy;
  948. }
  949. start = end;
  950. }
  951. if (skb_shinfo(skb)->frag_list) {
  952. struct sk_buff *list = skb_shinfo(skb)->frag_list;
  953. for (; list; list = list->next) {
  954. int end;
  955. BUG_TRAP(start <= offset + len);
  956. end = start + list->len;
  957. if ((copy = end - offset) > 0) {
  958. if (copy > len)
  959. copy = len;
  960. if (skb_copy_bits(list, offset - start,
  961. to, copy))
  962. goto fault;
  963. if ((len -= copy) == 0)
  964. return 0;
  965. offset += copy;
  966. to += copy;
  967. }
  968. start = end;
  969. }
  970. }
  971. if (!len)
  972. return 0;
  973. fault:
  974. return -EFAULT;
  975. }
  976. /**
  977. * skb_store_bits - store bits from kernel buffer to skb
  978. * @skb: destination buffer
  979. * @offset: offset in destination
  980. * @from: source buffer
  981. * @len: number of bytes to copy
  982. *
  983. * Copy the specified number of bytes from the source buffer to the
  984. * destination skb. This function handles all the messy bits of
  985. * traversing fragment lists and such.
  986. */
  987. int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
  988. {
  989. int i, copy;
  990. int start = skb_headlen(skb);
  991. if (offset > (int)skb->len - len)
  992. goto fault;
  993. if ((copy = start - offset) > 0) {
  994. if (copy > len)
  995. copy = len;
  996. skb_copy_to_linear_data_offset(skb, offset, from, copy);
  997. if ((len -= copy) == 0)
  998. return 0;
  999. offset += copy;
  1000. from += copy;
  1001. }
  1002. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1003. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  1004. int end;
  1005. BUG_TRAP(start <= offset + len);
  1006. end = start + frag->size;
  1007. if ((copy = end - offset) > 0) {
  1008. u8 *vaddr;
  1009. if (copy > len)
  1010. copy = len;
  1011. vaddr = kmap_skb_frag(frag);
  1012. memcpy(vaddr + frag->page_offset + offset - start,
  1013. from, copy);
  1014. kunmap_skb_frag(vaddr);
  1015. if ((len -= copy) == 0)
  1016. return 0;
  1017. offset += copy;
  1018. from += copy;
  1019. }
  1020. start = end;
  1021. }
  1022. if (skb_shinfo(skb)->frag_list) {
  1023. struct sk_buff *list = skb_shinfo(skb)->frag_list;
  1024. for (; list; list = list->next) {
  1025. int end;
  1026. BUG_TRAP(start <= offset + len);
  1027. end = start + list->len;
  1028. if ((copy = end - offset) > 0) {
  1029. if (copy > len)
  1030. copy = len;
  1031. if (skb_store_bits(list, offset - start,
  1032. from, copy))
  1033. goto fault;
  1034. if ((len -= copy) == 0)
  1035. return 0;
  1036. offset += copy;
  1037. from += copy;
  1038. }
  1039. start = end;
  1040. }
  1041. }
  1042. if (!len)
  1043. return 0;
  1044. fault:
  1045. return -EFAULT;
  1046. }
  1047. EXPORT_SYMBOL(skb_store_bits);
  1048. /* Checksum skb data. */
  1049. __wsum skb_checksum(const struct sk_buff *skb, int offset,
  1050. int len, __wsum csum)
  1051. {
  1052. int start = skb_headlen(skb);
  1053. int i, copy = start - offset;
  1054. int pos = 0;
  1055. /* Checksum header. */
  1056. if (copy > 0) {
  1057. if (copy > len)
  1058. copy = len;
  1059. csum = csum_partial(skb->data + offset, copy, csum);
  1060. if ((len -= copy) == 0)
  1061. return csum;
  1062. offset += copy;
  1063. pos = copy;
  1064. }
  1065. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1066. int end;
  1067. BUG_TRAP(start <= offset + len);
  1068. end = start + skb_shinfo(skb)->frags[i].size;
  1069. if ((copy = end - offset) > 0) {
  1070. __wsum csum2;
  1071. u8 *vaddr;
  1072. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  1073. if (copy > len)
  1074. copy = len;
  1075. vaddr = kmap_skb_frag(frag);
  1076. csum2 = csum_partial(vaddr + frag->page_offset +
  1077. offset - start, copy, 0);
  1078. kunmap_skb_frag(vaddr);
  1079. csum = csum_block_add(csum, csum2, pos);
  1080. if (!(len -= copy))
  1081. return csum;
  1082. offset += copy;
  1083. pos += copy;
  1084. }
  1085. start = end;
  1086. }
  1087. if (skb_shinfo(skb)->frag_list) {
  1088. struct sk_buff *list = skb_shinfo(skb)->frag_list;
  1089. for (; list; list = list->next) {
  1090. int end;
  1091. BUG_TRAP(start <= offset + len);
  1092. end = start + list->len;
  1093. if ((copy = end - offset) > 0) {
  1094. __wsum csum2;
  1095. if (copy > len)
  1096. copy = len;
  1097. csum2 = skb_checksum(list, offset - start,
  1098. copy, 0);
  1099. csum = csum_block_add(csum, csum2, pos);
  1100. if ((len -= copy) == 0)
  1101. return csum;
  1102. offset += copy;
  1103. pos += copy;
  1104. }
  1105. start = end;
  1106. }
  1107. }
  1108. BUG_ON(len);
  1109. return csum;
  1110. }
  1111. /* Both of above in one bottle. */
  1112. __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
  1113. u8 *to, int len, __wsum csum)
  1114. {
  1115. int start = skb_headlen(skb);
  1116. int i, copy = start - offset;
  1117. int pos = 0;
  1118. /* Copy header. */
  1119. if (copy > 0) {
  1120. if (copy > len)
  1121. copy = len;
  1122. csum = csum_partial_copy_nocheck(skb->data + offset, to,
  1123. copy, csum);
  1124. if ((len -= copy) == 0)
  1125. return csum;
  1126. offset += copy;
  1127. to += copy;
  1128. pos = copy;
  1129. }
  1130. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1131. int end;
  1132. BUG_TRAP(start <= offset + len);
  1133. end = start + skb_shinfo(skb)->frags[i].size;
  1134. if ((copy = end - offset) > 0) {
  1135. __wsum csum2;
  1136. u8 *vaddr;
  1137. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  1138. if (copy > len)
  1139. copy = len;
  1140. vaddr = kmap_skb_frag(frag);
  1141. csum2 = csum_partial_copy_nocheck(vaddr +
  1142. frag->page_offset +
  1143. offset - start, to,
  1144. copy, 0);
  1145. kunmap_skb_frag(vaddr);
  1146. csum = csum_block_add(csum, csum2, pos);
  1147. if (!(len -= copy))
  1148. return csum;
  1149. offset += copy;
  1150. to += copy;
  1151. pos += copy;
  1152. }
  1153. start = end;
  1154. }
  1155. if (skb_shinfo(skb)->frag_list) {
  1156. struct sk_buff *list = skb_shinfo(skb)->frag_list;
  1157. for (; list; list = list->next) {
  1158. __wsum csum2;
  1159. int end;
  1160. BUG_TRAP(start <= offset + len);
  1161. end = start + list->len;
  1162. if ((copy = end - offset) > 0) {
  1163. if (copy > len)
  1164. copy = len;
  1165. csum2 = skb_copy_and_csum_bits(list,
  1166. offset - start,
  1167. to, copy, 0);
  1168. csum = csum_block_add(csum, csum2, pos);
  1169. if ((len -= copy) == 0)
  1170. return csum;
  1171. offset += copy;
  1172. to += copy;
  1173. pos += copy;
  1174. }
  1175. start = end;
  1176. }
  1177. }
  1178. BUG_ON(len);
  1179. return csum;
  1180. }
  1181. void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
  1182. {
  1183. __wsum csum;
  1184. long csstart;
  1185. if (skb->ip_summed == CHECKSUM_PARTIAL)
  1186. csstart = skb->csum_start - skb_headroom(skb);
  1187. else
  1188. csstart = skb_headlen(skb);
  1189. BUG_ON(csstart > skb_headlen(skb));
  1190. skb_copy_from_linear_data(skb, to, csstart);
  1191. csum = 0;
  1192. if (csstart != skb->len)
  1193. csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
  1194. skb->len - csstart, 0);
  1195. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  1196. long csstuff = csstart + skb->csum_offset;
  1197. *((__sum16 *)(to + csstuff)) = csum_fold(csum);
  1198. }
  1199. }
  1200. /**
  1201. * skb_dequeue - remove from the head of the queue
  1202. * @list: list to dequeue from
  1203. *
  1204. * Remove the head of the list. The list lock is taken so the function
  1205. * may be used safely with other locking list functions. The head item is
  1206. * returned or %NULL if the list is empty.
  1207. */
  1208. struct sk_buff *skb_dequeue(struct sk_buff_head *list)
  1209. {
  1210. unsigned long flags;
  1211. struct sk_buff *result;
  1212. spin_lock_irqsave(&list->lock, flags);
  1213. result = __skb_dequeue(list);
  1214. spin_unlock_irqrestore(&list->lock, flags);
  1215. return result;
  1216. }
  1217. /**
  1218. * skb_dequeue_tail - remove from the tail of the queue
  1219. * @list: list to dequeue from
  1220. *
  1221. * Remove the tail of the list. The list lock is taken so the function
  1222. * may be used safely with other locking list functions. The tail item is
  1223. * returned or %NULL if the list is empty.
  1224. */
  1225. struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
  1226. {
  1227. unsigned long flags;
  1228. struct sk_buff *result;
  1229. spin_lock_irqsave(&list->lock, flags);
  1230. result = __skb_dequeue_tail(list);
  1231. spin_unlock_irqrestore(&list->lock, flags);
  1232. return result;
  1233. }
  1234. /**
  1235. * skb_queue_purge - empty a list
  1236. * @list: list to empty
  1237. *
  1238. * Delete all buffers on an &sk_buff list. Each buffer is removed from
  1239. * the list and one reference dropped. This function takes the list
  1240. * lock and is atomic with respect to other list locking functions.
  1241. */
  1242. void skb_queue_purge(struct sk_buff_head *list)
  1243. {
  1244. struct sk_buff *skb;
  1245. while ((skb = skb_dequeue(list)) != NULL)
  1246. kfree_skb(skb);
  1247. }
  1248. /**
  1249. * skb_queue_head - queue a buffer at the list head
  1250. * @list: list to use
  1251. * @newsk: buffer to queue
  1252. *
  1253. * Queue a buffer at the start of the list. This function takes the
  1254. * list lock and can be used safely with other locking &sk_buff functions
  1255. * safely.
  1256. *
  1257. * A buffer cannot be placed on two lists at the same time.
  1258. */
  1259. void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
  1260. {
  1261. unsigned long flags;
  1262. spin_lock_irqsave(&list->lock, flags);
  1263. __skb_queue_head(list, newsk);
  1264. spin_unlock_irqrestore(&list->lock, flags);
  1265. }
  1266. /**
  1267. * skb_queue_tail - queue a buffer at the list tail
  1268. * @list: list to use
  1269. * @newsk: buffer to queue
  1270. *
  1271. * Queue a buffer at the tail of the list. This function takes the
  1272. * list lock and can be used safely with other locking &sk_buff functions
  1273. * safely.
  1274. *
  1275. * A buffer cannot be placed on two lists at the same time.
  1276. */
  1277. void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
  1278. {
  1279. unsigned long flags;
  1280. spin_lock_irqsave(&list->lock, flags);
  1281. __skb_queue_tail(list, newsk);
  1282. spin_unlock_irqrestore(&list->lock, flags);
  1283. }
  1284. /**
  1285. * skb_unlink - remove a buffer from a list
  1286. * @skb: buffer to remove
  1287. * @list: list to use
  1288. *
  1289. * Remove a packet from a list. The list locks are taken and this
  1290. * function is atomic with respect to other list locked calls
  1291. *
  1292. * You must know what list the SKB is on.
  1293. */
  1294. void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
  1295. {
  1296. unsigned long flags;
  1297. spin_lock_irqsave(&list->lock, flags);
  1298. __skb_unlink(skb, list);
  1299. spin_unlock_irqrestore(&list->lock, flags);
  1300. }
  1301. /**
  1302. * skb_append - append a buffer
  1303. * @old: buffer to insert after
  1304. * @newsk: buffer to insert
  1305. * @list: list to use
  1306. *
  1307. * Place a packet after a given packet in a list. The list locks are taken
  1308. * and this function is atomic with respect to other list locked calls.
  1309. * A buffer cannot be placed on two lists at the same time.
  1310. */
  1311. void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
  1312. {
  1313. unsigned long flags;
  1314. spin_lock_irqsave(&list->lock, flags);
  1315. __skb_append(old, newsk, list);
  1316. spin_unlock_irqrestore(&list->lock, flags);
  1317. }
  1318. /**
  1319. * skb_insert - insert a buffer
  1320. * @old: buffer to insert before
  1321. * @newsk: buffer to insert
  1322. * @list: list to use
  1323. *
  1324. * Place a packet before a given packet in a list. The list locks are
  1325. * taken and this function is atomic with respect to other list locked
  1326. * calls.
  1327. *
  1328. * A buffer cannot be placed on two lists at the same time.
  1329. */
  1330. void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
  1331. {
  1332. unsigned long flags;
  1333. spin_lock_irqsave(&list->lock, flags);
  1334. __skb_insert(newsk, old->prev, old, list);
  1335. spin_unlock_irqrestore(&list->lock, flags);
  1336. }
  1337. static inline void skb_split_inside_header(struct sk_buff *skb,
  1338. struct sk_buff* skb1,
  1339. const u32 len, const int pos)
  1340. {
  1341. int i;
  1342. skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
  1343. pos - len);
  1344. /* And move data appendix as is. */
  1345. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
  1346. skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
  1347. skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
  1348. skb_shinfo(skb)->nr_frags = 0;
  1349. skb1->data_len = skb->data_len;
  1350. skb1->len += skb1->data_len;
  1351. skb->data_len = 0;
  1352. skb->len = len;
  1353. skb_set_tail_pointer(skb, len);
  1354. }
  1355. static inline void skb_split_no_header(struct sk_buff *skb,
  1356. struct sk_buff* skb1,
  1357. const u32 len, int pos)
  1358. {
  1359. int i, k = 0;
  1360. const int nfrags = skb_shinfo(skb)->nr_frags;
  1361. skb_shinfo(skb)->nr_frags = 0;
  1362. skb1->len = skb1->data_len = skb->len - len;
  1363. skb->len = len;
  1364. skb->data_len = len - pos;
  1365. for (i = 0; i < nfrags; i++) {
  1366. int size = skb_shinfo(skb)->frags[i].size;
  1367. if (pos + size > len) {
  1368. skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
  1369. if (pos < len) {
  1370. /* Split frag.
  1371. * We have two variants in this case:
  1372. * 1. Move all the frag to the second
  1373. * part, if it is possible. F.e.
  1374. * this approach is mandatory for TUX,
  1375. * where splitting is expensive.
  1376. * 2. Split is accurately. We make this.
  1377. */
  1378. get_page(skb_shinfo(skb)->frags[i].page);
  1379. skb_shinfo(skb1)->frags[0].page_offset += len - pos;
  1380. skb_shinfo(skb1)->frags[0].size -= len - pos;
  1381. skb_shinfo(skb)->frags[i].size = len - pos;
  1382. skb_shinfo(skb)->nr_frags++;
  1383. }
  1384. k++;
  1385. } else
  1386. skb_shinfo(skb)->nr_frags++;
  1387. pos += size;
  1388. }
  1389. skb_shinfo(skb1)->nr_frags = k;
  1390. }
  1391. /**
  1392. * skb_split - Split fragmented skb to two parts at length len.
  1393. * @skb: the buffer to split
  1394. * @skb1: the buffer to receive the second part
  1395. * @len: new length for skb
  1396. */
  1397. void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
  1398. {
  1399. int pos = skb_headlen(skb);
  1400. if (len < pos) /* Split line is inside header. */
  1401. skb_split_inside_header(skb, skb1, len, pos);
  1402. else /* Second chunk has no header, nothing to copy. */
  1403. skb_split_no_header(skb, skb1, len, pos);
  1404. }
  1405. /**
  1406. * skb_prepare_seq_read - Prepare a sequential read of skb data
  1407. * @skb: the buffer to read
  1408. * @from: lower offset of data to be read
  1409. * @to: upper offset of data to be read
  1410. * @st: state variable
  1411. *
  1412. * Initializes the specified state variable. Must be called before
  1413. * invoking skb_seq_read() for the first time.
  1414. */
  1415. void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
  1416. unsigned int to, struct skb_seq_state *st)
  1417. {
  1418. st->lower_offset = from;
  1419. st->upper_offset = to;
  1420. st->root_skb = st->cur_skb = skb;
  1421. st->frag_idx = st->stepped_offset = 0;
  1422. st->frag_data = NULL;
  1423. }
  1424. /**
  1425. * skb_seq_read - Sequentially read skb data
  1426. * @consumed: number of bytes consumed by the caller so far
  1427. * @data: destination pointer for data to be returned
  1428. * @st: state variable
  1429. *
  1430. * Reads a block of skb data at &consumed relative to the
  1431. * lower offset specified to skb_prepare_seq_read(). Assigns
  1432. * the head of the data block to &data and returns the length
  1433. * of the block or 0 if the end of the skb data or the upper
  1434. * offset has been reached.
  1435. *
  1436. * The caller is not required to consume all of the data
  1437. * returned, i.e. &consumed is typically set to the number
  1438. * of bytes already consumed and the next call to
  1439. * skb_seq_read() will return the remaining part of the block.
  1440. *
  1441. * Note: The size of each block of data returned can be arbitary,
  1442. * this limitation is the cost for zerocopy seqeuental
  1443. * reads of potentially non linear data.
  1444. *
  1445. * Note: Fragment lists within fragments are not implemented
  1446. * at the moment, state->root_skb could be replaced with
  1447. * a stack for this purpose.
  1448. */
  1449. unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
  1450. struct skb_seq_state *st)
  1451. {
  1452. unsigned int block_limit, abs_offset = consumed + st->lower_offset;
  1453. skb_frag_t *frag;
  1454. if (unlikely(abs_offset >= st->upper_offset))
  1455. return 0;
  1456. next_skb:
  1457. block_limit = skb_headlen(st->cur_skb);
  1458. if (abs_offset < block_limit) {
  1459. *data = st->cur_skb->data + abs_offset;
  1460. return block_limit - abs_offset;
  1461. }
  1462. if (st->frag_idx == 0 && !st->frag_data)
  1463. st->stepped_offset += skb_headlen(st->cur_skb);
  1464. while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
  1465. frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
  1466. block_limit = frag->size + st->stepped_offset;
  1467. if (abs_offset < block_limit) {
  1468. if (!st->frag_data)
  1469. st->frag_data = kmap_skb_frag(frag);
  1470. *data = (u8 *) st->frag_data + frag->page_offset +
  1471. (abs_offset - st->stepped_offset);
  1472. return block_limit - abs_offset;
  1473. }
  1474. if (st->frag_data) {
  1475. kunmap_skb_frag(st->frag_data);
  1476. st->frag_data = NULL;
  1477. }
  1478. st->frag_idx++;
  1479. st->stepped_offset += frag->size;
  1480. }
  1481. if (st->cur_skb->next) {
  1482. st->cur_skb = st->cur_skb->next;
  1483. st->frag_idx = 0;
  1484. goto next_skb;
  1485. } else if (st->root_skb == st->cur_skb &&
  1486. skb_shinfo(st->root_skb)->frag_list) {
  1487. st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
  1488. goto next_skb;
  1489. }
  1490. return 0;
  1491. }
  1492. /**
  1493. * skb_abort_seq_read - Abort a sequential read of skb data
  1494. * @st: state variable
  1495. *
  1496. * Must be called if skb_seq_read() was not called until it
  1497. * returned 0.
  1498. */
  1499. void skb_abort_seq_read(struct skb_seq_state *st)
  1500. {
  1501. if (st->frag_data)
  1502. kunmap_skb_frag(st->frag_data);
  1503. }
  1504. #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
  1505. static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
  1506. struct ts_config *conf,
  1507. struct ts_state *state)
  1508. {
  1509. return skb_seq_read(offset, text, TS_SKB_CB(state));
  1510. }
  1511. static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
  1512. {
  1513. skb_abort_seq_read(TS_SKB_CB(state));
  1514. }
  1515. /**
  1516. * skb_find_text - Find a text pattern in skb data
  1517. * @skb: the buffer to look in
  1518. * @from: search offset
  1519. * @to: search limit
  1520. * @config: textsearch configuration
  1521. * @state: uninitialized textsearch state variable
  1522. *
  1523. * Finds a pattern in the skb data according to the specified
  1524. * textsearch configuration. Use textsearch_next() to retrieve
  1525. * subsequent occurrences of the pattern. Returns the offset
  1526. * to the first occurrence or UINT_MAX if no match was found.
  1527. */
  1528. unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
  1529. unsigned int to, struct ts_config *config,
  1530. struct ts_state *state)
  1531. {
  1532. unsigned int ret;
  1533. config->get_next_block = skb_ts_get_next_block;
  1534. config->finish = skb_ts_finish;
  1535. skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
  1536. ret = textsearch_find(config, state);
  1537. return (ret <= to - from ? ret : UINT_MAX);
  1538. }
  1539. /**
  1540. * skb_append_datato_frags: - append the user data to a skb
  1541. * @sk: sock structure
  1542. * @skb: skb structure to be appened with user data.
  1543. * @getfrag: call back function to be used for getting the user data
  1544. * @from: pointer to user message iov
  1545. * @length: length of the iov message
  1546. *
  1547. * Description: This procedure append the user data in the fragment part
  1548. * of the skb if any page alloc fails user this procedure returns -ENOMEM
  1549. */
  1550. int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
  1551. int (*getfrag)(void *from, char *to, int offset,
  1552. int len, int odd, struct sk_buff *skb),
  1553. void *from, int length)
  1554. {
  1555. int frg_cnt = 0;
  1556. skb_frag_t *frag = NULL;
  1557. struct page *page = NULL;
  1558. int copy, left;
  1559. int offset = 0;
  1560. int ret;
  1561. do {
  1562. /* Return error if we don't have space for new frag */
  1563. frg_cnt = skb_shinfo(skb)->nr_frags;
  1564. if (frg_cnt >= MAX_SKB_FRAGS)
  1565. return -EFAULT;
  1566. /* allocate a new page for next frag */
  1567. page = alloc_pages(sk->sk_allocation, 0);
  1568. /* If alloc_page fails just return failure and caller will
  1569. * free previous allocated pages by doing kfree_skb()
  1570. */
  1571. if (page == NULL)
  1572. return -ENOMEM;
  1573. /* initialize the next frag */
  1574. sk->sk_sndmsg_page = page;
  1575. sk->sk_sndmsg_off = 0;
  1576. skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
  1577. skb->truesize += PAGE_SIZE;
  1578. atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
  1579. /* get the new initialized frag */
  1580. frg_cnt = skb_shinfo(skb)->nr_frags;
  1581. frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
  1582. /* copy the user data to page */
  1583. left = PAGE_SIZE - frag->page_offset;
  1584. copy = (length > left)? left : length;
  1585. ret = getfrag(from, (page_address(frag->page) +
  1586. frag->page_offset + frag->size),
  1587. offset, copy, 0, skb);
  1588. if (ret < 0)
  1589. return -EFAULT;
  1590. /* copy was successful so update the size parameters */
  1591. sk->sk_sndmsg_off += copy;
  1592. frag->size += copy;
  1593. skb->len += copy;
  1594. skb->data_len += copy;
  1595. offset += copy;
  1596. length -= copy;
  1597. } while (length > 0);
  1598. return 0;
  1599. }
  1600. /**
  1601. * skb_pull_rcsum - pull skb and update receive checksum
  1602. * @skb: buffer to update
  1603. * @start: start of data before pull
  1604. * @len: length of data pulled
  1605. *
  1606. * This function performs an skb_pull on the packet and updates
  1607. * update the CHECKSUM_COMPLETE checksum. It should be used on
  1608. * receive path processing instead of skb_pull unless you know
  1609. * that the checksum difference is zero (e.g., a valid IP header)
  1610. * or you are setting ip_summed to CHECKSUM_NONE.
  1611. */
  1612. unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
  1613. {
  1614. BUG_ON(len > skb->len);
  1615. skb->len -= len;
  1616. BUG_ON(skb->len < skb->data_len);
  1617. skb_postpull_rcsum(skb, skb->data, len);
  1618. return skb->data += len;
  1619. }
  1620. EXPORT_SYMBOL_GPL(skb_pull_rcsum);
  1621. /**
  1622. * skb_segment - Perform protocol segmentation on skb.
  1623. * @skb: buffer to segment
  1624. * @features: features for the output path (see dev->features)
  1625. *
  1626. * This function performs segmentation on the given skb. It returns
  1627. * the segment at the given position. It returns NULL if there are
  1628. * no more segments to generate, or when an error is encountered.
  1629. */
  1630. struct sk_buff *skb_segment(struct sk_buff *skb, int features)
  1631. {
  1632. struct sk_buff *segs = NULL;
  1633. struct sk_buff *tail = NULL;
  1634. unsigned int mss = skb_shinfo(skb)->gso_size;
  1635. unsigned int doffset = skb->data - skb_mac_header(skb);
  1636. unsigned int offset = doffset;
  1637. unsigned int headroom;
  1638. unsigned int len;
  1639. int sg = features & NETIF_F_SG;
  1640. int nfrags = skb_shinfo(skb)->nr_frags;
  1641. int err = -ENOMEM;
  1642. int i = 0;
  1643. int pos;
  1644. __skb_push(skb, doffset);
  1645. headroom = skb_headroom(skb);
  1646. pos = skb_headlen(skb);
  1647. do {
  1648. struct sk_buff *nskb;
  1649. skb_frag_t *frag;
  1650. int hsize;
  1651. int k;
  1652. int size;
  1653. len = skb->len - offset;
  1654. if (len > mss)
  1655. len = mss;
  1656. hsize = skb_headlen(skb) - offset;
  1657. if (hsize < 0)
  1658. hsize = 0;
  1659. if (hsize > len || !sg)
  1660. hsize = len;
  1661. nskb = alloc_skb(hsize + doffset + headroom, GFP_ATOMIC);
  1662. if (unlikely(!nskb))
  1663. goto err;
  1664. if (segs)
  1665. tail->next = nskb;
  1666. else
  1667. segs = nskb;
  1668. tail = nskb;
  1669. nskb->dev = skb->dev;
  1670. nskb->priority = skb->priority;
  1671. nskb->protocol = skb->protocol;
  1672. nskb->dst = dst_clone(skb->dst);
  1673. memcpy(nskb->cb, skb->cb, sizeof(skb->cb));
  1674. nskb->pkt_type = skb->pkt_type;
  1675. nskb->mac_len = skb->mac_len;
  1676. skb_reserve(nskb, headroom);
  1677. skb_reset_mac_header(nskb);
  1678. skb_set_network_header(nskb, skb->mac_len);
  1679. nskb->transport_header = (nskb->network_header +
  1680. skb_network_header_len(skb));
  1681. skb_copy_from_linear_data(skb, skb_put(nskb, doffset),
  1682. doffset);
  1683. if (!sg) {
  1684. nskb->csum = skb_copy_and_csum_bits(skb, offset,
  1685. skb_put(nskb, len),
  1686. len, 0);
  1687. continue;
  1688. }
  1689. frag = skb_shinfo(nskb)->frags;
  1690. k = 0;
  1691. nskb->ip_summed = CHECKSUM_PARTIAL;
  1692. nskb->csum = skb->csum;
  1693. skb_copy_from_linear_data_offset(skb, offset,
  1694. skb_put(nskb, hsize), hsize);
  1695. while (pos < offset + len) {
  1696. BUG_ON(i >= nfrags);
  1697. *frag = skb_shinfo(skb)->frags[i];
  1698. get_page(frag->page);
  1699. size = frag->size;
  1700. if (pos < offset) {
  1701. frag->page_offset += offset - pos;
  1702. frag->size -= offset - pos;
  1703. }
  1704. k++;
  1705. if (pos + size <= offset + len) {
  1706. i++;
  1707. pos += size;
  1708. } else {
  1709. frag->size -= pos + size - (offset + len);
  1710. break;
  1711. }
  1712. frag++;
  1713. }
  1714. skb_shinfo(nskb)->nr_frags = k;
  1715. nskb->data_len = len - hsize;
  1716. nskb->len += nskb->data_len;
  1717. nskb->truesize += nskb->data_len;
  1718. } while ((offset += len) < skb->len);
  1719. return segs;
  1720. err:
  1721. while ((skb = segs)) {
  1722. segs = skb->next;
  1723. kfree_skb(skb);
  1724. }
  1725. return ERR_PTR(err);
  1726. }
  1727. EXPORT_SYMBOL_GPL(skb_segment);
  1728. void __init skb_init(void)
  1729. {
  1730. skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
  1731. sizeof(struct sk_buff),
  1732. 0,
  1733. SLAB_HWCACHE_ALIGN|SLAB_PANIC,
  1734. NULL, NULL);
  1735. skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
  1736. (2*sizeof(struct sk_buff)) +
  1737. sizeof(atomic_t),
  1738. 0,
  1739. SLAB_HWCACHE_ALIGN|SLAB_PANIC,
  1740. NULL, NULL);
  1741. }
  1742. /**
  1743. * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
  1744. * @skb: Socket buffer containing the buffers to be mapped
  1745. * @sg: The scatter-gather list to map into
  1746. * @offset: The offset into the buffer's contents to start mapping
  1747. * @len: Length of buffer space to be mapped
  1748. *
  1749. * Fill the specified scatter-gather list with mappings/pointers into a
  1750. * region of the buffer space attached to a socket buffer.
  1751. */
  1752. int
  1753. skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
  1754. {
  1755. int start = skb_headlen(skb);
  1756. int i, copy = start - offset;
  1757. int elt = 0;
  1758. if (copy > 0) {
  1759. if (copy > len)
  1760. copy = len;
  1761. sg[elt].page = virt_to_page(skb->data + offset);
  1762. sg[elt].offset = (unsigned long)(skb->data + offset) % PAGE_SIZE;
  1763. sg[elt].length = copy;
  1764. elt++;
  1765. if ((len -= copy) == 0)
  1766. return elt;
  1767. offset += copy;
  1768. }
  1769. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1770. int end;
  1771. BUG_TRAP(start <= offset + len);
  1772. end = start + skb_shinfo(skb)->frags[i].size;
  1773. if ((copy = end - offset) > 0) {
  1774. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  1775. if (copy > len)
  1776. copy = len;
  1777. sg[elt].page = frag->page;
  1778. sg[elt].offset = frag->page_offset+offset-start;
  1779. sg[elt].length = copy;
  1780. elt++;
  1781. if (!(len -= copy))
  1782. return elt;
  1783. offset += copy;
  1784. }
  1785. start = end;
  1786. }
  1787. if (skb_shinfo(skb)->frag_list) {
  1788. struct sk_buff *list = skb_shinfo(skb)->frag_list;
  1789. for (; list; list = list->next) {
  1790. int end;
  1791. BUG_TRAP(start <= offset + len);
  1792. end = start + list->len;
  1793. if ((copy = end - offset) > 0) {
  1794. if (copy > len)
  1795. copy = len;
  1796. elt += skb_to_sgvec(list, sg+elt, offset - start, copy);
  1797. if ((len -= copy) == 0)
  1798. return elt;
  1799. offset += copy;
  1800. }
  1801. start = end;
  1802. }
  1803. }
  1804. BUG_ON(len);
  1805. return elt;
  1806. }
  1807. /**
  1808. * skb_cow_data - Check that a socket buffer's data buffers are writable
  1809. * @skb: The socket buffer to check.
  1810. * @tailbits: Amount of trailing space to be added
  1811. * @trailer: Returned pointer to the skb where the @tailbits space begins
  1812. *
  1813. * Make sure that the data buffers attached to a socket buffer are
  1814. * writable. If they are not, private copies are made of the data buffers
  1815. * and the socket buffer is set to use these instead.
  1816. *
  1817. * If @tailbits is given, make sure that there is space to write @tailbits
  1818. * bytes of data beyond current end of socket buffer. @trailer will be
  1819. * set to point to the skb in which this space begins.
  1820. *
  1821. * The number of scatterlist elements required to completely map the
  1822. * COW'd and extended socket buffer will be returned.
  1823. */
  1824. int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
  1825. {
  1826. int copyflag;
  1827. int elt;
  1828. struct sk_buff *skb1, **skb_p;
  1829. /* If skb is cloned or its head is paged, reallocate
  1830. * head pulling out all the pages (pages are considered not writable
  1831. * at the moment even if they are anonymous).
  1832. */
  1833. if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
  1834. __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
  1835. return -ENOMEM;
  1836. /* Easy case. Most of packets will go this way. */
  1837. if (!skb_shinfo(skb)->frag_list) {
  1838. /* A little of trouble, not enough of space for trailer.
  1839. * This should not happen, when stack is tuned to generate
  1840. * good frames. OK, on miss we reallocate and reserve even more
  1841. * space, 128 bytes is fair. */
  1842. if (skb_tailroom(skb) < tailbits &&
  1843. pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
  1844. return -ENOMEM;
  1845. /* Voila! */
  1846. *trailer = skb;
  1847. return 1;
  1848. }
  1849. /* Misery. We are in troubles, going to mincer fragments... */
  1850. elt = 1;
  1851. skb_p = &skb_shinfo(skb)->frag_list;
  1852. copyflag = 0;
  1853. while ((skb1 = *skb_p) != NULL) {
  1854. int ntail = 0;
  1855. /* The fragment is partially pulled by someone,
  1856. * this can happen on input. Copy it and everything
  1857. * after it. */
  1858. if (skb_shared(skb1))
  1859. copyflag = 1;
  1860. /* If the skb is the last, worry about trailer. */
  1861. if (skb1->next == NULL && tailbits) {
  1862. if (skb_shinfo(skb1)->nr_frags ||
  1863. skb_shinfo(skb1)->frag_list ||
  1864. skb_tailroom(skb1) < tailbits)
  1865. ntail = tailbits + 128;
  1866. }
  1867. if (copyflag ||
  1868. skb_cloned(skb1) ||
  1869. ntail ||
  1870. skb_shinfo(skb1)->nr_frags ||
  1871. skb_shinfo(skb1)->frag_list) {
  1872. struct sk_buff *skb2;
  1873. /* Fuck, we are miserable poor guys... */
  1874. if (ntail == 0)
  1875. skb2 = skb_copy(skb1, GFP_ATOMIC);
  1876. else
  1877. skb2 = skb_copy_expand(skb1,
  1878. skb_headroom(skb1),
  1879. ntail,
  1880. GFP_ATOMIC);
  1881. if (unlikely(skb2 == NULL))
  1882. return -ENOMEM;
  1883. if (skb1->sk)
  1884. skb_set_owner_w(skb2, skb1->sk);
  1885. /* Looking around. Are we still alive?
  1886. * OK, link new skb, drop old one */
  1887. skb2->next = skb1->next;
  1888. *skb_p = skb2;
  1889. kfree_skb(skb1);
  1890. skb1 = skb2;
  1891. }
  1892. elt++;
  1893. *trailer = skb1;
  1894. skb_p = &skb1->next;
  1895. }
  1896. return elt;
  1897. }
  1898. EXPORT_SYMBOL(___pskb_trim);
  1899. EXPORT_SYMBOL(__kfree_skb);
  1900. EXPORT_SYMBOL(kfree_skb);
  1901. EXPORT_SYMBOL(__pskb_pull_tail);
  1902. EXPORT_SYMBOL(__alloc_skb);
  1903. EXPORT_SYMBOL(__netdev_alloc_skb);
  1904. EXPORT_SYMBOL(pskb_copy);
  1905. EXPORT_SYMBOL(pskb_expand_head);
  1906. EXPORT_SYMBOL(skb_checksum);
  1907. EXPORT_SYMBOL(skb_clone);
  1908. EXPORT_SYMBOL(skb_clone_fraglist);
  1909. EXPORT_SYMBOL(skb_copy);
  1910. EXPORT_SYMBOL(skb_copy_and_csum_bits);
  1911. EXPORT_SYMBOL(skb_copy_and_csum_dev);
  1912. EXPORT_SYMBOL(skb_copy_bits);
  1913. EXPORT_SYMBOL(skb_copy_expand);
  1914. EXPORT_SYMBOL(skb_over_panic);
  1915. EXPORT_SYMBOL(skb_pad);
  1916. EXPORT_SYMBOL(skb_realloc_headroom);
  1917. EXPORT_SYMBOL(skb_under_panic);
  1918. EXPORT_SYMBOL(skb_dequeue);
  1919. EXPORT_SYMBOL(skb_dequeue_tail);
  1920. EXPORT_SYMBOL(skb_insert);
  1921. EXPORT_SYMBOL(skb_queue_purge);
  1922. EXPORT_SYMBOL(skb_queue_head);
  1923. EXPORT_SYMBOL(skb_queue_tail);
  1924. EXPORT_SYMBOL(skb_unlink);
  1925. EXPORT_SYMBOL(skb_append);
  1926. EXPORT_SYMBOL(skb_split);
  1927. EXPORT_SYMBOL(skb_prepare_seq_read);
  1928. EXPORT_SYMBOL(skb_seq_read);
  1929. EXPORT_SYMBOL(skb_abort_seq_read);
  1930. EXPORT_SYMBOL(skb_find_text);
  1931. EXPORT_SYMBOL(skb_append_datato_frags);
  1932. EXPORT_SYMBOL_GPL(skb_to_sgvec);
  1933. EXPORT_SYMBOL_GPL(skb_cow_data);