skbuff.c 50 KB

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