skbuff.c 78 KB

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