skbuff.c 74 KB

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