skbuff.c 46 KB

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