skbuff.c 78 KB

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