skbuff.c 78 KB

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