skbuff.c 79 KB

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