skbuff.c 41 KB

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