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