skbuff.c 51 KB

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