skbuff.c 55 KB

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