skbuff.c 79 KB

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