skbuff.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253
  1. /*
  2. * Routines having to do with the 'struct sk_buff' memory handlers.
  3. *
  4. * Authors: Alan Cox <iiitac@pyr.swan.ac.uk>
  5. * Florian La Roche <rzsfl@rz.uni-sb.de>
  6. *
  7. * Version: $Id: skbuff.c,v 1.90 2001/11/07 05:56:19 davem Exp $
  8. *
  9. * Fixes:
  10. * Alan Cox : Fixed the worst of the load
  11. * balancer bugs.
  12. * Dave Platt : Interrupt stacking fix.
  13. * Richard Kooijman : Timestamp fixes.
  14. * Alan Cox : Changed buffer format.
  15. * Alan Cox : destructor hook for AF_UNIX etc.
  16. * Linus Torvalds : Better skb_clone.
  17. * Alan Cox : Added skb_copy.
  18. * Alan Cox : Added all the changed routines Linus
  19. * only put in the headers
  20. * Ray VanTassle : Fixed --skb->lock in free
  21. * Alan Cox : skb_copy copy arp field
  22. * Andi Kleen : slabified it.
  23. * Robert Olsson : Removed skb_head_pool
  24. *
  25. * NOTE:
  26. * The __skb_ routines should be called with interrupts
  27. * disabled, or you better be *real* sure that the operation is atomic
  28. * with respect to whatever list is being frobbed (e.g. via lock_sock()
  29. * or via disabling bottom half handlers, etc).
  30. *
  31. * This program is free software; you can redistribute it and/or
  32. * modify it under the terms of the GNU General Public License
  33. * as published by the Free Software Foundation; either version
  34. * 2 of the License, or (at your option) any later version.
  35. */
  36. /*
  37. * The functions in this file will not compile correctly with gcc 2.4.x
  38. */
  39. #include <linux/module.h>
  40. #include <linux/types.h>
  41. #include <linux/kernel.h>
  42. #include <linux/mm.h>
  43. #include <linux/interrupt.h>
  44. #include <linux/in.h>
  45. #include <linux/inet.h>
  46. #include <linux/slab.h>
  47. #include <linux/netdevice.h>
  48. #ifdef CONFIG_NET_CLS_ACT
  49. #include <net/pkt_sched.h>
  50. #endif
  51. #include <linux/string.h>
  52. #include <linux/skbuff.h>
  53. #include <linux/cache.h>
  54. #include <linux/rtnetlink.h>
  55. #include <linux/init.h>
  56. #include <linux/scatterlist.h>
  57. #include <net/protocol.h>
  58. #include <net/dst.h>
  59. #include <net/sock.h>
  60. #include <net/checksum.h>
  61. #include <net/xfrm.h>
  62. #include <asm/uaccess.h>
  63. #include <asm/system.h>
  64. #include "kmap_skb.h"
  65. static struct kmem_cache *skbuff_head_cache __read_mostly;
  66. static struct kmem_cache *skbuff_fclone_cache __read_mostly;
  67. /*
  68. * Keep out-of-line to prevent kernel bloat.
  69. * __builtin_return_address is not used because it is not always
  70. * reliable.
  71. */
  72. /**
  73. * skb_over_panic - private function
  74. * @skb: buffer
  75. * @sz: size
  76. * @here: address
  77. *
  78. * Out of line support code for skb_put(). Not user callable.
  79. */
  80. void skb_over_panic(struct sk_buff *skb, int sz, void *here)
  81. {
  82. printk(KERN_EMERG "skb_over_panic: text:%p len:%d put:%d head:%p "
  83. "data:%p tail:%#lx end:%#lx dev:%s\n",
  84. here, skb->len, sz, skb->head, skb->data,
  85. (unsigned long)skb->tail, (unsigned long)skb->end,
  86. skb->dev ? skb->dev->name : "<NULL>");
  87. BUG();
  88. }
  89. /**
  90. * skb_under_panic - private function
  91. * @skb: buffer
  92. * @sz: size
  93. * @here: address
  94. *
  95. * Out of line support code for skb_push(). Not user callable.
  96. */
  97. void skb_under_panic(struct sk_buff *skb, int sz, void *here)
  98. {
  99. printk(KERN_EMERG "skb_under_panic: text:%p len:%d put:%d head:%p "
  100. "data:%p tail:%#lx end:%#lx dev:%s\n",
  101. here, skb->len, sz, skb->head, skb->data,
  102. (unsigned long)skb->tail, (unsigned long)skb->end,
  103. skb->dev ? skb->dev->name : "<NULL>");
  104. BUG();
  105. }
  106. void skb_truesize_bug(struct sk_buff *skb)
  107. {
  108. printk(KERN_ERR "SKB BUG: Invalid truesize (%u) "
  109. "len=%u, sizeof(sk_buff)=%Zd\n",
  110. skb->truesize, skb->len, sizeof(struct sk_buff));
  111. }
  112. EXPORT_SYMBOL(skb_truesize_bug);
  113. /* Allocate a new skbuff. We do this ourselves so we can fill in a few
  114. * 'private' fields and also do memory statistics to find all the
  115. * [BEEP] leaks.
  116. *
  117. */
  118. /**
  119. * __alloc_skb - allocate a network buffer
  120. * @size: size to allocate
  121. * @gfp_mask: allocation mask
  122. * @fclone: allocate from fclone cache instead of head cache
  123. * and allocate a cloned (child) skb
  124. * @node: numa node to allocate memory on
  125. *
  126. * Allocate a new &sk_buff. The returned buffer has no headroom and a
  127. * tail room of size bytes. The object has a reference count of one.
  128. * The return is the buffer. On a failure the return is %NULL.
  129. *
  130. * Buffers may only be allocated from interrupts using a @gfp_mask of
  131. * %GFP_ATOMIC.
  132. */
  133. struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
  134. int fclone, int node)
  135. {
  136. struct kmem_cache *cache;
  137. struct skb_shared_info *shinfo;
  138. struct sk_buff *skb;
  139. u8 *data;
  140. cache = fclone ? skbuff_fclone_cache : skbuff_head_cache;
  141. /* Get the HEAD */
  142. skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
  143. if (!skb)
  144. goto out;
  145. size = SKB_DATA_ALIGN(size);
  146. data = kmalloc_node_track_caller(size + sizeof(struct skb_shared_info),
  147. gfp_mask, node);
  148. if (!data)
  149. goto nodata;
  150. /*
  151. * See comment in sk_buff definition, just before the 'tail' member
  152. */
  153. memset(skb, 0, offsetof(struct sk_buff, tail));
  154. skb->truesize = size + sizeof(struct sk_buff);
  155. atomic_set(&skb->users, 1);
  156. skb->head = data;
  157. skb->data = data;
  158. skb_reset_tail_pointer(skb);
  159. skb->end = skb->tail + size;
  160. /* make sure we initialize shinfo sequentially */
  161. shinfo = skb_shinfo(skb);
  162. atomic_set(&shinfo->dataref, 1);
  163. shinfo->nr_frags = 0;
  164. shinfo->gso_size = 0;
  165. shinfo->gso_segs = 0;
  166. shinfo->gso_type = 0;
  167. shinfo->ip6_frag_id = 0;
  168. shinfo->frag_list = NULL;
  169. if (fclone) {
  170. struct sk_buff *child = skb + 1;
  171. atomic_t *fclone_ref = (atomic_t *) (child + 1);
  172. skb->fclone = SKB_FCLONE_ORIG;
  173. atomic_set(fclone_ref, 1);
  174. child->fclone = SKB_FCLONE_UNAVAILABLE;
  175. }
  176. out:
  177. return skb;
  178. nodata:
  179. kmem_cache_free(cache, skb);
  180. skb = NULL;
  181. goto out;
  182. }
  183. /**
  184. * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
  185. * @dev: network device to receive on
  186. * @length: length to allocate
  187. * @gfp_mask: get_free_pages mask, passed to alloc_skb
  188. *
  189. * Allocate a new &sk_buff and assign it a usage count of one. The
  190. * buffer has unspecified headroom built in. Users should allocate
  191. * the headroom they think they need without accounting for the
  192. * built in space. The built in space is used for optimisations.
  193. *
  194. * %NULL is returned if there is no free memory.
  195. */
  196. struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
  197. unsigned int length, gfp_t gfp_mask)
  198. {
  199. int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
  200. struct sk_buff *skb;
  201. skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask, 0, node);
  202. if (likely(skb)) {
  203. skb_reserve(skb, NET_SKB_PAD);
  204. skb->dev = dev;
  205. }
  206. return skb;
  207. }
  208. static void skb_drop_list(struct sk_buff **listp)
  209. {
  210. struct sk_buff *list = *listp;
  211. *listp = NULL;
  212. do {
  213. struct sk_buff *this = list;
  214. list = list->next;
  215. kfree_skb(this);
  216. } while (list);
  217. }
  218. static inline void skb_drop_fraglist(struct sk_buff *skb)
  219. {
  220. skb_drop_list(&skb_shinfo(skb)->frag_list);
  221. }
  222. static void skb_clone_fraglist(struct sk_buff *skb)
  223. {
  224. struct sk_buff *list;
  225. for (list = skb_shinfo(skb)->frag_list; list; list = list->next)
  226. skb_get(list);
  227. }
  228. static void skb_release_data(struct sk_buff *skb)
  229. {
  230. if (!skb->cloned ||
  231. !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
  232. &skb_shinfo(skb)->dataref)) {
  233. if (skb_shinfo(skb)->nr_frags) {
  234. int i;
  235. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
  236. put_page(skb_shinfo(skb)->frags[i].page);
  237. }
  238. if (skb_shinfo(skb)->frag_list)
  239. skb_drop_fraglist(skb);
  240. kfree(skb->head);
  241. }
  242. }
  243. /*
  244. * Free an skbuff by memory without cleaning the state.
  245. */
  246. static void kfree_skbmem(struct sk_buff *skb)
  247. {
  248. struct sk_buff *other;
  249. atomic_t *fclone_ref;
  250. switch (skb->fclone) {
  251. case SKB_FCLONE_UNAVAILABLE:
  252. kmem_cache_free(skbuff_head_cache, skb);
  253. break;
  254. case SKB_FCLONE_ORIG:
  255. fclone_ref = (atomic_t *) (skb + 2);
  256. if (atomic_dec_and_test(fclone_ref))
  257. kmem_cache_free(skbuff_fclone_cache, skb);
  258. break;
  259. case SKB_FCLONE_CLONE:
  260. fclone_ref = (atomic_t *) (skb + 1);
  261. other = skb - 1;
  262. /* The clone portion is available for
  263. * fast-cloning again.
  264. */
  265. skb->fclone = SKB_FCLONE_UNAVAILABLE;
  266. if (atomic_dec_and_test(fclone_ref))
  267. kmem_cache_free(skbuff_fclone_cache, other);
  268. break;
  269. }
  270. }
  271. /* Free everything but the sk_buff shell. */
  272. static void skb_release_all(struct sk_buff *skb)
  273. {
  274. dst_release(skb->dst);
  275. #ifdef CONFIG_XFRM
  276. secpath_put(skb->sp);
  277. #endif
  278. if (skb->destructor) {
  279. WARN_ON(in_irq());
  280. skb->destructor(skb);
  281. }
  282. #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
  283. nf_conntrack_put(skb->nfct);
  284. nf_conntrack_put_reasm(skb->nfct_reasm);
  285. #endif
  286. #ifdef CONFIG_BRIDGE_NETFILTER
  287. nf_bridge_put(skb->nf_bridge);
  288. #endif
  289. /* XXX: IS this still necessary? - JHS */
  290. #ifdef CONFIG_NET_SCHED
  291. skb->tc_index = 0;
  292. #ifdef CONFIG_NET_CLS_ACT
  293. skb->tc_verd = 0;
  294. #endif
  295. #endif
  296. skb_release_data(skb);
  297. }
  298. /**
  299. * __kfree_skb - private function
  300. * @skb: buffer
  301. *
  302. * Free an sk_buff. Release anything attached to the buffer.
  303. * Clean the state. This is an internal helper function. Users should
  304. * always call kfree_skb
  305. */
  306. void __kfree_skb(struct sk_buff *skb)
  307. {
  308. skb_release_all(skb);
  309. kfree_skbmem(skb);
  310. }
  311. /**
  312. * kfree_skb - free an sk_buff
  313. * @skb: buffer to free
  314. *
  315. * Drop a reference to the buffer and free it if the usage count has
  316. * hit zero.
  317. */
  318. void kfree_skb(struct sk_buff *skb)
  319. {
  320. if (unlikely(!skb))
  321. return;
  322. if (likely(atomic_read(&skb->users) == 1))
  323. smp_rmb();
  324. else if (likely(!atomic_dec_and_test(&skb->users)))
  325. return;
  326. __kfree_skb(skb);
  327. }
  328. static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
  329. {
  330. new->tstamp = old->tstamp;
  331. new->dev = old->dev;
  332. new->transport_header = old->transport_header;
  333. new->network_header = old->network_header;
  334. new->mac_header = old->mac_header;
  335. new->dst = dst_clone(old->dst);
  336. #ifdef CONFIG_INET
  337. new->sp = secpath_get(old->sp);
  338. #endif
  339. memcpy(new->cb, old->cb, sizeof(old->cb));
  340. new->csum_start = old->csum_start;
  341. new->csum_offset = old->csum_offset;
  342. new->local_df = old->local_df;
  343. new->pkt_type = old->pkt_type;
  344. new->ip_summed = old->ip_summed;
  345. skb_copy_queue_mapping(new, old);
  346. new->priority = old->priority;
  347. #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
  348. new->ipvs_property = old->ipvs_property;
  349. #endif
  350. new->protocol = old->protocol;
  351. new->mark = old->mark;
  352. __nf_copy(new, old);
  353. #if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
  354. defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)
  355. new->nf_trace = old->nf_trace;
  356. #endif
  357. #ifdef CONFIG_NET_SCHED
  358. new->tc_index = old->tc_index;
  359. #ifdef CONFIG_NET_CLS_ACT
  360. new->tc_verd = old->tc_verd;
  361. #endif
  362. #endif
  363. skb_copy_secmark(new, old);
  364. }
  365. static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
  366. {
  367. #define C(x) n->x = skb->x
  368. n->next = n->prev = NULL;
  369. n->sk = NULL;
  370. __copy_skb_header(n, skb);
  371. C(len);
  372. C(data_len);
  373. C(mac_len);
  374. n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
  375. n->cloned = 1;
  376. n->nohdr = 0;
  377. n->destructor = NULL;
  378. C(iif);
  379. C(tail);
  380. C(end);
  381. C(head);
  382. C(data);
  383. C(truesize);
  384. atomic_set(&n->users, 1);
  385. atomic_inc(&(skb_shinfo(skb)->dataref));
  386. skb->cloned = 1;
  387. return n;
  388. #undef C
  389. }
  390. /**
  391. * skb_morph - morph one skb into another
  392. * @dst: the skb to receive the contents
  393. * @src: the skb to supply the contents
  394. *
  395. * This is identical to skb_clone except that the target skb is
  396. * supplied by the user.
  397. *
  398. * The target skb is returned upon exit.
  399. */
  400. struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
  401. {
  402. skb_release_all(dst);
  403. return __skb_clone(dst, src);
  404. }
  405. EXPORT_SYMBOL_GPL(skb_morph);
  406. /**
  407. * skb_clone - duplicate an sk_buff
  408. * @skb: buffer to clone
  409. * @gfp_mask: allocation priority
  410. *
  411. * Duplicate an &sk_buff. The new one is not owned by a socket. Both
  412. * copies share the same packet data but not structure. The new
  413. * buffer has a reference count of 1. If the allocation fails the
  414. * function returns %NULL otherwise the new buffer is returned.
  415. *
  416. * If this function is called from an interrupt gfp_mask() must be
  417. * %GFP_ATOMIC.
  418. */
  419. struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
  420. {
  421. struct sk_buff *n;
  422. n = skb + 1;
  423. if (skb->fclone == SKB_FCLONE_ORIG &&
  424. n->fclone == SKB_FCLONE_UNAVAILABLE) {
  425. atomic_t *fclone_ref = (atomic_t *) (n + 1);
  426. n->fclone = SKB_FCLONE_CLONE;
  427. atomic_inc(fclone_ref);
  428. } else {
  429. n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
  430. if (!n)
  431. return NULL;
  432. n->fclone = SKB_FCLONE_UNAVAILABLE;
  433. }
  434. return __skb_clone(n, skb);
  435. }
  436. static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
  437. {
  438. #ifndef NET_SKBUFF_DATA_USES_OFFSET
  439. /*
  440. * Shift between the two data areas in bytes
  441. */
  442. unsigned long offset = new->data - old->data;
  443. #endif
  444. __copy_skb_header(new, old);
  445. #ifndef NET_SKBUFF_DATA_USES_OFFSET
  446. /* {transport,network,mac}_header are relative to skb->head */
  447. new->transport_header += offset;
  448. new->network_header += offset;
  449. new->mac_header += offset;
  450. #endif
  451. skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
  452. skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
  453. skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
  454. }
  455. /**
  456. * skb_copy - create private copy of an sk_buff
  457. * @skb: buffer to copy
  458. * @gfp_mask: allocation priority
  459. *
  460. * Make a copy of both an &sk_buff and its data. This is used when the
  461. * caller wishes to modify the data and needs a private copy of the
  462. * data to alter. Returns %NULL on failure or the pointer to the buffer
  463. * on success. The returned buffer has a reference count of 1.
  464. *
  465. * As by-product this function converts non-linear &sk_buff to linear
  466. * one, so that &sk_buff becomes completely private and caller is allowed
  467. * to modify all the data of returned buffer. This means that this
  468. * function is not recommended for use in circumstances when only
  469. * header is going to be modified. Use pskb_copy() instead.
  470. */
  471. struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
  472. {
  473. int headerlen = skb->data - skb->head;
  474. /*
  475. * Allocate the copy buffer
  476. */
  477. struct sk_buff *n;
  478. #ifdef NET_SKBUFF_DATA_USES_OFFSET
  479. n = alloc_skb(skb->end + skb->data_len, gfp_mask);
  480. #else
  481. n = alloc_skb(skb->end - skb->head + skb->data_len, gfp_mask);
  482. #endif
  483. if (!n)
  484. return NULL;
  485. /* Set the data pointer */
  486. skb_reserve(n, headerlen);
  487. /* Set the tail pointer and length */
  488. skb_put(n, skb->len);
  489. if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
  490. BUG();
  491. copy_skb_header(n, skb);
  492. return n;
  493. }
  494. /**
  495. * pskb_copy - create copy of an sk_buff with private head.
  496. * @skb: buffer to copy
  497. * @gfp_mask: allocation priority
  498. *
  499. * Make a copy of both an &sk_buff and part of its data, located
  500. * in header. Fragmented data remain shared. This is used when
  501. * the caller wishes to modify only header of &sk_buff and needs
  502. * private copy of the header to alter. Returns %NULL on failure
  503. * or the pointer to the buffer on success.
  504. * The returned buffer has a reference count of 1.
  505. */
  506. struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask)
  507. {
  508. /*
  509. * Allocate the copy buffer
  510. */
  511. struct sk_buff *n;
  512. #ifdef NET_SKBUFF_DATA_USES_OFFSET
  513. n = alloc_skb(skb->end, gfp_mask);
  514. #else
  515. n = alloc_skb(skb->end - skb->head, gfp_mask);
  516. #endif
  517. if (!n)
  518. goto out;
  519. /* Set the data pointer */
  520. skb_reserve(n, skb->data - skb->head);
  521. /* Set the tail pointer and length */
  522. skb_put(n, skb_headlen(skb));
  523. /* Copy the bytes */
  524. skb_copy_from_linear_data(skb, n->data, n->len);
  525. n->truesize += skb->data_len;
  526. n->data_len = skb->data_len;
  527. n->len = skb->len;
  528. if (skb_shinfo(skb)->nr_frags) {
  529. int i;
  530. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  531. skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
  532. get_page(skb_shinfo(n)->frags[i].page);
  533. }
  534. skb_shinfo(n)->nr_frags = i;
  535. }
  536. if (skb_shinfo(skb)->frag_list) {
  537. skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
  538. skb_clone_fraglist(n);
  539. }
  540. copy_skb_header(n, skb);
  541. out:
  542. return n;
  543. }
  544. /**
  545. * pskb_expand_head - reallocate header of &sk_buff
  546. * @skb: buffer to reallocate
  547. * @nhead: room to add at head
  548. * @ntail: room to add at tail
  549. * @gfp_mask: allocation priority
  550. *
  551. * Expands (or creates identical copy, if &nhead and &ntail are zero)
  552. * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
  553. * reference count of 1. Returns zero in the case of success or error,
  554. * if expansion failed. In the last case, &sk_buff is not changed.
  555. *
  556. * All the pointers pointing into skb header may change and must be
  557. * reloaded after call to this function.
  558. */
  559. int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
  560. gfp_t gfp_mask)
  561. {
  562. int i;
  563. u8 *data;
  564. #ifdef NET_SKBUFF_DATA_USES_OFFSET
  565. int size = nhead + skb->end + ntail;
  566. #else
  567. int size = nhead + (skb->end - skb->head) + ntail;
  568. #endif
  569. long off;
  570. if (skb_shared(skb))
  571. BUG();
  572. size = SKB_DATA_ALIGN(size);
  573. data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
  574. if (!data)
  575. goto nodata;
  576. /* Copy only real data... and, alas, header. This should be
  577. * optimized for the cases when header is void. */
  578. #ifdef NET_SKBUFF_DATA_USES_OFFSET
  579. memcpy(data + nhead, skb->head, skb->tail);
  580. #else
  581. memcpy(data + nhead, skb->head, skb->tail - skb->head);
  582. #endif
  583. memcpy(data + size, skb_end_pointer(skb),
  584. sizeof(struct skb_shared_info));
  585. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
  586. get_page(skb_shinfo(skb)->frags[i].page);
  587. if (skb_shinfo(skb)->frag_list)
  588. skb_clone_fraglist(skb);
  589. skb_release_data(skb);
  590. off = (data + nhead) - skb->head;
  591. skb->head = data;
  592. skb->data += off;
  593. #ifdef NET_SKBUFF_DATA_USES_OFFSET
  594. skb->end = size;
  595. off = nhead;
  596. #else
  597. skb->end = skb->head + size;
  598. #endif
  599. /* {transport,network,mac}_header and tail are relative to skb->head */
  600. skb->tail += off;
  601. skb->transport_header += off;
  602. skb->network_header += off;
  603. skb->mac_header += off;
  604. skb->csum_start += nhead;
  605. skb->cloned = 0;
  606. skb->hdr_len = 0;
  607. skb->nohdr = 0;
  608. atomic_set(&skb_shinfo(skb)->dataref, 1);
  609. return 0;
  610. nodata:
  611. return -ENOMEM;
  612. }
  613. /* Make private copy of skb with writable head and some headroom */
  614. struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
  615. {
  616. struct sk_buff *skb2;
  617. int delta = headroom - skb_headroom(skb);
  618. if (delta <= 0)
  619. skb2 = pskb_copy(skb, GFP_ATOMIC);
  620. else {
  621. skb2 = skb_clone(skb, GFP_ATOMIC);
  622. if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
  623. GFP_ATOMIC)) {
  624. kfree_skb(skb2);
  625. skb2 = NULL;
  626. }
  627. }
  628. return skb2;
  629. }
  630. /**
  631. * skb_copy_expand - copy and expand sk_buff
  632. * @skb: buffer to copy
  633. * @newheadroom: new free bytes at head
  634. * @newtailroom: new free bytes at tail
  635. * @gfp_mask: allocation priority
  636. *
  637. * Make a copy of both an &sk_buff and its data and while doing so
  638. * allocate additional space.
  639. *
  640. * This is used when the caller wishes to modify the data and needs a
  641. * private copy of the data to alter as well as more space for new fields.
  642. * Returns %NULL on failure or the pointer to the buffer
  643. * on success. The returned buffer has a reference count of 1.
  644. *
  645. * You must pass %GFP_ATOMIC as the allocation priority if this function
  646. * is called from an interrupt.
  647. */
  648. struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
  649. int newheadroom, int newtailroom,
  650. gfp_t gfp_mask)
  651. {
  652. /*
  653. * Allocate the copy buffer
  654. */
  655. struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
  656. gfp_mask);
  657. int oldheadroom = skb_headroom(skb);
  658. int head_copy_len, head_copy_off;
  659. int off;
  660. if (!n)
  661. return NULL;
  662. skb_reserve(n, newheadroom);
  663. /* Set the tail pointer and length */
  664. skb_put(n, skb->len);
  665. head_copy_len = oldheadroom;
  666. head_copy_off = 0;
  667. if (newheadroom <= head_copy_len)
  668. head_copy_len = newheadroom;
  669. else
  670. head_copy_off = newheadroom - head_copy_len;
  671. /* Copy the linear header and data. */
  672. if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
  673. skb->len + head_copy_len))
  674. BUG();
  675. copy_skb_header(n, skb);
  676. off = newheadroom - oldheadroom;
  677. n->csum_start += off;
  678. #ifdef NET_SKBUFF_DATA_USES_OFFSET
  679. n->transport_header += off;
  680. n->network_header += off;
  681. n->mac_header += off;
  682. #endif
  683. return n;
  684. }
  685. /**
  686. * skb_pad - zero pad the tail of an skb
  687. * @skb: buffer to pad
  688. * @pad: space to pad
  689. *
  690. * Ensure that a buffer is followed by a padding area that is zero
  691. * filled. Used by network drivers which may DMA or transfer data
  692. * beyond the buffer end onto the wire.
  693. *
  694. * May return error in out of memory cases. The skb is freed on error.
  695. */
  696. int skb_pad(struct sk_buff *skb, int pad)
  697. {
  698. int err;
  699. int ntail;
  700. /* If the skbuff is non linear tailroom is always zero.. */
  701. if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
  702. memset(skb->data+skb->len, 0, pad);
  703. return 0;
  704. }
  705. ntail = skb->data_len + pad - (skb->end - skb->tail);
  706. if (likely(skb_cloned(skb) || ntail > 0)) {
  707. err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
  708. if (unlikely(err))
  709. goto free_skb;
  710. }
  711. /* FIXME: The use of this function with non-linear skb's really needs
  712. * to be audited.
  713. */
  714. err = skb_linearize(skb);
  715. if (unlikely(err))
  716. goto free_skb;
  717. memset(skb->data + skb->len, 0, pad);
  718. return 0;
  719. free_skb:
  720. kfree_skb(skb);
  721. return err;
  722. }
  723. /* Trims skb to length len. It can change skb pointers.
  724. */
  725. int ___pskb_trim(struct sk_buff *skb, unsigned int len)
  726. {
  727. struct sk_buff **fragp;
  728. struct sk_buff *frag;
  729. int offset = skb_headlen(skb);
  730. int nfrags = skb_shinfo(skb)->nr_frags;
  731. int i;
  732. int err;
  733. if (skb_cloned(skb) &&
  734. unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
  735. return err;
  736. i = 0;
  737. if (offset >= len)
  738. goto drop_pages;
  739. for (; i < nfrags; i++) {
  740. int end = offset + skb_shinfo(skb)->frags[i].size;
  741. if (end < len) {
  742. offset = end;
  743. continue;
  744. }
  745. skb_shinfo(skb)->frags[i++].size = len - offset;
  746. drop_pages:
  747. skb_shinfo(skb)->nr_frags = i;
  748. for (; i < nfrags; i++)
  749. put_page(skb_shinfo(skb)->frags[i].page);
  750. if (skb_shinfo(skb)->frag_list)
  751. skb_drop_fraglist(skb);
  752. goto done;
  753. }
  754. for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
  755. fragp = &frag->next) {
  756. int end = offset + frag->len;
  757. if (skb_shared(frag)) {
  758. struct sk_buff *nfrag;
  759. nfrag = skb_clone(frag, GFP_ATOMIC);
  760. if (unlikely(!nfrag))
  761. return -ENOMEM;
  762. nfrag->next = frag->next;
  763. kfree_skb(frag);
  764. frag = nfrag;
  765. *fragp = frag;
  766. }
  767. if (end < len) {
  768. offset = end;
  769. continue;
  770. }
  771. if (end > len &&
  772. unlikely((err = pskb_trim(frag, len - offset))))
  773. return err;
  774. if (frag->next)
  775. skb_drop_list(&frag->next);
  776. break;
  777. }
  778. done:
  779. if (len > skb_headlen(skb)) {
  780. skb->data_len -= skb->len - len;
  781. skb->len = len;
  782. } else {
  783. skb->len = len;
  784. skb->data_len = 0;
  785. skb_set_tail_pointer(skb, len);
  786. }
  787. return 0;
  788. }
  789. /**
  790. * __pskb_pull_tail - advance tail of skb header
  791. * @skb: buffer to reallocate
  792. * @delta: number of bytes to advance tail
  793. *
  794. * The function makes a sense only on a fragmented &sk_buff,
  795. * it expands header moving its tail forward and copying necessary
  796. * data from fragmented part.
  797. *
  798. * &sk_buff MUST have reference count of 1.
  799. *
  800. * Returns %NULL (and &sk_buff does not change) if pull failed
  801. * or value of new tail of skb in the case of success.
  802. *
  803. * All the pointers pointing into skb header may change and must be
  804. * reloaded after call to this function.
  805. */
  806. /* Moves tail of skb head forward, copying data from fragmented part,
  807. * when it is necessary.
  808. * 1. It may fail due to malloc failure.
  809. * 2. It may change skb pointers.
  810. *
  811. * It is pretty complicated. Luckily, it is called only in exceptional cases.
  812. */
  813. unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
  814. {
  815. /* If skb has not enough free space at tail, get new one
  816. * plus 128 bytes for future expansions. If we have enough
  817. * room at tail, reallocate without expansion only if skb is cloned.
  818. */
  819. int i, k, eat = (skb->tail + delta) - skb->end;
  820. if (eat > 0 || skb_cloned(skb)) {
  821. if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
  822. GFP_ATOMIC))
  823. return NULL;
  824. }
  825. if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
  826. BUG();
  827. /* Optimization: no fragments, no reasons to preestimate
  828. * size of pulled pages. Superb.
  829. */
  830. if (!skb_shinfo(skb)->frag_list)
  831. goto pull_pages;
  832. /* Estimate size of pulled pages. */
  833. eat = delta;
  834. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  835. if (skb_shinfo(skb)->frags[i].size >= eat)
  836. goto pull_pages;
  837. eat -= skb_shinfo(skb)->frags[i].size;
  838. }
  839. /* If we need update frag list, we are in troubles.
  840. * Certainly, it possible to add an offset to skb data,
  841. * but taking into account that pulling is expected to
  842. * be very rare operation, it is worth to fight against
  843. * further bloating skb head and crucify ourselves here instead.
  844. * Pure masohism, indeed. 8)8)
  845. */
  846. if (eat) {
  847. struct sk_buff *list = skb_shinfo(skb)->frag_list;
  848. struct sk_buff *clone = NULL;
  849. struct sk_buff *insp = NULL;
  850. do {
  851. BUG_ON(!list);
  852. if (list->len <= eat) {
  853. /* Eaten as whole. */
  854. eat -= list->len;
  855. list = list->next;
  856. insp = list;
  857. } else {
  858. /* Eaten partially. */
  859. if (skb_shared(list)) {
  860. /* Sucks! We need to fork list. :-( */
  861. clone = skb_clone(list, GFP_ATOMIC);
  862. if (!clone)
  863. return NULL;
  864. insp = list->next;
  865. list = clone;
  866. } else {
  867. /* This may be pulled without
  868. * problems. */
  869. insp = list;
  870. }
  871. if (!pskb_pull(list, eat)) {
  872. if (clone)
  873. kfree_skb(clone);
  874. return NULL;
  875. }
  876. break;
  877. }
  878. } while (eat);
  879. /* Free pulled out fragments. */
  880. while ((list = skb_shinfo(skb)->frag_list) != insp) {
  881. skb_shinfo(skb)->frag_list = list->next;
  882. kfree_skb(list);
  883. }
  884. /* And insert new clone at head. */
  885. if (clone) {
  886. clone->next = list;
  887. skb_shinfo(skb)->frag_list = clone;
  888. }
  889. }
  890. /* Success! Now we may commit changes to skb data. */
  891. pull_pages:
  892. eat = delta;
  893. k = 0;
  894. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  895. if (skb_shinfo(skb)->frags[i].size <= eat) {
  896. put_page(skb_shinfo(skb)->frags[i].page);
  897. eat -= skb_shinfo(skb)->frags[i].size;
  898. } else {
  899. skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
  900. if (eat) {
  901. skb_shinfo(skb)->frags[k].page_offset += eat;
  902. skb_shinfo(skb)->frags[k].size -= eat;
  903. eat = 0;
  904. }
  905. k++;
  906. }
  907. }
  908. skb_shinfo(skb)->nr_frags = k;
  909. skb->tail += delta;
  910. skb->data_len -= delta;
  911. return skb_tail_pointer(skb);
  912. }
  913. /* Copy some data bits from skb to kernel buffer. */
  914. int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
  915. {
  916. int i, copy;
  917. int start = skb_headlen(skb);
  918. if (offset > (int)skb->len - len)
  919. goto fault;
  920. /* Copy header. */
  921. if ((copy = start - offset) > 0) {
  922. if (copy > len)
  923. copy = len;
  924. skb_copy_from_linear_data_offset(skb, offset, to, copy);
  925. if ((len -= copy) == 0)
  926. return 0;
  927. offset += copy;
  928. to += copy;
  929. }
  930. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  931. int end;
  932. BUG_TRAP(start <= offset + len);
  933. end = start + skb_shinfo(skb)->frags[i].size;
  934. if ((copy = end - offset) > 0) {
  935. u8 *vaddr;
  936. if (copy > len)
  937. copy = len;
  938. vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
  939. memcpy(to,
  940. vaddr + skb_shinfo(skb)->frags[i].page_offset+
  941. offset - start, copy);
  942. kunmap_skb_frag(vaddr);
  943. if ((len -= copy) == 0)
  944. return 0;
  945. offset += copy;
  946. to += copy;
  947. }
  948. start = end;
  949. }
  950. if (skb_shinfo(skb)->frag_list) {
  951. struct sk_buff *list = skb_shinfo(skb)->frag_list;
  952. for (; list; list = list->next) {
  953. int end;
  954. BUG_TRAP(start <= offset + len);
  955. end = start + list->len;
  956. if ((copy = end - offset) > 0) {
  957. if (copy > len)
  958. copy = len;
  959. if (skb_copy_bits(list, offset - start,
  960. to, copy))
  961. goto fault;
  962. if ((len -= copy) == 0)
  963. return 0;
  964. offset += copy;
  965. to += copy;
  966. }
  967. start = end;
  968. }
  969. }
  970. if (!len)
  971. return 0;
  972. fault:
  973. return -EFAULT;
  974. }
  975. /**
  976. * skb_store_bits - store bits from kernel buffer to skb
  977. * @skb: destination buffer
  978. * @offset: offset in destination
  979. * @from: source buffer
  980. * @len: number of bytes to copy
  981. *
  982. * Copy the specified number of bytes from the source buffer to the
  983. * destination skb. This function handles all the messy bits of
  984. * traversing fragment lists and such.
  985. */
  986. int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
  987. {
  988. int i, copy;
  989. int start = skb_headlen(skb);
  990. if (offset > (int)skb->len - len)
  991. goto fault;
  992. if ((copy = start - offset) > 0) {
  993. if (copy > len)
  994. copy = len;
  995. skb_copy_to_linear_data_offset(skb, offset, from, copy);
  996. if ((len -= copy) == 0)
  997. return 0;
  998. offset += copy;
  999. from += copy;
  1000. }
  1001. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1002. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  1003. int end;
  1004. BUG_TRAP(start <= offset + len);
  1005. end = start + frag->size;
  1006. if ((copy = end - offset) > 0) {
  1007. u8 *vaddr;
  1008. if (copy > len)
  1009. copy = len;
  1010. vaddr = kmap_skb_frag(frag);
  1011. memcpy(vaddr + frag->page_offset + offset - start,
  1012. from, copy);
  1013. kunmap_skb_frag(vaddr);
  1014. if ((len -= copy) == 0)
  1015. return 0;
  1016. offset += copy;
  1017. from += copy;
  1018. }
  1019. start = end;
  1020. }
  1021. if (skb_shinfo(skb)->frag_list) {
  1022. struct sk_buff *list = skb_shinfo(skb)->frag_list;
  1023. for (; list; list = list->next) {
  1024. int end;
  1025. BUG_TRAP(start <= offset + len);
  1026. end = start + list->len;
  1027. if ((copy = end - offset) > 0) {
  1028. if (copy > len)
  1029. copy = len;
  1030. if (skb_store_bits(list, offset - start,
  1031. from, copy))
  1032. goto fault;
  1033. if ((len -= copy) == 0)
  1034. return 0;
  1035. offset += copy;
  1036. from += copy;
  1037. }
  1038. start = end;
  1039. }
  1040. }
  1041. if (!len)
  1042. return 0;
  1043. fault:
  1044. return -EFAULT;
  1045. }
  1046. EXPORT_SYMBOL(skb_store_bits);
  1047. /* Checksum skb data. */
  1048. __wsum skb_checksum(const struct sk_buff *skb, int offset,
  1049. int len, __wsum csum)
  1050. {
  1051. int start = skb_headlen(skb);
  1052. int i, copy = start - offset;
  1053. int pos = 0;
  1054. /* Checksum header. */
  1055. if (copy > 0) {
  1056. if (copy > len)
  1057. copy = len;
  1058. csum = csum_partial(skb->data + offset, copy, csum);
  1059. if ((len -= copy) == 0)
  1060. return csum;
  1061. offset += copy;
  1062. pos = copy;
  1063. }
  1064. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1065. int end;
  1066. BUG_TRAP(start <= offset + len);
  1067. end = start + skb_shinfo(skb)->frags[i].size;
  1068. if ((copy = end - offset) > 0) {
  1069. __wsum csum2;
  1070. u8 *vaddr;
  1071. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  1072. if (copy > len)
  1073. copy = len;
  1074. vaddr = kmap_skb_frag(frag);
  1075. csum2 = csum_partial(vaddr + frag->page_offset +
  1076. offset - start, copy, 0);
  1077. kunmap_skb_frag(vaddr);
  1078. csum = csum_block_add(csum, csum2, pos);
  1079. if (!(len -= copy))
  1080. return csum;
  1081. offset += copy;
  1082. pos += copy;
  1083. }
  1084. start = end;
  1085. }
  1086. if (skb_shinfo(skb)->frag_list) {
  1087. struct sk_buff *list = skb_shinfo(skb)->frag_list;
  1088. for (; list; list = list->next) {
  1089. int end;
  1090. BUG_TRAP(start <= offset + len);
  1091. end = start + list->len;
  1092. if ((copy = end - offset) > 0) {
  1093. __wsum csum2;
  1094. if (copy > len)
  1095. copy = len;
  1096. csum2 = skb_checksum(list, offset - start,
  1097. copy, 0);
  1098. csum = csum_block_add(csum, csum2, pos);
  1099. if ((len -= copy) == 0)
  1100. return csum;
  1101. offset += copy;
  1102. pos += copy;
  1103. }
  1104. start = end;
  1105. }
  1106. }
  1107. BUG_ON(len);
  1108. return csum;
  1109. }
  1110. /* Both of above in one bottle. */
  1111. __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
  1112. u8 *to, int len, __wsum csum)
  1113. {
  1114. int start = skb_headlen(skb);
  1115. int i, copy = start - offset;
  1116. int pos = 0;
  1117. /* Copy header. */
  1118. if (copy > 0) {
  1119. if (copy > len)
  1120. copy = len;
  1121. csum = csum_partial_copy_nocheck(skb->data + offset, to,
  1122. copy, csum);
  1123. if ((len -= copy) == 0)
  1124. return csum;
  1125. offset += copy;
  1126. to += copy;
  1127. pos = copy;
  1128. }
  1129. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1130. int end;
  1131. BUG_TRAP(start <= offset + len);
  1132. end = start + skb_shinfo(skb)->frags[i].size;
  1133. if ((copy = end - offset) > 0) {
  1134. __wsum csum2;
  1135. u8 *vaddr;
  1136. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  1137. if (copy > len)
  1138. copy = len;
  1139. vaddr = kmap_skb_frag(frag);
  1140. csum2 = csum_partial_copy_nocheck(vaddr +
  1141. frag->page_offset +
  1142. offset - start, to,
  1143. copy, 0);
  1144. kunmap_skb_frag(vaddr);
  1145. csum = csum_block_add(csum, csum2, pos);
  1146. if (!(len -= copy))
  1147. return csum;
  1148. offset += copy;
  1149. to += copy;
  1150. pos += copy;
  1151. }
  1152. start = end;
  1153. }
  1154. if (skb_shinfo(skb)->frag_list) {
  1155. struct sk_buff *list = skb_shinfo(skb)->frag_list;
  1156. for (; list; list = list->next) {
  1157. __wsum csum2;
  1158. int end;
  1159. BUG_TRAP(start <= offset + len);
  1160. end = start + list->len;
  1161. if ((copy = end - offset) > 0) {
  1162. if (copy > len)
  1163. copy = len;
  1164. csum2 = skb_copy_and_csum_bits(list,
  1165. offset - start,
  1166. to, copy, 0);
  1167. csum = csum_block_add(csum, csum2, pos);
  1168. if ((len -= copy) == 0)
  1169. return csum;
  1170. offset += copy;
  1171. to += copy;
  1172. pos += copy;
  1173. }
  1174. start = end;
  1175. }
  1176. }
  1177. BUG_ON(len);
  1178. return csum;
  1179. }
  1180. void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
  1181. {
  1182. __wsum csum;
  1183. long csstart;
  1184. if (skb->ip_summed == CHECKSUM_PARTIAL)
  1185. csstart = skb->csum_start - skb_headroom(skb);
  1186. else
  1187. csstart = skb_headlen(skb);
  1188. BUG_ON(csstart > skb_headlen(skb));
  1189. skb_copy_from_linear_data(skb, to, csstart);
  1190. csum = 0;
  1191. if (csstart != skb->len)
  1192. csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
  1193. skb->len - csstart, 0);
  1194. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  1195. long csstuff = csstart + skb->csum_offset;
  1196. *((__sum16 *)(to + csstuff)) = csum_fold(csum);
  1197. }
  1198. }
  1199. /**
  1200. * skb_dequeue - remove from the head of the queue
  1201. * @list: list to dequeue from
  1202. *
  1203. * Remove the head of the list. The list lock is taken so the function
  1204. * may be used safely with other locking list functions. The head item is
  1205. * returned or %NULL if the list is empty.
  1206. */
  1207. struct sk_buff *skb_dequeue(struct sk_buff_head *list)
  1208. {
  1209. unsigned long flags;
  1210. struct sk_buff *result;
  1211. spin_lock_irqsave(&list->lock, flags);
  1212. result = __skb_dequeue(list);
  1213. spin_unlock_irqrestore(&list->lock, flags);
  1214. return result;
  1215. }
  1216. /**
  1217. * skb_dequeue_tail - remove from the tail of the queue
  1218. * @list: list to dequeue from
  1219. *
  1220. * Remove the tail of the list. The list lock is taken so the function
  1221. * may be used safely with other locking list functions. The tail item is
  1222. * returned or %NULL if the list is empty.
  1223. */
  1224. struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
  1225. {
  1226. unsigned long flags;
  1227. struct sk_buff *result;
  1228. spin_lock_irqsave(&list->lock, flags);
  1229. result = __skb_dequeue_tail(list);
  1230. spin_unlock_irqrestore(&list->lock, flags);
  1231. return result;
  1232. }
  1233. /**
  1234. * skb_queue_purge - empty a list
  1235. * @list: list to empty
  1236. *
  1237. * Delete all buffers on an &sk_buff list. Each buffer is removed from
  1238. * the list and one reference dropped. This function takes the list
  1239. * lock and is atomic with respect to other list locking functions.
  1240. */
  1241. void skb_queue_purge(struct sk_buff_head *list)
  1242. {
  1243. struct sk_buff *skb;
  1244. while ((skb = skb_dequeue(list)) != NULL)
  1245. kfree_skb(skb);
  1246. }
  1247. /**
  1248. * skb_queue_head - queue a buffer at the list head
  1249. * @list: list to use
  1250. * @newsk: buffer to queue
  1251. *
  1252. * Queue a buffer at the start of the list. This function takes the
  1253. * list lock and can be used safely with other locking &sk_buff functions
  1254. * safely.
  1255. *
  1256. * A buffer cannot be placed on two lists at the same time.
  1257. */
  1258. void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
  1259. {
  1260. unsigned long flags;
  1261. spin_lock_irqsave(&list->lock, flags);
  1262. __skb_queue_head(list, newsk);
  1263. spin_unlock_irqrestore(&list->lock, flags);
  1264. }
  1265. /**
  1266. * skb_queue_tail - queue a buffer at the list tail
  1267. * @list: list to use
  1268. * @newsk: buffer to queue
  1269. *
  1270. * Queue a buffer at the tail of the list. This function takes the
  1271. * list lock and can be used safely with other locking &sk_buff functions
  1272. * safely.
  1273. *
  1274. * A buffer cannot be placed on two lists at the same time.
  1275. */
  1276. void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
  1277. {
  1278. unsigned long flags;
  1279. spin_lock_irqsave(&list->lock, flags);
  1280. __skb_queue_tail(list, newsk);
  1281. spin_unlock_irqrestore(&list->lock, flags);
  1282. }
  1283. /**
  1284. * skb_unlink - remove a buffer from a list
  1285. * @skb: buffer to remove
  1286. * @list: list to use
  1287. *
  1288. * Remove a packet from a list. The list locks are taken and this
  1289. * function is atomic with respect to other list locked calls
  1290. *
  1291. * You must know what list the SKB is on.
  1292. */
  1293. void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
  1294. {
  1295. unsigned long flags;
  1296. spin_lock_irqsave(&list->lock, flags);
  1297. __skb_unlink(skb, list);
  1298. spin_unlock_irqrestore(&list->lock, flags);
  1299. }
  1300. /**
  1301. * skb_append - append a buffer
  1302. * @old: buffer to insert after
  1303. * @newsk: buffer to insert
  1304. * @list: list to use
  1305. *
  1306. * Place a packet after a given packet in a list. The list locks are taken
  1307. * and this function is atomic with respect to other list locked calls.
  1308. * A buffer cannot be placed on two lists at the same time.
  1309. */
  1310. void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
  1311. {
  1312. unsigned long flags;
  1313. spin_lock_irqsave(&list->lock, flags);
  1314. __skb_append(old, newsk, list);
  1315. spin_unlock_irqrestore(&list->lock, flags);
  1316. }
  1317. /**
  1318. * skb_insert - insert a buffer
  1319. * @old: buffer to insert before
  1320. * @newsk: buffer to insert
  1321. * @list: list to use
  1322. *
  1323. * Place a packet before a given packet in a list. The list locks are
  1324. * taken and this function is atomic with respect to other list locked
  1325. * calls.
  1326. *
  1327. * A buffer cannot be placed on two lists at the same time.
  1328. */
  1329. void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
  1330. {
  1331. unsigned long flags;
  1332. spin_lock_irqsave(&list->lock, flags);
  1333. __skb_insert(newsk, old->prev, old, list);
  1334. spin_unlock_irqrestore(&list->lock, flags);
  1335. }
  1336. static inline void skb_split_inside_header(struct sk_buff *skb,
  1337. struct sk_buff* skb1,
  1338. const u32 len, const int pos)
  1339. {
  1340. int i;
  1341. skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
  1342. pos - len);
  1343. /* And move data appendix as is. */
  1344. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
  1345. skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
  1346. skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
  1347. skb_shinfo(skb)->nr_frags = 0;
  1348. skb1->data_len = skb->data_len;
  1349. skb1->len += skb1->data_len;
  1350. skb->data_len = 0;
  1351. skb->len = len;
  1352. skb_set_tail_pointer(skb, len);
  1353. }
  1354. static inline void skb_split_no_header(struct sk_buff *skb,
  1355. struct sk_buff* skb1,
  1356. const u32 len, int pos)
  1357. {
  1358. int i, k = 0;
  1359. const int nfrags = skb_shinfo(skb)->nr_frags;
  1360. skb_shinfo(skb)->nr_frags = 0;
  1361. skb1->len = skb1->data_len = skb->len - len;
  1362. skb->len = len;
  1363. skb->data_len = len - pos;
  1364. for (i = 0; i < nfrags; i++) {
  1365. int size = skb_shinfo(skb)->frags[i].size;
  1366. if (pos + size > len) {
  1367. skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
  1368. if (pos < len) {
  1369. /* Split frag.
  1370. * We have two variants in this case:
  1371. * 1. Move all the frag to the second
  1372. * part, if it is possible. F.e.
  1373. * this approach is mandatory for TUX,
  1374. * where splitting is expensive.
  1375. * 2. Split is accurately. We make this.
  1376. */
  1377. get_page(skb_shinfo(skb)->frags[i].page);
  1378. skb_shinfo(skb1)->frags[0].page_offset += len - pos;
  1379. skb_shinfo(skb1)->frags[0].size -= len - pos;
  1380. skb_shinfo(skb)->frags[i].size = len - pos;
  1381. skb_shinfo(skb)->nr_frags++;
  1382. }
  1383. k++;
  1384. } else
  1385. skb_shinfo(skb)->nr_frags++;
  1386. pos += size;
  1387. }
  1388. skb_shinfo(skb1)->nr_frags = k;
  1389. }
  1390. /**
  1391. * skb_split - Split fragmented skb to two parts at length len.
  1392. * @skb: the buffer to split
  1393. * @skb1: the buffer to receive the second part
  1394. * @len: new length for skb
  1395. */
  1396. void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
  1397. {
  1398. int pos = skb_headlen(skb);
  1399. if (len < pos) /* Split line is inside header. */
  1400. skb_split_inside_header(skb, skb1, len, pos);
  1401. else /* Second chunk has no header, nothing to copy. */
  1402. skb_split_no_header(skb, skb1, len, pos);
  1403. }
  1404. /**
  1405. * skb_prepare_seq_read - Prepare a sequential read of skb data
  1406. * @skb: the buffer to read
  1407. * @from: lower offset of data to be read
  1408. * @to: upper offset of data to be read
  1409. * @st: state variable
  1410. *
  1411. * Initializes the specified state variable. Must be called before
  1412. * invoking skb_seq_read() for the first time.
  1413. */
  1414. void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
  1415. unsigned int to, struct skb_seq_state *st)
  1416. {
  1417. st->lower_offset = from;
  1418. st->upper_offset = to;
  1419. st->root_skb = st->cur_skb = skb;
  1420. st->frag_idx = st->stepped_offset = 0;
  1421. st->frag_data = NULL;
  1422. }
  1423. /**
  1424. * skb_seq_read - Sequentially read skb data
  1425. * @consumed: number of bytes consumed by the caller so far
  1426. * @data: destination pointer for data to be returned
  1427. * @st: state variable
  1428. *
  1429. * Reads a block of skb data at &consumed relative to the
  1430. * lower offset specified to skb_prepare_seq_read(). Assigns
  1431. * the head of the data block to &data and returns the length
  1432. * of the block or 0 if the end of the skb data or the upper
  1433. * offset has been reached.
  1434. *
  1435. * The caller is not required to consume all of the data
  1436. * returned, i.e. &consumed is typically set to the number
  1437. * of bytes already consumed and the next call to
  1438. * skb_seq_read() will return the remaining part of the block.
  1439. *
  1440. * Note: The size of each block of data returned can be arbitary,
  1441. * this limitation is the cost for zerocopy seqeuental
  1442. * reads of potentially non linear data.
  1443. *
  1444. * Note: Fragment lists within fragments are not implemented
  1445. * at the moment, state->root_skb could be replaced with
  1446. * a stack for this purpose.
  1447. */
  1448. unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
  1449. struct skb_seq_state *st)
  1450. {
  1451. unsigned int block_limit, abs_offset = consumed + st->lower_offset;
  1452. skb_frag_t *frag;
  1453. if (unlikely(abs_offset >= st->upper_offset))
  1454. return 0;
  1455. next_skb:
  1456. block_limit = skb_headlen(st->cur_skb);
  1457. if (abs_offset < block_limit) {
  1458. *data = st->cur_skb->data + abs_offset;
  1459. return block_limit - abs_offset;
  1460. }
  1461. if (st->frag_idx == 0 && !st->frag_data)
  1462. st->stepped_offset += skb_headlen(st->cur_skb);
  1463. while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
  1464. frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
  1465. block_limit = frag->size + st->stepped_offset;
  1466. if (abs_offset < block_limit) {
  1467. if (!st->frag_data)
  1468. st->frag_data = kmap_skb_frag(frag);
  1469. *data = (u8 *) st->frag_data + frag->page_offset +
  1470. (abs_offset - st->stepped_offset);
  1471. return block_limit - abs_offset;
  1472. }
  1473. if (st->frag_data) {
  1474. kunmap_skb_frag(st->frag_data);
  1475. st->frag_data = NULL;
  1476. }
  1477. st->frag_idx++;
  1478. st->stepped_offset += frag->size;
  1479. }
  1480. if (st->frag_data) {
  1481. kunmap_skb_frag(st->frag_data);
  1482. st->frag_data = NULL;
  1483. }
  1484. if (st->cur_skb->next) {
  1485. st->cur_skb = st->cur_skb->next;
  1486. st->frag_idx = 0;
  1487. goto next_skb;
  1488. } else if (st->root_skb == st->cur_skb &&
  1489. skb_shinfo(st->root_skb)->frag_list) {
  1490. st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
  1491. goto next_skb;
  1492. }
  1493. return 0;
  1494. }
  1495. /**
  1496. * skb_abort_seq_read - Abort a sequential read of skb data
  1497. * @st: state variable
  1498. *
  1499. * Must be called if skb_seq_read() was not called until it
  1500. * returned 0.
  1501. */
  1502. void skb_abort_seq_read(struct skb_seq_state *st)
  1503. {
  1504. if (st->frag_data)
  1505. kunmap_skb_frag(st->frag_data);
  1506. }
  1507. #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
  1508. static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
  1509. struct ts_config *conf,
  1510. struct ts_state *state)
  1511. {
  1512. return skb_seq_read(offset, text, TS_SKB_CB(state));
  1513. }
  1514. static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
  1515. {
  1516. skb_abort_seq_read(TS_SKB_CB(state));
  1517. }
  1518. /**
  1519. * skb_find_text - Find a text pattern in skb data
  1520. * @skb: the buffer to look in
  1521. * @from: search offset
  1522. * @to: search limit
  1523. * @config: textsearch configuration
  1524. * @state: uninitialized textsearch state variable
  1525. *
  1526. * Finds a pattern in the skb data according to the specified
  1527. * textsearch configuration. Use textsearch_next() to retrieve
  1528. * subsequent occurrences of the pattern. Returns the offset
  1529. * to the first occurrence or UINT_MAX if no match was found.
  1530. */
  1531. unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
  1532. unsigned int to, struct ts_config *config,
  1533. struct ts_state *state)
  1534. {
  1535. unsigned int ret;
  1536. config->get_next_block = skb_ts_get_next_block;
  1537. config->finish = skb_ts_finish;
  1538. skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
  1539. ret = textsearch_find(config, state);
  1540. return (ret <= to - from ? ret : UINT_MAX);
  1541. }
  1542. /**
  1543. * skb_append_datato_frags: - append the user data to a skb
  1544. * @sk: sock structure
  1545. * @skb: skb structure to be appened with user data.
  1546. * @getfrag: call back function to be used for getting the user data
  1547. * @from: pointer to user message iov
  1548. * @length: length of the iov message
  1549. *
  1550. * Description: This procedure append the user data in the fragment part
  1551. * of the skb if any page alloc fails user this procedure returns -ENOMEM
  1552. */
  1553. int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
  1554. int (*getfrag)(void *from, char *to, int offset,
  1555. int len, int odd, struct sk_buff *skb),
  1556. void *from, int length)
  1557. {
  1558. int frg_cnt = 0;
  1559. skb_frag_t *frag = NULL;
  1560. struct page *page = NULL;
  1561. int copy, left;
  1562. int offset = 0;
  1563. int ret;
  1564. do {
  1565. /* Return error if we don't have space for new frag */
  1566. frg_cnt = skb_shinfo(skb)->nr_frags;
  1567. if (frg_cnt >= MAX_SKB_FRAGS)
  1568. return -EFAULT;
  1569. /* allocate a new page for next frag */
  1570. page = alloc_pages(sk->sk_allocation, 0);
  1571. /* If alloc_page fails just return failure and caller will
  1572. * free previous allocated pages by doing kfree_skb()
  1573. */
  1574. if (page == NULL)
  1575. return -ENOMEM;
  1576. /* initialize the next frag */
  1577. sk->sk_sndmsg_page = page;
  1578. sk->sk_sndmsg_off = 0;
  1579. skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
  1580. skb->truesize += PAGE_SIZE;
  1581. atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
  1582. /* get the new initialized frag */
  1583. frg_cnt = skb_shinfo(skb)->nr_frags;
  1584. frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
  1585. /* copy the user data to page */
  1586. left = PAGE_SIZE - frag->page_offset;
  1587. copy = (length > left)? left : length;
  1588. ret = getfrag(from, (page_address(frag->page) +
  1589. frag->page_offset + frag->size),
  1590. offset, copy, 0, skb);
  1591. if (ret < 0)
  1592. return -EFAULT;
  1593. /* copy was successful so update the size parameters */
  1594. sk->sk_sndmsg_off += copy;
  1595. frag->size += copy;
  1596. skb->len += copy;
  1597. skb->data_len += copy;
  1598. offset += copy;
  1599. length -= copy;
  1600. } while (length > 0);
  1601. return 0;
  1602. }
  1603. /**
  1604. * skb_pull_rcsum - pull skb and update receive checksum
  1605. * @skb: buffer to update
  1606. * @start: start of data before pull
  1607. * @len: length of data pulled
  1608. *
  1609. * This function performs an skb_pull on the packet and updates
  1610. * update the CHECKSUM_COMPLETE checksum. It should be used on
  1611. * receive path processing instead of skb_pull unless you know
  1612. * that the checksum difference is zero (e.g., a valid IP header)
  1613. * or you are setting ip_summed to CHECKSUM_NONE.
  1614. */
  1615. unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
  1616. {
  1617. BUG_ON(len > skb->len);
  1618. skb->len -= len;
  1619. BUG_ON(skb->len < skb->data_len);
  1620. skb_postpull_rcsum(skb, skb->data, len);
  1621. return skb->data += len;
  1622. }
  1623. EXPORT_SYMBOL_GPL(skb_pull_rcsum);
  1624. /**
  1625. * skb_segment - Perform protocol segmentation on skb.
  1626. * @skb: buffer to segment
  1627. * @features: features for the output path (see dev->features)
  1628. *
  1629. * This function performs segmentation on the given skb. It returns
  1630. * the segment at the given position. It returns NULL if there are
  1631. * no more segments to generate, or when an error is encountered.
  1632. */
  1633. struct sk_buff *skb_segment(struct sk_buff *skb, int features)
  1634. {
  1635. struct sk_buff *segs = NULL;
  1636. struct sk_buff *tail = NULL;
  1637. unsigned int mss = skb_shinfo(skb)->gso_size;
  1638. unsigned int doffset = skb->data - skb_mac_header(skb);
  1639. unsigned int offset = doffset;
  1640. unsigned int headroom;
  1641. unsigned int len;
  1642. int sg = features & NETIF_F_SG;
  1643. int nfrags = skb_shinfo(skb)->nr_frags;
  1644. int err = -ENOMEM;
  1645. int i = 0;
  1646. int pos;
  1647. __skb_push(skb, doffset);
  1648. headroom = skb_headroom(skb);
  1649. pos = skb_headlen(skb);
  1650. do {
  1651. struct sk_buff *nskb;
  1652. skb_frag_t *frag;
  1653. int hsize;
  1654. int k;
  1655. int size;
  1656. len = skb->len - offset;
  1657. if (len > mss)
  1658. len = mss;
  1659. hsize = skb_headlen(skb) - offset;
  1660. if (hsize < 0)
  1661. hsize = 0;
  1662. if (hsize > len || !sg)
  1663. hsize = len;
  1664. nskb = alloc_skb(hsize + doffset + headroom, GFP_ATOMIC);
  1665. if (unlikely(!nskb))
  1666. goto err;
  1667. if (segs)
  1668. tail->next = nskb;
  1669. else
  1670. segs = nskb;
  1671. tail = nskb;
  1672. nskb->dev = skb->dev;
  1673. skb_copy_queue_mapping(nskb, skb);
  1674. nskb->priority = skb->priority;
  1675. nskb->protocol = skb->protocol;
  1676. nskb->dst = dst_clone(skb->dst);
  1677. memcpy(nskb->cb, skb->cb, sizeof(skb->cb));
  1678. nskb->pkt_type = skb->pkt_type;
  1679. nskb->mac_len = skb->mac_len;
  1680. skb_reserve(nskb, headroom);
  1681. skb_reset_mac_header(nskb);
  1682. skb_set_network_header(nskb, skb->mac_len);
  1683. nskb->transport_header = (nskb->network_header +
  1684. skb_network_header_len(skb));
  1685. skb_copy_from_linear_data(skb, skb_put(nskb, doffset),
  1686. doffset);
  1687. if (!sg) {
  1688. nskb->csum = skb_copy_and_csum_bits(skb, offset,
  1689. skb_put(nskb, len),
  1690. len, 0);
  1691. continue;
  1692. }
  1693. frag = skb_shinfo(nskb)->frags;
  1694. k = 0;
  1695. nskb->ip_summed = CHECKSUM_PARTIAL;
  1696. nskb->csum = skb->csum;
  1697. skb_copy_from_linear_data_offset(skb, offset,
  1698. skb_put(nskb, hsize), hsize);
  1699. while (pos < offset + len) {
  1700. BUG_ON(i >= nfrags);
  1701. *frag = skb_shinfo(skb)->frags[i];
  1702. get_page(frag->page);
  1703. size = frag->size;
  1704. if (pos < offset) {
  1705. frag->page_offset += offset - pos;
  1706. frag->size -= offset - pos;
  1707. }
  1708. k++;
  1709. if (pos + size <= offset + len) {
  1710. i++;
  1711. pos += size;
  1712. } else {
  1713. frag->size -= pos + size - (offset + len);
  1714. break;
  1715. }
  1716. frag++;
  1717. }
  1718. skb_shinfo(nskb)->nr_frags = k;
  1719. nskb->data_len = len - hsize;
  1720. nskb->len += nskb->data_len;
  1721. nskb->truesize += nskb->data_len;
  1722. } while ((offset += len) < skb->len);
  1723. return segs;
  1724. err:
  1725. while ((skb = segs)) {
  1726. segs = skb->next;
  1727. kfree_skb(skb);
  1728. }
  1729. return ERR_PTR(err);
  1730. }
  1731. EXPORT_SYMBOL_GPL(skb_segment);
  1732. void __init skb_init(void)
  1733. {
  1734. skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
  1735. sizeof(struct sk_buff),
  1736. 0,
  1737. SLAB_HWCACHE_ALIGN|SLAB_PANIC,
  1738. NULL);
  1739. skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
  1740. (2*sizeof(struct sk_buff)) +
  1741. sizeof(atomic_t),
  1742. 0,
  1743. SLAB_HWCACHE_ALIGN|SLAB_PANIC,
  1744. NULL);
  1745. }
  1746. /**
  1747. * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
  1748. * @skb: Socket buffer containing the buffers to be mapped
  1749. * @sg: The scatter-gather list to map into
  1750. * @offset: The offset into the buffer's contents to start mapping
  1751. * @len: Length of buffer space to be mapped
  1752. *
  1753. * Fill the specified scatter-gather list with mappings/pointers into a
  1754. * region of the buffer space attached to a socket buffer.
  1755. */
  1756. static int
  1757. __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
  1758. {
  1759. int start = skb_headlen(skb);
  1760. int i, copy = start - offset;
  1761. int elt = 0;
  1762. if (copy > 0) {
  1763. if (copy > len)
  1764. copy = len;
  1765. sg_set_buf(sg, skb->data + offset, copy);
  1766. elt++;
  1767. if ((len -= copy) == 0)
  1768. return elt;
  1769. offset += copy;
  1770. }
  1771. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1772. int end;
  1773. BUG_TRAP(start <= offset + len);
  1774. end = start + skb_shinfo(skb)->frags[i].size;
  1775. if ((copy = end - offset) > 0) {
  1776. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  1777. if (copy > len)
  1778. copy = len;
  1779. sg_set_page(&sg[elt], frag->page, copy,
  1780. frag->page_offset+offset-start);
  1781. elt++;
  1782. if (!(len -= copy))
  1783. return elt;
  1784. offset += copy;
  1785. }
  1786. start = end;
  1787. }
  1788. if (skb_shinfo(skb)->frag_list) {
  1789. struct sk_buff *list = skb_shinfo(skb)->frag_list;
  1790. for (; list; list = list->next) {
  1791. int end;
  1792. BUG_TRAP(start <= offset + len);
  1793. end = start + list->len;
  1794. if ((copy = end - offset) > 0) {
  1795. if (copy > len)
  1796. copy = len;
  1797. elt += __skb_to_sgvec(list, sg+elt, offset - start,
  1798. copy);
  1799. if ((len -= copy) == 0)
  1800. return elt;
  1801. offset += copy;
  1802. }
  1803. start = end;
  1804. }
  1805. }
  1806. BUG_ON(len);
  1807. return elt;
  1808. }
  1809. int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
  1810. {
  1811. int nsg = __skb_to_sgvec(skb, sg, offset, len);
  1812. sg_mark_end(&sg[nsg - 1]);
  1813. return nsg;
  1814. }
  1815. /**
  1816. * skb_cow_data - Check that a socket buffer's data buffers are writable
  1817. * @skb: The socket buffer to check.
  1818. * @tailbits: Amount of trailing space to be added
  1819. * @trailer: Returned pointer to the skb where the @tailbits space begins
  1820. *
  1821. * Make sure that the data buffers attached to a socket buffer are
  1822. * writable. If they are not, private copies are made of the data buffers
  1823. * and the socket buffer is set to use these instead.
  1824. *
  1825. * If @tailbits is given, make sure that there is space to write @tailbits
  1826. * bytes of data beyond current end of socket buffer. @trailer will be
  1827. * set to point to the skb in which this space begins.
  1828. *
  1829. * The number of scatterlist elements required to completely map the
  1830. * COW'd and extended socket buffer will be returned.
  1831. */
  1832. int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
  1833. {
  1834. int copyflag;
  1835. int elt;
  1836. struct sk_buff *skb1, **skb_p;
  1837. /* If skb is cloned or its head is paged, reallocate
  1838. * head pulling out all the pages (pages are considered not writable
  1839. * at the moment even if they are anonymous).
  1840. */
  1841. if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
  1842. __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
  1843. return -ENOMEM;
  1844. /* Easy case. Most of packets will go this way. */
  1845. if (!skb_shinfo(skb)->frag_list) {
  1846. /* A little of trouble, not enough of space for trailer.
  1847. * This should not happen, when stack is tuned to generate
  1848. * good frames. OK, on miss we reallocate and reserve even more
  1849. * space, 128 bytes is fair. */
  1850. if (skb_tailroom(skb) < tailbits &&
  1851. pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
  1852. return -ENOMEM;
  1853. /* Voila! */
  1854. *trailer = skb;
  1855. return 1;
  1856. }
  1857. /* Misery. We are in troubles, going to mincer fragments... */
  1858. elt = 1;
  1859. skb_p = &skb_shinfo(skb)->frag_list;
  1860. copyflag = 0;
  1861. while ((skb1 = *skb_p) != NULL) {
  1862. int ntail = 0;
  1863. /* The fragment is partially pulled by someone,
  1864. * this can happen on input. Copy it and everything
  1865. * after it. */
  1866. if (skb_shared(skb1))
  1867. copyflag = 1;
  1868. /* If the skb is the last, worry about trailer. */
  1869. if (skb1->next == NULL && tailbits) {
  1870. if (skb_shinfo(skb1)->nr_frags ||
  1871. skb_shinfo(skb1)->frag_list ||
  1872. skb_tailroom(skb1) < tailbits)
  1873. ntail = tailbits + 128;
  1874. }
  1875. if (copyflag ||
  1876. skb_cloned(skb1) ||
  1877. ntail ||
  1878. skb_shinfo(skb1)->nr_frags ||
  1879. skb_shinfo(skb1)->frag_list) {
  1880. struct sk_buff *skb2;
  1881. /* Fuck, we are miserable poor guys... */
  1882. if (ntail == 0)
  1883. skb2 = skb_copy(skb1, GFP_ATOMIC);
  1884. else
  1885. skb2 = skb_copy_expand(skb1,
  1886. skb_headroom(skb1),
  1887. ntail,
  1888. GFP_ATOMIC);
  1889. if (unlikely(skb2 == NULL))
  1890. return -ENOMEM;
  1891. if (skb1->sk)
  1892. skb_set_owner_w(skb2, skb1->sk);
  1893. /* Looking around. Are we still alive?
  1894. * OK, link new skb, drop old one */
  1895. skb2->next = skb1->next;
  1896. *skb_p = skb2;
  1897. kfree_skb(skb1);
  1898. skb1 = skb2;
  1899. }
  1900. elt++;
  1901. *trailer = skb1;
  1902. skb_p = &skb1->next;
  1903. }
  1904. return elt;
  1905. }
  1906. EXPORT_SYMBOL(___pskb_trim);
  1907. EXPORT_SYMBOL(__kfree_skb);
  1908. EXPORT_SYMBOL(kfree_skb);
  1909. EXPORT_SYMBOL(__pskb_pull_tail);
  1910. EXPORT_SYMBOL(__alloc_skb);
  1911. EXPORT_SYMBOL(__netdev_alloc_skb);
  1912. EXPORT_SYMBOL(pskb_copy);
  1913. EXPORT_SYMBOL(pskb_expand_head);
  1914. EXPORT_SYMBOL(skb_checksum);
  1915. EXPORT_SYMBOL(skb_clone);
  1916. EXPORT_SYMBOL(skb_copy);
  1917. EXPORT_SYMBOL(skb_copy_and_csum_bits);
  1918. EXPORT_SYMBOL(skb_copy_and_csum_dev);
  1919. EXPORT_SYMBOL(skb_copy_bits);
  1920. EXPORT_SYMBOL(skb_copy_expand);
  1921. EXPORT_SYMBOL(skb_over_panic);
  1922. EXPORT_SYMBOL(skb_pad);
  1923. EXPORT_SYMBOL(skb_realloc_headroom);
  1924. EXPORT_SYMBOL(skb_under_panic);
  1925. EXPORT_SYMBOL(skb_dequeue);
  1926. EXPORT_SYMBOL(skb_dequeue_tail);
  1927. EXPORT_SYMBOL(skb_insert);
  1928. EXPORT_SYMBOL(skb_queue_purge);
  1929. EXPORT_SYMBOL(skb_queue_head);
  1930. EXPORT_SYMBOL(skb_queue_tail);
  1931. EXPORT_SYMBOL(skb_unlink);
  1932. EXPORT_SYMBOL(skb_append);
  1933. EXPORT_SYMBOL(skb_split);
  1934. EXPORT_SYMBOL(skb_prepare_seq_read);
  1935. EXPORT_SYMBOL(skb_seq_read);
  1936. EXPORT_SYMBOL(skb_abort_seq_read);
  1937. EXPORT_SYMBOL(skb_find_text);
  1938. EXPORT_SYMBOL(skb_append_datato_frags);
  1939. EXPORT_SYMBOL_GPL(skb_to_sgvec);
  1940. EXPORT_SYMBOL_GPL(skb_cow_data);