skbuff.c 75 KB

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