skbuff.c 35 KB

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