skbuff.c 41 KB

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