skbuff.c 49 KB

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