skbuff.c 82 KB

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