skbuff.c 65 KB

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