skbuff.c 51 KB

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