skbuff.c 45 KB

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