skbuff.c 66 KB

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