skbuff.c 72 KB

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