skbuff.c 61 KB

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