skbuff.c 55 KB

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