skbuff.c 81 KB

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