skbuff.c 79 KB

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