skbuff.c 55 KB

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