skbuff.c 69 KB

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